blob: a1a75b8d5bb4470cc832f4d69e0409a06c6068b4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Jeff Brown98365d72012-08-19 20:30:52 -070042import android.hardware.display.DisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070043import android.hardware.display.DisplayManagerGlobal;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070044import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Handler;
46import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Parcel;
48import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070051import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070053import android.util.FloatProperty;
54import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070058import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070060import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080062import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070064import android.view.AccessibilityIterators.TextSegmentIterator;
65import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
66import android.view.AccessibilityIterators.WordTextSegmentIterator;
67import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.accessibility.AccessibilityEvent;
69import android.view.accessibility.AccessibilityEventSource;
70import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070071import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070072import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070074import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080075import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.view.inputmethod.InputConnection;
78import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.widget.ScrollBarDrawable;
80
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070082import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070083
Doug Feltcb3791202011-07-07 11:57:48 -070084import com.android.internal.R;
85import com.android.internal.util.Predicate;
86import com.android.internal.view.menu.MenuBuilder;
87
Christopher Tatea0374192010-10-05 13:06:41 -070088import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070089import java.lang.reflect.InvocationTargetException;
90import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.ArrayList;
92import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070093import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080094import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070095import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97/**
98 * <p>
99 * This class represents the basic building block for user interface components. A View
100 * occupies a rectangular area on the screen and is responsible for drawing and
101 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700102 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
104 * are invisible containers that hold other Views (or other ViewGroups) and define
105 * their layout properties.
106 * </p>
107 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700108 * <div class="special reference">
109 * <h3>Developer Guides</h3>
110 * <p>For information about using this class to develop your application's user interface,
111 * 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 -0800112 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * <a name="Using"></a>
115 * <h3>Using Views</h3>
116 * <p>
117 * All of the views in a window are arranged in a single tree. You can add views
118 * either from code or by specifying a tree of views in one or more XML layout
119 * files. There are many specialized subclasses of views that act as controls or
120 * are capable of displaying text, images, or other content.
121 * </p>
122 * <p>
123 * Once you have created a tree of views, there are typically a few types of
124 * common operations you may wish to perform:
125 * <ul>
126 * <li><strong>Set properties:</strong> for example setting the text of a
127 * {@link android.widget.TextView}. The available properties and the methods
128 * that set them will vary among the different subclasses of views. Note that
129 * properties that are known at build time can be set in the XML layout
130 * files.</li>
131 * <li><strong>Set focus:</strong> The framework will handled moving focus in
132 * response to user input. To force focus to a specific view, call
133 * {@link #requestFocus}.</li>
134 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
135 * that will be notified when something interesting happens to the view. For
136 * example, all views will let you set a listener to be notified when the view
137 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700138 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700139 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700140 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700142 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * </ul>
144 * </p>
145 * <p><em>
146 * Note: The Android framework is responsible for measuring, laying out and
147 * drawing views. You should not call methods that perform these actions on
148 * views yourself unless you are actually implementing a
149 * {@link android.view.ViewGroup}.
150 * </em></p>
151 *
152 * <a name="Lifecycle"></a>
153 * <h3>Implementing a Custom View</h3>
154 *
155 * <p>
156 * To implement a custom view, you will usually begin by providing overrides for
157 * some of the standard methods that the framework calls on all views. You do
158 * not need to override all of these methods. In fact, you can start by just
159 * overriding {@link #onDraw(android.graphics.Canvas)}.
160 * <table border="2" width="85%" align="center" cellpadding="5">
161 * <thead>
162 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
163 * </thead>
164 *
165 * <tbody>
166 * <tr>
167 * <td rowspan="2">Creation</td>
168 * <td>Constructors</td>
169 * <td>There is a form of the constructor that are called when the view
170 * is created from code and a form that is called when the view is
171 * inflated from a layout file. The second form should parse and apply
172 * any attributes defined in the layout file.
173 * </td>
174 * </tr>
175 * <tr>
176 * <td><code>{@link #onFinishInflate()}</code></td>
177 * <td>Called after a view and all of its children has been inflated
178 * from XML.</td>
179 * </tr>
180 *
181 * <tr>
182 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700183 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <td>Called to determine the size requirements for this view and all
185 * of its children.
186 * </td>
187 * </tr>
188 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700189 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * <td>Called when this view should assign a size and position to all
191 * of its children.
192 * </td>
193 * </tr>
194 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700195 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * <td>Called when the size of this view has changed.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700202 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * <td>Called when the view should render its content.
204 * </td>
205 * </tr>
206 *
207 * <tr>
208 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900210 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900215 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * </td>
217 * </tr>
218 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700219 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 * <td>Called when a trackball motion event occurs.
221 * </td>
222 * </tr>
223 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when a touch screen motion event occurs.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700231 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * <td>Called when the view gains or loses focus.
233 * </td>
234 * </tr>
235 *
236 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700237 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 * <td>Called when the window containing the view gains or loses focus.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td rowspan="3">Attaching</td>
244 * <td><code>{@link #onAttachedToWindow()}</code></td>
245 * <td>Called when the view is attached to a window.
246 * </td>
247 * </tr>
248 *
249 * <tr>
250 * <td><code>{@link #onDetachedFromWindow}</code></td>
251 * <td>Called when the view is detached from its window.
252 * </td>
253 * </tr>
254 *
255 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700256 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * <td>Called when the visibility of the window containing the view
258 * has changed.
259 * </td>
260 * </tr>
261 * </tbody>
262 *
263 * </table>
264 * </p>
265 *
266 * <a name="IDs"></a>
267 * <h3>IDs</h3>
268 * Views may have an integer id associated with them. These ids are typically
269 * assigned in the layout XML files, and are used to find specific views within
270 * the view tree. A common pattern is to:
271 * <ul>
272 * <li>Define a Button in the layout file and assign it a unique ID.
273 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700274 * &lt;Button
275 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * android:layout_width="wrap_content"
277 * android:layout_height="wrap_content"
278 * android:text="@string/my_button_text"/&gt;
279 * </pre></li>
280 * <li>From the onCreate method of an Activity, find the Button
281 * <pre class="prettyprint">
282 * Button myButton = (Button) findViewById(R.id.my_button);
283 * </pre></li>
284 * </ul>
285 * <p>
286 * View IDs need not be unique throughout the tree, but it is good practice to
287 * ensure that they are at least unique within the part of the tree you are
288 * searching.
289 * </p>
290 *
291 * <a name="Position"></a>
292 * <h3>Position</h3>
293 * <p>
294 * The geometry of a view is that of a rectangle. A view has a location,
295 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
296 * two dimensions, expressed as a width and a height. The unit for location
297 * and dimensions is the pixel.
298 * </p>
299 *
300 * <p>
301 * It is possible to retrieve the location of a view by invoking the methods
302 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
303 * coordinate of the rectangle representing the view. The latter returns the
304 * top, or Y, coordinate of the rectangle representing the view. These methods
305 * both return the location of the view relative to its parent. For instance,
306 * when getLeft() returns 20, that means the view is located 20 pixels to the
307 * right of the left edge of its direct parent.
308 * </p>
309 *
310 * <p>
311 * In addition, several convenience methods are offered to avoid unnecessary
312 * computations, namely {@link #getRight()} and {@link #getBottom()}.
313 * These methods return the coordinates of the right and bottom edges of the
314 * rectangle representing the view. For instance, calling {@link #getRight()}
315 * is similar to the following computation: <code>getLeft() + getWidth()</code>
316 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
317 * </p>
318 *
319 * <a name="SizePaddingMargins"></a>
320 * <h3>Size, padding and margins</h3>
321 * <p>
322 * The size of a view is expressed with a width and a height. A view actually
323 * possess two pairs of width and height values.
324 * </p>
325 *
326 * <p>
327 * The first pair is known as <em>measured width</em> and
328 * <em>measured height</em>. These dimensions define how big a view wants to be
329 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
330 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
331 * and {@link #getMeasuredHeight()}.
332 * </p>
333 *
334 * <p>
335 * The second pair is simply known as <em>width</em> and <em>height</em>, or
336 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
337 * dimensions define the actual size of the view on screen, at drawing time and
338 * after layout. These values may, but do not have to, be different from the
339 * measured width and height. The width and height can be obtained by calling
340 * {@link #getWidth()} and {@link #getHeight()}.
341 * </p>
342 *
343 * <p>
344 * To measure its dimensions, a view takes into account its padding. The padding
345 * is expressed in pixels for the left, top, right and bottom parts of the view.
346 * Padding can be used to offset the content of the view by a specific amount of
347 * pixels. For instance, a left padding of 2 will push the view's content by
348 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700349 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
350 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
351 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
352 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 * </p>
354 *
355 * <p>
356 * Even though a view can define a padding, it does not provide any support for
357 * margins. However, view groups provide such a support. Refer to
358 * {@link android.view.ViewGroup} and
359 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
360 * </p>
361 *
362 * <a name="Layout"></a>
363 * <h3>Layout</h3>
364 * <p>
365 * Layout is a two pass process: a measure pass and a layout pass. The measuring
366 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
367 * of the view tree. Each view pushes dimension specifications down the tree
368 * during the recursion. At the end of the measure pass, every view has stored
369 * its measurements. The second pass happens in
370 * {@link #layout(int,int,int,int)} and is also top-down. During
371 * this pass each parent is responsible for positioning all of its children
372 * using the sizes computed in the measure pass.
373 * </p>
374 *
375 * <p>
376 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
377 * {@link #getMeasuredHeight()} values must be set, along with those for all of
378 * that view's descendants. A view's measured width and measured height values
379 * must respect the constraints imposed by the view's parents. This guarantees
380 * that at the end of the measure pass, all parents accept all of their
381 * children's measurements. A parent view may call measure() more than once on
382 * its children. For example, the parent may measure each child once with
383 * unspecified dimensions to find out how big they want to be, then call
384 * measure() on them again with actual numbers if the sum of all the children's
385 * unconstrained sizes is too big or too small.
386 * </p>
387 *
388 * <p>
389 * The measure pass uses two classes to communicate dimensions. The
390 * {@link MeasureSpec} class is used by views to tell their parents how they
391 * want to be measured and positioned. The base LayoutParams class just
392 * describes how big the view wants to be for both width and height. For each
393 * dimension, it can specify one of:
394 * <ul>
395 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800396 * <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 -0800397 * (minus padding)
398 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
399 * enclose its content (plus padding).
400 * </ul>
401 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
402 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
403 * an X and Y value.
404 * </p>
405 *
406 * <p>
407 * MeasureSpecs are used to push requirements down the tree from parent to
408 * child. A MeasureSpec can be in one of three modes:
409 * <ul>
410 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
411 * of a child view. For example, a LinearLayout may call measure() on its child
412 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
413 * tall the child view wants to be given a width of 240 pixels.
414 * <li>EXACTLY: This is used by the parent to impose an exact size on the
415 * child. The child must use this size, and guarantee that all of its
416 * descendants will fit within this size.
417 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
418 * child. The child must gurantee that it and all of its descendants will fit
419 * within this size.
420 * </ul>
421 * </p>
422 *
423 * <p>
424 * To intiate a layout, call {@link #requestLayout}. This method is typically
425 * called by a view on itself when it believes that is can no longer fit within
426 * its current bounds.
427 * </p>
428 *
429 * <a name="Drawing"></a>
430 * <h3>Drawing</h3>
431 * <p>
432 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700433 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * this means that parents will draw before (i.e., behind) their children, with
435 * siblings drawn in the order they appear in the tree.
436 * If you set a background drawable for a View, then the View will draw it for you
437 * before calling back to its <code>onDraw()</code> method.
438 * </p>
439 *
440 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700441 * 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 -0800442 * </p>
443 *
444 * <p>
445 * To force a view to draw, call {@link #invalidate()}.
446 * </p>
447 *
448 * <a name="EventHandlingThreading"></a>
449 * <h3>Event Handling and Threading</h3>
450 * <p>
451 * The basic cycle of a view is as follows:
452 * <ol>
453 * <li>An event comes in and is dispatched to the appropriate view. The view
454 * handles the event and notifies any listeners.</li>
455 * <li>If in the course of processing the event, the view's bounds may need
456 * to be changed, the view will call {@link #requestLayout()}.</li>
457 * <li>Similarly, if in the course of processing the event the view's appearance
458 * may need to be changed, the view will call {@link #invalidate()}.</li>
459 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
460 * the framework will take care of measuring, laying out, and drawing the tree
461 * as appropriate.</li>
462 * </ol>
463 * </p>
464 *
465 * <p><em>Note: The entire view tree is single threaded. You must always be on
466 * the UI thread when calling any method on any view.</em>
467 * If you are doing work on other threads and want to update the state of a view
468 * from that thread, you should use a {@link Handler}.
469 * </p>
470 *
471 * <a name="FocusHandling"></a>
472 * <h3>Focus Handling</h3>
473 * <p>
474 * The framework will handle routine focus movement in response to user input.
475 * This includes changing the focus as views are removed or hidden, or as new
476 * views become available. Views indicate their willingness to take focus
477 * through the {@link #isFocusable} method. To change whether a view can take
478 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
479 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
480 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
481 * </p>
482 * <p>
483 * Focus movement is based on an algorithm which finds the nearest neighbor in a
484 * given direction. In rare cases, the default algorithm may not match the
485 * intended behavior of the developer. In these situations, you can provide
486 * explicit overrides by using these XML attributes in the layout file:
487 * <pre>
488 * nextFocusDown
489 * nextFocusLeft
490 * nextFocusRight
491 * nextFocusUp
492 * </pre>
493 * </p>
494 *
495 *
496 * <p>
497 * To get a particular view to take focus, call {@link #requestFocus()}.
498 * </p>
499 *
500 * <a name="TouchMode"></a>
501 * <h3>Touch Mode</h3>
502 * <p>
503 * When a user is navigating a user interface via directional keys such as a D-pad, it is
504 * necessary to give focus to actionable items such as buttons so the user can see
505 * what will take input. If the device has touch capabilities, however, and the user
506 * begins interacting with the interface by touching it, it is no longer necessary to
507 * always highlight, or give focus to, a particular view. This motivates a mode
508 * for interaction named 'touch mode'.
509 * </p>
510 * <p>
511 * For a touch capable device, once the user touches the screen, the device
512 * will enter touch mode. From this point onward, only views for which
513 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
514 * Other views that are touchable, like buttons, will not take focus when touched; they will
515 * only fire the on click listeners.
516 * </p>
517 * <p>
518 * Any time a user hits a directional key, such as a D-pad direction, the view device will
519 * exit touch mode, and find a view to take focus, so that the user may resume interacting
520 * with the user interface without touching the screen again.
521 * </p>
522 * <p>
523 * The touch mode state is maintained across {@link android.app.Activity}s. Call
524 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
525 * </p>
526 *
527 * <a name="Scrolling"></a>
528 * <h3>Scrolling</h3>
529 * <p>
530 * The framework provides basic support for views that wish to internally
531 * scroll their content. This includes keeping track of the X and Y scroll
532 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800533 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700534 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 * </p>
536 *
537 * <a name="Tags"></a>
538 * <h3>Tags</h3>
539 * <p>
540 * Unlike IDs, tags are not used to identify views. Tags are essentially an
541 * extra piece of information that can be associated with a view. They are most
542 * often used as a convenience to store data related to views in the views
543 * themselves rather than by putting them in a separate structure.
544 * </p>
545 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700546 * <a name="Properties"></a>
547 * <h3>Properties</h3>
548 * <p>
549 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
550 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
551 * available both in the {@link Property} form as well as in similarly-named setter/getter
552 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
553 * be used to set persistent state associated with these rendering-related properties on the view.
554 * The properties and methods can also be used in conjunction with
555 * {@link android.animation.Animator Animator}-based animations, described more in the
556 * <a href="#Animation">Animation</a> section.
557 * </p>
558 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 * <a name="Animation"></a>
560 * <h3>Animation</h3>
561 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700562 * Starting with Android 3.0, the preferred way of animating views is to use the
563 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
564 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
565 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
566 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
567 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
568 * makes animating these View properties particularly easy and efficient.
569 * </p>
570 * <p>
571 * 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 -0800572 * You can attach an {@link Animation} object to a view using
573 * {@link #setAnimation(Animation)} or
574 * {@link #startAnimation(Animation)}. The animation can alter the scale,
575 * rotation, translation and alpha of a view over time. If the animation is
576 * attached to a view that has children, the animation will affect the entire
577 * subtree rooted by that node. When an animation is started, the framework will
578 * take care of redrawing the appropriate views until the animation completes.
579 * </p>
580 *
Jeff Brown85a31762010-09-01 17:01:00 -0700581 * <a name="Security"></a>
582 * <h3>Security</h3>
583 * <p>
584 * Sometimes it is essential that an application be able to verify that an action
585 * is being performed with the full knowledge and consent of the user, such as
586 * granting a permission request, making a purchase or clicking on an advertisement.
587 * Unfortunately, a malicious application could try to spoof the user into
588 * performing these actions, unaware, by concealing the intended purpose of the view.
589 * As a remedy, the framework offers a touch filtering mechanism that can be used to
590 * improve the security of views that provide access to sensitive functionality.
591 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700592 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800593 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700594 * will discard touches that are received whenever the view's window is obscured by
595 * another visible window. As a result, the view will not receive touches whenever a
596 * toast, dialog or other window appears above the view's window.
597 * </p><p>
598 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700599 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
600 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700601 * </p>
602 *
Romain Guy171c5922011-01-06 10:04:23 -0800603 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700604 * @attr ref android.R.styleable#View_background
605 * @attr ref android.R.styleable#View_clickable
606 * @attr ref android.R.styleable#View_contentDescription
607 * @attr ref android.R.styleable#View_drawingCacheQuality
608 * @attr ref android.R.styleable#View_duplicateParentState
609 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700610 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700611 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700613 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700615 * @attr ref android.R.styleable#View_isScrollContainer
616 * @attr ref android.R.styleable#View_focusable
617 * @attr ref android.R.styleable#View_focusableInTouchMode
618 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
619 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800620 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700621 * @attr ref android.R.styleable#View_longClickable
622 * @attr ref android.R.styleable#View_minHeight
623 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * @attr ref android.R.styleable#View_nextFocusDown
625 * @attr ref android.R.styleable#View_nextFocusLeft
626 * @attr ref android.R.styleable#View_nextFocusRight
627 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700628 * @attr ref android.R.styleable#View_onClick
629 * @attr ref android.R.styleable#View_padding
630 * @attr ref android.R.styleable#View_paddingBottom
631 * @attr ref android.R.styleable#View_paddingLeft
632 * @attr ref android.R.styleable#View_paddingRight
633 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800634 * @attr ref android.R.styleable#View_paddingStart
635 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700636 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800637 * @attr ref android.R.styleable#View_rotation
638 * @attr ref android.R.styleable#View_rotationX
639 * @attr ref android.R.styleable#View_rotationY
640 * @attr ref android.R.styleable#View_scaleX
641 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 * @attr ref android.R.styleable#View_scrollX
643 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700644 * @attr ref android.R.styleable#View_scrollbarSize
645 * @attr ref android.R.styleable#View_scrollbarStyle
646 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700647 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
648 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
650 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 * @attr ref android.R.styleable#View_scrollbarThumbVertical
652 * @attr ref android.R.styleable#View_scrollbarTrackVertical
653 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
654 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_soundEffectsEnabled
656 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700657 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800658 * @attr ref android.R.styleable#View_transformPivotX
659 * @attr ref android.R.styleable#View_transformPivotY
660 * @attr ref android.R.styleable#View_translationX
661 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700662 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 *
664 * @see android.view.ViewGroup
665 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700666public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700667 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 private static final boolean DBG = false;
669
670 /**
671 * The logging tag used by this class with android.util.Log.
672 */
673 protected static final String VIEW_LOG_TAG = "View";
674
675 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700676 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700677 *
678 * @hide
679 */
680 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
681
682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 * Used to mark a View that has no ID.
684 */
685 public static final int NO_ID = -1;
686
687 /**
688 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
689 * calling setFlags.
690 */
691 private static final int NOT_FOCUSABLE = 0x00000000;
692
693 /**
694 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
695 * setFlags.
696 */
697 private static final int FOCUSABLE = 0x00000001;
698
699 /**
700 * Mask for use with setFlags indicating bits used for focus.
701 */
702 private static final int FOCUSABLE_MASK = 0x00000001;
703
704 /**
705 * This view will adjust its padding to fit sytem windows (e.g. status bar)
706 */
707 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
708
709 /**
Scott Main812634c22011-07-27 13:22:35 -0700710 * This view is visible.
711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int VISIBLE = 0x00000000;
715
716 /**
717 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700718 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int INVISIBLE = 0x00000004;
722
723 /**
724 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700725 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
726 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 */
728 public static final int GONE = 0x00000008;
729
730 /**
731 * Mask for use with setFlags indicating bits used for visibility.
732 * {@hide}
733 */
734 static final int VISIBILITY_MASK = 0x0000000C;
735
736 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int ENABLED = 0x00000000;
744
745 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700746 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 * Use with ENABLED_MASK when calling setFlags.
748 * {@hide}
749 */
750 static final int DISABLED = 0x00000020;
751
752 /**
753 * Mask for use with setFlags indicating bits used for indicating whether
754 * this view is enabled
755 * {@hide}
756 */
757 static final int ENABLED_MASK = 0x00000020;
758
759 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700760 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
761 * called and further optimizations will be performed. It is okay to have
762 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 * {@hide}
764 */
765 static final int WILL_NOT_DRAW = 0x00000080;
766
767 /**
768 * Mask for use with setFlags indicating bits used for indicating whether
769 * this view is will draw
770 * {@hide}
771 */
772 static final int DRAW_MASK = 0x00000080;
773
774 /**
775 * <p>This view doesn't show scrollbars.</p>
776 * {@hide}
777 */
778 static final int SCROLLBARS_NONE = 0x00000000;
779
780 /**
781 * <p>This view shows horizontal scrollbars.</p>
782 * {@hide}
783 */
784 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
785
786 /**
787 * <p>This view shows vertical scrollbars.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_VERTICAL = 0x00000200;
791
792 /**
793 * <p>Mask for use with setFlags indicating bits used for indicating which
794 * scrollbars are enabled.</p>
795 * {@hide}
796 */
797 static final int SCROLLBARS_MASK = 0x00000300;
798
Jeff Brown85a31762010-09-01 17:01:00 -0700799 /**
800 * Indicates that the view should filter touches when its window is obscured.
801 * Refer to the class comments for more information about this security feature.
802 * {@hide}
803 */
804 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
805
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700806 /**
807 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
808 * that they are optional and should be skipped if the window has
809 * requested system UI flags that ignore those insets for layout.
810 */
811 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812
813 /**
814 * <p>This view doesn't show fading edges.</p>
815 * {@hide}
816 */
817 static final int FADING_EDGE_NONE = 0x00000000;
818
819 /**
820 * <p>This view shows horizontal fading edges.</p>
821 * {@hide}
822 */
823 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
824
825 /**
826 * <p>This view shows vertical fading edges.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_VERTICAL = 0x00002000;
830
831 /**
832 * <p>Mask for use with setFlags indicating bits used for indicating which
833 * fading edges are enabled.</p>
834 * {@hide}
835 */
836 static final int FADING_EDGE_MASK = 0x00003000;
837
838 /**
839 * <p>Indicates this view can be clicked. When clickable, a View reacts
840 * to clicks by notifying the OnClickListener.<p>
841 * {@hide}
842 */
843 static final int CLICKABLE = 0x00004000;
844
845 /**
846 * <p>Indicates this view is caching its drawing into a bitmap.</p>
847 * {@hide}
848 */
849 static final int DRAWING_CACHE_ENABLED = 0x00008000;
850
851 /**
852 * <p>Indicates that no icicle should be saved for this view.<p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED = 0x000010000;
856
857 /**
858 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
859 * property.</p>
860 * {@hide}
861 */
862 static final int SAVE_DISABLED_MASK = 0x000010000;
863
864 /**
865 * <p>Indicates that no drawing cache should ever be created for this view.<p>
866 * {@hide}
867 */
868 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
869
870 /**
871 * <p>Indicates this view can take / keep focus when int touch mode.</p>
872 * {@hide}
873 */
874 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
875
876 /**
877 * <p>Enables low quality mode for the drawing cache.</p>
878 */
879 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
880
881 /**
882 * <p>Enables high quality mode for the drawing cache.</p>
883 */
884 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
885
886 /**
887 * <p>Enables automatic quality mode for the drawing cache.</p>
888 */
889 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
890
891 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
892 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
893 };
894
895 /**
896 * <p>Mask for use with setFlags indicating bits used for the cache
897 * quality property.</p>
898 * {@hide}
899 */
900 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
901
902 /**
903 * <p>
904 * Indicates this view can be long clicked. When long clickable, a View
905 * reacts to long clicks by notifying the OnLongClickListener or showing a
906 * context menu.
907 * </p>
908 * {@hide}
909 */
910 static final int LONG_CLICKABLE = 0x00200000;
911
912 /**
913 * <p>Indicates that this view gets its drawable states from its direct parent
914 * and ignores its original internal states.</p>
915 *
916 * @hide
917 */
918 static final int DUPLICATE_PARENT_STATE = 0x00400000;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the content area,
922 * without increasing the padding. The scrollbars will be overlaid with
923 * translucency on the view's content.
924 */
925 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
926
927 /**
928 * The scrollbar style to display the scrollbars inside the padded area,
929 * increasing the padding of the view. The scrollbars will not overlap the
930 * content area of the view.
931 */
932 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * without increasing the padding. The scrollbars will be overlaid with
937 * translucency.
938 */
939 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
940
941 /**
942 * The scrollbar style to display the scrollbars at the edge of the view,
943 * increasing the padding of the view. The scrollbars will only overlap the
944 * background, if any.
945 */
946 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
947
948 /**
949 * Mask to check if the scrollbar style is overlay or inset.
950 * {@hide}
951 */
952 static final int SCROLLBARS_INSET_MASK = 0x01000000;
953
954 /**
955 * Mask to check if the scrollbar style is inside or outside.
956 * {@hide}
957 */
958 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
959
960 /**
961 * Mask for scrollbar style.
962 * {@hide}
963 */
964 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
965
966 /**
967 * View flag indicating that the screen should remain on while the
968 * window containing this view is visible to the user. This effectively
969 * takes care of automatically setting the WindowManager's
970 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
971 */
972 public static final int KEEP_SCREEN_ON = 0x04000000;
973
974 /**
975 * View flag indicating whether this view should have sound effects enabled
976 * for events such as clicking and touching.
977 */
978 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
979
980 /**
981 * View flag indicating whether this view should have haptic feedback
982 * enabled for events such as long presses.
983 */
984 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
985
986 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700987 * <p>Indicates that the view hierarchy should stop saving state when
988 * it reaches this view. If state saving is initiated immediately at
989 * the view, it will be allowed.
990 * {@hide}
991 */
992 static final int PARENT_SAVE_DISABLED = 0x20000000;
993
994 /**
995 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
996 * {@hide}
997 */
998 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
999
1000 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001001 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1002 * should add all focusable Views regardless if they are focusable in touch mode.
1003 */
1004 public static final int FOCUSABLES_ALL = 0x00000000;
1005
1006 /**
1007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add only Views focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 * item.
1015 */
1016 public static final int FOCUS_BACKWARD = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_FORWARD = 0x00000002;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 */
1027 public static final int FOCUS_LEFT = 0x00000011;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_UP = 0x00000021;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_RIGHT = 0x00000042;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_DOWN = 0x00000082;
1043
Svetoslav Ganov42138042012-03-20 11:51:39 -07001044 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001045 * Bits of {@link #getMeasuredWidthAndState()} and
1046 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1047 */
1048 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1049
1050 /**
1051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1053 */
1054 public static final int MEASURED_STATE_MASK = 0xff000000;
1055
1056 /**
1057 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1058 * for functions that combine both width and height into a single int,
1059 * such as {@link #getMeasuredState()} and the childState argument of
1060 * {@link #resolveSizeAndState(int, int, int)}.
1061 */
1062 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1063
1064 /**
1065 * Bit of {@link #getMeasuredWidthAndState()} and
1066 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1067 * is smaller that the space the view would like to have.
1068 */
1069 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1070
1071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 * Base View state sets
1073 */
1074 // Singles
1075 /**
1076 * Indicates the view has no states set. States are used with
1077 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1078 * view depending on its state.
1079 *
1080 * @see android.graphics.drawable.Drawable
1081 * @see #getDrawableState()
1082 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001083 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 /**
1085 * Indicates the view is enabled. States are used with
1086 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1087 * view depending on its state.
1088 *
1089 * @see android.graphics.drawable.Drawable
1090 * @see #getDrawableState()
1091 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001092 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 /**
1094 * Indicates the view is focused. States are used with
1095 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1096 * view depending on its state.
1097 *
1098 * @see android.graphics.drawable.Drawable
1099 * @see #getDrawableState()
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is selected. States are used with
1104 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1105 * view depending on its state.
1106 *
1107 * @see android.graphics.drawable.Drawable
1108 * @see #getDrawableState()
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view is pressed. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 * @hide
1119 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001120 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /**
1122 * Indicates the view's window has focus. States are used with
1123 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1124 * view depending on its state.
1125 *
1126 * @see android.graphics.drawable.Drawable
1127 * @see #getDrawableState()
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 // Doubles
1131 /**
1132 * Indicates the view is enabled and has the focus.
1133 *
1134 * @see #ENABLED_STATE_SET
1135 * @see #FOCUSED_STATE_SET
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 /**
1139 * Indicates the view is enabled and selected.
1140 *
1141 * @see #ENABLED_STATE_SET
1142 * @see #SELECTED_STATE_SET
1143 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001144 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 /**
1146 * Indicates the view is enabled and that its window has focus.
1147 *
1148 * @see #ENABLED_STATE_SET
1149 * @see #WINDOW_FOCUSED_STATE_SET
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 /**
1153 * Indicates the view is focused and selected.
1154 *
1155 * @see #FOCUSED_STATE_SET
1156 * @see #SELECTED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view has the focus and that its window has the focus.
1161 *
1162 * @see #FOCUSED_STATE_SET
1163 * @see #WINDOW_FOCUSED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is selected and that its window has the focus.
1168 *
1169 * @see #SELECTED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 // Triples
1174 /**
1175 * Indicates the view is enabled, focused and selected.
1176 *
1177 * @see #ENABLED_STATE_SET
1178 * @see #FOCUSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is enabled, focused and its window has the focus.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #WINDOW_FOCUSED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, selected and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #SELECTED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is focused, selected and its window has the focus.
1200 *
1201 * @see #FOCUSED_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[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is enabled, focused, selected and its window
1208 * has the focus.
1209 *
1210 * @see #ENABLED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 * @see #SELECTED_STATE_SET
1213 * @see #WINDOW_FOCUSED_STATE_SET
1214 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001215 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 /**
1217 * Indicates the view is pressed and its window has the focus.
1218 *
1219 * @see #PRESSED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and selected.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #SELECTED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, selected and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #SELECTED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed and focused.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed, focused and its window has the focus.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #FOCUSED_STATE_SET
1250 * @see #WINDOW_FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and selected.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #SELECTED_STATE_SET
1258 * @see #FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused, selected and its window has the focus.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 * @see #WINDOW_FOCUSED_STATE_SET
1268 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001269 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 /**
1271 * Indicates the view is pressed and enabled.
1272 *
1273 * @see #PRESSED_STATE_SET
1274 * @see #ENABLED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed, enabled and its window has the focus.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 * @see #WINDOW_FOCUSED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and selected.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #SELECTED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled, selected and its window has the
1295 * focus.
1296 *
1297 * @see #PRESSED_STATE_SET
1298 * @see #ENABLED_STATE_SET
1299 * @see #SELECTED_STATE_SET
1300 * @see #WINDOW_FOCUSED_STATE_SET
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 /**
1304 * Indicates the view is pressed, enabled and focused.
1305 *
1306 * @see #PRESSED_STATE_SET
1307 * @see #ENABLED_STATE_SET
1308 * @see #FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled, focused and its window has the
1313 * focus.
1314 *
1315 * @see #PRESSED_STATE_SET
1316 * @see #ENABLED_STATE_SET
1317 * @see #FOCUSED_STATE_SET
1318 * @see #WINDOW_FOCUSED_STATE_SET
1319 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001320 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 /**
1322 * Indicates the view is pressed, enabled, focused and selected.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #SELECTED_STATE_SET
1327 * @see #FOCUSED_STATE_SET
1328 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 /**
1331 * Indicates the view is pressed, enabled, focused, selected and its window
1332 * has the focus.
1333 *
1334 * @see #PRESSED_STATE_SET
1335 * @see #ENABLED_STATE_SET
1336 * @see #SELECTED_STATE_SET
1337 * @see #FOCUSED_STATE_SET
1338 * @see #WINDOW_FOCUSED_STATE_SET
1339 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341
1342 /**
1343 * The order here is very important to {@link #getDrawableState()}
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 private static final int[][] VIEW_STATE_SETS;
1346
Romain Guyb051e892010-09-28 19:09:36 -07001347 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1348 static final int VIEW_STATE_SELECTED = 1 << 1;
1349 static final int VIEW_STATE_FOCUSED = 1 << 2;
1350 static final int VIEW_STATE_ENABLED = 1 << 3;
1351 static final int VIEW_STATE_PRESSED = 1 << 4;
1352 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001353 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001354 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001355 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1356 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001357
1358 static final int[] VIEW_STATE_IDS = new int[] {
1359 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1360 R.attr.state_selected, VIEW_STATE_SELECTED,
1361 R.attr.state_focused, VIEW_STATE_FOCUSED,
1362 R.attr.state_enabled, VIEW_STATE_ENABLED,
1363 R.attr.state_pressed, VIEW_STATE_PRESSED,
1364 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001365 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001366 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001367 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001368 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 };
1370
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001371 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001372 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1373 throw new IllegalStateException(
1374 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1375 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001379 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001381 orderedIds[i * 2] = viewState;
1382 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 }
1384 }
1385 }
Romain Guyb051e892010-09-28 19:09:36 -07001386 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1387 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1388 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 int numBits = Integer.bitCount(i);
1390 int[] set = new int[numBits];
1391 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001392 for (int j = 0; j < orderedIds.length; j += 2) {
1393 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 set[pos++] = orderedIds[j];
1395 }
1396 }
1397 VIEW_STATE_SETS[i] = set;
1398 }
1399
1400 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1401 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1402 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1403 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1405 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1406 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1410 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1412 | VIEW_STATE_FOCUSED];
1413 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1414 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1418 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1432
1433 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1434 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1438 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1440 | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1474 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1475 | VIEW_STATE_PRESSED];
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001479 * Accessibility event types that are dispatched for text population.
1480 */
1481 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1482 AccessibilityEvent.TYPE_VIEW_CLICKED
1483 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_SELECTED
1485 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1486 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1487 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001488 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001489 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001490 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001491 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1492 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001493
1494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 * Temporary Rect currently for use in setBackground(). This will probably
1496 * be extended in the future to hold our own class with more than just
1497 * a Rect. :)
1498 */
1499 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001500
1501 /**
1502 * Map used to store views' tags.
1503 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001504 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001507 * The next available accessiiblity id.
1508 */
1509 private static int sNextAccessibilityViewId;
1510
1511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 * The animation currently associated with this view.
1513 * @hide
1514 */
1515 protected Animation mCurrentAnimation = null;
1516
1517 /**
1518 * Width as measured during measure pass.
1519 * {@hide}
1520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001521 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001522 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
1524 /**
1525 * Height as measured during measure pass.
1526 * {@hide}
1527 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001528 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001529 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
1531 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001532 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1533 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1534 * its display list. This flag, used only when hw accelerated, allows us to clear the
1535 * flag while retaining this information until it's needed (at getDisplayList() time and
1536 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1537 *
1538 * {@hide}
1539 */
1540 boolean mRecreateDisplayList = false;
1541
1542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 * The view's identifier.
1544 * {@hide}
1545 *
1546 * @see #setId(int)
1547 * @see #getId()
1548 */
1549 @ViewDebug.ExportedProperty(resolveId = true)
1550 int mID = NO_ID;
1551
1552 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001553 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001554 */
1555 int mAccessibilityViewId = NO_ID;
1556
1557 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001558 * @hide
1559 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001560 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001561
1562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 * The view's tag.
1564 * {@hide}
1565 *
1566 * @see #setTag(Object)
1567 * @see #getTag()
1568 */
1569 protected Object mTag;
1570
1571 // for mPrivateFlags:
1572 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001573 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001575 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001577 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001579 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001581 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001583 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 /**
1585 * When this flag is set, this view is running an animation on behalf of its
1586 * children and should therefore not cancel invalidate requests, even if they
1587 * lie outside of this view's bounds.
1588 *
1589 * {@hide}
1590 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001591 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001593 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001595 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001597 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001599 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001601 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001603 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001604 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001605 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606
Dianne Hackborn4702a852012-08-17 15:18:29 -07001607 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608
1609 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001610 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 /**
1612 * Flag used to indicate that this view should be drawn once more (and only once
1613 * more) after its animation has completed.
1614 * {@hide}
1615 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001616 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617
Dianne Hackborn4702a852012-08-17 15:18:29 -07001618 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619
1620 /**
1621 * Indicates that the View returned true when onSetAlpha() was called and that
1622 * the alpha must be restored.
1623 * {@hide}
1624 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001625 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626
1627 /**
1628 * Set by {@link #setScrollContainer(boolean)}.
1629 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001630 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
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_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
1637 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001638 * View flag indicating whether this view was invalidated (fully or partially.)
1639 *
1640 * @hide
1641 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001642 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001643
1644 /**
1645 * View flag indicating whether this view was invalidated by an opaque
1646 * invalidate request.
1647 *
1648 * @hide
1649 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001650 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001651
1652 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001653 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001654 *
1655 * @hide
1656 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001657 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001658
1659 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001660 * Indicates whether the background is opaque.
1661 *
1662 * @hide
1663 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001664 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001665
1666 /**
1667 * Indicates whether the scrollbars are opaque.
1668 *
1669 * @hide
1670 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001671 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001672
1673 /**
1674 * Indicates whether the view is opaque.
1675 *
1676 * @hide
1677 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001678 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001679
Adam Powelle14579b2009-12-16 18:39:52 -08001680 /**
1681 * Indicates a prepressed state;
1682 * the short time between ACTION_DOWN and recognizing
1683 * a 'real' press. Prepressed is used to recognize quick taps
1684 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001685 *
Adam Powelle14579b2009-12-16 18:39:52 -08001686 * @hide
1687 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001688 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powellc9fbaab2010-02-16 17:16:19 -08001690 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001691 * Indicates whether the view is temporarily detached.
1692 *
1693 * @hide
1694 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001695 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001696
Adam Powell8568c3a2010-04-19 14:26:11 -07001697 /**
1698 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001699 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001700 * @hide
1701 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001702 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001703
1704 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001705 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1706 * @hide
1707 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001708 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001709
1710 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001711 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1712 * for transform operations
1713 *
1714 * @hide
1715 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001716 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001717
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001718 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001719 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001720
Chet Haasefd2b0022010-08-06 13:08:56 -07001721 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001722 * Indicates that this view was specifically invalidated, not just dirtied because some
1723 * child view was invalidated. The flag is used to determine when we need to recreate
1724 * a view's display list (as opposed to just returning a reference to its existing
1725 * display list).
1726 *
1727 * @hide
1728 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001729 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001730
Christopher Tate3d4bf172011-03-28 16:16:46 -07001731 /* Masks for mPrivateFlags2 */
1732
1733 /**
1734 * Indicates that this view has reported that it can accept the current drag's content.
1735 * Cleared when the drag operation concludes.
1736 * @hide
1737 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001738 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001739
1740 /**
1741 * Indicates that this view is currently directly under the drag location in a
1742 * drag-and-drop operation involving content that it can accept. Cleared when
1743 * the drag exits the view, or when the drag operation concludes.
1744 * @hide
1745 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001746 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001747
Cibu Johny86666632010-02-22 13:01:02 -08001748 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001749 * Horizontal layout direction of this view is from Left to Right.
1750 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001751 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001752 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001753
1754 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001755 * Horizontal layout direction of this view is from Right to Left.
1756 * Use with {@link #setLayoutDirection}.
1757 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001758 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001759
1760 /**
1761 * Horizontal layout direction of this view is inherited from its parent.
1762 * Use with {@link #setLayoutDirection}.
1763 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001764 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001765
1766 /**
1767 * Horizontal layout direction of this view is from deduced from the default language
1768 * script for the locale. Use with {@link #setLayoutDirection}.
1769 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001770 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001771
1772 /**
1773 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001774 * @hide
1775 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001776 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001777
1778 /**
1779 * Mask for use with private flags indicating bits used for horizontal layout direction.
1780 * @hide
1781 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001782 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001783
1784 /**
1785 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1786 * right-to-left direction.
1787 * @hide
1788 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001789 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001790
1791 /**
1792 * Indicates whether the view horizontal layout direction has been resolved.
1793 * @hide
1794 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001795 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001796
1797 /**
1798 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1799 * @hide
1800 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001801 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1802 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001803
1804 /*
1805 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1806 * flag value.
1807 * @hide
1808 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001809 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1810 LAYOUT_DIRECTION_LTR,
1811 LAYOUT_DIRECTION_RTL,
1812 LAYOUT_DIRECTION_INHERIT,
1813 LAYOUT_DIRECTION_LOCALE
1814 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001815
1816 /**
1817 * Default horizontal layout direction.
1818 * @hide
1819 */
1820 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001821
Adam Powell539ee872012-02-03 19:00:49 -08001822 /**
1823 * Indicates that the view is tracking some sort of transient state
1824 * that the app should not need to be aware of, but that the framework
1825 * should take special care to preserve.
1826 *
1827 * @hide
1828 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001829 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001830
1831
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 /**
1833 * Text direction is inherited thru {@link ViewGroup}
1834 */
1835 public static final int TEXT_DIRECTION_INHERIT = 0;
1836
1837 /**
1838 * Text direction is using "first strong algorithm". The first strong directional character
1839 * determines the paragraph direction. If there is no strong directional character, the
1840 * paragraph direction is the view's resolved layout direction.
1841 */
1842 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1843
1844 /**
1845 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1846 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1847 * If there are neither, the paragraph direction is the view's resolved layout direction.
1848 */
1849 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1850
1851 /**
1852 * Text direction is forced to LTR.
1853 */
1854 public static final int TEXT_DIRECTION_LTR = 3;
1855
1856 /**
1857 * Text direction is forced to RTL.
1858 */
1859 public static final int TEXT_DIRECTION_RTL = 4;
1860
1861 /**
1862 * Text direction is coming from the system Locale.
1863 */
1864 public static final int TEXT_DIRECTION_LOCALE = 5;
1865
1866 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001867 * Default text direction is inherited
1868 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001869 public static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001870
1871 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001872 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1873 * @hide
1874 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001875 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001876
1877 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001878 * Mask for use with private flags indicating bits used for text direction.
1879 * @hide
1880 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001881 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1882 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001883
1884 /**
1885 * Array of text direction flags for mapping attribute "textDirection" to correct
1886 * flag value.
1887 * @hide
1888 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001889 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1890 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1891 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1892 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1893 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1894 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1895 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001896 };
1897
1898 /**
1899 * Indicates whether the view text direction has been resolved.
1900 * @hide
1901 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001902 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1903 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001904
1905 /**
1906 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1907 * @hide
1908 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001909 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001910
1911 /**
1912 * Mask for use with private flags indicating bits used for resolved text direction.
1913 * @hide
1914 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001915 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1916 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001917
1918 /**
1919 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1920 * @hide
1921 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001922 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1923 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001924
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001925 /*
1926 * Default text alignment. The text alignment of this View is inherited from its parent.
1927 * Use with {@link #setTextAlignment(int)}
1928 */
1929 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1930
1931 /**
1932 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1933 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1934 *
1935 * Use with {@link #setTextAlignment(int)}
1936 */
1937 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1938
1939 /**
1940 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1941 *
1942 * Use with {@link #setTextAlignment(int)}
1943 */
1944 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1945
1946 /**
1947 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1948 *
1949 * Use with {@link #setTextAlignment(int)}
1950 */
1951 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1952
1953 /**
1954 * Center the paragraph, e.g. ALIGN_CENTER.
1955 *
1956 * Use with {@link #setTextAlignment(int)}
1957 */
1958 public static final int TEXT_ALIGNMENT_CENTER = 4;
1959
1960 /**
1961 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1962 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1963 *
1964 * Use with {@link #setTextAlignment(int)}
1965 */
1966 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1967
1968 /**
1969 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1970 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1971 *
1972 * Use with {@link #setTextAlignment(int)}
1973 */
1974 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1975
1976 /**
1977 * Default text alignment is inherited
1978 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001979 public static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001980
1981 /**
1982 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1983 * @hide
1984 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001985 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001986
1987 /**
1988 * Mask for use with private flags indicating bits used for text alignment.
1989 * @hide
1990 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001991 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992
1993 /**
1994 * Array of text direction flags for mapping attribute "textAlignment" to correct
1995 * flag value.
1996 * @hide
1997 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001998 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
1999 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2000 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2001 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2002 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2003 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2004 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2005 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002006 };
2007
2008 /**
2009 * Indicates whether the view text alignment has been resolved.
2010 * @hide
2011 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002012 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002013
2014 /**
2015 * Bit shift to get the resolved text alignment.
2016 * @hide
2017 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002018 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002019
2020 /**
2021 * Mask for use with private flags indicating bits used for text alignment.
2022 * @hide
2023 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002024 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2025 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026
2027 /**
2028 * Indicates whether if the view text alignment has been resolved to gravity
2029 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002030 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2031 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032
Svetoslav Ganov42138042012-03-20 11:51:39 -07002033 // Accessiblity constants for mPrivateFlags2
2034
2035 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002036 * Shift for the bits in {@link #mPrivateFlags2} related to the
2037 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002038 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002039 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002040
2041 /**
2042 * Automatically determine whether a view is important for accessibility.
2043 */
2044 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2045
2046 /**
2047 * The view is important for accessibility.
2048 */
2049 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2050
2051 /**
2052 * The view is not important for accessibility.
2053 */
2054 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2055
2056 /**
2057 * The default whether the view is important for accessiblity.
2058 */
2059 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2060
2061 /**
2062 * Mask for obtainig the bits which specify how to determine
2063 * whether a view is important for accessibility.
2064 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002065 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002066 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002067 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002068
2069 /**
2070 * Flag indicating whether a view has accessibility focus.
2071 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002072 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002073
2074 /**
2075 * Flag indicating whether a view state for accessibility has changed.
2076 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002077 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2078 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002079
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002080 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002081 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2082 * is used to check whether later changes to the view's transform should invalidate the
2083 * view to force the quickReject test to run again.
2084 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002085 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002086
Adam Powell0090f202012-08-07 17:15:30 -07002087 /**
2088 * Flag indicating that start/end padding has been resolved into left/right padding
2089 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2090 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2091 * during measurement. In some special cases this is required such as when an adapter-based
2092 * view measures prospective children without attaching them to a window.
2093 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002094 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002095
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002096 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002097
Christopher Tate3d4bf172011-03-28 16:16:46 -07002098 /* End of masks for mPrivateFlags2 */
2099
Chet Haase21433372012-06-05 07:54:09 -07002100 /* Masks for mPrivateFlags3 */
2101
2102 /**
2103 * Flag indicating that view has a transform animation set on it. This is used to track whether
2104 * an animation is cleared between successive frames, in order to tell the associated
2105 * DisplayList to clear its animation matrix.
2106 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002107 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002108
2109 /**
2110 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2111 * animation is cleared between successive frames, in order to tell the associated
2112 * DisplayList to restore its alpha value.
2113 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002114 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002115
2116
2117 /* End of masks for mPrivateFlags3 */
2118
Dianne Hackborn4702a852012-08-17 15:18:29 -07002119 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002120
Chet Haasedaf98e92011-01-10 14:10:36 -08002121 /**
Adam Powell637d3372010-08-25 14:37:03 -07002122 * Always allow a user to over-scroll this view, provided it is a
2123 * view that can scroll.
2124 *
2125 * @see #getOverScrollMode()
2126 * @see #setOverScrollMode(int)
2127 */
2128 public static final int OVER_SCROLL_ALWAYS = 0;
2129
2130 /**
2131 * Allow a user to over-scroll this view only if the content is large
2132 * enough to meaningfully scroll, provided it is a view that can scroll.
2133 *
2134 * @see #getOverScrollMode()
2135 * @see #setOverScrollMode(int)
2136 */
2137 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2138
2139 /**
2140 * Never allow a user to over-scroll this view.
2141 *
2142 * @see #getOverScrollMode()
2143 * @see #setOverScrollMode(int)
2144 */
2145 public static final int OVER_SCROLL_NEVER = 2;
2146
2147 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002148 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2149 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002150 *
Joe Malin32736f02011-01-19 16:14:20 -08002151 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002152 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002153 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002154
2155 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002156 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2157 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002158 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002159 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002160 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002161 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002162 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002163 *
Joe Malin32736f02011-01-19 16:14:20 -08002164 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002165 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002166 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2167
2168 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002169 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2170 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002171 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002172 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002173 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2174 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2175 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002176 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002177 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2178 * window flags) for displaying content using every last pixel on the display.
2179 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002180 * <p>There is a limitation: because navigation controls are so important, the least user
2181 * interaction will cause them to reappear immediately. When this happens, both
2182 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2183 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002184 *
2185 * @see #setSystemUiVisibility(int)
2186 */
2187 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2188
2189 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002190 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2191 * into the normal fullscreen mode so that its content can take over the screen
2192 * while still allowing the user to interact with the application.
2193 *
2194 * <p>This has the same visual effect as
2195 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2196 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2197 * meaning that non-critical screen decorations (such as the status bar) will be
2198 * hidden while the user is in the View's window, focusing the experience on
2199 * that content. Unlike the window flag, if you are using ActionBar in
2200 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2201 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2202 * hide the action bar.
2203 *
2204 * <p>This approach to going fullscreen is best used over the window flag when
2205 * it is a transient state -- that is, the application does this at certain
2206 * points in its user interaction where it wants to allow the user to focus
2207 * on content, but not as a continuous state. For situations where the application
2208 * would like to simply stay full screen the entire time (such as a game that
2209 * wants to take over the screen), the
2210 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2211 * is usually a better approach. The state set here will be removed by the system
2212 * in various situations (such as the user moving to another application) like
2213 * the other system UI states.
2214 *
2215 * <p>When using this flag, the application should provide some easy facility
2216 * for the user to go out of it. A common example would be in an e-book
2217 * reader, where tapping on the screen brings back whatever screen and UI
2218 * decorations that had been hidden while the user was immersed in reading
2219 * the book.
2220 *
2221 * @see #setSystemUiVisibility(int)
2222 */
2223 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2224
2225 /**
2226 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2227 * flags, we would like a stable view of the content insets given to
2228 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2229 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002230 * as a continuous state. In the stock Android UI this is the space for
2231 * the system bar, nav bar, and status bar, but not more transient elements
2232 * such as an input method.
2233 *
2234 * The stable layout your UI sees is based on the system UI modes you can
2235 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2236 * then you will get a stable layout for changes of the
2237 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2238 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2239 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2240 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2241 * with a stable layout. (Note that you should avoid using
2242 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2243 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002244 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002245 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2246 * then a hidden status bar will be considered a "stable" state for purposes
2247 * here. This allows your UI to continually hide the status bar, while still
2248 * using the system UI flags to hide the action bar while still retaining
2249 * a stable layout. Note that changing the window fullscreen flag will never
2250 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002251 *
2252 * <p>If you are using ActionBar in
2253 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2254 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2255 * insets it adds to those given to the application.
2256 */
2257 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2258
2259 /**
2260 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2261 * to be layed out as if it has requested
2262 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2263 * allows it to avoid artifacts when switching in and out of that mode, at
2264 * the expense that some of its user interface may be covered by screen
2265 * decorations when they are shown. You can perform layout of your inner
2266 * UI elements to account for the navagation system UI through the
2267 * {@link #fitSystemWindows(Rect)} method.
2268 */
2269 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2270
2271 /**
2272 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2273 * to be layed out as if it has requested
2274 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2275 * allows it to avoid artifacts when switching in and out of that mode, at
2276 * the expense that some of its user interface may be covered by screen
2277 * decorations when they are shown. You can perform layout of your inner
2278 * UI elements to account for non-fullscreen system UI through the
2279 * {@link #fitSystemWindows(Rect)} method.
2280 */
2281 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2282
2283 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002284 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2285 */
2286 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2287
2288 /**
2289 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2290 */
2291 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002292
2293 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002294 * @hide
2295 *
2296 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2297 * out of the public fields to keep the undefined bits out of the developer's way.
2298 *
2299 * Flag to make the status bar not expandable. Unless you also
2300 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2301 */
2302 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2303
2304 /**
2305 * @hide
2306 *
2307 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2308 * out of the public fields to keep the undefined bits out of the developer's way.
2309 *
2310 * Flag to hide notification icons and scrolling ticker text.
2311 */
2312 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2313
2314 /**
2315 * @hide
2316 *
2317 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2318 * out of the public fields to keep the undefined bits out of the developer's way.
2319 *
2320 * Flag to disable incoming notification alerts. This will not block
2321 * icons, but it will block sound, vibrating and other visual or aural notifications.
2322 */
2323 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2324
2325 /**
2326 * @hide
2327 *
2328 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2329 * out of the public fields to keep the undefined bits out of the developer's way.
2330 *
2331 * Flag to hide only the scrolling ticker. Note that
2332 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2333 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2334 */
2335 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2336
2337 /**
2338 * @hide
2339 *
2340 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2341 * out of the public fields to keep the undefined bits out of the developer's way.
2342 *
2343 * Flag to hide the center system info area.
2344 */
2345 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2346
2347 /**
2348 * @hide
2349 *
2350 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2351 * out of the public fields to keep the undefined bits out of the developer's way.
2352 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002353 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002354 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2355 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002356 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002357
2358 /**
2359 * @hide
2360 *
2361 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2362 * out of the public fields to keep the undefined bits out of the developer's way.
2363 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002364 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002365 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2366 */
2367 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2368
2369 /**
2370 * @hide
2371 *
2372 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2373 * out of the public fields to keep the undefined bits out of the developer's way.
2374 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002375 * Flag to hide only the clock. You might use this if your activity has
2376 * its own clock making the status bar's clock redundant.
2377 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002378 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2379
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002380 /**
2381 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002382 *
2383 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2384 * out of the public fields to keep the undefined bits out of the developer's way.
2385 *
2386 * Flag to hide only the recent apps button. Don't use this
2387 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2388 */
2389 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2390
2391 /**
2392 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002393 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002394 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002395
2396 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002397 * These are the system UI flags that can be cleared by events outside
2398 * of an application. Currently this is just the ability to tap on the
2399 * screen while hiding the navigation bar to have it return.
2400 * @hide
2401 */
2402 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002403 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2404 | SYSTEM_UI_FLAG_FULLSCREEN;
2405
2406 /**
2407 * Flags that can impact the layout in relation to system UI.
2408 */
2409 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2410 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2411 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002412
2413 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002414 * Find views that render the specified text.
2415 *
2416 * @see #findViewsWithText(ArrayList, CharSequence, int)
2417 */
2418 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2419
2420 /**
2421 * Find find views that contain the specified content description.
2422 *
2423 * @see #findViewsWithText(ArrayList, CharSequence, int)
2424 */
2425 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2426
2427 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002428 * Find views that contain {@link AccessibilityNodeProvider}. Such
2429 * a View is a root of virtual view hierarchy and may contain the searched
2430 * text. If this flag is set Views with providers are automatically
2431 * added and it is a responsibility of the client to call the APIs of
2432 * the provider to determine whether the virtual tree rooted at this View
2433 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2434 * represeting the virtual views with this text.
2435 *
2436 * @see #findViewsWithText(ArrayList, CharSequence, int)
2437 *
2438 * @hide
2439 */
2440 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2441
2442 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002443 * The undefined cursor position.
2444 */
2445 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2446
2447 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002448 * Indicates that the screen has changed state and is now off.
2449 *
2450 * @see #onScreenStateChanged(int)
2451 */
2452 public static final int SCREEN_STATE_OFF = 0x0;
2453
2454 /**
2455 * Indicates that the screen has changed state and is now on.
2456 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002457 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002458 */
2459 public static final int SCREEN_STATE_ON = 0x1;
2460
2461 /**
Adam Powell637d3372010-08-25 14:37:03 -07002462 * Controls the over-scroll mode for this view.
2463 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2464 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2465 * and {@link #OVER_SCROLL_NEVER}.
2466 */
2467 private int mOverScrollMode;
2468
2469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 * The parent this view is attached to.
2471 * {@hide}
2472 *
2473 * @see #getParent()
2474 */
2475 protected ViewParent mParent;
2476
2477 /**
2478 * {@hide}
2479 */
2480 AttachInfo mAttachInfo;
2481
2482 /**
2483 * {@hide}
2484 */
Romain Guy809a7f62009-05-14 15:44:42 -07002485 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002486 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002487 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002488 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002489 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002490 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002491 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002492 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2493 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2494 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2495 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002496 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002498 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002499 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500
2501 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002502 * This view's request for the visibility of the status bar.
2503 * @hide
2504 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002505 @ViewDebug.ExportedProperty(flagMapping = {
2506 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2507 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2508 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2509 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2510 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2511 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2512 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2513 equals = SYSTEM_UI_FLAG_VISIBLE,
2514 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2515 })
Joe Onorato664644d2011-01-23 17:53:23 -08002516 int mSystemUiVisibility;
2517
2518 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002519 * Reference count for transient state.
2520 * @see #setHasTransientState(boolean)
2521 */
2522 int mTransientStateCount = 0;
2523
2524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 * Count of how many windows this view has been attached to.
2526 */
2527 int mWindowAttachCount;
2528
2529 /**
2530 * The layout parameters associated with this view and used by the parent
2531 * {@link android.view.ViewGroup} to determine how this view should be
2532 * laid out.
2533 * {@hide}
2534 */
2535 protected ViewGroup.LayoutParams mLayoutParams;
2536
2537 /**
2538 * The view flags hold various views states.
2539 * {@hide}
2540 */
2541 @ViewDebug.ExportedProperty
2542 int mViewFlags;
2543
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002544 static class TransformationInfo {
2545 /**
2546 * The transform matrix for the View. This transform is calculated internally
2547 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2548 * is used by default. Do *not* use this variable directly; instead call
2549 * getMatrix(), which will automatically recalculate the matrix if necessary
2550 * to get the correct matrix based on the latest rotation and scale properties.
2551 */
2552 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002553
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002554 /**
2555 * The transform matrix for the View. This transform is calculated internally
2556 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2557 * is used by default. Do *not* use this variable directly; instead call
2558 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2559 * to get the correct matrix based on the latest rotation and scale properties.
2560 */
2561 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002562
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002563 /**
2564 * An internal variable that tracks whether we need to recalculate the
2565 * transform matrix, based on whether the rotation or scaleX/Y properties
2566 * have changed since the matrix was last calculated.
2567 */
2568 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002569
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002570 /**
2571 * An internal variable that tracks whether we need to recalculate the
2572 * transform matrix, based on whether the rotation or scaleX/Y properties
2573 * have changed since the matrix was last calculated.
2574 */
2575 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002576
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002577 /**
2578 * A variable that tracks whether we need to recalculate the
2579 * transform matrix, based on whether the rotation or scaleX/Y properties
2580 * have changed since the matrix was last calculated. This variable
2581 * is only valid after a call to updateMatrix() or to a function that
2582 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2583 */
2584 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002586 /**
2587 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2588 */
2589 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002590
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002591 /**
2592 * This matrix is used when computing the matrix for 3D rotations.
2593 */
2594 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002595
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002596 /**
2597 * These prev values are used to recalculate a centered pivot point when necessary. The
2598 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2599 * set), so thes values are only used then as well.
2600 */
2601 private int mPrevWidth = -1;
2602 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002603
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002604 /**
2605 * The degrees rotation around the vertical axis through the pivot point.
2606 */
2607 @ViewDebug.ExportedProperty
2608 float mRotationY = 0f;
2609
2610 /**
2611 * The degrees rotation around the horizontal axis through the pivot point.
2612 */
2613 @ViewDebug.ExportedProperty
2614 float mRotationX = 0f;
2615
2616 /**
2617 * The degrees rotation around the pivot point.
2618 */
2619 @ViewDebug.ExportedProperty
2620 float mRotation = 0f;
2621
2622 /**
2623 * The amount of translation of the object away from its left property (post-layout).
2624 */
2625 @ViewDebug.ExportedProperty
2626 float mTranslationX = 0f;
2627
2628 /**
2629 * The amount of translation of the object away from its top property (post-layout).
2630 */
2631 @ViewDebug.ExportedProperty
2632 float mTranslationY = 0f;
2633
2634 /**
2635 * The amount of scale in the x direction around the pivot point. A
2636 * value of 1 means no scaling is applied.
2637 */
2638 @ViewDebug.ExportedProperty
2639 float mScaleX = 1f;
2640
2641 /**
2642 * The amount of scale in the y direction around the pivot point. A
2643 * value of 1 means no scaling is applied.
2644 */
2645 @ViewDebug.ExportedProperty
2646 float mScaleY = 1f;
2647
2648 /**
Chet Haasea33de552012-02-03 16:28:24 -08002649 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002650 */
2651 @ViewDebug.ExportedProperty
2652 float mPivotX = 0f;
2653
2654 /**
Chet Haasea33de552012-02-03 16:28:24 -08002655 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002656 */
2657 @ViewDebug.ExportedProperty
2658 float mPivotY = 0f;
2659
2660 /**
2661 * The opacity of the View. This is a value from 0 to 1, where 0 means
2662 * completely transparent and 1 means completely opaque.
2663 */
2664 @ViewDebug.ExportedProperty
2665 float mAlpha = 1f;
2666 }
2667
2668 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002669
Joe Malin32736f02011-01-19 16:14:20 -08002670 private boolean mLastIsOpaque;
2671
Chet Haasefd2b0022010-08-06 13:08:56 -07002672 /**
2673 * Convenience value to check for float values that are close enough to zero to be considered
2674 * zero.
2675 */
Romain Guy2542d192010-08-18 11:47:12 -07002676 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002677
2678 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 * The distance in pixels from the left edge of this view's parent
2680 * to the left edge of this view.
2681 * {@hide}
2682 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002683 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 protected int mLeft;
2685 /**
2686 * The distance in pixels from the left edge of this view's parent
2687 * to the right edge of this view.
2688 * {@hide}
2689 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002690 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 protected int mRight;
2692 /**
2693 * The distance in pixels from the top edge of this view's parent
2694 * to the top edge of this view.
2695 * {@hide}
2696 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002697 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 protected int mTop;
2699 /**
2700 * The distance in pixels from the top edge of this view's parent
2701 * to the bottom edge of this view.
2702 * {@hide}
2703 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002704 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 protected int mBottom;
2706
2707 /**
2708 * The offset, in pixels, by which the content of this view is scrolled
2709 * horizontally.
2710 * {@hide}
2711 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002712 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 protected int mScrollX;
2714 /**
2715 * The offset, in pixels, by which the content of this view is scrolled
2716 * vertically.
2717 * {@hide}
2718 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002719 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 protected int mScrollY;
2721
2722 /**
2723 * The left padding in pixels, that is the distance in pixels between the
2724 * left edge of this view and the left edge of its content.
2725 * {@hide}
2726 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002727 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 protected int mPaddingLeft;
2729 /**
2730 * The right padding in pixels, that is the distance in pixels between the
2731 * right edge of this view and the right edge of its content.
2732 * {@hide}
2733 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002734 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 protected int mPaddingRight;
2736 /**
2737 * The top padding in pixels, that is the distance in pixels between the
2738 * top edge of this view and the top edge of its content.
2739 * {@hide}
2740 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002741 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 protected int mPaddingTop;
2743 /**
2744 * The bottom padding in pixels, that is the distance in pixels between the
2745 * bottom edge of this view and the bottom edge of its content.
2746 * {@hide}
2747 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002748 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 protected int mPaddingBottom;
2750
2751 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002752 * The layout insets in pixels, that is the distance in pixels between the
2753 * visible edges of this view its bounds.
2754 */
2755 private Insets mLayoutInsets;
2756
2757 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002758 * Briefly describes the view and is primarily used for accessibility support.
2759 */
2760 private CharSequence mContentDescription;
2761
2762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002764 *
2765 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002767 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002768 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769
2770 /**
2771 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002772 *
2773 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002775 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002776 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002778 /**
Adam Powell20232d02010-12-08 21:08:53 -08002779 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002780 *
2781 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002782 */
2783 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002784 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002785
2786 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002787 * Cache the paddingStart set by the user to append to the scrollbar's size.
2788 *
2789 */
2790 @ViewDebug.ExportedProperty(category = "padding")
2791 int mUserPaddingStart;
2792
2793 /**
2794 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2795 *
2796 */
2797 @ViewDebug.ExportedProperty(category = "padding")
2798 int mUserPaddingEnd;
2799
2800 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002801 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002802 *
2803 * @hide
2804 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002805 int mUserPaddingLeftInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002806
2807 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002808 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002809 *
2810 * @hide
2811 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002812 int mUserPaddingRightInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002813
2814 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002815 * Default undefined padding
2816 */
2817 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2818
2819 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002820 * @hide
2821 */
2822 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2823 /**
2824 * @hide
2825 */
2826 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827
Philip Milne6c8ea062012-04-03 17:38:43 -07002828 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829
2830 private int mBackgroundResource;
2831 private boolean mBackgroundSizeChanged;
2832
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002833 static class ListenerInfo {
2834 /**
2835 * Listener used to dispatch focus change events.
2836 * This field should be made private, so it is hidden from the SDK.
2837 * {@hide}
2838 */
2839 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002841 /**
2842 * Listeners for layout change events.
2843 */
2844 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002845
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002846 /**
2847 * Listeners for attach events.
2848 */
2849 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002851 /**
2852 * Listener used to dispatch click events.
2853 * This field should be made private, so it is hidden from the SDK.
2854 * {@hide}
2855 */
2856 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002858 /**
2859 * Listener used to dispatch long click events.
2860 * This field should be made private, so it is hidden from the SDK.
2861 * {@hide}
2862 */
2863 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002865 /**
2866 * Listener used to build the context menu.
2867 * This field should be made private, so it is hidden from the SDK.
2868 * {@hide}
2869 */
2870 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002872 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002874 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002876 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002877
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002878 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002879
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002880 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002881
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002882 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2883 }
2884
2885 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 /**
2888 * The application environment this view lives in.
2889 * This field should be made private, so it is hidden from the SDK.
2890 * {@hide}
2891 */
2892 protected Context mContext;
2893
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002894 private final Resources mResources;
2895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 private ScrollabilityCache mScrollCache;
2897
2898 private int[] mDrawableState = null;
2899
Romain Guy0211a0a2011-02-14 16:34:59 -08002900 /**
2901 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002902 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002903 * @hide
2904 */
2905 public boolean mCachingFailed;
2906
Romain Guy02890fd2010-08-06 17:58:44 -07002907 private Bitmap mDrawingCache;
2908 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002909 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002910 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911
2912 /**
2913 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2914 * the user may specify which view to go to next.
2915 */
2916 private int mNextFocusLeftId = View.NO_ID;
2917
2918 /**
2919 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2920 * the user may specify which view to go to next.
2921 */
2922 private int mNextFocusRightId = View.NO_ID;
2923
2924 /**
2925 * When this view has focus and the next focus is {@link #FOCUS_UP},
2926 * the user may specify which view to go to next.
2927 */
2928 private int mNextFocusUpId = View.NO_ID;
2929
2930 /**
2931 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2932 * the user may specify which view to go to next.
2933 */
2934 private int mNextFocusDownId = View.NO_ID;
2935
Jeff Brown4e6319b2010-12-13 10:36:51 -08002936 /**
2937 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2938 * the user may specify which view to go to next.
2939 */
2940 int mNextFocusForwardId = View.NO_ID;
2941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002943 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002944 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002945 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 private UnsetPressedState mUnsetPressedState;
2948
2949 /**
2950 * Whether the long press's action has been invoked. The tap's action is invoked on the
2951 * up event while a long press is invoked as soon as the long press duration is reached, so
2952 * a long press could be performed before the tap is checked, in which case the tap's action
2953 * should not be invoked.
2954 */
2955 private boolean mHasPerformedLongPress;
2956
2957 /**
2958 * The minimum height of the view. We'll try our best to have the height
2959 * of this view to at least this amount.
2960 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002961 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 private int mMinHeight;
2963
2964 /**
2965 * The minimum width of the view. We'll try our best to have the width
2966 * of this view to at least this amount.
2967 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002968 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 private int mMinWidth;
2970
2971 /**
2972 * The delegate to handle touch events that are physically in this view
2973 * but should be handled by another view.
2974 */
2975 private TouchDelegate mTouchDelegate = null;
2976
2977 /**
2978 * Solid color to use as a background when creating the drawing cache. Enables
2979 * the cache to use 16 bit bitmaps instead of 32 bit.
2980 */
2981 private int mDrawingCacheBackgroundColor = 0;
2982
2983 /**
2984 * Special tree observer used when mAttachInfo is null.
2985 */
2986 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002987
Adam Powelle14579b2009-12-16 18:39:52 -08002988 /**
2989 * Cache the touch slop from the context that created the view.
2990 */
2991 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002994 * Object that handles automatic animation of view properties.
2995 */
2996 private ViewPropertyAnimator mAnimator = null;
2997
2998 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002999 * Flag indicating that a drag can cross window boundaries. When
3000 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3001 * with this flag set, all visible applications will be able to participate
3002 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003003 *
3004 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003005 */
3006 public static final int DRAG_FLAG_GLOBAL = 1;
3007
3008 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003009 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3010 */
3011 private float mVerticalScrollFactor;
3012
3013 /**
Adam Powell20232d02010-12-08 21:08:53 -08003014 * Position of the vertical scroll bar.
3015 */
3016 private int mVerticalScrollbarPosition;
3017
3018 /**
3019 * Position the scroll bar at the default position as determined by the system.
3020 */
3021 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3022
3023 /**
3024 * Position the scroll bar along the left edge.
3025 */
3026 public static final int SCROLLBAR_POSITION_LEFT = 1;
3027
3028 /**
3029 * Position the scroll bar along the right edge.
3030 */
3031 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3032
3033 /**
Romain Guy171c5922011-01-06 10:04:23 -08003034 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003035 *
3036 * @see #getLayerType()
3037 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003038 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003039 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003040 */
3041 public static final int LAYER_TYPE_NONE = 0;
3042
3043 /**
3044 * <p>Indicates that the view has a software layer. A software layer is backed
3045 * by a bitmap and causes the view to be rendered using Android's software
3046 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003047 *
Romain Guy171c5922011-01-06 10:04:23 -08003048 * <p>Software layers have various usages:</p>
3049 * <p>When the application is not using hardware acceleration, a software layer
3050 * is useful to apply a specific color filter and/or blending mode and/or
3051 * translucency to a view and all its children.</p>
3052 * <p>When the application is using hardware acceleration, a software layer
3053 * is useful to render drawing primitives not supported by the hardware
3054 * accelerated pipeline. It can also be used to cache a complex view tree
3055 * into a texture and reduce the complexity of drawing operations. For instance,
3056 * when animating a complex view tree with a translation, a software layer can
3057 * be used to render the view tree only once.</p>
3058 * <p>Software layers should be avoided when the affected view tree updates
3059 * often. Every update will require to re-render the software layer, which can
3060 * potentially be slow (particularly when hardware acceleration is turned on
3061 * since the layer will have to be uploaded into a hardware texture after every
3062 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003063 *
3064 * @see #getLayerType()
3065 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003066 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003067 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003068 */
3069 public static final int LAYER_TYPE_SOFTWARE = 1;
3070
3071 /**
3072 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3073 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3074 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3075 * rendering pipeline, but only if hardware acceleration is turned on for the
3076 * view hierarchy. When hardware acceleration is turned off, hardware layers
3077 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003078 *
Romain Guy171c5922011-01-06 10:04:23 -08003079 * <p>A hardware layer is useful to apply a specific color filter and/or
3080 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003081 * <p>A hardware layer can be used to cache a complex view tree into a
3082 * texture and reduce the complexity of drawing operations. For instance,
3083 * when animating a complex view tree with a translation, a hardware layer can
3084 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003085 * <p>A hardware layer can also be used to increase the rendering quality when
3086 * rotation transformations are applied on a view. It can also be used to
3087 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003088 *
3089 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003090 * @see #setLayerType(int, android.graphics.Paint)
3091 * @see #LAYER_TYPE_NONE
3092 * @see #LAYER_TYPE_SOFTWARE
3093 */
3094 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003095
Romain Guy3aaff3a2011-01-12 14:18:47 -08003096 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3097 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3098 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3099 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3100 })
Romain Guy171c5922011-01-06 10:04:23 -08003101 int mLayerType = LAYER_TYPE_NONE;
3102 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003103 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003104
3105 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003106 * Set to true when the view is sending hover accessibility events because it
3107 * is the innermost hovered view.
3108 */
3109 private boolean mSendingHoverAccessibilityEvents;
3110
Dianne Hackborn4702a852012-08-17 15:18:29 -07003111 /**
3112 * Delegate for injecting accessiblity functionality.
3113 */
3114 AccessibilityDelegate mAccessibilityDelegate;
3115
3116 /**
3117 * Consistency verifier for debugging purposes.
3118 * @hide
3119 */
3120 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3121 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3122 new InputEventConsistencyVerifier(this, 0) : null;
3123
Adam Powella9108a22012-07-18 11:18:09 -07003124 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3125
Jeff Brown87b7f802011-06-21 18:35:45 -07003126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 * Simple constructor to use when creating a view from code.
3128 *
3129 * @param context The Context the view is running in, through which it can
3130 * access the current theme, resources, etc.
3131 */
3132 public View(Context context) {
3133 mContext = context;
3134 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003135 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003136 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003137 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3138 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3139 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3140 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003141 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003142 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003143 mUserPaddingStart = UNDEFINED_PADDING;
3144 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 }
3146
3147 /**
3148 * Constructor that is called when inflating a view from XML. This is called
3149 * when a view is being constructed from an XML file, supplying attributes
3150 * that were specified in the XML file. This version uses a default style of
3151 * 0, so the only attribute values applied are those in the Context's Theme
3152 * and the given AttributeSet.
3153 *
3154 * <p>
3155 * The method onFinishInflate() will be called after all children have been
3156 * added.
3157 *
3158 * @param context The Context the view is running in, through which it can
3159 * access the current theme, resources, etc.
3160 * @param attrs The attributes of the XML tag that is inflating the view.
3161 * @see #View(Context, AttributeSet, int)
3162 */
3163 public View(Context context, AttributeSet attrs) {
3164 this(context, attrs, 0);
3165 }
3166
3167 /**
3168 * Perform inflation from XML and apply a class-specific base style. This
3169 * constructor of View allows subclasses to use their own base style when
3170 * they are inflating. For example, a Button class's constructor would call
3171 * this version of the super class constructor and supply
3172 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3173 * the theme's button style to modify all of the base view attributes (in
3174 * particular its background) as well as the Button class's attributes.
3175 *
3176 * @param context The Context the view is running in, through which it can
3177 * access the current theme, resources, etc.
3178 * @param attrs The attributes of the XML tag that is inflating the view.
3179 * @param defStyle The default style to apply to this view. If 0, no style
3180 * will be applied (beyond what is included in the theme). This may
3181 * either be an attribute resource, whose value will be retrieved
3182 * from the current theme, or an explicit style resource.
3183 * @see #View(Context, AttributeSet)
3184 */
3185 public View(Context context, AttributeSet attrs, int defStyle) {
3186 this(context);
3187
3188 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3189 defStyle, 0);
3190
3191 Drawable background = null;
3192
3193 int leftPadding = -1;
3194 int topPadding = -1;
3195 int rightPadding = -1;
3196 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003197 int startPadding = UNDEFINED_PADDING;
3198 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199
3200 int padding = -1;
3201
3202 int viewFlagValues = 0;
3203 int viewFlagMasks = 0;
3204
3205 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 int x = 0;
3208 int y = 0;
3209
Chet Haase73066682010-11-29 15:55:32 -08003210 float tx = 0;
3211 float ty = 0;
3212 float rotation = 0;
3213 float rotationX = 0;
3214 float rotationY = 0;
3215 float sx = 1f;
3216 float sy = 1f;
3217 boolean transformSet = false;
3218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003220 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003221 boolean initializeScrollbars = false;
3222
3223 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 final int N = a.getIndexCount();
3226 for (int i = 0; i < N; i++) {
3227 int attr = a.getIndex(i);
3228 switch (attr) {
3229 case com.android.internal.R.styleable.View_background:
3230 background = a.getDrawable(attr);
3231 break;
3232 case com.android.internal.R.styleable.View_padding:
3233 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003234 mUserPaddingLeftInitial = padding;
3235 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 break;
3237 case com.android.internal.R.styleable.View_paddingLeft:
3238 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003239 mUserPaddingLeftInitial = leftPadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 break;
3241 case com.android.internal.R.styleable.View_paddingTop:
3242 topPadding = a.getDimensionPixelSize(attr, -1);
3243 break;
3244 case com.android.internal.R.styleable.View_paddingRight:
3245 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003246 mUserPaddingRightInitial = rightPadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 break;
3248 case com.android.internal.R.styleable.View_paddingBottom:
3249 bottomPadding = a.getDimensionPixelSize(attr, -1);
3250 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003251 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003252 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003253 break;
3254 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003255 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003256 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 case com.android.internal.R.styleable.View_scrollX:
3258 x = a.getDimensionPixelOffset(attr, 0);
3259 break;
3260 case com.android.internal.R.styleable.View_scrollY:
3261 y = a.getDimensionPixelOffset(attr, 0);
3262 break;
Chet Haase73066682010-11-29 15:55:32 -08003263 case com.android.internal.R.styleable.View_alpha:
3264 setAlpha(a.getFloat(attr, 1f));
3265 break;
3266 case com.android.internal.R.styleable.View_transformPivotX:
3267 setPivotX(a.getDimensionPixelOffset(attr, 0));
3268 break;
3269 case com.android.internal.R.styleable.View_transformPivotY:
3270 setPivotY(a.getDimensionPixelOffset(attr, 0));
3271 break;
3272 case com.android.internal.R.styleable.View_translationX:
3273 tx = a.getDimensionPixelOffset(attr, 0);
3274 transformSet = true;
3275 break;
3276 case com.android.internal.R.styleable.View_translationY:
3277 ty = a.getDimensionPixelOffset(attr, 0);
3278 transformSet = true;
3279 break;
3280 case com.android.internal.R.styleable.View_rotation:
3281 rotation = a.getFloat(attr, 0);
3282 transformSet = true;
3283 break;
3284 case com.android.internal.R.styleable.View_rotationX:
3285 rotationX = a.getFloat(attr, 0);
3286 transformSet = true;
3287 break;
3288 case com.android.internal.R.styleable.View_rotationY:
3289 rotationY = a.getFloat(attr, 0);
3290 transformSet = true;
3291 break;
3292 case com.android.internal.R.styleable.View_scaleX:
3293 sx = a.getFloat(attr, 1f);
3294 transformSet = true;
3295 break;
3296 case com.android.internal.R.styleable.View_scaleY:
3297 sy = a.getFloat(attr, 1f);
3298 transformSet = true;
3299 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 case com.android.internal.R.styleable.View_id:
3301 mID = a.getResourceId(attr, NO_ID);
3302 break;
3303 case com.android.internal.R.styleable.View_tag:
3304 mTag = a.getText(attr);
3305 break;
3306 case com.android.internal.R.styleable.View_fitsSystemWindows:
3307 if (a.getBoolean(attr, false)) {
3308 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3309 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3310 }
3311 break;
3312 case com.android.internal.R.styleable.View_focusable:
3313 if (a.getBoolean(attr, false)) {
3314 viewFlagValues |= FOCUSABLE;
3315 viewFlagMasks |= FOCUSABLE_MASK;
3316 }
3317 break;
3318 case com.android.internal.R.styleable.View_focusableInTouchMode:
3319 if (a.getBoolean(attr, false)) {
3320 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3321 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3322 }
3323 break;
3324 case com.android.internal.R.styleable.View_clickable:
3325 if (a.getBoolean(attr, false)) {
3326 viewFlagValues |= CLICKABLE;
3327 viewFlagMasks |= CLICKABLE;
3328 }
3329 break;
3330 case com.android.internal.R.styleable.View_longClickable:
3331 if (a.getBoolean(attr, false)) {
3332 viewFlagValues |= LONG_CLICKABLE;
3333 viewFlagMasks |= LONG_CLICKABLE;
3334 }
3335 break;
3336 case com.android.internal.R.styleable.View_saveEnabled:
3337 if (!a.getBoolean(attr, true)) {
3338 viewFlagValues |= SAVE_DISABLED;
3339 viewFlagMasks |= SAVE_DISABLED_MASK;
3340 }
3341 break;
3342 case com.android.internal.R.styleable.View_duplicateParentState:
3343 if (a.getBoolean(attr, false)) {
3344 viewFlagValues |= DUPLICATE_PARENT_STATE;
3345 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3346 }
3347 break;
3348 case com.android.internal.R.styleable.View_visibility:
3349 final int visibility = a.getInt(attr, 0);
3350 if (visibility != 0) {
3351 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3352 viewFlagMasks |= VISIBILITY_MASK;
3353 }
3354 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003355 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003356 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003357 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003358 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003359 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003360 final int value = (layoutDirection != -1) ?
3361 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003362 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003363 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 case com.android.internal.R.styleable.View_drawingCacheQuality:
3365 final int cacheQuality = a.getInt(attr, 0);
3366 if (cacheQuality != 0) {
3367 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3368 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3369 }
3370 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003371 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003372 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003373 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3375 if (!a.getBoolean(attr, true)) {
3376 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3377 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3378 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003379 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3381 if (!a.getBoolean(attr, true)) {
3382 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3383 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3384 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003385 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 case R.styleable.View_scrollbars:
3387 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3388 if (scrollbars != SCROLLBARS_NONE) {
3389 viewFlagValues |= scrollbars;
3390 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003391 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 }
3393 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003394 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003396 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003397 // Ignore the attribute starting with ICS
3398 break;
3399 }
3400 // With builds < ICS, fall through and apply fading edges
3401 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3403 if (fadingEdge != FADING_EDGE_NONE) {
3404 viewFlagValues |= fadingEdge;
3405 viewFlagMasks |= FADING_EDGE_MASK;
3406 initializeFadingEdge(a);
3407 }
3408 break;
3409 case R.styleable.View_scrollbarStyle:
3410 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3411 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3412 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3413 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3414 }
3415 break;
3416 case R.styleable.View_isScrollContainer:
3417 setScrollContainer = true;
3418 if (a.getBoolean(attr, false)) {
3419 setScrollContainer(true);
3420 }
3421 break;
3422 case com.android.internal.R.styleable.View_keepScreenOn:
3423 if (a.getBoolean(attr, false)) {
3424 viewFlagValues |= KEEP_SCREEN_ON;
3425 viewFlagMasks |= KEEP_SCREEN_ON;
3426 }
3427 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003428 case R.styleable.View_filterTouchesWhenObscured:
3429 if (a.getBoolean(attr, false)) {
3430 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3431 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3432 }
3433 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 case R.styleable.View_nextFocusLeft:
3435 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3436 break;
3437 case R.styleable.View_nextFocusRight:
3438 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3439 break;
3440 case R.styleable.View_nextFocusUp:
3441 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3442 break;
3443 case R.styleable.View_nextFocusDown:
3444 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3445 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003446 case R.styleable.View_nextFocusForward:
3447 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3448 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 case R.styleable.View_minWidth:
3450 mMinWidth = a.getDimensionPixelSize(attr, 0);
3451 break;
3452 case R.styleable.View_minHeight:
3453 mMinHeight = a.getDimensionPixelSize(attr, 0);
3454 break;
Romain Guy9a817362009-05-01 10:57:14 -07003455 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003456 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003457 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003458 + "be used within a restricted context");
3459 }
3460
Romain Guy9a817362009-05-01 10:57:14 -07003461 final String handlerName = a.getString(attr);
3462 if (handlerName != null) {
3463 setOnClickListener(new OnClickListener() {
3464 private Method mHandler;
3465
3466 public void onClick(View v) {
3467 if (mHandler == null) {
3468 try {
3469 mHandler = getContext().getClass().getMethod(handlerName,
3470 View.class);
3471 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003472 int id = getId();
3473 String idText = id == NO_ID ? "" : " with id '"
3474 + getContext().getResources().getResourceEntryName(
3475 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003476 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003477 handlerName + "(View) in the activity "
3478 + getContext().getClass() + " for onClick handler"
3479 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003480 }
3481 }
3482
3483 try {
3484 mHandler.invoke(getContext(), View.this);
3485 } catch (IllegalAccessException e) {
3486 throw new IllegalStateException("Could not execute non "
3487 + "public method of the activity", e);
3488 } catch (InvocationTargetException e) {
3489 throw new IllegalStateException("Could not execute "
3490 + "method of the activity", e);
3491 }
3492 }
3493 });
3494 }
3495 break;
Adam Powell637d3372010-08-25 14:37:03 -07003496 case R.styleable.View_overScrollMode:
3497 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3498 break;
Adam Powell20232d02010-12-08 21:08:53 -08003499 case R.styleable.View_verticalScrollbarPosition:
3500 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3501 break;
Romain Guy171c5922011-01-06 10:04:23 -08003502 case R.styleable.View_layerType:
3503 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3504 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003505 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003506 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003507 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003508 // Set the text direction flags depending on the value of the attribute
3509 final int textDirection = a.getInt(attr, -1);
3510 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003511 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003512 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003513 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003514 case R.styleable.View_textAlignment:
3515 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003516 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003517 // Set the text alignment flag depending on the value of the attribute
3518 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003519 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003520 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003521 case R.styleable.View_importantForAccessibility:
3522 setImportantForAccessibility(a.getInt(attr,
3523 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003524 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 }
3526 }
3527
Adam Powell637d3372010-08-25 14:37:03 -07003528 setOverScrollMode(overScrollMode);
3529
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003530 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3531 // the resolved layout direction). Those cached values will be used later during padding
3532 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003533 mUserPaddingStart = startPadding;
3534 mUserPaddingEnd = endPadding;
3535
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003536 if (background != null) {
3537 setBackground(background);
3538 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 if (padding >= 0) {
3541 leftPadding = padding;
3542 topPadding = padding;
3543 rightPadding = padding;
3544 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003545 mUserPaddingLeftInitial = padding;
3546 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
3548
3549 // If the user specified the padding (either with android:padding or
3550 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3551 // use the default padding or the padding from the background drawable
3552 // (stored at this point in mPadding*)
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003553 mUserPaddingLeftInitial = leftPadding >= 0 ? leftPadding : mPaddingLeft;
3554 mUserPaddingRightInitial = rightPadding >= 0 ? rightPadding : mPaddingRight;
3555 internalSetPadding(mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003557 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3559
3560 if (viewFlagMasks != 0) {
3561 setFlags(viewFlagValues, viewFlagMasks);
3562 }
3563
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003564 if (initializeScrollbars) {
3565 initializeScrollbars(a);
3566 }
3567
3568 a.recycle();
3569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 // Needs to be called after mViewFlags is set
3571 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3572 recomputePadding();
3573 }
3574
3575 if (x != 0 || y != 0) {
3576 scrollTo(x, y);
3577 }
3578
Chet Haase73066682010-11-29 15:55:32 -08003579 if (transformSet) {
3580 setTranslationX(tx);
3581 setTranslationY(ty);
3582 setRotation(rotation);
3583 setRotationX(rotationX);
3584 setRotationY(rotationY);
3585 setScaleX(sx);
3586 setScaleY(sy);
3587 }
3588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3590 setScrollContainer(true);
3591 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003592
3593 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
3595
3596 /**
3597 * Non-public constructor for use in testing
3598 */
3599 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003600 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 }
3602
Dianne Hackborn4702a852012-08-17 15:18:29 -07003603 public String toString() {
3604 StringBuilder out = new StringBuilder(128);
3605 out.append(getClass().getName());
3606 out.append('{');
3607 out.append(Integer.toHexString(System.identityHashCode(this)));
3608 out.append(' ');
3609 switch (mViewFlags&VISIBILITY_MASK) {
3610 case VISIBLE: out.append('V'); break;
3611 case INVISIBLE: out.append('I'); break;
3612 case GONE: out.append('G'); break;
3613 default: out.append('.'); break;
3614 }
3615 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3616 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3617 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3618 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3619 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3620 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3621 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3622 out.append(' ');
3623 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3624 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3625 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3626 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3627 out.append('p');
3628 } else {
3629 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3630 }
3631 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3632 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3633 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3634 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3635 out.append(' ');
3636 out.append(mLeft);
3637 out.append(',');
3638 out.append(mTop);
3639 out.append('-');
3640 out.append(mRight);
3641 out.append(',');
3642 out.append(mBottom);
3643 final int id = getId();
3644 if (id != NO_ID) {
3645 out.append(" #");
3646 out.append(Integer.toHexString(id));
3647 final Resources r = mResources;
3648 if (id != 0 && r != null) {
3649 try {
3650 String pkgname;
3651 switch (id&0xff000000) {
3652 case 0x7f000000:
3653 pkgname="app";
3654 break;
3655 case 0x01000000:
3656 pkgname="android";
3657 break;
3658 default:
3659 pkgname = r.getResourcePackageName(id);
3660 break;
3661 }
3662 String typename = r.getResourceTypeName(id);
3663 String entryname = r.getResourceEntryName(id);
3664 out.append(" ");
3665 out.append(pkgname);
3666 out.append(":");
3667 out.append(typename);
3668 out.append("/");
3669 out.append(entryname);
3670 } catch (Resources.NotFoundException e) {
3671 }
3672 }
3673 }
3674 out.append("}");
3675 return out.toString();
3676 }
3677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 /**
3679 * <p>
3680 * Initializes the fading edges from a given set of styled attributes. This
3681 * method should be called by subclasses that need fading edges and when an
3682 * instance of these subclasses is created programmatically rather than
3683 * being inflated from XML. This method is automatically called when the XML
3684 * is inflated.
3685 * </p>
3686 *
3687 * @param a the styled attributes set to initialize the fading edges from
3688 */
3689 protected void initializeFadingEdge(TypedArray a) {
3690 initScrollCache();
3691
3692 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3693 R.styleable.View_fadingEdgeLength,
3694 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3695 }
3696
3697 /**
3698 * Returns the size of the vertical faded edges used to indicate that more
3699 * content in this view is visible.
3700 *
3701 * @return The size in pixels of the vertical faded edge or 0 if vertical
3702 * faded edges are not enabled for this view.
3703 * @attr ref android.R.styleable#View_fadingEdgeLength
3704 */
3705 public int getVerticalFadingEdgeLength() {
3706 if (isVerticalFadingEdgeEnabled()) {
3707 ScrollabilityCache cache = mScrollCache;
3708 if (cache != null) {
3709 return cache.fadingEdgeLength;
3710 }
3711 }
3712 return 0;
3713 }
3714
3715 /**
3716 * Set the size of the faded edge used to indicate that more content in this
3717 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003718 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3719 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3720 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 *
3722 * @param length The size in pixels of the faded edge used to indicate that more
3723 * content in this view is visible.
3724 */
3725 public void setFadingEdgeLength(int length) {
3726 initScrollCache();
3727 mScrollCache.fadingEdgeLength = length;
3728 }
3729
3730 /**
3731 * Returns the size of the horizontal faded edges used to indicate that more
3732 * content in this view is visible.
3733 *
3734 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3735 * faded edges are not enabled for this view.
3736 * @attr ref android.R.styleable#View_fadingEdgeLength
3737 */
3738 public int getHorizontalFadingEdgeLength() {
3739 if (isHorizontalFadingEdgeEnabled()) {
3740 ScrollabilityCache cache = mScrollCache;
3741 if (cache != null) {
3742 return cache.fadingEdgeLength;
3743 }
3744 }
3745 return 0;
3746 }
3747
3748 /**
3749 * Returns the width of the vertical scrollbar.
3750 *
3751 * @return The width in pixels of the vertical scrollbar or 0 if there
3752 * is no vertical scrollbar.
3753 */
3754 public int getVerticalScrollbarWidth() {
3755 ScrollabilityCache cache = mScrollCache;
3756 if (cache != null) {
3757 ScrollBarDrawable scrollBar = cache.scrollBar;
3758 if (scrollBar != null) {
3759 int size = scrollBar.getSize(true);
3760 if (size <= 0) {
3761 size = cache.scrollBarSize;
3762 }
3763 return size;
3764 }
3765 return 0;
3766 }
3767 return 0;
3768 }
3769
3770 /**
3771 * Returns the height of the horizontal scrollbar.
3772 *
3773 * @return The height in pixels of the horizontal scrollbar or 0 if
3774 * there is no horizontal scrollbar.
3775 */
3776 protected int getHorizontalScrollbarHeight() {
3777 ScrollabilityCache cache = mScrollCache;
3778 if (cache != null) {
3779 ScrollBarDrawable scrollBar = cache.scrollBar;
3780 if (scrollBar != null) {
3781 int size = scrollBar.getSize(false);
3782 if (size <= 0) {
3783 size = cache.scrollBarSize;
3784 }
3785 return size;
3786 }
3787 return 0;
3788 }
3789 return 0;
3790 }
3791
3792 /**
3793 * <p>
3794 * Initializes the scrollbars from a given set of styled attributes. This
3795 * method should be called by subclasses that need scrollbars and when an
3796 * instance of these subclasses is created programmatically rather than
3797 * being inflated from XML. This method is automatically called when the XML
3798 * is inflated.
3799 * </p>
3800 *
3801 * @param a the styled attributes set to initialize the scrollbars from
3802 */
3803 protected void initializeScrollbars(TypedArray a) {
3804 initScrollCache();
3805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003807
Mike Cleronf116bf82009-09-27 19:14:12 -07003808 if (scrollabilityCache.scrollBar == null) {
3809 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3810 }
Joe Malin32736f02011-01-19 16:14:20 -08003811
Romain Guy8bda2482010-03-02 11:42:11 -08003812 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813
Mike Cleronf116bf82009-09-27 19:14:12 -07003814 if (!fadeScrollbars) {
3815 scrollabilityCache.state = ScrollabilityCache.ON;
3816 }
3817 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003818
3819
Mike Cleronf116bf82009-09-27 19:14:12 -07003820 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3821 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3822 .getScrollBarFadeDuration());
3823 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3824 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3825 ViewConfiguration.getScrollDefaultDelay());
3826
Joe Malin32736f02011-01-19 16:14:20 -08003827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3829 com.android.internal.R.styleable.View_scrollbarSize,
3830 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3831
3832 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3833 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3834
3835 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3836 if (thumb != null) {
3837 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3838 }
3839
3840 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3841 false);
3842 if (alwaysDraw) {
3843 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3844 }
3845
3846 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3847 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3848
3849 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3850 if (thumb != null) {
3851 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3852 }
3853
3854 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3855 false);
3856 if (alwaysDraw) {
3857 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3858 }
3859
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003860 // Apply layout direction to the new Drawables if needed
3861 final int layoutDirection = getResolvedLayoutDirection();
3862 if (track != null) {
3863 track.setLayoutDirection(layoutDirection);
3864 }
3865 if (thumb != null) {
3866 thumb.setLayoutDirection(layoutDirection);
3867 }
3868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003870 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
3872
3873 /**
3874 * <p>
3875 * Initalizes the scrollability cache if necessary.
3876 * </p>
3877 */
3878 private void initScrollCache() {
3879 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003880 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 }
3882 }
3883
Philip Milne6c8ea062012-04-03 17:38:43 -07003884 private ScrollabilityCache getScrollCache() {
3885 initScrollCache();
3886 return mScrollCache;
3887 }
3888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 /**
Adam Powell20232d02010-12-08 21:08:53 -08003890 * Set the position of the vertical scroll bar. Should be one of
3891 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3892 * {@link #SCROLLBAR_POSITION_RIGHT}.
3893 *
3894 * @param position Where the vertical scroll bar should be positioned.
3895 */
3896 public void setVerticalScrollbarPosition(int position) {
3897 if (mVerticalScrollbarPosition != position) {
3898 mVerticalScrollbarPosition = position;
3899 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003900 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003901 }
3902 }
3903
3904 /**
3905 * @return The position where the vertical scroll bar will show, if applicable.
3906 * @see #setVerticalScrollbarPosition(int)
3907 */
3908 public int getVerticalScrollbarPosition() {
3909 return mVerticalScrollbarPosition;
3910 }
3911
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003912 ListenerInfo getListenerInfo() {
3913 if (mListenerInfo != null) {
3914 return mListenerInfo;
3915 }
3916 mListenerInfo = new ListenerInfo();
3917 return mListenerInfo;
3918 }
3919
Adam Powell20232d02010-12-08 21:08:53 -08003920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 * Register a callback to be invoked when focus of this view changed.
3922 *
3923 * @param l The callback that will run.
3924 */
3925 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003926 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 }
3928
3929 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003930 * Add a listener that will be called when the bounds of the view change due to
3931 * layout processing.
3932 *
3933 * @param listener The listener that will be called when layout bounds change.
3934 */
3935 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003936 ListenerInfo li = getListenerInfo();
3937 if (li.mOnLayoutChangeListeners == null) {
3938 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003939 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003940 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3941 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003942 }
Chet Haase21cd1382010-09-01 17:42:29 -07003943 }
3944
3945 /**
3946 * Remove a listener for layout changes.
3947 *
3948 * @param listener The listener for layout bounds change.
3949 */
3950 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 ListenerInfo li = mListenerInfo;
3952 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003953 return;
3954 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003956 }
3957
3958 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003959 * Add a listener for attach state changes.
3960 *
3961 * This listener will be called whenever this view is attached or detached
3962 * from a window. Remove the listener using
3963 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3964 *
3965 * @param listener Listener to attach
3966 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3967 */
3968 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003969 ListenerInfo li = getListenerInfo();
3970 if (li.mOnAttachStateChangeListeners == null) {
3971 li.mOnAttachStateChangeListeners
3972 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003973 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003974 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003975 }
3976
3977 /**
3978 * Remove a listener for attach state changes. The listener will receive no further
3979 * notification of window attach/detach events.
3980 *
3981 * @param listener Listener to remove
3982 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3983 */
3984 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003987 return;
3988 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003989 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003990 }
3991
3992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 * Returns the focus-change callback registered for this view.
3994 *
3995 * @return The callback, or null if one is not registered.
3996 */
3997 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003998 ListenerInfo li = mListenerInfo;
3999 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 }
4001
4002 /**
4003 * Register a callback to be invoked when this view is clicked. If this view is not
4004 * clickable, it becomes clickable.
4005 *
4006 * @param l The callback that will run
4007 *
4008 * @see #setClickable(boolean)
4009 */
4010 public void setOnClickListener(OnClickListener l) {
4011 if (!isClickable()) {
4012 setClickable(true);
4013 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004014 getListenerInfo().mOnClickListener = l;
4015 }
4016
4017 /**
4018 * Return whether this view has an attached OnClickListener. Returns
4019 * true if there is a listener, false if there is none.
4020 */
4021 public boolean hasOnClickListeners() {
4022 ListenerInfo li = mListenerInfo;
4023 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025
4026 /**
4027 * Register a callback to be invoked when this view is clicked and held. If this view is not
4028 * long clickable, it becomes long clickable.
4029 *
4030 * @param l The callback that will run
4031 *
4032 * @see #setLongClickable(boolean)
4033 */
4034 public void setOnLongClickListener(OnLongClickListener l) {
4035 if (!isLongClickable()) {
4036 setLongClickable(true);
4037 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 /**
4042 * Register a callback to be invoked when the context menu for this view is
4043 * being built. If this view is not long clickable, it becomes long clickable.
4044 *
4045 * @param l The callback that will run
4046 *
4047 */
4048 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4049 if (!isLongClickable()) {
4050 setLongClickable(true);
4051 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004052 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
4054
4055 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004056 * Call this view's OnClickListener, if it is defined. Performs all normal
4057 * actions associated with clicking: reporting accessibility event, playing
4058 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 *
4060 * @return True there was an assigned OnClickListener that was called, false
4061 * otherwise is returned.
4062 */
4063 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004064 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4065
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004066 ListenerInfo li = mListenerInfo;
4067 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004069 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 return true;
4071 }
4072
4073 return false;
4074 }
4075
4076 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004077 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4078 * this only calls the listener, and does not do any associated clicking
4079 * actions like reporting an accessibility event.
4080 *
4081 * @return True there was an assigned OnClickListener that was called, false
4082 * otherwise is returned.
4083 */
4084 public boolean callOnClick() {
4085 ListenerInfo li = mListenerInfo;
4086 if (li != null && li.mOnClickListener != null) {
4087 li.mOnClickListener.onClick(this);
4088 return true;
4089 }
4090 return false;
4091 }
4092
4093 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004094 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4095 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004097 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 */
4099 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004100 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004103 ListenerInfo li = mListenerInfo;
4104 if (li != null && li.mOnLongClickListener != null) {
4105 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 if (!handled) {
4108 handled = showContextMenu();
4109 }
4110 if (handled) {
4111 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4112 }
4113 return handled;
4114 }
4115
4116 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004117 * Performs button-related actions during a touch down event.
4118 *
4119 * @param event The event.
4120 * @return True if the down was consumed.
4121 *
4122 * @hide
4123 */
4124 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4125 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4126 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4127 return true;
4128 }
4129 }
4130 return false;
4131 }
4132
4133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * Bring up the context menu for this view.
4135 *
4136 * @return Whether a context menu was displayed.
4137 */
4138 public boolean showContextMenu() {
4139 return getParent().showContextMenuForChild(this);
4140 }
4141
4142 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004143 * Bring up the context menu for this view, referring to the item under the specified point.
4144 *
4145 * @param x The referenced x coordinate.
4146 * @param y The referenced y coordinate.
4147 * @param metaState The keyboard modifiers that were pressed.
4148 * @return Whether a context menu was displayed.
4149 *
4150 * @hide
4151 */
4152 public boolean showContextMenu(float x, float y, int metaState) {
4153 return showContextMenu();
4154 }
4155
4156 /**
Adam Powell6e346362010-07-23 10:18:23 -07004157 * Start an action mode.
4158 *
4159 * @param callback Callback that will control the lifecycle of the action mode
4160 * @return The new action mode if it is started, null otherwise
4161 *
4162 * @see ActionMode
4163 */
4164 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004165 ViewParent parent = getParent();
4166 if (parent == null) return null;
4167 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004168 }
4169
4170 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004171 * Register a callback to be invoked when a hardware key is pressed in this view.
4172 * Key presses in software input methods will generally not trigger the methods of
4173 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 * @param l the key listener to attach to this view
4175 */
4176 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004177 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 }
4179
4180 /**
4181 * Register a callback to be invoked when a touch event is sent to this view.
4182 * @param l the touch listener to attach to this view
4183 */
4184 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004185 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
4187
4188 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004189 * Register a callback to be invoked when a generic motion event is sent to this view.
4190 * @param l the generic motion listener to attach to this view
4191 */
4192 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004193 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004194 }
4195
4196 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004197 * Register a callback to be invoked when a hover event is sent to this view.
4198 * @param l the hover listener to attach to this view
4199 */
4200 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004201 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004202 }
4203
4204 /**
Joe Malin32736f02011-01-19 16:14:20 -08004205 * Register a drag event listener callback object for this View. The parameter is
4206 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4207 * View, the system calls the
4208 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4209 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004210 */
4211 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004212 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004213 }
4214
4215 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004216 * Give this view focus. This will cause
4217 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 *
4219 * Note: this does not check whether this {@link View} should get focus, it just
4220 * gives it focus no matter what. It should only be called internally by framework
4221 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4222 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004223 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4224 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 * focus moved when requestFocus() is called. It may not always
4226 * apply, in which case use the default View.FOCUS_DOWN.
4227 * @param previouslyFocusedRect The rectangle of the view that had focus
4228 * prior in this View's coordinate system.
4229 */
4230 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4231 if (DBG) {
4232 System.out.println(this + " requestFocus()");
4233 }
4234
Dianne Hackborn4702a852012-08-17 15:18:29 -07004235 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4236 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237
4238 if (mParent != null) {
4239 mParent.requestChildFocus(this, this);
4240 }
4241
4242 onFocusChanged(true, direction, previouslyFocusedRect);
4243 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
4251 /**
4252 * Request that a rectangle of this view be visible on the screen,
4253 * scrolling if necessary just enough.
4254 *
4255 * <p>A View should call this if it maintains some notion of which part
4256 * of its content is interesting. For example, a text editing view
4257 * should call this when its cursor moves.
4258 *
4259 * @param rectangle The rectangle.
4260 * @return Whether any parent scrolled.
4261 */
4262 public boolean requestRectangleOnScreen(Rect rectangle) {
4263 return requestRectangleOnScreen(rectangle, false);
4264 }
4265
4266 /**
4267 * Request that a rectangle of this view be visible on the screen,
4268 * scrolling if necessary just enough.
4269 *
4270 * <p>A View should call this if it maintains some notion of which part
4271 * of its content is interesting. For example, a text editing view
4272 * should call this when its cursor moves.
4273 *
4274 * <p>When <code>immediate</code> is set to true, scrolling will not be
4275 * animated.
4276 *
4277 * @param rectangle The rectangle.
4278 * @param immediate True to forbid animated scrolling, false otherwise
4279 * @return Whether any parent scrolled.
4280 */
4281 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4282 View child = this;
4283 ViewParent parent = mParent;
4284 boolean scrolled = false;
4285 while (parent != null) {
4286 scrolled |= parent.requestChildRectangleOnScreen(child,
4287 rectangle, immediate);
4288
4289 // offset rect so next call has the rectangle in the
4290 // coordinate system of its direct child.
4291 rectangle.offset(child.getLeft(), child.getTop());
4292 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4293
4294 if (!(parent instanceof View)) {
4295 break;
4296 }
Romain Guy8506ab42009-06-11 17:35:47 -07004297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 child = (View) parent;
4299 parent = child.getParent();
4300 }
4301 return scrolled;
4302 }
4303
4304 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004305 * Called when this view wants to give up focus. If focus is cleared
4306 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4307 * <p>
4308 * <strong>Note:</strong> When a View clears focus the framework is trying
4309 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004310 * View is the first from the top that can take focus, then all callbacks
4311 * related to clearing focus will be invoked after wich the framework will
4312 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004313 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 */
4315 public void clearFocus() {
4316 if (DBG) {
4317 System.out.println(this + " clearFocus()");
4318 }
4319
Dianne Hackborn4702a852012-08-17 15:18:29 -07004320 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4321 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322
4323 if (mParent != null) {
4324 mParent.clearChildFocus(this);
4325 }
4326
4327 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004330
4331 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004332
4333 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4334 notifyAccessibilityStateChanged();
4335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 }
4337 }
4338
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004339 void ensureInputFocusOnFirstFocusable() {
4340 View root = getRootView();
4341 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004342 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 }
4344 }
4345
4346 /**
4347 * Called internally by the view system when a new view is getting focus.
4348 * This is what clears the old focus.
4349 */
4350 void unFocus() {
4351 if (DBG) {
4352 System.out.println(this + " unFocus()");
4353 }
4354
Dianne Hackborn4702a852012-08-17 15:18:29 -07004355 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4356 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357
4358 onFocusChanged(false, 0, null);
4359 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004360
4361 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4362 notifyAccessibilityStateChanged();
4363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365 }
4366
4367 /**
4368 * Returns true if this view has focus iteself, or is the ancestor of the
4369 * view that has focus.
4370 *
4371 * @return True if this view has or contains focus, false otherwise.
4372 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004373 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004375 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
4377
4378 /**
4379 * Returns true if this view is focusable or if it contains a reachable View
4380 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4381 * is a View whose parents do not block descendants focus.
4382 *
4383 * Only {@link #VISIBLE} views are considered focusable.
4384 *
4385 * @return True if the view is focusable or if the view contains a focusable
4386 * View, false otherwise.
4387 *
4388 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4389 */
4390 public boolean hasFocusable() {
4391 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4392 }
4393
4394 /**
4395 * Called by the view system when the focus state of this view changes.
4396 * When the focus change event is caused by directional navigation, direction
4397 * and previouslyFocusedRect provide insight into where the focus is coming from.
4398 * When overriding, be sure to call up through to the super class so that
4399 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 * @param gainFocus True if the View has focus; false otherwise.
4402 * @param direction The direction focus has moved when requestFocus()
4403 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004404 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4405 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4406 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4408 * system, of the previously focused view. If applicable, this will be
4409 * passed in as finer grained information about where the focus is coming
4410 * from (in addition to direction). Will be <code>null</code> otherwise.
4411 */
4412 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004413 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004414 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4415 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004416 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004417 }
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 InputMethodManager imm = InputMethodManager.peekInstance();
4420 if (!gainFocus) {
4421 if (isPressed()) {
4422 setPressed(false);
4423 }
4424 if (imm != null && mAttachInfo != null
4425 && mAttachInfo.mHasWindowFocus) {
4426 imm.focusOut(this);
4427 }
Romain Guya2431d02009-04-30 16:30:00 -07004428 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 } else if (imm != null && mAttachInfo != null
4430 && mAttachInfo.mHasWindowFocus) {
4431 imm.focusIn(this);
4432 }
Romain Guy8506ab42009-06-11 17:35:47 -07004433
Romain Guy0fd89bf2011-01-26 15:41:30 -08004434 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004435 ListenerInfo li = mListenerInfo;
4436 if (li != null && li.mOnFocusChangeListener != null) {
4437 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
Joe Malin32736f02011-01-19 16:14:20 -08004439
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004440 if (mAttachInfo != null) {
4441 mAttachInfo.mKeyDispatchState.reset(this);
4442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 }
4444
4445 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004446 * Sends an accessibility event of the given type. If accessiiblity is
4447 * not enabled this method has no effect. The default implementation calls
4448 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4449 * to populate information about the event source (this View), then calls
4450 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4451 * populate the text content of the event source including its descendants,
4452 * and last calls
4453 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4454 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004455 * <p>
4456 * If an {@link AccessibilityDelegate} has been specified via calling
4457 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4458 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4459 * responsible for handling this call.
4460 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 *
Scott Mainb303d832011-10-12 16:45:18 -07004462 * @param eventType The type of the event to send, as defined by several types from
4463 * {@link android.view.accessibility.AccessibilityEvent}, such as
4464 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4465 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004466 *
4467 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4468 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4469 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004470 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004471 */
4472 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004473 if (mAccessibilityDelegate != null) {
4474 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4475 } else {
4476 sendAccessibilityEventInternal(eventType);
4477 }
4478 }
4479
4480 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004481 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4482 * {@link AccessibilityEvent} to make an announcement which is related to some
4483 * sort of a context change for which none of the events representing UI transitions
4484 * is a good fit. For example, announcing a new page in a book. If accessibility
4485 * is not enabled this method does nothing.
4486 *
4487 * @param text The announcement text.
4488 */
4489 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004490 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004491 AccessibilityEvent event = AccessibilityEvent.obtain(
4492 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004493 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004494 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004495 event.setContentDescription(null);
4496 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004497 }
4498 }
4499
4500 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004501 * @see #sendAccessibilityEvent(int)
4502 *
4503 * Note: Called from the default {@link AccessibilityDelegate}.
4504 */
4505 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004506 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4507 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4508 }
4509 }
4510
4511 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004512 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4513 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004514 * perform a check whether accessibility is enabled.
4515 * <p>
4516 * If an {@link AccessibilityDelegate} has been specified via calling
4517 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4518 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4519 * is responsible for handling this call.
4520 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004521 *
4522 * @param event The event to send.
4523 *
4524 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004525 */
4526 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004527 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004528 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004529 } else {
4530 sendAccessibilityEventUncheckedInternal(event);
4531 }
4532 }
4533
4534 /**
4535 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4536 *
4537 * Note: Called from the default {@link AccessibilityDelegate}.
4538 */
4539 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004540 if (!isShown()) {
4541 return;
4542 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004543 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004544 // Only a subset of accessibility events populates text content.
4545 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4546 dispatchPopulateAccessibilityEvent(event);
4547 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004548 // In the beginning we called #isShown(), so we know that getParent() is not null.
4549 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004550 }
4551
4552 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004553 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4554 * to its children for adding their text content to the event. Note that the
4555 * event text is populated in a separate dispatch path since we add to the
4556 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004557 * A typical implementation will call
4558 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4559 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4560 * on each child. Override this method if custom population of the event text
4561 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004562 * <p>
4563 * If an {@link AccessibilityDelegate} has been specified via calling
4564 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4565 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4566 * is responsible for handling this call.
4567 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004568 * <p>
4569 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4570 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4571 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004572 *
4573 * @param event The event.
4574 *
4575 * @return True if the event population was completed.
4576 */
4577 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004578 if (mAccessibilityDelegate != null) {
4579 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4580 } else {
4581 return dispatchPopulateAccessibilityEventInternal(event);
4582 }
4583 }
4584
4585 /**
4586 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4587 *
4588 * Note: Called from the default {@link AccessibilityDelegate}.
4589 */
4590 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004591 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004592 return false;
4593 }
4594
4595 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004596 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004597 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004598 * text content. While this method is free to modify event
4599 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4601 * <p>
4602 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004603 * to the text added by the super implementation:
4604 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004605 * super.onPopulateAccessibilityEvent(event);
4606 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4607 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4608 * mCurrentDate.getTimeInMillis(), flags);
4609 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004610 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004611 * <p>
4612 * If an {@link AccessibilityDelegate} has been specified via calling
4613 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4614 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4615 * is responsible for handling this call.
4616 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004617 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4618 * information to the event, in case the default implementation has basic information to add.
4619 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004620 *
4621 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004622 *
4623 * @see #sendAccessibilityEvent(int)
4624 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004625 */
4626 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004627 if (mAccessibilityDelegate != null) {
4628 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4629 } else {
4630 onPopulateAccessibilityEventInternal(event);
4631 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004632 }
4633
4634 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004635 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4636 *
4637 * Note: Called from the default {@link AccessibilityDelegate}.
4638 */
4639 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4640
4641 }
4642
4643 /**
4644 * Initializes an {@link AccessibilityEvent} with information about
4645 * this View which is the event source. In other words, the source of
4646 * an accessibility event is the view whose state change triggered firing
4647 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004648 * <p>
4649 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004650 * to properties set by the super implementation:
4651 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4652 * super.onInitializeAccessibilityEvent(event);
4653 * event.setPassword(true);
4654 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004655 * <p>
4656 * If an {@link AccessibilityDelegate} has been specified via calling
4657 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4658 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4659 * is responsible for handling this call.
4660 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004661 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4662 * information to the event, in case the default implementation has basic information to add.
4663 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004664 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004665 *
4666 * @see #sendAccessibilityEvent(int)
4667 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4668 */
4669 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004670 if (mAccessibilityDelegate != null) {
4671 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4672 } else {
4673 onInitializeAccessibilityEventInternal(event);
4674 }
4675 }
4676
4677 /**
4678 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4679 *
4680 * Note: Called from the default {@link AccessibilityDelegate}.
4681 */
4682 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004683 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004684 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004685 event.setPackageName(getContext().getPackageName());
4686 event.setEnabled(isEnabled());
4687 event.setContentDescription(mContentDescription);
4688
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004689 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004690 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004691 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4692 FOCUSABLES_ALL);
4693 event.setItemCount(focusablesTempList.size());
4694 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4695 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004696 }
4697 }
4698
4699 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004700 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4701 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4702 * This method is responsible for obtaining an accessibility node info from a
4703 * pool of reusable instances and calling
4704 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4705 * initialize the former.
4706 * <p>
4707 * Note: The client is responsible for recycling the obtained instance by calling
4708 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4709 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004710 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004711 * @return A populated {@link AccessibilityNodeInfo}.
4712 *
4713 * @see AccessibilityNodeInfo
4714 */
4715 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004716 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4717 if (provider != null) {
4718 return provider.createAccessibilityNodeInfo(View.NO_ID);
4719 } else {
4720 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4721 onInitializeAccessibilityNodeInfo(info);
4722 return info;
4723 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004724 }
4725
4726 /**
4727 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4728 * The base implementation sets:
4729 * <ul>
4730 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004731 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4732 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004733 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4734 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4735 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4736 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4737 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4738 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4739 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4740 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4741 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4742 * </ul>
4743 * <p>
4744 * Subclasses should override this method, call the super implementation,
4745 * and set additional attributes.
4746 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004747 * <p>
4748 * If an {@link AccessibilityDelegate} has been specified via calling
4749 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4750 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4751 * is responsible for handling this call.
4752 * </p>
4753 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004754 * @param info The instance to initialize.
4755 */
4756 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004757 if (mAccessibilityDelegate != null) {
4758 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4759 } else {
4760 onInitializeAccessibilityNodeInfoInternal(info);
4761 }
4762 }
4763
4764 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004765 * Gets the location of this view in screen coordintates.
4766 *
4767 * @param outRect The output location
4768 */
4769 private void getBoundsOnScreen(Rect outRect) {
4770 if (mAttachInfo == null) {
4771 return;
4772 }
4773
4774 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004775 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004776
4777 if (!hasIdentityMatrix()) {
4778 getMatrix().mapRect(position);
4779 }
4780
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004781 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004782
4783 ViewParent parent = mParent;
4784 while (parent instanceof View) {
4785 View parentView = (View) parent;
4786
4787 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4788
4789 if (!parentView.hasIdentityMatrix()) {
4790 parentView.getMatrix().mapRect(position);
4791 }
4792
4793 position.offset(parentView.mLeft, parentView.mTop);
4794
4795 parent = parentView.mParent;
4796 }
4797
4798 if (parent instanceof ViewRootImpl) {
4799 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4800 position.offset(0, -viewRootImpl.mCurScrollY);
4801 }
4802
4803 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4804
4805 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4806 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4807 }
4808
4809 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004810 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4811 *
4812 * Note: Called from the default {@link AccessibilityDelegate}.
4813 */
4814 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004815 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004816
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004817 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004818 info.setBoundsInParent(bounds);
4819
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004820 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004821 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004822
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004823 ViewParent parent = getParentForAccessibility();
4824 if (parent instanceof View) {
4825 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004826 }
4827
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004828 info.setVisibleToUser(isVisibleToUser());
4829
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004830 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004831 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004832 info.setContentDescription(getContentDescription());
4833
4834 info.setEnabled(isEnabled());
4835 info.setClickable(isClickable());
4836 info.setFocusable(isFocusable());
4837 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004838 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004839 info.setSelected(isSelected());
4840 info.setLongClickable(isLongClickable());
4841
4842 // TODO: These make sense only if we are in an AdapterView but all
4843 // views can be selected. Maybe from accessiiblity perspective
4844 // we should report as selectable view in an AdapterView.
4845 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4846 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4847
4848 if (isFocusable()) {
4849 if (isFocused()) {
4850 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4851 } else {
4852 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4853 }
4854 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004855
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004856 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004857 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004858 } else {
4859 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4860 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004861
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004862 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004863 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4864 }
4865
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004866 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004867 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4868 }
4869
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004870 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004871 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4872 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4873 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004874 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4875 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004876 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004877 }
4878
4879 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004880 * Computes whether this view is visible to the user. Such a view is
4881 * attached, visible, all its predecessors are visible, it is not clipped
4882 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004883 *
4884 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004885 *
4886 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004887 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004888 protected boolean isVisibleToUser() {
4889 return isVisibleToUser(null);
4890 }
4891
4892 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004893 * Computes whether the given portion of this view is visible to the user.
4894 * Such a view is attached, visible, all its predecessors are visible,
4895 * has an alpha greater than zero, and the specified portion is not
4896 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004897 *
4898 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4899 * <code>null</code>, and the entire view will be tested in this case.
4900 * When <code>true</code> is returned by the function, the actual visible
4901 * region will be stored in this parameter; that is, if boundInView is fully
4902 * contained within the view, no modification will be made, otherwise regions
4903 * outside of the visible area of the view will be clipped.
4904 *
4905 * @return Whether the specified portion of the view is visible on the screen.
4906 *
4907 * @hide
4908 */
4909 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004910 if (mAttachInfo != null) {
4911 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4912 Point offset = mAttachInfo.mPoint;
4913 // The first two checks are made also made by isShown() which
4914 // however traverses the tree up to the parent to catch that.
4915 // Therefore, we do some fail fast check to minimize the up
4916 // tree traversal.
4917 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4918 && getAlpha() > 0
4919 && isShown()
4920 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004921 if (isVisible && boundInView != null) {
4922 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004923 // isVisible is always true here, use a simple assignment
4924 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004925 }
4926 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004927 }
4928
4929 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004930 }
4931
4932 /**
alanvb72fe7a2012-08-27 16:44:25 -07004933 * Returns the delegate for implementing accessibility support via
4934 * composition. For more details see {@link AccessibilityDelegate}.
4935 *
4936 * @return The delegate, or null if none set.
4937 *
4938 * @hide
4939 */
4940 public AccessibilityDelegate getAccessibilityDelegate() {
4941 return mAccessibilityDelegate;
4942 }
4943
4944 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004945 * Sets a delegate for implementing accessibility support via compositon as
4946 * opposed to inheritance. The delegate's primary use is for implementing
4947 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4948 *
4949 * @param delegate The delegate instance.
4950 *
4951 * @see AccessibilityDelegate
4952 */
4953 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4954 mAccessibilityDelegate = delegate;
4955 }
4956
4957 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004958 * Gets the provider for managing a virtual view hierarchy rooted at this View
4959 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4960 * that explore the window content.
4961 * <p>
4962 * If this method returns an instance, this instance is responsible for managing
4963 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4964 * View including the one representing the View itself. Similarly the returned
4965 * instance is responsible for performing accessibility actions on any virtual
4966 * view or the root view itself.
4967 * </p>
4968 * <p>
4969 * If an {@link AccessibilityDelegate} has been specified via calling
4970 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4971 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4972 * is responsible for handling this call.
4973 * </p>
4974 *
4975 * @return The provider.
4976 *
4977 * @see AccessibilityNodeProvider
4978 */
4979 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4980 if (mAccessibilityDelegate != null) {
4981 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4982 } else {
4983 return null;
4984 }
4985 }
4986
4987 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004988 * Gets the unique identifier of this view on the screen for accessibility purposes.
4989 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4990 *
4991 * @return The view accessibility id.
4992 *
4993 * @hide
4994 */
4995 public int getAccessibilityViewId() {
4996 if (mAccessibilityViewId == NO_ID) {
4997 mAccessibilityViewId = sNextAccessibilityViewId++;
4998 }
4999 return mAccessibilityViewId;
5000 }
5001
5002 /**
5003 * Gets the unique identifier of the window in which this View reseides.
5004 *
5005 * @return The window accessibility id.
5006 *
5007 * @hide
5008 */
5009 public int getAccessibilityWindowId() {
5010 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5011 }
5012
5013 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005014 * Gets the {@link View} description. It briefly describes the view and is
5015 * primarily used for accessibility support. Set this property to enable
5016 * better accessibility support for your application. This is especially
5017 * true for views that do not have textual representation (For example,
5018 * ImageButton).
5019 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005020 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005021 *
5022 * @attr ref android.R.styleable#View_contentDescription
5023 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005024 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005025 public CharSequence getContentDescription() {
5026 return mContentDescription;
5027 }
5028
5029 /**
5030 * Sets the {@link View} description. It briefly describes the view and is
5031 * primarily used for accessibility support. Set this property to enable
5032 * better accessibility support for your application. This is especially
5033 * true for views that do not have textual representation (For example,
5034 * ImageButton).
5035 *
5036 * @param contentDescription The content description.
5037 *
5038 * @attr ref android.R.styleable#View_contentDescription
5039 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005040 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005041 public void setContentDescription(CharSequence contentDescription) {
5042 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005043 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5044 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5045 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5046 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005047 }
5048
5049 /**
Romain Guya2431d02009-04-30 16:30:00 -07005050 * Invoked whenever this view loses focus, either by losing window focus or by losing
5051 * focus within its window. This method can be used to clear any state tied to the
5052 * focus. For instance, if a button is held pressed with the trackball and the window
5053 * loses focus, this method can be used to cancel the press.
5054 *
5055 * Subclasses of View overriding this method should always call super.onFocusLost().
5056 *
5057 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005058 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005059 *
5060 * @hide pending API council approval
5061 */
5062 protected void onFocusLost() {
5063 resetPressedState();
5064 }
5065
5066 private void resetPressedState() {
5067 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5068 return;
5069 }
5070
5071 if (isPressed()) {
5072 setPressed(false);
5073
5074 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005075 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005076 }
5077 }
5078 }
5079
5080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 * Returns true if this view has focus
5082 *
5083 * @return True if this view has focus, false otherwise.
5084 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005085 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005087 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 }
5089
5090 /**
5091 * Find the view in the hierarchy rooted at this view that currently has
5092 * focus.
5093 *
5094 * @return The view that currently has focus, or null if no focused view can
5095 * be found.
5096 */
5097 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005098 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 }
5100
5101 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005102 * Indicates whether this view is one of the set of scrollable containers in
5103 * its window.
5104 *
5105 * @return whether this view is one of the set of scrollable containers in
5106 * its window
5107 *
5108 * @attr ref android.R.styleable#View_isScrollContainer
5109 */
5110 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005111 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005112 }
5113
5114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 * Change whether this view is one of the set of scrollable containers in
5116 * its window. This will be used to determine whether the window can
5117 * resize or must pan when a soft input area is open -- scrollable
5118 * containers allow the window to use resize mode since the container
5119 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005120 *
5121 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 */
5123 public void setScrollContainer(boolean isScrollContainer) {
5124 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005125 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005127 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005129 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005131 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 mAttachInfo.mScrollContainers.remove(this);
5133 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005134 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 }
5136 }
5137
5138 /**
5139 * Returns the quality of the drawing cache.
5140 *
5141 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5142 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5143 *
5144 * @see #setDrawingCacheQuality(int)
5145 * @see #setDrawingCacheEnabled(boolean)
5146 * @see #isDrawingCacheEnabled()
5147 *
5148 * @attr ref android.R.styleable#View_drawingCacheQuality
5149 */
5150 public int getDrawingCacheQuality() {
5151 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5152 }
5153
5154 /**
5155 * Set the drawing cache quality of this view. This value is used only when the
5156 * drawing cache is enabled
5157 *
5158 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5159 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5160 *
5161 * @see #getDrawingCacheQuality()
5162 * @see #setDrawingCacheEnabled(boolean)
5163 * @see #isDrawingCacheEnabled()
5164 *
5165 * @attr ref android.R.styleable#View_drawingCacheQuality
5166 */
5167 public void setDrawingCacheQuality(int quality) {
5168 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5169 }
5170
5171 /**
5172 * Returns whether the screen should remain on, corresponding to the current
5173 * value of {@link #KEEP_SCREEN_ON}.
5174 *
5175 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5176 *
5177 * @see #setKeepScreenOn(boolean)
5178 *
5179 * @attr ref android.R.styleable#View_keepScreenOn
5180 */
5181 public boolean getKeepScreenOn() {
5182 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5183 }
5184
5185 /**
5186 * Controls whether the screen should remain on, modifying the
5187 * value of {@link #KEEP_SCREEN_ON}.
5188 *
5189 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5190 *
5191 * @see #getKeepScreenOn()
5192 *
5193 * @attr ref android.R.styleable#View_keepScreenOn
5194 */
5195 public void setKeepScreenOn(boolean keepScreenOn) {
5196 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5197 }
5198
5199 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005200 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5201 * @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 -08005202 *
5203 * @attr ref android.R.styleable#View_nextFocusLeft
5204 */
5205 public int getNextFocusLeftId() {
5206 return mNextFocusLeftId;
5207 }
5208
5209 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005210 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5211 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5212 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 *
5214 * @attr ref android.R.styleable#View_nextFocusLeft
5215 */
5216 public void setNextFocusLeftId(int nextFocusLeftId) {
5217 mNextFocusLeftId = nextFocusLeftId;
5218 }
5219
5220 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005221 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5222 * @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 -08005223 *
5224 * @attr ref android.R.styleable#View_nextFocusRight
5225 */
5226 public int getNextFocusRightId() {
5227 return mNextFocusRightId;
5228 }
5229
5230 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005231 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5232 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5233 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 *
5235 * @attr ref android.R.styleable#View_nextFocusRight
5236 */
5237 public void setNextFocusRightId(int nextFocusRightId) {
5238 mNextFocusRightId = nextFocusRightId;
5239 }
5240
5241 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005242 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5243 * @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 -08005244 *
5245 * @attr ref android.R.styleable#View_nextFocusUp
5246 */
5247 public int getNextFocusUpId() {
5248 return mNextFocusUpId;
5249 }
5250
5251 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005252 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5253 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5254 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 *
5256 * @attr ref android.R.styleable#View_nextFocusUp
5257 */
5258 public void setNextFocusUpId(int nextFocusUpId) {
5259 mNextFocusUpId = nextFocusUpId;
5260 }
5261
5262 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005263 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5264 * @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 -08005265 *
5266 * @attr ref android.R.styleable#View_nextFocusDown
5267 */
5268 public int getNextFocusDownId() {
5269 return mNextFocusDownId;
5270 }
5271
5272 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005273 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5274 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5275 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 *
5277 * @attr ref android.R.styleable#View_nextFocusDown
5278 */
5279 public void setNextFocusDownId(int nextFocusDownId) {
5280 mNextFocusDownId = nextFocusDownId;
5281 }
5282
5283 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005284 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5285 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5286 *
5287 * @attr ref android.R.styleable#View_nextFocusForward
5288 */
5289 public int getNextFocusForwardId() {
5290 return mNextFocusForwardId;
5291 }
5292
5293 /**
5294 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5295 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5296 * decide automatically.
5297 *
5298 * @attr ref android.R.styleable#View_nextFocusForward
5299 */
5300 public void setNextFocusForwardId(int nextFocusForwardId) {
5301 mNextFocusForwardId = nextFocusForwardId;
5302 }
5303
5304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 * Returns the visibility of this view and all of its ancestors
5306 *
5307 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5308 */
5309 public boolean isShown() {
5310 View current = this;
5311 //noinspection ConstantConditions
5312 do {
5313 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5314 return false;
5315 }
5316 ViewParent parent = current.mParent;
5317 if (parent == null) {
5318 return false; // We are not attached to the view root
5319 }
5320 if (!(parent instanceof View)) {
5321 return true;
5322 }
5323 current = (View) parent;
5324 } while (current != null);
5325
5326 return false;
5327 }
5328
5329 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005330 * Called by the view hierarchy when the content insets for a window have
5331 * changed, to allow it to adjust its content to fit within those windows.
5332 * The content insets tell you the space that the status bar, input method,
5333 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005335 * <p>You do not normally need to deal with this function, since the default
5336 * window decoration given to applications takes care of applying it to the
5337 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5338 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5339 * and your content can be placed under those system elements. You can then
5340 * use this method within your view hierarchy if you have parts of your UI
5341 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005343 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005344 * inset's to the view's padding, consuming that content (modifying the
5345 * insets to be 0), and returning true. This behavior is off by default, but can
5346 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5347 *
5348 * <p>This function's traversal down the hierarchy is depth-first. The same content
5349 * insets object is propagated down the hierarchy, so any changes made to it will
5350 * be seen by all following views (including potentially ones above in
5351 * the hierarchy since this is a depth-first traversal). The first view
5352 * that returns true will abort the entire traversal.
5353 *
5354 * <p>The default implementation works well for a situation where it is
5355 * used with a container that covers the entire window, allowing it to
5356 * apply the appropriate insets to its content on all edges. If you need
5357 * a more complicated layout (such as two different views fitting system
5358 * windows, one on the top of the window, and one on the bottom),
5359 * you can override the method and handle the insets however you would like.
5360 * Note that the insets provided by the framework are always relative to the
5361 * far edges of the window, not accounting for the location of the called view
5362 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005363 * where the layout will place the view, as it is done before layout happens.)
5364 *
5365 * <p>Note: unlike many View methods, there is no dispatch phase to this
5366 * call. If you are overriding it in a ViewGroup and want to allow the
5367 * call to continue to your children, you must be sure to call the super
5368 * implementation.
5369 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005370 * <p>Here is a sample layout that makes use of fitting system windows
5371 * to have controls for a video view placed inside of the window decorations
5372 * that it hides and shows. This can be used with code like the second
5373 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5374 *
5375 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5376 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005377 * @param insets Current content insets of the window. Prior to
5378 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5379 * the insets or else you and Android will be unhappy.
5380 *
5381 * @return Return true if this view applied the insets and it should not
5382 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005383 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005384 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005385 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 */
5387 protected boolean fitSystemWindows(Rect insets) {
5388 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005389 mUserPaddingStart = UNDEFINED_PADDING;
5390 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005391 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5392 || mAttachInfo == null
5393 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5394 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5395 return true;
5396 } else {
5397 internalSetPadding(0, 0, 0, 0);
5398 return false;
5399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 }
5401 return false;
5402 }
5403
5404 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005405 * Sets whether or not this view should account for system screen decorations
5406 * such as the status bar and inset its content; that is, controlling whether
5407 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5408 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005409 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005410 * <p>Note that if you are providing your own implementation of
5411 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5412 * flag to true -- your implementation will be overriding the default
5413 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005414 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005415 * @param fitSystemWindows If true, then the default implementation of
5416 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005417 *
5418 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005419 * @see #getFitsSystemWindows()
5420 * @see #fitSystemWindows(Rect)
5421 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005422 */
5423 public void setFitsSystemWindows(boolean fitSystemWindows) {
5424 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5425 }
5426
5427 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005428 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005429 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5430 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005431 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005432 * @return Returns true if the default implementation of
5433 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005434 *
5435 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005436 * @see #setFitsSystemWindows()
5437 * @see #fitSystemWindows(Rect)
5438 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005439 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005440 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005441 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5442 }
5443
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005444 /** @hide */
5445 public boolean fitsSystemWindows() {
5446 return getFitsSystemWindows();
5447 }
5448
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005449 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005450 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5451 */
5452 public void requestFitSystemWindows() {
5453 if (mParent != null) {
5454 mParent.requestFitSystemWindows();
5455 }
5456 }
5457
5458 /**
5459 * For use by PhoneWindow to make its own system window fitting optional.
5460 * @hide
5461 */
5462 public void makeOptionalFitsSystemWindows() {
5463 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5464 }
5465
5466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 * Returns the visibility status for this view.
5468 *
5469 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5470 * @attr ref android.R.styleable#View_visibility
5471 */
5472 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005473 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5474 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5475 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 })
5477 public int getVisibility() {
5478 return mViewFlags & VISIBILITY_MASK;
5479 }
5480
5481 /**
5482 * Set the enabled state of this view.
5483 *
5484 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5485 * @attr ref android.R.styleable#View_visibility
5486 */
5487 @RemotableViewMethod
5488 public void setVisibility(int visibility) {
5489 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005490 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 }
5492
5493 /**
5494 * Returns the enabled status for this view. The interpretation of the
5495 * enabled state varies by subclass.
5496 *
5497 * @return True if this view is enabled, false otherwise.
5498 */
5499 @ViewDebug.ExportedProperty
5500 public boolean isEnabled() {
5501 return (mViewFlags & ENABLED_MASK) == ENABLED;
5502 }
5503
5504 /**
5505 * Set the enabled state of this view. The interpretation of the enabled
5506 * state varies by subclass.
5507 *
5508 * @param enabled True if this view is enabled, false otherwise.
5509 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005510 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005511 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005512 if (enabled == isEnabled()) return;
5513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5515
5516 /*
5517 * The View most likely has to change its appearance, so refresh
5518 * the drawable state.
5519 */
5520 refreshDrawableState();
5521
5522 // Invalidate too, since the default behavior for views is to be
5523 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005524 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525 }
5526
5527 /**
5528 * Set whether this view can receive the focus.
5529 *
5530 * Setting this to false will also ensure that this view is not focusable
5531 * in touch mode.
5532 *
5533 * @param focusable If true, this view can receive the focus.
5534 *
5535 * @see #setFocusableInTouchMode(boolean)
5536 * @attr ref android.R.styleable#View_focusable
5537 */
5538 public void setFocusable(boolean focusable) {
5539 if (!focusable) {
5540 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5541 }
5542 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5543 }
5544
5545 /**
5546 * Set whether this view can receive focus while in touch mode.
5547 *
5548 * Setting this to true will also ensure that this view is focusable.
5549 *
5550 * @param focusableInTouchMode If true, this view can receive the focus while
5551 * in touch mode.
5552 *
5553 * @see #setFocusable(boolean)
5554 * @attr ref android.R.styleable#View_focusableInTouchMode
5555 */
5556 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5557 // Focusable in touch mode should always be set before the focusable flag
5558 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5559 // which, in touch mode, will not successfully request focus on this view
5560 // because the focusable in touch mode flag is not set
5561 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5562 if (focusableInTouchMode) {
5563 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5564 }
5565 }
5566
5567 /**
5568 * Set whether this view should have sound effects enabled for events such as
5569 * clicking and touching.
5570 *
5571 * <p>You may wish to disable sound effects for a view if you already play sounds,
5572 * for instance, a dial key that plays dtmf tones.
5573 *
5574 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5575 * @see #isSoundEffectsEnabled()
5576 * @see #playSoundEffect(int)
5577 * @attr ref android.R.styleable#View_soundEffectsEnabled
5578 */
5579 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5580 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5581 }
5582
5583 /**
5584 * @return whether this view should have sound effects enabled for events such as
5585 * clicking and touching.
5586 *
5587 * @see #setSoundEffectsEnabled(boolean)
5588 * @see #playSoundEffect(int)
5589 * @attr ref android.R.styleable#View_soundEffectsEnabled
5590 */
5591 @ViewDebug.ExportedProperty
5592 public boolean isSoundEffectsEnabled() {
5593 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5594 }
5595
5596 /**
5597 * Set whether this view should have haptic feedback for events such as
5598 * long presses.
5599 *
5600 * <p>You may wish to disable haptic feedback if your view already controls
5601 * its own haptic feedback.
5602 *
5603 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5604 * @see #isHapticFeedbackEnabled()
5605 * @see #performHapticFeedback(int)
5606 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5607 */
5608 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5609 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5610 }
5611
5612 /**
5613 * @return whether this view should have haptic feedback enabled for events
5614 * long presses.
5615 *
5616 * @see #setHapticFeedbackEnabled(boolean)
5617 * @see #performHapticFeedback(int)
5618 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5619 */
5620 @ViewDebug.ExportedProperty
5621 public boolean isHapticFeedbackEnabled() {
5622 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5623 }
5624
5625 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005626 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005627 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005628 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5629 * {@link #LAYOUT_DIRECTION_RTL},
5630 * {@link #LAYOUT_DIRECTION_INHERIT} or
5631 * {@link #LAYOUT_DIRECTION_LOCALE}.
5632 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005633 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005634 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005635 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5636 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5637 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5638 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005639 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005640 public int getLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005641 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005642 }
5643
5644 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005645 * Set the layout direction for this view. This will propagate a reset of layout direction
5646 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005647 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005648 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5649 * {@link #LAYOUT_DIRECTION_RTL},
5650 * {@link #LAYOUT_DIRECTION_INHERIT} or
5651 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005652 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005653 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005654 */
5655 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005656 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005657 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005658 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005659 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005660 resetResolvedLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005661 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -07005662 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005663 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005664 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005665 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005666 resolveRtlProperties();
5667 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005668 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005669 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005670 }
5671
5672 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005673 * Returns the resolved layout direction for this view.
5674 *
5675 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005676 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005677 */
5678 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005679 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5680 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005681 })
5682 public int getResolvedLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005683 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5684 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005685 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005686 return LAYOUT_DIRECTION_LTR;
5687 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005688 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005689 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005690 resolveLayoutDirection();
5691 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005692 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005693 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5694 }
5695
5696 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005697 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5698 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005699 *
5700 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005701 */
5702 @ViewDebug.ExportedProperty(category = "layout")
5703 public boolean isLayoutRtl() {
5704 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5705 }
5706
5707 /**
Adam Powell539ee872012-02-03 19:00:49 -08005708 * Indicates whether the view is currently tracking transient state that the
5709 * app should not need to concern itself with saving and restoring, but that
5710 * the framework should take special note to preserve when possible.
5711 *
Adam Powell785c4472012-05-02 21:25:39 -07005712 * <p>A view with transient state cannot be trivially rebound from an external
5713 * data source, such as an adapter binding item views in a list. This may be
5714 * because the view is performing an animation, tracking user selection
5715 * of content, or similar.</p>
5716 *
Adam Powell539ee872012-02-03 19:00:49 -08005717 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005718 */
5719 @ViewDebug.ExportedProperty(category = "layout")
5720 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005721 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005722 }
5723
5724 /**
5725 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005726 * framework should attempt to preserve when possible. This flag is reference counted,
5727 * so every call to setHasTransientState(true) should be paired with a later call
5728 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005729 *
Adam Powell785c4472012-05-02 21:25:39 -07005730 * <p>A view with transient state cannot be trivially rebound from an external
5731 * data source, such as an adapter binding item views in a list. This may be
5732 * because the view is performing an animation, tracking user selection
5733 * of content, or similar.</p>
5734 *
Adam Powell539ee872012-02-03 19:00:49 -08005735 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005736 */
5737 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005738 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5739 mTransientStateCount - 1;
5740 if (mTransientStateCount < 0) {
5741 mTransientStateCount = 0;
5742 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5743 "unmatched pair of setHasTransientState calls");
5744 }
5745 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005746 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005747 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005748 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5749 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005750 if (mParent != null) {
5751 try {
5752 mParent.childHasTransientStateChanged(this, hasTransientState);
5753 } catch (AbstractMethodError e) {
5754 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5755 " does not fully implement ViewParent", e);
5756 }
Adam Powell539ee872012-02-03 19:00:49 -08005757 }
5758 }
5759 }
5760
5761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 * If this view doesn't do any drawing on its own, set this flag to
5763 * allow further optimizations. By default, this flag is not set on
5764 * View, but could be set on some View subclasses such as ViewGroup.
5765 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005766 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5767 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 *
5769 * @param willNotDraw whether or not this View draw on its own
5770 */
5771 public void setWillNotDraw(boolean willNotDraw) {
5772 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5773 }
5774
5775 /**
5776 * Returns whether or not this View draws on its own.
5777 *
5778 * @return true if this view has nothing to draw, false otherwise
5779 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005780 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 public boolean willNotDraw() {
5782 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5783 }
5784
5785 /**
5786 * When a View's drawing cache is enabled, drawing is redirected to an
5787 * offscreen bitmap. Some views, like an ImageView, must be able to
5788 * bypass this mechanism if they already draw a single bitmap, to avoid
5789 * unnecessary usage of the memory.
5790 *
5791 * @param willNotCacheDrawing true if this view does not cache its
5792 * drawing, false otherwise
5793 */
5794 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5795 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5796 }
5797
5798 /**
5799 * Returns whether or not this View can cache its drawing or not.
5800 *
5801 * @return true if this view does not cache its drawing, false otherwise
5802 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005803 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 public boolean willNotCacheDrawing() {
5805 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5806 }
5807
5808 /**
5809 * Indicates whether this view reacts to click events or not.
5810 *
5811 * @return true if the view is clickable, false otherwise
5812 *
5813 * @see #setClickable(boolean)
5814 * @attr ref android.R.styleable#View_clickable
5815 */
5816 @ViewDebug.ExportedProperty
5817 public boolean isClickable() {
5818 return (mViewFlags & CLICKABLE) == CLICKABLE;
5819 }
5820
5821 /**
5822 * Enables or disables click events for this view. When a view
5823 * is clickable it will change its state to "pressed" on every click.
5824 * Subclasses should set the view clickable to visually react to
5825 * user's clicks.
5826 *
5827 * @param clickable true to make the view clickable, false otherwise
5828 *
5829 * @see #isClickable()
5830 * @attr ref android.R.styleable#View_clickable
5831 */
5832 public void setClickable(boolean clickable) {
5833 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5834 }
5835
5836 /**
5837 * Indicates whether this view reacts to long click events or not.
5838 *
5839 * @return true if the view is long clickable, false otherwise
5840 *
5841 * @see #setLongClickable(boolean)
5842 * @attr ref android.R.styleable#View_longClickable
5843 */
5844 public boolean isLongClickable() {
5845 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5846 }
5847
5848 /**
5849 * Enables or disables long click events for this view. When a view is long
5850 * clickable it reacts to the user holding down the button for a longer
5851 * duration than a tap. This event can either launch the listener or a
5852 * context menu.
5853 *
5854 * @param longClickable true to make the view long clickable, false otherwise
5855 * @see #isLongClickable()
5856 * @attr ref android.R.styleable#View_longClickable
5857 */
5858 public void setLongClickable(boolean longClickable) {
5859 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5860 }
5861
5862 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005863 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 *
5865 * @see #isClickable()
5866 * @see #setClickable(boolean)
5867 *
5868 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5869 * the View's internal state from a previously set "pressed" state.
5870 */
5871 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005872 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005875 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005877 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005879
5880 if (needsRefresh) {
5881 refreshDrawableState();
5882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 dispatchSetPressed(pressed);
5884 }
5885
5886 /**
5887 * Dispatch setPressed to all of this View's children.
5888 *
5889 * @see #setPressed(boolean)
5890 *
5891 * @param pressed The new pressed state
5892 */
5893 protected void dispatchSetPressed(boolean pressed) {
5894 }
5895
5896 /**
5897 * Indicates whether the view is currently in pressed state. Unless
5898 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5899 * the pressed state.
5900 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005901 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 * @see #isClickable()
5903 * @see #setClickable(boolean)
5904 *
5905 * @return true if the view is currently pressed, false otherwise
5906 */
5907 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005908 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 }
5910
5911 /**
5912 * Indicates whether this view will save its state (that is,
5913 * whether its {@link #onSaveInstanceState} method will be called).
5914 *
5915 * @return Returns true if the view state saving is enabled, else false.
5916 *
5917 * @see #setSaveEnabled(boolean)
5918 * @attr ref android.R.styleable#View_saveEnabled
5919 */
5920 public boolean isSaveEnabled() {
5921 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5922 }
5923
5924 /**
5925 * Controls whether the saving of this view's state is
5926 * enabled (that is, whether its {@link #onSaveInstanceState} method
5927 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005928 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 * for its state to be saved. This flag can only disable the
5930 * saving of this view; any child views may still have their state saved.
5931 *
5932 * @param enabled Set to false to <em>disable</em> state saving, or true
5933 * (the default) to allow it.
5934 *
5935 * @see #isSaveEnabled()
5936 * @see #setId(int)
5937 * @see #onSaveInstanceState()
5938 * @attr ref android.R.styleable#View_saveEnabled
5939 */
5940 public void setSaveEnabled(boolean enabled) {
5941 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5942 }
5943
Jeff Brown85a31762010-09-01 17:01:00 -07005944 /**
5945 * Gets whether the framework should discard touches when the view's
5946 * window is obscured by another visible window.
5947 * Refer to the {@link View} security documentation for more details.
5948 *
5949 * @return True if touch filtering is enabled.
5950 *
5951 * @see #setFilterTouchesWhenObscured(boolean)
5952 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5953 */
5954 @ViewDebug.ExportedProperty
5955 public boolean getFilterTouchesWhenObscured() {
5956 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5957 }
5958
5959 /**
5960 * Sets whether the framework should discard touches when the view's
5961 * window is obscured by another visible window.
5962 * Refer to the {@link View} security documentation for more details.
5963 *
5964 * @param enabled True if touch filtering should be enabled.
5965 *
5966 * @see #getFilterTouchesWhenObscured
5967 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5968 */
5969 public void setFilterTouchesWhenObscured(boolean enabled) {
5970 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5971 FILTER_TOUCHES_WHEN_OBSCURED);
5972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973
5974 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005975 * Indicates whether the entire hierarchy under this view will save its
5976 * state when a state saving traversal occurs from its parent. The default
5977 * is true; if false, these views will not be saved unless
5978 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5979 *
5980 * @return Returns true if the view state saving from parent is enabled, else false.
5981 *
5982 * @see #setSaveFromParentEnabled(boolean)
5983 */
5984 public boolean isSaveFromParentEnabled() {
5985 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5986 }
5987
5988 /**
5989 * Controls whether the entire hierarchy under this view will save its
5990 * state when a state saving traversal occurs from its parent. The default
5991 * is true; if false, these views will not be saved unless
5992 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5993 *
5994 * @param enabled Set to false to <em>disable</em> state saving, or true
5995 * (the default) to allow it.
5996 *
5997 * @see #isSaveFromParentEnabled()
5998 * @see #setId(int)
5999 * @see #onSaveInstanceState()
6000 */
6001 public void setSaveFromParentEnabled(boolean enabled) {
6002 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6003 }
6004
6005
6006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 * Returns whether this View is able to take focus.
6008 *
6009 * @return True if this view can take focus, or false otherwise.
6010 * @attr ref android.R.styleable#View_focusable
6011 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006012 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 public final boolean isFocusable() {
6014 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6015 }
6016
6017 /**
6018 * When a view is focusable, it may not want to take focus when in touch mode.
6019 * For example, a button would like focus when the user is navigating via a D-pad
6020 * so that the user can click on it, but once the user starts touching the screen,
6021 * the button shouldn't take focus
6022 * @return Whether the view is focusable in touch mode.
6023 * @attr ref android.R.styleable#View_focusableInTouchMode
6024 */
6025 @ViewDebug.ExportedProperty
6026 public final boolean isFocusableInTouchMode() {
6027 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6028 }
6029
6030 /**
6031 * Find the nearest view in the specified direction that can take focus.
6032 * This does not actually give focus to that view.
6033 *
6034 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6035 *
6036 * @return The nearest focusable in the specified direction, or null if none
6037 * can be found.
6038 */
6039 public View focusSearch(int direction) {
6040 if (mParent != null) {
6041 return mParent.focusSearch(this, direction);
6042 } else {
6043 return null;
6044 }
6045 }
6046
6047 /**
6048 * This method is the last chance for the focused view and its ancestors to
6049 * respond to an arrow key. This is called when the focused view did not
6050 * consume the key internally, nor could the view system find a new view in
6051 * the requested direction to give focus to.
6052 *
6053 * @param focused The currently focused view.
6054 * @param direction The direction focus wants to move. One of FOCUS_UP,
6055 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6056 * @return True if the this view consumed this unhandled move.
6057 */
6058 public boolean dispatchUnhandledMove(View focused, int direction) {
6059 return false;
6060 }
6061
6062 /**
6063 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006064 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006066 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6067 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 * @return The user specified next view, or null if there is none.
6069 */
6070 View findUserSetNextFocus(View root, int direction) {
6071 switch (direction) {
6072 case FOCUS_LEFT:
6073 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006074 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 case FOCUS_RIGHT:
6076 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006077 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 case FOCUS_UP:
6079 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006080 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 case FOCUS_DOWN:
6082 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006083 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006084 case FOCUS_FORWARD:
6085 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006086 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006087 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006088 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006089 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006090 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006091 @Override
6092 public boolean apply(View t) {
6093 return t.mNextFocusForwardId == id;
6094 }
6095 });
6096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 }
6098 return null;
6099 }
6100
Jeff Brown4dfbec22011-08-15 14:55:37 -07006101 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6102 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6103 @Override
6104 public boolean apply(View t) {
6105 return t.mID == childViewId;
6106 }
6107 });
6108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 if (result == null) {
6110 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6111 + "by user for id " + childViewId);
6112 }
6113 return result;
6114 }
6115
6116 /**
6117 * Find and return all focusable views that are descendants of this view,
6118 * possibly including this view if it is focusable itself.
6119 *
6120 * @param direction The direction of the focus
6121 * @return A list of focusable views
6122 */
6123 public ArrayList<View> getFocusables(int direction) {
6124 ArrayList<View> result = new ArrayList<View>(24);
6125 addFocusables(result, direction);
6126 return result;
6127 }
6128
6129 /**
6130 * Add any focusable views that are descendants of this view (possibly
6131 * including this view if it is focusable itself) to views. If we are in touch mode,
6132 * only add views that are also focusable in touch mode.
6133 *
6134 * @param views Focusable views found so far
6135 * @param direction The direction of the focus
6136 */
6137 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006138 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140
svetoslavganov75986cf2009-05-14 22:28:01 -07006141 /**
6142 * Adds any focusable views that are descendants of this view (possibly
6143 * including this view if it is focusable itself) to views. This method
6144 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006145 * only views focusable in touch mode if we are in touch mode or
6146 * only views that can take accessibility focus if accessibility is enabeld
6147 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006148 *
6149 * @param views Focusable views found so far or null if all we are interested is
6150 * the number of focusables.
6151 * @param direction The direction of the focus.
6152 * @param focusableMode The type of focusables to be added.
6153 *
6154 * @see #FOCUSABLES_ALL
6155 * @see #FOCUSABLES_TOUCH_MODE
6156 */
6157 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006158 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006159 return;
6160 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006161 if (!isFocusable()) {
6162 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006163 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006164 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6165 && isInTouchMode() && !isFocusableInTouchMode()) {
6166 return;
6167 }
6168 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 }
6170
6171 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006172 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006173 * The search is performed by either the text that the View renders or the content
6174 * description that describes the view for accessibility purposes and the view does
6175 * not render or both. Clients can specify how the search is to be performed via
6176 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6177 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006178 *
6179 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006180 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006181 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006182 * @see #FIND_VIEWS_WITH_TEXT
6183 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6184 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006185 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006186 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006187 if (getAccessibilityNodeProvider() != null) {
6188 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6189 outViews.add(this);
6190 }
6191 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006192 && (searched != null && searched.length() > 0)
6193 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006194 String searchedLowerCase = searched.toString().toLowerCase();
6195 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6196 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6197 outViews.add(this);
6198 }
6199 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006200 }
6201
6202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 * Find and return all touchable views that are descendants of this view,
6204 * possibly including this view if it is touchable itself.
6205 *
6206 * @return A list of touchable views
6207 */
6208 public ArrayList<View> getTouchables() {
6209 ArrayList<View> result = new ArrayList<View>();
6210 addTouchables(result);
6211 return result;
6212 }
6213
6214 /**
6215 * Add any touchable views that are descendants of this view (possibly
6216 * including this view if it is touchable itself) to views.
6217 *
6218 * @param views Touchable views found so far
6219 */
6220 public void addTouchables(ArrayList<View> views) {
6221 final int viewFlags = mViewFlags;
6222
6223 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6224 && (viewFlags & ENABLED_MASK) == ENABLED) {
6225 views.add(this);
6226 }
6227 }
6228
6229 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006230 * Returns whether this View is accessibility focused.
6231 *
6232 * @return True if this View is accessibility focused.
6233 */
6234 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006235 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006236 }
6237
6238 /**
6239 * Call this to try to give accessibility focus to this view.
6240 *
6241 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6242 * returns false or the view is no visible or the view already has accessibility
6243 * focus.
6244 *
6245 * See also {@link #focusSearch(int)}, which is what you call to say that you
6246 * have focus, and you want your parent to look for the next one.
6247 *
6248 * @return Whether this view actually took accessibility focus.
6249 *
6250 * @hide
6251 */
6252 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006253 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6254 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006255 return false;
6256 }
6257 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6258 return false;
6259 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006260 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6261 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006262 ViewRootImpl viewRootImpl = getViewRootImpl();
6263 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006264 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006265 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006266 if (mAttachInfo != null) {
6267 Rect rectangle = mAttachInfo.mTmpInvalRect;
6268 getDrawingRect(rectangle);
6269 requestRectangleOnScreen(rectangle);
6270 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006271 invalidate();
6272 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6273 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006274 return true;
6275 }
6276 return false;
6277 }
6278
6279 /**
6280 * Call this to try to clear accessibility focus of this view.
6281 *
6282 * See also {@link #focusSearch(int)}, which is what you call to say that you
6283 * have focus, and you want your parent to look for the next one.
6284 *
6285 * @hide
6286 */
6287 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006288 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6289 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006290 invalidate();
6291 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6292 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006293 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006294 // Clear the global reference of accessibility focus if this
6295 // view or any of its descendants had accessibility focus.
6296 ViewRootImpl viewRootImpl = getViewRootImpl();
6297 if (viewRootImpl != null) {
6298 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6299 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006300 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006301 }
6302 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006303 }
6304
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006305 private void sendAccessibilityHoverEvent(int eventType) {
6306 // Since we are not delivering to a client accessibility events from not
6307 // important views (unless the clinet request that) we need to fire the
6308 // event from the deepest view exposed to the client. As a consequence if
6309 // the user crosses a not exposed view the client will see enter and exit
6310 // of the exposed predecessor followed by and enter and exit of that same
6311 // predecessor when entering and exiting the not exposed descendant. This
6312 // is fine since the client has a clear idea which view is hovered at the
6313 // price of a couple more events being sent. This is a simple and
6314 // working solution.
6315 View source = this;
6316 while (true) {
6317 if (source.includeForAccessibility()) {
6318 source.sendAccessibilityEvent(eventType);
6319 return;
6320 }
6321 ViewParent parent = source.getParent();
6322 if (parent instanceof View) {
6323 source = (View) parent;
6324 } else {
6325 return;
6326 }
6327 }
6328 }
6329
Svetoslav Ganov42138042012-03-20 11:51:39 -07006330 /**
6331 * Clears accessibility focus without calling any callback methods
6332 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6333 * is used for clearing accessibility focus when giving this focus to
6334 * another view.
6335 */
6336 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006337 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6338 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006339 invalidate();
6340 }
6341 }
6342
6343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 * Call this to try to give focus to a specific view or to one of its
6345 * descendants.
6346 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006347 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6348 * false), or if it is focusable and it is not focusable in touch mode
6349 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006351 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 * have focus, and you want your parent to look for the next one.
6353 *
6354 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6355 * {@link #FOCUS_DOWN} and <code>null</code>.
6356 *
6357 * @return Whether this view or one of its descendants actually took focus.
6358 */
6359 public final boolean requestFocus() {
6360 return requestFocus(View.FOCUS_DOWN);
6361 }
6362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 /**
6364 * Call this to try to give focus to a specific view or to one of its
6365 * descendants and give it a hint about what direction focus is heading.
6366 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006367 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6368 * false), or if it is focusable and it is not focusable in touch mode
6369 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006371 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 * have focus, and you want your parent to look for the next one.
6373 *
6374 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6375 * <code>null</code> set for the previously focused rectangle.
6376 *
6377 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6378 * @return Whether this view or one of its descendants actually took focus.
6379 */
6380 public final boolean requestFocus(int direction) {
6381 return requestFocus(direction, null);
6382 }
6383
6384 /**
6385 * Call this to try to give focus to a specific view or to one of its descendants
6386 * and give it hints about the direction and a specific rectangle that the focus
6387 * is coming from. The rectangle can help give larger views a finer grained hint
6388 * about where focus is coming from, and therefore, where to show selection, or
6389 * forward focus change internally.
6390 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006391 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6392 * false), or if it is focusable and it is not focusable in touch mode
6393 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 *
6395 * A View will not take focus if it is not visible.
6396 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006397 * A View will not take focus if one of its parents has
6398 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6399 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006401 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 * have focus, and you want your parent to look for the next one.
6403 *
6404 * You may wish to override this method if your custom {@link View} has an internal
6405 * {@link View} that it wishes to forward the request to.
6406 *
6407 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6408 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6409 * to give a finer grained hint about where focus is coming from. May be null
6410 * if there is no hint.
6411 * @return Whether this view or one of its descendants actually took focus.
6412 */
6413 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006414 return requestFocusNoSearch(direction, previouslyFocusedRect);
6415 }
6416
6417 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 // need to be focusable
6419 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6420 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6421 return false;
6422 }
6423
6424 // need to be focusable in touch mode if in touch mode
6425 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006426 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6427 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006428 }
6429
6430 // need to not have any parents blocking us
6431 if (hasAncestorThatBlocksDescendantFocus()) {
6432 return false;
6433 }
6434
6435 handleFocusGainInternal(direction, previouslyFocusedRect);
6436 return true;
6437 }
6438
6439 /**
6440 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6441 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6442 * touch mode to request focus when they are touched.
6443 *
6444 * @return Whether this view or one of its descendants actually took focus.
6445 *
6446 * @see #isInTouchMode()
6447 *
6448 */
6449 public final boolean requestFocusFromTouch() {
6450 // Leave touch mode if we need to
6451 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006452 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006453 if (viewRoot != null) {
6454 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 }
6456 }
6457 return requestFocus(View.FOCUS_DOWN);
6458 }
6459
6460 /**
6461 * @return Whether any ancestor of this view blocks descendant focus.
6462 */
6463 private boolean hasAncestorThatBlocksDescendantFocus() {
6464 ViewParent ancestor = mParent;
6465 while (ancestor instanceof ViewGroup) {
6466 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6467 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6468 return true;
6469 } else {
6470 ancestor = vgAncestor.getParent();
6471 }
6472 }
6473 return false;
6474 }
6475
6476 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006477 * Gets the mode for determining whether this View is important for accessibility
6478 * which is if it fires accessibility events and if it is reported to
6479 * accessibility services that query the screen.
6480 *
6481 * @return The mode for determining whether a View is important for accessibility.
6482 *
6483 * @attr ref android.R.styleable#View_importantForAccessibility
6484 *
6485 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6486 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6487 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6488 */
6489 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006490 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6491 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6492 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006493 })
6494 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006495 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6496 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006497 }
6498
6499 /**
6500 * Sets how to determine whether this view is important for accessibility
6501 * which is if it fires accessibility events and if it is reported to
6502 * accessibility services that query the screen.
6503 *
6504 * @param mode How to determine whether this view is important for accessibility.
6505 *
6506 * @attr ref android.R.styleable#View_importantForAccessibility
6507 *
6508 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6509 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6510 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6511 */
6512 public void setImportantForAccessibility(int mode) {
6513 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006514 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6515 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6516 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006517 notifyAccessibilityStateChanged();
6518 }
6519 }
6520
6521 /**
6522 * Gets whether this view should be exposed for accessibility.
6523 *
6524 * @return Whether the view is exposed for accessibility.
6525 *
6526 * @hide
6527 */
6528 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006529 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6530 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006531 switch (mode) {
6532 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6533 return true;
6534 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6535 return false;
6536 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006537 return isActionableForAccessibility() || hasListenersForAccessibility()
6538 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006539 default:
6540 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6541 + mode);
6542 }
6543 }
6544
6545 /**
6546 * Gets the parent for accessibility purposes. Note that the parent for
6547 * accessibility is not necessary the immediate parent. It is the first
6548 * predecessor that is important for accessibility.
6549 *
6550 * @return The parent for accessibility purposes.
6551 */
6552 public ViewParent getParentForAccessibility() {
6553 if (mParent instanceof View) {
6554 View parentView = (View) mParent;
6555 if (parentView.includeForAccessibility()) {
6556 return mParent;
6557 } else {
6558 return mParent.getParentForAccessibility();
6559 }
6560 }
6561 return null;
6562 }
6563
6564 /**
6565 * Adds the children of a given View for accessibility. Since some Views are
6566 * not important for accessibility the children for accessibility are not
6567 * necessarily direct children of the riew, rather they are the first level of
6568 * descendants important for accessibility.
6569 *
6570 * @param children The list of children for accessibility.
6571 */
6572 public void addChildrenForAccessibility(ArrayList<View> children) {
6573 if (includeForAccessibility()) {
6574 children.add(this);
6575 }
6576 }
6577
6578 /**
6579 * Whether to regard this view for accessibility. A view is regarded for
6580 * accessibility if it is important for accessibility or the querying
6581 * accessibility service has explicitly requested that view not
6582 * important for accessibility are regarded.
6583 *
6584 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006585 *
6586 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006587 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006588 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006589 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006590 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006591 }
6592 return false;
6593 }
6594
6595 /**
6596 * Returns whether the View is considered actionable from
6597 * accessibility perspective. Such view are important for
6598 * accessiiblity.
6599 *
6600 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006601 *
6602 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006603 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006604 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006605 return (isClickable() || isLongClickable() || isFocusable());
6606 }
6607
6608 /**
6609 * Returns whether the View has registered callbacks wich makes it
6610 * important for accessiiblity.
6611 *
6612 * @return True if the view is actionable for accessibility.
6613 */
6614 private boolean hasListenersForAccessibility() {
6615 ListenerInfo info = getListenerInfo();
6616 return mTouchDelegate != null || info.mOnKeyListener != null
6617 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6618 || info.mOnHoverListener != null || info.mOnDragListener != null;
6619 }
6620
6621 /**
6622 * Notifies accessibility services that some view's important for
6623 * accessibility state has changed. Note that such notifications
6624 * are made at most once every
6625 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6626 * to avoid unnecessary load to the system. Also once a view has
6627 * made a notifucation this method is a NOP until the notification has
6628 * been sent to clients.
6629 *
6630 * @hide
6631 *
6632 * TODO: Makse sure this method is called for any view state change
6633 * that is interesting for accessilility purposes.
6634 */
6635 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006636 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6637 return;
6638 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006639 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6640 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006641 if (mParent != null) {
6642 mParent.childAccessibilityStateChanged(this);
6643 }
6644 }
6645 }
6646
6647 /**
6648 * Reset the state indicating the this view has requested clients
6649 * interested in its accessiblity state to be notified.
6650 *
6651 * @hide
6652 */
6653 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006654 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006655 }
6656
6657 /**
6658 * Performs the specified accessibility action on the view. For
6659 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006660 * <p>
6661 * If an {@link AccessibilityDelegate} has been specified via calling
6662 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6663 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6664 * is responsible for handling this call.
6665 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006666 *
6667 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006668 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006669 * @return Whether the action was performed.
6670 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006671 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006672 if (mAccessibilityDelegate != null) {
6673 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6674 } else {
6675 return performAccessibilityActionInternal(action, arguments);
6676 }
6677 }
6678
6679 /**
6680 * @see #performAccessibilityAction(int, Bundle)
6681 *
6682 * Note: Called from the default {@link AccessibilityDelegate}.
6683 */
6684 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006685 switch (action) {
6686 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006687 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006688 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006689 }
6690 } break;
6691 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6692 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006693 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006694 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006695 } break;
6696 case AccessibilityNodeInfo.ACTION_FOCUS: {
6697 if (!hasFocus()) {
6698 // Get out of touch mode since accessibility
6699 // wants to move focus around.
6700 getViewRootImpl().ensureTouchMode(false);
6701 return requestFocus();
6702 }
6703 } break;
6704 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6705 if (hasFocus()) {
6706 clearFocus();
6707 return !isFocused();
6708 }
6709 } break;
6710 case AccessibilityNodeInfo.ACTION_SELECT: {
6711 if (!isSelected()) {
6712 setSelected(true);
6713 return isSelected();
6714 }
6715 } break;
6716 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6717 if (isSelected()) {
6718 setSelected(false);
6719 return !isSelected();
6720 }
6721 } break;
6722 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006723 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006724 return requestAccessibilityFocus();
6725 }
6726 } break;
6727 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6728 if (isAccessibilityFocused()) {
6729 clearAccessibilityFocus();
6730 return true;
6731 }
6732 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006733 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6734 if (arguments != null) {
6735 final int granularity = arguments.getInt(
6736 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6737 return nextAtGranularity(granularity);
6738 }
6739 } break;
6740 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6741 if (arguments != null) {
6742 final int granularity = arguments.getInt(
6743 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6744 return previousAtGranularity(granularity);
6745 }
6746 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006747 }
6748 return false;
6749 }
6750
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006751 private boolean nextAtGranularity(int granularity) {
6752 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006753 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006754 return false;
6755 }
6756 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6757 if (iterator == null) {
6758 return false;
6759 }
6760 final int current = getAccessibilityCursorPosition();
6761 final int[] range = iterator.following(current);
6762 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006763 return false;
6764 }
6765 final int start = range[0];
6766 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006767 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006768 sendViewTextTraversedAtGranularityEvent(
6769 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6770 granularity, start, end);
6771 return true;
6772 }
6773
6774 private boolean previousAtGranularity(int granularity) {
6775 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006776 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006777 return false;
6778 }
6779 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6780 if (iterator == null) {
6781 return false;
6782 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006783 int current = getAccessibilityCursorPosition();
6784 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6785 current = text.length();
6786 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6787 // When traversing by character we always put the cursor after the character
6788 // to ease edit and have to compensate before asking the for previous segment.
6789 current--;
6790 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006791 final int[] range = iterator.preceding(current);
6792 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006793 return false;
6794 }
6795 final int start = range[0];
6796 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006797 // Always put the cursor after the character to ease edit.
6798 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6799 setAccessibilityCursorPosition(end);
6800 } else {
6801 setAccessibilityCursorPosition(start);
6802 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006803 sendViewTextTraversedAtGranularityEvent(
6804 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6805 granularity, start, end);
6806 return true;
6807 }
6808
6809 /**
6810 * Gets the text reported for accessibility purposes.
6811 *
6812 * @return The accessibility text.
6813 *
6814 * @hide
6815 */
6816 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07006817 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006818 }
6819
6820 /**
6821 * @hide
6822 */
6823 public int getAccessibilityCursorPosition() {
6824 return mAccessibilityCursorPosition;
6825 }
6826
6827 /**
6828 * @hide
6829 */
6830 public void setAccessibilityCursorPosition(int position) {
6831 mAccessibilityCursorPosition = position;
6832 }
6833
6834 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6835 int fromIndex, int toIndex) {
6836 if (mParent == null) {
6837 return;
6838 }
6839 AccessibilityEvent event = AccessibilityEvent.obtain(
6840 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6841 onInitializeAccessibilityEvent(event);
6842 onPopulateAccessibilityEvent(event);
6843 event.setFromIndex(fromIndex);
6844 event.setToIndex(toIndex);
6845 event.setAction(action);
6846 event.setMovementGranularity(granularity);
6847 mParent.requestSendAccessibilityEvent(this, event);
6848 }
6849
6850 /**
6851 * @hide
6852 */
6853 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6854 switch (granularity) {
6855 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6856 CharSequence text = getIterableTextForAccessibility();
6857 if (text != null && text.length() > 0) {
6858 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006859 CharacterTextSegmentIterator.getInstance(
6860 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006861 iterator.initialize(text.toString());
6862 return iterator;
6863 }
6864 } break;
6865 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6866 CharSequence text = getIterableTextForAccessibility();
6867 if (text != null && text.length() > 0) {
6868 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006869 WordTextSegmentIterator.getInstance(
6870 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006871 iterator.initialize(text.toString());
6872 return iterator;
6873 }
6874 } break;
6875 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6876 CharSequence text = getIterableTextForAccessibility();
6877 if (text != null && text.length() > 0) {
6878 ParagraphTextSegmentIterator iterator =
6879 ParagraphTextSegmentIterator.getInstance();
6880 iterator.initialize(text.toString());
6881 return iterator;
6882 }
6883 } break;
6884 }
6885 return null;
6886 }
6887
Svetoslav Ganov42138042012-03-20 11:51:39 -07006888 /**
Romain Guya440b002010-02-24 15:57:54 -08006889 * @hide
6890 */
6891 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006892 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006893 clearDisplayList();
6894
Romain Guya440b002010-02-24 15:57:54 -08006895 onStartTemporaryDetach();
6896 }
6897
6898 /**
6899 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6901 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006902 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 */
6904 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006905 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07006906 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006907 }
6908
6909 /**
6910 * @hide
6911 */
6912 public void dispatchFinishTemporaryDetach() {
6913 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 }
Romain Guy8506ab42009-06-11 17:35:47 -07006915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 /**
6917 * Called after {@link #onStartTemporaryDetach} when the container is done
6918 * changing the view.
6919 */
6920 public void onFinishTemporaryDetach() {
6921 }
Romain Guy8506ab42009-06-11 17:35:47 -07006922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006924 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6925 * for this view's window. Returns null if the view is not currently attached
6926 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006927 * just use the standard high-level event callbacks like
6928 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006929 */
6930 public KeyEvent.DispatcherState getKeyDispatcherState() {
6931 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6932 }
Joe Malin32736f02011-01-19 16:14:20 -08006933
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 * Dispatch a key event before it is processed by any input method
6936 * associated with the view hierarchy. This can be used to intercept
6937 * key events in special situations before the IME consumes them; a
6938 * typical example would be handling the BACK key to update the application's
6939 * UI instead of allowing the IME to see it and close itself.
6940 *
6941 * @param event The key event to be dispatched.
6942 * @return True if the event was handled, false otherwise.
6943 */
6944 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6945 return onKeyPreIme(event.getKeyCode(), event);
6946 }
6947
6948 /**
6949 * Dispatch a key event to the next view on the focus path. This path runs
6950 * from the top of the view tree down to the currently focused view. If this
6951 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6952 * the next node down the focus path. This method also fires any key
6953 * listeners.
6954 *
6955 * @param event The key event to be dispatched.
6956 * @return True if the event was handled, false otherwise.
6957 */
6958 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006959 if (mInputEventConsistencyVerifier != null) {
6960 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962
Jeff Brown21bc5c92011-02-28 18:27:14 -08006963 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006964 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006965 ListenerInfo li = mListenerInfo;
6966 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6967 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 return true;
6969 }
6970
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006971 if (event.dispatch(this, mAttachInfo != null
6972 ? mAttachInfo.mKeyDispatchState : null, this)) {
6973 return true;
6974 }
6975
6976 if (mInputEventConsistencyVerifier != null) {
6977 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6978 }
6979 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 }
6981
6982 /**
6983 * Dispatches a key shortcut event.
6984 *
6985 * @param event The key event to be dispatched.
6986 * @return True if the event was handled by the view, false otherwise.
6987 */
6988 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6989 return onKeyShortcut(event.getKeyCode(), event);
6990 }
6991
6992 /**
6993 * Pass the touch screen motion event down to the target view, or this
6994 * view if it is the target.
6995 *
6996 * @param event The motion event to be dispatched.
6997 * @return True if the event was handled by the view, false otherwise.
6998 */
6999 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007000 if (mInputEventConsistencyVerifier != null) {
7001 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7002 }
7003
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007004 if (onFilterTouchEventForSecurity(event)) {
7005 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007006 ListenerInfo li = mListenerInfo;
7007 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7008 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007009 return true;
7010 }
7011
7012 if (onTouchEvent(event)) {
7013 return true;
7014 }
Jeff Brown85a31762010-09-01 17:01:00 -07007015 }
7016
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007017 if (mInputEventConsistencyVerifier != null) {
7018 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007020 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021 }
7022
7023 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007024 * Filter the touch event to apply security policies.
7025 *
7026 * @param event The motion event to be filtered.
7027 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007028 *
Jeff Brown85a31762010-09-01 17:01:00 -07007029 * @see #getFilterTouchesWhenObscured
7030 */
7031 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007032 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007033 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7034 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7035 // Window is obscured, drop this touch.
7036 return false;
7037 }
7038 return true;
7039 }
7040
7041 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 * Pass a trackball motion event down to the focused view.
7043 *
7044 * @param event The motion event to be dispatched.
7045 * @return True if the event was handled by the view, false otherwise.
7046 */
7047 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007048 if (mInputEventConsistencyVerifier != null) {
7049 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7050 }
7051
Romain Guy02ccac62011-06-24 13:20:23 -07007052 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 }
7054
7055 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007056 * Dispatch a generic motion event.
7057 * <p>
7058 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7059 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007060 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007061 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007062 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007063 *
7064 * @param event The motion event to be dispatched.
7065 * @return True if the event was handled by the view, false otherwise.
7066 */
7067 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007068 if (mInputEventConsistencyVerifier != null) {
7069 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7070 }
7071
Jeff Browna032cc02011-03-07 16:56:21 -08007072 final int source = event.getSource();
7073 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7074 final int action = event.getAction();
7075 if (action == MotionEvent.ACTION_HOVER_ENTER
7076 || action == MotionEvent.ACTION_HOVER_MOVE
7077 || action == MotionEvent.ACTION_HOVER_EXIT) {
7078 if (dispatchHoverEvent(event)) {
7079 return true;
7080 }
7081 } else if (dispatchGenericPointerEvent(event)) {
7082 return true;
7083 }
7084 } else if (dispatchGenericFocusedEvent(event)) {
7085 return true;
7086 }
7087
Jeff Brown10b62902011-06-20 16:40:37 -07007088 if (dispatchGenericMotionEventInternal(event)) {
7089 return true;
7090 }
7091
7092 if (mInputEventConsistencyVerifier != null) {
7093 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7094 }
7095 return false;
7096 }
7097
7098 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007099 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007100 ListenerInfo li = mListenerInfo;
7101 if (li != null && li.mOnGenericMotionListener != null
7102 && (mViewFlags & ENABLED_MASK) == ENABLED
7103 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007104 return true;
7105 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007106
7107 if (onGenericMotionEvent(event)) {
7108 return true;
7109 }
7110
7111 if (mInputEventConsistencyVerifier != null) {
7112 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7113 }
7114 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007115 }
7116
7117 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007118 * Dispatch a hover event.
7119 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007120 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007121 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007122 * </p>
7123 *
7124 * @param event The motion event to be dispatched.
7125 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007126 */
7127 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007128 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007129 ListenerInfo li = mListenerInfo;
7130 if (li != null && li.mOnHoverListener != null
7131 && (mViewFlags & ENABLED_MASK) == ENABLED
7132 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007133 return true;
7134 }
7135
Jeff Browna032cc02011-03-07 16:56:21 -08007136 return onHoverEvent(event);
7137 }
7138
7139 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007140 * Returns true if the view has a child to which it has recently sent
7141 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7142 * it does not have a hovered child, then it must be the innermost hovered view.
7143 * @hide
7144 */
7145 protected boolean hasHoveredChild() {
7146 return false;
7147 }
7148
7149 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007150 * Dispatch a generic motion event to the view under the first pointer.
7151 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007152 * Do not call this method directly.
7153 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007154 * </p>
7155 *
7156 * @param event The motion event to be dispatched.
7157 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007158 */
7159 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7160 return false;
7161 }
7162
7163 /**
7164 * Dispatch a generic motion event to the currently focused view.
7165 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007166 * Do not call this method directly.
7167 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007168 * </p>
7169 *
7170 * @param event The motion event to be dispatched.
7171 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007172 */
7173 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7174 return false;
7175 }
7176
7177 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007178 * Dispatch a pointer event.
7179 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007180 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7181 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7182 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007183 * and should not be expected to handle other pointing device features.
7184 * </p>
7185 *
7186 * @param event The motion event to be dispatched.
7187 * @return True if the event was handled by the view, false otherwise.
7188 * @hide
7189 */
7190 public final boolean dispatchPointerEvent(MotionEvent event) {
7191 if (event.isTouchEvent()) {
7192 return dispatchTouchEvent(event);
7193 } else {
7194 return dispatchGenericMotionEvent(event);
7195 }
7196 }
7197
7198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 * Called when the window containing this view gains or loses window focus.
7200 * ViewGroups should override to route to their children.
7201 *
7202 * @param hasFocus True if the window containing this view now has focus,
7203 * false otherwise.
7204 */
7205 public void dispatchWindowFocusChanged(boolean hasFocus) {
7206 onWindowFocusChanged(hasFocus);
7207 }
7208
7209 /**
7210 * Called when the window containing this view gains or loses focus. Note
7211 * that this is separate from view focus: to receive key events, both
7212 * your view and its window must have focus. If a window is displayed
7213 * on top of yours that takes input focus, then your own window will lose
7214 * focus but the view focus will remain unchanged.
7215 *
7216 * @param hasWindowFocus True if the window containing this view now has
7217 * focus, false otherwise.
7218 */
7219 public void onWindowFocusChanged(boolean hasWindowFocus) {
7220 InputMethodManager imm = InputMethodManager.peekInstance();
7221 if (!hasWindowFocus) {
7222 if (isPressed()) {
7223 setPressed(false);
7224 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007225 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 imm.focusOut(this);
7227 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007228 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007229 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007230 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007231 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007232 imm.focusIn(this);
7233 }
7234 refreshDrawableState();
7235 }
7236
7237 /**
7238 * Returns true if this view is in a window that currently has window focus.
7239 * Note that this is not the same as the view itself having focus.
7240 *
7241 * @return True if this view is in a window that currently has window focus.
7242 */
7243 public boolean hasWindowFocus() {
7244 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7245 }
7246
7247 /**
Adam Powell326d8082009-12-09 15:10:07 -08007248 * Dispatch a view visibility change down the view hierarchy.
7249 * ViewGroups should override to route to their children.
7250 * @param changedView The view whose visibility changed. Could be 'this' or
7251 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007252 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7253 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007254 */
7255 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7256 onVisibilityChanged(changedView, visibility);
7257 }
7258
7259 /**
7260 * Called when the visibility of the view or an ancestor of the view is changed.
7261 * @param changedView The view whose visibility changed. Could be 'this' or
7262 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007263 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7264 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007265 */
7266 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007267 if (visibility == VISIBLE) {
7268 if (mAttachInfo != null) {
7269 initialAwakenScrollBars();
7270 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007271 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007272 }
7273 }
Adam Powell326d8082009-12-09 15:10:07 -08007274 }
7275
7276 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007277 * Dispatch a hint about whether this view is displayed. For instance, when
7278 * a View moves out of the screen, it might receives a display hint indicating
7279 * the view is not displayed. Applications should not <em>rely</em> on this hint
7280 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007281 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007282 * @param hint A hint about whether or not this view is displayed:
7283 * {@link #VISIBLE} or {@link #INVISIBLE}.
7284 */
7285 public void dispatchDisplayHint(int hint) {
7286 onDisplayHint(hint);
7287 }
7288
7289 /**
7290 * Gives this view a hint about whether is displayed or not. For instance, when
7291 * a View moves out of the screen, it might receives a display hint indicating
7292 * the view is not displayed. Applications should not <em>rely</em> on this hint
7293 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007294 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007295 * @param hint A hint about whether or not this view is displayed:
7296 * {@link #VISIBLE} or {@link #INVISIBLE}.
7297 */
7298 protected void onDisplayHint(int hint) {
7299 }
7300
7301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007302 * Dispatch a window visibility change down the view hierarchy.
7303 * ViewGroups should override to route to their children.
7304 *
7305 * @param visibility The new visibility of the window.
7306 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007307 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 */
7309 public void dispatchWindowVisibilityChanged(int visibility) {
7310 onWindowVisibilityChanged(visibility);
7311 }
7312
7313 /**
7314 * Called when the window containing has change its visibility
7315 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7316 * that this tells you whether or not your window is being made visible
7317 * to the window manager; this does <em>not</em> tell you whether or not
7318 * your window is obscured by other windows on the screen, even if it
7319 * is itself visible.
7320 *
7321 * @param visibility The new visibility of the window.
7322 */
7323 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007324 if (visibility == VISIBLE) {
7325 initialAwakenScrollBars();
7326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 }
7328
7329 /**
7330 * Returns the current visibility of the window this view is attached to
7331 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7332 *
7333 * @return Returns the current visibility of the view's window.
7334 */
7335 public int getWindowVisibility() {
7336 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7337 }
7338
7339 /**
7340 * Retrieve the overall visible display size in which the window this view is
7341 * attached to has been positioned in. This takes into account screen
7342 * decorations above the window, for both cases where the window itself
7343 * is being position inside of them or the window is being placed under
7344 * then and covered insets are used for the window to position its content
7345 * inside. In effect, this tells you the available area where content can
7346 * be placed and remain visible to users.
7347 *
7348 * <p>This function requires an IPC back to the window manager to retrieve
7349 * the requested information, so should not be used in performance critical
7350 * code like drawing.
7351 *
7352 * @param outRect Filled in with the visible display frame. If the view
7353 * is not attached to a window, this is simply the raw display size.
7354 */
7355 public void getWindowVisibleDisplayFrame(Rect outRect) {
7356 if (mAttachInfo != null) {
7357 try {
7358 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7359 } catch (RemoteException e) {
7360 return;
7361 }
7362 // XXX This is really broken, and probably all needs to be done
7363 // in the window manager, and we need to know more about whether
7364 // we want the area behind or in front of the IME.
7365 final Rect insets = mAttachInfo.mVisibleInsets;
7366 outRect.left += insets.left;
7367 outRect.top += insets.top;
7368 outRect.right -= insets.right;
7369 outRect.bottom -= insets.bottom;
7370 return;
7371 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007372 // The view is not attached to a display so we don't have a context.
7373 // Make a best guess about the display size.
7374 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007375 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 }
7377
7378 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007379 * Dispatch a notification about a resource configuration change down
7380 * the view hierarchy.
7381 * ViewGroups should override to route to their children.
7382 *
7383 * @param newConfig The new resource configuration.
7384 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007385 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007386 */
7387 public void dispatchConfigurationChanged(Configuration newConfig) {
7388 onConfigurationChanged(newConfig);
7389 }
7390
7391 /**
7392 * Called when the current configuration of the resources being used
7393 * by the application have changed. You can use this to decide when
7394 * to reload resources that can changed based on orientation and other
7395 * configuration characterstics. You only need to use this if you are
7396 * not relying on the normal {@link android.app.Activity} mechanism of
7397 * recreating the activity instance upon a configuration change.
7398 *
7399 * @param newConfig The new resource configuration.
7400 */
7401 protected void onConfigurationChanged(Configuration newConfig) {
7402 }
7403
7404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 * Private function to aggregate all per-view attributes in to the view
7406 * root.
7407 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007408 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7409 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 }
7411
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007412 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7413 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007414 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007415 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007416 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007417 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007418 ListenerInfo li = mListenerInfo;
7419 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007420 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007422 }
7423 }
7424
7425 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007426 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007428 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7429 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 ai.mRecomputeGlobalAttributes = true;
7431 }
7432 }
7433 }
7434
7435 /**
7436 * Returns whether the device is currently in touch mode. Touch mode is entered
7437 * once the user begins interacting with the device by touch, and affects various
7438 * things like whether focus is always visible to the user.
7439 *
7440 * @return Whether the device is in touch mode.
7441 */
7442 @ViewDebug.ExportedProperty
7443 public boolean isInTouchMode() {
7444 if (mAttachInfo != null) {
7445 return mAttachInfo.mInTouchMode;
7446 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007447 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 }
7449 }
7450
7451 /**
7452 * Returns the context the view is running in, through which it can
7453 * access the current theme, resources, etc.
7454 *
7455 * @return The view's Context.
7456 */
7457 @ViewDebug.CapturedViewProperty
7458 public final Context getContext() {
7459 return mContext;
7460 }
7461
7462 /**
7463 * Handle a key event before it is processed by any input method
7464 * associated with the view hierarchy. This can be used to intercept
7465 * key events in special situations before the IME consumes them; a
7466 * typical example would be handling the BACK key to update the application's
7467 * UI instead of allowing the IME to see it and close itself.
7468 *
7469 * @param keyCode The value in event.getKeyCode().
7470 * @param event Description of the key event.
7471 * @return If you handled the event, return true. If you want to allow the
7472 * event to be handled by the next receiver, return false.
7473 */
7474 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7475 return false;
7476 }
7477
7478 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007479 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7480 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7482 * is released, if the view is enabled and clickable.
7483 *
Jean Chalard405bc512012-05-29 19:12:34 +09007484 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7485 * although some may elect to do so in some situations. Do not rely on this to
7486 * catch software key presses.
7487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 * @param keyCode A key code that represents the button pressed, from
7489 * {@link android.view.KeyEvent}.
7490 * @param event The KeyEvent object that defines the button action.
7491 */
7492 public boolean onKeyDown(int keyCode, KeyEvent event) {
7493 boolean result = false;
7494
7495 switch (keyCode) {
7496 case KeyEvent.KEYCODE_DPAD_CENTER:
7497 case KeyEvent.KEYCODE_ENTER: {
7498 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7499 return true;
7500 }
7501 // Long clickable items don't necessarily have to be clickable
7502 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7503 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7504 (event.getRepeatCount() == 0)) {
7505 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007506 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 return true;
7508 }
7509 break;
7510 }
7511 }
7512 return result;
7513 }
7514
7515 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007516 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7517 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7518 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007519 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7520 * although some may elect to do so in some situations. Do not rely on this to
7521 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007522 */
7523 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7524 return false;
7525 }
7526
7527 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007528 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7529 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7531 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007532 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7533 * although some may elect to do so in some situations. Do not rely on this to
7534 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 *
7536 * @param keyCode A key code that represents the button pressed, from
7537 * {@link android.view.KeyEvent}.
7538 * @param event The KeyEvent object that defines the button action.
7539 */
7540 public boolean onKeyUp(int keyCode, KeyEvent event) {
7541 boolean result = false;
7542
7543 switch (keyCode) {
7544 case KeyEvent.KEYCODE_DPAD_CENTER:
7545 case KeyEvent.KEYCODE_ENTER: {
7546 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7547 return true;
7548 }
7549 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7550 setPressed(false);
7551
7552 if (!mHasPerformedLongPress) {
7553 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007554 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555
7556 result = performClick();
7557 }
7558 }
7559 break;
7560 }
7561 }
7562 return result;
7563 }
7564
7565 /**
7566 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7567 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7568 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007569 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7570 * although some may elect to do so in some situations. Do not rely on this to
7571 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 *
7573 * @param keyCode A key code that represents the button pressed, from
7574 * {@link android.view.KeyEvent}.
7575 * @param repeatCount The number of times the action was made.
7576 * @param event The KeyEvent object that defines the button action.
7577 */
7578 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7579 return false;
7580 }
7581
7582 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007583 * Called on the focused view when a key shortcut event is not handled.
7584 * Override this method to implement local key shortcuts for the View.
7585 * Key shortcuts can also be implemented by setting the
7586 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 *
7588 * @param keyCode The value in event.getKeyCode().
7589 * @param event Description of the key event.
7590 * @return If you handled the event, return true. If you want to allow the
7591 * event to be handled by the next receiver, return false.
7592 */
7593 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7594 return false;
7595 }
7596
7597 /**
7598 * Check whether the called view is a text editor, in which case it
7599 * would make sense to automatically display a soft input window for
7600 * it. Subclasses should override this if they implement
7601 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007602 * a call on that method would return a non-null InputConnection, and
7603 * they are really a first-class editor that the user would normally
7604 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007605 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007606 * <p>The default implementation always returns false. This does
7607 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7608 * will not be called or the user can not otherwise perform edits on your
7609 * view; it is just a hint to the system that this is not the primary
7610 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 * @return Returns true if this view is a text editor, else false.
7613 */
7614 public boolean onCheckIsTextEditor() {
7615 return false;
7616 }
Romain Guy8506ab42009-06-11 17:35:47 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 /**
7619 * Create a new InputConnection for an InputMethod to interact
7620 * with the view. The default implementation returns null, since it doesn't
7621 * support input methods. You can override this to implement such support.
7622 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 * <p>When implementing this, you probably also want to implement
7625 * {@link #onCheckIsTextEditor()} to indicate you will return a
7626 * non-null InputConnection.
7627 *
7628 * @param outAttrs Fill in with attribute information about the connection.
7629 */
7630 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7631 return null;
7632 }
7633
7634 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007635 * Called by the {@link android.view.inputmethod.InputMethodManager}
7636 * when a view who is not the current
7637 * input connection target is trying to make a call on the manager. The
7638 * default implementation returns false; you can override this to return
7639 * true for certain views if you are performing InputConnection proxying
7640 * to them.
7641 * @param view The View that is making the InputMethodManager call.
7642 * @return Return true to allow the call, false to reject.
7643 */
7644 public boolean checkInputConnectionProxy(View view) {
7645 return false;
7646 }
Romain Guy8506ab42009-06-11 17:35:47 -07007647
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 * Show the context menu for this view. It is not safe to hold on to the
7650 * menu after returning from this method.
7651 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007652 * You should normally not overload this method. Overload
7653 * {@link #onCreateContextMenu(ContextMenu)} or define an
7654 * {@link OnCreateContextMenuListener} to add items to the context menu.
7655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 * @param menu The context menu to populate
7657 */
7658 public void createContextMenu(ContextMenu menu) {
7659 ContextMenuInfo menuInfo = getContextMenuInfo();
7660
7661 // Sets the current menu info so all items added to menu will have
7662 // my extra info set.
7663 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7664
7665 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007666 ListenerInfo li = mListenerInfo;
7667 if (li != null && li.mOnCreateContextMenuListener != null) {
7668 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 }
7670
7671 // Clear the extra information so subsequent items that aren't mine don't
7672 // have my extra info.
7673 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7674
7675 if (mParent != null) {
7676 mParent.createContextMenu(menu);
7677 }
7678 }
7679
7680 /**
7681 * Views should implement this if they have extra information to associate
7682 * with the context menu. The return result is supplied as a parameter to
7683 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7684 * callback.
7685 *
7686 * @return Extra information about the item for which the context menu
7687 * should be shown. This information will vary across different
7688 * subclasses of View.
7689 */
7690 protected ContextMenuInfo getContextMenuInfo() {
7691 return null;
7692 }
7693
7694 /**
7695 * Views should implement this if the view itself is going to add items to
7696 * the context menu.
7697 *
7698 * @param menu the context menu to populate
7699 */
7700 protected void onCreateContextMenu(ContextMenu menu) {
7701 }
7702
7703 /**
7704 * Implement this method to handle trackball motion events. The
7705 * <em>relative</em> movement of the trackball since the last event
7706 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7707 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7708 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7709 * they will often be fractional values, representing the more fine-grained
7710 * movement information available from a trackball).
7711 *
7712 * @param event The motion event.
7713 * @return True if the event was handled, false otherwise.
7714 */
7715 public boolean onTrackballEvent(MotionEvent event) {
7716 return false;
7717 }
7718
7719 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007720 * Implement this method to handle generic motion events.
7721 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007722 * Generic motion events describe joystick movements, mouse hovers, track pad
7723 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007724 * {@link MotionEvent#getSource() source} of the motion event specifies
7725 * the class of input that was received. Implementations of this method
7726 * must examine the bits in the source before processing the event.
7727 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007728 * </p><p>
7729 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7730 * are delivered to the view under the pointer. All other generic motion events are
7731 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007732 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007733 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007734 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007735 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7736 * // process the joystick movement...
7737 * return true;
7738 * }
7739 * }
7740 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7741 * switch (event.getAction()) {
7742 * case MotionEvent.ACTION_HOVER_MOVE:
7743 * // process the mouse hover movement...
7744 * return true;
7745 * case MotionEvent.ACTION_SCROLL:
7746 * // process the scroll wheel movement...
7747 * return true;
7748 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007749 * }
7750 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007751 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007752 *
7753 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007754 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007755 */
7756 public boolean onGenericMotionEvent(MotionEvent event) {
7757 return false;
7758 }
7759
7760 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007761 * Implement this method to handle hover events.
7762 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007763 * This method is called whenever a pointer is hovering into, over, or out of the
7764 * bounds of a view and the view is not currently being touched.
7765 * Hover events are represented as pointer events with action
7766 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7767 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7768 * </p>
7769 * <ul>
7770 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7771 * when the pointer enters the bounds of the view.</li>
7772 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7773 * when the pointer has already entered the bounds of the view and has moved.</li>
7774 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7775 * when the pointer has exited the bounds of the view or when the pointer is
7776 * about to go down due to a button click, tap, or similar user action that
7777 * causes the view to be touched.</li>
7778 * </ul>
7779 * <p>
7780 * The view should implement this method to return true to indicate that it is
7781 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007782 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007783 * The default implementation calls {@link #setHovered} to update the hovered state
7784 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007785 * is enabled and is clickable. The default implementation also sends hover
7786 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007787 * </p>
7788 *
7789 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007790 * @return True if the view handled the hover event.
7791 *
7792 * @see #isHovered
7793 * @see #setHovered
7794 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007795 */
7796 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007797 // The root view may receive hover (or touch) events that are outside the bounds of
7798 // the window. This code ensures that we only send accessibility events for
7799 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007800 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007801 if (!mSendingHoverAccessibilityEvents) {
7802 if ((action == MotionEvent.ACTION_HOVER_ENTER
7803 || action == MotionEvent.ACTION_HOVER_MOVE)
7804 && !hasHoveredChild()
7805 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007806 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007807 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007808 }
7809 } else {
7810 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007811 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007812 && !pointInView(event.getX(), event.getY()))) {
7813 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007814 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007815 // If the window does not have input focus we take away accessibility
7816 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007817 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007818 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007819 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007820 }
Jeff Browna1b24182011-07-28 13:38:24 -07007821 }
7822
Jeff Brown87b7f802011-06-21 18:35:45 -07007823 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007824 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007825 case MotionEvent.ACTION_HOVER_ENTER:
7826 setHovered(true);
7827 break;
7828 case MotionEvent.ACTION_HOVER_EXIT:
7829 setHovered(false);
7830 break;
7831 }
Jeff Browna1b24182011-07-28 13:38:24 -07007832
7833 // Dispatch the event to onGenericMotionEvent before returning true.
7834 // This is to provide compatibility with existing applications that
7835 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7836 // break because of the new default handling for hoverable views
7837 // in onHoverEvent.
7838 // Note that onGenericMotionEvent will be called by default when
7839 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7840 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007841 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007842 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007843
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007844 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007845 }
7846
7847 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007848 * Returns true if the view should handle {@link #onHoverEvent}
7849 * by calling {@link #setHovered} to change its hovered state.
7850 *
7851 * @return True if the view is hoverable.
7852 */
7853 private boolean isHoverable() {
7854 final int viewFlags = mViewFlags;
7855 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7856 return false;
7857 }
7858
7859 return (viewFlags & CLICKABLE) == CLICKABLE
7860 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7861 }
7862
7863 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007864 * Returns true if the view is currently hovered.
7865 *
7866 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007867 *
7868 * @see #setHovered
7869 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007870 */
Jeff Brown10b62902011-06-20 16:40:37 -07007871 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007872 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007873 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08007874 }
7875
7876 /**
7877 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007878 * <p>
7879 * Calling this method also changes the drawable state of the view. This
7880 * enables the view to react to hover by using different drawable resources
7881 * to change its appearance.
7882 * </p><p>
7883 * The {@link #onHoverChanged} method is called when the hovered state changes.
7884 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007885 *
7886 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007887 *
7888 * @see #isHovered
7889 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007890 */
7891 public void setHovered(boolean hovered) {
7892 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007893 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
7894 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007895 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007896 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007897 }
7898 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007899 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
7900 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007901 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007902 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007903 }
7904 }
7905 }
7906
7907 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007908 * Implement this method to handle hover state changes.
7909 * <p>
7910 * This method is called whenever the hover state changes as a result of a
7911 * call to {@link #setHovered}.
7912 * </p>
7913 *
7914 * @param hovered The current hover state, as returned by {@link #isHovered}.
7915 *
7916 * @see #isHovered
7917 * @see #setHovered
7918 */
7919 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007920 }
7921
7922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 * Implement this method to handle touch screen motion events.
7924 *
7925 * @param event The motion event.
7926 * @return True if the event was handled, false otherwise.
7927 */
7928 public boolean onTouchEvent(MotionEvent event) {
7929 final int viewFlags = mViewFlags;
7930
7931 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007932 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007933 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 // A disabled view that is clickable still consumes the touch
7936 // events, it just doesn't respond to them.
7937 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7938 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7939 }
7940
7941 if (mTouchDelegate != null) {
7942 if (mTouchDelegate.onTouchEvent(event)) {
7943 return true;
7944 }
7945 }
7946
7947 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7948 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7949 switch (event.getAction()) {
7950 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07007951 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
7952 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 // take focus if we don't have it already and we should in
7954 // touch mode.
7955 boolean focusTaken = false;
7956 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7957 focusTaken = requestFocus();
7958 }
7959
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007960 if (prepressed) {
7961 // The button is being released before we actually
7962 // showed it as pressed. Make it show the pressed
7963 // state now (before scheduling the click) to ensure
7964 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007965 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007966 }
Joe Malin32736f02011-01-19 16:14:20 -08007967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 if (!mHasPerformedLongPress) {
7969 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007970 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971
7972 // Only perform take click actions if we were in the pressed state
7973 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007974 // Use a Runnable and post this rather than calling
7975 // performClick directly. This lets other visual state
7976 // of the view update before click actions start.
7977 if (mPerformClick == null) {
7978 mPerformClick = new PerformClick();
7979 }
7980 if (!post(mPerformClick)) {
7981 performClick();
7982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 }
7984 }
7985
7986 if (mUnsetPressedState == null) {
7987 mUnsetPressedState = new UnsetPressedState();
7988 }
7989
Adam Powelle14579b2009-12-16 18:39:52 -08007990 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007991 postDelayed(mUnsetPressedState,
7992 ViewConfiguration.getPressedStateDuration());
7993 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007994 // If the post failed, unpress right now
7995 mUnsetPressedState.run();
7996 }
Adam Powelle14579b2009-12-16 18:39:52 -08007997 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 }
7999 break;
8000
8001 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008002 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008003
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008004 if (performButtonActionOnTouchDown(event)) {
8005 break;
8006 }
8007
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008008 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008009 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008010
8011 // For views inside a scrolling container, delay the pressed feedback for
8012 // a short period in case this is a scroll.
8013 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008014 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008015 if (mPendingCheckForTap == null) {
8016 mPendingCheckForTap = new CheckForTap();
8017 }
8018 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8019 } else {
8020 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008021 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008022 checkForLongClick(0);
8023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 break;
8025
8026 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008027 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008028 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 break;
8030
8031 case MotionEvent.ACTION_MOVE:
8032 final int x = (int) event.getX();
8033 final int y = (int) event.getY();
8034
8035 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008036 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008038 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008039 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008040 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008041 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042
Adam Powell4d6f0662012-02-21 15:11:11 -08008043 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 }
8046 break;
8047 }
8048 return true;
8049 }
8050
8051 return false;
8052 }
8053
8054 /**
Adam Powell10298662011-08-14 18:26:30 -07008055 * @hide
8056 */
8057 public boolean isInScrollingContainer() {
8058 ViewParent p = getParent();
8059 while (p != null && p instanceof ViewGroup) {
8060 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8061 return true;
8062 }
8063 p = p.getParent();
8064 }
8065 return false;
8066 }
8067
8068 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008069 * Remove the longpress detection timer.
8070 */
8071 private void removeLongPressCallback() {
8072 if (mPendingCheckForLongPress != null) {
8073 removeCallbacks(mPendingCheckForLongPress);
8074 }
8075 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008076
8077 /**
8078 * Remove the pending click action
8079 */
8080 private void removePerformClickCallback() {
8081 if (mPerformClick != null) {
8082 removeCallbacks(mPerformClick);
8083 }
8084 }
8085
Adam Powelle14579b2009-12-16 18:39:52 -08008086 /**
Romain Guya440b002010-02-24 15:57:54 -08008087 * Remove the prepress detection timer.
8088 */
8089 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008090 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008091 setPressed(false);
8092 removeCallbacks(mUnsetPressedState);
8093 }
8094 }
8095
8096 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008097 * Remove the tap detection timer.
8098 */
8099 private void removeTapCallback() {
8100 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008101 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008102 removeCallbacks(mPendingCheckForTap);
8103 }
8104 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008105
8106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 * Cancels a pending long press. Your subclass can use this if you
8108 * want the context menu to come up if the user presses and holds
8109 * at the same place, but you don't want it to come up if they press
8110 * and then move around enough to cause scrolling.
8111 */
8112 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008113 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008114
8115 /*
8116 * The prepressed state handled by the tap callback is a display
8117 * construct, but the tap callback will post a long press callback
8118 * less its own timeout. Remove it here.
8119 */
8120 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122
8123 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008124 * Remove the pending callback for sending a
8125 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8126 */
8127 private void removeSendViewScrolledAccessibilityEventCallback() {
8128 if (mSendViewScrolledAccessibilityEvent != null) {
8129 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008130 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008131 }
8132 }
8133
8134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 * Sets the TouchDelegate for this View.
8136 */
8137 public void setTouchDelegate(TouchDelegate delegate) {
8138 mTouchDelegate = delegate;
8139 }
8140
8141 /**
8142 * Gets the TouchDelegate for this View.
8143 */
8144 public TouchDelegate getTouchDelegate() {
8145 return mTouchDelegate;
8146 }
8147
8148 /**
8149 * Set flags controlling behavior of this view.
8150 *
8151 * @param flags Constant indicating the value which should be set
8152 * @param mask Constant indicating the bit range that should be changed
8153 */
8154 void setFlags(int flags, int mask) {
8155 int old = mViewFlags;
8156 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8157
8158 int changed = mViewFlags ^ old;
8159 if (changed == 0) {
8160 return;
8161 }
8162 int privateFlags = mPrivateFlags;
8163
8164 /* Check if the FOCUSABLE bit has changed */
8165 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008166 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008167 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008168 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 /* Give up focus if we are no longer focusable */
8170 clearFocus();
8171 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008172 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 /*
8174 * Tell the view system that we are now available to take focus
8175 * if no one else already has it.
8176 */
8177 if (mParent != null) mParent.focusableViewAvailable(this);
8178 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008179 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8180 notifyAccessibilityStateChanged();
8181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 }
8183
8184 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8185 if ((changed & VISIBILITY_MASK) != 0) {
8186 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008187 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008188 * it was not visible. Marking it drawn ensures that the invalidation will
8189 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008191 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008192 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193
8194 needGlobalAttributesUpdate(true);
8195
8196 // a view becoming visible is worth notifying the parent
8197 // about in case nothing has focus. even if this specific view
8198 // isn't focusable, it may contain something that is, so let
8199 // the root view try to give this focus if nothing else does.
8200 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8201 mParent.focusableViewAvailable(this);
8202 }
8203 }
8204 }
8205
8206 /* Check if the GONE bit has changed */
8207 if ((changed & GONE) != 0) {
8208 needGlobalAttributesUpdate(false);
8209 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210
Romain Guyecd80ee2009-12-03 17:13:02 -08008211 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8212 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008213 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008214 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008215 if (mParent instanceof View) {
8216 // GONE views noop invalidation, so invalidate the parent
8217 ((View) mParent).invalidate(true);
8218 }
8219 // Mark the view drawn to ensure that it gets invalidated properly the next
8220 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008221 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 }
8223 if (mAttachInfo != null) {
8224 mAttachInfo.mViewVisibilityChanged = true;
8225 }
8226 }
8227
8228 /* Check if the VISIBLE bit has changed */
8229 if ((changed & INVISIBLE) != 0) {
8230 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008231 /*
8232 * If this view is becoming invisible, set the DRAWN flag so that
8233 * the next invalidate() will not be skipped.
8234 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008235 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236
8237 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008238 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 if (getRootView() != this) {
8240 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008241 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 }
8243 }
8244 if (mAttachInfo != null) {
8245 mAttachInfo.mViewVisibilityChanged = true;
8246 }
8247 }
8248
Adam Powell326d8082009-12-09 15:10:07 -08008249 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008250 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008251 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8252 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008253 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008254 } else if (mParent != null) {
8255 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008256 }
Adam Powell326d8082009-12-09 15:10:07 -08008257 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8258 }
8259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8261 destroyDrawingCache();
8262 }
8263
8264 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8265 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008266 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008267 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 }
8269
8270 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8271 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008272 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 }
8274
8275 if ((changed & DRAW_MASK) != 0) {
8276 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008277 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008278 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8279 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008281 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 }
8283 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008284 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 }
8286 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008287 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 }
8289
8290 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008291 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 mParent.recomputeViewAttributes(this);
8293 }
8294 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008295
8296 if (AccessibilityManager.getInstance(mContext).isEnabled()
8297 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8298 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8299 notifyAccessibilityStateChanged();
8300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008301 }
8302
8303 /**
8304 * Change the view's z order in the tree, so it's on top of other sibling
8305 * views
8306 */
8307 public void bringToFront() {
8308 if (mParent != null) {
8309 mParent.bringChildToFront(this);
8310 }
8311 }
8312
8313 /**
8314 * This is called in response to an internal scroll in this view (i.e., the
8315 * view scrolled its own contents). This is typically as a result of
8316 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8317 * called.
8318 *
8319 * @param l Current horizontal scroll origin.
8320 * @param t Current vertical scroll origin.
8321 * @param oldl Previous horizontal scroll origin.
8322 * @param oldt Previous vertical scroll origin.
8323 */
8324 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008325 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8326 postSendViewScrolledAccessibilityEventCallback();
8327 }
8328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 mBackgroundSizeChanged = true;
8330
8331 final AttachInfo ai = mAttachInfo;
8332 if (ai != null) {
8333 ai.mViewScrollChanged = true;
8334 }
8335 }
8336
8337 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008338 * Interface definition for a callback to be invoked when the layout bounds of a view
8339 * changes due to layout processing.
8340 */
8341 public interface OnLayoutChangeListener {
8342 /**
8343 * Called when the focus state of a view has changed.
8344 *
8345 * @param v The view whose state has changed.
8346 * @param left The new value of the view's left property.
8347 * @param top The new value of the view's top property.
8348 * @param right The new value of the view's right property.
8349 * @param bottom The new value of the view's bottom property.
8350 * @param oldLeft The previous value of the view's left property.
8351 * @param oldTop The previous value of the view's top property.
8352 * @param oldRight The previous value of the view's right property.
8353 * @param oldBottom The previous value of the view's bottom property.
8354 */
8355 void onLayoutChange(View v, int left, int top, int right, int bottom,
8356 int oldLeft, int oldTop, int oldRight, int oldBottom);
8357 }
8358
8359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 * This is called during layout when the size of this view has changed. If
8361 * you were just added to the view hierarchy, you're called with the old
8362 * values of 0.
8363 *
8364 * @param w Current width of this view.
8365 * @param h Current height of this view.
8366 * @param oldw Old width of this view.
8367 * @param oldh Old height of this view.
8368 */
8369 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8370 }
8371
8372 /**
8373 * Called by draw to draw the child views. This may be overridden
8374 * by derived classes to gain control just before its children are drawn
8375 * (but after its own view has been drawn).
8376 * @param canvas the canvas on which to draw the view
8377 */
8378 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 }
8381
8382 /**
8383 * Gets the parent of this view. Note that the parent is a
8384 * ViewParent and not necessarily a View.
8385 *
8386 * @return Parent of this view.
8387 */
8388 public final ViewParent getParent() {
8389 return mParent;
8390 }
8391
8392 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008393 * Set the horizontal scrolled position of your view. This will cause a call to
8394 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8395 * invalidated.
8396 * @param value the x position to scroll to
8397 */
8398 public void setScrollX(int value) {
8399 scrollTo(value, mScrollY);
8400 }
8401
8402 /**
8403 * Set the vertical scrolled position of your view. This will cause a call to
8404 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8405 * invalidated.
8406 * @param value the y position to scroll to
8407 */
8408 public void setScrollY(int value) {
8409 scrollTo(mScrollX, value);
8410 }
8411
8412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 * Return the scrolled left position of this view. This is the left edge of
8414 * the displayed part of your view. You do not need to draw any pixels
8415 * farther left, since those are outside of the frame of your view on
8416 * screen.
8417 *
8418 * @return The left edge of the displayed part of your view, in pixels.
8419 */
8420 public final int getScrollX() {
8421 return mScrollX;
8422 }
8423
8424 /**
8425 * Return the scrolled top position of this view. This is the top edge of
8426 * the displayed part of your view. You do not need to draw any pixels above
8427 * it, since those are outside of the frame of your view on screen.
8428 *
8429 * @return The top edge of the displayed part of your view, in pixels.
8430 */
8431 public final int getScrollY() {
8432 return mScrollY;
8433 }
8434
8435 /**
8436 * Return the width of the your view.
8437 *
8438 * @return The width of your view, in pixels.
8439 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008440 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 public final int getWidth() {
8442 return mRight - mLeft;
8443 }
8444
8445 /**
8446 * Return the height of your view.
8447 *
8448 * @return The height of your view, in pixels.
8449 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008450 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 public final int getHeight() {
8452 return mBottom - mTop;
8453 }
8454
8455 /**
8456 * Return the visible drawing bounds of your view. Fills in the output
8457 * rectangle with the values from getScrollX(), getScrollY(),
8458 * getWidth(), and getHeight().
8459 *
8460 * @param outRect The (scrolled) drawing bounds of the view.
8461 */
8462 public void getDrawingRect(Rect outRect) {
8463 outRect.left = mScrollX;
8464 outRect.top = mScrollY;
8465 outRect.right = mScrollX + (mRight - mLeft);
8466 outRect.bottom = mScrollY + (mBottom - mTop);
8467 }
8468
8469 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008470 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8471 * raw width component (that is the result is masked by
8472 * {@link #MEASURED_SIZE_MASK}).
8473 *
8474 * @return The raw measured width of this view.
8475 */
8476 public final int getMeasuredWidth() {
8477 return mMeasuredWidth & MEASURED_SIZE_MASK;
8478 }
8479
8480 /**
8481 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008482 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008483 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 * This should be used during measurement and layout calculations only. Use
8485 * {@link #getWidth()} to see how wide a view is after layout.
8486 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008487 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008489 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 return mMeasuredWidth;
8491 }
8492
8493 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008494 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8495 * raw width component (that is the result is masked by
8496 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008498 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 */
8500 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008501 return mMeasuredHeight & MEASURED_SIZE_MASK;
8502 }
8503
8504 /**
8505 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008506 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008507 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8508 * This should be used during measurement and layout calculations only. Use
8509 * {@link #getHeight()} to see how wide a view is after layout.
8510 *
8511 * @return The measured width of this view as a bit mask.
8512 */
8513 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 return mMeasuredHeight;
8515 }
8516
8517 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008518 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8519 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8520 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8521 * and the height component is at the shifted bits
8522 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8523 */
8524 public final int getMeasuredState() {
8525 return (mMeasuredWidth&MEASURED_STATE_MASK)
8526 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8527 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8528 }
8529
8530 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008531 * The transform matrix of this view, which is calculated based on the current
8532 * roation, scale, and pivot properties.
8533 *
8534 * @see #getRotation()
8535 * @see #getScaleX()
8536 * @see #getScaleY()
8537 * @see #getPivotX()
8538 * @see #getPivotY()
8539 * @return The current transform matrix for the view
8540 */
8541 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008542 if (mTransformationInfo != null) {
8543 updateMatrix();
8544 return mTransformationInfo.mMatrix;
8545 }
8546 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008547 }
8548
8549 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008550 * Utility function to determine if the value is far enough away from zero to be
8551 * considered non-zero.
8552 * @param value A floating point value to check for zero-ness
8553 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8554 */
8555 private static boolean nonzero(float value) {
8556 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8557 }
8558
8559 /**
Jeff Brown86671742010-09-30 20:00:15 -07008560 * Returns true if the transform matrix is the identity matrix.
8561 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008562 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008563 * @return True if the transform matrix is the identity matrix, false otherwise.
8564 */
Jeff Brown86671742010-09-30 20:00:15 -07008565 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008566 if (mTransformationInfo != null) {
8567 updateMatrix();
8568 return mTransformationInfo.mMatrixIsIdentity;
8569 }
8570 return true;
8571 }
8572
8573 void ensureTransformationInfo() {
8574 if (mTransformationInfo == null) {
8575 mTransformationInfo = new TransformationInfo();
8576 }
Jeff Brown86671742010-09-30 20:00:15 -07008577 }
8578
8579 /**
8580 * Recomputes the transform matrix if necessary.
8581 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008582 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008583 final TransformationInfo info = mTransformationInfo;
8584 if (info == null) {
8585 return;
8586 }
8587 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008588 // transform-related properties have changed since the last time someone
8589 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008590
8591 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008592 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008593 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8594 info.mPrevWidth = mRight - mLeft;
8595 info.mPrevHeight = mBottom - mTop;
8596 info.mPivotX = info.mPrevWidth / 2f;
8597 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008598 }
8599 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008600 info.mMatrix.reset();
8601 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8602 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8603 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8604 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008605 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008606 if (info.mCamera == null) {
8607 info.mCamera = new Camera();
8608 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008609 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008610 info.mCamera.save();
8611 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8612 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8613 info.mCamera.getMatrix(info.matrix3D);
8614 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8615 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8616 info.mPivotY + info.mTranslationY);
8617 info.mMatrix.postConcat(info.matrix3D);
8618 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008619 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008620 info.mMatrixDirty = false;
8621 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8622 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008623 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008624 }
8625
8626 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008627 * When searching for a view to focus this rectangle is used when considering if this view is
8628 * a good candidate for receiving focus.
8629 *
8630 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8631 *
8632 * @param r The rectangle to fill in, in this view's coordinates.
8633 */
8634 public void getFocusRect(Rect r) {
8635 getDrawingRect(r);
8636 }
8637
8638 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008639 * Utility method to retrieve the inverse of the current mMatrix property.
8640 * We cache the matrix to avoid recalculating it when transform properties
8641 * have not changed.
8642 *
8643 * @return The inverse of the current matrix of this view.
8644 */
Jeff Brown86671742010-09-30 20:00:15 -07008645 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008646 final TransformationInfo info = mTransformationInfo;
8647 if (info != null) {
8648 updateMatrix();
8649 if (info.mInverseMatrixDirty) {
8650 if (info.mInverseMatrix == null) {
8651 info.mInverseMatrix = new Matrix();
8652 }
8653 info.mMatrix.invert(info.mInverseMatrix);
8654 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008655 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008656 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008657 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008658 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008659 }
8660
8661 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008662 * Gets the distance along the Z axis from the camera to this view.
8663 *
8664 * @see #setCameraDistance(float)
8665 *
8666 * @return The distance along the Z axis.
8667 */
8668 public float getCameraDistance() {
8669 ensureTransformationInfo();
8670 final float dpi = mResources.getDisplayMetrics().densityDpi;
8671 final TransformationInfo info = mTransformationInfo;
8672 if (info.mCamera == null) {
8673 info.mCamera = new Camera();
8674 info.matrix3D = new Matrix();
8675 }
8676 return -(info.mCamera.getLocationZ() * dpi);
8677 }
8678
8679 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008680 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8681 * views are drawn) from the camera to this view. The camera's distance
8682 * affects 3D transformations, for instance rotations around the X and Y
8683 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008684 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008685 * use a camera distance that's greater than the height (X axis rotation) or
8686 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008687 *
Romain Guya5364ee2011-02-24 14:46:04 -08008688 * <p>The distance of the camera from the view plane can have an affect on the
8689 * perspective distortion of the view when it is rotated around the x or y axis.
8690 * For example, a large distance will result in a large viewing angle, and there
8691 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008692 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008693 * also result in some drawing artifacts if the rotated view ends up partially
8694 * behind the camera (which is why the recommendation is to use a distance at
8695 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008696 *
Romain Guya5364ee2011-02-24 14:46:04 -08008697 * <p>The distance is expressed in "depth pixels." The default distance depends
8698 * on the screen density. For instance, on a medium density display, the
8699 * default distance is 1280. On a high density display, the default distance
8700 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008701 *
Romain Guya5364ee2011-02-24 14:46:04 -08008702 * <p>If you want to specify a distance that leads to visually consistent
8703 * results across various densities, use the following formula:</p>
8704 * <pre>
8705 * float scale = context.getResources().getDisplayMetrics().density;
8706 * view.setCameraDistance(distance * scale);
8707 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008708 *
Romain Guya5364ee2011-02-24 14:46:04 -08008709 * <p>The density scale factor of a high density display is 1.5,
8710 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008711 *
Romain Guya5364ee2011-02-24 14:46:04 -08008712 * @param distance The distance in "depth pixels", if negative the opposite
8713 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008714 *
8715 * @see #setRotationX(float)
8716 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008717 */
8718 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008719 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008720
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008721 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008722 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008723 final TransformationInfo info = mTransformationInfo;
8724 if (info.mCamera == null) {
8725 info.mCamera = new Camera();
8726 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008727 }
8728
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008729 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8730 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008731
Chet Haase9d1992d2012-03-13 11:03:25 -07008732 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008733 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008734 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008735 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008736 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008737 // View was rejected last time it was drawn by its parent; this may have changed
8738 invalidateParentIfNeeded();
8739 }
Romain Guya5364ee2011-02-24 14:46:04 -08008740 }
8741
8742 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008743 * The degrees that the view is rotated around the pivot point.
8744 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008745 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008746 * @see #getPivotX()
8747 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008748 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008749 * @return The degrees of rotation.
8750 */
Chet Haasea5531132012-02-02 13:41:44 -08008751 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008752 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008753 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008754 }
8755
8756 /**
Chet Haase897247b2010-09-09 14:54:47 -07008757 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8758 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008759 *
8760 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008761 *
8762 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008763 * @see #getPivotX()
8764 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008765 * @see #setRotationX(float)
8766 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008767 *
8768 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008769 */
8770 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008771 ensureTransformationInfo();
8772 final TransformationInfo info = mTransformationInfo;
8773 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008774 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008775 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008776 info.mRotation = rotation;
8777 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008778 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008779 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008780 mDisplayList.setRotation(rotation);
8781 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008782 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008783 // View was rejected last time it was drawn by its parent; this may have changed
8784 invalidateParentIfNeeded();
8785 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008786 }
8787 }
8788
8789 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008790 * The degrees that the view is rotated around the vertical axis through the pivot point.
8791 *
8792 * @see #getPivotX()
8793 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008794 * @see #setRotationY(float)
8795 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008796 * @return The degrees of Y rotation.
8797 */
Chet Haasea5531132012-02-02 13:41:44 -08008798 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008799 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008800 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008801 }
8802
8803 /**
Chet Haase897247b2010-09-09 14:54:47 -07008804 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8805 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8806 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008807 *
Romain Guya5364ee2011-02-24 14:46:04 -08008808 * When rotating large views, it is recommended to adjust the camera distance
8809 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008810 *
8811 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008812 *
8813 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008814 * @see #getPivotX()
8815 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008816 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008817 * @see #setRotationX(float)
8818 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008819 *
8820 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008821 */
8822 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008823 ensureTransformationInfo();
8824 final TransformationInfo info = mTransformationInfo;
8825 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008826 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008827 info.mRotationY = rotationY;
8828 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008829 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008830 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008831 mDisplayList.setRotationY(rotationY);
8832 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008833 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008834 // View was rejected last time it was drawn by its parent; this may have changed
8835 invalidateParentIfNeeded();
8836 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008837 }
8838 }
8839
8840 /**
8841 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8842 *
8843 * @see #getPivotX()
8844 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008845 * @see #setRotationX(float)
8846 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008847 * @return The degrees of X rotation.
8848 */
Chet Haasea5531132012-02-02 13:41:44 -08008849 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008850 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008851 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008852 }
8853
8854 /**
Chet Haase897247b2010-09-09 14:54:47 -07008855 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8856 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8857 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008858 *
Romain Guya5364ee2011-02-24 14:46:04 -08008859 * When rotating large views, it is recommended to adjust the camera distance
8860 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008861 *
8862 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008863 *
8864 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008865 * @see #getPivotX()
8866 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008867 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008868 * @see #setRotationY(float)
8869 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008870 *
8871 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008872 */
8873 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008874 ensureTransformationInfo();
8875 final TransformationInfo info = mTransformationInfo;
8876 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008877 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008878 info.mRotationX = rotationX;
8879 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008880 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008881 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008882 mDisplayList.setRotationX(rotationX);
8883 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008884 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008885 // View was rejected last time it was drawn by its parent; this may have changed
8886 invalidateParentIfNeeded();
8887 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008888 }
8889 }
8890
8891 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008892 * The amount that the view is scaled in x around the pivot point, as a proportion of
8893 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8894 *
Joe Onorato93162322010-09-16 15:42:01 -04008895 * <p>By default, this is 1.0f.
8896 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008897 * @see #getPivotX()
8898 * @see #getPivotY()
8899 * @return The scaling factor.
8900 */
Chet Haasea5531132012-02-02 13:41:44 -08008901 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008902 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008903 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008904 }
8905
8906 /**
8907 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8908 * the view's unscaled width. A value of 1 means that no scaling is applied.
8909 *
8910 * @param scaleX The scaling factor.
8911 * @see #getPivotX()
8912 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008913 *
8914 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008915 */
8916 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008917 ensureTransformationInfo();
8918 final TransformationInfo info = mTransformationInfo;
8919 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008920 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008921 info.mScaleX = scaleX;
8922 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008923 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008924 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008925 mDisplayList.setScaleX(scaleX);
8926 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008927 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008928 // View was rejected last time it was drawn by its parent; this may have changed
8929 invalidateParentIfNeeded();
8930 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008931 }
8932 }
8933
8934 /**
8935 * The amount that the view is scaled in y around the pivot point, as a proportion of
8936 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8937 *
Joe Onorato93162322010-09-16 15:42:01 -04008938 * <p>By default, this is 1.0f.
8939 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008940 * @see #getPivotX()
8941 * @see #getPivotY()
8942 * @return The scaling factor.
8943 */
Chet Haasea5531132012-02-02 13:41:44 -08008944 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008945 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008946 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008947 }
8948
8949 /**
8950 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8951 * the view's unscaled width. A value of 1 means that no scaling is applied.
8952 *
8953 * @param scaleY The scaling factor.
8954 * @see #getPivotX()
8955 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008956 *
8957 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008958 */
8959 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008960 ensureTransformationInfo();
8961 final TransformationInfo info = mTransformationInfo;
8962 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008963 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008964 info.mScaleY = scaleY;
8965 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008966 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008967 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008968 mDisplayList.setScaleY(scaleY);
8969 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008970 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008971 // View was rejected last time it was drawn by its parent; this may have changed
8972 invalidateParentIfNeeded();
8973 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008974 }
8975 }
8976
8977 /**
8978 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8979 * and {@link #setScaleX(float) scaled}.
8980 *
8981 * @see #getRotation()
8982 * @see #getScaleX()
8983 * @see #getScaleY()
8984 * @see #getPivotY()
8985 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008986 *
8987 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008988 */
Chet Haasea5531132012-02-02 13:41:44 -08008989 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008990 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008992 }
8993
8994 /**
8995 * Sets the x location of the point around which the view is
8996 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008997 * By default, the pivot point is centered on the object.
8998 * Setting this property disables this behavior and causes the view to use only the
8999 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009000 *
9001 * @param pivotX The x location of the pivot point.
9002 * @see #getRotation()
9003 * @see #getScaleX()
9004 * @see #getScaleY()
9005 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009006 *
9007 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 */
9009 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009010 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009011 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009012 final TransformationInfo info = mTransformationInfo;
9013 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009014 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009015 info.mPivotX = pivotX;
9016 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009017 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009018 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009019 mDisplayList.setPivotX(pivotX);
9020 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009021 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009022 // View was rejected last time it was drawn by its parent; this may have changed
9023 invalidateParentIfNeeded();
9024 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009025 }
9026 }
9027
9028 /**
9029 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9030 * and {@link #setScaleY(float) scaled}.
9031 *
9032 * @see #getRotation()
9033 * @see #getScaleX()
9034 * @see #getScaleY()
9035 * @see #getPivotY()
9036 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009037 *
9038 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009039 */
Chet Haasea5531132012-02-02 13:41:44 -08009040 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009041 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009042 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009043 }
9044
9045 /**
9046 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009047 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9048 * Setting this property disables this behavior and causes the view to use only the
9049 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009050 *
9051 * @param pivotY The y location of the pivot point.
9052 * @see #getRotation()
9053 * @see #getScaleX()
9054 * @see #getScaleY()
9055 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009056 *
9057 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009058 */
9059 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009060 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009061 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009062 final TransformationInfo info = mTransformationInfo;
9063 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009064 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009065 info.mPivotY = pivotY;
9066 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009067 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009068 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009069 mDisplayList.setPivotY(pivotY);
9070 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009071 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009072 // View was rejected last time it was drawn by its parent; this may have changed
9073 invalidateParentIfNeeded();
9074 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009075 }
9076 }
9077
9078 /**
9079 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9080 * completely transparent and 1 means the view is completely opaque.
9081 *
Joe Onorato93162322010-09-16 15:42:01 -04009082 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009083 * @return The opacity of the view.
9084 */
Chet Haasea5531132012-02-02 13:41:44 -08009085 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009086 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009087 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009088 }
9089
9090 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009091 * Returns whether this View has content which overlaps. This function, intended to be
9092 * overridden by specific View types, is an optimization when alpha is set on a view. If
9093 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9094 * and then composited it into place, which can be expensive. If the view has no overlapping
9095 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9096 * An example of overlapping rendering is a TextView with a background image, such as a
9097 * Button. An example of non-overlapping rendering is a TextView with no background, or
9098 * an ImageView with only the foreground image. The default implementation returns true;
9099 * subclasses should override if they have cases which can be optimized.
9100 *
9101 * @return true if the content in this view might overlap, false otherwise.
9102 */
9103 public boolean hasOverlappingRendering() {
9104 return true;
9105 }
9106
9107 /**
Romain Guy171c5922011-01-06 10:04:23 -08009108 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9109 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009110 *
Romain Guy171c5922011-01-06 10:04:23 -08009111 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9112 * responsible for applying the opacity itself. Otherwise, calling this method is
9113 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009114 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009115 *
Chet Haasea5531132012-02-02 13:41:44 -08009116 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9117 * performance implications. It is generally best to use the alpha property sparingly and
9118 * transiently, as in the case of fading animations.</p>
9119 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009120 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009121 *
Joe Malin32736f02011-01-19 16:14:20 -08009122 * @see #setLayerType(int, android.graphics.Paint)
9123 *
Chet Haase73066682010-11-29 15:55:32 -08009124 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009125 */
9126 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009127 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009128 if (mTransformationInfo.mAlpha != alpha) {
9129 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009130 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009131 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009132 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009133 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009134 invalidate(true);
9135 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009136 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009137 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009138 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009139 mDisplayList.setAlpha(alpha);
9140 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009141 }
Chet Haaseed032702010-10-01 14:05:54 -07009142 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009143 }
9144
9145 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009146 * Faster version of setAlpha() which performs the same steps except there are
9147 * no calls to invalidate(). The caller of this function should perform proper invalidation
9148 * on the parent and this object. The return value indicates whether the subclass handles
9149 * alpha (the return value for onSetAlpha()).
9150 *
9151 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009152 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9153 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009154 */
9155 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009156 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009157 if (mTransformationInfo.mAlpha != alpha) {
9158 mTransformationInfo.mAlpha = alpha;
9159 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9160 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009161 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009162 return true;
9163 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009164 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009165 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009166 mDisplayList.setAlpha(alpha);
9167 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009168 }
Chet Haasea00f3862011-02-22 06:34:40 -08009169 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009170 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009171 }
9172
9173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 * Top position of this view relative to its parent.
9175 *
9176 * @return The top of this view, in pixels.
9177 */
9178 @ViewDebug.CapturedViewProperty
9179 public final int getTop() {
9180 return mTop;
9181 }
9182
9183 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009184 * Sets the top position of this view relative to its parent. This method is meant to be called
9185 * by the layout system and should not generally be called otherwise, because the property
9186 * may be changed at any time by the layout.
9187 *
9188 * @param top The top of this view, in pixels.
9189 */
9190 public final void setTop(int top) {
9191 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009192 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009193 final boolean matrixIsIdentity = mTransformationInfo == null
9194 || mTransformationInfo.mMatrixIsIdentity;
9195 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009196 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009197 int minTop;
9198 int yLoc;
9199 if (top < mTop) {
9200 minTop = top;
9201 yLoc = top - mTop;
9202 } else {
9203 minTop = mTop;
9204 yLoc = 0;
9205 }
Chet Haasee9140a72011-02-16 16:23:29 -08009206 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009207 }
9208 } else {
9209 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009210 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009211 }
9212
Chet Haaseed032702010-10-01 14:05:54 -07009213 int width = mRight - mLeft;
9214 int oldHeight = mBottom - mTop;
9215
Chet Haase21cd1382010-09-01 17:42:29 -07009216 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009217 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009218 mDisplayList.setTop(mTop);
9219 }
Chet Haase21cd1382010-09-01 17:42:29 -07009220
Chet Haaseed032702010-10-01 14:05:54 -07009221 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9222
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009223 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009224 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009225 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009226 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009227 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009228 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009229 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009230 }
Chet Haase55dbb652010-12-21 20:15:08 -08009231 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009232 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009233 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009234 // View was rejected last time it was drawn by its parent; this may have changed
9235 invalidateParentIfNeeded();
9236 }
Chet Haase21cd1382010-09-01 17:42:29 -07009237 }
9238 }
9239
9240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 * Bottom position of this view relative to its parent.
9242 *
9243 * @return The bottom of this view, in pixels.
9244 */
9245 @ViewDebug.CapturedViewProperty
9246 public final int getBottom() {
9247 return mBottom;
9248 }
9249
9250 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009251 * True if this view has changed since the last time being drawn.
9252 *
9253 * @return The dirty state of this view.
9254 */
9255 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009256 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009257 }
9258
9259 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009260 * Sets the bottom position of this view relative to its parent. This method is meant to be
9261 * called by the layout system and should not generally be called otherwise, because the
9262 * property may be changed at any time by the layout.
9263 *
9264 * @param bottom The bottom of this view, in pixels.
9265 */
9266 public final void setBottom(int bottom) {
9267 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009268 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009269 final boolean matrixIsIdentity = mTransformationInfo == null
9270 || mTransformationInfo.mMatrixIsIdentity;
9271 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009272 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009273 int maxBottom;
9274 if (bottom < mBottom) {
9275 maxBottom = mBottom;
9276 } else {
9277 maxBottom = bottom;
9278 }
Chet Haasee9140a72011-02-16 16:23:29 -08009279 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009280 }
9281 } else {
9282 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009283 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009284 }
9285
Chet Haaseed032702010-10-01 14:05:54 -07009286 int width = mRight - mLeft;
9287 int oldHeight = mBottom - mTop;
9288
Chet Haase21cd1382010-09-01 17:42:29 -07009289 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009290 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009291 mDisplayList.setBottom(mBottom);
9292 }
Chet Haase21cd1382010-09-01 17:42:29 -07009293
Chet Haaseed032702010-10-01 14:05:54 -07009294 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9295
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009296 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009297 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009298 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009299 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009300 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009301 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009302 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009303 }
Chet Haase55dbb652010-12-21 20:15:08 -08009304 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009305 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009306 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009307 // View was rejected last time it was drawn by its parent; this may have changed
9308 invalidateParentIfNeeded();
9309 }
Chet Haase21cd1382010-09-01 17:42:29 -07009310 }
9311 }
9312
9313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 * Left position of this view relative to its parent.
9315 *
9316 * @return The left edge of this view, in pixels.
9317 */
9318 @ViewDebug.CapturedViewProperty
9319 public final int getLeft() {
9320 return mLeft;
9321 }
9322
9323 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009324 * Sets the left position of this view relative to its parent. This method is meant to be called
9325 * by the layout system and should not generally be called otherwise, because the property
9326 * may be changed at any time by the layout.
9327 *
9328 * @param left The bottom of this view, in pixels.
9329 */
9330 public final void setLeft(int left) {
9331 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009332 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009333 final boolean matrixIsIdentity = mTransformationInfo == null
9334 || mTransformationInfo.mMatrixIsIdentity;
9335 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009336 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009337 int minLeft;
9338 int xLoc;
9339 if (left < mLeft) {
9340 minLeft = left;
9341 xLoc = left - mLeft;
9342 } else {
9343 minLeft = mLeft;
9344 xLoc = 0;
9345 }
Chet Haasee9140a72011-02-16 16:23:29 -08009346 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009347 }
9348 } else {
9349 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009350 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009351 }
9352
Chet Haaseed032702010-10-01 14:05:54 -07009353 int oldWidth = mRight - mLeft;
9354 int height = mBottom - mTop;
9355
Chet Haase21cd1382010-09-01 17:42:29 -07009356 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009357 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009358 mDisplayList.setLeft(left);
9359 }
Chet Haase21cd1382010-09-01 17:42:29 -07009360
Chet Haaseed032702010-10-01 14:05:54 -07009361 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9362
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009363 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009364 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009365 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009366 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009367 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009368 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009369 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009370 }
Chet Haase55dbb652010-12-21 20:15:08 -08009371 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009372 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009373 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009374 // View was rejected last time it was drawn by its parent; this may have changed
9375 invalidateParentIfNeeded();
9376 }
Chet Haase21cd1382010-09-01 17:42:29 -07009377 }
9378 }
9379
9380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 * Right position of this view relative to its parent.
9382 *
9383 * @return The right edge of this view, in pixels.
9384 */
9385 @ViewDebug.CapturedViewProperty
9386 public final int getRight() {
9387 return mRight;
9388 }
9389
9390 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009391 * Sets the right position of this view relative to its parent. This method is meant to be called
9392 * by the layout system and should not generally be called otherwise, because the property
9393 * may be changed at any time by the layout.
9394 *
9395 * @param right The bottom of this view, in pixels.
9396 */
9397 public final void setRight(int right) {
9398 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009399 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009400 final boolean matrixIsIdentity = mTransformationInfo == null
9401 || mTransformationInfo.mMatrixIsIdentity;
9402 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009403 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009404 int maxRight;
9405 if (right < mRight) {
9406 maxRight = mRight;
9407 } else {
9408 maxRight = right;
9409 }
Chet Haasee9140a72011-02-16 16:23:29 -08009410 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009411 }
9412 } else {
9413 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009414 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009415 }
9416
Chet Haaseed032702010-10-01 14:05:54 -07009417 int oldWidth = mRight - mLeft;
9418 int height = mBottom - mTop;
9419
Chet Haase21cd1382010-09-01 17:42:29 -07009420 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009421 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009422 mDisplayList.setRight(mRight);
9423 }
Chet Haase21cd1382010-09-01 17:42:29 -07009424
Chet Haaseed032702010-10-01 14:05:54 -07009425 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9426
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009427 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009428 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009429 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009430 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009431 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009432 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009433 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009434 }
Chet Haase55dbb652010-12-21 20:15:08 -08009435 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009436 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009437 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009438 // View was rejected last time it was drawn by its parent; this may have changed
9439 invalidateParentIfNeeded();
9440 }
Chet Haase21cd1382010-09-01 17:42:29 -07009441 }
9442 }
9443
9444 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009445 * The visual x position of this view, in pixels. This is equivalent to the
9446 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009447 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009448 *
Chet Haasedf030d22010-07-30 17:22:38 -07009449 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009450 */
Chet Haasea5531132012-02-02 13:41:44 -08009451 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009452 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009453 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009454 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009455
Chet Haasedf030d22010-07-30 17:22:38 -07009456 /**
9457 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9458 * {@link #setTranslationX(float) translationX} property to be the difference between
9459 * the x value passed in and the current {@link #getLeft() left} property.
9460 *
9461 * @param x The visual x position of this view, in pixels.
9462 */
9463 public void setX(float x) {
9464 setTranslationX(x - mLeft);
9465 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009466
Chet Haasedf030d22010-07-30 17:22:38 -07009467 /**
9468 * The visual y position of this view, in pixels. This is equivalent to the
9469 * {@link #setTranslationY(float) translationY} property plus the current
9470 * {@link #getTop() top} property.
9471 *
9472 * @return The visual y position of this view, in pixels.
9473 */
Chet Haasea5531132012-02-02 13:41:44 -08009474 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009475 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009476 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009477 }
9478
9479 /**
9480 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9481 * {@link #setTranslationY(float) translationY} property to be the difference between
9482 * the y value passed in and the current {@link #getTop() top} property.
9483 *
9484 * @param y The visual y position of this view, in pixels.
9485 */
9486 public void setY(float y) {
9487 setTranslationY(y - mTop);
9488 }
9489
9490
9491 /**
9492 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9493 * This position is post-layout, in addition to wherever the object's
9494 * layout placed it.
9495 *
9496 * @return The horizontal position of this view relative to its left position, in pixels.
9497 */
Chet Haasea5531132012-02-02 13:41:44 -08009498 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009499 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009500 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009501 }
9502
9503 /**
9504 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9505 * This effectively positions the object post-layout, in addition to wherever the object's
9506 * layout placed it.
9507 *
9508 * @param translationX The horizontal position of this view relative to its left position,
9509 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009510 *
9511 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009512 */
9513 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009514 ensureTransformationInfo();
9515 final TransformationInfo info = mTransformationInfo;
9516 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009517 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009518 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009519 info.mTranslationX = translationX;
9520 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009521 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009522 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009523 mDisplayList.setTranslationX(translationX);
9524 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009525 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009526 // View was rejected last time it was drawn by its parent; this may have changed
9527 invalidateParentIfNeeded();
9528 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009529 }
9530 }
9531
9532 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009533 * The horizontal location of this view relative to its {@link #getTop() top} position.
9534 * This position is post-layout, in addition to wherever the object's
9535 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009536 *
Chet Haasedf030d22010-07-30 17:22:38 -07009537 * @return The vertical position of this view relative to its top position,
9538 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009539 */
Chet Haasea5531132012-02-02 13:41:44 -08009540 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009541 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009542 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009543 }
9544
9545 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009546 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9547 * This effectively positions the object post-layout, in addition to wherever the object's
9548 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009549 *
Chet Haasedf030d22010-07-30 17:22:38 -07009550 * @param translationY The vertical position of this view relative to its top position,
9551 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009552 *
9553 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009554 */
Chet Haasedf030d22010-07-30 17:22:38 -07009555 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009556 ensureTransformationInfo();
9557 final TransformationInfo info = mTransformationInfo;
9558 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009559 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009560 info.mTranslationY = translationY;
9561 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009562 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009563 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009564 mDisplayList.setTranslationY(translationY);
9565 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009566 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009567 // View was rejected last time it was drawn by its parent; this may have changed
9568 invalidateParentIfNeeded();
9569 }
Chet Haasedf030d22010-07-30 17:22:38 -07009570 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009571 }
9572
9573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009574 * Hit rectangle in parent's coordinates
9575 *
9576 * @param outRect The hit rectangle of the view.
9577 */
9578 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009579 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009580 final TransformationInfo info = mTransformationInfo;
9581 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009582 outRect.set(mLeft, mTop, mRight, mBottom);
9583 } else {
9584 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009585 tmpRect.set(-info.mPivotX, -info.mPivotY,
9586 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9587 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009588 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9589 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009590 }
9591 }
9592
9593 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009594 * Determines whether the given point, in local coordinates is inside the view.
9595 */
9596 /*package*/ final boolean pointInView(float localX, float localY) {
9597 return localX >= 0 && localX < (mRight - mLeft)
9598 && localY >= 0 && localY < (mBottom - mTop);
9599 }
9600
9601 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009602 * Utility method to determine whether the given point, in local coordinates,
9603 * is inside the view, where the area of the view is expanded by the slop factor.
9604 * This method is called while processing touch-move events to determine if the event
9605 * is still within the view.
9606 */
9607 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009608 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009609 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009610 }
9611
9612 /**
9613 * When a view has focus and the user navigates away from it, the next view is searched for
9614 * starting from the rectangle filled in by this method.
9615 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009616 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9617 * of the view. However, if your view maintains some idea of internal selection,
9618 * such as a cursor, or a selected row or column, you should override this method and
9619 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 *
9621 * @param r The rectangle to fill in, in this view's coordinates.
9622 */
9623 public void getFocusedRect(Rect r) {
9624 getDrawingRect(r);
9625 }
9626
9627 /**
9628 * If some part of this view is not clipped by any of its parents, then
9629 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009630 * coordinates (without taking possible View rotations into account), offset
9631 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9632 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 *
9634 * @param r If true is returned, r holds the global coordinates of the
9635 * visible portion of this view.
9636 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9637 * between this view and its root. globalOffet may be null.
9638 * @return true if r is non-empty (i.e. part of the view is visible at the
9639 * root level.
9640 */
9641 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9642 int width = mRight - mLeft;
9643 int height = mBottom - mTop;
9644 if (width > 0 && height > 0) {
9645 r.set(0, 0, width, height);
9646 if (globalOffset != null) {
9647 globalOffset.set(-mScrollX, -mScrollY);
9648 }
9649 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9650 }
9651 return false;
9652 }
9653
9654 public final boolean getGlobalVisibleRect(Rect r) {
9655 return getGlobalVisibleRect(r, null);
9656 }
9657
9658 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009659 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 if (getGlobalVisibleRect(r, offset)) {
9661 r.offset(-offset.x, -offset.y); // make r local
9662 return true;
9663 }
9664 return false;
9665 }
9666
9667 /**
9668 * Offset this view's vertical location by the specified number of pixels.
9669 *
9670 * @param offset the number of pixels to offset the view by
9671 */
9672 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009673 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009674 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009675 final boolean matrixIsIdentity = mTransformationInfo == null
9676 || mTransformationInfo.mMatrixIsIdentity;
9677 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009678 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009679 invalidateViewProperty(false, false);
9680 } else {
9681 final ViewParent p = mParent;
9682 if (p != null && mAttachInfo != null) {
9683 final Rect r = mAttachInfo.mTmpInvalRect;
9684 int minTop;
9685 int maxBottom;
9686 int yLoc;
9687 if (offset < 0) {
9688 minTop = mTop + offset;
9689 maxBottom = mBottom;
9690 yLoc = offset;
9691 } else {
9692 minTop = mTop;
9693 maxBottom = mBottom + offset;
9694 yLoc = 0;
9695 }
9696 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9697 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009698 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009699 }
9700 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009701 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009702 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009703
Chet Haasec3aa3612010-06-17 08:50:37 -07009704 mTop += offset;
9705 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009706 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009707 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009708 invalidateViewProperty(false, false);
9709 } else {
9710 if (!matrixIsIdentity) {
9711 invalidateViewProperty(false, true);
9712 }
9713 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009714 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 }
9717
9718 /**
9719 * Offset this view's horizontal location by the specified amount of pixels.
9720 *
9721 * @param offset the numer of pixels to offset the view by
9722 */
9723 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009724 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009725 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009726 final boolean matrixIsIdentity = mTransformationInfo == null
9727 || mTransformationInfo.mMatrixIsIdentity;
9728 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009729 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009730 invalidateViewProperty(false, false);
9731 } else {
9732 final ViewParent p = mParent;
9733 if (p != null && mAttachInfo != null) {
9734 final Rect r = mAttachInfo.mTmpInvalRect;
9735 int minLeft;
9736 int maxRight;
9737 if (offset < 0) {
9738 minLeft = mLeft + offset;
9739 maxRight = mRight;
9740 } else {
9741 minLeft = mLeft;
9742 maxRight = mRight + offset;
9743 }
9744 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9745 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009746 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009747 }
9748 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009749 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009750 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009751
Chet Haasec3aa3612010-06-17 08:50:37 -07009752 mLeft += offset;
9753 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009754 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009755 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009756 invalidateViewProperty(false, false);
9757 } else {
9758 if (!matrixIsIdentity) {
9759 invalidateViewProperty(false, true);
9760 }
9761 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009762 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 }
9765
9766 /**
9767 * Get the LayoutParams associated with this view. All views should have
9768 * layout parameters. These supply parameters to the <i>parent</i> of this
9769 * view specifying how it should be arranged. There are many subclasses of
9770 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9771 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009772 *
9773 * This method may return null if this View is not attached to a parent
9774 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9775 * was not invoked successfully. When a View is attached to a parent
9776 * ViewGroup, this method must not return null.
9777 *
9778 * @return The LayoutParams associated with this view, or null if no
9779 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009781 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009782 public ViewGroup.LayoutParams getLayoutParams() {
9783 return mLayoutParams;
9784 }
9785
9786 /**
9787 * Set the layout parameters associated with this view. These supply
9788 * parameters to the <i>parent</i> of this view specifying how it should be
9789 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9790 * correspond to the different subclasses of ViewGroup that are responsible
9791 * for arranging their children.
9792 *
Romain Guy01c174b2011-02-22 11:51:06 -08009793 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 */
9795 public void setLayoutParams(ViewGroup.LayoutParams params) {
9796 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009797 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009798 }
9799 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009800 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009801 if (mParent instanceof ViewGroup) {
9802 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 requestLayout();
9805 }
9806
9807 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009808 * Resolve the layout parameters depending on the resolved layout direction
9809 */
9810 private void resolveLayoutParams() {
9811 if (mLayoutParams != null) {
9812 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
9813 }
9814 }
9815
9816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 * Set the scrolled position of your view. This will cause a call to
9818 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9819 * invalidated.
9820 * @param x the x position to scroll to
9821 * @param y the y position to scroll to
9822 */
9823 public void scrollTo(int x, int y) {
9824 if (mScrollX != x || mScrollY != y) {
9825 int oldX = mScrollX;
9826 int oldY = mScrollY;
9827 mScrollX = x;
9828 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009829 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009831 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009832 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 }
9835 }
9836
9837 /**
9838 * Move the scrolled position of your view. This will cause a call to
9839 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9840 * invalidated.
9841 * @param x the amount of pixels to scroll by horizontally
9842 * @param y the amount of pixels to scroll by vertically
9843 */
9844 public void scrollBy(int x, int y) {
9845 scrollTo(mScrollX + x, mScrollY + y);
9846 }
9847
9848 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009849 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9850 * animation to fade the scrollbars out after a default delay. If a subclass
9851 * provides animated scrolling, the start delay should equal the duration
9852 * of the scrolling animation.</p>
9853 *
9854 * <p>The animation starts only if at least one of the scrollbars is
9855 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9856 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9857 * this method returns true, and false otherwise. If the animation is
9858 * started, this method calls {@link #invalidate()}; in that case the
9859 * caller should not call {@link #invalidate()}.</p>
9860 *
9861 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009862 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009863 *
9864 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9865 * and {@link #scrollTo(int, int)}.</p>
9866 *
9867 * @return true if the animation is played, false otherwise
9868 *
9869 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009870 * @see #scrollBy(int, int)
9871 * @see #scrollTo(int, int)
9872 * @see #isHorizontalScrollBarEnabled()
9873 * @see #isVerticalScrollBarEnabled()
9874 * @see #setHorizontalScrollBarEnabled(boolean)
9875 * @see #setVerticalScrollBarEnabled(boolean)
9876 */
9877 protected boolean awakenScrollBars() {
9878 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009879 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009880 }
9881
9882 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009883 * Trigger the scrollbars to draw.
9884 * This method differs from awakenScrollBars() only in its default duration.
9885 * initialAwakenScrollBars() will show the scroll bars for longer than
9886 * usual to give the user more of a chance to notice them.
9887 *
9888 * @return true if the animation is played, false otherwise.
9889 */
9890 private boolean initialAwakenScrollBars() {
9891 return mScrollCache != null &&
9892 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9893 }
9894
9895 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009896 * <p>
9897 * Trigger the scrollbars to draw. When invoked this method starts an
9898 * animation to fade the scrollbars out after a fixed delay. If a subclass
9899 * provides animated scrolling, the start delay should equal the duration of
9900 * the scrolling animation.
9901 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009902 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009903 * <p>
9904 * The animation starts only if at least one of the scrollbars is enabled,
9905 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9906 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9907 * this method returns true, and false otherwise. If the animation is
9908 * started, this method calls {@link #invalidate()}; in that case the caller
9909 * should not call {@link #invalidate()}.
9910 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009911 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009912 * <p>
9913 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009914 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009915 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009916 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009917 * @param startDelay the delay, in milliseconds, after which the animation
9918 * should start; when the delay is 0, the animation starts
9919 * immediately
9920 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009921 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009922 * @see #scrollBy(int, int)
9923 * @see #scrollTo(int, int)
9924 * @see #isHorizontalScrollBarEnabled()
9925 * @see #isVerticalScrollBarEnabled()
9926 * @see #setHorizontalScrollBarEnabled(boolean)
9927 * @see #setVerticalScrollBarEnabled(boolean)
9928 */
9929 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009930 return awakenScrollBars(startDelay, true);
9931 }
Joe Malin32736f02011-01-19 16:14:20 -08009932
Mike Cleron290947b2009-09-29 18:34:32 -07009933 /**
9934 * <p>
9935 * Trigger the scrollbars to draw. When invoked this method starts an
9936 * animation to fade the scrollbars out after a fixed delay. If a subclass
9937 * provides animated scrolling, the start delay should equal the duration of
9938 * the scrolling animation.
9939 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009940 *
Mike Cleron290947b2009-09-29 18:34:32 -07009941 * <p>
9942 * The animation starts only if at least one of the scrollbars is enabled,
9943 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9944 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9945 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009946 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009947 * is set to true; in that case the caller
9948 * should not call {@link #invalidate()}.
9949 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009950 *
Mike Cleron290947b2009-09-29 18:34:32 -07009951 * <p>
9952 * This method should be invoked everytime a subclass directly updates the
9953 * scroll parameters.
9954 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009955 *
Mike Cleron290947b2009-09-29 18:34:32 -07009956 * @param startDelay the delay, in milliseconds, after which the animation
9957 * should start; when the delay is 0, the animation starts
9958 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009959 *
Mike Cleron290947b2009-09-29 18:34:32 -07009960 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009961 *
Mike Cleron290947b2009-09-29 18:34:32 -07009962 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009963 *
Mike Cleron290947b2009-09-29 18:34:32 -07009964 * @see #scrollBy(int, int)
9965 * @see #scrollTo(int, int)
9966 * @see #isHorizontalScrollBarEnabled()
9967 * @see #isVerticalScrollBarEnabled()
9968 * @see #setHorizontalScrollBarEnabled(boolean)
9969 * @see #setVerticalScrollBarEnabled(boolean)
9970 */
9971 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009972 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009973
Mike Cleronf116bf82009-09-27 19:14:12 -07009974 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9975 return false;
9976 }
9977
9978 if (scrollCache.scrollBar == null) {
9979 scrollCache.scrollBar = new ScrollBarDrawable();
9980 }
9981
9982 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9983
Mike Cleron290947b2009-09-29 18:34:32 -07009984 if (invalidate) {
9985 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009986 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009987 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009988
9989 if (scrollCache.state == ScrollabilityCache.OFF) {
9990 // FIXME: this is copied from WindowManagerService.
9991 // We should get this value from the system when it
9992 // is possible to do so.
9993 final int KEY_REPEAT_FIRST_DELAY = 750;
9994 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9995 }
9996
9997 // Tell mScrollCache when we should start fading. This may
9998 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009999 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010000 scrollCache.fadeStartTime = fadeStartTime;
10001 scrollCache.state = ScrollabilityCache.ON;
10002
10003 // Schedule our fader to run, unscheduling any old ones first
10004 if (mAttachInfo != null) {
10005 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10006 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10007 }
10008
10009 return true;
10010 }
10011
10012 return false;
10013 }
10014
10015 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010016 * Do not invalidate views which are not visible and which are not running an animation. They
10017 * will not get drawn and they should not set dirty flags as if they will be drawn
10018 */
10019 private boolean skipInvalidate() {
10020 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10021 (!(mParent instanceof ViewGroup) ||
10022 !((ViewGroup) mParent).isViewTransitioning(this));
10023 }
10024 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010025 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010026 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10027 * in the future. This must be called from a UI thread. To call from a non-UI
10028 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 *
10030 * WARNING: This method is destructive to dirty.
10031 * @param dirty the rectangle representing the bounds of the dirty region
10032 */
10033 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010034 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010035 return;
10036 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010037 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10038 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10039 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10040 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10041 mPrivateFlags |= PFLAG_INVALIDATED;
10042 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 final ViewParent p = mParent;
10044 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010045 //noinspection PointlessBooleanExpression,ConstantConditions
10046 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10047 if (p != null && ai != null && ai.mHardwareAccelerated) {
10048 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010049 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010050 p.invalidateChild(this, null);
10051 return;
10052 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 if (p != null && ai != null) {
10055 final int scrollX = mScrollX;
10056 final int scrollY = mScrollY;
10057 final Rect r = ai.mTmpInvalRect;
10058 r.set(dirty.left - scrollX, dirty.top - scrollY,
10059 dirty.right - scrollX, dirty.bottom - scrollY);
10060 mParent.invalidateChild(this, r);
10061 }
10062 }
10063 }
10064
10065 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010066 * 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 -080010067 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010068 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10069 * will be called at some point in the future. This must be called from
10070 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 * @param l the left position of the dirty region
10072 * @param t the top position of the dirty region
10073 * @param r the right position of the dirty region
10074 * @param b the bottom position of the dirty region
10075 */
10076 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010077 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010078 return;
10079 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010080 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10081 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10082 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10083 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10084 mPrivateFlags |= PFLAG_INVALIDATED;
10085 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010086 final ViewParent p = mParent;
10087 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010088 //noinspection PointlessBooleanExpression,ConstantConditions
10089 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10090 if (p != null && ai != null && ai.mHardwareAccelerated) {
10091 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010092 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010093 p.invalidateChild(this, null);
10094 return;
10095 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 if (p != null && ai != null && l < r && t < b) {
10098 final int scrollX = mScrollX;
10099 final int scrollY = mScrollY;
10100 final Rect tmpr = ai.mTmpInvalRect;
10101 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10102 p.invalidateChild(this, tmpr);
10103 }
10104 }
10105 }
10106
10107 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010108 * Invalidate the whole view. If the view is visible,
10109 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10110 * the future. This must be called from a UI thread. To call from a non-UI thread,
10111 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 */
10113 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010114 invalidate(true);
10115 }
Joe Malin32736f02011-01-19 16:14:20 -080010116
Chet Haaseed032702010-10-01 14:05:54 -070010117 /**
10118 * This is where the invalidate() work actually happens. A full invalidate()
10119 * causes the drawing cache to be invalidated, but this function can be called with
10120 * invalidateCache set to false to skip that invalidation step for cases that do not
10121 * need it (for example, a component that remains at the same dimensions with the same
10122 * content).
10123 *
10124 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10125 * well. This is usually true for a full invalidate, but may be set to false if the
10126 * View's contents or dimensions have not changed.
10127 */
Romain Guy849d0a32011-02-01 17:20:48 -080010128 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010129 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010130 return;
10131 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010132 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10133 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10134 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010135 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010136 mPrivateFlags &= ~PFLAG_DRAWN;
10137 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010138 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010139 mPrivateFlags |= PFLAG_INVALIDATED;
10140 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010143 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010144 //noinspection PointlessBooleanExpression,ConstantConditions
10145 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10146 if (p != null && ai != null && ai.mHardwareAccelerated) {
10147 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010148 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010149 p.invalidateChild(this, null);
10150 return;
10151 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010152 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 if (p != null && ai != null) {
10155 final Rect r = ai.mTmpInvalRect;
10156 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10157 // Don't call invalidate -- we don't want to internally scroll
10158 // our own bounds
10159 p.invalidateChild(this, r);
10160 }
10161 }
10162 }
10163
10164 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010165 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10166 * set any flags or handle all of the cases handled by the default invalidation methods.
10167 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10168 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10169 * walk up the hierarchy, transforming the dirty rect as necessary.
10170 *
10171 * The method also handles normal invalidation logic if display list properties are not
10172 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10173 * backup approach, to handle these cases used in the various property-setting methods.
10174 *
10175 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10176 * are not being used in this view
10177 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10178 * list properties are not being used in this view
10179 */
10180 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010181 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010182 if (invalidateParent) {
10183 invalidateParentCaches();
10184 }
10185 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010186 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010187 }
10188 invalidate(false);
10189 } else {
10190 final AttachInfo ai = mAttachInfo;
10191 final ViewParent p = mParent;
10192 if (p != null && ai != null) {
10193 final Rect r = ai.mTmpInvalRect;
10194 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10195 if (mParent instanceof ViewGroup) {
10196 ((ViewGroup) mParent).invalidateChildFast(this, r);
10197 } else {
10198 mParent.invalidateChild(this, r);
10199 }
10200 }
10201 }
10202 }
10203
10204 /**
10205 * Utility method to transform a given Rect by the current matrix of this view.
10206 */
10207 void transformRect(final Rect rect) {
10208 if (!getMatrix().isIdentity()) {
10209 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10210 boundingRect.set(rect);
10211 getMatrix().mapRect(boundingRect);
10212 rect.set((int) (boundingRect.left - 0.5f),
10213 (int) (boundingRect.top - 0.5f),
10214 (int) (boundingRect.right + 0.5f),
10215 (int) (boundingRect.bottom + 0.5f));
10216 }
10217 }
10218
10219 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010220 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010221 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10222 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010223 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10224 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010225 *
10226 * @hide
10227 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010228 protected void invalidateParentCaches() {
10229 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010230 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010231 }
10232 }
Joe Malin32736f02011-01-19 16:14:20 -080010233
Romain Guy0fd89bf2011-01-26 15:41:30 -080010234 /**
10235 * Used to indicate that the parent of this view should be invalidated. This functionality
10236 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10237 * which is necessary when various parent-managed properties of the view change, such as
10238 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10239 * an invalidation event to the parent.
10240 *
10241 * @hide
10242 */
10243 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010244 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010245 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010246 }
10247 }
10248
10249 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010250 * Indicates whether this View is opaque. An opaque View guarantees that it will
10251 * draw all the pixels overlapping its bounds using a fully opaque color.
10252 *
10253 * Subclasses of View should override this method whenever possible to indicate
10254 * whether an instance is opaque. Opaque Views are treated in a special way by
10255 * the View hierarchy, possibly allowing it to perform optimizations during
10256 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010257 *
Romain Guy24443ea2009-05-11 11:56:30 -070010258 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010259 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010260 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010261 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010262 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010263 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010264 }
10265
Adam Powell20232d02010-12-08 21:08:53 -080010266 /**
10267 * @hide
10268 */
10269 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010270 // Opaque if:
10271 // - Has a background
10272 // - Background is opaque
10273 // - Doesn't have scrollbars or scrollbars are inside overlay
10274
Philip Milne6c8ea062012-04-03 17:38:43 -070010275 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010276 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010277 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010278 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010279 }
10280
10281 final int flags = mViewFlags;
10282 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10283 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010284 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010285 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010286 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010287 }
10288 }
10289
10290 /**
10291 * @hide
10292 */
10293 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010294 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010295 }
10296
10297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 * @return A handler associated with the thread running the View. This
10299 * handler can be used to pump events in the UI events queue.
10300 */
10301 public Handler getHandler() {
10302 if (mAttachInfo != null) {
10303 return mAttachInfo.mHandler;
10304 }
10305 return null;
10306 }
10307
10308 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010309 * Gets the view root associated with the View.
10310 * @return The view root, or null if none.
10311 * @hide
10312 */
10313 public ViewRootImpl getViewRootImpl() {
10314 if (mAttachInfo != null) {
10315 return mAttachInfo.mViewRootImpl;
10316 }
10317 return null;
10318 }
10319
10320 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010321 * <p>Causes the Runnable to be added to the message queue.
10322 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010323 *
Romain Guye63a4f32011-08-11 11:33:31 -070010324 * <p>This method can be invoked from outside of the UI thread
10325 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326 *
10327 * @param action The Runnable that will be executed.
10328 *
10329 * @return Returns true if the Runnable was successfully placed in to the
10330 * message queue. Returns false on failure, usually because the
10331 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010332 *
10333 * @see #postDelayed
10334 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 */
10336 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010337 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010338 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010339 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010341 // Assume that post will succeed later
10342 ViewRootImpl.getRunQueue().post(action);
10343 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 }
10345
10346 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010347 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010349 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010350 *
Romain Guye63a4f32011-08-11 11:33:31 -070010351 * <p>This method can be invoked from outside of the UI thread
10352 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 *
10354 * @param action The Runnable that will be executed.
10355 * @param delayMillis The delay (in milliseconds) until the Runnable
10356 * will be executed.
10357 *
10358 * @return true if the Runnable was successfully placed in to the
10359 * message queue. Returns false on failure, usually because the
10360 * looper processing the message queue is exiting. Note that a
10361 * result of true does not mean the Runnable will be processed --
10362 * if the looper is quit before the delivery time of the message
10363 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010364 *
10365 * @see #post
10366 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 */
10368 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010369 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010370 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010371 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010373 // Assume that post will succeed later
10374 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10375 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 }
10377
10378 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010379 * <p>Causes the Runnable to execute on the next animation time step.
10380 * The runnable will be run on the user interface thread.</p>
10381 *
10382 * <p>This method can be invoked from outside of the UI thread
10383 * only when this View is attached to a window.</p>
10384 *
10385 * @param action The Runnable that will be executed.
10386 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010387 * @see #postOnAnimationDelayed
10388 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010389 */
10390 public void postOnAnimation(Runnable action) {
10391 final AttachInfo attachInfo = mAttachInfo;
10392 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010393 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10394 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010395 } else {
10396 // Assume that post will succeed later
10397 ViewRootImpl.getRunQueue().post(action);
10398 }
10399 }
10400
10401 /**
10402 * <p>Causes the Runnable to execute on the next animation time step,
10403 * after the specified amount of time elapses.
10404 * The runnable will be run on the user interface thread.</p>
10405 *
10406 * <p>This method can be invoked from outside of the UI thread
10407 * only when this View is attached to a window.</p>
10408 *
10409 * @param action The Runnable that will be executed.
10410 * @param delayMillis The delay (in milliseconds) until the Runnable
10411 * will be executed.
10412 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010413 * @see #postOnAnimation
10414 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010415 */
10416 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10417 final AttachInfo attachInfo = mAttachInfo;
10418 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010419 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10420 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010421 } else {
10422 // Assume that post will succeed later
10423 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10424 }
10425 }
10426
10427 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010428 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010429 *
Romain Guye63a4f32011-08-11 11:33:31 -070010430 * <p>This method can be invoked from outside of the UI thread
10431 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 *
10433 * @param action The Runnable to remove from the message handling queue
10434 *
10435 * @return true if this view could ask the Handler to remove the Runnable,
10436 * false otherwise. When the returned value is true, the Runnable
10437 * may or may not have been actually removed from the message queue
10438 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010439 *
10440 * @see #post
10441 * @see #postDelayed
10442 * @see #postOnAnimation
10443 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 */
10445 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010446 if (action != null) {
10447 final AttachInfo attachInfo = mAttachInfo;
10448 if (attachInfo != null) {
10449 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010450 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10451 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010452 } else {
10453 // Assume that post will succeed later
10454 ViewRootImpl.getRunQueue().removeCallbacks(action);
10455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 return true;
10458 }
10459
10460 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010461 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10462 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 *
Romain Guye63a4f32011-08-11 11:33:31 -070010464 * <p>This method can be invoked from outside of the UI thread
10465 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010468 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 */
10470 public void postInvalidate() {
10471 postInvalidateDelayed(0);
10472 }
10473
10474 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010475 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10476 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010477 *
Romain Guye63a4f32011-08-11 11:33:31 -070010478 * <p>This method can be invoked from outside of the UI thread
10479 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 *
10481 * @param left The left coordinate of the rectangle to invalidate.
10482 * @param top The top coordinate of the rectangle to invalidate.
10483 * @param right The right coordinate of the rectangle to invalidate.
10484 * @param bottom The bottom coordinate of the rectangle to invalidate.
10485 *
10486 * @see #invalidate(int, int, int, int)
10487 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010488 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 */
10490 public void postInvalidate(int left, int top, int right, int bottom) {
10491 postInvalidateDelayed(0, left, top, right, bottom);
10492 }
10493
10494 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010495 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10496 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010497 *
Romain Guye63a4f32011-08-11 11:33:31 -070010498 * <p>This method can be invoked from outside of the UI thread
10499 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 *
10501 * @param delayMilliseconds the duration in milliseconds to delay the
10502 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010503 *
10504 * @see #invalidate()
10505 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 */
10507 public void postInvalidateDelayed(long delayMilliseconds) {
10508 // We try only with the AttachInfo because there's no point in invalidating
10509 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010510 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010511 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010512 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010513 }
10514 }
10515
10516 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010517 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10518 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010519 *
Romain Guye63a4f32011-08-11 11:33:31 -070010520 * <p>This method can be invoked from outside of the UI thread
10521 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 *
10523 * @param delayMilliseconds the duration in milliseconds to delay the
10524 * invalidation by
10525 * @param left The left coordinate of the rectangle to invalidate.
10526 * @param top The top coordinate of the rectangle to invalidate.
10527 * @param right The right coordinate of the rectangle to invalidate.
10528 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010529 *
10530 * @see #invalidate(int, int, int, int)
10531 * @see #invalidate(Rect)
10532 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 */
10534 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10535 int right, int bottom) {
10536
10537 // We try only with the AttachInfo because there's no point in invalidating
10538 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010539 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010540 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10542 info.target = this;
10543 info.left = left;
10544 info.top = top;
10545 info.right = right;
10546 info.bottom = bottom;
10547
Jeff Browna175a5b2012-02-15 19:18:31 -080010548 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010549 }
10550 }
10551
10552 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010553 * <p>Cause an invalidate to happen on the next animation time step, typically the
10554 * next display frame.</p>
10555 *
10556 * <p>This method can be invoked from outside of the UI thread
10557 * only when this View is attached to a window.</p>
10558 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010559 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010560 */
10561 public void postInvalidateOnAnimation() {
10562 // We try only with the AttachInfo because there's no point in invalidating
10563 // if we are not attached to our window
10564 final AttachInfo attachInfo = mAttachInfo;
10565 if (attachInfo != null) {
10566 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10567 }
10568 }
10569
10570 /**
10571 * <p>Cause an invalidate of the specified area to happen on the next animation
10572 * time step, typically the next display frame.</p>
10573 *
10574 * <p>This method can be invoked from outside of the UI thread
10575 * only when this View is attached to a window.</p>
10576 *
10577 * @param left The left coordinate of the rectangle to invalidate.
10578 * @param top The top coordinate of the rectangle to invalidate.
10579 * @param right The right coordinate of the rectangle to invalidate.
10580 * @param bottom The bottom coordinate of the rectangle to invalidate.
10581 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010582 * @see #invalidate(int, int, int, int)
10583 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010584 */
10585 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10586 // We try only with the AttachInfo because there's no point in invalidating
10587 // if we are not attached to our window
10588 final AttachInfo attachInfo = mAttachInfo;
10589 if (attachInfo != null) {
10590 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10591 info.target = this;
10592 info.left = left;
10593 info.top = top;
10594 info.right = right;
10595 info.bottom = bottom;
10596
10597 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10598 }
10599 }
10600
10601 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010602 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10603 * This event is sent at most once every
10604 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10605 */
10606 private void postSendViewScrolledAccessibilityEventCallback() {
10607 if (mSendViewScrolledAccessibilityEvent == null) {
10608 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10609 }
10610 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10611 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10612 postDelayed(mSendViewScrolledAccessibilityEvent,
10613 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10614 }
10615 }
10616
10617 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 * Called by a parent to request that a child update its values for mScrollX
10619 * and mScrollY if necessary. This will typically be done if the child is
10620 * animating a scroll using a {@link android.widget.Scroller Scroller}
10621 * object.
10622 */
10623 public void computeScroll() {
10624 }
10625
10626 /**
10627 * <p>Indicate whether the horizontal edges are faded when the view is
10628 * scrolled horizontally.</p>
10629 *
10630 * @return true if the horizontal edges should are faded on scroll, false
10631 * otherwise
10632 *
10633 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010634 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010635 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 */
10637 public boolean isHorizontalFadingEdgeEnabled() {
10638 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10639 }
10640
10641 /**
10642 * <p>Define whether the horizontal edges should be faded when this view
10643 * is scrolled horizontally.</p>
10644 *
10645 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10646 * be faded when the view is scrolled
10647 * horizontally
10648 *
10649 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010650 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010651 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 */
10653 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10654 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10655 if (horizontalFadingEdgeEnabled) {
10656 initScrollCache();
10657 }
10658
10659 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10660 }
10661 }
10662
10663 /**
10664 * <p>Indicate whether the vertical edges are faded when the view is
10665 * scrolled horizontally.</p>
10666 *
10667 * @return true if the vertical edges should are faded on scroll, false
10668 * otherwise
10669 *
10670 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010671 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010672 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673 */
10674 public boolean isVerticalFadingEdgeEnabled() {
10675 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10676 }
10677
10678 /**
10679 * <p>Define whether the vertical edges should be faded when this view
10680 * is scrolled vertically.</p>
10681 *
10682 * @param verticalFadingEdgeEnabled true if the vertical edges should
10683 * be faded when the view is scrolled
10684 * vertically
10685 *
10686 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010687 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010688 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 */
10690 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10691 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10692 if (verticalFadingEdgeEnabled) {
10693 initScrollCache();
10694 }
10695
10696 mViewFlags ^= FADING_EDGE_VERTICAL;
10697 }
10698 }
10699
10700 /**
10701 * Returns the strength, or intensity, of the top faded edge. The strength is
10702 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10703 * returns 0.0 or 1.0 but no value in between.
10704 *
10705 * Subclasses should override this method to provide a smoother fade transition
10706 * when scrolling occurs.
10707 *
10708 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10709 */
10710 protected float getTopFadingEdgeStrength() {
10711 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10712 }
10713
10714 /**
10715 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10716 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10717 * returns 0.0 or 1.0 but no value in between.
10718 *
10719 * Subclasses should override this method to provide a smoother fade transition
10720 * when scrolling occurs.
10721 *
10722 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10723 */
10724 protected float getBottomFadingEdgeStrength() {
10725 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10726 computeVerticalScrollRange() ? 1.0f : 0.0f;
10727 }
10728
10729 /**
10730 * Returns the strength, or intensity, of the left faded edge. The strength is
10731 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10732 * returns 0.0 or 1.0 but no value in between.
10733 *
10734 * Subclasses should override this method to provide a smoother fade transition
10735 * when scrolling occurs.
10736 *
10737 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10738 */
10739 protected float getLeftFadingEdgeStrength() {
10740 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10741 }
10742
10743 /**
10744 * Returns the strength, or intensity, of the right faded edge. The strength is
10745 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10746 * returns 0.0 or 1.0 but no value in between.
10747 *
10748 * Subclasses should override this method to provide a smoother fade transition
10749 * when scrolling occurs.
10750 *
10751 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10752 */
10753 protected float getRightFadingEdgeStrength() {
10754 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10755 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10756 }
10757
10758 /**
10759 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10760 * scrollbar is not drawn by default.</p>
10761 *
10762 * @return true if the horizontal scrollbar should be painted, false
10763 * otherwise
10764 *
10765 * @see #setHorizontalScrollBarEnabled(boolean)
10766 */
10767 public boolean isHorizontalScrollBarEnabled() {
10768 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10769 }
10770
10771 /**
10772 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10773 * scrollbar is not drawn by default.</p>
10774 *
10775 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10776 * be painted
10777 *
10778 * @see #isHorizontalScrollBarEnabled()
10779 */
10780 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10781 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10782 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010783 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010784 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 }
10786 }
10787
10788 /**
10789 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10790 * scrollbar is not drawn by default.</p>
10791 *
10792 * @return true if the vertical scrollbar should be painted, false
10793 * otherwise
10794 *
10795 * @see #setVerticalScrollBarEnabled(boolean)
10796 */
10797 public boolean isVerticalScrollBarEnabled() {
10798 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10799 }
10800
10801 /**
10802 * <p>Define whether the vertical scrollbar should be drawn or not. The
10803 * scrollbar is not drawn by default.</p>
10804 *
10805 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10806 * be painted
10807 *
10808 * @see #isVerticalScrollBarEnabled()
10809 */
10810 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10811 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10812 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010813 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010814 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 }
10816 }
10817
Adam Powell20232d02010-12-08 21:08:53 -080010818 /**
10819 * @hide
10820 */
10821 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010822 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823 }
Joe Malin32736f02011-01-19 16:14:20 -080010824
Mike Cleronfe81d382009-09-28 14:22:16 -070010825 /**
10826 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010827 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010828 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010829 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010830 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010831 */
10832 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10833 initScrollCache();
10834 final ScrollabilityCache scrollabilityCache = mScrollCache;
10835 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010836 if (fadeScrollbars) {
10837 scrollabilityCache.state = ScrollabilityCache.OFF;
10838 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010839 scrollabilityCache.state = ScrollabilityCache.ON;
10840 }
10841 }
Joe Malin32736f02011-01-19 16:14:20 -080010842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 /**
Joe Malin32736f02011-01-19 16:14:20 -080010844 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010845 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010846 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010847 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010848 *
10849 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010850 */
10851 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010852 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010853 }
Joe Malin32736f02011-01-19 16:14:20 -080010854
Mike Cleron52f0a642009-09-28 18:21:37 -070010855 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010856 *
10857 * Returns the delay before scrollbars fade.
10858 *
10859 * @return the delay before scrollbars fade
10860 *
10861 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10862 */
10863 public int getScrollBarDefaultDelayBeforeFade() {
10864 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10865 mScrollCache.scrollBarDefaultDelayBeforeFade;
10866 }
10867
10868 /**
10869 * Define the delay before scrollbars fade.
10870 *
10871 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10872 *
10873 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10874 */
10875 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10876 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10877 }
10878
10879 /**
10880 *
10881 * Returns the scrollbar fade duration.
10882 *
10883 * @return the scrollbar fade duration
10884 *
10885 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10886 */
10887 public int getScrollBarFadeDuration() {
10888 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10889 mScrollCache.scrollBarFadeDuration;
10890 }
10891
10892 /**
10893 * Define the scrollbar fade duration.
10894 *
10895 * @param scrollBarFadeDuration - the scrollbar fade duration
10896 *
10897 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10898 */
10899 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10900 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10901 }
10902
10903 /**
10904 *
10905 * Returns the scrollbar size.
10906 *
10907 * @return the scrollbar size
10908 *
10909 * @attr ref android.R.styleable#View_scrollbarSize
10910 */
10911 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010912 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010913 mScrollCache.scrollBarSize;
10914 }
10915
10916 /**
10917 * Define the scrollbar size.
10918 *
10919 * @param scrollBarSize - the scrollbar size
10920 *
10921 * @attr ref android.R.styleable#View_scrollbarSize
10922 */
10923 public void setScrollBarSize(int scrollBarSize) {
10924 getScrollCache().scrollBarSize = scrollBarSize;
10925 }
10926
10927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10929 * inset. When inset, they add to the padding of the view. And the scrollbars
10930 * can be drawn inside the padding area or on the edge of the view. For example,
10931 * if a view has a background drawable and you want to draw the scrollbars
10932 * inside the padding specified by the drawable, you can use
10933 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10934 * appear at the edge of the view, ignoring the padding, then you can use
10935 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10936 * @param style the style of the scrollbars. Should be one of
10937 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10938 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10939 * @see #SCROLLBARS_INSIDE_OVERLAY
10940 * @see #SCROLLBARS_INSIDE_INSET
10941 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10942 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010943 *
10944 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010945 */
10946 public void setScrollBarStyle(int style) {
10947 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10948 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010949 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010950 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 }
10952 }
10953
10954 /**
10955 * <p>Returns the current scrollbar style.</p>
10956 * @return the current scrollbar style
10957 * @see #SCROLLBARS_INSIDE_OVERLAY
10958 * @see #SCROLLBARS_INSIDE_INSET
10959 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10960 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010961 *
10962 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010963 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010964 @ViewDebug.ExportedProperty(mapping = {
10965 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10966 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10967 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10968 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10969 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010970 public int getScrollBarStyle() {
10971 return mViewFlags & SCROLLBARS_STYLE_MASK;
10972 }
10973
10974 /**
10975 * <p>Compute the horizontal range that the horizontal scrollbar
10976 * represents.</p>
10977 *
10978 * <p>The range is expressed in arbitrary units that must be the same as the
10979 * units used by {@link #computeHorizontalScrollExtent()} and
10980 * {@link #computeHorizontalScrollOffset()}.</p>
10981 *
10982 * <p>The default range is the drawing width of this view.</p>
10983 *
10984 * @return the total horizontal range represented by the horizontal
10985 * scrollbar
10986 *
10987 * @see #computeHorizontalScrollExtent()
10988 * @see #computeHorizontalScrollOffset()
10989 * @see android.widget.ScrollBarDrawable
10990 */
10991 protected int computeHorizontalScrollRange() {
10992 return getWidth();
10993 }
10994
10995 /**
10996 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10997 * within the horizontal range. This value is used to compute the position
10998 * of the thumb within the scrollbar's track.</p>
10999 *
11000 * <p>The range is expressed in arbitrary units that must be the same as the
11001 * units used by {@link #computeHorizontalScrollRange()} and
11002 * {@link #computeHorizontalScrollExtent()}.</p>
11003 *
11004 * <p>The default offset is the scroll offset of this view.</p>
11005 *
11006 * @return the horizontal offset of the scrollbar's thumb
11007 *
11008 * @see #computeHorizontalScrollRange()
11009 * @see #computeHorizontalScrollExtent()
11010 * @see android.widget.ScrollBarDrawable
11011 */
11012 protected int computeHorizontalScrollOffset() {
11013 return mScrollX;
11014 }
11015
11016 /**
11017 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11018 * within the horizontal range. This value is used to compute the length
11019 * of the thumb within the scrollbar's track.</p>
11020 *
11021 * <p>The range is expressed in arbitrary units that must be the same as the
11022 * units used by {@link #computeHorizontalScrollRange()} and
11023 * {@link #computeHorizontalScrollOffset()}.</p>
11024 *
11025 * <p>The default extent is the drawing width of this view.</p>
11026 *
11027 * @return the horizontal extent of the scrollbar's thumb
11028 *
11029 * @see #computeHorizontalScrollRange()
11030 * @see #computeHorizontalScrollOffset()
11031 * @see android.widget.ScrollBarDrawable
11032 */
11033 protected int computeHorizontalScrollExtent() {
11034 return getWidth();
11035 }
11036
11037 /**
11038 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11039 *
11040 * <p>The range is expressed in arbitrary units that must be the same as the
11041 * units used by {@link #computeVerticalScrollExtent()} and
11042 * {@link #computeVerticalScrollOffset()}.</p>
11043 *
11044 * @return the total vertical range represented by the vertical scrollbar
11045 *
11046 * <p>The default range is the drawing height of this view.</p>
11047 *
11048 * @see #computeVerticalScrollExtent()
11049 * @see #computeVerticalScrollOffset()
11050 * @see android.widget.ScrollBarDrawable
11051 */
11052 protected int computeVerticalScrollRange() {
11053 return getHeight();
11054 }
11055
11056 /**
11057 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11058 * within the horizontal range. This value is used to compute the position
11059 * of the thumb within the scrollbar's track.</p>
11060 *
11061 * <p>The range is expressed in arbitrary units that must be the same as the
11062 * units used by {@link #computeVerticalScrollRange()} and
11063 * {@link #computeVerticalScrollExtent()}.</p>
11064 *
11065 * <p>The default offset is the scroll offset of this view.</p>
11066 *
11067 * @return the vertical offset of the scrollbar's thumb
11068 *
11069 * @see #computeVerticalScrollRange()
11070 * @see #computeVerticalScrollExtent()
11071 * @see android.widget.ScrollBarDrawable
11072 */
11073 protected int computeVerticalScrollOffset() {
11074 return mScrollY;
11075 }
11076
11077 /**
11078 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11079 * within the vertical range. This value is used to compute the length
11080 * of the thumb within the scrollbar's track.</p>
11081 *
11082 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011083 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011084 * {@link #computeVerticalScrollOffset()}.</p>
11085 *
11086 * <p>The default extent is the drawing height of this view.</p>
11087 *
11088 * @return the vertical extent of the scrollbar's thumb
11089 *
11090 * @see #computeVerticalScrollRange()
11091 * @see #computeVerticalScrollOffset()
11092 * @see android.widget.ScrollBarDrawable
11093 */
11094 protected int computeVerticalScrollExtent() {
11095 return getHeight();
11096 }
11097
11098 /**
Adam Powell69159442011-06-13 17:53:06 -070011099 * Check if this view can be scrolled horizontally in a certain direction.
11100 *
11101 * @param direction Negative to check scrolling left, positive to check scrolling right.
11102 * @return true if this view can be scrolled in the specified direction, false otherwise.
11103 */
11104 public boolean canScrollHorizontally(int direction) {
11105 final int offset = computeHorizontalScrollOffset();
11106 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11107 if (range == 0) return false;
11108 if (direction < 0) {
11109 return offset > 0;
11110 } else {
11111 return offset < range - 1;
11112 }
11113 }
11114
11115 /**
11116 * Check if this view can be scrolled vertically in a certain direction.
11117 *
11118 * @param direction Negative to check scrolling up, positive to check scrolling down.
11119 * @return true if this view can be scrolled in the specified direction, false otherwise.
11120 */
11121 public boolean canScrollVertically(int direction) {
11122 final int offset = computeVerticalScrollOffset();
11123 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11124 if (range == 0) return false;
11125 if (direction < 0) {
11126 return offset > 0;
11127 } else {
11128 return offset < range - 1;
11129 }
11130 }
11131
11132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011133 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11134 * scrollbars are painted only if they have been awakened first.</p>
11135 *
11136 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011137 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011138 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011140 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 // scrollbars are drawn only when the animation is running
11142 final ScrollabilityCache cache = mScrollCache;
11143 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011144
Mike Cleronf116bf82009-09-27 19:14:12 -070011145 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011146
Mike Cleronf116bf82009-09-27 19:14:12 -070011147 if (state == ScrollabilityCache.OFF) {
11148 return;
11149 }
Joe Malin32736f02011-01-19 16:14:20 -080011150
Mike Cleronf116bf82009-09-27 19:14:12 -070011151 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011152
Mike Cleronf116bf82009-09-27 19:14:12 -070011153 if (state == ScrollabilityCache.FADING) {
11154 // We're fading -- get our fade interpolation
11155 if (cache.interpolatorValues == null) {
11156 cache.interpolatorValues = new float[1];
11157 }
Joe Malin32736f02011-01-19 16:14:20 -080011158
Mike Cleronf116bf82009-09-27 19:14:12 -070011159 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011160
Mike Cleronf116bf82009-09-27 19:14:12 -070011161 // Stops the animation if we're done
11162 if (cache.scrollBarInterpolator.timeToValues(values) ==
11163 Interpolator.Result.FREEZE_END) {
11164 cache.state = ScrollabilityCache.OFF;
11165 } else {
11166 cache.scrollBar.setAlpha(Math.round(values[0]));
11167 }
Joe Malin32736f02011-01-19 16:14:20 -080011168
11169 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011170 // drawing. We only want this when we're fading so that
11171 // we prevent excessive redraws
11172 invalidate = true;
11173 } else {
11174 // We're just on -- but we may have been fading before so
11175 // reset alpha
11176 cache.scrollBar.setAlpha(255);
11177 }
11178
Joe Malin32736f02011-01-19 16:14:20 -080011179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011180 final int viewFlags = mViewFlags;
11181
11182 final boolean drawHorizontalScrollBar =
11183 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11184 final boolean drawVerticalScrollBar =
11185 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11186 && !isVerticalScrollBarHidden();
11187
11188 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11189 final int width = mRight - mLeft;
11190 final int height = mBottom - mTop;
11191
11192 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193
Mike Reede8853fc2009-09-04 14:01:48 -040011194 final int scrollX = mScrollX;
11195 final int scrollY = mScrollY;
11196 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11197
Mike Cleronf116bf82009-09-27 19:14:12 -070011198 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011200 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011201 int size = scrollBar.getSize(false);
11202 if (size <= 0) {
11203 size = cache.scrollBarSize;
11204 }
11205
Mike Cleronf116bf82009-09-27 19:14:12 -070011206 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011207 computeHorizontalScrollOffset(),
11208 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011209 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011210 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011211 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011212 left = scrollX + (mPaddingLeft & inside);
11213 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11214 bottom = top + size;
11215 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11216 if (invalidate) {
11217 invalidate(left, top, right, bottom);
11218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011219 }
11220
11221 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011222 int size = scrollBar.getSize(true);
11223 if (size <= 0) {
11224 size = cache.scrollBarSize;
11225 }
11226
Mike Reede8853fc2009-09-04 14:01:48 -040011227 scrollBar.setParameters(computeVerticalScrollRange(),
11228 computeVerticalScrollOffset(),
11229 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011230 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11231 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11232 verticalScrollbarPosition = isLayoutRtl() ?
11233 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11234 }
11235 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011236 default:
Adam Powell20232d02010-12-08 21:08:53 -080011237 case SCROLLBAR_POSITION_RIGHT:
11238 left = scrollX + width - size - (mUserPaddingRight & inside);
11239 break;
11240 case SCROLLBAR_POSITION_LEFT:
11241 left = scrollX + (mUserPaddingLeft & inside);
11242 break;
11243 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011244 top = scrollY + (mPaddingTop & inside);
11245 right = left + size;
11246 bottom = scrollY + height - (mUserPaddingBottom & inside);
11247 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11248 if (invalidate) {
11249 invalidate(left, top, right, bottom);
11250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011251 }
11252 }
11253 }
11254 }
Romain Guy8506ab42009-06-11 17:35:47 -070011255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011257 * 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 -080011258 * FastScroller is visible.
11259 * @return whether to temporarily hide the vertical scrollbar
11260 * @hide
11261 */
11262 protected boolean isVerticalScrollBarHidden() {
11263 return false;
11264 }
11265
11266 /**
11267 * <p>Draw the horizontal scrollbar if
11268 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011270 * @param canvas the canvas on which to draw the scrollbar
11271 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011272 *
11273 * @see #isHorizontalScrollBarEnabled()
11274 * @see #computeHorizontalScrollRange()
11275 * @see #computeHorizontalScrollExtent()
11276 * @see #computeHorizontalScrollOffset()
11277 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011278 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011279 */
Romain Guy8fb95422010-08-17 18:38:51 -070011280 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11281 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011282 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011283 scrollBar.draw(canvas);
11284 }
Mike Reede8853fc2009-09-04 14:01:48 -040011285
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011287 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11288 * returns true.</p>
11289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 * @param canvas the canvas on which to draw the scrollbar
11291 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 *
11293 * @see #isVerticalScrollBarEnabled()
11294 * @see #computeVerticalScrollRange()
11295 * @see #computeVerticalScrollExtent()
11296 * @see #computeVerticalScrollOffset()
11297 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011298 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011299 */
Romain Guy8fb95422010-08-17 18:38:51 -070011300 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11301 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011302 scrollBar.setBounds(l, t, r, b);
11303 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011304 }
11305
11306 /**
11307 * Implement this to do your drawing.
11308 *
11309 * @param canvas the canvas on which the background will be drawn
11310 */
11311 protected void onDraw(Canvas canvas) {
11312 }
11313
11314 /*
11315 * Caller is responsible for calling requestLayout if necessary.
11316 * (This allows addViewInLayout to not request a new layout.)
11317 */
11318 void assignParent(ViewParent parent) {
11319 if (mParent == null) {
11320 mParent = parent;
11321 } else if (parent == null) {
11322 mParent = null;
11323 } else {
11324 throw new RuntimeException("view " + this + " being added, but"
11325 + " it already has a parent");
11326 }
11327 }
11328
11329 /**
11330 * This is called when the view is attached to a window. At this point it
11331 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011332 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11333 * however it may be called any time before the first onDraw -- including
11334 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011335 *
11336 * @see #onDetachedFromWindow()
11337 */
11338 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011339 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 mParent.requestTransparentRegion(this);
11341 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011342
Dianne Hackborn4702a852012-08-17 15:18:29 -070011343 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011344 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011345 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011346 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011347
Chet Haasea9b61ac2010-12-20 07:40:25 -080011348 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011349
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011350 resolveRtlProperties();
Romain Guy2a0f2282012-05-08 14:43:12 -070011351
Svetoslav Ganov42138042012-03-20 11:51:39 -070011352 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011353 if (isFocused()) {
11354 InputMethodManager imm = InputMethodManager.peekInstance();
11355 imm.focusIn(this);
11356 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011357
11358 if (mAttachInfo != null && mDisplayList != null) {
11359 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11360 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011361 }
Cibu Johny86666632010-02-22 13:01:02 -080011362
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011363 void resolveRtlProperties() {
11364 // Order is important here: LayoutDirection MUST be resolved first...
11365 resolveLayoutDirection();
11366 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
11367 resolvePadding();
11368 resolveLayoutParams();
11369 resolveTextDirection();
11370 resolveTextAlignment();
Fabrice Di Meglio0af4b8b2012-06-11 18:30:05 -070011371 resolveDrawables();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011372 }
11373
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011374 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011375 * @see #onScreenStateChanged(int)
11376 */
11377 void dispatchScreenStateChanged(int screenState) {
11378 onScreenStateChanged(screenState);
11379 }
11380
11381 /**
11382 * This method is called whenever the state of the screen this view is
11383 * attached to changes. A state change will usually occurs when the screen
11384 * turns on or off (whether it happens automatically or the user does it
11385 * manually.)
11386 *
11387 * @param screenState The new state of the screen. Can be either
11388 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11389 */
11390 public void onScreenStateChanged(int screenState) {
11391 }
11392
11393 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011394 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11395 */
11396 private boolean hasRtlSupport() {
11397 return mContext.getApplicationInfo().hasRtlSupport();
11398 }
11399
11400 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011401 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11402 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011403 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011404 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011405 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011406 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011407 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011408
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011409 if (hasRtlSupport()) {
11410 // Set resolved depending on layout direction
11411 switch (getLayoutDirection()) {
11412 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011413 // We cannot resolve yet. LTR is by default and let the resolution happen again
11414 // later to get the correct resolved value
11415 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011416
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011417 ViewGroup viewGroup = ((ViewGroup) mParent);
11418
11419 // We cannot resolve yet on the parent too. LTR is by default and let the
11420 // resolution happen again later
11421 if (!viewGroup.canResolveLayoutDirection()) return;
11422
11423 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011424 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011425 }
11426 break;
11427 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011428 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011429 break;
11430 case LAYOUT_DIRECTION_LOCALE:
11431 if(isLayoutDirectionRtl(Locale.getDefault())) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011432 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011433 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011434 break;
11435 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011436 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011437 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011438 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011439
11440 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011441 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011442 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011443 }
11444
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011445 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011446 * Called when layout direction has been resolved.
11447 *
11448 * The default implementation does nothing.
11449 */
11450 public void onResolvedLayoutDirectionChanged() {
11451 }
11452
11453 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011454 * Return if padding has been resolved
11455 */
11456 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011457 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011458 }
11459
11460 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011461 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011462 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011463 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011464 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11465 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11466 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11467 // left / right padding are used if defined. If they are not defined and start / end
11468 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11469 // resolve them as left / right (layout direction is not taken into account).
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011470 if (mUserPaddingLeftInitial == UNDEFINED_PADDING &&
11471 mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011472 mUserPaddingLeft = mUserPaddingStart;
11473 }
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011474 if (mUserPaddingRightInitial == UNDEFINED_PADDING
11475 && mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011476 mUserPaddingRight = mUserPaddingEnd;
11477 }
11478
11479 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11480
11481 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11482 mUserPaddingBottom);
11483 } else {
11484 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11485 // If start / end padding are defined, they will be resolved (hence overriding) to
11486 // left / right or right / left depending on the resolved layout direction.
11487 // If start / end padding are not defined, use the left / right ones.
11488 int resolvedLayoutDirection = getResolvedLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011489 // Set user padding to initial values ...
11490 mUserPaddingLeft = (mUserPaddingLeftInitial == UNDEFINED_PADDING) ?
11491 0 : mUserPaddingLeftInitial;
11492 mUserPaddingRight = (mUserPaddingRightInitial == UNDEFINED_PADDING) ?
11493 0 : mUserPaddingRightInitial;
11494 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011495 switch (resolvedLayoutDirection) {
11496 case LAYOUT_DIRECTION_RTL:
11497 if (mUserPaddingStart != UNDEFINED_PADDING) {
11498 mUserPaddingRight = mUserPaddingStart;
11499 }
11500 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11501 mUserPaddingLeft = mUserPaddingEnd;
11502 }
11503 break;
11504 case LAYOUT_DIRECTION_LTR:
11505 default:
11506 if (mUserPaddingStart != UNDEFINED_PADDING) {
11507 mUserPaddingLeft = mUserPaddingStart;
11508 }
11509 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11510 mUserPaddingRight = mUserPaddingEnd;
11511 }
11512 }
11513
11514 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11515
11516 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11517 mUserPaddingBottom);
11518 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011519 }
11520
Dianne Hackborn4702a852012-08-17 15:18:29 -070011521 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011522 }
11523
11524 /**
11525 * Resolve padding depending on the layout direction. Subclasses that care about
11526 * padding resolution should override this method. The default implementation does
11527 * nothing.
11528 *
11529 * @param layoutDirection the direction of the layout
11530 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011531 * @see {@link #LAYOUT_DIRECTION_LTR}
11532 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011533 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011534 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011535 }
11536
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011537 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011538 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011539 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011540 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011541 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011542 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011543 switch (getLayoutDirection()) {
11544 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011545 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011546 default:
11547 return true;
11548 }
11549 }
11550
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011551 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011552 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11553 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011554 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011555 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011556 // Reset the current resolved bits
Dianne Hackborn4702a852012-08-17 15:18:29 -070011557 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011558 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011559 // Reset also the text direction
11560 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011561 }
11562
11563 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011564 * Called during reset of resolved layout direction.
11565 *
11566 * Subclasses need to override this method to clear cached information that depends on the
11567 * resolved layout direction, or to inform child views that inherit their layout direction.
11568 *
11569 * The default implementation does nothing.
11570 */
11571 public void onResolvedLayoutDirectionReset() {
11572 }
11573
11574 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011575 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011576 *
11577 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011578 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011579 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011580 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011581 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011582 }
11583
11584 /**
11585 * This is called when the view is detached from a window. At this point it
11586 * no longer has a surface for drawing.
11587 *
11588 * @see #onAttachedToWindow()
11589 */
11590 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011591 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011592
Romain Guya440b002010-02-24 15:57:54 -080011593 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011594 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011595 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011596 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011598 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011599
Romain Guya998dff2012-03-23 18:58:36 -070011600 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011601
11602 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011603 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011604 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011605 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011606 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011607 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011608 // Should never happen
11609 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011610 }
11611
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011612 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011613
11614 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011615 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011616 resetAccessibilityStateChanged();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011617 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 }
11619
11620 /**
11621 * @return The number of times this view has been attached to a window
11622 */
11623 protected int getWindowAttachCount() {
11624 return mWindowAttachCount;
11625 }
11626
11627 /**
11628 * Retrieve a unique token identifying the window this view is attached to.
11629 * @return Return the window's token for use in
11630 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11631 */
11632 public IBinder getWindowToken() {
11633 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11634 }
11635
11636 /**
11637 * Retrieve a unique token identifying the top-level "real" window of
11638 * the window that this view is attached to. That is, this is like
11639 * {@link #getWindowToken}, except if the window this view in is a panel
11640 * window (attached to another containing window), then the token of
11641 * the containing window is returned instead.
11642 *
11643 * @return Returns the associated window token, either
11644 * {@link #getWindowToken()} or the containing window's token.
11645 */
11646 public IBinder getApplicationWindowToken() {
11647 AttachInfo ai = mAttachInfo;
11648 if (ai != null) {
11649 IBinder appWindowToken = ai.mPanelParentWindowToken;
11650 if (appWindowToken == null) {
11651 appWindowToken = ai.mWindowToken;
11652 }
11653 return appWindowToken;
11654 }
11655 return null;
11656 }
11657
11658 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011659 * Gets the logical display to which the view's window has been attached.
11660 *
11661 * @return The logical display, or null if the view is not currently attached to a window.
11662 */
11663 public Display getDisplay() {
11664 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11665 }
11666
11667 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 * Retrieve private session object this view hierarchy is using to
11669 * communicate with the window manager.
11670 * @return the session object to communicate with the window manager
11671 */
11672 /*package*/ IWindowSession getWindowSession() {
11673 return mAttachInfo != null ? mAttachInfo.mSession : null;
11674 }
11675
11676 /**
11677 * @param info the {@link android.view.View.AttachInfo} to associated with
11678 * this view
11679 */
11680 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11681 //System.out.println("Attached! " + this);
11682 mAttachInfo = info;
11683 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011684 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011685 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011686 if (mFloatingTreeObserver != null) {
11687 info.mTreeObserver.merge(mFloatingTreeObserver);
11688 mFloatingTreeObserver = null;
11689 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011690 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011691 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011692 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011694 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011696
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011697 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011698 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011699 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011700 if (listeners != null && listeners.size() > 0) {
11701 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11702 // perform the dispatching. The iterator is a safe guard against listeners that
11703 // could mutate the list by calling the various add/remove methods. This prevents
11704 // the array from being modified while we iterate it.
11705 for (OnAttachStateChangeListener listener : listeners) {
11706 listener.onViewAttachedToWindow(this);
11707 }
11708 }
11709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011710 int vis = info.mWindowVisibility;
11711 if (vis != GONE) {
11712 onWindowVisibilityChanged(vis);
11713 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011714 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011715 // If nobody has evaluated the drawable state yet, then do it now.
11716 refreshDrawableState();
11717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011718 }
11719
11720 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721 AttachInfo info = mAttachInfo;
11722 if (info != null) {
11723 int vis = info.mWindowVisibility;
11724 if (vis != GONE) {
11725 onWindowVisibilityChanged(GONE);
11726 }
11727 }
11728
11729 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011730
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011731 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011732 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011733 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011734 if (listeners != null && listeners.size() > 0) {
11735 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11736 // perform the dispatching. The iterator is a safe guard against listeners that
11737 // could mutate the list by calling the various add/remove methods. This prevents
11738 // the array from being modified while we iterate it.
11739 for (OnAttachStateChangeListener listener : listeners) {
11740 listener.onViewDetachedFromWindow(this);
11741 }
11742 }
11743
Dianne Hackborn4702a852012-08-17 15:18:29 -070011744 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011746 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011747 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011749 mAttachInfo = null;
11750 }
11751
11752 /**
11753 * Store this view hierarchy's frozen state into the given container.
11754 *
11755 * @param container The SparseArray in which to save the view's state.
11756 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011757 * @see #restoreHierarchyState(android.util.SparseArray)
11758 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11759 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011760 */
11761 public void saveHierarchyState(SparseArray<Parcelable> container) {
11762 dispatchSaveInstanceState(container);
11763 }
11764
11765 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011766 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11767 * this view and its children. May be overridden to modify how freezing happens to a
11768 * 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 -080011769 *
11770 * @param container The SparseArray in which to save the view's state.
11771 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011772 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11773 * @see #saveHierarchyState(android.util.SparseArray)
11774 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011775 */
11776 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11777 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011778 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011779 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011780 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011781 throw new IllegalStateException(
11782 "Derived class did not call super.onSaveInstanceState()");
11783 }
11784 if (state != null) {
11785 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11786 // + ": " + state);
11787 container.put(mID, state);
11788 }
11789 }
11790 }
11791
11792 /**
11793 * Hook allowing a view to generate a representation of its internal state
11794 * that can later be used to create a new instance with that same state.
11795 * This state should only contain information that is not persistent or can
11796 * not be reconstructed later. For example, you will never store your
11797 * current position on screen because that will be computed again when a
11798 * new instance of the view is placed in its view hierarchy.
11799 * <p>
11800 * Some examples of things you may store here: the current cursor position
11801 * in a text view (but usually not the text itself since that is stored in a
11802 * content provider or other persistent storage), the currently selected
11803 * item in a list view.
11804 *
11805 * @return Returns a Parcelable object containing the view's current dynamic
11806 * state, or null if there is nothing interesting to save. The
11807 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011808 * @see #onRestoreInstanceState(android.os.Parcelable)
11809 * @see #saveHierarchyState(android.util.SparseArray)
11810 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011811 * @see #setSaveEnabled(boolean)
11812 */
11813 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011814 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011815 return BaseSavedState.EMPTY_STATE;
11816 }
11817
11818 /**
11819 * Restore this view hierarchy's frozen state from the given container.
11820 *
11821 * @param container The SparseArray which holds previously frozen states.
11822 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011823 * @see #saveHierarchyState(android.util.SparseArray)
11824 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11825 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011826 */
11827 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11828 dispatchRestoreInstanceState(container);
11829 }
11830
11831 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011832 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11833 * state for this view and its children. May be overridden to modify how restoring
11834 * happens to a view's children; for example, some views may want to not store state
11835 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011836 *
11837 * @param container The SparseArray which holds previously saved state.
11838 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011839 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11840 * @see #restoreHierarchyState(android.util.SparseArray)
11841 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011842 */
11843 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11844 if (mID != NO_ID) {
11845 Parcelable state = container.get(mID);
11846 if (state != null) {
11847 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11848 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011849 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011851 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 throw new IllegalStateException(
11853 "Derived class did not call super.onRestoreInstanceState()");
11854 }
11855 }
11856 }
11857 }
11858
11859 /**
11860 * Hook allowing a view to re-apply a representation of its internal state that had previously
11861 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11862 * null state.
11863 *
11864 * @param state The frozen state that had previously been returned by
11865 * {@link #onSaveInstanceState}.
11866 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011867 * @see #onSaveInstanceState()
11868 * @see #restoreHierarchyState(android.util.SparseArray)
11869 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 */
11871 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011872 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011874 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11875 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011876 + "when two views of different type have the same id in the same hierarchy. "
11877 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011878 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 }
11880 }
11881
11882 /**
11883 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11884 *
11885 * @return the drawing start time in milliseconds
11886 */
11887 public long getDrawingTime() {
11888 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11889 }
11890
11891 /**
11892 * <p>Enables or disables the duplication of the parent's state into this view. When
11893 * duplication is enabled, this view gets its drawable state from its parent rather
11894 * than from its own internal properties.</p>
11895 *
11896 * <p>Note: in the current implementation, setting this property to true after the
11897 * view was added to a ViewGroup might have no effect at all. This property should
11898 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11899 *
11900 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11901 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011902 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011903 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11904 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011905 *
11906 * @param enabled True to enable duplication of the parent's drawable state, false
11907 * to disable it.
11908 *
11909 * @see #getDrawableState()
11910 * @see #isDuplicateParentStateEnabled()
11911 */
11912 public void setDuplicateParentStateEnabled(boolean enabled) {
11913 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11914 }
11915
11916 /**
11917 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11918 *
11919 * @return True if this view's drawable state is duplicated from the parent,
11920 * false otherwise
11921 *
11922 * @see #getDrawableState()
11923 * @see #setDuplicateParentStateEnabled(boolean)
11924 */
11925 public boolean isDuplicateParentStateEnabled() {
11926 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11927 }
11928
11929 /**
Romain Guy171c5922011-01-06 10:04:23 -080011930 * <p>Specifies the type of layer backing this view. The layer can be
11931 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11932 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011933 *
Romain Guy171c5922011-01-06 10:04:23 -080011934 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11935 * instance that controls how the layer is composed on screen. The following
11936 * properties of the paint are taken into account when composing the layer:</p>
11937 * <ul>
11938 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11939 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11940 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11941 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011942 *
Romain Guy171c5922011-01-06 10:04:23 -080011943 * <p>If this view has an alpha value set to < 1.0 by calling
11944 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11945 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11946 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070011947 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011948 *
Romain Guy171c5922011-01-06 10:04:23 -080011949 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11950 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11951 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011952 *
Chet Haased15ebf22012-09-05 11:40:29 -070011953 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080011954 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11955 * {@link #LAYER_TYPE_HARDWARE}
11956 * @param paint The paint used to compose the layer. This argument is optional
11957 * and can be null. It is ignored when the layer type is
11958 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011959 *
11960 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011961 * @see #LAYER_TYPE_NONE
11962 * @see #LAYER_TYPE_SOFTWARE
11963 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011964 * @see #setAlpha(float)
11965 *
Romain Guy171c5922011-01-06 10:04:23 -080011966 * @attr ref android.R.styleable#View_layerType
11967 */
11968 public void setLayerType(int layerType, Paint paint) {
11969 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011970 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011971 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11972 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011973
Romain Guyd6cd5722011-01-17 14:42:41 -080011974 if (layerType == mLayerType) {
11975 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11976 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011977 invalidateParentCaches();
11978 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011979 }
11980 return;
11981 }
Romain Guy171c5922011-01-06 10:04:23 -080011982
11983 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011984 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011985 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011986 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011987 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011988 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011989 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011990 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011991 default:
11992 break;
Romain Guy171c5922011-01-06 10:04:23 -080011993 }
11994
11995 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011996 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11997 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11998 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011999
Romain Guy0fd89bf2011-01-26 15:41:30 -080012000 invalidateParentCaches();
12001 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012002 }
12003
12004 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012005 * Updates the {@link Paint} object used with the current layer (used only if the current
12006 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12007 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12008 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12009 * ensure that the view gets redrawn immediately.
12010 *
12011 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12012 * instance that controls how the layer is composed on screen. The following
12013 * properties of the paint are taken into account when composing the layer:</p>
12014 * <ul>
12015 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12016 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12017 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12018 * </ul>
12019 *
12020 * <p>If this view has an alpha value set to < 1.0 by calling
12021 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12022 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12023 * equivalent to setting a hardware layer on this view and providing a paint with
12024 * the desired alpha value.</p>
12025 *
12026 * @param paint The paint used to compose the layer. This argument is optional
12027 * and can be null. It is ignored when the layer type is
12028 * {@link #LAYER_TYPE_NONE}
12029 *
12030 * @see #setLayerType(int, android.graphics.Paint)
12031 */
12032 public void setLayerPaint(Paint paint) {
12033 int layerType = getLayerType();
12034 if (layerType != LAYER_TYPE_NONE) {
12035 mLayerPaint = paint == null ? new Paint() : paint;
12036 if (layerType == LAYER_TYPE_HARDWARE) {
12037 HardwareLayer layer = getHardwareLayer();
12038 if (layer != null) {
12039 layer.setLayerPaint(paint);
12040 }
12041 invalidateViewProperty(false, false);
12042 } else {
12043 invalidate();
12044 }
12045 }
12046 }
12047
12048 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012049 * Indicates whether this view has a static layer. A view with layer type
12050 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12051 * dynamic.
12052 */
12053 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012054 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012055 }
12056
12057 /**
Romain Guy171c5922011-01-06 10:04:23 -080012058 * Indicates what type of layer is currently associated with this view. By default
12059 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12060 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12061 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012062 *
Romain Guy171c5922011-01-06 10:04:23 -080012063 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12064 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012065 *
12066 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012067 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012068 * @see #LAYER_TYPE_NONE
12069 * @see #LAYER_TYPE_SOFTWARE
12070 * @see #LAYER_TYPE_HARDWARE
12071 */
12072 public int getLayerType() {
12073 return mLayerType;
12074 }
Joe Malin32736f02011-01-19 16:14:20 -080012075
Romain Guy6c319ca2011-01-11 14:29:25 -080012076 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012077 * Forces this view's layer to be created and this view to be rendered
12078 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12079 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012080 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012081 * This method can for instance be used to render a view into its layer before
12082 * starting an animation. If this view is complex, rendering into the layer
12083 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012084 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012085 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012086 *
12087 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012088 */
12089 public void buildLayer() {
12090 if (mLayerType == LAYER_TYPE_NONE) return;
12091
12092 if (mAttachInfo == null) {
12093 throw new IllegalStateException("This view must be attached to a window first");
12094 }
12095
12096 switch (mLayerType) {
12097 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012098 if (mAttachInfo.mHardwareRenderer != null &&
12099 mAttachInfo.mHardwareRenderer.isEnabled() &&
12100 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012101 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012102 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012103 break;
12104 case LAYER_TYPE_SOFTWARE:
12105 buildDrawingCache(true);
12106 break;
12107 }
12108 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012109
Romain Guyf1ae1062011-03-02 18:16:04 -080012110 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012111 * <p>Returns a hardware layer that can be used to draw this view again
12112 * without executing its draw method.</p>
12113 *
12114 * @return A HardwareLayer ready to render, or null if an error occurred.
12115 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012116 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012117 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12118 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012119 return null;
12120 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012121
Romain Guy9c4b79a2011-11-10 19:23:58 -080012122 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012123
12124 final int width = mRight - mLeft;
12125 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012126
Romain Guy6c319ca2011-01-11 14:29:25 -080012127 if (width == 0 || height == 0) {
12128 return null;
12129 }
12130
Dianne Hackborn4702a852012-08-17 15:18:29 -070012131 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012132 if (mHardwareLayer == null) {
12133 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12134 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012135 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012136 } else {
12137 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12138 mHardwareLayer.resize(width, height);
12139 mLocalDirtyRect.set(0, 0, width, height);
12140 }
12141
12142 // This should not be necessary but applications that change
12143 // the parameters of their background drawable without calling
12144 // this.setBackground(Drawable) can leave the view in a bad state
12145 // (for instance isOpaque() returns true, but the background is
12146 // not opaque.)
12147 computeOpaqueFlags();
12148
12149 final boolean opaque = isOpaque();
12150 if (mHardwareLayer.isOpaque() != opaque) {
12151 mHardwareLayer.setOpaque(opaque);
12152 mLocalDirtyRect.set(0, 0, width, height);
12153 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012154 }
12155
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012156 // The layer is not valid if the underlying GPU resources cannot be allocated
12157 if (!mHardwareLayer.isValid()) {
12158 return null;
12159 }
Chet Haased15ebf22012-09-05 11:40:29 -070012160 mHardwareLayer.setLayerPaint(mLayerPaint);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012161
Chet Haasea1cff502012-02-21 13:43:44 -080012162 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012163 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012164 }
12165
12166 return mHardwareLayer;
12167 }
Romain Guy171c5922011-01-06 10:04:23 -080012168
Romain Guy589b0bb2011-10-10 13:57:47 -070012169 /**
12170 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012171 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012172 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012173 *
12174 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012175 * @see #LAYER_TYPE_HARDWARE
12176 */
Romain Guya998dff2012-03-23 18:58:36 -070012177 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012178 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012179 AttachInfo info = mAttachInfo;
12180 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012181 info.mHardwareRenderer.isEnabled() &&
12182 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012183 mHardwareLayer.destroy();
12184 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012185
Romain Guy9c4b79a2011-11-10 19:23:58 -080012186 invalidate(true);
12187 invalidateParentCaches();
12188 }
Romain Guy65b345f2011-07-27 18:51:50 -070012189 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012190 }
Romain Guy65b345f2011-07-27 18:51:50 -070012191 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012192 }
12193
Romain Guy171c5922011-01-06 10:04:23 -080012194 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012195 * Destroys all hardware rendering resources. This method is invoked
12196 * when the system needs to reclaim resources. Upon execution of this
12197 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012198 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012199 * Note: you <strong>must</strong> call
12200 * <code>super.destroyHardwareResources()</code> when overriding
12201 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012202 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012203 * @hide
12204 */
12205 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012206 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012207 }
12208
12209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12211 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12212 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12213 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12214 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12215 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012216 *
Romain Guy171c5922011-01-06 10:04:23 -080012217 * <p>Enabling the drawing cache is similar to
12218 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012219 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12220 * drawing cache has no effect on rendering because the system uses a different mechanism
12221 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12222 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12223 * for information on how to enable software and hardware layers.</p>
12224 *
12225 * <p>This API can be used to manually generate
12226 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12227 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 *
12229 * @param enabled true to enable the drawing cache, false otherwise
12230 *
12231 * @see #isDrawingCacheEnabled()
12232 * @see #getDrawingCache()
12233 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012234 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012235 */
12236 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012237 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012238 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12239 }
12240
12241 /**
12242 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12243 *
12244 * @return true if the drawing cache is enabled
12245 *
12246 * @see #setDrawingCacheEnabled(boolean)
12247 * @see #getDrawingCache()
12248 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012249 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012250 public boolean isDrawingCacheEnabled() {
12251 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12252 }
12253
12254 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012255 * Debugging utility which recursively outputs the dirty state of a view and its
12256 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012257 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012258 * @hide
12259 */
Romain Guy676b1732011-02-14 14:45:33 -080012260 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012261 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012262 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12263 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12264 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12265 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012266 if (clear) {
12267 mPrivateFlags &= clearMask;
12268 }
12269 if (this instanceof ViewGroup) {
12270 ViewGroup parent = (ViewGroup) this;
12271 final int count = parent.getChildCount();
12272 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012273 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012274 child.outputDirtyFlags(indent + " ", clear, clearMask);
12275 }
12276 }
12277 }
12278
12279 /**
12280 * This method is used by ViewGroup to cause its children to restore or recreate their
12281 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12282 * to recreate its own display list, which would happen if it went through the normal
12283 * draw/dispatchDraw mechanisms.
12284 *
12285 * @hide
12286 */
12287 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012288
12289 /**
12290 * A view that is not attached or hardware accelerated cannot create a display list.
12291 * This method checks these conditions and returns the appropriate result.
12292 *
12293 * @return true if view has the ability to create a display list, false otherwise.
12294 *
12295 * @hide
12296 */
12297 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012298 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012299 }
Joe Malin32736f02011-01-19 16:14:20 -080012300
Chet Haasedaf98e92011-01-10 14:10:36 -080012301 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012302 * @return The HardwareRenderer associated with that view or null if hardware rendering
12303 * is not supported or this this has not been attached to a window.
12304 *
12305 * @hide
12306 */
12307 public HardwareRenderer getHardwareRenderer() {
12308 if (mAttachInfo != null) {
12309 return mAttachInfo.mHardwareRenderer;
12310 }
12311 return null;
12312 }
12313
12314 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012315 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12316 * Otherwise, the same display list will be returned (after having been rendered into
12317 * along the way, depending on the invalidation state of the view).
12318 *
12319 * @param displayList The previous version of this displayList, could be null.
12320 * @param isLayer Whether the requester of the display list is a layer. If so,
12321 * the view will avoid creating a layer inside the resulting display list.
12322 * @return A new or reused DisplayList object.
12323 */
12324 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12325 if (!canHaveDisplayList()) {
12326 return null;
12327 }
12328
Dianne Hackborn4702a852012-08-17 15:18:29 -070012329 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012330 displayList == null || !displayList.isValid() ||
12331 (!isLayer && mRecreateDisplayList))) {
12332 // Don't need to recreate the display list, just need to tell our
12333 // children to restore/recreate theirs
12334 if (displayList != null && displayList.isValid() &&
12335 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012336 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12337 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012338 dispatchGetDisplayList();
12339
12340 return displayList;
12341 }
12342
12343 if (!isLayer) {
12344 // If we got here, we're recreating it. Mark it as such to ensure that
12345 // we copy in child display lists into ours in drawChild()
12346 mRecreateDisplayList = true;
12347 }
12348 if (displayList == null) {
12349 final String name = getClass().getSimpleName();
12350 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12351 // If we're creating a new display list, make sure our parent gets invalidated
12352 // since they will need to recreate their display list to account for this
12353 // new child display list.
12354 invalidateParentCaches();
12355 }
12356
12357 boolean caching = false;
12358 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012359 int width = mRight - mLeft;
12360 int height = mBottom - mTop;
12361
12362 try {
12363 canvas.setViewport(width, height);
12364 // The dirty rect should always be null for a display list
12365 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012366 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012367 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012368 if (layerType == LAYER_TYPE_HARDWARE) {
12369 final HardwareLayer layer = getHardwareLayer();
12370 if (layer != null && layer.isValid()) {
12371 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12372 } else {
12373 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12374 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12375 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12376 }
12377 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012378 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012379 buildDrawingCache(true);
12380 Bitmap cache = getDrawingCache(true);
12381 if (cache != null) {
12382 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12383 caching = true;
12384 }
Chet Haasea1cff502012-02-21 13:43:44 -080012385 }
Chet Haasea1cff502012-02-21 13:43:44 -080012386 } else {
12387
12388 computeScroll();
12389
Chet Haasea1cff502012-02-21 13:43:44 -080012390 canvas.translate(-mScrollX, -mScrollY);
12391 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012392 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12393 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012394 }
12395
12396 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012397 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012398 dispatchDraw(canvas);
12399 } else {
12400 draw(canvas);
12401 }
12402 }
12403 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012404 canvas.onPostDraw();
12405
12406 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012407 displayList.setCaching(caching);
12408 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012409 displayList.setLeftTopRightBottom(0, 0, width, height);
12410 } else {
12411 setDisplayListProperties(displayList);
12412 }
12413 }
12414 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012415 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12416 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012417 }
12418
12419 return displayList;
12420 }
12421
12422 /**
12423 * Get the DisplayList for the HardwareLayer
12424 *
12425 * @param layer The HardwareLayer whose DisplayList we want
12426 * @return A DisplayList fopr the specified HardwareLayer
12427 */
12428 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12429 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12430 layer.setDisplayList(displayList);
12431 return displayList;
12432 }
12433
12434
12435 /**
Romain Guyb051e892010-09-28 19:09:36 -070012436 * <p>Returns a display list that can be used to draw this view again
12437 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012438 *
Romain Guyb051e892010-09-28 19:09:36 -070012439 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012440 *
12441 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012442 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012443 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012444 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012445 return mDisplayList;
12446 }
12447
Romain Guy38c2ece2012-05-24 14:20:56 -070012448 private void clearDisplayList() {
12449 if (mDisplayList != null) {
12450 mDisplayList.invalidate();
12451 mDisplayList.clear();
12452 }
12453 }
12454
Romain Guyb051e892010-09-28 19:09:36 -070012455 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012456 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012457 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012458 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012459 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012460 * @see #getDrawingCache(boolean)
12461 */
12462 public Bitmap getDrawingCache() {
12463 return getDrawingCache(false);
12464 }
12465
12466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12468 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12469 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12470 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12471 * request the drawing cache by calling this method and draw it on screen if the
12472 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012473 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012474 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12475 * this method will create a bitmap of the same size as this view. Because this bitmap
12476 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12477 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12478 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12479 * size than the view. This implies that your application must be able to handle this
12480 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012481 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012482 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12483 * the current density of the screen when the application is in compatibility
12484 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012485 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012486 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 * @see #setDrawingCacheEnabled(boolean)
12489 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012490 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012491 * @see #destroyDrawingCache()
12492 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012493 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12495 return null;
12496 }
12497 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012498 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012499 }
Romain Guy02890fd2010-08-06 17:58:44 -070012500 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012501 }
12502
12503 /**
12504 * <p>Frees the resources used by the drawing cache. If you call
12505 * {@link #buildDrawingCache()} manually without calling
12506 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12507 * should cleanup the cache with this method afterwards.</p>
12508 *
12509 * @see #setDrawingCacheEnabled(boolean)
12510 * @see #buildDrawingCache()
12511 * @see #getDrawingCache()
12512 */
12513 public void destroyDrawingCache() {
12514 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012515 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012516 mDrawingCache = null;
12517 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012518 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012519 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012520 mUnscaledDrawingCache = null;
12521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012522 }
12523
12524 /**
12525 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012526 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012527 * view will always be drawn on top of a solid color.
12528 *
12529 * @param color The background color to use for the drawing cache's bitmap
12530 *
12531 * @see #setDrawingCacheEnabled(boolean)
12532 * @see #buildDrawingCache()
12533 * @see #getDrawingCache()
12534 */
12535 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012536 if (color != mDrawingCacheBackgroundColor) {
12537 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012538 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012540 }
12541
12542 /**
12543 * @see #setDrawingCacheBackgroundColor(int)
12544 *
12545 * @return The background color to used for the drawing cache's bitmap
12546 */
12547 public int getDrawingCacheBackgroundColor() {
12548 return mDrawingCacheBackgroundColor;
12549 }
12550
12551 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012552 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012553 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012554 * @see #buildDrawingCache(boolean)
12555 */
12556 public void buildDrawingCache() {
12557 buildDrawingCache(false);
12558 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012559
Romain Guyfbd8f692009-06-26 14:51:58 -070012560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012561 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12562 *
12563 * <p>If you call {@link #buildDrawingCache()} manually without calling
12564 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12565 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012566 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012567 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12568 * this method will create a bitmap of the same size as this view. Because this bitmap
12569 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12570 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12571 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12572 * size than the view. This implies that your application must be able to handle this
12573 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012574 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012575 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12576 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012577 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012578 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012579 *
12580 * @see #getDrawingCache()
12581 * @see #destroyDrawingCache()
12582 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012583 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012584 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012585 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012586 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587
Romain Guy8506ab42009-06-11 17:35:47 -070012588 int width = mRight - mLeft;
12589 int height = mBottom - mTop;
12590
12591 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012592 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012593
Romain Guye1123222009-06-29 14:24:56 -070012594 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012595 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12596 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012598
12599 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012600 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012601 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012602
Chris Craik10e9d1d2012-09-06 14:42:44 -070012603 final int projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12604 final int drawingCacheSize =
12605 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12606 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012607 if (width > 0 && height > 0) {
12608 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12609 + projectedBitmapSize + " bytes, only "
12610 + drawingCacheSize + " available");
12611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012612 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012613 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012614 return;
12615 }
12616
12617 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012618 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619
12620 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012621 Bitmap.Config quality;
12622 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012623 // Never pick ARGB_4444 because it looks awful
12624 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012625 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12626 case DRAWING_CACHE_QUALITY_AUTO:
12627 quality = Bitmap.Config.ARGB_8888;
12628 break;
12629 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012630 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012631 break;
12632 case DRAWING_CACHE_QUALITY_HIGH:
12633 quality = Bitmap.Config.ARGB_8888;
12634 break;
12635 default:
12636 quality = Bitmap.Config.ARGB_8888;
12637 break;
12638 }
12639 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012640 // Optimization for translucent windows
12641 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012642 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643 }
12644
12645 // Try to cleanup memory
12646 if (bitmap != null) bitmap.recycle();
12647
12648 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012649 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12650 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012651 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012652 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012653 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012654 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012655 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012656 }
Adam Powell26153a32010-11-08 15:22:27 -080012657 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 } catch (OutOfMemoryError e) {
12659 // If there is not enough memory to create the bitmap cache, just
12660 // ignore the issue as bitmap caches are not required to draw the
12661 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012662 if (autoScale) {
12663 mDrawingCache = null;
12664 } else {
12665 mUnscaledDrawingCache = null;
12666 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012667 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012668 return;
12669 }
12670
12671 clear = drawingCacheBackgroundColor != 0;
12672 }
12673
12674 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675 if (attachInfo != null) {
12676 canvas = attachInfo.mCanvas;
12677 if (canvas == null) {
12678 canvas = new Canvas();
12679 }
12680 canvas.setBitmap(bitmap);
12681 // Temporarily clobber the cached Canvas in case one of our children
12682 // is also using a drawing cache. Without this, the children would
12683 // steal the canvas by attaching their own bitmap to it and bad, bad
12684 // thing would happen (invisible views, corrupted drawings, etc.)
12685 attachInfo.mCanvas = null;
12686 } else {
12687 // This case should hopefully never or seldom happen
12688 canvas = new Canvas(bitmap);
12689 }
12690
12691 if (clear) {
12692 bitmap.eraseColor(drawingCacheBackgroundColor);
12693 }
12694
12695 computeScroll();
12696 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012697
Romain Guye1123222009-06-29 14:24:56 -070012698 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012699 final float scale = attachInfo.mApplicationScale;
12700 canvas.scale(scale, scale);
12701 }
Joe Malin32736f02011-01-19 16:14:20 -080012702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012703 canvas.translate(-mScrollX, -mScrollY);
12704
Dianne Hackborn4702a852012-08-17 15:18:29 -070012705 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012706 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12707 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012708 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710
12711 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012712 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12713 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012714 dispatchDraw(canvas);
12715 } else {
12716 draw(canvas);
12717 }
12718
12719 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012720 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721
12722 if (attachInfo != null) {
12723 // Restore the cached Canvas for our siblings
12724 attachInfo.mCanvas = canvas;
12725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012726 }
12727 }
12728
12729 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012730 * Create a snapshot of the view into a bitmap. We should probably make
12731 * some form of this public, but should think about the API.
12732 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012733 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012734 int width = mRight - mLeft;
12735 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012736
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012737 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012738 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012739 width = (int) ((width * scale) + 0.5f);
12740 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012741
Dianne Hackborndde331c2012-08-03 14:01:57 -070012742 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12743 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012744 if (bitmap == null) {
12745 throw new OutOfMemoryError();
12746 }
12747
Romain Guyc529d8d2011-09-06 15:01:39 -070012748 Resources resources = getResources();
12749 if (resources != null) {
12750 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12751 }
Joe Malin32736f02011-01-19 16:14:20 -080012752
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012753 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012754 if (attachInfo != null) {
12755 canvas = attachInfo.mCanvas;
12756 if (canvas == null) {
12757 canvas = new Canvas();
12758 }
12759 canvas.setBitmap(bitmap);
12760 // Temporarily clobber the cached Canvas in case one of our children
12761 // is also using a drawing cache. Without this, the children would
12762 // steal the canvas by attaching their own bitmap to it and bad, bad
12763 // things would happen (invisible views, corrupted drawings, etc.)
12764 attachInfo.mCanvas = null;
12765 } else {
12766 // This case should hopefully never or seldom happen
12767 canvas = new Canvas(bitmap);
12768 }
12769
Romain Guy5bcdff42009-05-14 21:27:18 -070012770 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012771 bitmap.eraseColor(backgroundColor);
12772 }
12773
12774 computeScroll();
12775 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012776 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012777 canvas.translate(-mScrollX, -mScrollY);
12778
Romain Guy5bcdff42009-05-14 21:27:18 -070012779 // Temporarily remove the dirty mask
12780 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012781 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012782
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012783 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012784 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012785 dispatchDraw(canvas);
12786 } else {
12787 draw(canvas);
12788 }
12789
Romain Guy5bcdff42009-05-14 21:27:18 -070012790 mPrivateFlags = flags;
12791
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012792 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012793 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012794
12795 if (attachInfo != null) {
12796 // Restore the cached Canvas for our siblings
12797 attachInfo.mCanvas = canvas;
12798 }
Romain Guy8506ab42009-06-11 17:35:47 -070012799
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012800 return bitmap;
12801 }
12802
12803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012804 * Indicates whether this View is currently in edit mode. A View is usually
12805 * in edit mode when displayed within a developer tool. For instance, if
12806 * this View is being drawn by a visual user interface builder, this method
12807 * should return true.
12808 *
12809 * Subclasses should check the return value of this method to provide
12810 * different behaviors if their normal behavior might interfere with the
12811 * host environment. For instance: the class spawns a thread in its
12812 * constructor, the drawing code relies on device-specific features, etc.
12813 *
12814 * This method is usually checked in the drawing code of custom widgets.
12815 *
12816 * @return True if this View is in edit mode, false otherwise.
12817 */
12818 public boolean isInEditMode() {
12819 return false;
12820 }
12821
12822 /**
12823 * If the View draws content inside its padding and enables fading edges,
12824 * it needs to support padding offsets. Padding offsets are added to the
12825 * fading edges to extend the length of the fade so that it covers pixels
12826 * drawn inside the padding.
12827 *
12828 * Subclasses of this class should override this method if they need
12829 * to draw content inside the padding.
12830 *
12831 * @return True if padding offset must be applied, false otherwise.
12832 *
12833 * @see #getLeftPaddingOffset()
12834 * @see #getRightPaddingOffset()
12835 * @see #getTopPaddingOffset()
12836 * @see #getBottomPaddingOffset()
12837 *
12838 * @since CURRENT
12839 */
12840 protected boolean isPaddingOffsetRequired() {
12841 return false;
12842 }
12843
12844 /**
12845 * Amount by which to extend the left fading region. Called only when
12846 * {@link #isPaddingOffsetRequired()} returns true.
12847 *
12848 * @return The left padding offset in pixels.
12849 *
12850 * @see #isPaddingOffsetRequired()
12851 *
12852 * @since CURRENT
12853 */
12854 protected int getLeftPaddingOffset() {
12855 return 0;
12856 }
12857
12858 /**
12859 * Amount by which to extend the right fading region. Called only when
12860 * {@link #isPaddingOffsetRequired()} returns true.
12861 *
12862 * @return The right padding offset in pixels.
12863 *
12864 * @see #isPaddingOffsetRequired()
12865 *
12866 * @since CURRENT
12867 */
12868 protected int getRightPaddingOffset() {
12869 return 0;
12870 }
12871
12872 /**
12873 * Amount by which to extend the top fading region. Called only when
12874 * {@link #isPaddingOffsetRequired()} returns true.
12875 *
12876 * @return The top padding offset in pixels.
12877 *
12878 * @see #isPaddingOffsetRequired()
12879 *
12880 * @since CURRENT
12881 */
12882 protected int getTopPaddingOffset() {
12883 return 0;
12884 }
12885
12886 /**
12887 * Amount by which to extend the bottom fading region. Called only when
12888 * {@link #isPaddingOffsetRequired()} returns true.
12889 *
12890 * @return The bottom padding offset in pixels.
12891 *
12892 * @see #isPaddingOffsetRequired()
12893 *
12894 * @since CURRENT
12895 */
12896 protected int getBottomPaddingOffset() {
12897 return 0;
12898 }
12899
12900 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012901 * @hide
12902 * @param offsetRequired
12903 */
12904 protected int getFadeTop(boolean offsetRequired) {
12905 int top = mPaddingTop;
12906 if (offsetRequired) top += getTopPaddingOffset();
12907 return top;
12908 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012909
Romain Guyf2fc4602011-07-19 15:20:03 -070012910 /**
12911 * @hide
12912 * @param offsetRequired
12913 */
12914 protected int getFadeHeight(boolean offsetRequired) {
12915 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012916 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012917 return mBottom - mTop - mPaddingBottom - padding;
12918 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012919
Romain Guyf2fc4602011-07-19 15:20:03 -070012920 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012921 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012922 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012923 *
Romain Guy2bffd262010-09-12 17:40:02 -070012924 * <p>Even if this method returns true, it does not mean that every call
12925 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12926 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012927 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012928 * window is hardware accelerated,
12929 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12930 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012931 *
Romain Guy2bffd262010-09-12 17:40:02 -070012932 * @return True if the view is attached to a window and the window is
12933 * hardware accelerated; false in any other case.
12934 */
12935 public boolean isHardwareAccelerated() {
12936 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12937 }
Joe Malin32736f02011-01-19 16:14:20 -080012938
Romain Guy2bffd262010-09-12 17:40:02 -070012939 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012940 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12941 * case of an active Animation being run on the view.
12942 */
12943 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12944 Animation a, boolean scalingRequired) {
12945 Transformation invalidationTransform;
12946 final int flags = parent.mGroupFlags;
12947 final boolean initialized = a.isInitialized();
12948 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012949 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012950 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012951 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012952 onAnimationStart();
12953 }
12954
12955 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12956 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12957 if (parent.mInvalidationTransformation == null) {
12958 parent.mInvalidationTransformation = new Transformation();
12959 }
12960 invalidationTransform = parent.mInvalidationTransformation;
12961 a.getTransformation(drawingTime, invalidationTransform, 1f);
12962 } else {
12963 invalidationTransform = parent.mChildTransformation;
12964 }
Romain Guy393a52c2012-05-22 20:21:08 -070012965
Chet Haasebcca79a2012-02-14 08:45:14 -080012966 if (more) {
12967 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070012968 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
12969 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
12970 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
12971 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012972 // The child need to draw an animation, potentially offscreen, so
12973 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012974 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012975 parent.invalidate(mLeft, mTop, mRight, mBottom);
12976 }
12977 } else {
12978 if (parent.mInvalidateRegion == null) {
12979 parent.mInvalidateRegion = new RectF();
12980 }
12981 final RectF region = parent.mInvalidateRegion;
12982 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12983 invalidationTransform);
12984
12985 // The child need to draw an animation, potentially offscreen, so
12986 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012987 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012988
12989 final int left = mLeft + (int) region.left;
12990 final int top = mTop + (int) region.top;
12991 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12992 top + (int) (region.height() + .5f));
12993 }
12994 }
12995 return more;
12996 }
12997
Chet Haasea1cff502012-02-21 13:43:44 -080012998 /**
12999 * This method is called by getDisplayList() when a display list is created or re-rendered.
13000 * It sets or resets the current value of all properties on that display list (resetting is
13001 * necessary when a display list is being re-created, because we need to make sure that
13002 * previously-set transform values
13003 */
13004 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013005 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013006 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013007 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013008 if (mParent instanceof ViewGroup) {
13009 displayList.setClipChildren(
13010 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13011 }
Chet Haase9420abd2012-03-29 16:28:32 -070013012 float alpha = 1;
13013 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13014 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13015 ViewGroup parentVG = (ViewGroup) mParent;
13016 final boolean hasTransform =
13017 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13018 if (hasTransform) {
13019 Transformation transform = parentVG.mChildTransformation;
13020 final int transformType = parentVG.mChildTransformation.getTransformationType();
13021 if (transformType != Transformation.TYPE_IDENTITY) {
13022 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13023 alpha = transform.getAlpha();
13024 }
13025 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13026 displayList.setStaticMatrix(transform.getMatrix());
13027 }
13028 }
13029 }
Chet Haasea1cff502012-02-21 13:43:44 -080013030 }
13031 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013032 alpha *= mTransformationInfo.mAlpha;
13033 if (alpha < 1) {
13034 final int multipliedAlpha = (int) (255 * alpha);
13035 if (onSetAlpha(multipliedAlpha)) {
13036 alpha = 1;
13037 }
13038 }
13039 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013040 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13041 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13042 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13043 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013044 if (mTransformationInfo.mCamera == null) {
13045 mTransformationInfo.mCamera = new Camera();
13046 mTransformationInfo.matrix3D = new Matrix();
13047 }
13048 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013049 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013050 displayList.setPivotX(getPivotX());
13051 displayList.setPivotY(getPivotY());
13052 }
Chet Haase9420abd2012-03-29 16:28:32 -070013053 } else if (alpha < 1) {
13054 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013055 }
13056 }
13057 }
13058
Chet Haasebcca79a2012-02-14 08:45:14 -080013059 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013060 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13061 * This draw() method is an implementation detail and is not intended to be overridden or
13062 * to be called from anywhere else other than ViewGroup.drawChild().
13063 */
13064 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013065 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013066 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013067 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013068 final int flags = parent.mGroupFlags;
13069
Chet Haasea1cff502012-02-21 13:43:44 -080013070 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013071 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013072 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013073 }
13074
13075 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013076 boolean concatMatrix = false;
13077
13078 boolean scalingRequired = false;
13079 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013080 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013081
13082 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013083 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13084 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013085 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013086 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013087 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13088 } else {
13089 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13090 }
13091
Chet Haasebcca79a2012-02-14 08:45:14 -080013092 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013093 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013094 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013095 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013096 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013097 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013098 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013099 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013100 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013101 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013102 mDisplayList != null) {
13103 // No longer animating: clear out old animation matrix
13104 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013105 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013106 }
13107 if (!useDisplayListProperties &&
13108 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13109 final boolean hasTransform =
13110 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13111 if (hasTransform) {
13112 final int transformType = parent.mChildTransformation.getTransformationType();
13113 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13114 parent.mChildTransformation : null;
13115 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13116 }
Chet Haase64a48c12012-02-13 16:33:29 -080013117 }
13118 }
13119
13120 concatMatrix |= !childHasIdentityMatrix;
13121
13122 // Sets the flag as early as possible to allow draw() implementations
13123 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013124 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013125
Romain Guyfbb43212012-08-30 15:19:27 -070013126 if (!concatMatrix &&
13127 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13128 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013129 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013130 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13131 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013132 return more;
13133 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013134 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013135
13136 if (hardwareAccelerated) {
13137 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13138 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013139 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13140 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013141 }
13142
Chet Haase64a48c12012-02-13 16:33:29 -080013143 DisplayList displayList = null;
13144 Bitmap cache = null;
13145 boolean hasDisplayList = false;
13146 if (caching) {
13147 if (!hardwareAccelerated) {
13148 if (layerType != LAYER_TYPE_NONE) {
13149 layerType = LAYER_TYPE_SOFTWARE;
13150 buildDrawingCache(true);
13151 }
13152 cache = getDrawingCache(true);
13153 } else {
13154 switch (layerType) {
13155 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013156 if (useDisplayListProperties) {
13157 hasDisplayList = canHaveDisplayList();
13158 } else {
13159 buildDrawingCache(true);
13160 cache = getDrawingCache(true);
13161 }
Chet Haase64a48c12012-02-13 16:33:29 -080013162 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013163 case LAYER_TYPE_HARDWARE:
13164 if (useDisplayListProperties) {
13165 hasDisplayList = canHaveDisplayList();
13166 }
13167 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013168 case LAYER_TYPE_NONE:
13169 // Delay getting the display list until animation-driven alpha values are
13170 // set up and possibly passed on to the view
13171 hasDisplayList = canHaveDisplayList();
13172 break;
13173 }
13174 }
13175 }
Chet Haasea1cff502012-02-21 13:43:44 -080013176 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013177 if (useDisplayListProperties) {
13178 displayList = getDisplayList();
13179 if (!displayList.isValid()) {
13180 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13181 // to getDisplayList(), the display list will be marked invalid and we should not
13182 // try to use it again.
13183 displayList = null;
13184 hasDisplayList = false;
13185 useDisplayListProperties = false;
13186 }
13187 }
Chet Haase64a48c12012-02-13 16:33:29 -080013188
Chet Haase526057b2012-07-12 17:50:41 -070013189 int sx = 0;
13190 int sy = 0;
13191 if (!hasDisplayList) {
13192 computeScroll();
13193 sx = mScrollX;
13194 sy = mScrollY;
13195 }
13196
Chet Haase64a48c12012-02-13 16:33:29 -080013197 final boolean hasNoCache = cache == null || hasDisplayList;
13198 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13199 layerType != LAYER_TYPE_HARDWARE;
13200
Chet Haasea1cff502012-02-21 13:43:44 -080013201 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013202 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013203 restoreTo = canvas.save();
13204 }
Chet Haase64a48c12012-02-13 16:33:29 -080013205 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013206 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013207 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013208 if (!useDisplayListProperties) {
13209 canvas.translate(mLeft, mTop);
13210 }
Chet Haase64a48c12012-02-13 16:33:29 -080013211 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013212 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013213 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013214 restoreTo = canvas.save();
13215 }
Chet Haase64a48c12012-02-13 16:33:29 -080013216 // mAttachInfo cannot be null, otherwise scalingRequired == false
13217 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13218 canvas.scale(scale, scale);
13219 }
13220 }
13221
Chet Haasea1cff502012-02-21 13:43:44 -080013222 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013223 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013224 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013225 if (transformToApply != null || !childHasIdentityMatrix) {
13226 int transX = 0;
13227 int transY = 0;
13228
13229 if (offsetForScroll) {
13230 transX = -sx;
13231 transY = -sy;
13232 }
13233
13234 if (transformToApply != null) {
13235 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013236 if (useDisplayListProperties) {
13237 displayList.setAnimationMatrix(transformToApply.getMatrix());
13238 } else {
13239 // Undo the scroll translation, apply the transformation matrix,
13240 // then redo the scroll translate to get the correct result.
13241 canvas.translate(-transX, -transY);
13242 canvas.concat(transformToApply.getMatrix());
13243 canvas.translate(transX, transY);
13244 }
Chet Haasea1cff502012-02-21 13:43:44 -080013245 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013246 }
13247
13248 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013249 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013250 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013251 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013252 }
13253 }
13254
Chet Haasea1cff502012-02-21 13:43:44 -080013255 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013256 canvas.translate(-transX, -transY);
13257 canvas.concat(getMatrix());
13258 canvas.translate(transX, transY);
13259 }
13260 }
13261
Chet Haase21433372012-06-05 07:54:09 -070013262 // Deal with alpha if it is or used to be <1
13263 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013264 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013265 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013266 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013267 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013268 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013269 }
Chet Haasea1cff502012-02-21 13:43:44 -080013270 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013271 if (hasNoCache) {
13272 final int multipliedAlpha = (int) (255 * alpha);
13273 if (!onSetAlpha(multipliedAlpha)) {
13274 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013275 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013276 layerType != LAYER_TYPE_NONE) {
13277 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13278 }
Chet Haase9420abd2012-03-29 16:28:32 -070013279 if (useDisplayListProperties) {
13280 displayList.setAlpha(alpha * getAlpha());
13281 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013282 final int scrollX = hasDisplayList ? 0 : sx;
13283 final int scrollY = hasDisplayList ? 0 : sy;
13284 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13285 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013286 }
13287 } else {
13288 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013289 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013290 }
13291 }
13292 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013293 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013294 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013295 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013296 }
13297
Chet Haasea1cff502012-02-21 13:43:44 -080013298 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13299 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013300 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013301 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013302 } else {
13303 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013304 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013305 } else {
13306 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13307 }
13308 }
13309 }
13310
Chet Haase9420abd2012-03-29 16:28:32 -070013311 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013312 displayList = getDisplayList();
13313 if (!displayList.isValid()) {
13314 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13315 // to getDisplayList(), the display list will be marked invalid and we should not
13316 // try to use it again.
13317 displayList = null;
13318 hasDisplayList = false;
13319 }
13320 }
13321
13322 if (hasNoCache) {
13323 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013324 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013325 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013326 if (layer != null && layer.isValid()) {
13327 mLayerPaint.setAlpha((int) (alpha * 255));
13328 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13329 layerRendered = true;
13330 } else {
13331 final int scrollX = hasDisplayList ? 0 : sx;
13332 final int scrollY = hasDisplayList ? 0 : sy;
13333 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013334 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013335 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13336 }
13337 }
13338
13339 if (!layerRendered) {
13340 if (!hasDisplayList) {
13341 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013342 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13343 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013344 dispatchDraw(canvas);
13345 } else {
13346 draw(canvas);
13347 }
13348 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013349 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013350 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013351 }
13352 }
13353 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013354 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013355 Paint cachePaint;
13356
13357 if (layerType == LAYER_TYPE_NONE) {
13358 cachePaint = parent.mCachePaint;
13359 if (cachePaint == null) {
13360 cachePaint = new Paint();
13361 cachePaint.setDither(false);
13362 parent.mCachePaint = cachePaint;
13363 }
Chet Haase9420abd2012-03-29 16:28:32 -070013364 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013365 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013366 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13367 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013368 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013369 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013370 }
13371 } else {
13372 cachePaint = mLayerPaint;
13373 cachePaint.setAlpha((int) (alpha * 255));
13374 }
13375 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13376 }
13377
Chet Haasea1cff502012-02-21 13:43:44 -080013378 if (restoreTo >= 0) {
13379 canvas.restoreToCount(restoreTo);
13380 }
Chet Haase64a48c12012-02-13 16:33:29 -080013381
13382 if (a != null && !more) {
13383 if (!hardwareAccelerated && !a.getFillAfter()) {
13384 onSetAlpha(255);
13385 }
13386 parent.finishAnimatingView(this, a);
13387 }
13388
13389 if (more && hardwareAccelerated) {
13390 // invalidation is the trigger to recreate display lists, so if we're using
13391 // display lists to render, force an invalidate to allow the animation to
13392 // continue drawing another frame
13393 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013394 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013395 // alpha animations should cause the child to recreate its display list
13396 invalidate(true);
13397 }
13398 }
13399
13400 mRecreateDisplayList = false;
13401
13402 return more;
13403 }
13404
13405 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013406 * Manually render this view (and all of its children) to the given Canvas.
13407 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013408 * called. When implementing a view, implement
13409 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13410 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 *
13412 * @param canvas The Canvas to which the View is rendered.
13413 */
13414 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013415 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013416 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013417 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013418 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013420 /*
13421 * Draw traversal performs several drawing steps which must be executed
13422 * in the appropriate order:
13423 *
13424 * 1. Draw the background
13425 * 2. If necessary, save the canvas' layers to prepare for fading
13426 * 3. Draw view's content
13427 * 4. Draw children
13428 * 5. If necessary, draw the fading edges and restore layers
13429 * 6. Draw decorations (scrollbars for instance)
13430 */
13431
13432 // Step 1, draw the background, if needed
13433 int saveCount;
13434
Romain Guy24443ea2009-05-11 11:56:30 -070013435 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013436 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013437 if (background != null) {
13438 final int scrollX = mScrollX;
13439 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013440
Romain Guy24443ea2009-05-11 11:56:30 -070013441 if (mBackgroundSizeChanged) {
13442 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13443 mBackgroundSizeChanged = false;
13444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013445
Romain Guy24443ea2009-05-11 11:56:30 -070013446 if ((scrollX | scrollY) == 0) {
13447 background.draw(canvas);
13448 } else {
13449 canvas.translate(scrollX, scrollY);
13450 background.draw(canvas);
13451 canvas.translate(-scrollX, -scrollY);
13452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013453 }
13454 }
13455
13456 // skip step 2 & 5 if possible (common case)
13457 final int viewFlags = mViewFlags;
13458 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13459 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13460 if (!verticalEdges && !horizontalEdges) {
13461 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013462 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013463
13464 // Step 4, draw the children
13465 dispatchDraw(canvas);
13466
13467 // Step 6, draw decorations (scrollbars)
13468 onDrawScrollBars(canvas);
13469
13470 // we're done...
13471 return;
13472 }
13473
13474 /*
13475 * Here we do the full fledged routine...
13476 * (this is an uncommon case where speed matters less,
13477 * this is why we repeat some of the tests that have been
13478 * done above)
13479 */
13480
13481 boolean drawTop = false;
13482 boolean drawBottom = false;
13483 boolean drawLeft = false;
13484 boolean drawRight = false;
13485
13486 float topFadeStrength = 0.0f;
13487 float bottomFadeStrength = 0.0f;
13488 float leftFadeStrength = 0.0f;
13489 float rightFadeStrength = 0.0f;
13490
13491 // Step 2, save the canvas' layers
13492 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493
13494 final boolean offsetRequired = isPaddingOffsetRequired();
13495 if (offsetRequired) {
13496 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013497 }
13498
13499 int left = mScrollX + paddingLeft;
13500 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013501 int top = mScrollY + getFadeTop(offsetRequired);
13502 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013503
13504 if (offsetRequired) {
13505 right += getRightPaddingOffset();
13506 bottom += getBottomPaddingOffset();
13507 }
13508
13509 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013510 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013511 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512
13513 // clip the fade length if top and bottom fades overlap
13514 // overlapping fades produce odd-looking artifacts
13515 if (verticalEdges && (top + length > bottom - length)) {
13516 length = (bottom - top) / 2;
13517 }
13518
13519 // also clip horizontal fades if necessary
13520 if (horizontalEdges && (left + length > right - length)) {
13521 length = (right - left) / 2;
13522 }
13523
13524 if (verticalEdges) {
13525 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013526 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013528 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013529 }
13530
13531 if (horizontalEdges) {
13532 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013533 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013534 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013535 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013536 }
13537
13538 saveCount = canvas.getSaveCount();
13539
13540 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013541 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13543
13544 if (drawTop) {
13545 canvas.saveLayer(left, top, right, top + length, null, flags);
13546 }
13547
13548 if (drawBottom) {
13549 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13550 }
13551
13552 if (drawLeft) {
13553 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13554 }
13555
13556 if (drawRight) {
13557 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13558 }
13559 } else {
13560 scrollabilityCache.setFadeColor(solidColor);
13561 }
13562
13563 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013564 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013565
13566 // Step 4, draw the children
13567 dispatchDraw(canvas);
13568
13569 // Step 5, draw the fade effect and restore layers
13570 final Paint p = scrollabilityCache.paint;
13571 final Matrix matrix = scrollabilityCache.matrix;
13572 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013573
13574 if (drawTop) {
13575 matrix.setScale(1, fadeHeight * topFadeStrength);
13576 matrix.postTranslate(left, top);
13577 fade.setLocalMatrix(matrix);
13578 canvas.drawRect(left, top, right, top + length, p);
13579 }
13580
13581 if (drawBottom) {
13582 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13583 matrix.postRotate(180);
13584 matrix.postTranslate(left, bottom);
13585 fade.setLocalMatrix(matrix);
13586 canvas.drawRect(left, bottom - length, right, bottom, p);
13587 }
13588
13589 if (drawLeft) {
13590 matrix.setScale(1, fadeHeight * leftFadeStrength);
13591 matrix.postRotate(-90);
13592 matrix.postTranslate(left, top);
13593 fade.setLocalMatrix(matrix);
13594 canvas.drawRect(left, top, left + length, bottom, p);
13595 }
13596
13597 if (drawRight) {
13598 matrix.setScale(1, fadeHeight * rightFadeStrength);
13599 matrix.postRotate(90);
13600 matrix.postTranslate(right, top);
13601 fade.setLocalMatrix(matrix);
13602 canvas.drawRect(right - length, top, right, bottom, p);
13603 }
13604
13605 canvas.restoreToCount(saveCount);
13606
13607 // Step 6, draw decorations (scrollbars)
13608 onDrawScrollBars(canvas);
13609 }
13610
13611 /**
13612 * Override this if your view is known to always be drawn on top of a solid color background,
13613 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13614 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13615 * should be set to 0xFF.
13616 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013617 * @see #setVerticalFadingEdgeEnabled(boolean)
13618 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013619 *
13620 * @return The known solid color background for this view, or 0 if the color may vary
13621 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013622 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013623 public int getSolidColor() {
13624 return 0;
13625 }
13626
13627 /**
13628 * Build a human readable string representation of the specified view flags.
13629 *
13630 * @param flags the view flags to convert to a string
13631 * @return a String representing the supplied flags
13632 */
13633 private static String printFlags(int flags) {
13634 String output = "";
13635 int numFlags = 0;
13636 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13637 output += "TAKES_FOCUS";
13638 numFlags++;
13639 }
13640
13641 switch (flags & VISIBILITY_MASK) {
13642 case INVISIBLE:
13643 if (numFlags > 0) {
13644 output += " ";
13645 }
13646 output += "INVISIBLE";
13647 // USELESS HERE numFlags++;
13648 break;
13649 case GONE:
13650 if (numFlags > 0) {
13651 output += " ";
13652 }
13653 output += "GONE";
13654 // USELESS HERE numFlags++;
13655 break;
13656 default:
13657 break;
13658 }
13659 return output;
13660 }
13661
13662 /**
13663 * Build a human readable string representation of the specified private
13664 * view flags.
13665 *
13666 * @param privateFlags the private view flags to convert to a string
13667 * @return a String representing the supplied flags
13668 */
13669 private static String printPrivateFlags(int privateFlags) {
13670 String output = "";
13671 int numFlags = 0;
13672
Dianne Hackborn4702a852012-08-17 15:18:29 -070013673 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674 output += "WANTS_FOCUS";
13675 numFlags++;
13676 }
13677
Dianne Hackborn4702a852012-08-17 15:18:29 -070013678 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013679 if (numFlags > 0) {
13680 output += " ";
13681 }
13682 output += "FOCUSED";
13683 numFlags++;
13684 }
13685
Dianne Hackborn4702a852012-08-17 15:18:29 -070013686 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013687 if (numFlags > 0) {
13688 output += " ";
13689 }
13690 output += "SELECTED";
13691 numFlags++;
13692 }
13693
Dianne Hackborn4702a852012-08-17 15:18:29 -070013694 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013695 if (numFlags > 0) {
13696 output += " ";
13697 }
13698 output += "IS_ROOT_NAMESPACE";
13699 numFlags++;
13700 }
13701
Dianne Hackborn4702a852012-08-17 15:18:29 -070013702 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013703 if (numFlags > 0) {
13704 output += " ";
13705 }
13706 output += "HAS_BOUNDS";
13707 numFlags++;
13708 }
13709
Dianne Hackborn4702a852012-08-17 15:18:29 -070013710 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013711 if (numFlags > 0) {
13712 output += " ";
13713 }
13714 output += "DRAWN";
13715 // USELESS HERE numFlags++;
13716 }
13717 return output;
13718 }
13719
13720 /**
13721 * <p>Indicates whether or not this view's layout will be requested during
13722 * the next hierarchy layout pass.</p>
13723 *
13724 * @return true if the layout will be forced during next layout pass
13725 */
13726 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013727 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013728 }
13729
13730 /**
13731 * Assign a size and position to a view and all of its
13732 * descendants
13733 *
13734 * <p>This is the second phase of the layout mechanism.
13735 * (The first is measuring). In this phase, each parent calls
13736 * layout on all of its children to position them.
13737 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013738 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 *
Chet Haase9c087442011-01-12 16:20:16 -080013740 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741 * Derived classes with children should override
13742 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013743 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013744 *
13745 * @param l Left position, relative to parent
13746 * @param t Top position, relative to parent
13747 * @param r Right position, relative to parent
13748 * @param b Bottom position, relative to parent
13749 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013750 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013751 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013752 int oldL = mLeft;
13753 int oldT = mTop;
13754 int oldB = mBottom;
13755 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013756 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013757 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013759 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013760
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013761 ListenerInfo li = mListenerInfo;
13762 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013763 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013764 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013765 int numListeners = listenersCopy.size();
13766 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013767 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013768 }
13769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013770 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013771 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013772 }
13773
13774 /**
13775 * Called from layout when this view should
13776 * assign a size and position to each of its children.
13777 *
13778 * Derived classes with children should override
13779 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013780 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013781 * @param changed This is a new size or position for this view
13782 * @param left Left position, relative to parent
13783 * @param top Top position, relative to parent
13784 * @param right Right position, relative to parent
13785 * @param bottom Bottom position, relative to parent
13786 */
13787 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13788 }
13789
13790 /**
13791 * Assign a size and position to this view.
13792 *
13793 * This is called from layout.
13794 *
13795 * @param left Left position, relative to parent
13796 * @param top Top position, relative to parent
13797 * @param right Right position, relative to parent
13798 * @param bottom Bottom position, relative to parent
13799 * @return true if the new size and position are different than the
13800 * previous ones
13801 * {@hide}
13802 */
13803 protected boolean setFrame(int left, int top, int right, int bottom) {
13804 boolean changed = false;
13805
13806 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013807 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013808 + right + "," + bottom + ")");
13809 }
13810
13811 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13812 changed = true;
13813
13814 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013815 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013817 int oldWidth = mRight - mLeft;
13818 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013819 int newWidth = right - left;
13820 int newHeight = bottom - top;
13821 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13822
13823 // Invalidate our old position
13824 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013825
13826 mLeft = left;
13827 mTop = top;
13828 mRight = right;
13829 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013830 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013831 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833
Dianne Hackborn4702a852012-08-17 15:18:29 -070013834 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013836
Chet Haase75755e22011-07-18 17:48:25 -070013837 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013838 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013839 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013840 if (mTransformationInfo != null) {
13841 mTransformationInfo.mMatrixDirty = true;
13842 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013844 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13845 }
13846
13847 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13848 // If we are visible, force the DRAWN bit to on so that
13849 // this invalidate will go through (at least to our parent).
13850 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013851 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013852 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013853 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013854 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013855 // parent display list may need to be recreated based on a change in the bounds
13856 // of any child
13857 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013858 }
13859
13860 // Reset drawn bit to original value (invalidate turns it off)
13861 mPrivateFlags |= drawn;
13862
13863 mBackgroundSizeChanged = true;
13864 }
13865 return changed;
13866 }
13867
13868 /**
13869 * Finalize inflating a view from XML. This is called as the last phase
13870 * of inflation, after all child views have been added.
13871 *
13872 * <p>Even if the subclass overrides onFinishInflate, they should always be
13873 * sure to call the super method, so that we get called.
13874 */
13875 protected void onFinishInflate() {
13876 }
13877
13878 /**
13879 * Returns the resources associated with this view.
13880 *
13881 * @return Resources object.
13882 */
13883 public Resources getResources() {
13884 return mResources;
13885 }
13886
13887 /**
13888 * Invalidates the specified Drawable.
13889 *
13890 * @param drawable the drawable to invalidate
13891 */
13892 public void invalidateDrawable(Drawable drawable) {
13893 if (verifyDrawable(drawable)) {
13894 final Rect dirty = drawable.getBounds();
13895 final int scrollX = mScrollX;
13896 final int scrollY = mScrollY;
13897
13898 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13899 dirty.right + scrollX, dirty.bottom + scrollY);
13900 }
13901 }
13902
13903 /**
13904 * Schedules an action on a drawable to occur at a specified time.
13905 *
13906 * @param who the recipient of the action
13907 * @param what the action to run on the drawable
13908 * @param when the time at which the action must occur. Uses the
13909 * {@link SystemClock#uptimeMillis} timebase.
13910 */
13911 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013912 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013913 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013914 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013915 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13916 Choreographer.CALLBACK_ANIMATION, what, who,
13917 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013918 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013919 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 }
13922 }
13923
13924 /**
13925 * Cancels a scheduled action on a drawable.
13926 *
13927 * @param who the recipient of the action
13928 * @param what the action to cancel
13929 */
13930 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013931 if (verifyDrawable(who) && what != null) {
13932 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013933 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13934 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013935 } else {
13936 ViewRootImpl.getRunQueue().removeCallbacks(what);
13937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013938 }
13939 }
13940
13941 /**
13942 * Unschedule any events associated with the given Drawable. This can be
13943 * used when selecting a new Drawable into a view, so that the previous
13944 * one is completely unscheduled.
13945 *
13946 * @param who The Drawable to unschedule.
13947 *
13948 * @see #drawableStateChanged
13949 */
13950 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013951 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013952 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13953 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 }
13955 }
13956
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013957 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013958 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13959 * that the View directionality can and will be resolved before its Drawables.
13960 *
13961 * Will call {@link View#onResolveDrawables} when resolution is done.
13962 */
13963 public void resolveDrawables() {
13964 if (mBackground != null) {
13965 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13966 }
13967 onResolveDrawables(getResolvedLayoutDirection());
13968 }
13969
13970 /**
13971 * Called when layout direction has been resolved.
13972 *
13973 * The default implementation does nothing.
13974 *
13975 * @param layoutDirection The resolved layout direction.
13976 *
13977 * @see {@link #LAYOUT_DIRECTION_LTR}
13978 * @see {@link #LAYOUT_DIRECTION_RTL}
13979 */
13980 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013981 }
13982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 /**
13984 * If your view subclass is displaying its own Drawable objects, it should
13985 * override this function and return true for any Drawable it is
13986 * displaying. This allows animations for those drawables to be
13987 * scheduled.
13988 *
13989 * <p>Be sure to call through to the super class when overriding this
13990 * function.
13991 *
13992 * @param who The Drawable to verify. Return true if it is one you are
13993 * displaying, else return the result of calling through to the
13994 * super class.
13995 *
13996 * @return boolean If true than the Drawable is being displayed in the
13997 * view; else false and it is not allowed to animate.
13998 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013999 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14000 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014001 */
14002 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014003 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 }
14005
14006 /**
14007 * This function is called whenever the state of the view changes in such
14008 * a way that it impacts the state of drawables being shown.
14009 *
14010 * <p>Be sure to call through to the superclass when overriding this
14011 * function.
14012 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014013 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 */
14015 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014016 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 if (d != null && d.isStateful()) {
14018 d.setState(getDrawableState());
14019 }
14020 }
14021
14022 /**
14023 * Call this to force a view to update its drawable state. This will cause
14024 * drawableStateChanged to be called on this view. Views that are interested
14025 * in the new state should call getDrawableState.
14026 *
14027 * @see #drawableStateChanged
14028 * @see #getDrawableState
14029 */
14030 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014031 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014032 drawableStateChanged();
14033
14034 ViewParent parent = mParent;
14035 if (parent != null) {
14036 parent.childDrawableStateChanged(this);
14037 }
14038 }
14039
14040 /**
14041 * Return an array of resource IDs of the drawable states representing the
14042 * current state of the view.
14043 *
14044 * @return The current drawable state
14045 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014046 * @see Drawable#setState(int[])
14047 * @see #drawableStateChanged()
14048 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014049 */
14050 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014051 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052 return mDrawableState;
14053 } else {
14054 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014055 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014056 return mDrawableState;
14057 }
14058 }
14059
14060 /**
14061 * Generate the new {@link android.graphics.drawable.Drawable} state for
14062 * this view. This is called by the view
14063 * system when the cached Drawable state is determined to be invalid. To
14064 * retrieve the current state, you should use {@link #getDrawableState}.
14065 *
14066 * @param extraSpace if non-zero, this is the number of extra entries you
14067 * would like in the returned array in which you can place your own
14068 * states.
14069 *
14070 * @return Returns an array holding the current {@link Drawable} state of
14071 * the view.
14072 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014073 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 */
14075 protected int[] onCreateDrawableState(int extraSpace) {
14076 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14077 mParent instanceof View) {
14078 return ((View) mParent).onCreateDrawableState(extraSpace);
14079 }
14080
14081 int[] drawableState;
14082
14083 int privateFlags = mPrivateFlags;
14084
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014085 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014086 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014087 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14088 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014089 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014090 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014091 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014092 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14093 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014094 // This is set if HW acceleration is requested, even if the current
14095 // process doesn't allow it. This is just to allow app preview
14096 // windows to better match their app.
14097 viewStateIndex |= VIEW_STATE_ACCELERATED;
14098 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014099 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014100
Christopher Tate3d4bf172011-03-28 16:16:46 -070014101 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014102 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14103 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105 drawableState = VIEW_STATE_SETS[viewStateIndex];
14106
14107 //noinspection ConstantIfStatement
14108 if (false) {
14109 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14110 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014111 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014112 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14113 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014114 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014115 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014116 + ": " + Arrays.toString(drawableState));
14117 }
14118
14119 if (extraSpace == 0) {
14120 return drawableState;
14121 }
14122
14123 final int[] fullState;
14124 if (drawableState != null) {
14125 fullState = new int[drawableState.length + extraSpace];
14126 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14127 } else {
14128 fullState = new int[extraSpace];
14129 }
14130
14131 return fullState;
14132 }
14133
14134 /**
14135 * Merge your own state values in <var>additionalState</var> into the base
14136 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014137 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014138 *
14139 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014140 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014141 * own additional state values.
14142 *
14143 * @param additionalState The additional state values you would like
14144 * added to <var>baseState</var>; this array is not modified.
14145 *
14146 * @return As a convenience, the <var>baseState</var> array you originally
14147 * passed into the function is returned.
14148 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014149 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150 */
14151 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14152 final int N = baseState.length;
14153 int i = N - 1;
14154 while (i >= 0 && baseState[i] == 0) {
14155 i--;
14156 }
14157 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14158 return baseState;
14159 }
14160
14161 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014162 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14163 * on all Drawable objects associated with this view.
14164 */
14165 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014166 if (mBackground != null) {
14167 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014168 }
14169 }
14170
14171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 * Sets the background color for this view.
14173 * @param color the color of the background
14174 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014175 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014176 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014177 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014178 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014179 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014180 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014181 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183 }
14184
14185 /**
14186 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014187 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014189 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014190 * @attr ref android.R.styleable#View_background
14191 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014192 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014193 public void setBackgroundResource(int resid) {
14194 if (resid != 0 && resid == mBackgroundResource) {
14195 return;
14196 }
14197
14198 Drawable d= null;
14199 if (resid != 0) {
14200 d = mResources.getDrawable(resid);
14201 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014202 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014203
14204 mBackgroundResource = resid;
14205 }
14206
14207 /**
14208 * Set the background to a given Drawable, or remove the background. If the
14209 * background has padding, this View's padding is set to the background's
14210 * padding. However, when a background is removed, this View's padding isn't
14211 * touched. If setting the padding is desired, please use
14212 * {@link #setPadding(int, int, int, int)}.
14213 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014214 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215 * background
14216 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014217 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014218 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014219 setBackgroundDrawable(background);
14220 }
14221
14222 /**
14223 * @deprecated use {@link #setBackground(Drawable)} instead
14224 */
14225 @Deprecated
14226 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014227 computeOpaqueFlags();
14228
Philip Milne6c8ea062012-04-03 17:38:43 -070014229 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014230 return;
14231 }
14232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014233 boolean requestLayout = false;
14234
14235 mBackgroundResource = 0;
14236
14237 /*
14238 * Regardless of whether we're setting a new background or not, we want
14239 * to clear the previous drawable.
14240 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014241 if (mBackground != null) {
14242 mBackground.setCallback(null);
14243 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014244 }
14245
Philip Milne6c8ea062012-04-03 17:38:43 -070014246 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247 Rect padding = sThreadLocal.get();
14248 if (padding == null) {
14249 padding = new Rect();
14250 sThreadLocal.set(padding);
14251 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014252 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014253 if (background.getPadding(padding)) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014254 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014255 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014256 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014257 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014258 mUserPaddingLeftInitial = padding.right;
14259 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014260 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014261 break;
14262 case LAYOUT_DIRECTION_LTR:
14263 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014264 mUserPaddingLeftInitial = padding.left;
14265 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014266 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 }
14269
14270 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14271 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014272 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14273 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274 requestLayout = true;
14275 }
14276
Philip Milne6c8ea062012-04-03 17:38:43 -070014277 background.setCallback(this);
14278 if (background.isStateful()) {
14279 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014280 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014281 background.setVisible(getVisibility() == VISIBLE, false);
14282 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014283
Dianne Hackborn4702a852012-08-17 15:18:29 -070014284 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14285 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14286 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014287 requestLayout = true;
14288 }
14289 } else {
14290 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014291 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014292
Dianne Hackborn4702a852012-08-17 15:18:29 -070014293 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014294 /*
14295 * This view ONLY drew the background before and we're removing
14296 * the background, so now it won't draw anything
14297 * (hence we SKIP_DRAW)
14298 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014299 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14300 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014301 }
14302
14303 /*
14304 * When the background is set, we try to apply its padding to this
14305 * View. When the background is removed, we don't touch this View's
14306 * padding. This is noted in the Javadocs. Hence, we don't need to
14307 * requestLayout(), the invalidate() below is sufficient.
14308 */
14309
14310 // The old background's minimum size could have affected this
14311 // View's layout, so let's requestLayout
14312 requestLayout = true;
14313 }
14314
Romain Guy8f1344f52009-05-15 16:03:59 -070014315 computeOpaqueFlags();
14316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014317 if (requestLayout) {
14318 requestLayout();
14319 }
14320
14321 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014322 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014323 }
14324
14325 /**
14326 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014327 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014328 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014329 *
14330 * @see #setBackground(Drawable)
14331 *
14332 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014333 */
14334 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014335 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014336 }
14337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014338 /**
14339 * Sets the padding. The view may add on the space required to display
14340 * the scrollbars, depending on the style and visibility of the scrollbars.
14341 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14342 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14343 * from the values set in this call.
14344 *
14345 * @attr ref android.R.styleable#View_padding
14346 * @attr ref android.R.styleable#View_paddingBottom
14347 * @attr ref android.R.styleable#View_paddingLeft
14348 * @attr ref android.R.styleable#View_paddingRight
14349 * @attr ref android.R.styleable#View_paddingTop
14350 * @param left the left padding in pixels
14351 * @param top the top padding in pixels
14352 * @param right the right padding in pixels
14353 * @param bottom the bottom padding in pixels
14354 */
14355 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014356 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014357 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014358
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014359 mUserPaddingStart = UNDEFINED_PADDING;
14360 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014361
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014362 mUserPaddingLeftInitial = left;
14363 mUserPaddingRightInitial = right;
14364
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014365 internalSetPadding(left, top, right, bottom);
14366 }
14367
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014368 /**
14369 * @hide
14370 */
14371 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014372 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014373 mUserPaddingRight = right;
14374 mUserPaddingBottom = bottom;
14375
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014376 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014377 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014378
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014379 // Common case is there are no scroll bars.
14380 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014381 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014382 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014383 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014384 switch (mVerticalScrollbarPosition) {
14385 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014386 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014387 left += offset;
14388 } else {
14389 right += offset;
14390 }
14391 break;
Adam Powell20232d02010-12-08 21:08:53 -080014392 case SCROLLBAR_POSITION_RIGHT:
14393 right += offset;
14394 break;
14395 case SCROLLBAR_POSITION_LEFT:
14396 left += offset;
14397 break;
14398 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014399 }
Adam Powell20232d02010-12-08 21:08:53 -080014400 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014401 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14402 ? 0 : getHorizontalScrollbarHeight();
14403 }
14404 }
Romain Guy8506ab42009-06-11 17:35:47 -070014405
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014406 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 changed = true;
14408 mPaddingLeft = left;
14409 }
14410 if (mPaddingTop != top) {
14411 changed = true;
14412 mPaddingTop = top;
14413 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014414 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014415 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014416 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014417 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014418 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014419 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014420 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 }
14422
14423 if (changed) {
14424 requestLayout();
14425 }
14426 }
14427
14428 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014429 * Sets the relative padding. The view may add on the space required to display
14430 * the scrollbars, depending on the style and visibility of the scrollbars.
14431 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14432 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14433 * from the values set in this call.
14434 *
14435 * @attr ref android.R.styleable#View_padding
14436 * @attr ref android.R.styleable#View_paddingBottom
14437 * @attr ref android.R.styleable#View_paddingStart
14438 * @attr ref android.R.styleable#View_paddingEnd
14439 * @attr ref android.R.styleable#View_paddingTop
14440 * @param start the start padding in pixels
14441 * @param top the top padding in pixels
14442 * @param end the end padding in pixels
14443 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014444 */
14445 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014446 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014447 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014448
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014449 mUserPaddingStart = start;
14450 mUserPaddingEnd = end;
14451
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014452 switch(getResolvedLayoutDirection()) {
14453 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014454 mUserPaddingLeftInitial = end;
14455 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014456 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014457 break;
14458 case LAYOUT_DIRECTION_LTR:
14459 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014460 mUserPaddingLeftInitial = start;
14461 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014462 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014463 }
14464 }
14465
14466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 * Returns the top padding of this view.
14468 *
14469 * @return the top padding in pixels
14470 */
14471 public int getPaddingTop() {
14472 return mPaddingTop;
14473 }
14474
14475 /**
14476 * Returns the bottom padding of this view. If there are inset and enabled
14477 * scrollbars, this value may include the space required to display the
14478 * scrollbars as well.
14479 *
14480 * @return the bottom padding in pixels
14481 */
14482 public int getPaddingBottom() {
14483 return mPaddingBottom;
14484 }
14485
14486 /**
14487 * Returns the left padding of this view. If there are inset and enabled
14488 * scrollbars, this value may include the space required to display the
14489 * scrollbars as well.
14490 *
14491 * @return the left padding in pixels
14492 */
14493 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014494 if (!isPaddingResolved()) {
14495 resolvePadding();
14496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014497 return mPaddingLeft;
14498 }
14499
14500 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014501 * Returns the start padding of this view depending on its resolved layout direction.
14502 * If there are inset and enabled scrollbars, this value may include the space
14503 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014504 *
14505 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014506 */
14507 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014508 if (!isPaddingResolved()) {
14509 resolvePadding();
14510 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014511 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14512 mPaddingRight : mPaddingLeft;
14513 }
14514
14515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516 * Returns the right padding of this view. If there are inset and enabled
14517 * scrollbars, this value may include the space required to display the
14518 * scrollbars as well.
14519 *
14520 * @return the right padding in pixels
14521 */
14522 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014523 if (!isPaddingResolved()) {
14524 resolvePadding();
14525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 return mPaddingRight;
14527 }
14528
14529 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014530 * Returns the end padding of this view depending on its resolved layout direction.
14531 * If there are inset and enabled scrollbars, this value may include the space
14532 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014533 *
14534 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014535 */
14536 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014537 if (!isPaddingResolved()) {
14538 resolvePadding();
14539 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014540 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14541 mPaddingLeft : mPaddingRight;
14542 }
14543
14544 /**
14545 * Return if the padding as been set thru relative values
14546 * {@link #setPaddingRelative(int, int, int, int)} or thru
14547 * @attr ref android.R.styleable#View_paddingStart or
14548 * @attr ref android.R.styleable#View_paddingEnd
14549 *
14550 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014551 */
14552 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014553 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014554 }
14555
14556 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014557 * @hide
14558 */
Philip Milne7a23b492012-04-24 22:12:36 -070014559 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014560 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014561 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014562 }
14563 return mLayoutInsets;
14564 }
14565
14566 /**
14567 * @hide
14568 */
14569 public void setLayoutInsets(Insets layoutInsets) {
14570 mLayoutInsets = layoutInsets;
14571 }
14572
14573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014574 * Changes the selection state of this view. A view can be selected or not.
14575 * Note that selection is not the same as focus. Views are typically
14576 * selected in the context of an AdapterView like ListView or GridView;
14577 * the selected view is the view that is highlighted.
14578 *
14579 * @param selected true if the view must be selected, false otherwise
14580 */
14581 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014582 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14583 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014584 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014585 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014586 refreshDrawableState();
14587 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014588 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14589 notifyAccessibilityStateChanged();
14590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014591 }
14592 }
14593
14594 /**
14595 * Dispatch setSelected to all of this View's children.
14596 *
14597 * @see #setSelected(boolean)
14598 *
14599 * @param selected The new selected state
14600 */
14601 protected void dispatchSetSelected(boolean selected) {
14602 }
14603
14604 /**
14605 * Indicates the selection state of this view.
14606 *
14607 * @return true if the view is selected, false otherwise
14608 */
14609 @ViewDebug.ExportedProperty
14610 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014611 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014612 }
14613
14614 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014615 * Changes the activated state of this view. A view can be activated or not.
14616 * Note that activation is not the same as selection. Selection is
14617 * a transient property, representing the view (hierarchy) the user is
14618 * currently interacting with. Activation is a longer-term state that the
14619 * user can move views in and out of. For example, in a list view with
14620 * single or multiple selection enabled, the views in the current selection
14621 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14622 * here.) The activated state is propagated down to children of the view it
14623 * is set on.
14624 *
14625 * @param activated true if the view must be activated, false otherwise
14626 */
14627 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014628 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14629 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014630 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014631 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014632 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014633 }
14634 }
14635
14636 /**
14637 * Dispatch setActivated to all of this View's children.
14638 *
14639 * @see #setActivated(boolean)
14640 *
14641 * @param activated The new activated state
14642 */
14643 protected void dispatchSetActivated(boolean activated) {
14644 }
14645
14646 /**
14647 * Indicates the activation state of this view.
14648 *
14649 * @return true if the view is activated, false otherwise
14650 */
14651 @ViewDebug.ExportedProperty
14652 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014653 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014654 }
14655
14656 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014657 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14658 * observer can be used to get notifications when global events, like
14659 * layout, happen.
14660 *
14661 * The returned ViewTreeObserver observer is not guaranteed to remain
14662 * valid for the lifetime of this View. If the caller of this method keeps
14663 * a long-lived reference to ViewTreeObserver, it should always check for
14664 * the return value of {@link ViewTreeObserver#isAlive()}.
14665 *
14666 * @return The ViewTreeObserver for this view's hierarchy.
14667 */
14668 public ViewTreeObserver getViewTreeObserver() {
14669 if (mAttachInfo != null) {
14670 return mAttachInfo.mTreeObserver;
14671 }
14672 if (mFloatingTreeObserver == null) {
14673 mFloatingTreeObserver = new ViewTreeObserver();
14674 }
14675 return mFloatingTreeObserver;
14676 }
14677
14678 /**
14679 * <p>Finds the topmost view in the current view hierarchy.</p>
14680 *
14681 * @return the topmost view containing this view
14682 */
14683 public View getRootView() {
14684 if (mAttachInfo != null) {
14685 final View v = mAttachInfo.mRootView;
14686 if (v != null) {
14687 return v;
14688 }
14689 }
Romain Guy8506ab42009-06-11 17:35:47 -070014690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014691 View parent = this;
14692
14693 while (parent.mParent != null && parent.mParent instanceof View) {
14694 parent = (View) parent.mParent;
14695 }
14696
14697 return parent;
14698 }
14699
14700 /**
14701 * <p>Computes the coordinates of this view on the screen. The argument
14702 * must be an array of two integers. After the method returns, the array
14703 * contains the x and y location in that order.</p>
14704 *
14705 * @param location an array of two integers in which to hold the coordinates
14706 */
14707 public void getLocationOnScreen(int[] location) {
14708 getLocationInWindow(location);
14709
14710 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014711 if (info != null) {
14712 location[0] += info.mWindowLeft;
14713 location[1] += info.mWindowTop;
14714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014715 }
14716
14717 /**
14718 * <p>Computes the coordinates of this view in its window. The argument
14719 * must be an array of two integers. After the method returns, the array
14720 * contains the x and y location in that order.</p>
14721 *
14722 * @param location an array of two integers in which to hold the coordinates
14723 */
14724 public void getLocationInWindow(int[] location) {
14725 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014726 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014727 }
14728
Gilles Debunne6583ce52011-12-06 18:09:02 -080014729 if (mAttachInfo == null) {
14730 // When the view is not attached to a window, this method does not make sense
14731 location[0] = location[1] = 0;
14732 return;
14733 }
14734
Gilles Debunnecea45132011-11-24 02:19:27 +010014735 float[] position = mAttachInfo.mTmpTransformLocation;
14736 position[0] = position[1] = 0.0f;
14737
14738 if (!hasIdentityMatrix()) {
14739 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014741
Gilles Debunnecea45132011-11-24 02:19:27 +010014742 position[0] += mLeft;
14743 position[1] += mTop;
14744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014745 ViewParent viewParent = mParent;
14746 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014747 final View view = (View) viewParent;
14748
14749 position[0] -= view.mScrollX;
14750 position[1] -= view.mScrollY;
14751
14752 if (!view.hasIdentityMatrix()) {
14753 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014754 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014755
14756 position[0] += view.mLeft;
14757 position[1] += view.mTop;
14758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014759 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014760 }
Romain Guy8506ab42009-06-11 17:35:47 -070014761
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014762 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014763 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014764 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14765 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014766 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014767
14768 location[0] = (int) (position[0] + 0.5f);
14769 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770 }
14771
14772 /**
14773 * {@hide}
14774 * @param id the id of the view to be found
14775 * @return the view of the specified id, null if cannot be found
14776 */
14777 protected View findViewTraversal(int id) {
14778 if (id == mID) {
14779 return this;
14780 }
14781 return null;
14782 }
14783
14784 /**
14785 * {@hide}
14786 * @param tag the tag of the view to be found
14787 * @return the view of specified tag, null if cannot be found
14788 */
14789 protected View findViewWithTagTraversal(Object tag) {
14790 if (tag != null && tag.equals(mTag)) {
14791 return this;
14792 }
14793 return null;
14794 }
14795
14796 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014797 * {@hide}
14798 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014799 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014800 * @return The first view that matches the predicate or null.
14801 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014802 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014803 if (predicate.apply(this)) {
14804 return this;
14805 }
14806 return null;
14807 }
14808
14809 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014810 * Look for a child view with the given id. If this view has the given
14811 * id, return this view.
14812 *
14813 * @param id The id to search for.
14814 * @return The view that has the given id in the hierarchy or null
14815 */
14816 public final View findViewById(int id) {
14817 if (id < 0) {
14818 return null;
14819 }
14820 return findViewTraversal(id);
14821 }
14822
14823 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014824 * Finds a view by its unuque and stable accessibility id.
14825 *
14826 * @param accessibilityId The searched accessibility id.
14827 * @return The found view.
14828 */
14829 final View findViewByAccessibilityId(int accessibilityId) {
14830 if (accessibilityId < 0) {
14831 return null;
14832 }
14833 return findViewByAccessibilityIdTraversal(accessibilityId);
14834 }
14835
14836 /**
14837 * Performs the traversal to find a view by its unuque and stable accessibility id.
14838 *
14839 * <strong>Note:</strong>This method does not stop at the root namespace
14840 * boundary since the user can touch the screen at an arbitrary location
14841 * potentially crossing the root namespace bounday which will send an
14842 * accessibility event to accessibility services and they should be able
14843 * to obtain the event source. Also accessibility ids are guaranteed to be
14844 * unique in the window.
14845 *
14846 * @param accessibilityId The accessibility id.
14847 * @return The found view.
14848 */
14849 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14850 if (getAccessibilityViewId() == accessibilityId) {
14851 return this;
14852 }
14853 return null;
14854 }
14855
14856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014857 * Look for a child view with the given tag. If this view has the given
14858 * tag, return this view.
14859 *
14860 * @param tag The tag to search for, using "tag.equals(getTag())".
14861 * @return The View that has the given tag in the hierarchy or null
14862 */
14863 public final View findViewWithTag(Object tag) {
14864 if (tag == null) {
14865 return null;
14866 }
14867 return findViewWithTagTraversal(tag);
14868 }
14869
14870 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014871 * {@hide}
14872 * Look for a child view that matches the specified predicate.
14873 * If this view matches the predicate, return this view.
14874 *
14875 * @param predicate The predicate to evaluate.
14876 * @return The first view that matches the predicate or null.
14877 */
14878 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014879 return findViewByPredicateTraversal(predicate, null);
14880 }
14881
14882 /**
14883 * {@hide}
14884 * Look for a child view that matches the specified predicate,
14885 * starting with the specified view and its descendents and then
14886 * recusively searching the ancestors and siblings of that view
14887 * until this view is reached.
14888 *
14889 * This method is useful in cases where the predicate does not match
14890 * a single unique view (perhaps multiple views use the same id)
14891 * and we are trying to find the view that is "closest" in scope to the
14892 * starting view.
14893 *
14894 * @param start The view to start from.
14895 * @param predicate The predicate to evaluate.
14896 * @return The first view that matches the predicate or null.
14897 */
14898 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14899 View childToSkip = null;
14900 for (;;) {
14901 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14902 if (view != null || start == this) {
14903 return view;
14904 }
14905
14906 ViewParent parent = start.getParent();
14907 if (parent == null || !(parent instanceof View)) {
14908 return null;
14909 }
14910
14911 childToSkip = start;
14912 start = (View) parent;
14913 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014914 }
14915
14916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014917 * Sets the identifier for this view. The identifier does not have to be
14918 * unique in this view's hierarchy. The identifier should be a positive
14919 * number.
14920 *
14921 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014922 * @see #getId()
14923 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014924 *
14925 * @param id a number used to identify the view
14926 *
14927 * @attr ref android.R.styleable#View_id
14928 */
14929 public void setId(int id) {
14930 mID = id;
14931 }
14932
14933 /**
14934 * {@hide}
14935 *
14936 * @param isRoot true if the view belongs to the root namespace, false
14937 * otherwise
14938 */
14939 public void setIsRootNamespace(boolean isRoot) {
14940 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014941 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014942 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014943 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014944 }
14945 }
14946
14947 /**
14948 * {@hide}
14949 *
14950 * @return true if the view belongs to the root namespace, false otherwise
14951 */
14952 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014953 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014954 }
14955
14956 /**
14957 * Returns this view's identifier.
14958 *
14959 * @return a positive integer used to identify the view or {@link #NO_ID}
14960 * if the view has no ID
14961 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014962 * @see #setId(int)
14963 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014964 * @attr ref android.R.styleable#View_id
14965 */
14966 @ViewDebug.CapturedViewProperty
14967 public int getId() {
14968 return mID;
14969 }
14970
14971 /**
14972 * Returns this view's tag.
14973 *
14974 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014975 *
14976 * @see #setTag(Object)
14977 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014978 */
14979 @ViewDebug.ExportedProperty
14980 public Object getTag() {
14981 return mTag;
14982 }
14983
14984 /**
14985 * Sets the tag associated with this view. A tag can be used to mark
14986 * a view in its hierarchy and does not have to be unique within the
14987 * hierarchy. Tags can also be used to store data within a view without
14988 * resorting to another data structure.
14989 *
14990 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014991 *
14992 * @see #getTag()
14993 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014994 */
14995 public void setTag(final Object tag) {
14996 mTag = tag;
14997 }
14998
14999 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015000 * Returns the tag associated with this view and the specified key.
15001 *
15002 * @param key The key identifying the tag
15003 *
15004 * @return the Object stored in this view as a tag
15005 *
15006 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015007 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015008 */
15009 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015010 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015011 return null;
15012 }
15013
15014 /**
15015 * Sets a tag associated with this view and a key. A tag can be used
15016 * to mark a view in its hierarchy and does not have to be unique within
15017 * the hierarchy. Tags can also be used to store data within a view
15018 * without resorting to another data structure.
15019 *
15020 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015021 * application to ensure it is unique (see the <a
15022 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15023 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015024 * the Android framework or not associated with any package will cause
15025 * an {@link IllegalArgumentException} to be thrown.
15026 *
15027 * @param key The key identifying the tag
15028 * @param tag An Object to tag the view with
15029 *
15030 * @throws IllegalArgumentException If they specified key is not valid
15031 *
15032 * @see #setTag(Object)
15033 * @see #getTag(int)
15034 */
15035 public void setTag(int key, final Object tag) {
15036 // If the package id is 0x00 or 0x01, it's either an undefined package
15037 // or a framework id
15038 if ((key >>> 24) < 2) {
15039 throw new IllegalArgumentException("The key must be an application-specific "
15040 + "resource id.");
15041 }
15042
Adam Powell2b2f6d62011-09-23 11:15:39 -070015043 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015044 }
15045
15046 /**
15047 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15048 * framework id.
15049 *
15050 * @hide
15051 */
15052 public void setTagInternal(int key, Object tag) {
15053 if ((key >>> 24) != 0x1) {
15054 throw new IllegalArgumentException("The key must be a framework-specific "
15055 + "resource id.");
15056 }
15057
Adam Powell2b2f6d62011-09-23 11:15:39 -070015058 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015059 }
15060
Adam Powell2b2f6d62011-09-23 11:15:39 -070015061 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015062 if (mKeyedTags == null) {
15063 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015064 }
15065
Adam Powell7db82ac2011-09-22 19:44:04 -070015066 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015067 }
15068
15069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015070 * Prints information about this view in the log output, with the tag
15071 * {@link #VIEW_LOG_TAG}.
15072 *
15073 * @hide
15074 */
15075 public void debug() {
15076 debug(0);
15077 }
15078
15079 /**
15080 * Prints information about this view in the log output, with the tag
15081 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15082 * indentation defined by the <code>depth</code>.
15083 *
15084 * @param depth the indentation level
15085 *
15086 * @hide
15087 */
15088 protected void debug(int depth) {
15089 String output = debugIndent(depth - 1);
15090
15091 output += "+ " + this;
15092 int id = getId();
15093 if (id != -1) {
15094 output += " (id=" + id + ")";
15095 }
15096 Object tag = getTag();
15097 if (tag != null) {
15098 output += " (tag=" + tag + ")";
15099 }
15100 Log.d(VIEW_LOG_TAG, output);
15101
Dianne Hackborn4702a852012-08-17 15:18:29 -070015102 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015103 output = debugIndent(depth) + " FOCUSED";
15104 Log.d(VIEW_LOG_TAG, output);
15105 }
15106
15107 output = debugIndent(depth);
15108 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15109 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15110 + "} ";
15111 Log.d(VIEW_LOG_TAG, output);
15112
15113 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15114 || mPaddingBottom != 0) {
15115 output = debugIndent(depth);
15116 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15117 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15118 Log.d(VIEW_LOG_TAG, output);
15119 }
15120
15121 output = debugIndent(depth);
15122 output += "mMeasureWidth=" + mMeasuredWidth +
15123 " mMeasureHeight=" + mMeasuredHeight;
15124 Log.d(VIEW_LOG_TAG, output);
15125
15126 output = debugIndent(depth);
15127 if (mLayoutParams == null) {
15128 output += "BAD! no layout params";
15129 } else {
15130 output = mLayoutParams.debug(output);
15131 }
15132 Log.d(VIEW_LOG_TAG, output);
15133
15134 output = debugIndent(depth);
15135 output += "flags={";
15136 output += View.printFlags(mViewFlags);
15137 output += "}";
15138 Log.d(VIEW_LOG_TAG, output);
15139
15140 output = debugIndent(depth);
15141 output += "privateFlags={";
15142 output += View.printPrivateFlags(mPrivateFlags);
15143 output += "}";
15144 Log.d(VIEW_LOG_TAG, output);
15145 }
15146
15147 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015148 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015149 *
15150 * @param depth the indentation level
15151 * @return a String containing (depth * 2 + 3) * 2 white spaces
15152 *
15153 * @hide
15154 */
15155 protected static String debugIndent(int depth) {
15156 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15157 for (int i = 0; i < (depth * 2) + 3; i++) {
15158 spaces.append(' ').append(' ');
15159 }
15160 return spaces.toString();
15161 }
15162
15163 /**
15164 * <p>Return the offset of the widget's text baseline from the widget's top
15165 * boundary. If this widget does not support baseline alignment, this
15166 * method returns -1. </p>
15167 *
15168 * @return the offset of the baseline within the widget's bounds or -1
15169 * if baseline alignment is not supported
15170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015171 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015172 public int getBaseline() {
15173 return -1;
15174 }
15175
15176 /**
15177 * Call this when something has changed which has invalidated the
15178 * layout of this view. This will schedule a layout pass of the view
15179 * tree.
15180 */
15181 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015182 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15183 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015184
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015185 if (mParent != null && !mParent.isLayoutRequested()) {
15186 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015187 }
15188 }
15189
15190 /**
15191 * Forces this view to be laid out during the next layout pass.
15192 * This method does not call requestLayout() or forceLayout()
15193 * on the parent.
15194 */
15195 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015196 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15197 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015198 }
15199
15200 /**
15201 * <p>
15202 * This is called to find out how big a view should be. The parent
15203 * supplies constraint information in the width and height parameters.
15204 * </p>
15205 *
15206 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015207 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015208 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015209 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015210 * </p>
15211 *
15212 *
15213 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15214 * parent
15215 * @param heightMeasureSpec Vertical space requirements as imposed by the
15216 * parent
15217 *
15218 * @see #onMeasure(int, int)
15219 */
15220 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015221 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015222 widthMeasureSpec != mOldWidthMeasureSpec ||
15223 heightMeasureSpec != mOldHeightMeasureSpec) {
15224
15225 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015226 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015227
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015228 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015229 resolvePadding();
15230 }
15231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015232 // measure ourselves, this should set the measured dimension flag back
15233 onMeasure(widthMeasureSpec, heightMeasureSpec);
15234
15235 // flag not set, setMeasuredDimension() was not invoked, we raise
15236 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015237 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015238 throw new IllegalStateException("onMeasure() did not set the"
15239 + " measured dimension by calling"
15240 + " setMeasuredDimension()");
15241 }
15242
Dianne Hackborn4702a852012-08-17 15:18:29 -070015243 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015244 }
15245
15246 mOldWidthMeasureSpec = widthMeasureSpec;
15247 mOldHeightMeasureSpec = heightMeasureSpec;
15248 }
15249
15250 /**
15251 * <p>
15252 * Measure the view and its content to determine the measured width and the
15253 * measured height. This method is invoked by {@link #measure(int, int)} and
15254 * should be overriden by subclasses to provide accurate and efficient
15255 * measurement of their contents.
15256 * </p>
15257 *
15258 * <p>
15259 * <strong>CONTRACT:</strong> When overriding this method, you
15260 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15261 * measured width and height of this view. Failure to do so will trigger an
15262 * <code>IllegalStateException</code>, thrown by
15263 * {@link #measure(int, int)}. Calling the superclass'
15264 * {@link #onMeasure(int, int)} is a valid use.
15265 * </p>
15266 *
15267 * <p>
15268 * The base class implementation of measure defaults to the background size,
15269 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15270 * override {@link #onMeasure(int, int)} to provide better measurements of
15271 * their content.
15272 * </p>
15273 *
15274 * <p>
15275 * If this method is overridden, it is the subclass's responsibility to make
15276 * sure the measured height and width are at least the view's minimum height
15277 * and width ({@link #getSuggestedMinimumHeight()} and
15278 * {@link #getSuggestedMinimumWidth()}).
15279 * </p>
15280 *
15281 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15282 * The requirements are encoded with
15283 * {@link android.view.View.MeasureSpec}.
15284 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15285 * The requirements are encoded with
15286 * {@link android.view.View.MeasureSpec}.
15287 *
15288 * @see #getMeasuredWidth()
15289 * @see #getMeasuredHeight()
15290 * @see #setMeasuredDimension(int, int)
15291 * @see #getSuggestedMinimumHeight()
15292 * @see #getSuggestedMinimumWidth()
15293 * @see android.view.View.MeasureSpec#getMode(int)
15294 * @see android.view.View.MeasureSpec#getSize(int)
15295 */
15296 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15297 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15298 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15299 }
15300
15301 /**
15302 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15303 * measured width and measured height. Failing to do so will trigger an
15304 * exception at measurement time.</p>
15305 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015306 * @param measuredWidth The measured width of this view. May be a complex
15307 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15308 * {@link #MEASURED_STATE_TOO_SMALL}.
15309 * @param measuredHeight The measured height of this view. May be a complex
15310 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15311 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015312 */
15313 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15314 mMeasuredWidth = measuredWidth;
15315 mMeasuredHeight = measuredHeight;
15316
Dianne Hackborn4702a852012-08-17 15:18:29 -070015317 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015318 }
15319
15320 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015321 * Merge two states as returned by {@link #getMeasuredState()}.
15322 * @param curState The current state as returned from a view or the result
15323 * of combining multiple views.
15324 * @param newState The new view state to combine.
15325 * @return Returns a new integer reflecting the combination of the two
15326 * states.
15327 */
15328 public static int combineMeasuredStates(int curState, int newState) {
15329 return curState | newState;
15330 }
15331
15332 /**
15333 * Version of {@link #resolveSizeAndState(int, int, int)}
15334 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15335 */
15336 public static int resolveSize(int size, int measureSpec) {
15337 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15338 }
15339
15340 /**
15341 * Utility to reconcile a desired size and state, with constraints imposed
15342 * by a MeasureSpec. Will take the desired size, unless a different size
15343 * is imposed by the constraints. The returned value is a compound integer,
15344 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15345 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15346 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015347 *
15348 * @param size How big the view wants to be
15349 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015350 * @return Size information bit mask as defined by
15351 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015352 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015353 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015354 int result = size;
15355 int specMode = MeasureSpec.getMode(measureSpec);
15356 int specSize = MeasureSpec.getSize(measureSpec);
15357 switch (specMode) {
15358 case MeasureSpec.UNSPECIFIED:
15359 result = size;
15360 break;
15361 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015362 if (specSize < size) {
15363 result = specSize | MEASURED_STATE_TOO_SMALL;
15364 } else {
15365 result = size;
15366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015367 break;
15368 case MeasureSpec.EXACTLY:
15369 result = specSize;
15370 break;
15371 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015372 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015373 }
15374
15375 /**
15376 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015377 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015378 * by the MeasureSpec.
15379 *
15380 * @param size Default size for this view
15381 * @param measureSpec Constraints imposed by the parent
15382 * @return The size this view should be.
15383 */
15384 public static int getDefaultSize(int size, int measureSpec) {
15385 int result = size;
15386 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015387 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015388
15389 switch (specMode) {
15390 case MeasureSpec.UNSPECIFIED:
15391 result = size;
15392 break;
15393 case MeasureSpec.AT_MOST:
15394 case MeasureSpec.EXACTLY:
15395 result = specSize;
15396 break;
15397 }
15398 return result;
15399 }
15400
15401 /**
15402 * Returns the suggested minimum height that the view should use. This
15403 * returns the maximum of the view's minimum height
15404 * and the background's minimum height
15405 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15406 * <p>
15407 * When being used in {@link #onMeasure(int, int)}, the caller should still
15408 * ensure the returned height is within the requirements of the parent.
15409 *
15410 * @return The suggested minimum height of the view.
15411 */
15412 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015413 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015415 }
15416
15417 /**
15418 * Returns the suggested minimum width that the view should use. This
15419 * returns the maximum of the view's minimum width)
15420 * and the background's minimum width
15421 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15422 * <p>
15423 * When being used in {@link #onMeasure(int, int)}, the caller should still
15424 * ensure the returned width is within the requirements of the parent.
15425 *
15426 * @return The suggested minimum width of the view.
15427 */
15428 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015429 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015431
Philip Milne6c8ea062012-04-03 17:38:43 -070015432 /**
15433 * Returns the minimum height of the view.
15434 *
15435 * @return the minimum height the view will try to be.
15436 *
15437 * @see #setMinimumHeight(int)
15438 *
15439 * @attr ref android.R.styleable#View_minHeight
15440 */
15441 public int getMinimumHeight() {
15442 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015443 }
15444
15445 /**
15446 * Sets the minimum height of the view. It is not guaranteed the view will
15447 * be able to achieve this minimum height (for example, if its parent layout
15448 * constrains it with less available height).
15449 *
15450 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015451 *
15452 * @see #getMinimumHeight()
15453 *
15454 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015455 */
15456 public void setMinimumHeight(int minHeight) {
15457 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015458 requestLayout();
15459 }
15460
15461 /**
15462 * Returns the minimum width of the view.
15463 *
15464 * @return the minimum width the view will try to be.
15465 *
15466 * @see #setMinimumWidth(int)
15467 *
15468 * @attr ref android.R.styleable#View_minWidth
15469 */
15470 public int getMinimumWidth() {
15471 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015472 }
15473
15474 /**
15475 * Sets the minimum width of the view. It is not guaranteed the view will
15476 * be able to achieve this minimum width (for example, if its parent layout
15477 * constrains it with less available width).
15478 *
15479 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015480 *
15481 * @see #getMinimumWidth()
15482 *
15483 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015484 */
15485 public void setMinimumWidth(int minWidth) {
15486 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015487 requestLayout();
15488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015489 }
15490
15491 /**
15492 * Get the animation currently associated with this view.
15493 *
15494 * @return The animation that is currently playing or
15495 * scheduled to play for this view.
15496 */
15497 public Animation getAnimation() {
15498 return mCurrentAnimation;
15499 }
15500
15501 /**
15502 * Start the specified animation now.
15503 *
15504 * @param animation the animation to start now
15505 */
15506 public void startAnimation(Animation animation) {
15507 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15508 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015509 invalidateParentCaches();
15510 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015511 }
15512
15513 /**
15514 * Cancels any animations for this view.
15515 */
15516 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015517 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015518 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015520 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015521 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015522 }
15523
15524 /**
15525 * Sets the next animation to play for this view.
15526 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015527 * {@link #startAnimation(android.view.animation.Animation)} instead.
15528 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015529 * control over the start time and invalidation, but you
15530 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015531 * 2) the view's parent (which controls animations on its children)
15532 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015533 * start.
15534 *
15535 * @param animation The next animation, or null.
15536 */
15537 public void setAnimation(Animation animation) {
15538 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015540 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015541 // If the screen is off assume the animation start time is now instead of
15542 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15543 // would cause the animation to start when the screen turns back on
15544 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15545 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15546 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015548 animation.reset();
15549 }
15550 }
15551
15552 /**
15553 * Invoked by a parent ViewGroup to notify the start of the animation
15554 * currently associated with this view. If you override this method,
15555 * always call super.onAnimationStart();
15556 *
15557 * @see #setAnimation(android.view.animation.Animation)
15558 * @see #getAnimation()
15559 */
15560 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015561 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015562 }
15563
15564 /**
15565 * Invoked by a parent ViewGroup to notify the end of the animation
15566 * currently associated with this view. If you override this method,
15567 * always call super.onAnimationEnd();
15568 *
15569 * @see #setAnimation(android.view.animation.Animation)
15570 * @see #getAnimation()
15571 */
15572 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015573 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015574 }
15575
15576 /**
15577 * Invoked if there is a Transform that involves alpha. Subclass that can
15578 * draw themselves with the specified alpha should return true, and then
15579 * respect that alpha when their onDraw() is called. If this returns false
15580 * then the view may be redirected to draw into an offscreen buffer to
15581 * fulfill the request, which will look fine, but may be slower than if the
15582 * subclass handles it internally. The default implementation returns false.
15583 *
15584 * @param alpha The alpha (0..255) to apply to the view's drawing
15585 * @return true if the view can draw with the specified alpha.
15586 */
15587 protected boolean onSetAlpha(int alpha) {
15588 return false;
15589 }
15590
15591 /**
15592 * This is used by the RootView to perform an optimization when
15593 * the view hierarchy contains one or several SurfaceView.
15594 * SurfaceView is always considered transparent, but its children are not,
15595 * therefore all View objects remove themselves from the global transparent
15596 * region (passed as a parameter to this function).
15597 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015598 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015599 *
15600 * @return Returns true if the effective visibility of the view at this
15601 * point is opaque, regardless of the transparent region; returns false
15602 * if it is possible for underlying windows to be seen behind the view.
15603 *
15604 * {@hide}
15605 */
15606 public boolean gatherTransparentRegion(Region region) {
15607 final AttachInfo attachInfo = mAttachInfo;
15608 if (region != null && attachInfo != null) {
15609 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015610 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015611 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15612 // remove it from the transparent region.
15613 final int[] location = attachInfo.mTransparentLocation;
15614 getLocationInWindow(location);
15615 region.op(location[0], location[1], location[0] + mRight - mLeft,
15616 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015617 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015618 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15619 // exists, so we remove the background drawable's non-transparent
15620 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015621 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015622 }
15623 }
15624 return true;
15625 }
15626
15627 /**
15628 * Play a sound effect for this view.
15629 *
15630 * <p>The framework will play sound effects for some built in actions, such as
15631 * clicking, but you may wish to play these effects in your widget,
15632 * for instance, for internal navigation.
15633 *
15634 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15635 * {@link #isSoundEffectsEnabled()} is true.
15636 *
15637 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15638 */
15639 public void playSoundEffect(int soundConstant) {
15640 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15641 return;
15642 }
15643 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15644 }
15645
15646 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015647 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015648 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015649 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015650 *
15651 * <p>The framework will provide haptic feedback for some built in actions,
15652 * such as long presses, but you may wish to provide feedback for your
15653 * own widget.
15654 *
15655 * <p>The feedback will only be performed if
15656 * {@link #isHapticFeedbackEnabled()} is true.
15657 *
15658 * @param feedbackConstant One of the constants defined in
15659 * {@link HapticFeedbackConstants}
15660 */
15661 public boolean performHapticFeedback(int feedbackConstant) {
15662 return performHapticFeedback(feedbackConstant, 0);
15663 }
15664
15665 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015666 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015667 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015668 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015669 *
15670 * @param feedbackConstant One of the constants defined in
15671 * {@link HapticFeedbackConstants}
15672 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15673 */
15674 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15675 if (mAttachInfo == null) {
15676 return false;
15677 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015678 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015679 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015680 && !isHapticFeedbackEnabled()) {
15681 return false;
15682 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015683 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15684 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015685 }
15686
15687 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015688 * Request that the visibility of the status bar or other screen/window
15689 * decorations be changed.
15690 *
15691 * <p>This method is used to put the over device UI into temporary modes
15692 * where the user's attention is focused more on the application content,
15693 * by dimming or hiding surrounding system affordances. This is typically
15694 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15695 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15696 * to be placed behind the action bar (and with these flags other system
15697 * affordances) so that smooth transitions between hiding and showing them
15698 * can be done.
15699 *
15700 * <p>Two representative examples of the use of system UI visibility is
15701 * implementing a content browsing application (like a magazine reader)
15702 * and a video playing application.
15703 *
15704 * <p>The first code shows a typical implementation of a View in a content
15705 * browsing application. In this implementation, the application goes
15706 * into a content-oriented mode by hiding the status bar and action bar,
15707 * and putting the navigation elements into lights out mode. The user can
15708 * then interact with content while in this mode. Such an application should
15709 * provide an easy way for the user to toggle out of the mode (such as to
15710 * check information in the status bar or access notifications). In the
15711 * implementation here, this is done simply by tapping on the content.
15712 *
15713 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15714 * content}
15715 *
15716 * <p>This second code sample shows a typical implementation of a View
15717 * in a video playing application. In this situation, while the video is
15718 * playing the application would like to go into a complete full-screen mode,
15719 * to use as much of the display as possible for the video. When in this state
15720 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015721 * touching on the screen to pop the UI out of full screen mode. See
15722 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015723 *
15724 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15725 * content}
15726 *
15727 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15728 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15729 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15730 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015731 */
15732 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015733 if (visibility != mSystemUiVisibility) {
15734 mSystemUiVisibility = visibility;
15735 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15736 mParent.recomputeViewAttributes(this);
15737 }
Joe Onorato664644d2011-01-23 17:53:23 -080015738 }
15739 }
15740
15741 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015742 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15743 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15744 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15745 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15746 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015747 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015748 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015749 return mSystemUiVisibility;
15750 }
15751
Scott Mainec6331b2011-05-24 16:55:56 -070015752 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015753 * Returns the current system UI visibility that is currently set for
15754 * the entire window. This is the combination of the
15755 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15756 * views in the window.
15757 */
15758 public int getWindowSystemUiVisibility() {
15759 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15760 }
15761
15762 /**
15763 * Override to find out when the window's requested system UI visibility
15764 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15765 * This is different from the callbacks recieved through
15766 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15767 * in that this is only telling you about the local request of the window,
15768 * not the actual values applied by the system.
15769 */
15770 public void onWindowSystemUiVisibilityChanged(int visible) {
15771 }
15772
15773 /**
15774 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15775 * the view hierarchy.
15776 */
15777 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15778 onWindowSystemUiVisibilityChanged(visible);
15779 }
15780
15781 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015782 * Set a listener to receive callbacks when the visibility of the system bar changes.
15783 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15784 */
Joe Onorato664644d2011-01-23 17:53:23 -080015785 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015786 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015787 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15788 mParent.recomputeViewAttributes(this);
15789 }
15790 }
15791
15792 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015793 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15794 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015795 */
15796 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015797 ListenerInfo li = mListenerInfo;
15798 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15799 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015800 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015801 }
15802 }
15803
Dianne Hackborncf675782012-05-10 15:07:24 -070015804 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015805 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15806 if (val != mSystemUiVisibility) {
15807 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015808 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015809 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015810 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015811 }
15812
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015813 /** @hide */
15814 public void setDisabledSystemUiVisibility(int flags) {
15815 if (mAttachInfo != null) {
15816 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15817 mAttachInfo.mDisabledSystemUiVisibility = flags;
15818 if (mParent != null) {
15819 mParent.recomputeViewAttributes(this);
15820 }
15821 }
15822 }
15823 }
15824
Joe Onorato664644d2011-01-23 17:53:23 -080015825 /**
Joe Malin32736f02011-01-19 16:14:20 -080015826 * Creates an image that the system displays during the drag and drop
15827 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15828 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15829 * appearance as the given View. The default also positions the center of the drag shadow
15830 * directly under the touch point. If no View is provided (the constructor with no parameters
15831 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15832 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15833 * default is an invisible drag shadow.
15834 * <p>
15835 * You are not required to use the View you provide to the constructor as the basis of the
15836 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15837 * anything you want as the drag shadow.
15838 * </p>
15839 * <p>
15840 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15841 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15842 * size and position of the drag shadow. It uses this data to construct a
15843 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15844 * so that your application can draw the shadow image in the Canvas.
15845 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015846 *
15847 * <div class="special reference">
15848 * <h3>Developer Guides</h3>
15849 * <p>For a guide to implementing drag and drop features, read the
15850 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15851 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015852 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015853 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015854 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015855
15856 /**
Joe Malin32736f02011-01-19 16:14:20 -080015857 * Constructs a shadow image builder based on a View. By default, the resulting drag
15858 * shadow will have the same appearance and dimensions as the View, with the touch point
15859 * over the center of the View.
15860 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015861 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015862 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015863 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015864 }
15865
Christopher Tate17ed60c2011-01-18 12:50:26 -080015866 /**
15867 * Construct a shadow builder object with no associated View. This
15868 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15869 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15870 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015871 * reference to any View object. If they are not overridden, then the result is an
15872 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015873 */
15874 public DragShadowBuilder() {
15875 mView = new WeakReference<View>(null);
15876 }
15877
15878 /**
15879 * Returns the View object that had been passed to the
15880 * {@link #View.DragShadowBuilder(View)}
15881 * constructor. If that View parameter was {@code null} or if the
15882 * {@link #View.DragShadowBuilder()}
15883 * constructor was used to instantiate the builder object, this method will return
15884 * null.
15885 *
15886 * @return The View object associate with this builder object.
15887 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015888 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015889 final public View getView() {
15890 return mView.get();
15891 }
15892
Christopher Tate2c095f32010-10-04 14:13:40 -070015893 /**
Joe Malin32736f02011-01-19 16:14:20 -080015894 * Provides the metrics for the shadow image. These include the dimensions of
15895 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015896 * be centered under the touch location while dragging.
15897 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015898 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015899 * same as the dimensions of the View itself and centers the shadow under
15900 * the touch point.
15901 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015902 *
Joe Malin32736f02011-01-19 16:14:20 -080015903 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15904 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15905 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15906 * image.
15907 *
15908 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15909 * shadow image that should be underneath the touch point during the drag and drop
15910 * operation. Your application must set {@link android.graphics.Point#x} to the
15911 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015912 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015913 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015914 final View view = mView.get();
15915 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015916 shadowSize.set(view.getWidth(), view.getHeight());
15917 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015918 } else {
15919 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15920 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015921 }
15922
15923 /**
Joe Malin32736f02011-01-19 16:14:20 -080015924 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15925 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015926 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015927 *
Joe Malin32736f02011-01-19 16:14:20 -080015928 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015929 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015930 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015931 final View view = mView.get();
15932 if (view != null) {
15933 view.draw(canvas);
15934 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015935 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015936 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015937 }
15938 }
15939
15940 /**
Joe Malin32736f02011-01-19 16:14:20 -080015941 * Starts a drag and drop operation. When your application calls this method, it passes a
15942 * {@link android.view.View.DragShadowBuilder} object to the system. The
15943 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15944 * to get metrics for the drag shadow, and then calls the object's
15945 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15946 * <p>
15947 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15948 * drag events to all the View objects in your application that are currently visible. It does
15949 * this either by calling the View object's drag listener (an implementation of
15950 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15951 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15952 * Both are passed a {@link android.view.DragEvent} object that has a
15953 * {@link android.view.DragEvent#getAction()} value of
15954 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15955 * </p>
15956 * <p>
15957 * Your application can invoke startDrag() on any attached View object. The View object does not
15958 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15959 * be related to the View the user selected for dragging.
15960 * </p>
15961 * @param data A {@link android.content.ClipData} object pointing to the data to be
15962 * transferred by the drag and drop operation.
15963 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15964 * drag shadow.
15965 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15966 * drop operation. This Object is put into every DragEvent object sent by the system during the
15967 * current drag.
15968 * <p>
15969 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15970 * to the target Views. For example, it can contain flags that differentiate between a
15971 * a copy operation and a move operation.
15972 * </p>
15973 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15974 * so the parameter should be set to 0.
15975 * @return {@code true} if the method completes successfully, or
15976 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15977 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015978 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015979 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015980 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015981 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015982 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015983 }
15984 boolean okay = false;
15985
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015986 Point shadowSize = new Point();
15987 Point shadowTouchPoint = new Point();
15988 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015989
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015990 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15991 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15992 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015993 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015994
Chris Tatea32dcf72010-10-14 12:13:50 -070015995 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015996 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15997 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015998 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015999 Surface surface = new Surface();
16000 try {
16001 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016002 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016003 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016004 + " surface=" + surface);
16005 if (token != null) {
16006 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016007 try {
Chris Tate6b391282010-10-14 15:48:59 -070016008 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016009 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016010 } finally {
16011 surface.unlockCanvasAndPost(canvas);
16012 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016013
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016014 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016015
16016 // Cache the local state object for delivery with DragEvents
16017 root.setLocalDragState(myLocalState);
16018
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016019 // repurpose 'shadowSize' for the last touch point
16020 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016021
Christopher Tatea53146c2010-09-07 11:57:52 -070016022 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016023 shadowSize.x, shadowSize.y,
16024 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016025 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016026
16027 // Off and running! Release our local surface instance; the drag
16028 // shadow surface is now managed by the system process.
16029 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016030 }
16031 } catch (Exception e) {
16032 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16033 surface.destroy();
16034 }
16035
16036 return okay;
16037 }
16038
Christopher Tatea53146c2010-09-07 11:57:52 -070016039 /**
Joe Malin32736f02011-01-19 16:14:20 -080016040 * Handles drag events sent by the system following a call to
16041 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16042 *<p>
16043 * When the system calls this method, it passes a
16044 * {@link android.view.DragEvent} object. A call to
16045 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16046 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16047 * operation.
16048 * @param event The {@link android.view.DragEvent} sent by the system.
16049 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16050 * in DragEvent, indicating the type of drag event represented by this object.
16051 * @return {@code true} if the method was successful, otherwise {@code false}.
16052 * <p>
16053 * The method should return {@code true} in response to an action type of
16054 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16055 * operation.
16056 * </p>
16057 * <p>
16058 * The method should also return {@code true} in response to an action type of
16059 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16060 * {@code false} if it didn't.
16061 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016062 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016063 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016064 return false;
16065 }
16066
16067 /**
Joe Malin32736f02011-01-19 16:14:20 -080016068 * Detects if this View is enabled and has a drag event listener.
16069 * If both are true, then it calls the drag event listener with the
16070 * {@link android.view.DragEvent} it received. If the drag event listener returns
16071 * {@code true}, then dispatchDragEvent() returns {@code true}.
16072 * <p>
16073 * For all other cases, the method calls the
16074 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16075 * method and returns its result.
16076 * </p>
16077 * <p>
16078 * This ensures that a drag event is always consumed, even if the View does not have a drag
16079 * event listener. However, if the View has a listener and the listener returns true, then
16080 * onDragEvent() is not called.
16081 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016082 */
16083 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016084 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016085 ListenerInfo li = mListenerInfo;
16086 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16087 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016088 return true;
16089 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016090 return onDragEvent(event);
16091 }
16092
Christopher Tate3d4bf172011-03-28 16:16:46 -070016093 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016094 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016095 }
16096
Christopher Tatea53146c2010-09-07 11:57:52 -070016097 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016098 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16099 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016100 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016101 */
16102 public void onCloseSystemDialogs(String reason) {
16103 }
Joe Malin32736f02011-01-19 16:14:20 -080016104
Dianne Hackbornffa42482009-09-23 22:20:11 -070016105 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016106 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016107 * update a Region being computed for
16108 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016109 * that any non-transparent parts of the Drawable are removed from the
16110 * given transparent region.
16111 *
16112 * @param dr The Drawable whose transparency is to be applied to the region.
16113 * @param region A Region holding the current transparency information,
16114 * where any parts of the region that are set are considered to be
16115 * transparent. On return, this region will be modified to have the
16116 * transparency information reduced by the corresponding parts of the
16117 * Drawable that are not transparent.
16118 * {@hide}
16119 */
16120 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16121 if (DBG) {
16122 Log.i("View", "Getting transparent region for: " + this);
16123 }
16124 final Region r = dr.getTransparentRegion();
16125 final Rect db = dr.getBounds();
16126 final AttachInfo attachInfo = mAttachInfo;
16127 if (r != null && attachInfo != null) {
16128 final int w = getRight()-getLeft();
16129 final int h = getBottom()-getTop();
16130 if (db.left > 0) {
16131 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16132 r.op(0, 0, db.left, h, Region.Op.UNION);
16133 }
16134 if (db.right < w) {
16135 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16136 r.op(db.right, 0, w, h, Region.Op.UNION);
16137 }
16138 if (db.top > 0) {
16139 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16140 r.op(0, 0, w, db.top, Region.Op.UNION);
16141 }
16142 if (db.bottom < h) {
16143 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16144 r.op(0, db.bottom, w, h, Region.Op.UNION);
16145 }
16146 final int[] location = attachInfo.mTransparentLocation;
16147 getLocationInWindow(location);
16148 r.translate(location[0], location[1]);
16149 region.op(r, Region.Op.INTERSECT);
16150 } else {
16151 region.op(db, Region.Op.DIFFERENCE);
16152 }
16153 }
16154
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016155 private void checkForLongClick(int delayOffset) {
16156 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16157 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016158
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016159 if (mPendingCheckForLongPress == null) {
16160 mPendingCheckForLongPress = new CheckForLongPress();
16161 }
16162 mPendingCheckForLongPress.rememberWindowAttachCount();
16163 postDelayed(mPendingCheckForLongPress,
16164 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016166 }
16167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016168 /**
16169 * Inflate a view from an XML resource. This convenience method wraps the {@link
16170 * LayoutInflater} class, which provides a full range of options for view inflation.
16171 *
16172 * @param context The Context object for your activity or application.
16173 * @param resource The resource ID to inflate
16174 * @param root A view group that will be the parent. Used to properly inflate the
16175 * layout_* parameters.
16176 * @see LayoutInflater
16177 */
16178 public static View inflate(Context context, int resource, ViewGroup root) {
16179 LayoutInflater factory = LayoutInflater.from(context);
16180 return factory.inflate(resource, root);
16181 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016183 /**
Adam Powell637d3372010-08-25 14:37:03 -070016184 * Scroll the view with standard behavior for scrolling beyond the normal
16185 * content boundaries. Views that call this method should override
16186 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16187 * results of an over-scroll operation.
16188 *
16189 * Views can use this method to handle any touch or fling-based scrolling.
16190 *
16191 * @param deltaX Change in X in pixels
16192 * @param deltaY Change in Y in pixels
16193 * @param scrollX Current X scroll value in pixels before applying deltaX
16194 * @param scrollY Current Y scroll value in pixels before applying deltaY
16195 * @param scrollRangeX Maximum content scroll range along the X axis
16196 * @param scrollRangeY Maximum content scroll range along the Y axis
16197 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16198 * along the X axis.
16199 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16200 * along the Y axis.
16201 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16202 * @return true if scrolling was clamped to an over-scroll boundary along either
16203 * axis, false otherwise.
16204 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016205 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016206 protected boolean overScrollBy(int deltaX, int deltaY,
16207 int scrollX, int scrollY,
16208 int scrollRangeX, int scrollRangeY,
16209 int maxOverScrollX, int maxOverScrollY,
16210 boolean isTouchEvent) {
16211 final int overScrollMode = mOverScrollMode;
16212 final boolean canScrollHorizontal =
16213 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16214 final boolean canScrollVertical =
16215 computeVerticalScrollRange() > computeVerticalScrollExtent();
16216 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16217 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16218 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16219 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16220
16221 int newScrollX = scrollX + deltaX;
16222 if (!overScrollHorizontal) {
16223 maxOverScrollX = 0;
16224 }
16225
16226 int newScrollY = scrollY + deltaY;
16227 if (!overScrollVertical) {
16228 maxOverScrollY = 0;
16229 }
16230
16231 // Clamp values if at the limits and record
16232 final int left = -maxOverScrollX;
16233 final int right = maxOverScrollX + scrollRangeX;
16234 final int top = -maxOverScrollY;
16235 final int bottom = maxOverScrollY + scrollRangeY;
16236
16237 boolean clampedX = false;
16238 if (newScrollX > right) {
16239 newScrollX = right;
16240 clampedX = true;
16241 } else if (newScrollX < left) {
16242 newScrollX = left;
16243 clampedX = true;
16244 }
16245
16246 boolean clampedY = false;
16247 if (newScrollY > bottom) {
16248 newScrollY = bottom;
16249 clampedY = true;
16250 } else if (newScrollY < top) {
16251 newScrollY = top;
16252 clampedY = true;
16253 }
16254
16255 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16256
16257 return clampedX || clampedY;
16258 }
16259
16260 /**
16261 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16262 * respond to the results of an over-scroll operation.
16263 *
16264 * @param scrollX New X scroll value in pixels
16265 * @param scrollY New Y scroll value in pixels
16266 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16267 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16268 */
16269 protected void onOverScrolled(int scrollX, int scrollY,
16270 boolean clampedX, boolean clampedY) {
16271 // Intentionally empty.
16272 }
16273
16274 /**
16275 * Returns the over-scroll mode for this view. The result will be
16276 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16277 * (allow over-scrolling only if the view content is larger than the container),
16278 * or {@link #OVER_SCROLL_NEVER}.
16279 *
16280 * @return This view's over-scroll mode.
16281 */
16282 public int getOverScrollMode() {
16283 return mOverScrollMode;
16284 }
16285
16286 /**
16287 * Set the over-scroll mode for this view. Valid over-scroll modes are
16288 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16289 * (allow over-scrolling only if the view content is larger than the container),
16290 * or {@link #OVER_SCROLL_NEVER}.
16291 *
16292 * Setting the over-scroll mode of a view will have an effect only if the
16293 * view is capable of scrolling.
16294 *
16295 * @param overScrollMode The new over-scroll mode for this view.
16296 */
16297 public void setOverScrollMode(int overScrollMode) {
16298 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16299 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16300 overScrollMode != OVER_SCROLL_NEVER) {
16301 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16302 }
16303 mOverScrollMode = overScrollMode;
16304 }
16305
16306 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016307 * Gets a scale factor that determines the distance the view should scroll
16308 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16309 * @return The vertical scroll scale factor.
16310 * @hide
16311 */
16312 protected float getVerticalScrollFactor() {
16313 if (mVerticalScrollFactor == 0) {
16314 TypedValue outValue = new TypedValue();
16315 if (!mContext.getTheme().resolveAttribute(
16316 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16317 throw new IllegalStateException(
16318 "Expected theme to define listPreferredItemHeight.");
16319 }
16320 mVerticalScrollFactor = outValue.getDimension(
16321 mContext.getResources().getDisplayMetrics());
16322 }
16323 return mVerticalScrollFactor;
16324 }
16325
16326 /**
16327 * Gets a scale factor that determines the distance the view should scroll
16328 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16329 * @return The horizontal scroll scale factor.
16330 * @hide
16331 */
16332 protected float getHorizontalScrollFactor() {
16333 // TODO: Should use something else.
16334 return getVerticalScrollFactor();
16335 }
16336
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016337 /**
16338 * Return the value specifying the text direction or policy that was set with
16339 * {@link #setTextDirection(int)}.
16340 *
16341 * @return the defined text direction. It can be one of:
16342 *
16343 * {@link #TEXT_DIRECTION_INHERIT},
16344 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16345 * {@link #TEXT_DIRECTION_ANY_RTL},
16346 * {@link #TEXT_DIRECTION_LTR},
16347 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016348 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016349 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016350 @ViewDebug.ExportedProperty(category = "text", mapping = {
16351 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16352 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16353 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16354 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16355 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16356 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16357 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016358 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016359 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016360 }
16361
16362 /**
16363 * Set the text direction.
16364 *
16365 * @param textDirection the direction to set. Should be one of:
16366 *
16367 * {@link #TEXT_DIRECTION_INHERIT},
16368 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16369 * {@link #TEXT_DIRECTION_ANY_RTL},
16370 * {@link #TEXT_DIRECTION_LTR},
16371 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016372 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016373 */
16374 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016375 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016376 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016377 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016378 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016379 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016380 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016381 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016382 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016383 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016384 }
16385 }
16386
16387 /**
16388 * Return the resolved text direction.
16389 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016390 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16391 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16392 * up the parent chain of the view. if there is no parent, then it will return the default
16393 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16394 *
16395 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016396 *
Doug Feltcb3791202011-07-07 11:57:48 -070016397 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16398 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016399 * {@link #TEXT_DIRECTION_LTR},
16400 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016401 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016402 */
16403 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016404 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016405 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016406 resolveTextDirection();
16407 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016408 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016409 }
16410
16411 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016412 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16413 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016414 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016415 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016416 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016417 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016418
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016419 if (hasRtlSupport()) {
16420 // Set resolved text direction flag depending on text direction flag
16421 final int textDirection = getTextDirection();
16422 switch(textDirection) {
16423 case TEXT_DIRECTION_INHERIT:
16424 if (canResolveTextDirection()) {
16425 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016426
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016427 // Set current resolved direction to the same value as the parent's one
16428 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16429 switch (parentResolvedDirection) {
16430 case TEXT_DIRECTION_FIRST_STRONG:
16431 case TEXT_DIRECTION_ANY_RTL:
16432 case TEXT_DIRECTION_LTR:
16433 case TEXT_DIRECTION_RTL:
16434 case TEXT_DIRECTION_LOCALE:
16435 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016436 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016437 break;
16438 default:
16439 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016440 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016441 }
16442 } else {
16443 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016444 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016445 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016446 break;
16447 case TEXT_DIRECTION_FIRST_STRONG:
16448 case TEXT_DIRECTION_ANY_RTL:
16449 case TEXT_DIRECTION_LTR:
16450 case TEXT_DIRECTION_RTL:
16451 case TEXT_DIRECTION_LOCALE:
16452 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016453 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016454 break;
16455 default:
16456 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016457 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016458 }
16459 } else {
16460 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016461 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016462 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016463
16464 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016465 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016466 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016467 }
16468
16469 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016470 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016471 * resolution should override this method.
16472 *
16473 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016474 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016475 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016476 }
16477
16478 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016479 * Check if text direction resolution can be done.
16480 *
16481 * @return true if text direction resolution can be done otherwise return false.
16482 */
16483 public boolean canResolveTextDirection() {
16484 switch (getTextDirection()) {
16485 case TEXT_DIRECTION_INHERIT:
16486 return (mParent != null) && (mParent instanceof ViewGroup);
16487 default:
16488 return true;
16489 }
16490 }
16491
16492 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016493 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016494 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016495 * reset is done.
16496 */
16497 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016498 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016499 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016500 }
16501
16502 /**
16503 * Called when text direction is reset. Subclasses that care about text direction reset should
16504 * override this method and do a reset of the text direction of their children. The default
16505 * implementation does nothing.
16506 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016507 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016508 }
16509
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016510 /**
16511 * Return the value specifying the text alignment or policy that was set with
16512 * {@link #setTextAlignment(int)}.
16513 *
16514 * @return the defined text alignment. It can be one of:
16515 *
16516 * {@link #TEXT_ALIGNMENT_INHERIT},
16517 * {@link #TEXT_ALIGNMENT_GRAVITY},
16518 * {@link #TEXT_ALIGNMENT_CENTER},
16519 * {@link #TEXT_ALIGNMENT_TEXT_START},
16520 * {@link #TEXT_ALIGNMENT_TEXT_END},
16521 * {@link #TEXT_ALIGNMENT_VIEW_START},
16522 * {@link #TEXT_ALIGNMENT_VIEW_END}
16523 */
16524 @ViewDebug.ExportedProperty(category = "text", mapping = {
16525 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16526 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16527 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16528 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16529 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16530 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16531 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16532 })
16533 public int getTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016534 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016535 }
16536
16537 /**
16538 * Set the text alignment.
16539 *
16540 * @param textAlignment The text alignment to set. Should be one of
16541 *
16542 * {@link #TEXT_ALIGNMENT_INHERIT},
16543 * {@link #TEXT_ALIGNMENT_GRAVITY},
16544 * {@link #TEXT_ALIGNMENT_CENTER},
16545 * {@link #TEXT_ALIGNMENT_TEXT_START},
16546 * {@link #TEXT_ALIGNMENT_TEXT_END},
16547 * {@link #TEXT_ALIGNMENT_VIEW_START},
16548 * {@link #TEXT_ALIGNMENT_VIEW_END}
16549 *
16550 * @attr ref android.R.styleable#View_textAlignment
16551 */
16552 public void setTextAlignment(int textAlignment) {
16553 if (textAlignment != getTextAlignment()) {
16554 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016555 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016556 resetResolvedTextAlignment();
16557 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016558 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016559 // Refresh
16560 requestLayout();
16561 invalidate(true);
16562 }
16563 }
16564
16565 /**
16566 * Return the resolved text alignment.
16567 *
16568 * The resolved text alignment. This needs resolution if the value is
16569 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16570 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16571 *
16572 * @return the resolved text alignment. Returns one of:
16573 *
16574 * {@link #TEXT_ALIGNMENT_GRAVITY},
16575 * {@link #TEXT_ALIGNMENT_CENTER},
16576 * {@link #TEXT_ALIGNMENT_TEXT_START},
16577 * {@link #TEXT_ALIGNMENT_TEXT_END},
16578 * {@link #TEXT_ALIGNMENT_VIEW_START},
16579 * {@link #TEXT_ALIGNMENT_VIEW_END}
16580 */
16581 @ViewDebug.ExportedProperty(category = "text", mapping = {
16582 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16583 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16584 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16585 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16586 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16587 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16588 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16589 })
16590 public int getResolvedTextAlignment() {
16591 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016592 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016593 resolveTextAlignment();
16594 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016595 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016596 }
16597
16598 /**
16599 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16600 * resolution is done.
16601 */
16602 public void resolveTextAlignment() {
16603 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016604 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016605
16606 if (hasRtlSupport()) {
16607 // Set resolved text alignment flag depending on text alignment flag
16608 final int textAlignment = getTextAlignment();
16609 switch (textAlignment) {
16610 case TEXT_ALIGNMENT_INHERIT:
16611 // Check if we can resolve the text alignment
16612 if (canResolveLayoutDirection() && mParent instanceof View) {
16613 View view = (View) mParent;
16614
16615 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16616 switch (parentResolvedTextAlignment) {
16617 case TEXT_ALIGNMENT_GRAVITY:
16618 case TEXT_ALIGNMENT_TEXT_START:
16619 case TEXT_ALIGNMENT_TEXT_END:
16620 case TEXT_ALIGNMENT_CENTER:
16621 case TEXT_ALIGNMENT_VIEW_START:
16622 case TEXT_ALIGNMENT_VIEW_END:
16623 // Resolved text alignment is the same as the parent resolved
16624 // text alignment
16625 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016626 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016627 break;
16628 default:
16629 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016630 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016631 }
16632 }
16633 else {
16634 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016635 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016636 }
16637 break;
16638 case TEXT_ALIGNMENT_GRAVITY:
16639 case TEXT_ALIGNMENT_TEXT_START:
16640 case TEXT_ALIGNMENT_TEXT_END:
16641 case TEXT_ALIGNMENT_CENTER:
16642 case TEXT_ALIGNMENT_VIEW_START:
16643 case TEXT_ALIGNMENT_VIEW_END:
16644 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016645 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016646 break;
16647 default:
16648 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016649 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016650 }
16651 } else {
16652 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016653 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016654 }
16655
16656 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016657 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016658 onResolvedTextAlignmentChanged();
16659 }
16660
16661 /**
16662 * Check if text alignment resolution can be done.
16663 *
16664 * @return true if text alignment resolution can be done otherwise return false.
16665 */
16666 public boolean canResolveTextAlignment() {
16667 switch (getTextAlignment()) {
16668 case TEXT_DIRECTION_INHERIT:
16669 return (mParent != null);
16670 default:
16671 return true;
16672 }
16673 }
16674
16675 /**
16676 * Called when text alignment has been resolved. Subclasses that care about text alignment
16677 * resolution should override this method.
16678 *
16679 * The default implementation does nothing.
16680 */
16681 public void onResolvedTextAlignmentChanged() {
16682 }
16683
16684 /**
16685 * Reset resolved text alignment. Text alignment can be resolved with a call to
16686 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16687 * reset is done.
16688 */
16689 public void resetResolvedTextAlignment() {
16690 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016691 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016692 onResolvedTextAlignmentReset();
16693 }
16694
16695 /**
16696 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16697 * override this method and do a reset of the text alignment of their children. The default
16698 * implementation does nothing.
16699 */
16700 public void onResolvedTextAlignmentReset() {
16701 }
16702
Adam Powella9108a22012-07-18 11:18:09 -070016703 /**
16704 * Generate a value suitable for use in {@link #setId(int)}.
16705 * This value will not collide with ID values generated at build time by aapt for R.id.
16706 *
16707 * @return a generated ID value
16708 */
16709 public static int generateViewId() {
16710 for (;;) {
16711 final int result = sNextGeneratedId.get();
16712 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16713 int newValue = result + 1;
16714 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16715 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16716 return result;
16717 }
16718 }
16719 }
16720
Chet Haaseb39f0512011-05-24 14:36:40 -070016721 //
16722 // Properties
16723 //
16724 /**
16725 * A Property wrapper around the <code>alpha</code> functionality handled by the
16726 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16727 */
Chet Haased47f1532011-12-16 11:18:52 -080016728 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016729 @Override
16730 public void setValue(View object, float value) {
16731 object.setAlpha(value);
16732 }
16733
16734 @Override
16735 public Float get(View object) {
16736 return object.getAlpha();
16737 }
16738 };
16739
16740 /**
16741 * A Property wrapper around the <code>translationX</code> functionality handled by the
16742 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16743 */
Chet Haased47f1532011-12-16 11:18:52 -080016744 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016745 @Override
16746 public void setValue(View object, float value) {
16747 object.setTranslationX(value);
16748 }
16749
16750 @Override
16751 public Float get(View object) {
16752 return object.getTranslationX();
16753 }
16754 };
16755
16756 /**
16757 * A Property wrapper around the <code>translationY</code> functionality handled by the
16758 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16759 */
Chet Haased47f1532011-12-16 11:18:52 -080016760 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016761 @Override
16762 public void setValue(View object, float value) {
16763 object.setTranslationY(value);
16764 }
16765
16766 @Override
16767 public Float get(View object) {
16768 return object.getTranslationY();
16769 }
16770 };
16771
16772 /**
16773 * A Property wrapper around the <code>x</code> functionality handled by the
16774 * {@link View#setX(float)} and {@link View#getX()} methods.
16775 */
Chet Haased47f1532011-12-16 11:18:52 -080016776 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016777 @Override
16778 public void setValue(View object, float value) {
16779 object.setX(value);
16780 }
16781
16782 @Override
16783 public Float get(View object) {
16784 return object.getX();
16785 }
16786 };
16787
16788 /**
16789 * A Property wrapper around the <code>y</code> functionality handled by the
16790 * {@link View#setY(float)} and {@link View#getY()} methods.
16791 */
Chet Haased47f1532011-12-16 11:18:52 -080016792 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016793 @Override
16794 public void setValue(View object, float value) {
16795 object.setY(value);
16796 }
16797
16798 @Override
16799 public Float get(View object) {
16800 return object.getY();
16801 }
16802 };
16803
16804 /**
16805 * A Property wrapper around the <code>rotation</code> functionality handled by the
16806 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16807 */
Chet Haased47f1532011-12-16 11:18:52 -080016808 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016809 @Override
16810 public void setValue(View object, float value) {
16811 object.setRotation(value);
16812 }
16813
16814 @Override
16815 public Float get(View object) {
16816 return object.getRotation();
16817 }
16818 };
16819
16820 /**
16821 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16822 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16823 */
Chet Haased47f1532011-12-16 11:18:52 -080016824 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016825 @Override
16826 public void setValue(View object, float value) {
16827 object.setRotationX(value);
16828 }
16829
16830 @Override
16831 public Float get(View object) {
16832 return object.getRotationX();
16833 }
16834 };
16835
16836 /**
16837 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16838 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16839 */
Chet Haased47f1532011-12-16 11:18:52 -080016840 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016841 @Override
16842 public void setValue(View object, float value) {
16843 object.setRotationY(value);
16844 }
16845
16846 @Override
16847 public Float get(View object) {
16848 return object.getRotationY();
16849 }
16850 };
16851
16852 /**
16853 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16854 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16855 */
Chet Haased47f1532011-12-16 11:18:52 -080016856 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016857 @Override
16858 public void setValue(View object, float value) {
16859 object.setScaleX(value);
16860 }
16861
16862 @Override
16863 public Float get(View object) {
16864 return object.getScaleX();
16865 }
16866 };
16867
16868 /**
16869 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16870 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16871 */
Chet Haased47f1532011-12-16 11:18:52 -080016872 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016873 @Override
16874 public void setValue(View object, float value) {
16875 object.setScaleY(value);
16876 }
16877
16878 @Override
16879 public Float get(View object) {
16880 return object.getScaleY();
16881 }
16882 };
16883
Jeff Brown33bbfd22011-02-24 20:55:35 -080016884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016885 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16886 * Each MeasureSpec represents a requirement for either the width or the height.
16887 * A MeasureSpec is comprised of a size and a mode. There are three possible
16888 * modes:
16889 * <dl>
16890 * <dt>UNSPECIFIED</dt>
16891 * <dd>
16892 * The parent has not imposed any constraint on the child. It can be whatever size
16893 * it wants.
16894 * </dd>
16895 *
16896 * <dt>EXACTLY</dt>
16897 * <dd>
16898 * The parent has determined an exact size for the child. The child is going to be
16899 * given those bounds regardless of how big it wants to be.
16900 * </dd>
16901 *
16902 * <dt>AT_MOST</dt>
16903 * <dd>
16904 * The child can be as large as it wants up to the specified size.
16905 * </dd>
16906 * </dl>
16907 *
16908 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16909 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16910 */
16911 public static class MeasureSpec {
16912 private static final int MODE_SHIFT = 30;
16913 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16914
16915 /**
16916 * Measure specification mode: The parent has not imposed any constraint
16917 * on the child. It can be whatever size it wants.
16918 */
16919 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16920
16921 /**
16922 * Measure specification mode: The parent has determined an exact size
16923 * for the child. The child is going to be given those bounds regardless
16924 * of how big it wants to be.
16925 */
16926 public static final int EXACTLY = 1 << MODE_SHIFT;
16927
16928 /**
16929 * Measure specification mode: The child can be as large as it wants up
16930 * to the specified size.
16931 */
16932 public static final int AT_MOST = 2 << MODE_SHIFT;
16933
16934 /**
16935 * Creates a measure specification based on the supplied size and mode.
16936 *
16937 * The mode must always be one of the following:
16938 * <ul>
16939 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16940 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16941 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16942 * </ul>
16943 *
16944 * @param size the size of the measure specification
16945 * @param mode the mode of the measure specification
16946 * @return the measure specification based on size and mode
16947 */
16948 public static int makeMeasureSpec(int size, int mode) {
16949 return size + mode;
16950 }
16951
16952 /**
16953 * Extracts the mode from the supplied measure specification.
16954 *
16955 * @param measureSpec the measure specification to extract the mode from
16956 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16957 * {@link android.view.View.MeasureSpec#AT_MOST} or
16958 * {@link android.view.View.MeasureSpec#EXACTLY}
16959 */
16960 public static int getMode(int measureSpec) {
16961 return (measureSpec & MODE_MASK);
16962 }
16963
16964 /**
16965 * Extracts the size from the supplied measure specification.
16966 *
16967 * @param measureSpec the measure specification to extract the size from
16968 * @return the size in pixels defined in the supplied measure specification
16969 */
16970 public static int getSize(int measureSpec) {
16971 return (measureSpec & ~MODE_MASK);
16972 }
16973
16974 /**
16975 * Returns a String representation of the specified measure
16976 * specification.
16977 *
16978 * @param measureSpec the measure specification to convert to a String
16979 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16980 */
16981 public static String toString(int measureSpec) {
16982 int mode = getMode(measureSpec);
16983 int size = getSize(measureSpec);
16984
16985 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16986
16987 if (mode == UNSPECIFIED)
16988 sb.append("UNSPECIFIED ");
16989 else if (mode == EXACTLY)
16990 sb.append("EXACTLY ");
16991 else if (mode == AT_MOST)
16992 sb.append("AT_MOST ");
16993 else
16994 sb.append(mode).append(" ");
16995
16996 sb.append(size);
16997 return sb.toString();
16998 }
16999 }
17000
17001 class CheckForLongPress implements Runnable {
17002
17003 private int mOriginalWindowAttachCount;
17004
17005 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017006 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017007 && mOriginalWindowAttachCount == mWindowAttachCount) {
17008 if (performLongClick()) {
17009 mHasPerformedLongPress = true;
17010 }
17011 }
17012 }
17013
17014 public void rememberWindowAttachCount() {
17015 mOriginalWindowAttachCount = mWindowAttachCount;
17016 }
17017 }
Joe Malin32736f02011-01-19 16:14:20 -080017018
Adam Powelle14579b2009-12-16 18:39:52 -080017019 private final class CheckForTap implements Runnable {
17020 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017021 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017022 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017023 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017024 }
17025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017026
Adam Powella35d7682010-03-12 14:48:13 -080017027 private final class PerformClick implements Runnable {
17028 public void run() {
17029 performClick();
17030 }
17031 }
17032
Dianne Hackborn63042d62011-01-26 18:56:29 -080017033 /** @hide */
17034 public void hackTurnOffWindowResizeAnim(boolean off) {
17035 mAttachInfo.mTurnOffWindowResizeAnim = off;
17036 }
Joe Malin32736f02011-01-19 16:14:20 -080017037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017038 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017039 * This method returns a ViewPropertyAnimator object, which can be used to animate
17040 * specific properties on this View.
17041 *
17042 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17043 */
17044 public ViewPropertyAnimator animate() {
17045 if (mAnimator == null) {
17046 mAnimator = new ViewPropertyAnimator(this);
17047 }
17048 return mAnimator;
17049 }
17050
17051 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017052 * Interface definition for a callback to be invoked when a hardware key event is
17053 * dispatched to this view. The callback will be invoked before the key event is
17054 * given to the view. This is only useful for hardware keyboards; a software input
17055 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017056 */
17057 public interface OnKeyListener {
17058 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017059 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017060 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017061 * <p>Key presses in software keyboards will generally NOT trigger this method,
17062 * although some may elect to do so in some situations. Do not assume a
17063 * software input method has to be key-based; even if it is, it may use key presses
17064 * in a different way than you expect, so there is no way to reliably catch soft
17065 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017066 *
17067 * @param v The view the key has been dispatched to.
17068 * @param keyCode The code for the physical key that was pressed
17069 * @param event The KeyEvent object containing full information about
17070 * the event.
17071 * @return True if the listener has consumed the event, false otherwise.
17072 */
17073 boolean onKey(View v, int keyCode, KeyEvent event);
17074 }
17075
17076 /**
17077 * Interface definition for a callback to be invoked when a touch event is
17078 * dispatched to this view. The callback will be invoked before the touch
17079 * event is given to the view.
17080 */
17081 public interface OnTouchListener {
17082 /**
17083 * Called when a touch event is dispatched to a view. This allows listeners to
17084 * get a chance to respond before the target view.
17085 *
17086 * @param v The view the touch event has been dispatched to.
17087 * @param event The MotionEvent object containing full information about
17088 * the event.
17089 * @return True if the listener has consumed the event, false otherwise.
17090 */
17091 boolean onTouch(View v, MotionEvent event);
17092 }
17093
17094 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017095 * Interface definition for a callback to be invoked when a hover event is
17096 * dispatched to this view. The callback will be invoked before the hover
17097 * event is given to the view.
17098 */
17099 public interface OnHoverListener {
17100 /**
17101 * Called when a hover event is dispatched to a view. This allows listeners to
17102 * get a chance to respond before the target view.
17103 *
17104 * @param v The view the hover event has been dispatched to.
17105 * @param event The MotionEvent object containing full information about
17106 * the event.
17107 * @return True if the listener has consumed the event, false otherwise.
17108 */
17109 boolean onHover(View v, MotionEvent event);
17110 }
17111
17112 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017113 * Interface definition for a callback to be invoked when a generic motion event is
17114 * dispatched to this view. The callback will be invoked before the generic motion
17115 * event is given to the view.
17116 */
17117 public interface OnGenericMotionListener {
17118 /**
17119 * Called when a generic motion event is dispatched to a view. This allows listeners to
17120 * get a chance to respond before the target view.
17121 *
17122 * @param v The view the generic motion event has been dispatched to.
17123 * @param event The MotionEvent object containing full information about
17124 * the event.
17125 * @return True if the listener has consumed the event, false otherwise.
17126 */
17127 boolean onGenericMotion(View v, MotionEvent event);
17128 }
17129
17130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017131 * Interface definition for a callback to be invoked when a view has been clicked and held.
17132 */
17133 public interface OnLongClickListener {
17134 /**
17135 * Called when a view has been clicked and held.
17136 *
17137 * @param v The view that was clicked and held.
17138 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017139 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017140 */
17141 boolean onLongClick(View v);
17142 }
17143
17144 /**
Chris Tate32affef2010-10-18 15:29:21 -070017145 * Interface definition for a callback to be invoked when a drag is being dispatched
17146 * to this view. The callback will be invoked before the hosting view's own
17147 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17148 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017149 *
17150 * <div class="special reference">
17151 * <h3>Developer Guides</h3>
17152 * <p>For a guide to implementing drag and drop features, read the
17153 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17154 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017155 */
17156 public interface OnDragListener {
17157 /**
17158 * Called when a drag event is dispatched to a view. This allows listeners
17159 * to get a chance to override base View behavior.
17160 *
Joe Malin32736f02011-01-19 16:14:20 -080017161 * @param v The View that received the drag event.
17162 * @param event The {@link android.view.DragEvent} object for the drag event.
17163 * @return {@code true} if the drag event was handled successfully, or {@code false}
17164 * if the drag event was not handled. Note that {@code false} will trigger the View
17165 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017166 */
17167 boolean onDrag(View v, DragEvent event);
17168 }
17169
17170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017171 * Interface definition for a callback to be invoked when the focus state of
17172 * a view changed.
17173 */
17174 public interface OnFocusChangeListener {
17175 /**
17176 * Called when the focus state of a view has changed.
17177 *
17178 * @param v The view whose state has changed.
17179 * @param hasFocus The new focus state of v.
17180 */
17181 void onFocusChange(View v, boolean hasFocus);
17182 }
17183
17184 /**
17185 * Interface definition for a callback to be invoked when a view is clicked.
17186 */
17187 public interface OnClickListener {
17188 /**
17189 * Called when a view has been clicked.
17190 *
17191 * @param v The view that was clicked.
17192 */
17193 void onClick(View v);
17194 }
17195
17196 /**
17197 * Interface definition for a callback to be invoked when the context menu
17198 * for this view is being built.
17199 */
17200 public interface OnCreateContextMenuListener {
17201 /**
17202 * Called when the context menu for this view is being built. It is not
17203 * safe to hold onto the menu after this method returns.
17204 *
17205 * @param menu The context menu that is being built
17206 * @param v The view for which the context menu is being built
17207 * @param menuInfo Extra information about the item for which the
17208 * context menu should be shown. This information will vary
17209 * depending on the class of v.
17210 */
17211 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17212 }
17213
Joe Onorato664644d2011-01-23 17:53:23 -080017214 /**
17215 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017216 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017217 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017218 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017219 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017220 */
17221 public interface OnSystemUiVisibilityChangeListener {
17222 /**
17223 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017224 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017225 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017226 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17227 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17228 * This tells you the <strong>global</strong> state of these UI visibility
17229 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017230 */
17231 public void onSystemUiVisibilityChange(int visibility);
17232 }
17233
Adam Powell4afd62b2011-02-18 15:02:18 -080017234 /**
17235 * Interface definition for a callback to be invoked when this view is attached
17236 * or detached from its window.
17237 */
17238 public interface OnAttachStateChangeListener {
17239 /**
17240 * Called when the view is attached to a window.
17241 * @param v The view that was attached
17242 */
17243 public void onViewAttachedToWindow(View v);
17244 /**
17245 * Called when the view is detached from a window.
17246 * @param v The view that was detached
17247 */
17248 public void onViewDetachedFromWindow(View v);
17249 }
17250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017251 private final class UnsetPressedState implements Runnable {
17252 public void run() {
17253 setPressed(false);
17254 }
17255 }
17256
17257 /**
17258 * Base class for derived classes that want to save and restore their own
17259 * state in {@link android.view.View#onSaveInstanceState()}.
17260 */
17261 public static class BaseSavedState extends AbsSavedState {
17262 /**
17263 * Constructor used when reading from a parcel. Reads the state of the superclass.
17264 *
17265 * @param source
17266 */
17267 public BaseSavedState(Parcel source) {
17268 super(source);
17269 }
17270
17271 /**
17272 * Constructor called by derived classes when creating their SavedState objects
17273 *
17274 * @param superState The state of the superclass of this view
17275 */
17276 public BaseSavedState(Parcelable superState) {
17277 super(superState);
17278 }
17279
17280 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17281 new Parcelable.Creator<BaseSavedState>() {
17282 public BaseSavedState createFromParcel(Parcel in) {
17283 return new BaseSavedState(in);
17284 }
17285
17286 public BaseSavedState[] newArray(int size) {
17287 return new BaseSavedState[size];
17288 }
17289 };
17290 }
17291
17292 /**
17293 * A set of information given to a view when it is attached to its parent
17294 * window.
17295 */
17296 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017297 interface Callbacks {
17298 void playSoundEffect(int effectId);
17299 boolean performHapticFeedback(int effectId, boolean always);
17300 }
17301
17302 /**
17303 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17304 * to a Handler. This class contains the target (View) to invalidate and
17305 * the coordinates of the dirty rectangle.
17306 *
17307 * For performance purposes, this class also implements a pool of up to
17308 * POOL_LIMIT objects that get reused. This reduces memory allocations
17309 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017310 */
Romain Guyd928d682009-03-31 17:52:16 -070017311 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017312 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017313 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17314 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017315 public InvalidateInfo newInstance() {
17316 return new InvalidateInfo();
17317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017318
Romain Guyd928d682009-03-31 17:52:16 -070017319 public void onAcquired(InvalidateInfo element) {
17320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017321
Romain Guyd928d682009-03-31 17:52:16 -070017322 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017323 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017324 }
17325 }, POOL_LIMIT)
17326 );
17327
17328 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017329 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017330
17331 View target;
17332
17333 int left;
17334 int top;
17335 int right;
17336 int bottom;
17337
Romain Guyd928d682009-03-31 17:52:16 -070017338 public void setNextPoolable(InvalidateInfo element) {
17339 mNext = element;
17340 }
17341
17342 public InvalidateInfo getNextPoolable() {
17343 return mNext;
17344 }
17345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017346 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017347 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017348 }
17349
17350 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017351 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017353
17354 public boolean isPooled() {
17355 return mIsPooled;
17356 }
17357
17358 public void setPooled(boolean isPooled) {
17359 mIsPooled = isPooled;
17360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017361 }
17362
17363 final IWindowSession mSession;
17364
17365 final IWindow mWindow;
17366
17367 final IBinder mWindowToken;
17368
Jeff Brown98365d72012-08-19 20:30:52 -070017369 final Display mDisplay;
17370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017371 final Callbacks mRootCallbacks;
17372
Romain Guy59a12ca2011-06-09 17:48:21 -070017373 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017375 /**
17376 * The top view of the hierarchy.
17377 */
17378 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017380 IBinder mPanelParentWindowToken;
17381 Surface mSurface;
17382
Romain Guyb051e892010-09-28 19:09:36 -070017383 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017384 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017385 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017386
Romain Guy7e4e5612012-03-05 14:37:29 -080017387 boolean mScreenOn;
17388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017390 * Scale factor used by the compatibility mode
17391 */
17392 float mApplicationScale;
17393
17394 /**
17395 * Indicates whether the application is in compatibility mode
17396 */
17397 boolean mScalingRequired;
17398
17399 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017400 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017401 */
17402 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017403
Dianne Hackborn63042d62011-01-26 18:56:29 -080017404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017405 * Left position of this view's window
17406 */
17407 int mWindowLeft;
17408
17409 /**
17410 * Top position of this view's window
17411 */
17412 int mWindowTop;
17413
17414 /**
Adam Powell26153a32010-11-08 15:22:27 -080017415 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017416 */
Adam Powell26153a32010-11-08 15:22:27 -080017417 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017418
17419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017420 * For windows that are full-screen but using insets to layout inside
17421 * of the screen decorations, these are the current insets for the
17422 * content of the window.
17423 */
17424 final Rect mContentInsets = new Rect();
17425
17426 /**
17427 * For windows that are full-screen but using insets to layout inside
17428 * of the screen decorations, these are the current insets for the
17429 * actual visible parts of the window.
17430 */
17431 final Rect mVisibleInsets = new Rect();
17432
17433 /**
17434 * The internal insets given by this window. This value is
17435 * supplied by the client (through
17436 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17437 * be given to the window manager when changed to be used in laying
17438 * out windows behind it.
17439 */
17440 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17441 = new ViewTreeObserver.InternalInsetsInfo();
17442
17443 /**
17444 * All views in the window's hierarchy that serve as scroll containers,
17445 * used to determine if the window can be resized or must be panned
17446 * to adjust for a soft input area.
17447 */
17448 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17449
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017450 final KeyEvent.DispatcherState mKeyDispatchState
17451 = new KeyEvent.DispatcherState();
17452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017453 /**
17454 * Indicates whether the view's window currently has the focus.
17455 */
17456 boolean mHasWindowFocus;
17457
17458 /**
17459 * The current visibility of the window.
17460 */
17461 int mWindowVisibility;
17462
17463 /**
17464 * Indicates the time at which drawing started to occur.
17465 */
17466 long mDrawingTime;
17467
17468 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017469 * Indicates whether or not ignoring the DIRTY_MASK flags.
17470 */
17471 boolean mIgnoreDirtyState;
17472
17473 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017474 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17475 * to avoid clearing that flag prematurely.
17476 */
17477 boolean mSetIgnoreDirtyState = false;
17478
17479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017480 * Indicates whether the view's window is currently in touch mode.
17481 */
17482 boolean mInTouchMode;
17483
17484 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017485 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017486 * the next time it performs a traversal
17487 */
17488 boolean mRecomputeGlobalAttributes;
17489
17490 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017491 * Always report new attributes at next traversal.
17492 */
17493 boolean mForceReportNewAttributes;
17494
17495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017496 * Set during a traveral if any views want to keep the screen on.
17497 */
17498 boolean mKeepScreenOn;
17499
17500 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017501 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17502 */
17503 int mSystemUiVisibility;
17504
17505 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017506 * Hack to force certain system UI visibility flags to be cleared.
17507 */
17508 int mDisabledSystemUiVisibility;
17509
17510 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017511 * Last global system UI visibility reported by the window manager.
17512 */
17513 int mGlobalSystemUiVisibility;
17514
17515 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017516 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17517 * attached.
17518 */
17519 boolean mHasSystemUiListeners;
17520
17521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017522 * Set if the visibility of any views has changed.
17523 */
17524 boolean mViewVisibilityChanged;
17525
17526 /**
17527 * Set to true if a view has been scrolled.
17528 */
17529 boolean mViewScrollChanged;
17530
17531 /**
17532 * Global to the view hierarchy used as a temporary for dealing with
17533 * x/y points in the transparent region computations.
17534 */
17535 final int[] mTransparentLocation = new int[2];
17536
17537 /**
17538 * Global to the view hierarchy used as a temporary for dealing with
17539 * x/y points in the ViewGroup.invalidateChild implementation.
17540 */
17541 final int[] mInvalidateChildLocation = new int[2];
17542
Chet Haasec3aa3612010-06-17 08:50:37 -070017543
17544 /**
17545 * Global to the view hierarchy used as a temporary for dealing with
17546 * x/y location when view is transformed.
17547 */
17548 final float[] mTmpTransformLocation = new float[2];
17549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017550 /**
17551 * The view tree observer used to dispatch global events like
17552 * layout, pre-draw, touch mode change, etc.
17553 */
17554 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17555
17556 /**
17557 * A Canvas used by the view hierarchy to perform bitmap caching.
17558 */
17559 Canvas mCanvas;
17560
17561 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017562 * The view root impl.
17563 */
17564 final ViewRootImpl mViewRootImpl;
17565
17566 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017567 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017568 * handler can be used to pump events in the UI events queue.
17569 */
17570 final Handler mHandler;
17571
17572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017573 * Temporary for use in computing invalidate rectangles while
17574 * calling up the hierarchy.
17575 */
17576 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017577
17578 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017579 * Temporary for use in computing hit areas with transformed views
17580 */
17581 final RectF mTmpTransformRect = new RectF();
17582
17583 /**
Chet Haase599913d2012-07-23 16:22:05 -070017584 * Temporary for use in transforming invalidation rect
17585 */
17586 final Matrix mTmpMatrix = new Matrix();
17587
17588 /**
17589 * Temporary for use in transforming invalidation rect
17590 */
17591 final Transformation mTmpTransformation = new Transformation();
17592
17593 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017594 * Temporary list for use in collecting focusable descendents of a view.
17595 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017596 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017598 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017599 * The id of the window for accessibility purposes.
17600 */
17601 int mAccessibilityWindowId = View.NO_ID;
17602
17603 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017604 * Whether to ingore not exposed for accessibility Views when
17605 * reporting the view tree to accessibility services.
17606 */
17607 boolean mIncludeNotImportantViews;
17608
17609 /**
17610 * The drawable for highlighting accessibility focus.
17611 */
17612 Drawable mAccessibilityFocusDrawable;
17613
17614 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017615 * Show where the margins, bounds and layout bounds are for each view.
17616 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017617 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017618
17619 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017620 * Point used to compute visible regions.
17621 */
17622 final Point mPoint = new Point();
17623
17624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017625 * Creates a new set of attachment information with the specified
17626 * events handler and thread.
17627 *
17628 * @param handler the events handler the view must use
17629 */
Jeff Brown98365d72012-08-19 20:30:52 -070017630 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017631 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017632 mSession = session;
17633 mWindow = window;
17634 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017635 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017636 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017637 mHandler = handler;
17638 mRootCallbacks = effectPlayer;
17639 }
17640 }
17641
17642 /**
17643 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17644 * is supported. This avoids keeping too many unused fields in most
17645 * instances of View.</p>
17646 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017647 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017648
Mike Cleronf116bf82009-09-27 19:14:12 -070017649 /**
17650 * Scrollbars are not visible
17651 */
17652 public static final int OFF = 0;
17653
17654 /**
17655 * Scrollbars are visible
17656 */
17657 public static final int ON = 1;
17658
17659 /**
17660 * Scrollbars are fading away
17661 */
17662 public static final int FADING = 2;
17663
17664 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017666 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017667 public int scrollBarDefaultDelayBeforeFade;
17668 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017669
17670 public int scrollBarSize;
17671 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017672 public float[] interpolatorValues;
17673 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017674
17675 public final Paint paint;
17676 public final Matrix matrix;
17677 public Shader shader;
17678
Mike Cleronf116bf82009-09-27 19:14:12 -070017679 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17680
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017681 private static final float[] OPAQUE = { 255 };
17682 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017683
Mike Cleronf116bf82009-09-27 19:14:12 -070017684 /**
17685 * When fading should start. This time moves into the future every time
17686 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17687 */
17688 public long fadeStartTime;
17689
17690
17691 /**
17692 * The current state of the scrollbars: ON, OFF, or FADING
17693 */
17694 public int state = OFF;
17695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017696 private int mLastColor;
17697
Mike Cleronf116bf82009-09-27 19:14:12 -070017698 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017699 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17700 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017701 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17702 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017703
17704 paint = new Paint();
17705 matrix = new Matrix();
17706 // use use a height of 1, and then wack the matrix each time we
17707 // actually use it.
17708 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017709 paint.setShader(shader);
17710 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017711
Mike Cleronf116bf82009-09-27 19:14:12 -070017712 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017713 }
Romain Guy8506ab42009-06-11 17:35:47 -070017714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017715 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070017716 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017717 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070017718
Romain Guyd679b572012-08-29 21:49:00 -070017719 if (color != 0) {
17720 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17721 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17722 paint.setShader(shader);
17723 // Restore the default transfer mode (src_over)
17724 paint.setXfermode(null);
17725 } else {
17726 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17727 paint.setShader(shader);
17728 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017730 }
17731 }
Joe Malin32736f02011-01-19 16:14:20 -080017732
Mike Cleronf116bf82009-09-27 19:14:12 -070017733 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017734 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017735 if (now >= fadeStartTime) {
17736
17737 // the animation fades the scrollbars out by changing
17738 // the opacity (alpha) from fully opaque to fully
17739 // transparent
17740 int nextFrame = (int) now;
17741 int framesCount = 0;
17742
17743 Interpolator interpolator = scrollBarInterpolator;
17744
17745 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017746 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017747
17748 // End transparent
17749 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017750 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017751
17752 state = FADING;
17753
17754 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017755 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017756 }
17757 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017758 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017759
Svetoslav Ganova0156172011-06-26 17:55:44 -070017760 /**
17761 * Resuable callback for sending
17762 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17763 */
17764 private class SendViewScrolledAccessibilityEvent implements Runnable {
17765 public volatile boolean mIsPending;
17766
17767 public void run() {
17768 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17769 mIsPending = false;
17770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017771 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017772
17773 /**
17774 * <p>
17775 * This class represents a delegate that can be registered in a {@link View}
17776 * to enhance accessibility support via composition rather via inheritance.
17777 * It is specifically targeted to widget developers that extend basic View
17778 * classes i.e. classes in package android.view, that would like their
17779 * applications to be backwards compatible.
17780 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017781 * <div class="special reference">
17782 * <h3>Developer Guides</h3>
17783 * <p>For more information about making applications accessible, read the
17784 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17785 * developer guide.</p>
17786 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017787 * <p>
17788 * A scenario in which a developer would like to use an accessibility delegate
17789 * is overriding a method introduced in a later API version then the minimal API
17790 * version supported by the application. For example, the method
17791 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17792 * in API version 4 when the accessibility APIs were first introduced. If a
17793 * developer would like his application to run on API version 4 devices (assuming
17794 * all other APIs used by the application are version 4 or lower) and take advantage
17795 * of this method, instead of overriding the method which would break the application's
17796 * backwards compatibility, he can override the corresponding method in this
17797 * delegate and register the delegate in the target View if the API version of
17798 * the system is high enough i.e. the API version is same or higher to the API
17799 * version that introduced
17800 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17801 * </p>
17802 * <p>
17803 * Here is an example implementation:
17804 * </p>
17805 * <code><pre><p>
17806 * if (Build.VERSION.SDK_INT >= 14) {
17807 * // If the API version is equal of higher than the version in
17808 * // which onInitializeAccessibilityNodeInfo was introduced we
17809 * // register a delegate with a customized implementation.
17810 * View view = findViewById(R.id.view_id);
17811 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17812 * public void onInitializeAccessibilityNodeInfo(View host,
17813 * AccessibilityNodeInfo info) {
17814 * // Let the default implementation populate the info.
17815 * super.onInitializeAccessibilityNodeInfo(host, info);
17816 * // Set some other information.
17817 * info.setEnabled(host.isEnabled());
17818 * }
17819 * });
17820 * }
17821 * </code></pre></p>
17822 * <p>
17823 * This delegate contains methods that correspond to the accessibility methods
17824 * in View. If a delegate has been specified the implementation in View hands
17825 * off handling to the corresponding method in this delegate. The default
17826 * implementation the delegate methods behaves exactly as the corresponding
17827 * method in View for the case of no accessibility delegate been set. Hence,
17828 * to customize the behavior of a View method, clients can override only the
17829 * corresponding delegate method without altering the behavior of the rest
17830 * accessibility related methods of the host view.
17831 * </p>
17832 */
17833 public static class AccessibilityDelegate {
17834
17835 /**
17836 * Sends an accessibility event of the given type. If accessibility is not
17837 * enabled this method has no effect.
17838 * <p>
17839 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17840 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17841 * been set.
17842 * </p>
17843 *
17844 * @param host The View hosting the delegate.
17845 * @param eventType The type of the event to send.
17846 *
17847 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17848 */
17849 public void sendAccessibilityEvent(View host, int eventType) {
17850 host.sendAccessibilityEventInternal(eventType);
17851 }
17852
17853 /**
alanv8eeefef2012-05-07 16:57:53 -070017854 * Performs the specified accessibility action on the view. For
17855 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17856 * <p>
17857 * The default implementation behaves as
17858 * {@link View#performAccessibilityAction(int, Bundle)
17859 * View#performAccessibilityAction(int, Bundle)} for the case of
17860 * no accessibility delegate been set.
17861 * </p>
17862 *
17863 * @param action The action to perform.
17864 * @return Whether the action was performed.
17865 *
17866 * @see View#performAccessibilityAction(int, Bundle)
17867 * View#performAccessibilityAction(int, Bundle)
17868 */
17869 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17870 return host.performAccessibilityActionInternal(action, args);
17871 }
17872
17873 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017874 * Sends an accessibility event. This method behaves exactly as
17875 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17876 * empty {@link AccessibilityEvent} and does not perform a check whether
17877 * accessibility is enabled.
17878 * <p>
17879 * The default implementation behaves as
17880 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17881 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17882 * the case of no accessibility delegate been set.
17883 * </p>
17884 *
17885 * @param host The View hosting the delegate.
17886 * @param event The event to send.
17887 *
17888 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17889 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17890 */
17891 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17892 host.sendAccessibilityEventUncheckedInternal(event);
17893 }
17894
17895 /**
17896 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17897 * to its children for adding their text content to the event.
17898 * <p>
17899 * The default implementation behaves as
17900 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17901 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17902 * the case of no accessibility delegate been set.
17903 * </p>
17904 *
17905 * @param host The View hosting the delegate.
17906 * @param event The event.
17907 * @return True if the event population was completed.
17908 *
17909 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17910 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17911 */
17912 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17913 return host.dispatchPopulateAccessibilityEventInternal(event);
17914 }
17915
17916 /**
17917 * Gives a chance to the host View to populate the accessibility event with its
17918 * text content.
17919 * <p>
17920 * The default implementation behaves as
17921 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17922 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17923 * the case of no accessibility delegate been set.
17924 * </p>
17925 *
17926 * @param host The View hosting the delegate.
17927 * @param event The accessibility event which to populate.
17928 *
17929 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17930 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17931 */
17932 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17933 host.onPopulateAccessibilityEventInternal(event);
17934 }
17935
17936 /**
17937 * Initializes an {@link AccessibilityEvent} with information about the
17938 * the host View which is the event source.
17939 * <p>
17940 * The default implementation behaves as
17941 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17942 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17943 * the case of no accessibility delegate been set.
17944 * </p>
17945 *
17946 * @param host The View hosting the delegate.
17947 * @param event The event to initialize.
17948 *
17949 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17950 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17951 */
17952 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17953 host.onInitializeAccessibilityEventInternal(event);
17954 }
17955
17956 /**
17957 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17958 * <p>
17959 * The default implementation behaves as
17960 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17961 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17962 * the case of no accessibility delegate been set.
17963 * </p>
17964 *
17965 * @param host The View hosting the delegate.
17966 * @param info The instance to initialize.
17967 *
17968 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17969 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17970 */
17971 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17972 host.onInitializeAccessibilityNodeInfoInternal(info);
17973 }
17974
17975 /**
17976 * Called when a child of the host View has requested sending an
17977 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17978 * to augment the event.
17979 * <p>
17980 * The default implementation behaves as
17981 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17982 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17983 * the case of no accessibility delegate been set.
17984 * </p>
17985 *
17986 * @param host The View hosting the delegate.
17987 * @param child The child which requests sending the event.
17988 * @param event The event to be sent.
17989 * @return True if the event should be sent
17990 *
17991 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17992 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17993 */
17994 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17995 AccessibilityEvent event) {
17996 return host.onRequestSendAccessibilityEventInternal(child, event);
17997 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017998
17999 /**
18000 * Gets the provider for managing a virtual view hierarchy rooted at this View
18001 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18002 * that explore the window content.
18003 * <p>
18004 * The default implementation behaves as
18005 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18006 * the case of no accessibility delegate been set.
18007 * </p>
18008 *
18009 * @return The provider.
18010 *
18011 * @see AccessibilityNodeProvider
18012 */
18013 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18014 return null;
18015 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018017}