blob: 043d1d49fa11d569e3d594cda85dcabd3d4aa44f [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;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070093import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95/**
96 * <p>
97 * This class represents the basic building block for user interface components. A View
98 * occupies a rectangular area on the screen and is responsible for drawing and
99 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700100 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
102 * are invisible containers that hold other Views (or other ViewGroups) and define
103 * their layout properties.
104 * </p>
105 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700106 * <div class="special reference">
107 * <h3>Developer Guides</h3>
108 * <p>For information about using this class to develop your application's user interface,
109 * 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 -0800110 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * <a name="Using"></a>
113 * <h3>Using Views</h3>
114 * <p>
115 * All of the views in a window are arranged in a single tree. You can add views
116 * either from code or by specifying a tree of views in one or more XML layout
117 * files. There are many specialized subclasses of views that act as controls or
118 * are capable of displaying text, images, or other content.
119 * </p>
120 * <p>
121 * Once you have created a tree of views, there are typically a few types of
122 * common operations you may wish to perform:
123 * <ul>
124 * <li><strong>Set properties:</strong> for example setting the text of a
125 * {@link android.widget.TextView}. The available properties and the methods
126 * that set them will vary among the different subclasses of views. Note that
127 * properties that are known at build time can be set in the XML layout
128 * files.</li>
129 * <li><strong>Set focus:</strong> The framework will handled moving focus in
130 * response to user input. To force focus to a specific view, call
131 * {@link #requestFocus}.</li>
132 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
133 * that will be notified when something interesting happens to the view. For
134 * example, all views will let you set a listener to be notified when the view
135 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700136 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700137 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700138 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700140 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * </ul>
142 * </p>
143 * <p><em>
144 * Note: The Android framework is responsible for measuring, laying out and
145 * drawing views. You should not call methods that perform these actions on
146 * views yourself unless you are actually implementing a
147 * {@link android.view.ViewGroup}.
148 * </em></p>
149 *
150 * <a name="Lifecycle"></a>
151 * <h3>Implementing a Custom View</h3>
152 *
153 * <p>
154 * To implement a custom view, you will usually begin by providing overrides for
155 * some of the standard methods that the framework calls on all views. You do
156 * not need to override all of these methods. In fact, you can start by just
157 * overriding {@link #onDraw(android.graphics.Canvas)}.
158 * <table border="2" width="85%" align="center" cellpadding="5">
159 * <thead>
160 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
161 * </thead>
162 *
163 * <tbody>
164 * <tr>
165 * <td rowspan="2">Creation</td>
166 * <td>Constructors</td>
167 * <td>There is a form of the constructor that are called when the view
168 * is created from code and a form that is called when the view is
169 * inflated from a layout file. The second form should parse and apply
170 * any attributes defined in the layout file.
171 * </td>
172 * </tr>
173 * <tr>
174 * <td><code>{@link #onFinishInflate()}</code></td>
175 * <td>Called after a view and all of its children has been inflated
176 * from XML.</td>
177 * </tr>
178 *
179 * <tr>
180 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700181 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <td>Called to determine the size requirements for this view and all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called when this view should assign a size and position to all
189 * of its children.
190 * </td>
191 * </tr>
192 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the size of this view has changed.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when the view should render its content.
202 * </td>
203 * </tr>
204 *
205 * <tr>
206 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900208 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900213 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a trackball motion event occurs.
219 * </td>
220 * </tr>
221 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when a touch screen motion event occurs.
224 * </td>
225 * </tr>
226 *
227 * <tr>
228 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700235 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <td>Called when the window containing the view gains or loses focus.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td rowspan="3">Attaching</td>
242 * <td><code>{@link #onAttachedToWindow()}</code></td>
243 * <td>Called when the view is attached to a window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
248 * <td><code>{@link #onDetachedFromWindow}</code></td>
249 * <td>Called when the view is detached from its window.
250 * </td>
251 * </tr>
252 *
253 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700254 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * <td>Called when the visibility of the window containing the view
256 * has changed.
257 * </td>
258 * </tr>
259 * </tbody>
260 *
261 * </table>
262 * </p>
263 *
264 * <a name="IDs"></a>
265 * <h3>IDs</h3>
266 * Views may have an integer id associated with them. These ids are typically
267 * assigned in the layout XML files, and are used to find specific views within
268 * the view tree. A common pattern is to:
269 * <ul>
270 * <li>Define a Button in the layout file and assign it a unique ID.
271 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700272 * &lt;Button
273 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * android:layout_width="wrap_content"
275 * android:layout_height="wrap_content"
276 * android:text="@string/my_button_text"/&gt;
277 * </pre></li>
278 * <li>From the onCreate method of an Activity, find the Button
279 * <pre class="prettyprint">
280 * Button myButton = (Button) findViewById(R.id.my_button);
281 * </pre></li>
282 * </ul>
283 * <p>
284 * View IDs need not be unique throughout the tree, but it is good practice to
285 * ensure that they are at least unique within the part of the tree you are
286 * searching.
287 * </p>
288 *
289 * <a name="Position"></a>
290 * <h3>Position</h3>
291 * <p>
292 * The geometry of a view is that of a rectangle. A view has a location,
293 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
294 * two dimensions, expressed as a width and a height. The unit for location
295 * and dimensions is the pixel.
296 * </p>
297 *
298 * <p>
299 * It is possible to retrieve the location of a view by invoking the methods
300 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
301 * coordinate of the rectangle representing the view. The latter returns the
302 * top, or Y, coordinate of the rectangle representing the view. These methods
303 * both return the location of the view relative to its parent. For instance,
304 * when getLeft() returns 20, that means the view is located 20 pixels to the
305 * right of the left edge of its direct parent.
306 * </p>
307 *
308 * <p>
309 * In addition, several convenience methods are offered to avoid unnecessary
310 * computations, namely {@link #getRight()} and {@link #getBottom()}.
311 * These methods return the coordinates of the right and bottom edges of the
312 * rectangle representing the view. For instance, calling {@link #getRight()}
313 * is similar to the following computation: <code>getLeft() + getWidth()</code>
314 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
315 * </p>
316 *
317 * <a name="SizePaddingMargins"></a>
318 * <h3>Size, padding and margins</h3>
319 * <p>
320 * The size of a view is expressed with a width and a height. A view actually
321 * possess two pairs of width and height values.
322 * </p>
323 *
324 * <p>
325 * The first pair is known as <em>measured width</em> and
326 * <em>measured height</em>. These dimensions define how big a view wants to be
327 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
328 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
329 * and {@link #getMeasuredHeight()}.
330 * </p>
331 *
332 * <p>
333 * The second pair is simply known as <em>width</em> and <em>height</em>, or
334 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
335 * dimensions define the actual size of the view on screen, at drawing time and
336 * after layout. These values may, but do not have to, be different from the
337 * measured width and height. The width and height can be obtained by calling
338 * {@link #getWidth()} and {@link #getHeight()}.
339 * </p>
340 *
341 * <p>
342 * To measure its dimensions, a view takes into account its padding. The padding
343 * is expressed in pixels for the left, top, right and bottom parts of the view.
344 * Padding can be used to offset the content of the view by a specific amount of
345 * pixels. For instance, a left padding of 2 will push the view's content by
346 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700347 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
348 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
349 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
350 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * </p>
352 *
353 * <p>
354 * Even though a view can define a padding, it does not provide any support for
355 * margins. However, view groups provide such a support. Refer to
356 * {@link android.view.ViewGroup} and
357 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
358 * </p>
359 *
360 * <a name="Layout"></a>
361 * <h3>Layout</h3>
362 * <p>
363 * Layout is a two pass process: a measure pass and a layout pass. The measuring
364 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
365 * of the view tree. Each view pushes dimension specifications down the tree
366 * during the recursion. At the end of the measure pass, every view has stored
367 * its measurements. The second pass happens in
368 * {@link #layout(int,int,int,int)} and is also top-down. During
369 * this pass each parent is responsible for positioning all of its children
370 * using the sizes computed in the measure pass.
371 * </p>
372 *
373 * <p>
374 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
375 * {@link #getMeasuredHeight()} values must be set, along with those for all of
376 * that view's descendants. A view's measured width and measured height values
377 * must respect the constraints imposed by the view's parents. This guarantees
378 * that at the end of the measure pass, all parents accept all of their
379 * children's measurements. A parent view may call measure() more than once on
380 * its children. For example, the parent may measure each child once with
381 * unspecified dimensions to find out how big they want to be, then call
382 * measure() on them again with actual numbers if the sum of all the children's
383 * unconstrained sizes is too big or too small.
384 * </p>
385 *
386 * <p>
387 * The measure pass uses two classes to communicate dimensions. The
388 * {@link MeasureSpec} class is used by views to tell their parents how they
389 * want to be measured and positioned. The base LayoutParams class just
390 * describes how big the view wants to be for both width and height. For each
391 * dimension, it can specify one of:
392 * <ul>
393 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800394 * <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 -0800395 * (minus padding)
396 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
397 * enclose its content (plus padding).
398 * </ul>
399 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
400 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
401 * an X and Y value.
402 * </p>
403 *
404 * <p>
405 * MeasureSpecs are used to push requirements down the tree from parent to
406 * child. A MeasureSpec can be in one of three modes:
407 * <ul>
408 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
409 * of a child view. For example, a LinearLayout may call measure() on its child
410 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
411 * tall the child view wants to be given a width of 240 pixels.
412 * <li>EXACTLY: This is used by the parent to impose an exact size on the
413 * child. The child must use this size, and guarantee that all of its
414 * descendants will fit within this size.
415 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
416 * child. The child must gurantee that it and all of its descendants will fit
417 * within this size.
418 * </ul>
419 * </p>
420 *
421 * <p>
422 * To intiate a layout, call {@link #requestLayout}. This method is typically
423 * called by a view on itself when it believes that is can no longer fit within
424 * its current bounds.
425 * </p>
426 *
427 * <a name="Drawing"></a>
428 * <h3>Drawing</h3>
429 * <p>
430 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700431 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * this means that parents will draw before (i.e., behind) their children, with
433 * siblings drawn in the order they appear in the tree.
434 * If you set a background drawable for a View, then the View will draw it for you
435 * before calling back to its <code>onDraw()</code> method.
436 * </p>
437 *
438 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700439 * 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 -0800440 * </p>
441 *
442 * <p>
443 * To force a view to draw, call {@link #invalidate()}.
444 * </p>
445 *
446 * <a name="EventHandlingThreading"></a>
447 * <h3>Event Handling and Threading</h3>
448 * <p>
449 * The basic cycle of a view is as follows:
450 * <ol>
451 * <li>An event comes in and is dispatched to the appropriate view. The view
452 * handles the event and notifies any listeners.</li>
453 * <li>If in the course of processing the event, the view's bounds may need
454 * to be changed, the view will call {@link #requestLayout()}.</li>
455 * <li>Similarly, if in the course of processing the event the view's appearance
456 * may need to be changed, the view will call {@link #invalidate()}.</li>
457 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
458 * the framework will take care of measuring, laying out, and drawing the tree
459 * as appropriate.</li>
460 * </ol>
461 * </p>
462 *
463 * <p><em>Note: The entire view tree is single threaded. You must always be on
464 * the UI thread when calling any method on any view.</em>
465 * If you are doing work on other threads and want to update the state of a view
466 * from that thread, you should use a {@link Handler}.
467 * </p>
468 *
469 * <a name="FocusHandling"></a>
470 * <h3>Focus Handling</h3>
471 * <p>
472 * The framework will handle routine focus movement in response to user input.
473 * This includes changing the focus as views are removed or hidden, or as new
474 * views become available. Views indicate their willingness to take focus
475 * through the {@link #isFocusable} method. To change whether a view can take
476 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
477 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
478 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
479 * </p>
480 * <p>
481 * Focus movement is based on an algorithm which finds the nearest neighbor in a
482 * given direction. In rare cases, the default algorithm may not match the
483 * intended behavior of the developer. In these situations, you can provide
484 * explicit overrides by using these XML attributes in the layout file:
485 * <pre>
486 * nextFocusDown
487 * nextFocusLeft
488 * nextFocusRight
489 * nextFocusUp
490 * </pre>
491 * </p>
492 *
493 *
494 * <p>
495 * To get a particular view to take focus, call {@link #requestFocus()}.
496 * </p>
497 *
498 * <a name="TouchMode"></a>
499 * <h3>Touch Mode</h3>
500 * <p>
501 * When a user is navigating a user interface via directional keys such as a D-pad, it is
502 * necessary to give focus to actionable items such as buttons so the user can see
503 * what will take input. If the device has touch capabilities, however, and the user
504 * begins interacting with the interface by touching it, it is no longer necessary to
505 * always highlight, or give focus to, a particular view. This motivates a mode
506 * for interaction named 'touch mode'.
507 * </p>
508 * <p>
509 * For a touch capable device, once the user touches the screen, the device
510 * will enter touch mode. From this point onward, only views for which
511 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
512 * Other views that are touchable, like buttons, will not take focus when touched; they will
513 * only fire the on click listeners.
514 * </p>
515 * <p>
516 * Any time a user hits a directional key, such as a D-pad direction, the view device will
517 * exit touch mode, and find a view to take focus, so that the user may resume interacting
518 * with the user interface without touching the screen again.
519 * </p>
520 * <p>
521 * The touch mode state is maintained across {@link android.app.Activity}s. Call
522 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
523 * </p>
524 *
525 * <a name="Scrolling"></a>
526 * <h3>Scrolling</h3>
527 * <p>
528 * The framework provides basic support for views that wish to internally
529 * scroll their content. This includes keeping track of the X and Y scroll
530 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800531 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700532 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * </p>
534 *
535 * <a name="Tags"></a>
536 * <h3>Tags</h3>
537 * <p>
538 * Unlike IDs, tags are not used to identify views. Tags are essentially an
539 * extra piece of information that can be associated with a view. They are most
540 * often used as a convenience to store data related to views in the views
541 * themselves rather than by putting them in a separate structure.
542 * </p>
543 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700544 * <a name="Properties"></a>
545 * <h3>Properties</h3>
546 * <p>
547 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
548 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
549 * available both in the {@link Property} form as well as in similarly-named setter/getter
550 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
551 * be used to set persistent state associated with these rendering-related properties on the view.
552 * The properties and methods can also be used in conjunction with
553 * {@link android.animation.Animator Animator}-based animations, described more in the
554 * <a href="#Animation">Animation</a> section.
555 * </p>
556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 * <a name="Animation"></a>
558 * <h3>Animation</h3>
559 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700560 * Starting with Android 3.0, the preferred way of animating views is to use the
561 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
562 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
563 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
564 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
565 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
566 * makes animating these View properties particularly easy and efficient.
567 * </p>
568 * <p>
569 * 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 -0800570 * You can attach an {@link Animation} object to a view using
571 * {@link #setAnimation(Animation)} or
572 * {@link #startAnimation(Animation)}. The animation can alter the scale,
573 * rotation, translation and alpha of a view over time. If the animation is
574 * attached to a view that has children, the animation will affect the entire
575 * subtree rooted by that node. When an animation is started, the framework will
576 * take care of redrawing the appropriate views until the animation completes.
577 * </p>
578 *
Jeff Brown85a31762010-09-01 17:01:00 -0700579 * <a name="Security"></a>
580 * <h3>Security</h3>
581 * <p>
582 * Sometimes it is essential that an application be able to verify that an action
583 * is being performed with the full knowledge and consent of the user, such as
584 * granting a permission request, making a purchase or clicking on an advertisement.
585 * Unfortunately, a malicious application could try to spoof the user into
586 * performing these actions, unaware, by concealing the intended purpose of the view.
587 * As a remedy, the framework offers a touch filtering mechanism that can be used to
588 * improve the security of views that provide access to sensitive functionality.
589 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700590 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800591 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700592 * will discard touches that are received whenever the view's window is obscured by
593 * another visible window. As a result, the view will not receive touches whenever a
594 * toast, dialog or other window appears above the view's window.
595 * </p><p>
596 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700597 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
598 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700599 * </p>
600 *
Romain Guy171c5922011-01-06 10:04:23 -0800601 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_background
603 * @attr ref android.R.styleable#View_clickable
604 * @attr ref android.R.styleable#View_contentDescription
605 * @attr ref android.R.styleable#View_drawingCacheQuality
606 * @attr ref android.R.styleable#View_duplicateParentState
607 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700608 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700609 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700611 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_isScrollContainer
614 * @attr ref android.R.styleable#View_focusable
615 * @attr ref android.R.styleable#View_focusableInTouchMode
616 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
617 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800618 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_longClickable
620 * @attr ref android.R.styleable#View_minHeight
621 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @attr ref android.R.styleable#View_nextFocusDown
623 * @attr ref android.R.styleable#View_nextFocusLeft
624 * @attr ref android.R.styleable#View_nextFocusRight
625 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_onClick
627 * @attr ref android.R.styleable#View_padding
628 * @attr ref android.R.styleable#View_paddingBottom
629 * @attr ref android.R.styleable#View_paddingLeft
630 * @attr ref android.R.styleable#View_paddingRight
631 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800632 * @attr ref android.R.styleable#View_paddingStart
633 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800635 * @attr ref android.R.styleable#View_rotation
636 * @attr ref android.R.styleable#View_rotationX
637 * @attr ref android.R.styleable#View_rotationY
638 * @attr ref android.R.styleable#View_scaleX
639 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * @attr ref android.R.styleable#View_scrollX
641 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700642 * @attr ref android.R.styleable#View_scrollbarSize
643 * @attr ref android.R.styleable#View_scrollbarStyle
644 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700645 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
646 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
648 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * @attr ref android.R.styleable#View_scrollbarThumbVertical
650 * @attr ref android.R.styleable#View_scrollbarTrackVertical
651 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
652 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700653 * @attr ref android.R.styleable#View_soundEffectsEnabled
654 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700655 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800656 * @attr ref android.R.styleable#View_transformPivotX
657 * @attr ref android.R.styleable#View_transformPivotY
658 * @attr ref android.R.styleable#View_translationX
659 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 *
662 * @see android.view.ViewGroup
663 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700664public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700665 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 private static final boolean DBG = false;
667
668 /**
669 * The logging tag used by this class with android.util.Log.
670 */
671 protected static final String VIEW_LOG_TAG = "View";
672
673 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700674 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700675 *
676 * @hide
677 */
678 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
679
680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * Used to mark a View that has no ID.
682 */
683 public static final int NO_ID = -1;
684
685 /**
686 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
687 * calling setFlags.
688 */
689 private static final int NOT_FOCUSABLE = 0x00000000;
690
691 /**
692 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
693 * setFlags.
694 */
695 private static final int FOCUSABLE = 0x00000001;
696
697 /**
698 * Mask for use with setFlags indicating bits used for focus.
699 */
700 private static final int FOCUSABLE_MASK = 0x00000001;
701
702 /**
703 * This view will adjust its padding to fit sytem windows (e.g. status bar)
704 */
705 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
706
707 /**
Scott Main812634c22011-07-27 13:22:35 -0700708 * This view is visible.
709 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
710 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 */
712 public static final int VISIBLE = 0x00000000;
713
714 /**
715 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700716 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
717 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 */
719 public static final int INVISIBLE = 0x00000004;
720
721 /**
722 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700723 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
724 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
726 public static final int GONE = 0x00000008;
727
728 /**
729 * Mask for use with setFlags indicating bits used for visibility.
730 * {@hide}
731 */
732 static final int VISIBILITY_MASK = 0x0000000C;
733
734 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
735
736 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700737 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * Use with ENABLED_MASK when calling setFlags.
739 * {@hide}
740 */
741 static final int ENABLED = 0x00000000;
742
743 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700744 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 * Use with ENABLED_MASK when calling setFlags.
746 * {@hide}
747 */
748 static final int DISABLED = 0x00000020;
749
750 /**
751 * Mask for use with setFlags indicating bits used for indicating whether
752 * this view is enabled
753 * {@hide}
754 */
755 static final int ENABLED_MASK = 0x00000020;
756
757 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700758 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
759 * called and further optimizations will be performed. It is okay to have
760 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 * {@hide}
762 */
763 static final int WILL_NOT_DRAW = 0x00000080;
764
765 /**
766 * Mask for use with setFlags indicating bits used for indicating whether
767 * this view is will draw
768 * {@hide}
769 */
770 static final int DRAW_MASK = 0x00000080;
771
772 /**
773 * <p>This view doesn't show scrollbars.</p>
774 * {@hide}
775 */
776 static final int SCROLLBARS_NONE = 0x00000000;
777
778 /**
779 * <p>This view shows horizontal scrollbars.</p>
780 * {@hide}
781 */
782 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
783
784 /**
785 * <p>This view shows vertical scrollbars.</p>
786 * {@hide}
787 */
788 static final int SCROLLBARS_VERTICAL = 0x00000200;
789
790 /**
791 * <p>Mask for use with setFlags indicating bits used for indicating which
792 * scrollbars are enabled.</p>
793 * {@hide}
794 */
795 static final int SCROLLBARS_MASK = 0x00000300;
796
Jeff Brown85a31762010-09-01 17:01:00 -0700797 /**
798 * Indicates that the view should filter touches when its window is obscured.
799 * Refer to the class comments for more information about this security feature.
800 * {@hide}
801 */
802 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
803
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700804 /**
805 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
806 * that they are optional and should be skipped if the window has
807 * requested system UI flags that ignore those insets for layout.
808 */
809 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810
811 /**
812 * <p>This view doesn't show fading edges.</p>
813 * {@hide}
814 */
815 static final int FADING_EDGE_NONE = 0x00000000;
816
817 /**
818 * <p>This view shows horizontal fading edges.</p>
819 * {@hide}
820 */
821 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
822
823 /**
824 * <p>This view shows vertical fading edges.</p>
825 * {@hide}
826 */
827 static final int FADING_EDGE_VERTICAL = 0x00002000;
828
829 /**
830 * <p>Mask for use with setFlags indicating bits used for indicating which
831 * fading edges are enabled.</p>
832 * {@hide}
833 */
834 static final int FADING_EDGE_MASK = 0x00003000;
835
836 /**
837 * <p>Indicates this view can be clicked. When clickable, a View reacts
838 * to clicks by notifying the OnClickListener.<p>
839 * {@hide}
840 */
841 static final int CLICKABLE = 0x00004000;
842
843 /**
844 * <p>Indicates this view is caching its drawing into a bitmap.</p>
845 * {@hide}
846 */
847 static final int DRAWING_CACHE_ENABLED = 0x00008000;
848
849 /**
850 * <p>Indicates that no icicle should be saved for this view.<p>
851 * {@hide}
852 */
853 static final int SAVE_DISABLED = 0x000010000;
854
855 /**
856 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
857 * property.</p>
858 * {@hide}
859 */
860 static final int SAVE_DISABLED_MASK = 0x000010000;
861
862 /**
863 * <p>Indicates that no drawing cache should ever be created for this view.<p>
864 * {@hide}
865 */
866 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
867
868 /**
869 * <p>Indicates this view can take / keep focus when int touch mode.</p>
870 * {@hide}
871 */
872 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
873
874 /**
875 * <p>Enables low quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
878
879 /**
880 * <p>Enables high quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
883
884 /**
885 * <p>Enables automatic quality mode for the drawing cache.</p>
886 */
887 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
888
889 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
890 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
891 };
892
893 /**
894 * <p>Mask for use with setFlags indicating bits used for the cache
895 * quality property.</p>
896 * {@hide}
897 */
898 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
899
900 /**
901 * <p>
902 * Indicates this view can be long clicked. When long clickable, a View
903 * reacts to long clicks by notifying the OnLongClickListener or showing a
904 * context menu.
905 * </p>
906 * {@hide}
907 */
908 static final int LONG_CLICKABLE = 0x00200000;
909
910 /**
911 * <p>Indicates that this view gets its drawable states from its direct parent
912 * and ignores its original internal states.</p>
913 *
914 * @hide
915 */
916 static final int DUPLICATE_PARENT_STATE = 0x00400000;
917
918 /**
919 * The scrollbar style to display the scrollbars inside the content area,
920 * without increasing the padding. The scrollbars will be overlaid with
921 * translucency on the view's content.
922 */
923 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
924
925 /**
926 * The scrollbar style to display the scrollbars inside the padded area,
927 * increasing the padding of the view. The scrollbars will not overlap the
928 * content area of the view.
929 */
930 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
931
932 /**
933 * The scrollbar style to display the scrollbars at the edge of the view,
934 * without increasing the padding. The scrollbars will be overlaid with
935 * translucency.
936 */
937 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
938
939 /**
940 * The scrollbar style to display the scrollbars at the edge of the view,
941 * increasing the padding of the view. The scrollbars will only overlap the
942 * background, if any.
943 */
944 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
945
946 /**
947 * Mask to check if the scrollbar style is overlay or inset.
948 * {@hide}
949 */
950 static final int SCROLLBARS_INSET_MASK = 0x01000000;
951
952 /**
953 * Mask to check if the scrollbar style is inside or outside.
954 * {@hide}
955 */
956 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
957
958 /**
959 * Mask for scrollbar style.
960 * {@hide}
961 */
962 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
963
964 /**
965 * View flag indicating that the screen should remain on while the
966 * window containing this view is visible to the user. This effectively
967 * takes care of automatically setting the WindowManager's
968 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
969 */
970 public static final int KEEP_SCREEN_ON = 0x04000000;
971
972 /**
973 * View flag indicating whether this view should have sound effects enabled
974 * for events such as clicking and touching.
975 */
976 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
977
978 /**
979 * View flag indicating whether this view should have haptic feedback
980 * enabled for events such as long presses.
981 */
982 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
983
984 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700985 * <p>Indicates that the view hierarchy should stop saving state when
986 * it reaches this view. If state saving is initiated immediately at
987 * the view, it will be allowed.
988 * {@hide}
989 */
990 static final int PARENT_SAVE_DISABLED = 0x20000000;
991
992 /**
993 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
994 * {@hide}
995 */
996 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
997
998 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700999 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1000 * should add all focusable Views regardless if they are focusable in touch mode.
1001 */
1002 public static final int FOCUSABLES_ALL = 0x00000000;
1003
1004 /**
1005 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1006 * should add only Views focusable in touch mode.
1007 */
1008 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1009
1010 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001011 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * item.
1013 */
1014 public static final int FOCUS_BACKWARD = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_FORWARD = 0x00000002;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_LEFT = 0x00000011;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public static final int FOCUS_UP = 0x00000021;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_RIGHT = 0x00000042;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_DOWN = 0x00000082;
1041
Svetoslav Ganov42138042012-03-20 11:51:39 -07001042 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001043 * Bits of {@link #getMeasuredWidthAndState()} and
1044 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1045 */
1046 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1047
1048 /**
1049 * Bits of {@link #getMeasuredWidthAndState()} and
1050 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1051 */
1052 public static final int MEASURED_STATE_MASK = 0xff000000;
1053
1054 /**
1055 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1056 * for functions that combine both width and height into a single int,
1057 * such as {@link #getMeasuredState()} and the childState argument of
1058 * {@link #resolveSizeAndState(int, int, int)}.
1059 */
1060 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1061
1062 /**
1063 * Bit of {@link #getMeasuredWidthAndState()} and
1064 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1065 * is smaller that the space the view would like to have.
1066 */
1067 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1068
1069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 * Base View state sets
1071 */
1072 // Singles
1073 /**
1074 * Indicates the view has no states set. States are used with
1075 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1076 * view depending on its state.
1077 *
1078 * @see android.graphics.drawable.Drawable
1079 * @see #getDrawableState()
1080 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001081 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 /**
1083 * Indicates the view is enabled. States are used with
1084 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1085 * view depending on its state.
1086 *
1087 * @see android.graphics.drawable.Drawable
1088 * @see #getDrawableState()
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is focused. States are used with
1093 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1094 * view depending on its state.
1095 *
1096 * @see android.graphics.drawable.Drawable
1097 * @see #getDrawableState()
1098 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001099 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /**
1101 * Indicates the view is selected. States are used with
1102 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1103 * view depending on its state.
1104 *
1105 * @see android.graphics.drawable.Drawable
1106 * @see #getDrawableState()
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view is pressed. States are used with
1111 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1112 * view depending on its state.
1113 *
1114 * @see android.graphics.drawable.Drawable
1115 * @see #getDrawableState()
1116 * @hide
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view's window has focus. States are used with
1121 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1122 * view depending on its state.
1123 *
1124 * @see android.graphics.drawable.Drawable
1125 * @see #getDrawableState()
1126 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001127 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 // Doubles
1129 /**
1130 * Indicates the view is enabled and has the focus.
1131 *
1132 * @see #ENABLED_STATE_SET
1133 * @see #FOCUSED_STATE_SET
1134 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001135 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 /**
1137 * Indicates the view is enabled and selected.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #SELECTED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and that its window has focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #WINDOW_FOCUSED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is focused and selected.
1152 *
1153 * @see #FOCUSED_STATE_SET
1154 * @see #SELECTED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view has the focus and that its window has the focus.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #WINDOW_FOCUSED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view is selected and that its window has the focus.
1166 *
1167 * @see #SELECTED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 // Triples
1172 /**
1173 * Indicates the view is enabled, focused and selected.
1174 *
1175 * @see #ENABLED_STATE_SET
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view is enabled, focused and its window has the focus.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #FOCUSED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is enabled, selected and its window has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is focused, selected and its window has the focus.
1198 *
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is enabled, focused, selected and its window
1206 * has the focus.
1207 *
1208 * @see #ENABLED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #WINDOW_FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and selected.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #SELECTED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed, selected and its window has the focus.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 * @see #WINDOW_FOCUSED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed and focused.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed, focused and its window has the focus.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 * @see #WINDOW_FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused and selected.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 * @see #FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, focused, selected and its window has the focus.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 * @see #SELECTED_STATE_SET
1265 * @see #WINDOW_FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed and enabled.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #ENABLED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed, enabled and its window has the focus.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 * @see #WINDOW_FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled and selected.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled, selected and its window has the
1293 * focus.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #ENABLED_STATE_SET
1297 * @see #SELECTED_STATE_SET
1298 * @see #WINDOW_FOCUSED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled and focused.
1303 *
1304 * @see #PRESSED_STATE_SET
1305 * @see #ENABLED_STATE_SET
1306 * @see #FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled, focused and its window has the
1311 * focus.
1312 *
1313 * @see #PRESSED_STATE_SET
1314 * @see #ENABLED_STATE_SET
1315 * @see #FOCUSED_STATE_SET
1316 * @see #WINDOW_FOCUSED_STATE_SET
1317 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
1320 * Indicates the view is pressed, enabled, focused and selected.
1321 *
1322 * @see #PRESSED_STATE_SET
1323 * @see #ENABLED_STATE_SET
1324 * @see #SELECTED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 /**
1329 * Indicates the view is pressed, enabled, focused, selected and its window
1330 * has the focus.
1331 *
1332 * @see #PRESSED_STATE_SET
1333 * @see #ENABLED_STATE_SET
1334 * @see #SELECTED_STATE_SET
1335 * @see #FOCUSED_STATE_SET
1336 * @see #WINDOW_FOCUSED_STATE_SET
1337 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
1340 /**
1341 * The order here is very important to {@link #getDrawableState()}
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 private static final int[][] VIEW_STATE_SETS;
1344
Romain Guyb051e892010-09-28 19:09:36 -07001345 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1346 static final int VIEW_STATE_SELECTED = 1 << 1;
1347 static final int VIEW_STATE_FOCUSED = 1 << 2;
1348 static final int VIEW_STATE_ENABLED = 1 << 3;
1349 static final int VIEW_STATE_PRESSED = 1 << 4;
1350 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001351 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001352 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001353 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1354 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001355
1356 static final int[] VIEW_STATE_IDS = new int[] {
1357 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1358 R.attr.state_selected, VIEW_STATE_SELECTED,
1359 R.attr.state_focused, VIEW_STATE_FOCUSED,
1360 R.attr.state_enabled, VIEW_STATE_ENABLED,
1361 R.attr.state_pressed, VIEW_STATE_PRESSED,
1362 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001363 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001364 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001365 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001366 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 };
1368
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001369 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1371 throw new IllegalStateException(
1372 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1373 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001374 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001375 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001379 orderedIds[i * 2] = viewState;
1380 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 }
1382 }
1383 }
Romain Guyb051e892010-09-28 19:09:36 -07001384 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1385 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1386 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 int numBits = Integer.bitCount(i);
1388 int[] set = new int[numBits];
1389 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001390 for (int j = 0; j < orderedIds.length; j += 2) {
1391 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 set[pos++] = orderedIds[j];
1393 }
1394 }
1395 VIEW_STATE_SETS[i] = set;
1396 }
1397
1398 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1399 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1400 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1401 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1403 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1404 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1406 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1410 | VIEW_STATE_FOCUSED];
1411 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1412 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1414 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1418 | VIEW_STATE_ENABLED];
1419 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1423 | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1429 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1430
1431 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1432 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1434 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1438 | VIEW_STATE_PRESSED];
1439 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1443 | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1449 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1450 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1460 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1466 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1472 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1473 | VIEW_STATE_PRESSED];
1474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001477 * Accessibility event types that are dispatched for text population.
1478 */
1479 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1480 AccessibilityEvent.TYPE_VIEW_CLICKED
1481 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1482 | AccessibilityEvent.TYPE_VIEW_SELECTED
1483 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1484 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1485 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001486 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001487 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001488 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001489 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001556 * @hide
1557 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001558 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * The view's tag.
1562 * {@hide}
1563 *
1564 * @see #setTag(Object)
1565 * @see #getTag()
1566 */
1567 protected Object mTag;
1568
1569 // for mPrivateFlags:
1570 /** {@hide} */
1571 static final int WANTS_FOCUS = 0x00000001;
1572 /** {@hide} */
1573 static final int FOCUSED = 0x00000002;
1574 /** {@hide} */
1575 static final int SELECTED = 0x00000004;
1576 /** {@hide} */
1577 static final int IS_ROOT_NAMESPACE = 0x00000008;
1578 /** {@hide} */
1579 static final int HAS_BOUNDS = 0x00000010;
1580 /** {@hide} */
1581 static final int DRAWN = 0x00000020;
1582 /**
1583 * When this flag is set, this view is running an animation on behalf of its
1584 * children and should therefore not cancel invalidate requests, even if they
1585 * lie outside of this view's bounds.
1586 *
1587 * {@hide}
1588 */
1589 static final int DRAW_ANIMATION = 0x00000040;
1590 /** {@hide} */
1591 static final int SKIP_DRAW = 0x00000080;
1592 /** {@hide} */
1593 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1594 /** {@hide} */
1595 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1596 /** {@hide} */
1597 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1598 /** {@hide} */
1599 static final int MEASURED_DIMENSION_SET = 0x00000800;
1600 /** {@hide} */
1601 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001602 /** {@hide} */
1603 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
1605 private static final int PRESSED = 0x00004000;
1606
1607 /** {@hide} */
1608 static final int DRAWING_CACHE_VALID = 0x00008000;
1609 /**
1610 * Flag used to indicate that this view should be drawn once more (and only once
1611 * more) after its animation has completed.
1612 * {@hide}
1613 */
1614 static final int ANIMATION_STARTED = 0x00010000;
1615
1616 private static final int SAVE_STATE_CALLED = 0x00020000;
1617
1618 /**
1619 * Indicates that the View returned true when onSetAlpha() was called and that
1620 * the alpha must be restored.
1621 * {@hide}
1622 */
1623 static final int ALPHA_SET = 0x00040000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER = 0x00080000;
1629
1630 /**
1631 * Set by {@link #setScrollContainer(boolean)}.
1632 */
1633 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1634
1635 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001636 * View flag indicating whether this view was invalidated (fully or partially.)
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY = 0x00200000;
1641
1642 /**
1643 * View flag indicating whether this view was invalidated by an opaque
1644 * invalidate request.
1645 *
1646 * @hide
1647 */
1648 static final int DIRTY_OPAQUE = 0x00400000;
1649
1650 /**
1651 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1652 *
1653 * @hide
1654 */
1655 static final int DIRTY_MASK = 0x00600000;
1656
1657 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001658 * Indicates whether the background is opaque.
1659 *
1660 * @hide
1661 */
1662 static final int OPAQUE_BACKGROUND = 0x00800000;
1663
1664 /**
1665 * Indicates whether the scrollbars are opaque.
1666 *
1667 * @hide
1668 */
1669 static final int OPAQUE_SCROLLBARS = 0x01000000;
1670
1671 /**
1672 * Indicates whether the view is opaque.
1673 *
1674 * @hide
1675 */
1676 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powelle14579b2009-12-16 18:39:52 -08001678 /**
1679 * Indicates a prepressed state;
1680 * the short time between ACTION_DOWN and recognizing
1681 * a 'real' press. Prepressed is used to recognize quick taps
1682 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001683 *
Adam Powelle14579b2009-12-16 18:39:52 -08001684 * @hide
1685 */
1686 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001687
Adam Powellc9fbaab2010-02-16 17:16:19 -08001688 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001689 * Indicates whether the view is temporarily detached.
1690 *
1691 * @hide
1692 */
1693 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 /**
1696 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001697 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001698 * @hide
1699 */
1700 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001701
1702 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001703 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1704 * @hide
1705 */
1706 private static final int HOVERED = 0x10000000;
1707
1708 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1710 * for transform operations
1711 *
1712 * @hide
1713 */
Adam Powellf37df072010-09-17 16:22:49 -07001714 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001715
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001716 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001717 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001718
Chet Haasefd2b0022010-08-06 13:08:56 -07001719 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001720 * Indicates that this view was specifically invalidated, not just dirtied because some
1721 * child view was invalidated. The flag is used to determine when we need to recreate
1722 * a view's display list (as opposed to just returning a reference to its existing
1723 * display list).
1724 *
1725 * @hide
1726 */
1727 static final int INVALIDATED = 0x80000000;
1728
Christopher Tate3d4bf172011-03-28 16:16:46 -07001729 /* Masks for mPrivateFlags2 */
1730
1731 /**
1732 * Indicates that this view has reported that it can accept the current drag's content.
1733 * Cleared when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_CAN_ACCEPT = 0x00000001;
1737
1738 /**
1739 * Indicates that this view is currently directly under the drag location in a
1740 * drag-and-drop operation involving content that it can accept. Cleared when
1741 * the drag exits the view, or when the drag operation concludes.
1742 * @hide
1743 */
1744 static final int DRAG_HOVERED = 0x00000002;
1745
Cibu Johny86666632010-02-22 13:01:02 -08001746 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001747 * Horizontal layout direction of this view is from Left to Right.
1748 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001749 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001750 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001751
1752 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001753 * Horizontal layout direction of this view is from Right to Left.
1754 * Use with {@link #setLayoutDirection}.
1755 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001756 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001757
1758 /**
1759 * Horizontal layout direction of this view is inherited from its parent.
1760 * Use with {@link #setLayoutDirection}.
1761 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001762 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001763
1764 /**
1765 * Horizontal layout direction of this view is from deduced from the default language
1766 * script for the locale. Use with {@link #setLayoutDirection}.
1767 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001768 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001769
1770 /**
1771 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001772 * @hide
1773 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001774 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1775
1776 /**
1777 * Mask for use with private flags indicating bits used for horizontal layout direction.
1778 * @hide
1779 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001780 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001781
1782 /**
1783 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1784 * right-to-left direction.
1785 * @hide
1786 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001787 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788
1789 /**
1790 * Indicates whether the view horizontal layout direction has been resolved.
1791 * @hide
1792 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001793 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001794
1795 /**
1796 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1797 * @hide
1798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /*
1802 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1803 * flag value.
1804 * @hide
1805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1807 LAYOUT_DIRECTION_LTR,
1808 LAYOUT_DIRECTION_RTL,
1809 LAYOUT_DIRECTION_INHERIT,
1810 LAYOUT_DIRECTION_LOCALE
1811 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812
1813 /**
1814 * Default horizontal layout direction.
1815 * @hide
1816 */
1817 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001818
Adam Powell539ee872012-02-03 19:00:49 -08001819 /**
1820 * Indicates that the view is tracking some sort of transient state
1821 * that the app should not need to be aware of, but that the framework
1822 * should take special care to preserve.
1823 *
1824 * @hide
1825 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001827
1828
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001829 /**
1830 * Text direction is inherited thru {@link ViewGroup}
1831 */
1832 public static final int TEXT_DIRECTION_INHERIT = 0;
1833
1834 /**
1835 * Text direction is using "first strong algorithm". The first strong directional character
1836 * determines the paragraph direction. If there is no strong directional character, the
1837 * paragraph direction is the view's resolved layout direction.
1838 */
1839 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1840
1841 /**
1842 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1843 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1844 * If there are neither, the paragraph direction is the view's resolved layout direction.
1845 */
1846 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1847
1848 /**
1849 * Text direction is forced to LTR.
1850 */
1851 public static final int TEXT_DIRECTION_LTR = 3;
1852
1853 /**
1854 * Text direction is forced to RTL.
1855 */
1856 public static final int TEXT_DIRECTION_RTL = 4;
1857
1858 /**
1859 * Text direction is coming from the system Locale.
1860 */
1861 public static final int TEXT_DIRECTION_LOCALE = 5;
1862
1863 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001864 * Default text direction is inherited
1865 */
1866 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1867
1868 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001869 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1870 * @hide
1871 */
1872 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1873
1874 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001875 * Mask for use with private flags indicating bits used for text direction.
1876 * @hide
1877 */
1878 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1879
1880 /**
1881 * Array of text direction flags for mapping attribute "textDirection" to correct
1882 * flag value.
1883 * @hide
1884 */
1885 private static final int[] TEXT_DIRECTION_FLAGS = {
1886 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1887 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1888 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1889 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1891 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1892 };
1893
1894 /**
1895 * Indicates whether the view text direction has been resolved.
1896 * @hide
1897 */
1898 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1899
1900 /**
1901 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1902 * @hide
1903 */
1904 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1905
1906 /**
1907 * Mask for use with private flags indicating bits used for resolved text direction.
1908 * @hide
1909 */
1910 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1911
1912 /**
1913 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1914 * @hide
1915 */
1916 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1917 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1918
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001919 /*
1920 * Default text alignment. The text alignment of this View is inherited from its parent.
1921 * Use with {@link #setTextAlignment(int)}
1922 */
1923 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1924
1925 /**
1926 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1927 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1928 *
1929 * Use with {@link #setTextAlignment(int)}
1930 */
1931 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1932
1933 /**
1934 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1935 *
1936 * Use with {@link #setTextAlignment(int)}
1937 */
1938 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1939
1940 /**
1941 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1942 *
1943 * Use with {@link #setTextAlignment(int)}
1944 */
1945 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1946
1947 /**
1948 * Center the paragraph, e.g. ALIGN_CENTER.
1949 *
1950 * Use with {@link #setTextAlignment(int)}
1951 */
1952 public static final int TEXT_ALIGNMENT_CENTER = 4;
1953
1954 /**
1955 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1956 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1957 *
1958 * Use with {@link #setTextAlignment(int)}
1959 */
1960 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1961
1962 /**
1963 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1964 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1965 *
1966 * Use with {@link #setTextAlignment(int)}
1967 */
1968 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1969
1970 /**
1971 * Default text alignment is inherited
1972 */
1973 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1974
1975 /**
1976 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1977 * @hide
1978 */
1979 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1980
1981 /**
1982 * Mask for use with private flags indicating bits used for text alignment.
1983 * @hide
1984 */
1985 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1986
1987 /**
1988 * Array of text direction flags for mapping attribute "textAlignment" to correct
1989 * flag value.
1990 * @hide
1991 */
1992 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1993 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1994 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1995 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1996 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1997 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1998 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1999 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2000 };
2001
2002 /**
2003 * Indicates whether the view text alignment has been resolved.
2004 * @hide
2005 */
2006 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2007
2008 /**
2009 * Bit shift to get the resolved text alignment.
2010 * @hide
2011 */
2012 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2013
2014 /**
2015 * Mask for use with private flags indicating bits used for text alignment.
2016 * @hide
2017 */
2018 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2019
2020 /**
2021 * Indicates whether if the view text alignment has been resolved to gravity
2022 */
2023 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2024 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2025
Svetoslav Ganov42138042012-03-20 11:51:39 -07002026 // Accessiblity constants for mPrivateFlags2
2027
2028 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002029 * Shift for the bits in {@link #mPrivateFlags2} related to the
2030 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002031 */
2032 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2033
2034 /**
2035 * Automatically determine whether a view is important for accessibility.
2036 */
2037 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2038
2039 /**
2040 * The view is important for accessibility.
2041 */
2042 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2043
2044 /**
2045 * The view is not important for accessibility.
2046 */
2047 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2048
2049 /**
2050 * The default whether the view is important for accessiblity.
2051 */
2052 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2053
2054 /**
2055 * Mask for obtainig the bits which specify how to determine
2056 * whether a view is important for accessibility.
2057 */
2058 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2059 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2060 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2061
2062 /**
2063 * Flag indicating whether a view has accessibility focus.
2064 */
2065 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2066
2067 /**
2068 * Flag indicating whether a view state for accessibility has changed.
2069 */
2070 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002071
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002072 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002073 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2074 * is used to check whether later changes to the view's transform should invalidate the
2075 * view to force the quickReject test to run again.
2076 */
Chet Haase21433372012-06-05 07:54:09 -07002077 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002078
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002079 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002080
Christopher Tate3d4bf172011-03-28 16:16:46 -07002081 /* End of masks for mPrivateFlags2 */
2082
Chet Haase21433372012-06-05 07:54:09 -07002083 /* Masks for mPrivateFlags3 */
2084
2085 /**
2086 * Flag indicating that view has a transform animation set on it. This is used to track whether
2087 * an animation is cleared between successive frames, in order to tell the associated
2088 * DisplayList to clear its animation matrix.
2089 */
2090 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2091
2092 /**
2093 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2094 * animation is cleared between successive frames, in order to tell the associated
2095 * DisplayList to restore its alpha value.
2096 */
2097 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2098
2099
2100 /* End of masks for mPrivateFlags3 */
2101
Christopher Tate3d4bf172011-03-28 16:16:46 -07002102 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2103
Chet Haasedaf98e92011-01-10 14:10:36 -08002104 /**
Adam Powell637d3372010-08-25 14:37:03 -07002105 * Always allow a user to over-scroll this view, provided it is a
2106 * view that can scroll.
2107 *
2108 * @see #getOverScrollMode()
2109 * @see #setOverScrollMode(int)
2110 */
2111 public static final int OVER_SCROLL_ALWAYS = 0;
2112
2113 /**
2114 * Allow a user to over-scroll this view only if the content is large
2115 * enough to meaningfully scroll, provided it is a view that can scroll.
2116 *
2117 * @see #getOverScrollMode()
2118 * @see #setOverScrollMode(int)
2119 */
2120 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2121
2122 /**
2123 * Never allow a user to over-scroll this view.
2124 *
2125 * @see #getOverScrollMode()
2126 * @see #setOverScrollMode(int)
2127 */
2128 public static final int OVER_SCROLL_NEVER = 2;
2129
2130 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002131 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2132 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002133 *
Joe Malin32736f02011-01-19 16:14:20 -08002134 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002135 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002136 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002137
2138 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002139 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2140 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002141 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002142 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002143 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002144 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002145 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002146 *
Joe Malin32736f02011-01-19 16:14:20 -08002147 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002148 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002149 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2150
2151 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002152 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2153 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002154 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002155 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002156 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2157 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2158 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002159 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002160 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2161 * window flags) for displaying content using every last pixel on the display.
2162 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002163 * <p>There is a limitation: because navigation controls are so important, the least user
2164 * interaction will cause them to reappear immediately. When this happens, both
2165 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2166 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002167 *
2168 * @see #setSystemUiVisibility(int)
2169 */
2170 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2171
2172 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002173 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2174 * into the normal fullscreen mode so that its content can take over the screen
2175 * while still allowing the user to interact with the application.
2176 *
2177 * <p>This has the same visual effect as
2178 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2179 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2180 * meaning that non-critical screen decorations (such as the status bar) will be
2181 * hidden while the user is in the View's window, focusing the experience on
2182 * that content. Unlike the window flag, if you are using ActionBar in
2183 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2184 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2185 * hide the action bar.
2186 *
2187 * <p>This approach to going fullscreen is best used over the window flag when
2188 * it is a transient state -- that is, the application does this at certain
2189 * points in its user interaction where it wants to allow the user to focus
2190 * on content, but not as a continuous state. For situations where the application
2191 * would like to simply stay full screen the entire time (such as a game that
2192 * wants to take over the screen), the
2193 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2194 * is usually a better approach. The state set here will be removed by the system
2195 * in various situations (such as the user moving to another application) like
2196 * the other system UI states.
2197 *
2198 * <p>When using this flag, the application should provide some easy facility
2199 * for the user to go out of it. A common example would be in an e-book
2200 * reader, where tapping on the screen brings back whatever screen and UI
2201 * decorations that had been hidden while the user was immersed in reading
2202 * the book.
2203 *
2204 * @see #setSystemUiVisibility(int)
2205 */
2206 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2207
2208 /**
2209 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2210 * flags, we would like a stable view of the content insets given to
2211 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2212 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002213 * as a continuous state. In the stock Android UI this is the space for
2214 * the system bar, nav bar, and status bar, but not more transient elements
2215 * such as an input method.
2216 *
2217 * The stable layout your UI sees is based on the system UI modes you can
2218 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2219 * then you will get a stable layout for changes of the
2220 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2221 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2222 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2223 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2224 * with a stable layout. (Note that you should avoid using
2225 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2226 *
2227 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2228 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2229 * then a hidden status bar will be considered a "stable" state for purposes
2230 * here. This allows your UI to continually hide the status bar, while still
2231 * using the system UI flags to hide the action bar while still retaining
2232 * a stable layout. Note that changing the window fullscreen flag will never
2233 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002234 *
2235 * <p>If you are using ActionBar in
2236 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2237 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2238 * insets it adds to those given to the application.
2239 */
2240 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2241
2242 /**
2243 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2244 * to be layed out as if it has requested
2245 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2246 * allows it to avoid artifacts when switching in and out of that mode, at
2247 * the expense that some of its user interface may be covered by screen
2248 * decorations when they are shown. You can perform layout of your inner
2249 * UI elements to account for the navagation system UI through the
2250 * {@link #fitSystemWindows(Rect)} method.
2251 */
2252 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2253
2254 /**
2255 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2256 * to be layed out as if it has requested
2257 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2258 * allows it to avoid artifacts when switching in and out of that mode, at
2259 * the expense that some of its user interface may be covered by screen
2260 * decorations when they are shown. You can perform layout of your inner
2261 * UI elements to account for non-fullscreen system UI through the
2262 * {@link #fitSystemWindows(Rect)} method.
2263 */
2264 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2265
2266 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002267 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2268 */
2269 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2270
2271 /**
2272 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2273 */
2274 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002275
2276 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002277 * @hide
2278 *
2279 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2280 * out of the public fields to keep the undefined bits out of the developer's way.
2281 *
2282 * Flag to make the status bar not expandable. Unless you also
2283 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2284 */
2285 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2286
2287 /**
2288 * @hide
2289 *
2290 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2291 * out of the public fields to keep the undefined bits out of the developer's way.
2292 *
2293 * Flag to hide notification icons and scrolling ticker text.
2294 */
2295 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2296
2297 /**
2298 * @hide
2299 *
2300 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2301 * out of the public fields to keep the undefined bits out of the developer's way.
2302 *
2303 * Flag to disable incoming notification alerts. This will not block
2304 * icons, but it will block sound, vibrating and other visual or aural notifications.
2305 */
2306 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2307
2308 /**
2309 * @hide
2310 *
2311 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2312 * out of the public fields to keep the undefined bits out of the developer's way.
2313 *
2314 * Flag to hide only the scrolling ticker. Note that
2315 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2316 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2317 */
2318 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2319
2320 /**
2321 * @hide
2322 *
2323 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2324 * out of the public fields to keep the undefined bits out of the developer's way.
2325 *
2326 * Flag to hide the center system info area.
2327 */
2328 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2329
2330 /**
2331 * @hide
2332 *
2333 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2334 * out of the public fields to keep the undefined bits out of the developer's way.
2335 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002336 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002337 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2338 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002339 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002340
2341 /**
2342 * @hide
2343 *
2344 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2345 * out of the public fields to keep the undefined bits out of the developer's way.
2346 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002347 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002348 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2349 */
2350 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2351
2352 /**
2353 * @hide
2354 *
2355 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2356 * out of the public fields to keep the undefined bits out of the developer's way.
2357 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002358 * Flag to hide only the clock. You might use this if your activity has
2359 * its own clock making the status bar's clock redundant.
2360 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002361 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2362
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002363 /**
2364 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002365 *
2366 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2367 * out of the public fields to keep the undefined bits out of the developer's way.
2368 *
2369 * Flag to hide only the recent apps button. Don't use this
2370 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2371 */
2372 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2373
2374 /**
2375 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002376 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002377 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002378
2379 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002380 * These are the system UI flags that can be cleared by events outside
2381 * of an application. Currently this is just the ability to tap on the
2382 * screen while hiding the navigation bar to have it return.
2383 * @hide
2384 */
2385 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002386 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2387 | SYSTEM_UI_FLAG_FULLSCREEN;
2388
2389 /**
2390 * Flags that can impact the layout in relation to system UI.
2391 */
2392 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2393 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2394 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002395
2396 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002397 * Find views that render the specified text.
2398 *
2399 * @see #findViewsWithText(ArrayList, CharSequence, int)
2400 */
2401 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2402
2403 /**
2404 * Find find views that contain the specified content description.
2405 *
2406 * @see #findViewsWithText(ArrayList, CharSequence, int)
2407 */
2408 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2409
2410 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002411 * Find views that contain {@link AccessibilityNodeProvider}. Such
2412 * a View is a root of virtual view hierarchy and may contain the searched
2413 * text. If this flag is set Views with providers are automatically
2414 * added and it is a responsibility of the client to call the APIs of
2415 * the provider to determine whether the virtual tree rooted at this View
2416 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2417 * represeting the virtual views with this text.
2418 *
2419 * @see #findViewsWithText(ArrayList, CharSequence, int)
2420 *
2421 * @hide
2422 */
2423 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2424
2425 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002426 * The undefined cursor position.
2427 */
2428 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2429
2430 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002431 * Indicates that the screen has changed state and is now off.
2432 *
2433 * @see #onScreenStateChanged(int)
2434 */
2435 public static final int SCREEN_STATE_OFF = 0x0;
2436
2437 /**
2438 * Indicates that the screen has changed state and is now on.
2439 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002440 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002441 */
2442 public static final int SCREEN_STATE_ON = 0x1;
2443
2444 /**
Adam Powell637d3372010-08-25 14:37:03 -07002445 * Controls the over-scroll mode for this view.
2446 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2447 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2448 * and {@link #OVER_SCROLL_NEVER}.
2449 */
2450 private int mOverScrollMode;
2451
2452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 * The parent this view is attached to.
2454 * {@hide}
2455 *
2456 * @see #getParent()
2457 */
2458 protected ViewParent mParent;
2459
2460 /**
2461 * {@hide}
2462 */
2463 AttachInfo mAttachInfo;
2464
2465 /**
2466 * {@hide}
2467 */
Romain Guy809a7f62009-05-14 15:44:42 -07002468 @ViewDebug.ExportedProperty(flagMapping = {
2469 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2470 name = "FORCE_LAYOUT"),
2471 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2472 name = "LAYOUT_REQUIRED"),
2473 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002474 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002475 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2476 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2477 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2478 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2479 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002481 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002482 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483
2484 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002485 * This view's request for the visibility of the status bar.
2486 * @hide
2487 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002488 @ViewDebug.ExportedProperty(flagMapping = {
2489 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2490 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2491 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2492 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2493 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2494 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2495 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2496 equals = SYSTEM_UI_FLAG_VISIBLE,
2497 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2498 })
Joe Onorato664644d2011-01-23 17:53:23 -08002499 int mSystemUiVisibility;
2500
2501 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002502 * Reference count for transient state.
2503 * @see #setHasTransientState(boolean)
2504 */
2505 int mTransientStateCount = 0;
2506
2507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 * Count of how many windows this view has been attached to.
2509 */
2510 int mWindowAttachCount;
2511
2512 /**
2513 * The layout parameters associated with this view and used by the parent
2514 * {@link android.view.ViewGroup} to determine how this view should be
2515 * laid out.
2516 * {@hide}
2517 */
2518 protected ViewGroup.LayoutParams mLayoutParams;
2519
2520 /**
2521 * The view flags hold various views states.
2522 * {@hide}
2523 */
2524 @ViewDebug.ExportedProperty
2525 int mViewFlags;
2526
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002527 static class TransformationInfo {
2528 /**
2529 * The transform matrix for the View. This transform is calculated internally
2530 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2531 * is used by default. Do *not* use this variable directly; instead call
2532 * getMatrix(), which will automatically recalculate the matrix if necessary
2533 * to get the correct matrix based on the latest rotation and scale properties.
2534 */
2535 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002536
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002537 /**
2538 * The transform matrix for the View. This transform is calculated internally
2539 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2540 * is used by default. Do *not* use this variable directly; instead call
2541 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2542 * to get the correct matrix based on the latest rotation and scale properties.
2543 */
2544 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002545
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002546 /**
2547 * An internal variable that tracks whether we need to recalculate the
2548 * transform matrix, based on whether the rotation or scaleX/Y properties
2549 * have changed since the matrix was last calculated.
2550 */
2551 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * An internal variable that tracks whether we need to recalculate the
2555 * transform matrix, based on whether the rotation or scaleX/Y properties
2556 * have changed since the matrix was last calculated.
2557 */
2558 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002559
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002560 /**
2561 * A variable that tracks whether we need to recalculate the
2562 * transform matrix, based on whether the rotation or scaleX/Y properties
2563 * have changed since the matrix was last calculated. This variable
2564 * is only valid after a call to updateMatrix() or to a function that
2565 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2566 */
2567 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002568
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002569 /**
2570 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2571 */
2572 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002573
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002574 /**
2575 * This matrix is used when computing the matrix for 3D rotations.
2576 */
2577 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002578
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002579 /**
2580 * These prev values are used to recalculate a centered pivot point when necessary. The
2581 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2582 * set), so thes values are only used then as well.
2583 */
2584 private int mPrevWidth = -1;
2585 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002586
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002587 /**
2588 * The degrees rotation around the vertical axis through the pivot point.
2589 */
2590 @ViewDebug.ExportedProperty
2591 float mRotationY = 0f;
2592
2593 /**
2594 * The degrees rotation around the horizontal axis through the pivot point.
2595 */
2596 @ViewDebug.ExportedProperty
2597 float mRotationX = 0f;
2598
2599 /**
2600 * The degrees rotation around the pivot point.
2601 */
2602 @ViewDebug.ExportedProperty
2603 float mRotation = 0f;
2604
2605 /**
2606 * The amount of translation of the object away from its left property (post-layout).
2607 */
2608 @ViewDebug.ExportedProperty
2609 float mTranslationX = 0f;
2610
2611 /**
2612 * The amount of translation of the object away from its top property (post-layout).
2613 */
2614 @ViewDebug.ExportedProperty
2615 float mTranslationY = 0f;
2616
2617 /**
2618 * The amount of scale in the x direction around the pivot point. A
2619 * value of 1 means no scaling is applied.
2620 */
2621 @ViewDebug.ExportedProperty
2622 float mScaleX = 1f;
2623
2624 /**
2625 * The amount of scale in the y direction around the pivot point. A
2626 * value of 1 means no scaling is applied.
2627 */
2628 @ViewDebug.ExportedProperty
2629 float mScaleY = 1f;
2630
2631 /**
Chet Haasea33de552012-02-03 16:28:24 -08002632 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002633 */
2634 @ViewDebug.ExportedProperty
2635 float mPivotX = 0f;
2636
2637 /**
Chet Haasea33de552012-02-03 16:28:24 -08002638 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002639 */
2640 @ViewDebug.ExportedProperty
2641 float mPivotY = 0f;
2642
2643 /**
2644 * The opacity of the View. This is a value from 0 to 1, where 0 means
2645 * completely transparent and 1 means completely opaque.
2646 */
2647 @ViewDebug.ExportedProperty
2648 float mAlpha = 1f;
2649 }
2650
2651 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002652
Joe Malin32736f02011-01-19 16:14:20 -08002653 private boolean mLastIsOpaque;
2654
Chet Haasefd2b0022010-08-06 13:08:56 -07002655 /**
2656 * Convenience value to check for float values that are close enough to zero to be considered
2657 * zero.
2658 */
Romain Guy2542d192010-08-18 11:47:12 -07002659 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002660
2661 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 * The distance in pixels from the left edge of this view's parent
2663 * to the left edge of this view.
2664 * {@hide}
2665 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002666 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 protected int mLeft;
2668 /**
2669 * The distance in pixels from the left edge of this view's parent
2670 * to the right edge of this view.
2671 * {@hide}
2672 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002673 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 protected int mRight;
2675 /**
2676 * The distance in pixels from the top edge of this view's parent
2677 * to the top edge of this view.
2678 * {@hide}
2679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002680 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 protected int mTop;
2682 /**
2683 * The distance in pixels from the top edge of this view's parent
2684 * to the bottom edge of this view.
2685 * {@hide}
2686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 protected int mBottom;
2689
2690 /**
2691 * The offset, in pixels, by which the content of this view is scrolled
2692 * horizontally.
2693 * {@hide}
2694 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002695 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 protected int mScrollX;
2697 /**
2698 * The offset, in pixels, by which the content of this view is scrolled
2699 * vertically.
2700 * {@hide}
2701 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002702 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 protected int mScrollY;
2704
2705 /**
2706 * The left padding in pixels, that is the distance in pixels between the
2707 * left edge of this view and the left edge of its content.
2708 * {@hide}
2709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002710 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 protected int mPaddingLeft;
2712 /**
2713 * The right padding in pixels, that is the distance in pixels between the
2714 * right edge of this view and the right edge of its content.
2715 * {@hide}
2716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002717 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 protected int mPaddingRight;
2719 /**
2720 * The top padding in pixels, that is the distance in pixels between the
2721 * top edge of this view and the top edge of its content.
2722 * {@hide}
2723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 protected int mPaddingTop;
2726 /**
2727 * The bottom padding in pixels, that is the distance in pixels between the
2728 * bottom edge of this view and the bottom edge of its content.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mPaddingBottom;
2733
2734 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002735 * The layout insets in pixels, that is the distance in pixels between the
2736 * visible edges of this view its bounds.
2737 */
2738 private Insets mLayoutInsets;
2739
2740 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002741 * Briefly describes the view and is primarily used for accessibility support.
2742 */
2743 private CharSequence mContentDescription;
2744
2745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002747 *
2748 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002750 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002751 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752
2753 /**
2754 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002755 *
2756 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002758 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002759 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002761 /**
Adam Powell20232d02010-12-08 21:08:53 -08002762 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002763 *
2764 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002765 */
2766 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002767 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002768
2769 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002770 * Cache the paddingStart set by the user to append to the scrollbar's size.
2771 *
2772 */
2773 @ViewDebug.ExportedProperty(category = "padding")
2774 int mUserPaddingStart;
2775
2776 /**
2777 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2778 *
2779 */
2780 @ViewDebug.ExportedProperty(category = "padding")
2781 int mUserPaddingEnd;
2782
2783 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002784 * Default undefined padding
2785 */
2786 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2787
2788 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002789 * @hide
2790 */
2791 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2792 /**
2793 * @hide
2794 */
2795 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796
Philip Milne6c8ea062012-04-03 17:38:43 -07002797 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798
2799 private int mBackgroundResource;
2800 private boolean mBackgroundSizeChanged;
2801
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002802 static class ListenerInfo {
2803 /**
2804 * Listener used to dispatch focus change events.
2805 * This field should be made private, so it is hidden from the SDK.
2806 * {@hide}
2807 */
2808 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002810 /**
2811 * Listeners for layout change events.
2812 */
2813 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002814
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002815 /**
2816 * Listeners for attach events.
2817 */
2818 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002819
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002820 /**
2821 * Listener used to dispatch click events.
2822 * This field should be made private, so it is hidden from the SDK.
2823 * {@hide}
2824 */
2825 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002827 /**
2828 * Listener used to dispatch long click events.
2829 * This field should be made private, so it is hidden from the SDK.
2830 * {@hide}
2831 */
2832 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002834 /**
2835 * Listener used to build the context menu.
2836 * This field should be made private, so it is hidden from the SDK.
2837 * {@hide}
2838 */
2839 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002841 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002845 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002846
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002847 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002848
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002849 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002851 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2852 }
2853
2854 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 /**
2857 * The application environment this view lives in.
2858 * This field should be made private, so it is hidden from the SDK.
2859 * {@hide}
2860 */
2861 protected Context mContext;
2862
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002863 private final Resources mResources;
2864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 private ScrollabilityCache mScrollCache;
2866
2867 private int[] mDrawableState = null;
2868
Romain Guy0211a0a2011-02-14 16:34:59 -08002869 /**
2870 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002871 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002872 * @hide
2873 */
2874 public boolean mCachingFailed;
2875
Romain Guy02890fd2010-08-06 17:58:44 -07002876 private Bitmap mDrawingCache;
2877 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002878 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002879 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880
2881 /**
2882 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2883 * the user may specify which view to go to next.
2884 */
2885 private int mNextFocusLeftId = View.NO_ID;
2886
2887 /**
2888 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2889 * the user may specify which view to go to next.
2890 */
2891 private int mNextFocusRightId = View.NO_ID;
2892
2893 /**
2894 * When this view has focus and the next focus is {@link #FOCUS_UP},
2895 * the user may specify which view to go to next.
2896 */
2897 private int mNextFocusUpId = View.NO_ID;
2898
2899 /**
2900 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2901 * the user may specify which view to go to next.
2902 */
2903 private int mNextFocusDownId = View.NO_ID;
2904
Jeff Brown4e6319b2010-12-13 10:36:51 -08002905 /**
2906 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2907 * the user may specify which view to go to next.
2908 */
2909 int mNextFocusForwardId = View.NO_ID;
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002912 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002913 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002914 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 private UnsetPressedState mUnsetPressedState;
2917
2918 /**
2919 * Whether the long press's action has been invoked. The tap's action is invoked on the
2920 * up event while a long press is invoked as soon as the long press duration is reached, so
2921 * a long press could be performed before the tap is checked, in which case the tap's action
2922 * should not be invoked.
2923 */
2924 private boolean mHasPerformedLongPress;
2925
2926 /**
2927 * The minimum height of the view. We'll try our best to have the height
2928 * of this view to at least this amount.
2929 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002930 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 private int mMinHeight;
2932
2933 /**
2934 * The minimum width of the view. We'll try our best to have the width
2935 * of this view to at least this amount.
2936 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002937 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 private int mMinWidth;
2939
2940 /**
2941 * The delegate to handle touch events that are physically in this view
2942 * but should be handled by another view.
2943 */
2944 private TouchDelegate mTouchDelegate = null;
2945
2946 /**
2947 * Solid color to use as a background when creating the drawing cache. Enables
2948 * the cache to use 16 bit bitmaps instead of 32 bit.
2949 */
2950 private int mDrawingCacheBackgroundColor = 0;
2951
2952 /**
2953 * Special tree observer used when mAttachInfo is null.
2954 */
2955 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002956
Adam Powelle14579b2009-12-16 18:39:52 -08002957 /**
2958 * Cache the touch slop from the context that created the view.
2959 */
2960 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002963 * Object that handles automatic animation of view properties.
2964 */
2965 private ViewPropertyAnimator mAnimator = null;
2966
2967 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002968 * Flag indicating that a drag can cross window boundaries. When
2969 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2970 * with this flag set, all visible applications will be able to participate
2971 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002972 *
2973 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002974 */
2975 public static final int DRAG_FLAG_GLOBAL = 1;
2976
2977 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002978 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2979 */
2980 private float mVerticalScrollFactor;
2981
2982 /**
Adam Powell20232d02010-12-08 21:08:53 -08002983 * Position of the vertical scroll bar.
2984 */
2985 private int mVerticalScrollbarPosition;
2986
2987 /**
2988 * Position the scroll bar at the default position as determined by the system.
2989 */
2990 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2991
2992 /**
2993 * Position the scroll bar along the left edge.
2994 */
2995 public static final int SCROLLBAR_POSITION_LEFT = 1;
2996
2997 /**
2998 * Position the scroll bar along the right edge.
2999 */
3000 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3001
3002 /**
Romain Guy171c5922011-01-06 10:04:23 -08003003 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003004 *
3005 * @see #getLayerType()
3006 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003007 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003008 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003009 */
3010 public static final int LAYER_TYPE_NONE = 0;
3011
3012 /**
3013 * <p>Indicates that the view has a software layer. A software layer is backed
3014 * by a bitmap and causes the view to be rendered using Android's software
3015 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003016 *
Romain Guy171c5922011-01-06 10:04:23 -08003017 * <p>Software layers have various usages:</p>
3018 * <p>When the application is not using hardware acceleration, a software layer
3019 * is useful to apply a specific color filter and/or blending mode and/or
3020 * translucency to a view and all its children.</p>
3021 * <p>When the application is using hardware acceleration, a software layer
3022 * is useful to render drawing primitives not supported by the hardware
3023 * accelerated pipeline. It can also be used to cache a complex view tree
3024 * into a texture and reduce the complexity of drawing operations. For instance,
3025 * when animating a complex view tree with a translation, a software layer can
3026 * be used to render the view tree only once.</p>
3027 * <p>Software layers should be avoided when the affected view tree updates
3028 * often. Every update will require to re-render the software layer, which can
3029 * potentially be slow (particularly when hardware acceleration is turned on
3030 * since the layer will have to be uploaded into a hardware texture after every
3031 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003032 *
3033 * @see #getLayerType()
3034 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003035 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003036 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003037 */
3038 public static final int LAYER_TYPE_SOFTWARE = 1;
3039
3040 /**
3041 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3042 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3043 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3044 * rendering pipeline, but only if hardware acceleration is turned on for the
3045 * view hierarchy. When hardware acceleration is turned off, hardware layers
3046 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003047 *
Romain Guy171c5922011-01-06 10:04:23 -08003048 * <p>A hardware layer is useful to apply a specific color filter and/or
3049 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003050 * <p>A hardware layer can be used to cache a complex view tree into a
3051 * texture and reduce the complexity of drawing operations. For instance,
3052 * when animating a complex view tree with a translation, a hardware layer can
3053 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003054 * <p>A hardware layer can also be used to increase the rendering quality when
3055 * rotation transformations are applied on a view. It can also be used to
3056 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003057 *
3058 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003059 * @see #setLayerType(int, android.graphics.Paint)
3060 * @see #LAYER_TYPE_NONE
3061 * @see #LAYER_TYPE_SOFTWARE
3062 */
3063 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003064
Romain Guy3aaff3a2011-01-12 14:18:47 -08003065 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3066 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3067 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3068 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3069 })
Romain Guy171c5922011-01-06 10:04:23 -08003070 int mLayerType = LAYER_TYPE_NONE;
3071 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003072 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003073
3074 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003075 * Set to true when the view is sending hover accessibility events because it
3076 * is the innermost hovered view.
3077 */
3078 private boolean mSendingHoverAccessibilityEvents;
3079
Adam Powella9108a22012-07-18 11:18:09 -07003080 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3081
Jeff Brown87b7f802011-06-21 18:35:45 -07003082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * Simple constructor to use when creating a view from code.
3084 *
3085 * @param context The Context the view is running in, through which it can
3086 * access the current theme, resources, etc.
3087 */
3088 public View(Context context) {
3089 mContext = context;
3090 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003091 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003092 // Set layout and text direction defaults
3093 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003094 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003095 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07003096 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003097 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003098 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003099 mUserPaddingStart = UNDEFINED_PADDING;
3100 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102
3103 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003104 * Delegate for injecting accessiblity functionality.
3105 */
3106 AccessibilityDelegate mAccessibilityDelegate;
3107
3108 /**
3109 * Consistency verifier for debugging purposes.
3110 * @hide
3111 */
3112 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3113 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3114 new InputEventConsistencyVerifier(this, 0) : null;
3115
3116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 * Constructor that is called when inflating a view from XML. This is called
3118 * when a view is being constructed from an XML file, supplying attributes
3119 * that were specified in the XML file. This version uses a default style of
3120 * 0, so the only attribute values applied are those in the Context's Theme
3121 * and the given AttributeSet.
3122 *
3123 * <p>
3124 * The method onFinishInflate() will be called after all children have been
3125 * added.
3126 *
3127 * @param context The Context the view is running in, through which it can
3128 * access the current theme, resources, etc.
3129 * @param attrs The attributes of the XML tag that is inflating the view.
3130 * @see #View(Context, AttributeSet, int)
3131 */
3132 public View(Context context, AttributeSet attrs) {
3133 this(context, attrs, 0);
3134 }
3135
3136 /**
3137 * Perform inflation from XML and apply a class-specific base style. This
3138 * constructor of View allows subclasses to use their own base style when
3139 * they are inflating. For example, a Button class's constructor would call
3140 * this version of the super class constructor and supply
3141 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3142 * the theme's button style to modify all of the base view attributes (in
3143 * particular its background) as well as the Button class's attributes.
3144 *
3145 * @param context The Context the view is running in, through which it can
3146 * access the current theme, resources, etc.
3147 * @param attrs The attributes of the XML tag that is inflating the view.
3148 * @param defStyle The default style to apply to this view. If 0, no style
3149 * will be applied (beyond what is included in the theme). This may
3150 * either be an attribute resource, whose value will be retrieved
3151 * from the current theme, or an explicit style resource.
3152 * @see #View(Context, AttributeSet)
3153 */
3154 public View(Context context, AttributeSet attrs, int defStyle) {
3155 this(context);
3156
3157 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3158 defStyle, 0);
3159
3160 Drawable background = null;
3161
3162 int leftPadding = -1;
3163 int topPadding = -1;
3164 int rightPadding = -1;
3165 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003166 int startPadding = UNDEFINED_PADDING;
3167 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168
3169 int padding = -1;
3170
3171 int viewFlagValues = 0;
3172 int viewFlagMasks = 0;
3173
3174 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 int x = 0;
3177 int y = 0;
3178
Chet Haase73066682010-11-29 15:55:32 -08003179 float tx = 0;
3180 float ty = 0;
3181 float rotation = 0;
3182 float rotationX = 0;
3183 float rotationY = 0;
3184 float sx = 1f;
3185 float sy = 1f;
3186 boolean transformSet = false;
3187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3189
Adam Powell637d3372010-08-25 14:37:03 -07003190 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 final int N = a.getIndexCount();
3192 for (int i = 0; i < N; i++) {
3193 int attr = a.getIndex(i);
3194 switch (attr) {
3195 case com.android.internal.R.styleable.View_background:
3196 background = a.getDrawable(attr);
3197 break;
3198 case com.android.internal.R.styleable.View_padding:
3199 padding = a.getDimensionPixelSize(attr, -1);
3200 break;
3201 case com.android.internal.R.styleable.View_paddingLeft:
3202 leftPadding = a.getDimensionPixelSize(attr, -1);
3203 break;
3204 case com.android.internal.R.styleable.View_paddingTop:
3205 topPadding = a.getDimensionPixelSize(attr, -1);
3206 break;
3207 case com.android.internal.R.styleable.View_paddingRight:
3208 rightPadding = a.getDimensionPixelSize(attr, -1);
3209 break;
3210 case com.android.internal.R.styleable.View_paddingBottom:
3211 bottomPadding = a.getDimensionPixelSize(attr, -1);
3212 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003213 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003214 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003215 break;
3216 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003217 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003218 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 case com.android.internal.R.styleable.View_scrollX:
3220 x = a.getDimensionPixelOffset(attr, 0);
3221 break;
3222 case com.android.internal.R.styleable.View_scrollY:
3223 y = a.getDimensionPixelOffset(attr, 0);
3224 break;
Chet Haase73066682010-11-29 15:55:32 -08003225 case com.android.internal.R.styleable.View_alpha:
3226 setAlpha(a.getFloat(attr, 1f));
3227 break;
3228 case com.android.internal.R.styleable.View_transformPivotX:
3229 setPivotX(a.getDimensionPixelOffset(attr, 0));
3230 break;
3231 case com.android.internal.R.styleable.View_transformPivotY:
3232 setPivotY(a.getDimensionPixelOffset(attr, 0));
3233 break;
3234 case com.android.internal.R.styleable.View_translationX:
3235 tx = a.getDimensionPixelOffset(attr, 0);
3236 transformSet = true;
3237 break;
3238 case com.android.internal.R.styleable.View_translationY:
3239 ty = a.getDimensionPixelOffset(attr, 0);
3240 transformSet = true;
3241 break;
3242 case com.android.internal.R.styleable.View_rotation:
3243 rotation = a.getFloat(attr, 0);
3244 transformSet = true;
3245 break;
3246 case com.android.internal.R.styleable.View_rotationX:
3247 rotationX = a.getFloat(attr, 0);
3248 transformSet = true;
3249 break;
3250 case com.android.internal.R.styleable.View_rotationY:
3251 rotationY = a.getFloat(attr, 0);
3252 transformSet = true;
3253 break;
3254 case com.android.internal.R.styleable.View_scaleX:
3255 sx = a.getFloat(attr, 1f);
3256 transformSet = true;
3257 break;
3258 case com.android.internal.R.styleable.View_scaleY:
3259 sy = a.getFloat(attr, 1f);
3260 transformSet = true;
3261 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 case com.android.internal.R.styleable.View_id:
3263 mID = a.getResourceId(attr, NO_ID);
3264 break;
3265 case com.android.internal.R.styleable.View_tag:
3266 mTag = a.getText(attr);
3267 break;
3268 case com.android.internal.R.styleable.View_fitsSystemWindows:
3269 if (a.getBoolean(attr, false)) {
3270 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3271 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3272 }
3273 break;
3274 case com.android.internal.R.styleable.View_focusable:
3275 if (a.getBoolean(attr, false)) {
3276 viewFlagValues |= FOCUSABLE;
3277 viewFlagMasks |= FOCUSABLE_MASK;
3278 }
3279 break;
3280 case com.android.internal.R.styleable.View_focusableInTouchMode:
3281 if (a.getBoolean(attr, false)) {
3282 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3283 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3284 }
3285 break;
3286 case com.android.internal.R.styleable.View_clickable:
3287 if (a.getBoolean(attr, false)) {
3288 viewFlagValues |= CLICKABLE;
3289 viewFlagMasks |= CLICKABLE;
3290 }
3291 break;
3292 case com.android.internal.R.styleable.View_longClickable:
3293 if (a.getBoolean(attr, false)) {
3294 viewFlagValues |= LONG_CLICKABLE;
3295 viewFlagMasks |= LONG_CLICKABLE;
3296 }
3297 break;
3298 case com.android.internal.R.styleable.View_saveEnabled:
3299 if (!a.getBoolean(attr, true)) {
3300 viewFlagValues |= SAVE_DISABLED;
3301 viewFlagMasks |= SAVE_DISABLED_MASK;
3302 }
3303 break;
3304 case com.android.internal.R.styleable.View_duplicateParentState:
3305 if (a.getBoolean(attr, false)) {
3306 viewFlagValues |= DUPLICATE_PARENT_STATE;
3307 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3308 }
3309 break;
3310 case com.android.internal.R.styleable.View_visibility:
3311 final int visibility = a.getInt(attr, 0);
3312 if (visibility != 0) {
3313 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3314 viewFlagMasks |= VISIBILITY_MASK;
3315 }
3316 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003317 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003318 // Clear any layout direction flags (included resolved bits) already set
3319 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3320 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003321 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003322 final int value = (layoutDirection != -1) ?
3323 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3324 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003325 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 case com.android.internal.R.styleable.View_drawingCacheQuality:
3327 final int cacheQuality = a.getInt(attr, 0);
3328 if (cacheQuality != 0) {
3329 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3330 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3331 }
3332 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003333 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003334 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003335 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3337 if (!a.getBoolean(attr, true)) {
3338 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3339 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3340 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003341 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3343 if (!a.getBoolean(attr, true)) {
3344 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3345 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3346 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003347 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 case R.styleable.View_scrollbars:
3349 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3350 if (scrollbars != SCROLLBARS_NONE) {
3351 viewFlagValues |= scrollbars;
3352 viewFlagMasks |= SCROLLBARS_MASK;
3353 initializeScrollbars(a);
3354 }
3355 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003356 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003358 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3359 // Ignore the attribute starting with ICS
3360 break;
3361 }
3362 // With builds < ICS, fall through and apply fading edges
3363 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3365 if (fadingEdge != FADING_EDGE_NONE) {
3366 viewFlagValues |= fadingEdge;
3367 viewFlagMasks |= FADING_EDGE_MASK;
3368 initializeFadingEdge(a);
3369 }
3370 break;
3371 case R.styleable.View_scrollbarStyle:
3372 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3373 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3374 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3375 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3376 }
3377 break;
3378 case R.styleable.View_isScrollContainer:
3379 setScrollContainer = true;
3380 if (a.getBoolean(attr, false)) {
3381 setScrollContainer(true);
3382 }
3383 break;
3384 case com.android.internal.R.styleable.View_keepScreenOn:
3385 if (a.getBoolean(attr, false)) {
3386 viewFlagValues |= KEEP_SCREEN_ON;
3387 viewFlagMasks |= KEEP_SCREEN_ON;
3388 }
3389 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003390 case R.styleable.View_filterTouchesWhenObscured:
3391 if (a.getBoolean(attr, false)) {
3392 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3393 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3394 }
3395 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 case R.styleable.View_nextFocusLeft:
3397 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3398 break;
3399 case R.styleable.View_nextFocusRight:
3400 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3401 break;
3402 case R.styleable.View_nextFocusUp:
3403 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3404 break;
3405 case R.styleable.View_nextFocusDown:
3406 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3407 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003408 case R.styleable.View_nextFocusForward:
3409 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3410 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 case R.styleable.View_minWidth:
3412 mMinWidth = a.getDimensionPixelSize(attr, 0);
3413 break;
3414 case R.styleable.View_minHeight:
3415 mMinHeight = a.getDimensionPixelSize(attr, 0);
3416 break;
Romain Guy9a817362009-05-01 10:57:14 -07003417 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003418 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003419 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003420 + "be used within a restricted context");
3421 }
3422
Romain Guy9a817362009-05-01 10:57:14 -07003423 final String handlerName = a.getString(attr);
3424 if (handlerName != null) {
3425 setOnClickListener(new OnClickListener() {
3426 private Method mHandler;
3427
3428 public void onClick(View v) {
3429 if (mHandler == null) {
3430 try {
3431 mHandler = getContext().getClass().getMethod(handlerName,
3432 View.class);
3433 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003434 int id = getId();
3435 String idText = id == NO_ID ? "" : " with id '"
3436 + getContext().getResources().getResourceEntryName(
3437 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003438 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003439 handlerName + "(View) in the activity "
3440 + getContext().getClass() + " for onClick handler"
3441 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003442 }
3443 }
3444
3445 try {
3446 mHandler.invoke(getContext(), View.this);
3447 } catch (IllegalAccessException e) {
3448 throw new IllegalStateException("Could not execute non "
3449 + "public method of the activity", e);
3450 } catch (InvocationTargetException e) {
3451 throw new IllegalStateException("Could not execute "
3452 + "method of the activity", e);
3453 }
3454 }
3455 });
3456 }
3457 break;
Adam Powell637d3372010-08-25 14:37:03 -07003458 case R.styleable.View_overScrollMode:
3459 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3460 break;
Adam Powell20232d02010-12-08 21:08:53 -08003461 case R.styleable.View_verticalScrollbarPosition:
3462 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3463 break;
Romain Guy171c5922011-01-06 10:04:23 -08003464 case R.styleable.View_layerType:
3465 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3466 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003467 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003468 // Clear any text direction flag already set
3469 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3470 // Set the text direction flags depending on the value of the attribute
3471 final int textDirection = a.getInt(attr, -1);
3472 if (textDirection != -1) {
3473 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3474 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003475 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003476 case R.styleable.View_textAlignment:
3477 // Clear any text alignment flag already set
3478 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3479 // Set the text alignment flag depending on the value of the attribute
3480 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3481 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3482 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003483 case R.styleable.View_importantForAccessibility:
3484 setImportantForAccessibility(a.getInt(attr,
3485 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003486 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003490 a.recycle();
3491
Adam Powell637d3372010-08-25 14:37:03 -07003492 setOverScrollMode(overScrollMode);
3493
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003494 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3495 // layout direction). Those cached values will be used later during padding resolution.
3496 mUserPaddingStart = startPadding;
3497 mUserPaddingEnd = endPadding;
3498
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003499 if (background != null) {
3500 setBackground(background);
3501 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 if (padding >= 0) {
3504 leftPadding = padding;
3505 topPadding = padding;
3506 rightPadding = padding;
3507 bottomPadding = padding;
3508 }
3509
3510 // If the user specified the padding (either with android:padding or
3511 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3512 // use the default padding or the padding from the background drawable
3513 // (stored at this point in mPadding*)
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003514 internalSetPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 topPadding >= 0 ? topPadding : mPaddingTop,
3516 rightPadding >= 0 ? rightPadding : mPaddingRight,
3517 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3518
3519 if (viewFlagMasks != 0) {
3520 setFlags(viewFlagValues, viewFlagMasks);
3521 }
3522
3523 // Needs to be called after mViewFlags is set
3524 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3525 recomputePadding();
3526 }
3527
3528 if (x != 0 || y != 0) {
3529 scrollTo(x, y);
3530 }
3531
Chet Haase73066682010-11-29 15:55:32 -08003532 if (transformSet) {
3533 setTranslationX(tx);
3534 setTranslationY(ty);
3535 setRotation(rotation);
3536 setRotationX(rotationX);
3537 setRotationY(rotationY);
3538 setScaleX(sx);
3539 setScaleY(sy);
3540 }
3541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3543 setScrollContainer(true);
3544 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003545
3546 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
3548
3549 /**
3550 * Non-public constructor for use in testing
3551 */
3552 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003553 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
3555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 /**
3557 * <p>
3558 * Initializes the fading edges from a given set of styled attributes. This
3559 * method should be called by subclasses that need fading edges and when an
3560 * instance of these subclasses is created programmatically rather than
3561 * being inflated from XML. This method is automatically called when the XML
3562 * is inflated.
3563 * </p>
3564 *
3565 * @param a the styled attributes set to initialize the fading edges from
3566 */
3567 protected void initializeFadingEdge(TypedArray a) {
3568 initScrollCache();
3569
3570 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3571 R.styleable.View_fadingEdgeLength,
3572 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3573 }
3574
3575 /**
3576 * Returns the size of the vertical faded edges used to indicate that more
3577 * content in this view is visible.
3578 *
3579 * @return The size in pixels of the vertical faded edge or 0 if vertical
3580 * faded edges are not enabled for this view.
3581 * @attr ref android.R.styleable#View_fadingEdgeLength
3582 */
3583 public int getVerticalFadingEdgeLength() {
3584 if (isVerticalFadingEdgeEnabled()) {
3585 ScrollabilityCache cache = mScrollCache;
3586 if (cache != null) {
3587 return cache.fadingEdgeLength;
3588 }
3589 }
3590 return 0;
3591 }
3592
3593 /**
3594 * Set the size of the faded edge used to indicate that more content in this
3595 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003596 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3597 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3598 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 *
3600 * @param length The size in pixels of the faded edge used to indicate that more
3601 * content in this view is visible.
3602 */
3603 public void setFadingEdgeLength(int length) {
3604 initScrollCache();
3605 mScrollCache.fadingEdgeLength = length;
3606 }
3607
3608 /**
3609 * Returns the size of the horizontal faded edges used to indicate that more
3610 * content in this view is visible.
3611 *
3612 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3613 * faded edges are not enabled for this view.
3614 * @attr ref android.R.styleable#View_fadingEdgeLength
3615 */
3616 public int getHorizontalFadingEdgeLength() {
3617 if (isHorizontalFadingEdgeEnabled()) {
3618 ScrollabilityCache cache = mScrollCache;
3619 if (cache != null) {
3620 return cache.fadingEdgeLength;
3621 }
3622 }
3623 return 0;
3624 }
3625
3626 /**
3627 * Returns the width of the vertical scrollbar.
3628 *
3629 * @return The width in pixels of the vertical scrollbar or 0 if there
3630 * is no vertical scrollbar.
3631 */
3632 public int getVerticalScrollbarWidth() {
3633 ScrollabilityCache cache = mScrollCache;
3634 if (cache != null) {
3635 ScrollBarDrawable scrollBar = cache.scrollBar;
3636 if (scrollBar != null) {
3637 int size = scrollBar.getSize(true);
3638 if (size <= 0) {
3639 size = cache.scrollBarSize;
3640 }
3641 return size;
3642 }
3643 return 0;
3644 }
3645 return 0;
3646 }
3647
3648 /**
3649 * Returns the height of the horizontal scrollbar.
3650 *
3651 * @return The height in pixels of the horizontal scrollbar or 0 if
3652 * there is no horizontal scrollbar.
3653 */
3654 protected int getHorizontalScrollbarHeight() {
3655 ScrollabilityCache cache = mScrollCache;
3656 if (cache != null) {
3657 ScrollBarDrawable scrollBar = cache.scrollBar;
3658 if (scrollBar != null) {
3659 int size = scrollBar.getSize(false);
3660 if (size <= 0) {
3661 size = cache.scrollBarSize;
3662 }
3663 return size;
3664 }
3665 return 0;
3666 }
3667 return 0;
3668 }
3669
3670 /**
3671 * <p>
3672 * Initializes the scrollbars from a given set of styled attributes. This
3673 * method should be called by subclasses that need scrollbars and when an
3674 * instance of these subclasses is created programmatically rather than
3675 * being inflated from XML. This method is automatically called when the XML
3676 * is inflated.
3677 * </p>
3678 *
3679 * @param a the styled attributes set to initialize the scrollbars from
3680 */
3681 protected void initializeScrollbars(TypedArray a) {
3682 initScrollCache();
3683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003685
Mike Cleronf116bf82009-09-27 19:14:12 -07003686 if (scrollabilityCache.scrollBar == null) {
3687 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3688 }
Joe Malin32736f02011-01-19 16:14:20 -08003689
Romain Guy8bda2482010-03-02 11:42:11 -08003690 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691
Mike Cleronf116bf82009-09-27 19:14:12 -07003692 if (!fadeScrollbars) {
3693 scrollabilityCache.state = ScrollabilityCache.ON;
3694 }
3695 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003696
3697
Mike Cleronf116bf82009-09-27 19:14:12 -07003698 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3699 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3700 .getScrollBarFadeDuration());
3701 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3702 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3703 ViewConfiguration.getScrollDefaultDelay());
3704
Joe Malin32736f02011-01-19 16:14:20 -08003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3707 com.android.internal.R.styleable.View_scrollbarSize,
3708 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3709
3710 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3711 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3712
3713 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3714 if (thumb != null) {
3715 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3716 }
3717
3718 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3719 false);
3720 if (alwaysDraw) {
3721 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3722 }
3723
3724 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3725 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3726
3727 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3728 if (thumb != null) {
3729 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3730 }
3731
3732 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3733 false);
3734 if (alwaysDraw) {
3735 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3736 }
3737
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003738 // Apply layout direction to the new Drawables if needed
3739 final int layoutDirection = getResolvedLayoutDirection();
3740 if (track != null) {
3741 track.setLayoutDirection(layoutDirection);
3742 }
3743 if (thumb != null) {
3744 thumb.setLayoutDirection(layoutDirection);
3745 }
3746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003748 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 }
3750
3751 /**
3752 * <p>
3753 * Initalizes the scrollability cache if necessary.
3754 * </p>
3755 */
3756 private void initScrollCache() {
3757 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003758 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 }
3760 }
3761
Philip Milne6c8ea062012-04-03 17:38:43 -07003762 private ScrollabilityCache getScrollCache() {
3763 initScrollCache();
3764 return mScrollCache;
3765 }
3766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 /**
Adam Powell20232d02010-12-08 21:08:53 -08003768 * Set the position of the vertical scroll bar. Should be one of
3769 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3770 * {@link #SCROLLBAR_POSITION_RIGHT}.
3771 *
3772 * @param position Where the vertical scroll bar should be positioned.
3773 */
3774 public void setVerticalScrollbarPosition(int position) {
3775 if (mVerticalScrollbarPosition != position) {
3776 mVerticalScrollbarPosition = position;
3777 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003778 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003779 }
3780 }
3781
3782 /**
3783 * @return The position where the vertical scroll bar will show, if applicable.
3784 * @see #setVerticalScrollbarPosition(int)
3785 */
3786 public int getVerticalScrollbarPosition() {
3787 return mVerticalScrollbarPosition;
3788 }
3789
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 ListenerInfo getListenerInfo() {
3791 if (mListenerInfo != null) {
3792 return mListenerInfo;
3793 }
3794 mListenerInfo = new ListenerInfo();
3795 return mListenerInfo;
3796 }
3797
Adam Powell20232d02010-12-08 21:08:53 -08003798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 * Register a callback to be invoked when focus of this view changed.
3800 *
3801 * @param l The callback that will run.
3802 */
3803 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003804 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806
3807 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003808 * Add a listener that will be called when the bounds of the view change due to
3809 * layout processing.
3810 *
3811 * @param listener The listener that will be called when layout bounds change.
3812 */
3813 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003814 ListenerInfo li = getListenerInfo();
3815 if (li.mOnLayoutChangeListeners == null) {
3816 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003817 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3819 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003820 }
Chet Haase21cd1382010-09-01 17:42:29 -07003821 }
3822
3823 /**
3824 * Remove a listener for layout changes.
3825 *
3826 * @param listener The listener for layout bounds change.
3827 */
3828 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003829 ListenerInfo li = mListenerInfo;
3830 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003831 return;
3832 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003833 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003834 }
3835
3836 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003837 * Add a listener for attach state changes.
3838 *
3839 * This listener will be called whenever this view is attached or detached
3840 * from a window. Remove the listener using
3841 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3842 *
3843 * @param listener Listener to attach
3844 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3845 */
3846 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003847 ListenerInfo li = getListenerInfo();
3848 if (li.mOnAttachStateChangeListeners == null) {
3849 li.mOnAttachStateChangeListeners
3850 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003851 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003852 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003853 }
3854
3855 /**
3856 * Remove a listener for attach state changes. The listener will receive no further
3857 * notification of window attach/detach events.
3858 *
3859 * @param listener Listener to remove
3860 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3861 */
3862 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003863 ListenerInfo li = mListenerInfo;
3864 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003865 return;
3866 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003867 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003868 }
3869
3870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 * Returns the focus-change callback registered for this view.
3872 *
3873 * @return The callback, or null if one is not registered.
3874 */
3875 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003876 ListenerInfo li = mListenerInfo;
3877 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879
3880 /**
3881 * Register a callback to be invoked when this view is clicked. If this view is not
3882 * clickable, it becomes clickable.
3883 *
3884 * @param l The callback that will run
3885 *
3886 * @see #setClickable(boolean)
3887 */
3888 public void setOnClickListener(OnClickListener l) {
3889 if (!isClickable()) {
3890 setClickable(true);
3891 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003892 getListenerInfo().mOnClickListener = l;
3893 }
3894
3895 /**
3896 * Return whether this view has an attached OnClickListener. Returns
3897 * true if there is a listener, false if there is none.
3898 */
3899 public boolean hasOnClickListeners() {
3900 ListenerInfo li = mListenerInfo;
3901 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 /**
3905 * Register a callback to be invoked when this view is clicked and held. If this view is not
3906 * long clickable, it becomes long clickable.
3907 *
3908 * @param l The callback that will run
3909 *
3910 * @see #setLongClickable(boolean)
3911 */
3912 public void setOnLongClickListener(OnLongClickListener l) {
3913 if (!isLongClickable()) {
3914 setLongClickable(true);
3915 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003916 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 }
3918
3919 /**
3920 * Register a callback to be invoked when the context menu for this view is
3921 * being built. If this view is not long clickable, it becomes long clickable.
3922 *
3923 * @param l The callback that will run
3924 *
3925 */
3926 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3927 if (!isLongClickable()) {
3928 setLongClickable(true);
3929 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003930 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 }
3932
3933 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003934 * Call this view's OnClickListener, if it is defined. Performs all normal
3935 * actions associated with clicking: reporting accessibility event, playing
3936 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 *
3938 * @return True there was an assigned OnClickListener that was called, false
3939 * otherwise is returned.
3940 */
3941 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003942 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3943
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003944 ListenerInfo li = mListenerInfo;
3945 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003947 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 return true;
3949 }
3950
3951 return false;
3952 }
3953
3954 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3956 * this only calls the listener, and does not do any associated clicking
3957 * actions like reporting an accessibility event.
3958 *
3959 * @return True there was an assigned OnClickListener that was called, false
3960 * otherwise is returned.
3961 */
3962 public boolean callOnClick() {
3963 ListenerInfo li = mListenerInfo;
3964 if (li != null && li.mOnClickListener != null) {
3965 li.mOnClickListener.onClick(this);
3966 return true;
3967 }
3968 return false;
3969 }
3970
3971 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003972 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3973 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003975 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 */
3977 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003978 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003981 ListenerInfo li = mListenerInfo;
3982 if (li != null && li.mOnLongClickListener != null) {
3983 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 }
3985 if (!handled) {
3986 handled = showContextMenu();
3987 }
3988 if (handled) {
3989 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3990 }
3991 return handled;
3992 }
3993
3994 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003995 * Performs button-related actions during a touch down event.
3996 *
3997 * @param event The event.
3998 * @return True if the down was consumed.
3999 *
4000 * @hide
4001 */
4002 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4003 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4004 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4005 return true;
4006 }
4007 }
4008 return false;
4009 }
4010
4011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 * Bring up the context menu for this view.
4013 *
4014 * @return Whether a context menu was displayed.
4015 */
4016 public boolean showContextMenu() {
4017 return getParent().showContextMenuForChild(this);
4018 }
4019
4020 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004021 * Bring up the context menu for this view, referring to the item under the specified point.
4022 *
4023 * @param x The referenced x coordinate.
4024 * @param y The referenced y coordinate.
4025 * @param metaState The keyboard modifiers that were pressed.
4026 * @return Whether a context menu was displayed.
4027 *
4028 * @hide
4029 */
4030 public boolean showContextMenu(float x, float y, int metaState) {
4031 return showContextMenu();
4032 }
4033
4034 /**
Adam Powell6e346362010-07-23 10:18:23 -07004035 * Start an action mode.
4036 *
4037 * @param callback Callback that will control the lifecycle of the action mode
4038 * @return The new action mode if it is started, null otherwise
4039 *
4040 * @see ActionMode
4041 */
4042 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004043 ViewParent parent = getParent();
4044 if (parent == null) return null;
4045 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004046 }
4047
4048 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004049 * Register a callback to be invoked when a hardware key is pressed in this view.
4050 * Key presses in software input methods will generally not trigger the methods of
4051 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 * @param l the key listener to attach to this view
4053 */
4054 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004055 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 }
4057
4058 /**
4059 * Register a callback to be invoked when a touch event is sent to this view.
4060 * @param l the touch listener to attach to this view
4061 */
4062 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004063 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
4065
4066 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004067 * Register a callback to be invoked when a generic motion event is sent to this view.
4068 * @param l the generic motion listener to attach to this view
4069 */
4070 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004071 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004072 }
4073
4074 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004075 * Register a callback to be invoked when a hover event is sent to this view.
4076 * @param l the hover listener to attach to this view
4077 */
4078 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004079 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004080 }
4081
4082 /**
Joe Malin32736f02011-01-19 16:14:20 -08004083 * Register a drag event listener callback object for this View. The parameter is
4084 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4085 * View, the system calls the
4086 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4087 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004088 */
4089 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004090 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004091 }
4092
4093 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004094 * Give this view focus. This will cause
4095 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 *
4097 * Note: this does not check whether this {@link View} should get focus, it just
4098 * gives it focus no matter what. It should only be called internally by framework
4099 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4100 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004101 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4102 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 * focus moved when requestFocus() is called. It may not always
4104 * apply, in which case use the default View.FOCUS_DOWN.
4105 * @param previouslyFocusedRect The rectangle of the view that had focus
4106 * prior in this View's coordinate system.
4107 */
4108 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4109 if (DBG) {
4110 System.out.println(this + " requestFocus()");
4111 }
4112
4113 if ((mPrivateFlags & FOCUSED) == 0) {
4114 mPrivateFlags |= FOCUSED;
4115
4116 if (mParent != null) {
4117 mParent.requestChildFocus(this, this);
4118 }
4119
4120 onFocusChanged(true, direction, previouslyFocusedRect);
4121 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004122
4123 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4124 notifyAccessibilityStateChanged();
4125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
4127 }
4128
4129 /**
4130 * Request that a rectangle of this view be visible on the screen,
4131 * scrolling if necessary just enough.
4132 *
4133 * <p>A View should call this if it maintains some notion of which part
4134 * of its content is interesting. For example, a text editing view
4135 * should call this when its cursor moves.
4136 *
4137 * @param rectangle The rectangle.
4138 * @return Whether any parent scrolled.
4139 */
4140 public boolean requestRectangleOnScreen(Rect rectangle) {
4141 return requestRectangleOnScreen(rectangle, false);
4142 }
4143
4144 /**
4145 * Request that a rectangle of this view be visible on the screen,
4146 * scrolling if necessary just enough.
4147 *
4148 * <p>A View should call this if it maintains some notion of which part
4149 * of its content is interesting. For example, a text editing view
4150 * should call this when its cursor moves.
4151 *
4152 * <p>When <code>immediate</code> is set to true, scrolling will not be
4153 * animated.
4154 *
4155 * @param rectangle The rectangle.
4156 * @param immediate True to forbid animated scrolling, false otherwise
4157 * @return Whether any parent scrolled.
4158 */
4159 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4160 View child = this;
4161 ViewParent parent = mParent;
4162 boolean scrolled = false;
4163 while (parent != null) {
4164 scrolled |= parent.requestChildRectangleOnScreen(child,
4165 rectangle, immediate);
4166
4167 // offset rect so next call has the rectangle in the
4168 // coordinate system of its direct child.
4169 rectangle.offset(child.getLeft(), child.getTop());
4170 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4171
4172 if (!(parent instanceof View)) {
4173 break;
4174 }
Romain Guy8506ab42009-06-11 17:35:47 -07004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 child = (View) parent;
4177 parent = child.getParent();
4178 }
4179 return scrolled;
4180 }
4181
4182 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004183 * Called when this view wants to give up focus. If focus is cleared
4184 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4185 * <p>
4186 * <strong>Note:</strong> When a View clears focus the framework is trying
4187 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004188 * View is the first from the top that can take focus, then all callbacks
4189 * related to clearing focus will be invoked after wich the framework will
4190 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004191 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 */
4193 public void clearFocus() {
4194 if (DBG) {
4195 System.out.println(this + " clearFocus()");
4196 }
4197
4198 if ((mPrivateFlags & FOCUSED) != 0) {
4199 mPrivateFlags &= ~FOCUSED;
4200
4201 if (mParent != null) {
4202 mParent.clearChildFocus(this);
4203 }
4204
4205 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004208
4209 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004210
4211 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4212 notifyAccessibilityStateChanged();
4213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 }
4215 }
4216
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004217 void ensureInputFocusOnFirstFocusable() {
4218 View root = getRootView();
4219 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004220 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
4222 }
4223
4224 /**
4225 * Called internally by the view system when a new view is getting focus.
4226 * This is what clears the old focus.
4227 */
4228 void unFocus() {
4229 if (DBG) {
4230 System.out.println(this + " unFocus()");
4231 }
4232
4233 if ((mPrivateFlags & FOCUSED) != 0) {
4234 mPrivateFlags &= ~FOCUSED;
4235
4236 onFocusChanged(false, 0, null);
4237 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004238
4239 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4240 notifyAccessibilityStateChanged();
4241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 }
4243 }
4244
4245 /**
4246 * Returns true if this view has focus iteself, or is the ancestor of the
4247 * view that has focus.
4248 *
4249 * @return True if this view has or contains focus, false otherwise.
4250 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004251 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 public boolean hasFocus() {
4253 return (mPrivateFlags & FOCUSED) != 0;
4254 }
4255
4256 /**
4257 * Returns true if this view is focusable or if it contains a reachable View
4258 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4259 * is a View whose parents do not block descendants focus.
4260 *
4261 * Only {@link #VISIBLE} views are considered focusable.
4262 *
4263 * @return True if the view is focusable or if the view contains a focusable
4264 * View, false otherwise.
4265 *
4266 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4267 */
4268 public boolean hasFocusable() {
4269 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4270 }
4271
4272 /**
4273 * Called by the view system when the focus state of this view changes.
4274 * When the focus change event is caused by directional navigation, direction
4275 * and previouslyFocusedRect provide insight into where the focus is coming from.
4276 * When overriding, be sure to call up through to the super class so that
4277 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 * @param gainFocus True if the View has focus; false otherwise.
4280 * @param direction The direction focus has moved when requestFocus()
4281 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004282 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4283 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4284 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4286 * system, of the previously focused view. If applicable, this will be
4287 * passed in as finer grained information about where the focus is coming
4288 * from (in addition to direction). Will be <code>null</code> otherwise.
4289 */
4290 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004291 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004292 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4293 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004294 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004295 }
4296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 InputMethodManager imm = InputMethodManager.peekInstance();
4298 if (!gainFocus) {
4299 if (isPressed()) {
4300 setPressed(false);
4301 }
4302 if (imm != null && mAttachInfo != null
4303 && mAttachInfo.mHasWindowFocus) {
4304 imm.focusOut(this);
4305 }
Romain Guya2431d02009-04-30 16:30:00 -07004306 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 } else if (imm != null && mAttachInfo != null
4308 && mAttachInfo.mHasWindowFocus) {
4309 imm.focusIn(this);
4310 }
Romain Guy8506ab42009-06-11 17:35:47 -07004311
Romain Guy0fd89bf2011-01-26 15:41:30 -08004312 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004313 ListenerInfo li = mListenerInfo;
4314 if (li != null && li.mOnFocusChangeListener != null) {
4315 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 }
Joe Malin32736f02011-01-19 16:14:20 -08004317
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004318 if (mAttachInfo != null) {
4319 mAttachInfo.mKeyDispatchState.reset(this);
4320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322
4323 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004324 * Sends an accessibility event of the given type. If accessiiblity is
4325 * not enabled this method has no effect. The default implementation calls
4326 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4327 * to populate information about the event source (this View), then calls
4328 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4329 * populate the text content of the event source including its descendants,
4330 * and last calls
4331 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4332 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004333 * <p>
4334 * If an {@link AccessibilityDelegate} has been specified via calling
4335 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4336 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4337 * responsible for handling this call.
4338 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004339 *
Scott Mainb303d832011-10-12 16:45:18 -07004340 * @param eventType The type of the event to send, as defined by several types from
4341 * {@link android.view.accessibility.AccessibilityEvent}, such as
4342 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4343 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004344 *
4345 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4346 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4347 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004348 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004349 */
4350 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004351 if (mAccessibilityDelegate != null) {
4352 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4353 } else {
4354 sendAccessibilityEventInternal(eventType);
4355 }
4356 }
4357
4358 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004359 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4360 * {@link AccessibilityEvent} to make an announcement which is related to some
4361 * sort of a context change for which none of the events representing UI transitions
4362 * is a good fit. For example, announcing a new page in a book. If accessibility
4363 * is not enabled this method does nothing.
4364 *
4365 * @param text The announcement text.
4366 */
4367 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004368 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004369 AccessibilityEvent event = AccessibilityEvent.obtain(
4370 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004371 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004372 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004373 event.setContentDescription(null);
4374 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004375 }
4376 }
4377
4378 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004379 * @see #sendAccessibilityEvent(int)
4380 *
4381 * Note: Called from the default {@link AccessibilityDelegate}.
4382 */
4383 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004384 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4385 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4386 }
4387 }
4388
4389 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004390 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4391 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004392 * perform a check whether accessibility is enabled.
4393 * <p>
4394 * If an {@link AccessibilityDelegate} has been specified via calling
4395 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4396 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4397 * is responsible for handling this call.
4398 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004399 *
4400 * @param event The event to send.
4401 *
4402 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004403 */
4404 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004405 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004406 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004407 } else {
4408 sendAccessibilityEventUncheckedInternal(event);
4409 }
4410 }
4411
4412 /**
4413 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4414 *
4415 * Note: Called from the default {@link AccessibilityDelegate}.
4416 */
4417 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004418 if (!isShown()) {
4419 return;
4420 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004421 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004422 // Only a subset of accessibility events populates text content.
4423 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4424 dispatchPopulateAccessibilityEvent(event);
4425 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004426 // In the beginning we called #isShown(), so we know that getParent() is not null.
4427 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004428 }
4429
4430 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004431 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4432 * to its children for adding their text content to the event. Note that the
4433 * event text is populated in a separate dispatch path since we add to the
4434 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004435 * A typical implementation will call
4436 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4437 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4438 * on each child. Override this method if custom population of the event text
4439 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004440 * <p>
4441 * If an {@link AccessibilityDelegate} has been specified via calling
4442 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4443 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4444 * is responsible for handling this call.
4445 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004446 * <p>
4447 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4448 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4449 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004450 *
4451 * @param event The event.
4452 *
4453 * @return True if the event population was completed.
4454 */
4455 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004456 if (mAccessibilityDelegate != null) {
4457 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4458 } else {
4459 return dispatchPopulateAccessibilityEventInternal(event);
4460 }
4461 }
4462
4463 /**
4464 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4465 *
4466 * Note: Called from the default {@link AccessibilityDelegate}.
4467 */
4468 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004469 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004470 return false;
4471 }
4472
4473 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004474 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004475 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004476 * text content. While this method is free to modify event
4477 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004478 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4479 * <p>
4480 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004481 * to the text added by the super implementation:
4482 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004483 * super.onPopulateAccessibilityEvent(event);
4484 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4485 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4486 * mCurrentDate.getTimeInMillis(), flags);
4487 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004488 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004489 * <p>
4490 * If an {@link AccessibilityDelegate} has been specified via calling
4491 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4492 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4493 * is responsible for handling this call.
4494 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004495 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4496 * information to the event, in case the default implementation has basic information to add.
4497 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004498 *
4499 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004500 *
4501 * @see #sendAccessibilityEvent(int)
4502 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004503 */
4504 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004505 if (mAccessibilityDelegate != null) {
4506 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4507 } else {
4508 onPopulateAccessibilityEventInternal(event);
4509 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004510 }
4511
4512 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004513 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4514 *
4515 * Note: Called from the default {@link AccessibilityDelegate}.
4516 */
4517 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4518
4519 }
4520
4521 /**
4522 * Initializes an {@link AccessibilityEvent} with information about
4523 * this View which is the event source. In other words, the source of
4524 * an accessibility event is the view whose state change triggered firing
4525 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004526 * <p>
4527 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004528 * to properties set by the super implementation:
4529 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4530 * super.onInitializeAccessibilityEvent(event);
4531 * event.setPassword(true);
4532 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004533 * <p>
4534 * If an {@link AccessibilityDelegate} has been specified via calling
4535 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4536 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4537 * is responsible for handling this call.
4538 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004539 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4540 * information to the event, in case the default implementation has basic information to add.
4541 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004542 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004543 *
4544 * @see #sendAccessibilityEvent(int)
4545 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4546 */
4547 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004548 if (mAccessibilityDelegate != null) {
4549 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4550 } else {
4551 onInitializeAccessibilityEventInternal(event);
4552 }
4553 }
4554
4555 /**
4556 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4557 *
4558 * Note: Called from the default {@link AccessibilityDelegate}.
4559 */
4560 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004561 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004562 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004563 event.setPackageName(getContext().getPackageName());
4564 event.setEnabled(isEnabled());
4565 event.setContentDescription(mContentDescription);
4566
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004567 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004568 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004569 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4570 FOCUSABLES_ALL);
4571 event.setItemCount(focusablesTempList.size());
4572 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4573 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004574 }
4575 }
4576
4577 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004578 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4579 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4580 * This method is responsible for obtaining an accessibility node info from a
4581 * pool of reusable instances and calling
4582 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4583 * initialize the former.
4584 * <p>
4585 * Note: The client is responsible for recycling the obtained instance by calling
4586 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4587 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004588 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004589 * @return A populated {@link AccessibilityNodeInfo}.
4590 *
4591 * @see AccessibilityNodeInfo
4592 */
4593 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004594 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4595 if (provider != null) {
4596 return provider.createAccessibilityNodeInfo(View.NO_ID);
4597 } else {
4598 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4599 onInitializeAccessibilityNodeInfo(info);
4600 return info;
4601 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004602 }
4603
4604 /**
4605 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4606 * The base implementation sets:
4607 * <ul>
4608 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004609 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4610 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004611 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4612 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4613 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4614 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4615 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4616 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4617 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4618 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4619 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4620 * </ul>
4621 * <p>
4622 * Subclasses should override this method, call the super implementation,
4623 * and set additional attributes.
4624 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004625 * <p>
4626 * If an {@link AccessibilityDelegate} has been specified via calling
4627 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4628 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4629 * is responsible for handling this call.
4630 * </p>
4631 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004632 * @param info The instance to initialize.
4633 */
4634 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004635 if (mAccessibilityDelegate != null) {
4636 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4637 } else {
4638 onInitializeAccessibilityNodeInfoInternal(info);
4639 }
4640 }
4641
4642 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004643 * Gets the location of this view in screen coordintates.
4644 *
4645 * @param outRect The output location
4646 */
4647 private void getBoundsOnScreen(Rect outRect) {
4648 if (mAttachInfo == null) {
4649 return;
4650 }
4651
4652 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004653 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004654
4655 if (!hasIdentityMatrix()) {
4656 getMatrix().mapRect(position);
4657 }
4658
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004659 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004660
4661 ViewParent parent = mParent;
4662 while (parent instanceof View) {
4663 View parentView = (View) parent;
4664
4665 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4666
4667 if (!parentView.hasIdentityMatrix()) {
4668 parentView.getMatrix().mapRect(position);
4669 }
4670
4671 position.offset(parentView.mLeft, parentView.mTop);
4672
4673 parent = parentView.mParent;
4674 }
4675
4676 if (parent instanceof ViewRootImpl) {
4677 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4678 position.offset(0, -viewRootImpl.mCurScrollY);
4679 }
4680
4681 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4682
4683 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4684 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4685 }
4686
4687 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004688 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4689 *
4690 * Note: Called from the default {@link AccessibilityDelegate}.
4691 */
4692 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004693 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004694
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004695 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004696 info.setBoundsInParent(bounds);
4697
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004698 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004699 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004700
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004701 ViewParent parent = getParentForAccessibility();
4702 if (parent instanceof View) {
4703 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004704 }
4705
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004706 info.setVisibleToUser(isVisibleToUser());
4707
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004708 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004709 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004710 info.setContentDescription(getContentDescription());
4711
4712 info.setEnabled(isEnabled());
4713 info.setClickable(isClickable());
4714 info.setFocusable(isFocusable());
4715 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004716 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004717 info.setSelected(isSelected());
4718 info.setLongClickable(isLongClickable());
4719
4720 // TODO: These make sense only if we are in an AdapterView but all
4721 // views can be selected. Maybe from accessiiblity perspective
4722 // we should report as selectable view in an AdapterView.
4723 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4724 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4725
4726 if (isFocusable()) {
4727 if (isFocused()) {
4728 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4729 } else {
4730 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4731 }
4732 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004733
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004734 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004735 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004736 } else {
4737 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4738 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004739
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004740 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004741 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4742 }
4743
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004744 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004745 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4746 }
4747
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004748 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004749 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4750 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4751 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004752 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4753 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004754 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004755 }
4756
4757 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004758 * Computes whether this view is visible to the user. Such a view is
4759 * attached, visible, all its predecessors are visible, it is not clipped
4760 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004761 *
4762 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004763 *
4764 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004765 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004766 protected boolean isVisibleToUser() {
4767 return isVisibleToUser(null);
4768 }
4769
4770 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004771 * Computes whether the given portion of this view is visible to the user.
4772 * Such a view is attached, visible, all its predecessors are visible,
4773 * has an alpha greater than zero, and the specified portion is not
4774 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004775 *
4776 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4777 * <code>null</code>, and the entire view will be tested in this case.
4778 * When <code>true</code> is returned by the function, the actual visible
4779 * region will be stored in this parameter; that is, if boundInView is fully
4780 * contained within the view, no modification will be made, otherwise regions
4781 * outside of the visible area of the view will be clipped.
4782 *
4783 * @return Whether the specified portion of the view is visible on the screen.
4784 *
4785 * @hide
4786 */
4787 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004788 if (mAttachInfo != null) {
4789 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4790 Point offset = mAttachInfo.mPoint;
4791 // The first two checks are made also made by isShown() which
4792 // however traverses the tree up to the parent to catch that.
4793 // Therefore, we do some fail fast check to minimize the up
4794 // tree traversal.
4795 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4796 && getAlpha() > 0
4797 && isShown()
4798 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004799 if (isVisible && boundInView != null) {
4800 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004801 // isVisible is always true here, use a simple assignment
4802 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004803 }
4804 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004805 }
4806
4807 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004808 }
4809
4810 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004811 * Sets a delegate for implementing accessibility support via compositon as
4812 * opposed to inheritance. The delegate's primary use is for implementing
4813 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4814 *
4815 * @param delegate The delegate instance.
4816 *
4817 * @see AccessibilityDelegate
4818 */
4819 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4820 mAccessibilityDelegate = delegate;
4821 }
4822
4823 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004824 * Gets the provider for managing a virtual view hierarchy rooted at this View
4825 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4826 * that explore the window content.
4827 * <p>
4828 * If this method returns an instance, this instance is responsible for managing
4829 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4830 * View including the one representing the View itself. Similarly the returned
4831 * instance is responsible for performing accessibility actions on any virtual
4832 * view or the root view itself.
4833 * </p>
4834 * <p>
4835 * If an {@link AccessibilityDelegate} has been specified via calling
4836 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4837 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4838 * is responsible for handling this call.
4839 * </p>
4840 *
4841 * @return The provider.
4842 *
4843 * @see AccessibilityNodeProvider
4844 */
4845 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4846 if (mAccessibilityDelegate != null) {
4847 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4848 } else {
4849 return null;
4850 }
4851 }
4852
4853 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004854 * Gets the unique identifier of this view on the screen for accessibility purposes.
4855 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4856 *
4857 * @return The view accessibility id.
4858 *
4859 * @hide
4860 */
4861 public int getAccessibilityViewId() {
4862 if (mAccessibilityViewId == NO_ID) {
4863 mAccessibilityViewId = sNextAccessibilityViewId++;
4864 }
4865 return mAccessibilityViewId;
4866 }
4867
4868 /**
4869 * Gets the unique identifier of the window in which this View reseides.
4870 *
4871 * @return The window accessibility id.
4872 *
4873 * @hide
4874 */
4875 public int getAccessibilityWindowId() {
4876 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4877 }
4878
4879 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004880 * Gets the {@link View} description. It briefly describes the view and is
4881 * primarily used for accessibility support. Set this property to enable
4882 * better accessibility support for your application. This is especially
4883 * true for views that do not have textual representation (For example,
4884 * ImageButton).
4885 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004886 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004887 *
4888 * @attr ref android.R.styleable#View_contentDescription
4889 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004890 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004891 public CharSequence getContentDescription() {
4892 return mContentDescription;
4893 }
4894
4895 /**
4896 * Sets the {@link View} description. It briefly describes the view and is
4897 * primarily used for accessibility support. Set this property to enable
4898 * better accessibility support for your application. This is especially
4899 * true for views that do not have textual representation (For example,
4900 * ImageButton).
4901 *
4902 * @param contentDescription The content description.
4903 *
4904 * @attr ref android.R.styleable#View_contentDescription
4905 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004906 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004907 public void setContentDescription(CharSequence contentDescription) {
4908 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07004909 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
4910 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
4911 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
4912 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004913 }
4914
4915 /**
Romain Guya2431d02009-04-30 16:30:00 -07004916 * Invoked whenever this view loses focus, either by losing window focus or by losing
4917 * focus within its window. This method can be used to clear any state tied to the
4918 * focus. For instance, if a button is held pressed with the trackball and the window
4919 * loses focus, this method can be used to cancel the press.
4920 *
4921 * Subclasses of View overriding this method should always call super.onFocusLost().
4922 *
4923 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004924 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004925 *
4926 * @hide pending API council approval
4927 */
4928 protected void onFocusLost() {
4929 resetPressedState();
4930 }
4931
4932 private void resetPressedState() {
4933 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4934 return;
4935 }
4936
4937 if (isPressed()) {
4938 setPressed(false);
4939
4940 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004941 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004942 }
4943 }
4944 }
4945
4946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 * Returns true if this view has focus
4948 *
4949 * @return True if this view has focus, false otherwise.
4950 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004951 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 public boolean isFocused() {
4953 return (mPrivateFlags & FOCUSED) != 0;
4954 }
4955
4956 /**
4957 * Find the view in the hierarchy rooted at this view that currently has
4958 * focus.
4959 *
4960 * @return The view that currently has focus, or null if no focused view can
4961 * be found.
4962 */
4963 public View findFocus() {
4964 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4965 }
4966
4967 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004968 * Indicates whether this view is one of the set of scrollable containers in
4969 * its window.
4970 *
4971 * @return whether this view is one of the set of scrollable containers in
4972 * its window
4973 *
4974 * @attr ref android.R.styleable#View_isScrollContainer
4975 */
4976 public boolean isScrollContainer() {
4977 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4978 }
4979
4980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 * Change whether this view is one of the set of scrollable containers in
4982 * its window. This will be used to determine whether the window can
4983 * resize or must pan when a soft input area is open -- scrollable
4984 * containers allow the window to use resize mode since the container
4985 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004986 *
4987 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 */
4989 public void setScrollContainer(boolean isScrollContainer) {
4990 if (isScrollContainer) {
4991 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4992 mAttachInfo.mScrollContainers.add(this);
4993 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4994 }
4995 mPrivateFlags |= SCROLL_CONTAINER;
4996 } else {
4997 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4998 mAttachInfo.mScrollContainers.remove(this);
4999 }
5000 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5001 }
5002 }
5003
5004 /**
5005 * Returns the quality of the drawing cache.
5006 *
5007 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5008 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5009 *
5010 * @see #setDrawingCacheQuality(int)
5011 * @see #setDrawingCacheEnabled(boolean)
5012 * @see #isDrawingCacheEnabled()
5013 *
5014 * @attr ref android.R.styleable#View_drawingCacheQuality
5015 */
5016 public int getDrawingCacheQuality() {
5017 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5018 }
5019
5020 /**
5021 * Set the drawing cache quality of this view. This value is used only when the
5022 * drawing cache is enabled
5023 *
5024 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5025 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5026 *
5027 * @see #getDrawingCacheQuality()
5028 * @see #setDrawingCacheEnabled(boolean)
5029 * @see #isDrawingCacheEnabled()
5030 *
5031 * @attr ref android.R.styleable#View_drawingCacheQuality
5032 */
5033 public void setDrawingCacheQuality(int quality) {
5034 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5035 }
5036
5037 /**
5038 * Returns whether the screen should remain on, corresponding to the current
5039 * value of {@link #KEEP_SCREEN_ON}.
5040 *
5041 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5042 *
5043 * @see #setKeepScreenOn(boolean)
5044 *
5045 * @attr ref android.R.styleable#View_keepScreenOn
5046 */
5047 public boolean getKeepScreenOn() {
5048 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5049 }
5050
5051 /**
5052 * Controls whether the screen should remain on, modifying the
5053 * value of {@link #KEEP_SCREEN_ON}.
5054 *
5055 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5056 *
5057 * @see #getKeepScreenOn()
5058 *
5059 * @attr ref android.R.styleable#View_keepScreenOn
5060 */
5061 public void setKeepScreenOn(boolean keepScreenOn) {
5062 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5063 }
5064
5065 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005066 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5067 * @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 -08005068 *
5069 * @attr ref android.R.styleable#View_nextFocusLeft
5070 */
5071 public int getNextFocusLeftId() {
5072 return mNextFocusLeftId;
5073 }
5074
5075 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005076 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5077 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5078 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 *
5080 * @attr ref android.R.styleable#View_nextFocusLeft
5081 */
5082 public void setNextFocusLeftId(int nextFocusLeftId) {
5083 mNextFocusLeftId = nextFocusLeftId;
5084 }
5085
5086 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005087 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5088 * @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 -08005089 *
5090 * @attr ref android.R.styleable#View_nextFocusRight
5091 */
5092 public int getNextFocusRightId() {
5093 return mNextFocusRightId;
5094 }
5095
5096 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005097 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5098 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5099 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 *
5101 * @attr ref android.R.styleable#View_nextFocusRight
5102 */
5103 public void setNextFocusRightId(int nextFocusRightId) {
5104 mNextFocusRightId = nextFocusRightId;
5105 }
5106
5107 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005108 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5109 * @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 -08005110 *
5111 * @attr ref android.R.styleable#View_nextFocusUp
5112 */
5113 public int getNextFocusUpId() {
5114 return mNextFocusUpId;
5115 }
5116
5117 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005118 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5119 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5120 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 *
5122 * @attr ref android.R.styleable#View_nextFocusUp
5123 */
5124 public void setNextFocusUpId(int nextFocusUpId) {
5125 mNextFocusUpId = nextFocusUpId;
5126 }
5127
5128 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005129 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5130 * @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 -08005131 *
5132 * @attr ref android.R.styleable#View_nextFocusDown
5133 */
5134 public int getNextFocusDownId() {
5135 return mNextFocusDownId;
5136 }
5137
5138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005139 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5140 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5141 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 *
5143 * @attr ref android.R.styleable#View_nextFocusDown
5144 */
5145 public void setNextFocusDownId(int nextFocusDownId) {
5146 mNextFocusDownId = nextFocusDownId;
5147 }
5148
5149 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005150 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5151 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5152 *
5153 * @attr ref android.R.styleable#View_nextFocusForward
5154 */
5155 public int getNextFocusForwardId() {
5156 return mNextFocusForwardId;
5157 }
5158
5159 /**
5160 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5161 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5162 * decide automatically.
5163 *
5164 * @attr ref android.R.styleable#View_nextFocusForward
5165 */
5166 public void setNextFocusForwardId(int nextFocusForwardId) {
5167 mNextFocusForwardId = nextFocusForwardId;
5168 }
5169
5170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 * Returns the visibility of this view and all of its ancestors
5172 *
5173 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5174 */
5175 public boolean isShown() {
5176 View current = this;
5177 //noinspection ConstantConditions
5178 do {
5179 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5180 return false;
5181 }
5182 ViewParent parent = current.mParent;
5183 if (parent == null) {
5184 return false; // We are not attached to the view root
5185 }
5186 if (!(parent instanceof View)) {
5187 return true;
5188 }
5189 current = (View) parent;
5190 } while (current != null);
5191
5192 return false;
5193 }
5194
5195 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005196 * Called by the view hierarchy when the content insets for a window have
5197 * changed, to allow it to adjust its content to fit within those windows.
5198 * The content insets tell you the space that the status bar, input method,
5199 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005201 * <p>You do not normally need to deal with this function, since the default
5202 * window decoration given to applications takes care of applying it to the
5203 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5204 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5205 * and your content can be placed under those system elements. You can then
5206 * use this method within your view hierarchy if you have parts of your UI
5207 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005209 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005210 * inset's to the view's padding, consuming that content (modifying the
5211 * insets to be 0), and returning true. This behavior is off by default, but can
5212 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5213 *
5214 * <p>This function's traversal down the hierarchy is depth-first. The same content
5215 * insets object is propagated down the hierarchy, so any changes made to it will
5216 * be seen by all following views (including potentially ones above in
5217 * the hierarchy since this is a depth-first traversal). The first view
5218 * that returns true will abort the entire traversal.
5219 *
5220 * <p>The default implementation works well for a situation where it is
5221 * used with a container that covers the entire window, allowing it to
5222 * apply the appropriate insets to its content on all edges. If you need
5223 * a more complicated layout (such as two different views fitting system
5224 * windows, one on the top of the window, and one on the bottom),
5225 * you can override the method and handle the insets however you would like.
5226 * Note that the insets provided by the framework are always relative to the
5227 * far edges of the window, not accounting for the location of the called view
5228 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005229 * where the layout will place the view, as it is done before layout happens.)
5230 *
5231 * <p>Note: unlike many View methods, there is no dispatch phase to this
5232 * call. If you are overriding it in a ViewGroup and want to allow the
5233 * call to continue to your children, you must be sure to call the super
5234 * implementation.
5235 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005236 * <p>Here is a sample layout that makes use of fitting system windows
5237 * to have controls for a video view placed inside of the window decorations
5238 * that it hides and shows. This can be used with code like the second
5239 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5240 *
5241 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5242 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005243 * @param insets Current content insets of the window. Prior to
5244 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5245 * the insets or else you and Android will be unhappy.
5246 *
5247 * @return Return true if this view applied the insets and it should not
5248 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005249 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005250 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005251 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 */
5253 protected boolean fitSystemWindows(Rect insets) {
5254 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005255 mUserPaddingStart = UNDEFINED_PADDING;
5256 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005257 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5258 || mAttachInfo == null
5259 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5260 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5261 return true;
5262 } else {
5263 internalSetPadding(0, 0, 0, 0);
5264 return false;
5265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 }
5267 return false;
5268 }
5269
5270 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005271 * Sets whether or not this view should account for system screen decorations
5272 * such as the status bar and inset its content; that is, controlling whether
5273 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5274 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005275 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005276 * <p>Note that if you are providing your own implementation of
5277 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5278 * flag to true -- your implementation will be overriding the default
5279 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005280 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005281 * @param fitSystemWindows If true, then the default implementation of
5282 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005283 *
5284 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005285 * @see #getFitsSystemWindows()
5286 * @see #fitSystemWindows(Rect)
5287 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005288 */
5289 public void setFitsSystemWindows(boolean fitSystemWindows) {
5290 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5291 }
5292
5293 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005294 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005295 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5296 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005297 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005298 * @return Returns true if the default implementation of
5299 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005300 *
5301 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005302 * @see #setFitsSystemWindows()
5303 * @see #fitSystemWindows(Rect)
5304 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005305 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005306 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005307 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5308 }
5309
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005310 /** @hide */
5311 public boolean fitsSystemWindows() {
5312 return getFitsSystemWindows();
5313 }
5314
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005315 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005316 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5317 */
5318 public void requestFitSystemWindows() {
5319 if (mParent != null) {
5320 mParent.requestFitSystemWindows();
5321 }
5322 }
5323
5324 /**
5325 * For use by PhoneWindow to make its own system window fitting optional.
5326 * @hide
5327 */
5328 public void makeOptionalFitsSystemWindows() {
5329 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5330 }
5331
5332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 * Returns the visibility status for this view.
5334 *
5335 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5336 * @attr ref android.R.styleable#View_visibility
5337 */
5338 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005339 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5340 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5341 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 })
5343 public int getVisibility() {
5344 return mViewFlags & VISIBILITY_MASK;
5345 }
5346
5347 /**
5348 * Set the enabled state of this view.
5349 *
5350 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5351 * @attr ref android.R.styleable#View_visibility
5352 */
5353 @RemotableViewMethod
5354 public void setVisibility(int visibility) {
5355 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005356 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 }
5358
5359 /**
5360 * Returns the enabled status for this view. The interpretation of the
5361 * enabled state varies by subclass.
5362 *
5363 * @return True if this view is enabled, false otherwise.
5364 */
5365 @ViewDebug.ExportedProperty
5366 public boolean isEnabled() {
5367 return (mViewFlags & ENABLED_MASK) == ENABLED;
5368 }
5369
5370 /**
5371 * Set the enabled state of this view. The interpretation of the enabled
5372 * state varies by subclass.
5373 *
5374 * @param enabled True if this view is enabled, false otherwise.
5375 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005376 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005378 if (enabled == isEnabled()) return;
5379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5381
5382 /*
5383 * The View most likely has to change its appearance, so refresh
5384 * the drawable state.
5385 */
5386 refreshDrawableState();
5387
5388 // Invalidate too, since the default behavior for views is to be
5389 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005390 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 }
5392
5393 /**
5394 * Set whether this view can receive the focus.
5395 *
5396 * Setting this to false will also ensure that this view is not focusable
5397 * in touch mode.
5398 *
5399 * @param focusable If true, this view can receive the focus.
5400 *
5401 * @see #setFocusableInTouchMode(boolean)
5402 * @attr ref android.R.styleable#View_focusable
5403 */
5404 public void setFocusable(boolean focusable) {
5405 if (!focusable) {
5406 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5407 }
5408 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5409 }
5410
5411 /**
5412 * Set whether this view can receive focus while in touch mode.
5413 *
5414 * Setting this to true will also ensure that this view is focusable.
5415 *
5416 * @param focusableInTouchMode If true, this view can receive the focus while
5417 * in touch mode.
5418 *
5419 * @see #setFocusable(boolean)
5420 * @attr ref android.R.styleable#View_focusableInTouchMode
5421 */
5422 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5423 // Focusable in touch mode should always be set before the focusable flag
5424 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5425 // which, in touch mode, will not successfully request focus on this view
5426 // because the focusable in touch mode flag is not set
5427 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5428 if (focusableInTouchMode) {
5429 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5430 }
5431 }
5432
5433 /**
5434 * Set whether this view should have sound effects enabled for events such as
5435 * clicking and touching.
5436 *
5437 * <p>You may wish to disable sound effects for a view if you already play sounds,
5438 * for instance, a dial key that plays dtmf tones.
5439 *
5440 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5441 * @see #isSoundEffectsEnabled()
5442 * @see #playSoundEffect(int)
5443 * @attr ref android.R.styleable#View_soundEffectsEnabled
5444 */
5445 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5446 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5447 }
5448
5449 /**
5450 * @return whether this view should have sound effects enabled for events such as
5451 * clicking and touching.
5452 *
5453 * @see #setSoundEffectsEnabled(boolean)
5454 * @see #playSoundEffect(int)
5455 * @attr ref android.R.styleable#View_soundEffectsEnabled
5456 */
5457 @ViewDebug.ExportedProperty
5458 public boolean isSoundEffectsEnabled() {
5459 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5460 }
5461
5462 /**
5463 * Set whether this view should have haptic feedback for events such as
5464 * long presses.
5465 *
5466 * <p>You may wish to disable haptic feedback if your view already controls
5467 * its own haptic feedback.
5468 *
5469 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5470 * @see #isHapticFeedbackEnabled()
5471 * @see #performHapticFeedback(int)
5472 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5473 */
5474 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5475 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5476 }
5477
5478 /**
5479 * @return whether this view should have haptic feedback enabled for events
5480 * long presses.
5481 *
5482 * @see #setHapticFeedbackEnabled(boolean)
5483 * @see #performHapticFeedback(int)
5484 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5485 */
5486 @ViewDebug.ExportedProperty
5487 public boolean isHapticFeedbackEnabled() {
5488 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5489 }
5490
5491 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005492 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005493 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005494 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5495 * {@link #LAYOUT_DIRECTION_RTL},
5496 * {@link #LAYOUT_DIRECTION_INHERIT} or
5497 * {@link #LAYOUT_DIRECTION_LOCALE}.
5498 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005499 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005500 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005501 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5502 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5503 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5504 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005505 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005506 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005507 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005508 }
5509
5510 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005511 * Set the layout direction for this view. This will propagate a reset of layout direction
5512 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005513 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005514 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5515 * {@link #LAYOUT_DIRECTION_RTL},
5516 * {@link #LAYOUT_DIRECTION_INHERIT} or
5517 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005518 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005519 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005520 */
5521 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005522 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005523 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005524 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005525 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005526 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005527 // Set the new layout direction (filtered) and ask for a layout pass
5528 mPrivateFlags2 |=
5529 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005530 resolvePadding();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005531 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005532 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005533 }
5534
5535 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005536 * Returns the resolved layout direction for this view.
5537 *
5538 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005539 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005540 */
5541 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005542 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5543 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005544 })
5545 public int getResolvedLayoutDirection() {
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005546 // The layout direction will be resolved only if needed
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005547 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5548 resolveLayoutDirection();
5549 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005550 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005551 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5552 }
5553
5554 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005555 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5556 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005557 *
5558 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005559 */
5560 @ViewDebug.ExportedProperty(category = "layout")
5561 public boolean isLayoutRtl() {
5562 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5563 }
5564
5565 /**
Adam Powell539ee872012-02-03 19:00:49 -08005566 * Indicates whether the view is currently tracking transient state that the
5567 * app should not need to concern itself with saving and restoring, but that
5568 * the framework should take special note to preserve when possible.
5569 *
Adam Powell785c4472012-05-02 21:25:39 -07005570 * <p>A view with transient state cannot be trivially rebound from an external
5571 * data source, such as an adapter binding item views in a list. This may be
5572 * because the view is performing an animation, tracking user selection
5573 * of content, or similar.</p>
5574 *
Adam Powell539ee872012-02-03 19:00:49 -08005575 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005576 */
5577 @ViewDebug.ExportedProperty(category = "layout")
5578 public boolean hasTransientState() {
5579 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5580 }
5581
5582 /**
5583 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005584 * framework should attempt to preserve when possible. This flag is reference counted,
5585 * so every call to setHasTransientState(true) should be paired with a later call
5586 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005587 *
Adam Powell785c4472012-05-02 21:25:39 -07005588 * <p>A view with transient state cannot be trivially rebound from an external
5589 * data source, such as an adapter binding item views in a list. This may be
5590 * because the view is performing an animation, tracking user selection
5591 * of content, or similar.</p>
5592 *
Adam Powell539ee872012-02-03 19:00:49 -08005593 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005594 */
5595 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005596 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5597 mTransientStateCount - 1;
5598 if (mTransientStateCount < 0) {
5599 mTransientStateCount = 0;
5600 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5601 "unmatched pair of setHasTransientState calls");
5602 }
5603 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005604 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005605 // update flag if we've just incremented up from 0 or decremented down to 0
5606 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5607 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5608 if (mParent != null) {
5609 try {
5610 mParent.childHasTransientStateChanged(this, hasTransientState);
5611 } catch (AbstractMethodError e) {
5612 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5613 " does not fully implement ViewParent", e);
5614 }
Adam Powell539ee872012-02-03 19:00:49 -08005615 }
5616 }
5617 }
5618
5619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 * If this view doesn't do any drawing on its own, set this flag to
5621 * allow further optimizations. By default, this flag is not set on
5622 * View, but could be set on some View subclasses such as ViewGroup.
5623 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005624 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5625 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 *
5627 * @param willNotDraw whether or not this View draw on its own
5628 */
5629 public void setWillNotDraw(boolean willNotDraw) {
5630 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5631 }
5632
5633 /**
5634 * Returns whether or not this View draws on its own.
5635 *
5636 * @return true if this view has nothing to draw, false otherwise
5637 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005638 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 public boolean willNotDraw() {
5640 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5641 }
5642
5643 /**
5644 * When a View's drawing cache is enabled, drawing is redirected to an
5645 * offscreen bitmap. Some views, like an ImageView, must be able to
5646 * bypass this mechanism if they already draw a single bitmap, to avoid
5647 * unnecessary usage of the memory.
5648 *
5649 * @param willNotCacheDrawing true if this view does not cache its
5650 * drawing, false otherwise
5651 */
5652 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5653 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5654 }
5655
5656 /**
5657 * Returns whether or not this View can cache its drawing or not.
5658 *
5659 * @return true if this view does not cache its drawing, false otherwise
5660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005661 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 public boolean willNotCacheDrawing() {
5663 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5664 }
5665
5666 /**
5667 * Indicates whether this view reacts to click events or not.
5668 *
5669 * @return true if the view is clickable, false otherwise
5670 *
5671 * @see #setClickable(boolean)
5672 * @attr ref android.R.styleable#View_clickable
5673 */
5674 @ViewDebug.ExportedProperty
5675 public boolean isClickable() {
5676 return (mViewFlags & CLICKABLE) == CLICKABLE;
5677 }
5678
5679 /**
5680 * Enables or disables click events for this view. When a view
5681 * is clickable it will change its state to "pressed" on every click.
5682 * Subclasses should set the view clickable to visually react to
5683 * user's clicks.
5684 *
5685 * @param clickable true to make the view clickable, false otherwise
5686 *
5687 * @see #isClickable()
5688 * @attr ref android.R.styleable#View_clickable
5689 */
5690 public void setClickable(boolean clickable) {
5691 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5692 }
5693
5694 /**
5695 * Indicates whether this view reacts to long click events or not.
5696 *
5697 * @return true if the view is long clickable, false otherwise
5698 *
5699 * @see #setLongClickable(boolean)
5700 * @attr ref android.R.styleable#View_longClickable
5701 */
5702 public boolean isLongClickable() {
5703 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5704 }
5705
5706 /**
5707 * Enables or disables long click events for this view. When a view is long
5708 * clickable it reacts to the user holding down the button for a longer
5709 * duration than a tap. This event can either launch the listener or a
5710 * context menu.
5711 *
5712 * @param longClickable true to make the view long clickable, false otherwise
5713 * @see #isLongClickable()
5714 * @attr ref android.R.styleable#View_longClickable
5715 */
5716 public void setLongClickable(boolean longClickable) {
5717 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5718 }
5719
5720 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005721 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 *
5723 * @see #isClickable()
5724 * @see #setClickable(boolean)
5725 *
5726 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5727 * the View's internal state from a previously set "pressed" state.
5728 */
5729 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005730 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 if (pressed) {
5733 mPrivateFlags |= PRESSED;
5734 } else {
5735 mPrivateFlags &= ~PRESSED;
5736 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005737
5738 if (needsRefresh) {
5739 refreshDrawableState();
5740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 dispatchSetPressed(pressed);
5742 }
5743
5744 /**
5745 * Dispatch setPressed to all of this View's children.
5746 *
5747 * @see #setPressed(boolean)
5748 *
5749 * @param pressed The new pressed state
5750 */
5751 protected void dispatchSetPressed(boolean pressed) {
5752 }
5753
5754 /**
5755 * Indicates whether the view is currently in pressed state. Unless
5756 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5757 * the pressed state.
5758 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005759 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 * @see #isClickable()
5761 * @see #setClickable(boolean)
5762 *
5763 * @return true if the view is currently pressed, false otherwise
5764 */
5765 public boolean isPressed() {
5766 return (mPrivateFlags & PRESSED) == PRESSED;
5767 }
5768
5769 /**
5770 * Indicates whether this view will save its state (that is,
5771 * whether its {@link #onSaveInstanceState} method will be called).
5772 *
5773 * @return Returns true if the view state saving is enabled, else false.
5774 *
5775 * @see #setSaveEnabled(boolean)
5776 * @attr ref android.R.styleable#View_saveEnabled
5777 */
5778 public boolean isSaveEnabled() {
5779 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5780 }
5781
5782 /**
5783 * Controls whether the saving of this view's state is
5784 * enabled (that is, whether its {@link #onSaveInstanceState} method
5785 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005786 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 * for its state to be saved. This flag can only disable the
5788 * saving of this view; any child views may still have their state saved.
5789 *
5790 * @param enabled Set to false to <em>disable</em> state saving, or true
5791 * (the default) to allow it.
5792 *
5793 * @see #isSaveEnabled()
5794 * @see #setId(int)
5795 * @see #onSaveInstanceState()
5796 * @attr ref android.R.styleable#View_saveEnabled
5797 */
5798 public void setSaveEnabled(boolean enabled) {
5799 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5800 }
5801
Jeff Brown85a31762010-09-01 17:01:00 -07005802 /**
5803 * Gets whether the framework should discard touches when the view's
5804 * window is obscured by another visible window.
5805 * Refer to the {@link View} security documentation for more details.
5806 *
5807 * @return True if touch filtering is enabled.
5808 *
5809 * @see #setFilterTouchesWhenObscured(boolean)
5810 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5811 */
5812 @ViewDebug.ExportedProperty
5813 public boolean getFilterTouchesWhenObscured() {
5814 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5815 }
5816
5817 /**
5818 * Sets whether the framework should discard touches when the view's
5819 * window is obscured by another visible window.
5820 * Refer to the {@link View} security documentation for more details.
5821 *
5822 * @param enabled True if touch filtering should be enabled.
5823 *
5824 * @see #getFilterTouchesWhenObscured
5825 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5826 */
5827 public void setFilterTouchesWhenObscured(boolean enabled) {
5828 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5829 FILTER_TOUCHES_WHEN_OBSCURED);
5830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831
5832 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005833 * Indicates whether the entire hierarchy under this view will save its
5834 * state when a state saving traversal occurs from its parent. The default
5835 * is true; if false, these views will not be saved unless
5836 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5837 *
5838 * @return Returns true if the view state saving from parent is enabled, else false.
5839 *
5840 * @see #setSaveFromParentEnabled(boolean)
5841 */
5842 public boolean isSaveFromParentEnabled() {
5843 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5844 }
5845
5846 /**
5847 * Controls whether the entire hierarchy under this view will save its
5848 * state when a state saving traversal occurs from its parent. The default
5849 * is true; if false, these views will not be saved unless
5850 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5851 *
5852 * @param enabled Set to false to <em>disable</em> state saving, or true
5853 * (the default) to allow it.
5854 *
5855 * @see #isSaveFromParentEnabled()
5856 * @see #setId(int)
5857 * @see #onSaveInstanceState()
5858 */
5859 public void setSaveFromParentEnabled(boolean enabled) {
5860 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5861 }
5862
5863
5864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 * Returns whether this View is able to take focus.
5866 *
5867 * @return True if this view can take focus, or false otherwise.
5868 * @attr ref android.R.styleable#View_focusable
5869 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005870 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 public final boolean isFocusable() {
5872 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5873 }
5874
5875 /**
5876 * When a view is focusable, it may not want to take focus when in touch mode.
5877 * For example, a button would like focus when the user is navigating via a D-pad
5878 * so that the user can click on it, but once the user starts touching the screen,
5879 * the button shouldn't take focus
5880 * @return Whether the view is focusable in touch mode.
5881 * @attr ref android.R.styleable#View_focusableInTouchMode
5882 */
5883 @ViewDebug.ExportedProperty
5884 public final boolean isFocusableInTouchMode() {
5885 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5886 }
5887
5888 /**
5889 * Find the nearest view in the specified direction that can take focus.
5890 * This does not actually give focus to that view.
5891 *
5892 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5893 *
5894 * @return The nearest focusable in the specified direction, or null if none
5895 * can be found.
5896 */
5897 public View focusSearch(int direction) {
5898 if (mParent != null) {
5899 return mParent.focusSearch(this, direction);
5900 } else {
5901 return null;
5902 }
5903 }
5904
5905 /**
5906 * This method is the last chance for the focused view and its ancestors to
5907 * respond to an arrow key. This is called when the focused view did not
5908 * consume the key internally, nor could the view system find a new view in
5909 * the requested direction to give focus to.
5910 *
5911 * @param focused The currently focused view.
5912 * @param direction The direction focus wants to move. One of FOCUS_UP,
5913 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5914 * @return True if the this view consumed this unhandled move.
5915 */
5916 public boolean dispatchUnhandledMove(View focused, int direction) {
5917 return false;
5918 }
5919
5920 /**
5921 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005922 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005924 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5925 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 * @return The user specified next view, or null if there is none.
5927 */
5928 View findUserSetNextFocus(View root, int direction) {
5929 switch (direction) {
5930 case FOCUS_LEFT:
5931 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005932 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 case FOCUS_RIGHT:
5934 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005935 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 case FOCUS_UP:
5937 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005938 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939 case FOCUS_DOWN:
5940 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005941 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005942 case FOCUS_FORWARD:
5943 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005944 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005945 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005946 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005947 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005948 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005949 @Override
5950 public boolean apply(View t) {
5951 return t.mNextFocusForwardId == id;
5952 }
5953 });
5954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 }
5956 return null;
5957 }
5958
Jeff Brown4dfbec22011-08-15 14:55:37 -07005959 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5960 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5961 @Override
5962 public boolean apply(View t) {
5963 return t.mID == childViewId;
5964 }
5965 });
5966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 if (result == null) {
5968 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5969 + "by user for id " + childViewId);
5970 }
5971 return result;
5972 }
5973
5974 /**
5975 * Find and return all focusable views that are descendants of this view,
5976 * possibly including this view if it is focusable itself.
5977 *
5978 * @param direction The direction of the focus
5979 * @return A list of focusable views
5980 */
5981 public ArrayList<View> getFocusables(int direction) {
5982 ArrayList<View> result = new ArrayList<View>(24);
5983 addFocusables(result, direction);
5984 return result;
5985 }
5986
5987 /**
5988 * Add any focusable views that are descendants of this view (possibly
5989 * including this view if it is focusable itself) to views. If we are in touch mode,
5990 * only add views that are also focusable in touch mode.
5991 *
5992 * @param views Focusable views found so far
5993 * @param direction The direction of the focus
5994 */
5995 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005996 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998
svetoslavganov75986cf2009-05-14 22:28:01 -07005999 /**
6000 * Adds any focusable views that are descendants of this view (possibly
6001 * including this view if it is focusable itself) to views. This method
6002 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006003 * only views focusable in touch mode if we are in touch mode or
6004 * only views that can take accessibility focus if accessibility is enabeld
6005 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006006 *
6007 * @param views Focusable views found so far or null if all we are interested is
6008 * the number of focusables.
6009 * @param direction The direction of the focus.
6010 * @param focusableMode The type of focusables to be added.
6011 *
6012 * @see #FOCUSABLES_ALL
6013 * @see #FOCUSABLES_TOUCH_MODE
6014 */
6015 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006016 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006017 return;
6018 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006019 if (!isFocusable()) {
6020 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006021 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006022 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6023 && isInTouchMode() && !isFocusableInTouchMode()) {
6024 return;
6025 }
6026 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 }
6028
6029 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006030 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006031 * The search is performed by either the text that the View renders or the content
6032 * description that describes the view for accessibility purposes and the view does
6033 * not render or both. Clients can specify how the search is to be performed via
6034 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6035 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006036 *
6037 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006038 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006039 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006040 * @see #FIND_VIEWS_WITH_TEXT
6041 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6042 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006043 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006044 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006045 if (getAccessibilityNodeProvider() != null) {
6046 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6047 outViews.add(this);
6048 }
6049 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006050 && (searched != null && searched.length() > 0)
6051 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006052 String searchedLowerCase = searched.toString().toLowerCase();
6053 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6054 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6055 outViews.add(this);
6056 }
6057 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006058 }
6059
6060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 * Find and return all touchable views that are descendants of this view,
6062 * possibly including this view if it is touchable itself.
6063 *
6064 * @return A list of touchable views
6065 */
6066 public ArrayList<View> getTouchables() {
6067 ArrayList<View> result = new ArrayList<View>();
6068 addTouchables(result);
6069 return result;
6070 }
6071
6072 /**
6073 * Add any touchable views that are descendants of this view (possibly
6074 * including this view if it is touchable itself) to views.
6075 *
6076 * @param views Touchable views found so far
6077 */
6078 public void addTouchables(ArrayList<View> views) {
6079 final int viewFlags = mViewFlags;
6080
6081 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6082 && (viewFlags & ENABLED_MASK) == ENABLED) {
6083 views.add(this);
6084 }
6085 }
6086
6087 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006088 * Returns whether this View is accessibility focused.
6089 *
6090 * @return True if this View is accessibility focused.
6091 */
6092 boolean isAccessibilityFocused() {
6093 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6094 }
6095
6096 /**
6097 * Call this to try to give accessibility focus to this view.
6098 *
6099 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6100 * returns false or the view is no visible or the view already has accessibility
6101 * focus.
6102 *
6103 * See also {@link #focusSearch(int)}, which is what you call to say that you
6104 * have focus, and you want your parent to look for the next one.
6105 *
6106 * @return Whether this view actually took accessibility focus.
6107 *
6108 * @hide
6109 */
6110 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006111 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6112 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006113 return false;
6114 }
6115 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6116 return false;
6117 }
6118 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6119 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6120 ViewRootImpl viewRootImpl = getViewRootImpl();
6121 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006122 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006123 }
6124 invalidate();
6125 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6126 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006127 return true;
6128 }
6129 return false;
6130 }
6131
6132 /**
6133 * Call this to try to clear accessibility focus of this view.
6134 *
6135 * See also {@link #focusSearch(int)}, which is what you call to say that you
6136 * have focus, and you want your parent to look for the next one.
6137 *
6138 * @hide
6139 */
6140 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006141 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6142 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006143 invalidate();
6144 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6145 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006147 // Clear the global reference of accessibility focus if this
6148 // view or any of its descendants had accessibility focus.
6149 ViewRootImpl viewRootImpl = getViewRootImpl();
6150 if (viewRootImpl != null) {
6151 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6152 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006153 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006154 }
6155 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006156 }
6157
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006158 private void sendAccessibilityHoverEvent(int eventType) {
6159 // Since we are not delivering to a client accessibility events from not
6160 // important views (unless the clinet request that) we need to fire the
6161 // event from the deepest view exposed to the client. As a consequence if
6162 // the user crosses a not exposed view the client will see enter and exit
6163 // of the exposed predecessor followed by and enter and exit of that same
6164 // predecessor when entering and exiting the not exposed descendant. This
6165 // is fine since the client has a clear idea which view is hovered at the
6166 // price of a couple more events being sent. This is a simple and
6167 // working solution.
6168 View source = this;
6169 while (true) {
6170 if (source.includeForAccessibility()) {
6171 source.sendAccessibilityEvent(eventType);
6172 return;
6173 }
6174 ViewParent parent = source.getParent();
6175 if (parent instanceof View) {
6176 source = (View) parent;
6177 } else {
6178 return;
6179 }
6180 }
6181 }
6182
Svetoslav Ganov42138042012-03-20 11:51:39 -07006183 /**
6184 * Clears accessibility focus without calling any callback methods
6185 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6186 * is used for clearing accessibility focus when giving this focus to
6187 * another view.
6188 */
6189 void clearAccessibilityFocusNoCallbacks() {
6190 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6191 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6192 invalidate();
6193 }
6194 }
6195
6196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 * Call this to try to give focus to a specific view or to one of its
6198 * descendants.
6199 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006200 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6201 * false), or if it is focusable and it is not focusable in touch mode
6202 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006204 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 * have focus, and you want your parent to look for the next one.
6206 *
6207 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6208 * {@link #FOCUS_DOWN} and <code>null</code>.
6209 *
6210 * @return Whether this view or one of its descendants actually took focus.
6211 */
6212 public final boolean requestFocus() {
6213 return requestFocus(View.FOCUS_DOWN);
6214 }
6215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 /**
6217 * Call this to try to give focus to a specific view or to one of its
6218 * descendants and give it a hint about what direction focus is heading.
6219 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006220 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6221 * false), or if it is focusable and it is not focusable in touch mode
6222 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006224 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 * have focus, and you want your parent to look for the next one.
6226 *
6227 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6228 * <code>null</code> set for the previously focused rectangle.
6229 *
6230 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6231 * @return Whether this view or one of its descendants actually took focus.
6232 */
6233 public final boolean requestFocus(int direction) {
6234 return requestFocus(direction, null);
6235 }
6236
6237 /**
6238 * Call this to try to give focus to a specific view or to one of its descendants
6239 * and give it hints about the direction and a specific rectangle that the focus
6240 * is coming from. The rectangle can help give larger views a finer grained hint
6241 * about where focus is coming from, and therefore, where to show selection, or
6242 * forward focus change internally.
6243 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006244 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6245 * false), or if it is focusable and it is not focusable in touch mode
6246 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 *
6248 * A View will not take focus if it is not visible.
6249 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006250 * A View will not take focus if one of its parents has
6251 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6252 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006254 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 * have focus, and you want your parent to look for the next one.
6256 *
6257 * You may wish to override this method if your custom {@link View} has an internal
6258 * {@link View} that it wishes to forward the request to.
6259 *
6260 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6261 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6262 * to give a finer grained hint about where focus is coming from. May be null
6263 * if there is no hint.
6264 * @return Whether this view or one of its descendants actually took focus.
6265 */
6266 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006267 return requestFocusNoSearch(direction, previouslyFocusedRect);
6268 }
6269
6270 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 // need to be focusable
6272 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6273 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6274 return false;
6275 }
6276
6277 // need to be focusable in touch mode if in touch mode
6278 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006279 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6280 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 }
6282
6283 // need to not have any parents blocking us
6284 if (hasAncestorThatBlocksDescendantFocus()) {
6285 return false;
6286 }
6287
6288 handleFocusGainInternal(direction, previouslyFocusedRect);
6289 return true;
6290 }
6291
6292 /**
6293 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6294 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6295 * touch mode to request focus when they are touched.
6296 *
6297 * @return Whether this view or one of its descendants actually took focus.
6298 *
6299 * @see #isInTouchMode()
6300 *
6301 */
6302 public final boolean requestFocusFromTouch() {
6303 // Leave touch mode if we need to
6304 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006305 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006306 if (viewRoot != null) {
6307 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 }
6309 }
6310 return requestFocus(View.FOCUS_DOWN);
6311 }
6312
6313 /**
6314 * @return Whether any ancestor of this view blocks descendant focus.
6315 */
6316 private boolean hasAncestorThatBlocksDescendantFocus() {
6317 ViewParent ancestor = mParent;
6318 while (ancestor instanceof ViewGroup) {
6319 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6320 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6321 return true;
6322 } else {
6323 ancestor = vgAncestor.getParent();
6324 }
6325 }
6326 return false;
6327 }
6328
6329 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006330 * Gets the mode for determining whether this View is important for accessibility
6331 * which is if it fires accessibility events and if it is reported to
6332 * accessibility services that query the screen.
6333 *
6334 * @return The mode for determining whether a View is important for accessibility.
6335 *
6336 * @attr ref android.R.styleable#View_importantForAccessibility
6337 *
6338 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6339 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6340 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6341 */
6342 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006343 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6344 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6345 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006346 })
6347 public int getImportantForAccessibility() {
6348 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6349 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6350 }
6351
6352 /**
6353 * Sets how to determine whether this view is important for accessibility
6354 * which is if it fires accessibility events and if it is reported to
6355 * accessibility services that query the screen.
6356 *
6357 * @param mode How to determine whether this view is important for accessibility.
6358 *
6359 * @attr ref android.R.styleable#View_importantForAccessibility
6360 *
6361 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6362 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6363 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6364 */
6365 public void setImportantForAccessibility(int mode) {
6366 if (mode != getImportantForAccessibility()) {
6367 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6368 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6369 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6370 notifyAccessibilityStateChanged();
6371 }
6372 }
6373
6374 /**
6375 * Gets whether this view should be exposed for accessibility.
6376 *
6377 * @return Whether the view is exposed for accessibility.
6378 *
6379 * @hide
6380 */
6381 public boolean isImportantForAccessibility() {
6382 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6383 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6384 switch (mode) {
6385 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6386 return true;
6387 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6388 return false;
6389 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6390 return isActionableForAccessibility() || hasListenersForAccessibility();
6391 default:
6392 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6393 + mode);
6394 }
6395 }
6396
6397 /**
6398 * Gets the parent for accessibility purposes. Note that the parent for
6399 * accessibility is not necessary the immediate parent. It is the first
6400 * predecessor that is important for accessibility.
6401 *
6402 * @return The parent for accessibility purposes.
6403 */
6404 public ViewParent getParentForAccessibility() {
6405 if (mParent instanceof View) {
6406 View parentView = (View) mParent;
6407 if (parentView.includeForAccessibility()) {
6408 return mParent;
6409 } else {
6410 return mParent.getParentForAccessibility();
6411 }
6412 }
6413 return null;
6414 }
6415
6416 /**
6417 * Adds the children of a given View for accessibility. Since some Views are
6418 * not important for accessibility the children for accessibility are not
6419 * necessarily direct children of the riew, rather they are the first level of
6420 * descendants important for accessibility.
6421 *
6422 * @param children The list of children for accessibility.
6423 */
6424 public void addChildrenForAccessibility(ArrayList<View> children) {
6425 if (includeForAccessibility()) {
6426 children.add(this);
6427 }
6428 }
6429
6430 /**
6431 * Whether to regard this view for accessibility. A view is regarded for
6432 * accessibility if it is important for accessibility or the querying
6433 * accessibility service has explicitly requested that view not
6434 * important for accessibility are regarded.
6435 *
6436 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006437 *
6438 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006439 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006440 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006441 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006442 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006443 }
6444 return false;
6445 }
6446
6447 /**
6448 * Returns whether the View is considered actionable from
6449 * accessibility perspective. Such view are important for
6450 * accessiiblity.
6451 *
6452 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006453 *
6454 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006455 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006456 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006457 return (isClickable() || isLongClickable() || isFocusable());
6458 }
6459
6460 /**
6461 * Returns whether the View has registered callbacks wich makes it
6462 * important for accessiiblity.
6463 *
6464 * @return True if the view is actionable for accessibility.
6465 */
6466 private boolean hasListenersForAccessibility() {
6467 ListenerInfo info = getListenerInfo();
6468 return mTouchDelegate != null || info.mOnKeyListener != null
6469 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6470 || info.mOnHoverListener != null || info.mOnDragListener != null;
6471 }
6472
6473 /**
6474 * Notifies accessibility services that some view's important for
6475 * accessibility state has changed. Note that such notifications
6476 * are made at most once every
6477 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6478 * to avoid unnecessary load to the system. Also once a view has
6479 * made a notifucation this method is a NOP until the notification has
6480 * been sent to clients.
6481 *
6482 * @hide
6483 *
6484 * TODO: Makse sure this method is called for any view state change
6485 * that is interesting for accessilility purposes.
6486 */
6487 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006488 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6489 return;
6490 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006491 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6492 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6493 if (mParent != null) {
6494 mParent.childAccessibilityStateChanged(this);
6495 }
6496 }
6497 }
6498
6499 /**
6500 * Reset the state indicating the this view has requested clients
6501 * interested in its accessiblity state to be notified.
6502 *
6503 * @hide
6504 */
6505 public void resetAccessibilityStateChanged() {
6506 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6507 }
6508
6509 /**
6510 * Performs the specified accessibility action on the view. For
6511 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006512 * <p>
6513 * If an {@link AccessibilityDelegate} has been specified via calling
6514 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6515 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6516 * is responsible for handling this call.
6517 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006518 *
6519 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006520 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006521 * @return Whether the action was performed.
6522 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006523 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006524 if (mAccessibilityDelegate != null) {
6525 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6526 } else {
6527 return performAccessibilityActionInternal(action, arguments);
6528 }
6529 }
6530
6531 /**
6532 * @see #performAccessibilityAction(int, Bundle)
6533 *
6534 * Note: Called from the default {@link AccessibilityDelegate}.
6535 */
6536 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006537 switch (action) {
6538 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006539 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006540 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006541 }
6542 } break;
6543 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6544 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006545 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006546 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006547 } break;
6548 case AccessibilityNodeInfo.ACTION_FOCUS: {
6549 if (!hasFocus()) {
6550 // Get out of touch mode since accessibility
6551 // wants to move focus around.
6552 getViewRootImpl().ensureTouchMode(false);
6553 return requestFocus();
6554 }
6555 } break;
6556 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6557 if (hasFocus()) {
6558 clearFocus();
6559 return !isFocused();
6560 }
6561 } break;
6562 case AccessibilityNodeInfo.ACTION_SELECT: {
6563 if (!isSelected()) {
6564 setSelected(true);
6565 return isSelected();
6566 }
6567 } break;
6568 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6569 if (isSelected()) {
6570 setSelected(false);
6571 return !isSelected();
6572 }
6573 } break;
6574 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006575 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006576 return requestAccessibilityFocus();
6577 }
6578 } break;
6579 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6580 if (isAccessibilityFocused()) {
6581 clearAccessibilityFocus();
6582 return true;
6583 }
6584 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006585 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6586 if (arguments != null) {
6587 final int granularity = arguments.getInt(
6588 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6589 return nextAtGranularity(granularity);
6590 }
6591 } break;
6592 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6593 if (arguments != null) {
6594 final int granularity = arguments.getInt(
6595 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6596 return previousAtGranularity(granularity);
6597 }
6598 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006599 }
6600 return false;
6601 }
6602
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006603 private boolean nextAtGranularity(int granularity) {
6604 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006605 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006606 return false;
6607 }
6608 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6609 if (iterator == null) {
6610 return false;
6611 }
6612 final int current = getAccessibilityCursorPosition();
6613 final int[] range = iterator.following(current);
6614 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006615 return false;
6616 }
6617 final int start = range[0];
6618 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006619 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006620 sendViewTextTraversedAtGranularityEvent(
6621 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6622 granularity, start, end);
6623 return true;
6624 }
6625
6626 private boolean previousAtGranularity(int granularity) {
6627 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006628 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006629 return false;
6630 }
6631 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6632 if (iterator == null) {
6633 return false;
6634 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006635 int current = getAccessibilityCursorPosition();
6636 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6637 current = text.length();
6638 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6639 // When traversing by character we always put the cursor after the character
6640 // to ease edit and have to compensate before asking the for previous segment.
6641 current--;
6642 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006643 final int[] range = iterator.preceding(current);
6644 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006645 return false;
6646 }
6647 final int start = range[0];
6648 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006649 // Always put the cursor after the character to ease edit.
6650 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6651 setAccessibilityCursorPosition(end);
6652 } else {
6653 setAccessibilityCursorPosition(start);
6654 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006655 sendViewTextTraversedAtGranularityEvent(
6656 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6657 granularity, start, end);
6658 return true;
6659 }
6660
6661 /**
6662 * Gets the text reported for accessibility purposes.
6663 *
6664 * @return The accessibility text.
6665 *
6666 * @hide
6667 */
6668 public CharSequence getIterableTextForAccessibility() {
6669 return mContentDescription;
6670 }
6671
6672 /**
6673 * @hide
6674 */
6675 public int getAccessibilityCursorPosition() {
6676 return mAccessibilityCursorPosition;
6677 }
6678
6679 /**
6680 * @hide
6681 */
6682 public void setAccessibilityCursorPosition(int position) {
6683 mAccessibilityCursorPosition = position;
6684 }
6685
6686 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6687 int fromIndex, int toIndex) {
6688 if (mParent == null) {
6689 return;
6690 }
6691 AccessibilityEvent event = AccessibilityEvent.obtain(
6692 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6693 onInitializeAccessibilityEvent(event);
6694 onPopulateAccessibilityEvent(event);
6695 event.setFromIndex(fromIndex);
6696 event.setToIndex(toIndex);
6697 event.setAction(action);
6698 event.setMovementGranularity(granularity);
6699 mParent.requestSendAccessibilityEvent(this, event);
6700 }
6701
6702 /**
6703 * @hide
6704 */
6705 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6706 switch (granularity) {
6707 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6708 CharSequence text = getIterableTextForAccessibility();
6709 if (text != null && text.length() > 0) {
6710 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006711 CharacterTextSegmentIterator.getInstance(
6712 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006713 iterator.initialize(text.toString());
6714 return iterator;
6715 }
6716 } break;
6717 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6718 CharSequence text = getIterableTextForAccessibility();
6719 if (text != null && text.length() > 0) {
6720 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006721 WordTextSegmentIterator.getInstance(
6722 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006723 iterator.initialize(text.toString());
6724 return iterator;
6725 }
6726 } break;
6727 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6728 CharSequence text = getIterableTextForAccessibility();
6729 if (text != null && text.length() > 0) {
6730 ParagraphTextSegmentIterator iterator =
6731 ParagraphTextSegmentIterator.getInstance();
6732 iterator.initialize(text.toString());
6733 return iterator;
6734 }
6735 } break;
6736 }
6737 return null;
6738 }
6739
Svetoslav Ganov42138042012-03-20 11:51:39 -07006740 /**
Romain Guya440b002010-02-24 15:57:54 -08006741 * @hide
6742 */
6743 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006744 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006745 clearDisplayList();
6746
Romain Guya440b002010-02-24 15:57:54 -08006747 onStartTemporaryDetach();
6748 }
6749
6750 /**
6751 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6753 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006754 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 */
6756 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006757 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006758 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006759 }
6760
6761 /**
6762 * @hide
6763 */
6764 public void dispatchFinishTemporaryDetach() {
6765 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 }
Romain Guy8506ab42009-06-11 17:35:47 -07006767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 /**
6769 * Called after {@link #onStartTemporaryDetach} when the container is done
6770 * changing the view.
6771 */
6772 public void onFinishTemporaryDetach() {
6773 }
Romain Guy8506ab42009-06-11 17:35:47 -07006774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006776 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6777 * for this view's window. Returns null if the view is not currently attached
6778 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006779 * just use the standard high-level event callbacks like
6780 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006781 */
6782 public KeyEvent.DispatcherState getKeyDispatcherState() {
6783 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6784 }
Joe Malin32736f02011-01-19 16:14:20 -08006785
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 * Dispatch a key event before it is processed by any input method
6788 * associated with the view hierarchy. This can be used to intercept
6789 * key events in special situations before the IME consumes them; a
6790 * typical example would be handling the BACK key to update the application's
6791 * UI instead of allowing the IME to see it and close itself.
6792 *
6793 * @param event The key event to be dispatched.
6794 * @return True if the event was handled, false otherwise.
6795 */
6796 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6797 return onKeyPreIme(event.getKeyCode(), event);
6798 }
6799
6800 /**
6801 * Dispatch a key event to the next view on the focus path. This path runs
6802 * from the top of the view tree down to the currently focused view. If this
6803 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6804 * the next node down the focus path. This method also fires any key
6805 * listeners.
6806 *
6807 * @param event The key event to be dispatched.
6808 * @return True if the event was handled, false otherwise.
6809 */
6810 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006811 if (mInputEventConsistencyVerifier != null) {
6812 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814
Jeff Brown21bc5c92011-02-28 18:27:14 -08006815 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006816 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006817 ListenerInfo li = mListenerInfo;
6818 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6819 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 return true;
6821 }
6822
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006823 if (event.dispatch(this, mAttachInfo != null
6824 ? mAttachInfo.mKeyDispatchState : null, this)) {
6825 return true;
6826 }
6827
6828 if (mInputEventConsistencyVerifier != null) {
6829 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6830 }
6831 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 }
6833
6834 /**
6835 * Dispatches a key shortcut event.
6836 *
6837 * @param event The key event to be dispatched.
6838 * @return True if the event was handled by the view, false otherwise.
6839 */
6840 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6841 return onKeyShortcut(event.getKeyCode(), event);
6842 }
6843
6844 /**
6845 * Pass the touch screen motion event down to the target view, or this
6846 * view if it is the target.
6847 *
6848 * @param event The motion event to be dispatched.
6849 * @return True if the event was handled by the view, false otherwise.
6850 */
6851 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006852 if (mInputEventConsistencyVerifier != null) {
6853 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6854 }
6855
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006856 if (onFilterTouchEventForSecurity(event)) {
6857 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006858 ListenerInfo li = mListenerInfo;
6859 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6860 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006861 return true;
6862 }
6863
6864 if (onTouchEvent(event)) {
6865 return true;
6866 }
Jeff Brown85a31762010-09-01 17:01:00 -07006867 }
6868
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006869 if (mInputEventConsistencyVerifier != null) {
6870 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006872 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 }
6874
6875 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006876 * Filter the touch event to apply security policies.
6877 *
6878 * @param event The motion event to be filtered.
6879 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006880 *
Jeff Brown85a31762010-09-01 17:01:00 -07006881 * @see #getFilterTouchesWhenObscured
6882 */
6883 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006884 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006885 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6886 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6887 // Window is obscured, drop this touch.
6888 return false;
6889 }
6890 return true;
6891 }
6892
6893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 * Pass a trackball motion event down to the focused view.
6895 *
6896 * @param event The motion event to be dispatched.
6897 * @return True if the event was handled by the view, false otherwise.
6898 */
6899 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006900 if (mInputEventConsistencyVerifier != null) {
6901 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6902 }
6903
Romain Guy02ccac62011-06-24 13:20:23 -07006904 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 }
6906
6907 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006908 * Dispatch a generic motion event.
6909 * <p>
6910 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6911 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006912 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006913 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006914 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006915 *
6916 * @param event The motion event to be dispatched.
6917 * @return True if the event was handled by the view, false otherwise.
6918 */
6919 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006920 if (mInputEventConsistencyVerifier != null) {
6921 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6922 }
6923
Jeff Browna032cc02011-03-07 16:56:21 -08006924 final int source = event.getSource();
6925 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6926 final int action = event.getAction();
6927 if (action == MotionEvent.ACTION_HOVER_ENTER
6928 || action == MotionEvent.ACTION_HOVER_MOVE
6929 || action == MotionEvent.ACTION_HOVER_EXIT) {
6930 if (dispatchHoverEvent(event)) {
6931 return true;
6932 }
6933 } else if (dispatchGenericPointerEvent(event)) {
6934 return true;
6935 }
6936 } else if (dispatchGenericFocusedEvent(event)) {
6937 return true;
6938 }
6939
Jeff Brown10b62902011-06-20 16:40:37 -07006940 if (dispatchGenericMotionEventInternal(event)) {
6941 return true;
6942 }
6943
6944 if (mInputEventConsistencyVerifier != null) {
6945 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6946 }
6947 return false;
6948 }
6949
6950 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006951 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006952 ListenerInfo li = mListenerInfo;
6953 if (li != null && li.mOnGenericMotionListener != null
6954 && (mViewFlags & ENABLED_MASK) == ENABLED
6955 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006956 return true;
6957 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006958
6959 if (onGenericMotionEvent(event)) {
6960 return true;
6961 }
6962
6963 if (mInputEventConsistencyVerifier != null) {
6964 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6965 }
6966 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006967 }
6968
6969 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006970 * Dispatch a hover event.
6971 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006972 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006973 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006974 * </p>
6975 *
6976 * @param event The motion event to be dispatched.
6977 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006978 */
6979 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006980 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006981 ListenerInfo li = mListenerInfo;
6982 if (li != null && li.mOnHoverListener != null
6983 && (mViewFlags & ENABLED_MASK) == ENABLED
6984 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006985 return true;
6986 }
6987
Jeff Browna032cc02011-03-07 16:56:21 -08006988 return onHoverEvent(event);
6989 }
6990
6991 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006992 * Returns true if the view has a child to which it has recently sent
6993 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6994 * it does not have a hovered child, then it must be the innermost hovered view.
6995 * @hide
6996 */
6997 protected boolean hasHoveredChild() {
6998 return false;
6999 }
7000
7001 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007002 * Dispatch a generic motion event to the view under the first pointer.
7003 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007004 * Do not call this method directly.
7005 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007006 * </p>
7007 *
7008 * @param event The motion event to be dispatched.
7009 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007010 */
7011 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7012 return false;
7013 }
7014
7015 /**
7016 * Dispatch a generic motion event to the currently focused view.
7017 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007018 * Do not call this method directly.
7019 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007020 * </p>
7021 *
7022 * @param event The motion event to be dispatched.
7023 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007024 */
7025 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7026 return false;
7027 }
7028
7029 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007030 * Dispatch a pointer event.
7031 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007032 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7033 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7034 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007035 * and should not be expected to handle other pointing device features.
7036 * </p>
7037 *
7038 * @param event The motion event to be dispatched.
7039 * @return True if the event was handled by the view, false otherwise.
7040 * @hide
7041 */
7042 public final boolean dispatchPointerEvent(MotionEvent event) {
7043 if (event.isTouchEvent()) {
7044 return dispatchTouchEvent(event);
7045 } else {
7046 return dispatchGenericMotionEvent(event);
7047 }
7048 }
7049
7050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 * Called when the window containing this view gains or loses window focus.
7052 * ViewGroups should override to route to their children.
7053 *
7054 * @param hasFocus True if the window containing this view now has focus,
7055 * false otherwise.
7056 */
7057 public void dispatchWindowFocusChanged(boolean hasFocus) {
7058 onWindowFocusChanged(hasFocus);
7059 }
7060
7061 /**
7062 * Called when the window containing this view gains or loses focus. Note
7063 * that this is separate from view focus: to receive key events, both
7064 * your view and its window must have focus. If a window is displayed
7065 * on top of yours that takes input focus, then your own window will lose
7066 * focus but the view focus will remain unchanged.
7067 *
7068 * @param hasWindowFocus True if the window containing this view now has
7069 * focus, false otherwise.
7070 */
7071 public void onWindowFocusChanged(boolean hasWindowFocus) {
7072 InputMethodManager imm = InputMethodManager.peekInstance();
7073 if (!hasWindowFocus) {
7074 if (isPressed()) {
7075 setPressed(false);
7076 }
7077 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7078 imm.focusOut(this);
7079 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007080 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007081 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007082 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7084 imm.focusIn(this);
7085 }
7086 refreshDrawableState();
7087 }
7088
7089 /**
7090 * Returns true if this view is in a window that currently has window focus.
7091 * Note that this is not the same as the view itself having focus.
7092 *
7093 * @return True if this view is in a window that currently has window focus.
7094 */
7095 public boolean hasWindowFocus() {
7096 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7097 }
7098
7099 /**
Adam Powell326d8082009-12-09 15:10:07 -08007100 * Dispatch a view visibility change down the view hierarchy.
7101 * ViewGroups should override to route to their children.
7102 * @param changedView The view whose visibility changed. Could be 'this' or
7103 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007104 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7105 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007106 */
7107 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7108 onVisibilityChanged(changedView, visibility);
7109 }
7110
7111 /**
7112 * Called when the visibility of the view or an ancestor of the view is changed.
7113 * @param changedView The view whose visibility changed. Could be 'this' or
7114 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007115 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7116 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007117 */
7118 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007119 if (visibility == VISIBLE) {
7120 if (mAttachInfo != null) {
7121 initialAwakenScrollBars();
7122 } else {
7123 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7124 }
7125 }
Adam Powell326d8082009-12-09 15:10:07 -08007126 }
7127
7128 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007129 * Dispatch a hint about whether this view is displayed. For instance, when
7130 * a View moves out of the screen, it might receives a display hint indicating
7131 * the view is not displayed. Applications should not <em>rely</em> on this hint
7132 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007133 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007134 * @param hint A hint about whether or not this view is displayed:
7135 * {@link #VISIBLE} or {@link #INVISIBLE}.
7136 */
7137 public void dispatchDisplayHint(int hint) {
7138 onDisplayHint(hint);
7139 }
7140
7141 /**
7142 * Gives this view a hint about whether is displayed or not. For instance, when
7143 * a View moves out of the screen, it might receives a display hint indicating
7144 * the view is not displayed. Applications should not <em>rely</em> on this hint
7145 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007146 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007147 * @param hint A hint about whether or not this view is displayed:
7148 * {@link #VISIBLE} or {@link #INVISIBLE}.
7149 */
7150 protected void onDisplayHint(int hint) {
7151 }
7152
7153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 * Dispatch a window visibility change down the view hierarchy.
7155 * ViewGroups should override to route to their children.
7156 *
7157 * @param visibility The new visibility of the window.
7158 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007159 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 */
7161 public void dispatchWindowVisibilityChanged(int visibility) {
7162 onWindowVisibilityChanged(visibility);
7163 }
7164
7165 /**
7166 * Called when the window containing has change its visibility
7167 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7168 * that this tells you whether or not your window is being made visible
7169 * to the window manager; this does <em>not</em> tell you whether or not
7170 * your window is obscured by other windows on the screen, even if it
7171 * is itself visible.
7172 *
7173 * @param visibility The new visibility of the window.
7174 */
7175 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007176 if (visibility == VISIBLE) {
7177 initialAwakenScrollBars();
7178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 }
7180
7181 /**
7182 * Returns the current visibility of the window this view is attached to
7183 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7184 *
7185 * @return Returns the current visibility of the view's window.
7186 */
7187 public int getWindowVisibility() {
7188 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7189 }
7190
7191 /**
7192 * Retrieve the overall visible display size in which the window this view is
7193 * attached to has been positioned in. This takes into account screen
7194 * decorations above the window, for both cases where the window itself
7195 * is being position inside of them or the window is being placed under
7196 * then and covered insets are used for the window to position its content
7197 * inside. In effect, this tells you the available area where content can
7198 * be placed and remain visible to users.
7199 *
7200 * <p>This function requires an IPC back to the window manager to retrieve
7201 * the requested information, so should not be used in performance critical
7202 * code like drawing.
7203 *
7204 * @param outRect Filled in with the visible display frame. If the view
7205 * is not attached to a window, this is simply the raw display size.
7206 */
7207 public void getWindowVisibleDisplayFrame(Rect outRect) {
7208 if (mAttachInfo != null) {
7209 try {
7210 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7211 } catch (RemoteException e) {
7212 return;
7213 }
7214 // XXX This is really broken, and probably all needs to be done
7215 // in the window manager, and we need to know more about whether
7216 // we want the area behind or in front of the IME.
7217 final Rect insets = mAttachInfo.mVisibleInsets;
7218 outRect.left += insets.left;
7219 outRect.top += insets.top;
7220 outRect.right -= insets.right;
7221 outRect.bottom -= insets.bottom;
7222 return;
7223 }
7224 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007225 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 }
7227
7228 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007229 * Dispatch a notification about a resource configuration change down
7230 * the view hierarchy.
7231 * ViewGroups should override to route to their children.
7232 *
7233 * @param newConfig The new resource configuration.
7234 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007235 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007236 */
7237 public void dispatchConfigurationChanged(Configuration newConfig) {
7238 onConfigurationChanged(newConfig);
7239 }
7240
7241 /**
7242 * Called when the current configuration of the resources being used
7243 * by the application have changed. You can use this to decide when
7244 * to reload resources that can changed based on orientation and other
7245 * configuration characterstics. You only need to use this if you are
7246 * not relying on the normal {@link android.app.Activity} mechanism of
7247 * recreating the activity instance upon a configuration change.
7248 *
7249 * @param newConfig The new resource configuration.
7250 */
7251 protected void onConfigurationChanged(Configuration newConfig) {
7252 }
7253
7254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 * Private function to aggregate all per-view attributes in to the view
7256 * root.
7257 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007258 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7259 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 }
7261
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007262 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7263 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007264 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007265 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007266 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007267 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007268 ListenerInfo li = mListenerInfo;
7269 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007270 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 }
7273 }
7274
7275 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007276 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007278 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7279 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 ai.mRecomputeGlobalAttributes = true;
7281 }
7282 }
7283 }
7284
7285 /**
7286 * Returns whether the device is currently in touch mode. Touch mode is entered
7287 * once the user begins interacting with the device by touch, and affects various
7288 * things like whether focus is always visible to the user.
7289 *
7290 * @return Whether the device is in touch mode.
7291 */
7292 @ViewDebug.ExportedProperty
7293 public boolean isInTouchMode() {
7294 if (mAttachInfo != null) {
7295 return mAttachInfo.mInTouchMode;
7296 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007297 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 }
7299 }
7300
7301 /**
7302 * Returns the context the view is running in, through which it can
7303 * access the current theme, resources, etc.
7304 *
7305 * @return The view's Context.
7306 */
7307 @ViewDebug.CapturedViewProperty
7308 public final Context getContext() {
7309 return mContext;
7310 }
7311
7312 /**
7313 * Handle a key event before it is processed by any input method
7314 * associated with the view hierarchy. This can be used to intercept
7315 * key events in special situations before the IME consumes them; a
7316 * typical example would be handling the BACK key to update the application's
7317 * UI instead of allowing the IME to see it and close itself.
7318 *
7319 * @param keyCode The value in event.getKeyCode().
7320 * @param event Description of the key event.
7321 * @return If you handled the event, return true. If you want to allow the
7322 * event to be handled by the next receiver, return false.
7323 */
7324 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7325 return false;
7326 }
7327
7328 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007329 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7330 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7332 * is released, if the view is enabled and clickable.
7333 *
Jean Chalard405bc512012-05-29 19:12:34 +09007334 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7335 * although some may elect to do so in some situations. Do not rely on this to
7336 * catch software key presses.
7337 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 * @param keyCode A key code that represents the button pressed, from
7339 * {@link android.view.KeyEvent}.
7340 * @param event The KeyEvent object that defines the button action.
7341 */
7342 public boolean onKeyDown(int keyCode, KeyEvent event) {
7343 boolean result = false;
7344
7345 switch (keyCode) {
7346 case KeyEvent.KEYCODE_DPAD_CENTER:
7347 case KeyEvent.KEYCODE_ENTER: {
7348 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7349 return true;
7350 }
7351 // Long clickable items don't necessarily have to be clickable
7352 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7353 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7354 (event.getRepeatCount() == 0)) {
7355 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007356 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 return true;
7358 }
7359 break;
7360 }
7361 }
7362 return result;
7363 }
7364
7365 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007366 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7367 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7368 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007369 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7370 * although some may elect to do so in some situations. Do not rely on this to
7371 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007372 */
7373 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7374 return false;
7375 }
7376
7377 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007378 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7379 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7381 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007382 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7383 * although some may elect to do so in some situations. Do not rely on this to
7384 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 *
7386 * @param keyCode A key code that represents the button pressed, from
7387 * {@link android.view.KeyEvent}.
7388 * @param event The KeyEvent object that defines the button action.
7389 */
7390 public boolean onKeyUp(int keyCode, KeyEvent event) {
7391 boolean result = false;
7392
7393 switch (keyCode) {
7394 case KeyEvent.KEYCODE_DPAD_CENTER:
7395 case KeyEvent.KEYCODE_ENTER: {
7396 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7397 return true;
7398 }
7399 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7400 setPressed(false);
7401
7402 if (!mHasPerformedLongPress) {
7403 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007404 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405
7406 result = performClick();
7407 }
7408 }
7409 break;
7410 }
7411 }
7412 return result;
7413 }
7414
7415 /**
7416 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7417 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7418 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007419 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7420 * although some may elect to do so in some situations. Do not rely on this to
7421 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007422 *
7423 * @param keyCode A key code that represents the button pressed, from
7424 * {@link android.view.KeyEvent}.
7425 * @param repeatCount The number of times the action was made.
7426 * @param event The KeyEvent object that defines the button action.
7427 */
7428 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7429 return false;
7430 }
7431
7432 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007433 * Called on the focused view when a key shortcut event is not handled.
7434 * Override this method to implement local key shortcuts for the View.
7435 * Key shortcuts can also be implemented by setting the
7436 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 *
7438 * @param keyCode The value in event.getKeyCode().
7439 * @param event Description of the key event.
7440 * @return If you handled the event, return true. If you want to allow the
7441 * event to be handled by the next receiver, return false.
7442 */
7443 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7444 return false;
7445 }
7446
7447 /**
7448 * Check whether the called view is a text editor, in which case it
7449 * would make sense to automatically display a soft input window for
7450 * it. Subclasses should override this if they implement
7451 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007452 * a call on that method would return a non-null InputConnection, and
7453 * they are really a first-class editor that the user would normally
7454 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007455 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007456 * <p>The default implementation always returns false. This does
7457 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7458 * will not be called or the user can not otherwise perform edits on your
7459 * view; it is just a hint to the system that this is not the primary
7460 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 * @return Returns true if this view is a text editor, else false.
7463 */
7464 public boolean onCheckIsTextEditor() {
7465 return false;
7466 }
Romain Guy8506ab42009-06-11 17:35:47 -07007467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 /**
7469 * Create a new InputConnection for an InputMethod to interact
7470 * with the view. The default implementation returns null, since it doesn't
7471 * support input methods. You can override this to implement such support.
7472 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007473 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 * <p>When implementing this, you probably also want to implement
7475 * {@link #onCheckIsTextEditor()} to indicate you will return a
7476 * non-null InputConnection.
7477 *
7478 * @param outAttrs Fill in with attribute information about the connection.
7479 */
7480 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7481 return null;
7482 }
7483
7484 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007485 * Called by the {@link android.view.inputmethod.InputMethodManager}
7486 * when a view who is not the current
7487 * input connection target is trying to make a call on the manager. The
7488 * default implementation returns false; you can override this to return
7489 * true for certain views if you are performing InputConnection proxying
7490 * to them.
7491 * @param view The View that is making the InputMethodManager call.
7492 * @return Return true to allow the call, false to reject.
7493 */
7494 public boolean checkInputConnectionProxy(View view) {
7495 return false;
7496 }
Romain Guy8506ab42009-06-11 17:35:47 -07007497
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 * Show the context menu for this view. It is not safe to hold on to the
7500 * menu after returning from this method.
7501 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007502 * You should normally not overload this method. Overload
7503 * {@link #onCreateContextMenu(ContextMenu)} or define an
7504 * {@link OnCreateContextMenuListener} to add items to the context menu.
7505 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 * @param menu The context menu to populate
7507 */
7508 public void createContextMenu(ContextMenu menu) {
7509 ContextMenuInfo menuInfo = getContextMenuInfo();
7510
7511 // Sets the current menu info so all items added to menu will have
7512 // my extra info set.
7513 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7514
7515 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007516 ListenerInfo li = mListenerInfo;
7517 if (li != null && li.mOnCreateContextMenuListener != null) {
7518 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 }
7520
7521 // Clear the extra information so subsequent items that aren't mine don't
7522 // have my extra info.
7523 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7524
7525 if (mParent != null) {
7526 mParent.createContextMenu(menu);
7527 }
7528 }
7529
7530 /**
7531 * Views should implement this if they have extra information to associate
7532 * with the context menu. The return result is supplied as a parameter to
7533 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7534 * callback.
7535 *
7536 * @return Extra information about the item for which the context menu
7537 * should be shown. This information will vary across different
7538 * subclasses of View.
7539 */
7540 protected ContextMenuInfo getContextMenuInfo() {
7541 return null;
7542 }
7543
7544 /**
7545 * Views should implement this if the view itself is going to add items to
7546 * the context menu.
7547 *
7548 * @param menu the context menu to populate
7549 */
7550 protected void onCreateContextMenu(ContextMenu menu) {
7551 }
7552
7553 /**
7554 * Implement this method to handle trackball motion events. The
7555 * <em>relative</em> movement of the trackball since the last event
7556 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7557 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7558 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7559 * they will often be fractional values, representing the more fine-grained
7560 * movement information available from a trackball).
7561 *
7562 * @param event The motion event.
7563 * @return True if the event was handled, false otherwise.
7564 */
7565 public boolean onTrackballEvent(MotionEvent event) {
7566 return false;
7567 }
7568
7569 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007570 * Implement this method to handle generic motion events.
7571 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007572 * Generic motion events describe joystick movements, mouse hovers, track pad
7573 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007574 * {@link MotionEvent#getSource() source} of the motion event specifies
7575 * the class of input that was received. Implementations of this method
7576 * must examine the bits in the source before processing the event.
7577 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007578 * </p><p>
7579 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7580 * are delivered to the view under the pointer. All other generic motion events are
7581 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007582 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007583 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007584 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007585 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7586 * // process the joystick movement...
7587 * return true;
7588 * }
7589 * }
7590 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7591 * switch (event.getAction()) {
7592 * case MotionEvent.ACTION_HOVER_MOVE:
7593 * // process the mouse hover movement...
7594 * return true;
7595 * case MotionEvent.ACTION_SCROLL:
7596 * // process the scroll wheel movement...
7597 * return true;
7598 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007599 * }
7600 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007601 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007602 *
7603 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007604 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007605 */
7606 public boolean onGenericMotionEvent(MotionEvent event) {
7607 return false;
7608 }
7609
7610 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007611 * Implement this method to handle hover events.
7612 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007613 * This method is called whenever a pointer is hovering into, over, or out of the
7614 * bounds of a view and the view is not currently being touched.
7615 * Hover events are represented as pointer events with action
7616 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7617 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7618 * </p>
7619 * <ul>
7620 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7621 * when the pointer enters the bounds of the view.</li>
7622 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7623 * when the pointer has already entered the bounds of the view and has moved.</li>
7624 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7625 * when the pointer has exited the bounds of the view or when the pointer is
7626 * about to go down due to a button click, tap, or similar user action that
7627 * causes the view to be touched.</li>
7628 * </ul>
7629 * <p>
7630 * The view should implement this method to return true to indicate that it is
7631 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007632 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007633 * The default implementation calls {@link #setHovered} to update the hovered state
7634 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007635 * is enabled and is clickable. The default implementation also sends hover
7636 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007637 * </p>
7638 *
7639 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007640 * @return True if the view handled the hover event.
7641 *
7642 * @see #isHovered
7643 * @see #setHovered
7644 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007645 */
7646 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007647 // The root view may receive hover (or touch) events that are outside the bounds of
7648 // the window. This code ensures that we only send accessibility events for
7649 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007650 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007651 if (!mSendingHoverAccessibilityEvents) {
7652 if ((action == MotionEvent.ACTION_HOVER_ENTER
7653 || action == MotionEvent.ACTION_HOVER_MOVE)
7654 && !hasHoveredChild()
7655 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007656 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007657 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007658 }
7659 } else {
7660 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007661 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007662 && !pointInView(event.getX(), event.getY()))) {
7663 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007664 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007665 // If the window does not have input focus we take away accessibility
7666 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007667 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007668 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007669 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007670 }
Jeff Browna1b24182011-07-28 13:38:24 -07007671 }
7672
Jeff Brown87b7f802011-06-21 18:35:45 -07007673 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007674 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007675 case MotionEvent.ACTION_HOVER_ENTER:
7676 setHovered(true);
7677 break;
7678 case MotionEvent.ACTION_HOVER_EXIT:
7679 setHovered(false);
7680 break;
7681 }
Jeff Browna1b24182011-07-28 13:38:24 -07007682
7683 // Dispatch the event to onGenericMotionEvent before returning true.
7684 // This is to provide compatibility with existing applications that
7685 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7686 // break because of the new default handling for hoverable views
7687 // in onHoverEvent.
7688 // Note that onGenericMotionEvent will be called by default when
7689 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7690 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007691 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007692 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007693
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007694 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007695 }
7696
7697 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007698 * Returns true if the view should handle {@link #onHoverEvent}
7699 * by calling {@link #setHovered} to change its hovered state.
7700 *
7701 * @return True if the view is hoverable.
7702 */
7703 private boolean isHoverable() {
7704 final int viewFlags = mViewFlags;
7705 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7706 return false;
7707 }
7708
7709 return (viewFlags & CLICKABLE) == CLICKABLE
7710 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7711 }
7712
7713 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007714 * Returns true if the view is currently hovered.
7715 *
7716 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007717 *
7718 * @see #setHovered
7719 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007720 */
Jeff Brown10b62902011-06-20 16:40:37 -07007721 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007722 public boolean isHovered() {
7723 return (mPrivateFlags & HOVERED) != 0;
7724 }
7725
7726 /**
7727 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007728 * <p>
7729 * Calling this method also changes the drawable state of the view. This
7730 * enables the view to react to hover by using different drawable resources
7731 * to change its appearance.
7732 * </p><p>
7733 * The {@link #onHoverChanged} method is called when the hovered state changes.
7734 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007735 *
7736 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007737 *
7738 * @see #isHovered
7739 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007740 */
7741 public void setHovered(boolean hovered) {
7742 if (hovered) {
7743 if ((mPrivateFlags & HOVERED) == 0) {
7744 mPrivateFlags |= HOVERED;
7745 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007746 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007747 }
7748 } else {
7749 if ((mPrivateFlags & HOVERED) != 0) {
7750 mPrivateFlags &= ~HOVERED;
7751 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007752 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007753 }
7754 }
7755 }
7756
7757 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007758 * Implement this method to handle hover state changes.
7759 * <p>
7760 * This method is called whenever the hover state changes as a result of a
7761 * call to {@link #setHovered}.
7762 * </p>
7763 *
7764 * @param hovered The current hover state, as returned by {@link #isHovered}.
7765 *
7766 * @see #isHovered
7767 * @see #setHovered
7768 */
7769 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007770 }
7771
7772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 * Implement this method to handle touch screen motion events.
7774 *
7775 * @param event The motion event.
7776 * @return True if the event was handled, false otherwise.
7777 */
7778 public boolean onTouchEvent(MotionEvent event) {
7779 final int viewFlags = mViewFlags;
7780
7781 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007782 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007783 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 // A disabled view that is clickable still consumes the touch
7786 // events, it just doesn't respond to them.
7787 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7788 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7789 }
7790
7791 if (mTouchDelegate != null) {
7792 if (mTouchDelegate.onTouchEvent(event)) {
7793 return true;
7794 }
7795 }
7796
7797 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7798 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7799 switch (event.getAction()) {
7800 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007801 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7802 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 // take focus if we don't have it already and we should in
7804 // touch mode.
7805 boolean focusTaken = false;
7806 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7807 focusTaken = requestFocus();
7808 }
7809
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007810 if (prepressed) {
7811 // The button is being released before we actually
7812 // showed it as pressed. Make it show the pressed
7813 // state now (before scheduling the click) to ensure
7814 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007815 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007816 }
Joe Malin32736f02011-01-19 16:14:20 -08007817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 if (!mHasPerformedLongPress) {
7819 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007820 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821
7822 // Only perform take click actions if we were in the pressed state
7823 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007824 // Use a Runnable and post this rather than calling
7825 // performClick directly. This lets other visual state
7826 // of the view update before click actions start.
7827 if (mPerformClick == null) {
7828 mPerformClick = new PerformClick();
7829 }
7830 if (!post(mPerformClick)) {
7831 performClick();
7832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 }
7834 }
7835
7836 if (mUnsetPressedState == null) {
7837 mUnsetPressedState = new UnsetPressedState();
7838 }
7839
Adam Powelle14579b2009-12-16 18:39:52 -08007840 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007841 postDelayed(mUnsetPressedState,
7842 ViewConfiguration.getPressedStateDuration());
7843 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 // If the post failed, unpress right now
7845 mUnsetPressedState.run();
7846 }
Adam Powelle14579b2009-12-16 18:39:52 -08007847 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849 break;
7850
7851 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007852 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007853
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007854 if (performButtonActionOnTouchDown(event)) {
7855 break;
7856 }
7857
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007858 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007859 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007860
7861 // For views inside a scrolling container, delay the pressed feedback for
7862 // a short period in case this is a scroll.
7863 if (isInScrollingContainer) {
7864 mPrivateFlags |= PREPRESSED;
7865 if (mPendingCheckForTap == null) {
7866 mPendingCheckForTap = new CheckForTap();
7867 }
7868 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7869 } else {
7870 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007871 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007872 checkForLongClick(0);
7873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 break;
7875
7876 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007877 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007878 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 break;
7880
7881 case MotionEvent.ACTION_MOVE:
7882 final int x = (int) event.getX();
7883 final int y = (int) event.getY();
7884
7885 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007886 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007888 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007890 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007891 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892
Adam Powell4d6f0662012-02-21 15:11:11 -08007893 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 }
7896 break;
7897 }
7898 return true;
7899 }
7900
7901 return false;
7902 }
7903
7904 /**
Adam Powell10298662011-08-14 18:26:30 -07007905 * @hide
7906 */
7907 public boolean isInScrollingContainer() {
7908 ViewParent p = getParent();
7909 while (p != null && p instanceof ViewGroup) {
7910 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7911 return true;
7912 }
7913 p = p.getParent();
7914 }
7915 return false;
7916 }
7917
7918 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007919 * Remove the longpress detection timer.
7920 */
7921 private void removeLongPressCallback() {
7922 if (mPendingCheckForLongPress != null) {
7923 removeCallbacks(mPendingCheckForLongPress);
7924 }
7925 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007926
7927 /**
7928 * Remove the pending click action
7929 */
7930 private void removePerformClickCallback() {
7931 if (mPerformClick != null) {
7932 removeCallbacks(mPerformClick);
7933 }
7934 }
7935
Adam Powelle14579b2009-12-16 18:39:52 -08007936 /**
Romain Guya440b002010-02-24 15:57:54 -08007937 * Remove the prepress detection timer.
7938 */
7939 private void removeUnsetPressCallback() {
7940 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7941 setPressed(false);
7942 removeCallbacks(mUnsetPressedState);
7943 }
7944 }
7945
7946 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007947 * Remove the tap detection timer.
7948 */
7949 private void removeTapCallback() {
7950 if (mPendingCheckForTap != null) {
7951 mPrivateFlags &= ~PREPRESSED;
7952 removeCallbacks(mPendingCheckForTap);
7953 }
7954 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007955
7956 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 * Cancels a pending long press. Your subclass can use this if you
7958 * want the context menu to come up if the user presses and holds
7959 * at the same place, but you don't want it to come up if they press
7960 * and then move around enough to cause scrolling.
7961 */
7962 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007963 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007964
7965 /*
7966 * The prepressed state handled by the tap callback is a display
7967 * construct, but the tap callback will post a long press callback
7968 * less its own timeout. Remove it here.
7969 */
7970 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 }
7972
7973 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007974 * Remove the pending callback for sending a
7975 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7976 */
7977 private void removeSendViewScrolledAccessibilityEventCallback() {
7978 if (mSendViewScrolledAccessibilityEvent != null) {
7979 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07007980 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07007981 }
7982 }
7983
7984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 * Sets the TouchDelegate for this View.
7986 */
7987 public void setTouchDelegate(TouchDelegate delegate) {
7988 mTouchDelegate = delegate;
7989 }
7990
7991 /**
7992 * Gets the TouchDelegate for this View.
7993 */
7994 public TouchDelegate getTouchDelegate() {
7995 return mTouchDelegate;
7996 }
7997
7998 /**
7999 * Set flags controlling behavior of this view.
8000 *
8001 * @param flags Constant indicating the value which should be set
8002 * @param mask Constant indicating the bit range that should be changed
8003 */
8004 void setFlags(int flags, int mask) {
8005 int old = mViewFlags;
8006 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8007
8008 int changed = mViewFlags ^ old;
8009 if (changed == 0) {
8010 return;
8011 }
8012 int privateFlags = mPrivateFlags;
8013
8014 /* Check if the FOCUSABLE bit has changed */
8015 if (((changed & FOCUSABLE_MASK) != 0) &&
8016 ((privateFlags & HAS_BOUNDS) !=0)) {
8017 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8018 && ((privateFlags & FOCUSED) != 0)) {
8019 /* Give up focus if we are no longer focusable */
8020 clearFocus();
8021 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8022 && ((privateFlags & FOCUSED) == 0)) {
8023 /*
8024 * Tell the view system that we are now available to take focus
8025 * if no one else already has it.
8026 */
8027 if (mParent != null) mParent.focusableViewAvailable(this);
8028 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008029 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8030 notifyAccessibilityStateChanged();
8031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 }
8033
8034 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8035 if ((changed & VISIBILITY_MASK) != 0) {
8036 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008037 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008038 * it was not visible. Marking it drawn ensures that the invalidation will
8039 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008041 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008042 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043
8044 needGlobalAttributesUpdate(true);
8045
8046 // a view becoming visible is worth notifying the parent
8047 // about in case nothing has focus. even if this specific view
8048 // isn't focusable, it may contain something that is, so let
8049 // the root view try to give this focus if nothing else does.
8050 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8051 mParent.focusableViewAvailable(this);
8052 }
8053 }
8054 }
8055
8056 /* Check if the GONE bit has changed */
8057 if ((changed & GONE) != 0) {
8058 needGlobalAttributesUpdate(false);
8059 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060
Romain Guyecd80ee2009-12-03 17:13:02 -08008061 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8062 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008063 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008064 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008065 if (mParent instanceof View) {
8066 // GONE views noop invalidation, so invalidate the parent
8067 ((View) mParent).invalidate(true);
8068 }
8069 // Mark the view drawn to ensure that it gets invalidated properly the next
8070 // time it is visible and gets invalidated
8071 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 }
8073 if (mAttachInfo != null) {
8074 mAttachInfo.mViewVisibilityChanged = true;
8075 }
8076 }
8077
8078 /* Check if the VISIBLE bit has changed */
8079 if ((changed & INVISIBLE) != 0) {
8080 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008081 /*
8082 * If this view is becoming invisible, set the DRAWN flag so that
8083 * the next invalidate() will not be skipped.
8084 */
8085 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008086
8087 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008088 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 if (getRootView() != this) {
8090 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008091 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 }
8093 }
8094 if (mAttachInfo != null) {
8095 mAttachInfo.mViewVisibilityChanged = true;
8096 }
8097 }
8098
Adam Powell326d8082009-12-09 15:10:07 -08008099 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008100 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008101 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8102 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008103 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008104 } else if (mParent != null) {
8105 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008106 }
Adam Powell326d8082009-12-09 15:10:07 -08008107 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8108 }
8109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8111 destroyDrawingCache();
8112 }
8113
8114 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8115 destroyDrawingCache();
8116 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008117 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 }
8119
8120 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8121 destroyDrawingCache();
8122 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8123 }
8124
8125 if ((changed & DRAW_MASK) != 0) {
8126 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008127 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 mPrivateFlags &= ~SKIP_DRAW;
8129 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8130 } else {
8131 mPrivateFlags |= SKIP_DRAW;
8132 }
8133 } else {
8134 mPrivateFlags &= ~SKIP_DRAW;
8135 }
8136 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008137 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 }
8139
8140 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008141 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 mParent.recomputeViewAttributes(this);
8143 }
8144 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008145
8146 if (AccessibilityManager.getInstance(mContext).isEnabled()
8147 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8148 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8149 notifyAccessibilityStateChanged();
8150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 }
8152
8153 /**
8154 * Change the view's z order in the tree, so it's on top of other sibling
8155 * views
8156 */
8157 public void bringToFront() {
8158 if (mParent != null) {
8159 mParent.bringChildToFront(this);
8160 }
8161 }
8162
8163 /**
8164 * This is called in response to an internal scroll in this view (i.e., the
8165 * view scrolled its own contents). This is typically as a result of
8166 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8167 * called.
8168 *
8169 * @param l Current horizontal scroll origin.
8170 * @param t Current vertical scroll origin.
8171 * @param oldl Previous horizontal scroll origin.
8172 * @param oldt Previous vertical scroll origin.
8173 */
8174 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008175 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8176 postSendViewScrolledAccessibilityEventCallback();
8177 }
8178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 mBackgroundSizeChanged = true;
8180
8181 final AttachInfo ai = mAttachInfo;
8182 if (ai != null) {
8183 ai.mViewScrollChanged = true;
8184 }
8185 }
8186
8187 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008188 * Interface definition for a callback to be invoked when the layout bounds of a view
8189 * changes due to layout processing.
8190 */
8191 public interface OnLayoutChangeListener {
8192 /**
8193 * Called when the focus state of a view has changed.
8194 *
8195 * @param v The view whose state has changed.
8196 * @param left The new value of the view's left property.
8197 * @param top The new value of the view's top property.
8198 * @param right The new value of the view's right property.
8199 * @param bottom The new value of the view's bottom property.
8200 * @param oldLeft The previous value of the view's left property.
8201 * @param oldTop The previous value of the view's top property.
8202 * @param oldRight The previous value of the view's right property.
8203 * @param oldBottom The previous value of the view's bottom property.
8204 */
8205 void onLayoutChange(View v, int left, int top, int right, int bottom,
8206 int oldLeft, int oldTop, int oldRight, int oldBottom);
8207 }
8208
8209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 * This is called during layout when the size of this view has changed. If
8211 * you were just added to the view hierarchy, you're called with the old
8212 * values of 0.
8213 *
8214 * @param w Current width of this view.
8215 * @param h Current height of this view.
8216 * @param oldw Old width of this view.
8217 * @param oldh Old height of this view.
8218 */
8219 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8220 }
8221
8222 /**
8223 * Called by draw to draw the child views. This may be overridden
8224 * by derived classes to gain control just before its children are drawn
8225 * (but after its own view has been drawn).
8226 * @param canvas the canvas on which to draw the view
8227 */
8228 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 }
8231
8232 /**
8233 * Gets the parent of this view. Note that the parent is a
8234 * ViewParent and not necessarily a View.
8235 *
8236 * @return Parent of this view.
8237 */
8238 public final ViewParent getParent() {
8239 return mParent;
8240 }
8241
8242 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008243 * Set the horizontal scrolled position of your view. This will cause a call to
8244 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8245 * invalidated.
8246 * @param value the x position to scroll to
8247 */
8248 public void setScrollX(int value) {
8249 scrollTo(value, mScrollY);
8250 }
8251
8252 /**
8253 * Set the vertical scrolled position of your view. This will cause a call to
8254 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8255 * invalidated.
8256 * @param value the y position to scroll to
8257 */
8258 public void setScrollY(int value) {
8259 scrollTo(mScrollX, value);
8260 }
8261
8262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 * Return the scrolled left position of this view. This is the left edge of
8264 * the displayed part of your view. You do not need to draw any pixels
8265 * farther left, since those are outside of the frame of your view on
8266 * screen.
8267 *
8268 * @return The left edge of the displayed part of your view, in pixels.
8269 */
8270 public final int getScrollX() {
8271 return mScrollX;
8272 }
8273
8274 /**
8275 * Return the scrolled top position of this view. This is the top edge of
8276 * the displayed part of your view. You do not need to draw any pixels above
8277 * it, since those are outside of the frame of your view on screen.
8278 *
8279 * @return The top edge of the displayed part of your view, in pixels.
8280 */
8281 public final int getScrollY() {
8282 return mScrollY;
8283 }
8284
8285 /**
8286 * Return the width of the your view.
8287 *
8288 * @return The width of your view, in pixels.
8289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008290 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 public final int getWidth() {
8292 return mRight - mLeft;
8293 }
8294
8295 /**
8296 * Return the height of your view.
8297 *
8298 * @return The height of your view, in pixels.
8299 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008300 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008301 public final int getHeight() {
8302 return mBottom - mTop;
8303 }
8304
8305 /**
8306 * Return the visible drawing bounds of your view. Fills in the output
8307 * rectangle with the values from getScrollX(), getScrollY(),
8308 * getWidth(), and getHeight().
8309 *
8310 * @param outRect The (scrolled) drawing bounds of the view.
8311 */
8312 public void getDrawingRect(Rect outRect) {
8313 outRect.left = mScrollX;
8314 outRect.top = mScrollY;
8315 outRect.right = mScrollX + (mRight - mLeft);
8316 outRect.bottom = mScrollY + (mBottom - mTop);
8317 }
8318
8319 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008320 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8321 * raw width component (that is the result is masked by
8322 * {@link #MEASURED_SIZE_MASK}).
8323 *
8324 * @return The raw measured width of this view.
8325 */
8326 public final int getMeasuredWidth() {
8327 return mMeasuredWidth & MEASURED_SIZE_MASK;
8328 }
8329
8330 /**
8331 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008332 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008333 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334 * This should be used during measurement and layout calculations only. Use
8335 * {@link #getWidth()} to see how wide a view is after layout.
8336 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008337 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008339 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 return mMeasuredWidth;
8341 }
8342
8343 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008344 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8345 * raw width component (that is the result is masked by
8346 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008348 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 */
8350 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008351 return mMeasuredHeight & MEASURED_SIZE_MASK;
8352 }
8353
8354 /**
8355 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008356 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008357 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8358 * This should be used during measurement and layout calculations only. Use
8359 * {@link #getHeight()} to see how wide a view is after layout.
8360 *
8361 * @return The measured width of this view as a bit mask.
8362 */
8363 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008364 return mMeasuredHeight;
8365 }
8366
8367 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008368 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8369 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8370 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8371 * and the height component is at the shifted bits
8372 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8373 */
8374 public final int getMeasuredState() {
8375 return (mMeasuredWidth&MEASURED_STATE_MASK)
8376 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8377 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8378 }
8379
8380 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008381 * The transform matrix of this view, which is calculated based on the current
8382 * roation, scale, and pivot properties.
8383 *
8384 * @see #getRotation()
8385 * @see #getScaleX()
8386 * @see #getScaleY()
8387 * @see #getPivotX()
8388 * @see #getPivotY()
8389 * @return The current transform matrix for the view
8390 */
8391 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008392 if (mTransformationInfo != null) {
8393 updateMatrix();
8394 return mTransformationInfo.mMatrix;
8395 }
8396 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008397 }
8398
8399 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008400 * Utility function to determine if the value is far enough away from zero to be
8401 * considered non-zero.
8402 * @param value A floating point value to check for zero-ness
8403 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8404 */
8405 private static boolean nonzero(float value) {
8406 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8407 }
8408
8409 /**
Jeff Brown86671742010-09-30 20:00:15 -07008410 * Returns true if the transform matrix is the identity matrix.
8411 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008412 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008413 * @return True if the transform matrix is the identity matrix, false otherwise.
8414 */
Jeff Brown86671742010-09-30 20:00:15 -07008415 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008416 if (mTransformationInfo != null) {
8417 updateMatrix();
8418 return mTransformationInfo.mMatrixIsIdentity;
8419 }
8420 return true;
8421 }
8422
8423 void ensureTransformationInfo() {
8424 if (mTransformationInfo == null) {
8425 mTransformationInfo = new TransformationInfo();
8426 }
Jeff Brown86671742010-09-30 20:00:15 -07008427 }
8428
8429 /**
8430 * Recomputes the transform matrix if necessary.
8431 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008432 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008433 final TransformationInfo info = mTransformationInfo;
8434 if (info == null) {
8435 return;
8436 }
8437 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008438 // transform-related properties have changed since the last time someone
8439 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008440
8441 // Figure out if we need to update the pivot point
8442 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008443 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8444 info.mPrevWidth = mRight - mLeft;
8445 info.mPrevHeight = mBottom - mTop;
8446 info.mPivotX = info.mPrevWidth / 2f;
8447 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008448 }
8449 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008450 info.mMatrix.reset();
8451 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8452 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8453 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8454 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008455 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008456 if (info.mCamera == null) {
8457 info.mCamera = new Camera();
8458 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008459 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008460 info.mCamera.save();
8461 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8462 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8463 info.mCamera.getMatrix(info.matrix3D);
8464 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8465 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8466 info.mPivotY + info.mTranslationY);
8467 info.mMatrix.postConcat(info.matrix3D);
8468 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008469 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008470 info.mMatrixDirty = false;
8471 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8472 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008473 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008474 }
8475
8476 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008477 * When searching for a view to focus this rectangle is used when considering if this view is
8478 * a good candidate for receiving focus.
8479 *
8480 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8481 *
8482 * @param r The rectangle to fill in, in this view's coordinates.
8483 */
8484 public void getFocusRect(Rect r) {
8485 getDrawingRect(r);
8486 }
8487
8488 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008489 * Utility method to retrieve the inverse of the current mMatrix property.
8490 * We cache the matrix to avoid recalculating it when transform properties
8491 * have not changed.
8492 *
8493 * @return The inverse of the current matrix of this view.
8494 */
Jeff Brown86671742010-09-30 20:00:15 -07008495 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008496 final TransformationInfo info = mTransformationInfo;
8497 if (info != null) {
8498 updateMatrix();
8499 if (info.mInverseMatrixDirty) {
8500 if (info.mInverseMatrix == null) {
8501 info.mInverseMatrix = new Matrix();
8502 }
8503 info.mMatrix.invert(info.mInverseMatrix);
8504 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008505 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008506 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008507 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008508 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008509 }
8510
8511 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008512 * Gets the distance along the Z axis from the camera to this view.
8513 *
8514 * @see #setCameraDistance(float)
8515 *
8516 * @return The distance along the Z axis.
8517 */
8518 public float getCameraDistance() {
8519 ensureTransformationInfo();
8520 final float dpi = mResources.getDisplayMetrics().densityDpi;
8521 final TransformationInfo info = mTransformationInfo;
8522 if (info.mCamera == null) {
8523 info.mCamera = new Camera();
8524 info.matrix3D = new Matrix();
8525 }
8526 return -(info.mCamera.getLocationZ() * dpi);
8527 }
8528
8529 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008530 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8531 * views are drawn) from the camera to this view. The camera's distance
8532 * affects 3D transformations, for instance rotations around the X and Y
8533 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008534 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008535 * use a camera distance that's greater than the height (X axis rotation) or
8536 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008537 *
Romain Guya5364ee2011-02-24 14:46:04 -08008538 * <p>The distance of the camera from the view plane can have an affect on the
8539 * perspective distortion of the view when it is rotated around the x or y axis.
8540 * For example, a large distance will result in a large viewing angle, and there
8541 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008542 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008543 * also result in some drawing artifacts if the rotated view ends up partially
8544 * behind the camera (which is why the recommendation is to use a distance at
8545 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008546 *
Romain Guya5364ee2011-02-24 14:46:04 -08008547 * <p>The distance is expressed in "depth pixels." The default distance depends
8548 * on the screen density. For instance, on a medium density display, the
8549 * default distance is 1280. On a high density display, the default distance
8550 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008551 *
Romain Guya5364ee2011-02-24 14:46:04 -08008552 * <p>If you want to specify a distance that leads to visually consistent
8553 * results across various densities, use the following formula:</p>
8554 * <pre>
8555 * float scale = context.getResources().getDisplayMetrics().density;
8556 * view.setCameraDistance(distance * scale);
8557 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008558 *
Romain Guya5364ee2011-02-24 14:46:04 -08008559 * <p>The density scale factor of a high density display is 1.5,
8560 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008561 *
Romain Guya5364ee2011-02-24 14:46:04 -08008562 * @param distance The distance in "depth pixels", if negative the opposite
8563 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008564 *
8565 * @see #setRotationX(float)
8566 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008567 */
8568 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008569 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008570
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008571 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008572 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008573 final TransformationInfo info = mTransformationInfo;
8574 if (info.mCamera == null) {
8575 info.mCamera = new Camera();
8576 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008577 }
8578
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008579 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8580 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008581
Chet Haase9d1992d2012-03-13 11:03:25 -07008582 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008583 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008584 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008585 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008586 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8587 // View was rejected last time it was drawn by its parent; this may have changed
8588 invalidateParentIfNeeded();
8589 }
Romain Guya5364ee2011-02-24 14:46:04 -08008590 }
8591
8592 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008593 * The degrees that the view is rotated around the pivot point.
8594 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008595 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008596 * @see #getPivotX()
8597 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008598 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 * @return The degrees of rotation.
8600 */
Chet Haasea5531132012-02-02 13:41:44 -08008601 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008602 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008603 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008604 }
8605
8606 /**
Chet Haase897247b2010-09-09 14:54:47 -07008607 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8608 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008609 *
8610 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008611 *
8612 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008613 * @see #getPivotX()
8614 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008615 * @see #setRotationX(float)
8616 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008617 *
8618 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008619 */
8620 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008621 ensureTransformationInfo();
8622 final TransformationInfo info = mTransformationInfo;
8623 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008624 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008625 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008626 info.mRotation = rotation;
8627 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008628 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008629 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008630 mDisplayList.setRotation(rotation);
8631 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008632 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8633 // View was rejected last time it was drawn by its parent; this may have changed
8634 invalidateParentIfNeeded();
8635 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008636 }
8637 }
8638
8639 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008640 * The degrees that the view is rotated around the vertical axis through the pivot point.
8641 *
8642 * @see #getPivotX()
8643 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008644 * @see #setRotationY(float)
8645 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008646 * @return The degrees of Y rotation.
8647 */
Chet Haasea5531132012-02-02 13:41:44 -08008648 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008649 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008650 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008651 }
8652
8653 /**
Chet Haase897247b2010-09-09 14:54:47 -07008654 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8655 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8656 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008657 *
Romain Guya5364ee2011-02-24 14:46:04 -08008658 * When rotating large views, it is recommended to adjust the camera distance
8659 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008660 *
8661 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008662 *
8663 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008664 * @see #getPivotX()
8665 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008666 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008667 * @see #setRotationX(float)
8668 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008669 *
8670 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008671 */
8672 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008673 ensureTransformationInfo();
8674 final TransformationInfo info = mTransformationInfo;
8675 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008676 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008677 info.mRotationY = rotationY;
8678 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008679 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008680 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008681 mDisplayList.setRotationY(rotationY);
8682 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008683 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8684 // View was rejected last time it was drawn by its parent; this may have changed
8685 invalidateParentIfNeeded();
8686 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008687 }
8688 }
8689
8690 /**
8691 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8692 *
8693 * @see #getPivotX()
8694 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008695 * @see #setRotationX(float)
8696 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008697 * @return The degrees of X rotation.
8698 */
Chet Haasea5531132012-02-02 13:41:44 -08008699 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008700 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008701 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008702 }
8703
8704 /**
Chet Haase897247b2010-09-09 14:54:47 -07008705 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8706 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8707 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008708 *
Romain Guya5364ee2011-02-24 14:46:04 -08008709 * When rotating large views, it is recommended to adjust the camera distance
8710 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008711 *
8712 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008713 *
8714 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008715 * @see #getPivotX()
8716 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008717 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008718 * @see #setRotationY(float)
8719 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008720 *
8721 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008722 */
8723 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008724 ensureTransformationInfo();
8725 final TransformationInfo info = mTransformationInfo;
8726 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008727 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008728 info.mRotationX = rotationX;
8729 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008730 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008731 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008732 mDisplayList.setRotationX(rotationX);
8733 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008734 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8735 // View was rejected last time it was drawn by its parent; this may have changed
8736 invalidateParentIfNeeded();
8737 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008738 }
8739 }
8740
8741 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008742 * The amount that the view is scaled in x around the pivot point, as a proportion of
8743 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8744 *
Joe Onorato93162322010-09-16 15:42:01 -04008745 * <p>By default, this is 1.0f.
8746 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008747 * @see #getPivotX()
8748 * @see #getPivotY()
8749 * @return The scaling factor.
8750 */
Chet Haasea5531132012-02-02 13:41:44 -08008751 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008752 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008753 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008754 }
8755
8756 /**
8757 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8758 * the view's unscaled width. A value of 1 means that no scaling is applied.
8759 *
8760 * @param scaleX The scaling factor.
8761 * @see #getPivotX()
8762 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008763 *
8764 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008765 */
8766 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008767 ensureTransformationInfo();
8768 final TransformationInfo info = mTransformationInfo;
8769 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008770 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008771 info.mScaleX = scaleX;
8772 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008773 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008774 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008775 mDisplayList.setScaleX(scaleX);
8776 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008777 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8778 // View was rejected last time it was drawn by its parent; this may have changed
8779 invalidateParentIfNeeded();
8780 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008781 }
8782 }
8783
8784 /**
8785 * The amount that the view is scaled in y around the pivot point, as a proportion of
8786 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8787 *
Joe Onorato93162322010-09-16 15:42:01 -04008788 * <p>By default, this is 1.0f.
8789 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008790 * @see #getPivotX()
8791 * @see #getPivotY()
8792 * @return The scaling factor.
8793 */
Chet Haasea5531132012-02-02 13:41:44 -08008794 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008795 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008796 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008797 }
8798
8799 /**
8800 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8801 * the view's unscaled width. A value of 1 means that no scaling is applied.
8802 *
8803 * @param scaleY The scaling factor.
8804 * @see #getPivotX()
8805 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008806 *
8807 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008808 */
8809 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008810 ensureTransformationInfo();
8811 final TransformationInfo info = mTransformationInfo;
8812 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008813 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008814 info.mScaleY = scaleY;
8815 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008816 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008817 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008818 mDisplayList.setScaleY(scaleY);
8819 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008820 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8821 // View was rejected last time it was drawn by its parent; this may have changed
8822 invalidateParentIfNeeded();
8823 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008824 }
8825 }
8826
8827 /**
8828 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8829 * and {@link #setScaleX(float) scaled}.
8830 *
8831 * @see #getRotation()
8832 * @see #getScaleX()
8833 * @see #getScaleY()
8834 * @see #getPivotY()
8835 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008836 *
8837 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008838 */
Chet Haasea5531132012-02-02 13:41:44 -08008839 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008841 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008842 }
8843
8844 /**
8845 * Sets the x location of the point around which the view is
8846 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008847 * By default, the pivot point is centered on the object.
8848 * Setting this property disables this behavior and causes the view to use only the
8849 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008850 *
8851 * @param pivotX The x location of the pivot point.
8852 * @see #getRotation()
8853 * @see #getScaleX()
8854 * @see #getScaleY()
8855 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008856 *
8857 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008858 */
8859 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008860 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008861 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008862 final TransformationInfo info = mTransformationInfo;
8863 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008864 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008865 info.mPivotX = pivotX;
8866 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008867 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008868 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008869 mDisplayList.setPivotX(pivotX);
8870 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008871 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8872 // View was rejected last time it was drawn by its parent; this may have changed
8873 invalidateParentIfNeeded();
8874 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008875 }
8876 }
8877
8878 /**
8879 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8880 * and {@link #setScaleY(float) scaled}.
8881 *
8882 * @see #getRotation()
8883 * @see #getScaleX()
8884 * @see #getScaleY()
8885 * @see #getPivotY()
8886 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008887 *
8888 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008889 */
Chet Haasea5531132012-02-02 13:41:44 -08008890 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008891 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008892 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008893 }
8894
8895 /**
8896 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008897 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8898 * Setting this property disables this behavior and causes the view to use only the
8899 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008900 *
8901 * @param pivotY The y location of the pivot point.
8902 * @see #getRotation()
8903 * @see #getScaleX()
8904 * @see #getScaleY()
8905 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008906 *
8907 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008908 */
8909 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008910 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008911 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008912 final TransformationInfo info = mTransformationInfo;
8913 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008914 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008915 info.mPivotY = pivotY;
8916 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008917 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008918 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008919 mDisplayList.setPivotY(pivotY);
8920 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008921 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8922 // View was rejected last time it was drawn by its parent; this may have changed
8923 invalidateParentIfNeeded();
8924 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008925 }
8926 }
8927
8928 /**
8929 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8930 * completely transparent and 1 means the view is completely opaque.
8931 *
Joe Onorato93162322010-09-16 15:42:01 -04008932 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 * @return The opacity of the view.
8934 */
Chet Haasea5531132012-02-02 13:41:44 -08008935 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008936 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008937 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008938 }
8939
8940 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008941 * Returns whether this View has content which overlaps. This function, intended to be
8942 * overridden by specific View types, is an optimization when alpha is set on a view. If
8943 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8944 * and then composited it into place, which can be expensive. If the view has no overlapping
8945 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8946 * An example of overlapping rendering is a TextView with a background image, such as a
8947 * Button. An example of non-overlapping rendering is a TextView with no background, or
8948 * an ImageView with only the foreground image. The default implementation returns true;
8949 * subclasses should override if they have cases which can be optimized.
8950 *
8951 * @return true if the content in this view might overlap, false otherwise.
8952 */
8953 public boolean hasOverlappingRendering() {
8954 return true;
8955 }
8956
8957 /**
Romain Guy171c5922011-01-06 10:04:23 -08008958 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8959 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008960 *
Romain Guy171c5922011-01-06 10:04:23 -08008961 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8962 * responsible for applying the opacity itself. Otherwise, calling this method is
8963 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008964 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008965 *
Chet Haasea5531132012-02-02 13:41:44 -08008966 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8967 * performance implications. It is generally best to use the alpha property sparingly and
8968 * transiently, as in the case of fading animations.</p>
8969 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008970 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008971 *
Joe Malin32736f02011-01-19 16:14:20 -08008972 * @see #setLayerType(int, android.graphics.Paint)
8973 *
Chet Haase73066682010-11-29 15:55:32 -08008974 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008975 */
8976 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008977 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008978 if (mTransformationInfo.mAlpha != alpha) {
8979 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008980 if (onSetAlpha((int) (alpha * 255))) {
8981 mPrivateFlags |= ALPHA_SET;
8982 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008983 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008984 invalidate(true);
8985 } else {
8986 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008987 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008988 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008989 mDisplayList.setAlpha(alpha);
8990 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008991 }
Chet Haaseed032702010-10-01 14:05:54 -07008992 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008993 }
8994
8995 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008996 * Faster version of setAlpha() which performs the same steps except there are
8997 * no calls to invalidate(). The caller of this function should perform proper invalidation
8998 * on the parent and this object. The return value indicates whether the subclass handles
8999 * alpha (the return value for onSetAlpha()).
9000 *
9001 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009002 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9003 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009004 */
9005 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009006 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009007 if (mTransformationInfo.mAlpha != alpha) {
9008 mTransformationInfo.mAlpha = alpha;
9009 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9010 if (subclassHandlesAlpha) {
9011 mPrivateFlags |= ALPHA_SET;
9012 return true;
9013 } else {
9014 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009015 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009016 mDisplayList.setAlpha(alpha);
9017 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009018 }
Chet Haasea00f3862011-02-22 06:34:40 -08009019 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009020 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009021 }
9022
9023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009024 * Top position of this view relative to its parent.
9025 *
9026 * @return The top of this view, in pixels.
9027 */
9028 @ViewDebug.CapturedViewProperty
9029 public final int getTop() {
9030 return mTop;
9031 }
9032
9033 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009034 * Sets the top position of this view relative to its parent. This method is meant to be called
9035 * by the layout system and should not generally be called otherwise, because the property
9036 * may be changed at any time by the layout.
9037 *
9038 * @param top The top of this view, in pixels.
9039 */
9040 public final void setTop(int top) {
9041 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009042 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009043 final boolean matrixIsIdentity = mTransformationInfo == null
9044 || mTransformationInfo.mMatrixIsIdentity;
9045 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009046 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009047 int minTop;
9048 int yLoc;
9049 if (top < mTop) {
9050 minTop = top;
9051 yLoc = top - mTop;
9052 } else {
9053 minTop = mTop;
9054 yLoc = 0;
9055 }
Chet Haasee9140a72011-02-16 16:23:29 -08009056 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009057 }
9058 } else {
9059 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009060 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009061 }
9062
Chet Haaseed032702010-10-01 14:05:54 -07009063 int width = mRight - mLeft;
9064 int oldHeight = mBottom - mTop;
9065
Chet Haase21cd1382010-09-01 17:42:29 -07009066 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009067 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009068 mDisplayList.setTop(mTop);
9069 }
Chet Haase21cd1382010-09-01 17:42:29 -07009070
Chet Haaseed032702010-10-01 14:05:54 -07009071 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9072
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009073 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009074 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9075 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009076 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009077 }
Chet Haase21cd1382010-09-01 17:42:29 -07009078 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009079 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009080 }
Chet Haase55dbb652010-12-21 20:15:08 -08009081 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009082 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009083 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9084 // View was rejected last time it was drawn by its parent; this may have changed
9085 invalidateParentIfNeeded();
9086 }
Chet Haase21cd1382010-09-01 17:42:29 -07009087 }
9088 }
9089
9090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 * Bottom position of this view relative to its parent.
9092 *
9093 * @return The bottom of this view, in pixels.
9094 */
9095 @ViewDebug.CapturedViewProperty
9096 public final int getBottom() {
9097 return mBottom;
9098 }
9099
9100 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009101 * True if this view has changed since the last time being drawn.
9102 *
9103 * @return The dirty state of this view.
9104 */
9105 public boolean isDirty() {
9106 return (mPrivateFlags & DIRTY_MASK) != 0;
9107 }
9108
9109 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009110 * Sets the bottom position of this view relative to its parent. This method is meant to be
9111 * called by the layout system and should not generally be called otherwise, because the
9112 * property may be changed at any time by the layout.
9113 *
9114 * @param bottom The bottom of this view, in pixels.
9115 */
9116 public final void setBottom(int bottom) {
9117 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009118 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009119 final boolean matrixIsIdentity = mTransformationInfo == null
9120 || mTransformationInfo.mMatrixIsIdentity;
9121 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009122 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009123 int maxBottom;
9124 if (bottom < mBottom) {
9125 maxBottom = mBottom;
9126 } else {
9127 maxBottom = bottom;
9128 }
Chet Haasee9140a72011-02-16 16:23:29 -08009129 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009130 }
9131 } else {
9132 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009133 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009134 }
9135
Chet Haaseed032702010-10-01 14:05:54 -07009136 int width = mRight - mLeft;
9137 int oldHeight = mBottom - mTop;
9138
Chet Haase21cd1382010-09-01 17:42:29 -07009139 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009140 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009141 mDisplayList.setBottom(mBottom);
9142 }
Chet Haase21cd1382010-09-01 17:42:29 -07009143
Chet Haaseed032702010-10-01 14:05:54 -07009144 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9145
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009146 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009147 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9148 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009149 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009150 }
Chet Haase21cd1382010-09-01 17:42:29 -07009151 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009152 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009153 }
Chet Haase55dbb652010-12-21 20:15:08 -08009154 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009155 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009156 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9157 // View was rejected last time it was drawn by its parent; this may have changed
9158 invalidateParentIfNeeded();
9159 }
Chet Haase21cd1382010-09-01 17:42:29 -07009160 }
9161 }
9162
9163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 * Left position of this view relative to its parent.
9165 *
9166 * @return The left edge of this view, in pixels.
9167 */
9168 @ViewDebug.CapturedViewProperty
9169 public final int getLeft() {
9170 return mLeft;
9171 }
9172
9173 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009174 * Sets the left position of this view relative to its parent. This method is meant to be called
9175 * by the layout system and should not generally be called otherwise, because the property
9176 * may be changed at any time by the layout.
9177 *
9178 * @param left The bottom of this view, in pixels.
9179 */
9180 public final void setLeft(int left) {
9181 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009182 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009183 final boolean matrixIsIdentity = mTransformationInfo == null
9184 || mTransformationInfo.mMatrixIsIdentity;
9185 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009186 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009187 int minLeft;
9188 int xLoc;
9189 if (left < mLeft) {
9190 minLeft = left;
9191 xLoc = left - mLeft;
9192 } else {
9193 minLeft = mLeft;
9194 xLoc = 0;
9195 }
Chet Haasee9140a72011-02-16 16:23:29 -08009196 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009197 }
9198 } else {
9199 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009200 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009201 }
9202
Chet Haaseed032702010-10-01 14:05:54 -07009203 int oldWidth = mRight - mLeft;
9204 int height = mBottom - mTop;
9205
Chet Haase21cd1382010-09-01 17:42:29 -07009206 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009207 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009208 mDisplayList.setLeft(left);
9209 }
Chet Haase21cd1382010-09-01 17:42:29 -07009210
Chet Haaseed032702010-10-01 14:05:54 -07009211 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9212
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009213 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009214 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9215 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009216 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009217 }
Chet Haase21cd1382010-09-01 17:42:29 -07009218 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009219 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009220 }
Chet Haase55dbb652010-12-21 20:15:08 -08009221 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009222 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009223 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9224 // View was rejected last time it was drawn by its parent; this may have changed
9225 invalidateParentIfNeeded();
9226 }
Chet Haase21cd1382010-09-01 17:42:29 -07009227 }
9228 }
9229
9230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 * Right position of this view relative to its parent.
9232 *
9233 * @return The right edge of this view, in pixels.
9234 */
9235 @ViewDebug.CapturedViewProperty
9236 public final int getRight() {
9237 return mRight;
9238 }
9239
9240 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009241 * Sets the right position of this view relative to its parent. This method is meant to be called
9242 * by the layout system and should not generally be called otherwise, because the property
9243 * may be changed at any time by the layout.
9244 *
9245 * @param right The bottom of this view, in pixels.
9246 */
9247 public final void setRight(int right) {
9248 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009249 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009250 final boolean matrixIsIdentity = mTransformationInfo == null
9251 || mTransformationInfo.mMatrixIsIdentity;
9252 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009253 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009254 int maxRight;
9255 if (right < mRight) {
9256 maxRight = mRight;
9257 } else {
9258 maxRight = right;
9259 }
Chet Haasee9140a72011-02-16 16:23:29 -08009260 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009261 }
9262 } else {
9263 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009264 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009265 }
9266
Chet Haaseed032702010-10-01 14:05:54 -07009267 int oldWidth = mRight - mLeft;
9268 int height = mBottom - mTop;
9269
Chet Haase21cd1382010-09-01 17:42:29 -07009270 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009271 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009272 mDisplayList.setRight(mRight);
9273 }
Chet Haase21cd1382010-09-01 17:42:29 -07009274
Chet Haaseed032702010-10-01 14:05:54 -07009275 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9276
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009277 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009278 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9279 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009280 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009281 }
Chet Haase21cd1382010-09-01 17:42:29 -07009282 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009283 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009284 }
Chet Haase55dbb652010-12-21 20:15:08 -08009285 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009286 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009287 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9288 // View was rejected last time it was drawn by its parent; this may have changed
9289 invalidateParentIfNeeded();
9290 }
Chet Haase21cd1382010-09-01 17:42:29 -07009291 }
9292 }
9293
9294 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009295 * The visual x position of this view, in pixels. This is equivalent to the
9296 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009297 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009298 *
Chet Haasedf030d22010-07-30 17:22:38 -07009299 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009300 */
Chet Haasea5531132012-02-02 13:41:44 -08009301 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009302 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009303 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009304 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009305
Chet Haasedf030d22010-07-30 17:22:38 -07009306 /**
9307 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9308 * {@link #setTranslationX(float) translationX} property to be the difference between
9309 * the x value passed in and the current {@link #getLeft() left} property.
9310 *
9311 * @param x The visual x position of this view, in pixels.
9312 */
9313 public void setX(float x) {
9314 setTranslationX(x - mLeft);
9315 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009316
Chet Haasedf030d22010-07-30 17:22:38 -07009317 /**
9318 * The visual y position of this view, in pixels. This is equivalent to the
9319 * {@link #setTranslationY(float) translationY} property plus the current
9320 * {@link #getTop() top} property.
9321 *
9322 * @return The visual y position of this view, in pixels.
9323 */
Chet Haasea5531132012-02-02 13:41:44 -08009324 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009325 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009326 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009327 }
9328
9329 /**
9330 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9331 * {@link #setTranslationY(float) translationY} property to be the difference between
9332 * the y value passed in and the current {@link #getTop() top} property.
9333 *
9334 * @param y The visual y position of this view, in pixels.
9335 */
9336 public void setY(float y) {
9337 setTranslationY(y - mTop);
9338 }
9339
9340
9341 /**
9342 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9343 * This position is post-layout, in addition to wherever the object's
9344 * layout placed it.
9345 *
9346 * @return The horizontal position of this view relative to its left position, in pixels.
9347 */
Chet Haasea5531132012-02-02 13:41:44 -08009348 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009349 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009350 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009351 }
9352
9353 /**
9354 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9355 * This effectively positions the object post-layout, in addition to wherever the object's
9356 * layout placed it.
9357 *
9358 * @param translationX The horizontal position of this view relative to its left position,
9359 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009360 *
9361 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009362 */
9363 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009364 ensureTransformationInfo();
9365 final TransformationInfo info = mTransformationInfo;
9366 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009367 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009368 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009369 info.mTranslationX = translationX;
9370 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009371 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009372 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009373 mDisplayList.setTranslationX(translationX);
9374 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009375 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9376 // View was rejected last time it was drawn by its parent; this may have changed
9377 invalidateParentIfNeeded();
9378 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009379 }
9380 }
9381
9382 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009383 * The horizontal location of this view relative to its {@link #getTop() top} position.
9384 * This position is post-layout, in addition to wherever the object's
9385 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009386 *
Chet Haasedf030d22010-07-30 17:22:38 -07009387 * @return The vertical position of this view relative to its top position,
9388 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009389 */
Chet Haasea5531132012-02-02 13:41:44 -08009390 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009391 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009392 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009393 }
9394
9395 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009396 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9397 * This effectively positions the object post-layout, in addition to wherever the object's
9398 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009399 *
Chet Haasedf030d22010-07-30 17:22:38 -07009400 * @param translationY The vertical position of this view relative to its top position,
9401 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009402 *
9403 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009404 */
Chet Haasedf030d22010-07-30 17:22:38 -07009405 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009406 ensureTransformationInfo();
9407 final TransformationInfo info = mTransformationInfo;
9408 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009409 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009410 info.mTranslationY = translationY;
9411 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009412 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009413 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009414 mDisplayList.setTranslationY(translationY);
9415 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009416 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9417 // View was rejected last time it was drawn by its parent; this may have changed
9418 invalidateParentIfNeeded();
9419 }
Chet Haasedf030d22010-07-30 17:22:38 -07009420 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009421 }
9422
9423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 * Hit rectangle in parent's coordinates
9425 *
9426 * @param outRect The hit rectangle of the view.
9427 */
9428 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009429 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009430 final TransformationInfo info = mTransformationInfo;
9431 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009432 outRect.set(mLeft, mTop, mRight, mBottom);
9433 } else {
9434 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009435 tmpRect.set(-info.mPivotX, -info.mPivotY,
9436 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9437 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009438 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9439 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009440 }
9441 }
9442
9443 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009444 * Determines whether the given point, in local coordinates is inside the view.
9445 */
9446 /*package*/ final boolean pointInView(float localX, float localY) {
9447 return localX >= 0 && localX < (mRight - mLeft)
9448 && localY >= 0 && localY < (mBottom - mTop);
9449 }
9450
9451 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009452 * Utility method to determine whether the given point, in local coordinates,
9453 * is inside the view, where the area of the view is expanded by the slop factor.
9454 * This method is called while processing touch-move events to determine if the event
9455 * is still within the view.
9456 */
9457 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009458 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009459 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009460 }
9461
9462 /**
9463 * When a view has focus and the user navigates away from it, the next view is searched for
9464 * starting from the rectangle filled in by this method.
9465 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009466 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9467 * of the view. However, if your view maintains some idea of internal selection,
9468 * such as a cursor, or a selected row or column, you should override this method and
9469 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 *
9471 * @param r The rectangle to fill in, in this view's coordinates.
9472 */
9473 public void getFocusedRect(Rect r) {
9474 getDrawingRect(r);
9475 }
9476
9477 /**
9478 * If some part of this view is not clipped by any of its parents, then
9479 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009480 * coordinates (without taking possible View rotations into account), offset
9481 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9482 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 *
9484 * @param r If true is returned, r holds the global coordinates of the
9485 * visible portion of this view.
9486 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9487 * between this view and its root. globalOffet may be null.
9488 * @return true if r is non-empty (i.e. part of the view is visible at the
9489 * root level.
9490 */
9491 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9492 int width = mRight - mLeft;
9493 int height = mBottom - mTop;
9494 if (width > 0 && height > 0) {
9495 r.set(0, 0, width, height);
9496 if (globalOffset != null) {
9497 globalOffset.set(-mScrollX, -mScrollY);
9498 }
9499 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9500 }
9501 return false;
9502 }
9503
9504 public final boolean getGlobalVisibleRect(Rect r) {
9505 return getGlobalVisibleRect(r, null);
9506 }
9507
9508 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009509 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 if (getGlobalVisibleRect(r, offset)) {
9511 r.offset(-offset.x, -offset.y); // make r local
9512 return true;
9513 }
9514 return false;
9515 }
9516
9517 /**
9518 * Offset this view's vertical location by the specified number of pixels.
9519 *
9520 * @param offset the number of pixels to offset the view by
9521 */
9522 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009523 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009524 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009525 final boolean matrixIsIdentity = mTransformationInfo == null
9526 || mTransformationInfo.mMatrixIsIdentity;
9527 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009528 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009529 invalidateViewProperty(false, false);
9530 } else {
9531 final ViewParent p = mParent;
9532 if (p != null && mAttachInfo != null) {
9533 final Rect r = mAttachInfo.mTmpInvalRect;
9534 int minTop;
9535 int maxBottom;
9536 int yLoc;
9537 if (offset < 0) {
9538 minTop = mTop + offset;
9539 maxBottom = mBottom;
9540 yLoc = offset;
9541 } else {
9542 minTop = mTop;
9543 maxBottom = mBottom + offset;
9544 yLoc = 0;
9545 }
9546 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9547 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009548 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009549 }
9550 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009551 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009552 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009553
Chet Haasec3aa3612010-06-17 08:50:37 -07009554 mTop += offset;
9555 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009556 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009557 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009558 invalidateViewProperty(false, false);
9559 } else {
9560 if (!matrixIsIdentity) {
9561 invalidateViewProperty(false, true);
9562 }
9563 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009564 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009566 }
9567
9568 /**
9569 * Offset this view's horizontal location by the specified amount of pixels.
9570 *
9571 * @param offset the numer of pixels to offset the view by
9572 */
9573 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009574 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009575 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009576 final boolean matrixIsIdentity = mTransformationInfo == null
9577 || mTransformationInfo.mMatrixIsIdentity;
9578 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009579 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009580 invalidateViewProperty(false, false);
9581 } else {
9582 final ViewParent p = mParent;
9583 if (p != null && mAttachInfo != null) {
9584 final Rect r = mAttachInfo.mTmpInvalRect;
9585 int minLeft;
9586 int maxRight;
9587 if (offset < 0) {
9588 minLeft = mLeft + offset;
9589 maxRight = mRight;
9590 } else {
9591 minLeft = mLeft;
9592 maxRight = mRight + offset;
9593 }
9594 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9595 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009596 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009597 }
9598 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009599 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009600 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009601
Chet Haasec3aa3612010-06-17 08:50:37 -07009602 mLeft += offset;
9603 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009604 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009605 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009606 invalidateViewProperty(false, false);
9607 } else {
9608 if (!matrixIsIdentity) {
9609 invalidateViewProperty(false, true);
9610 }
9611 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009612 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614 }
9615
9616 /**
9617 * Get the LayoutParams associated with this view. All views should have
9618 * layout parameters. These supply parameters to the <i>parent</i> of this
9619 * view specifying how it should be arranged. There are many subclasses of
9620 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9621 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009622 *
9623 * This method may return null if this View is not attached to a parent
9624 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9625 * was not invoked successfully. When a View is attached to a parent
9626 * ViewGroup, this method must not return null.
9627 *
9628 * @return The LayoutParams associated with this view, or null if no
9629 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009630 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009631 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 public ViewGroup.LayoutParams getLayoutParams() {
9633 return mLayoutParams;
9634 }
9635
9636 /**
9637 * Set the layout parameters associated with this view. These supply
9638 * parameters to the <i>parent</i> of this view specifying how it should be
9639 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9640 * correspond to the different subclasses of ViewGroup that are responsible
9641 * for arranging their children.
9642 *
Romain Guy01c174b2011-02-22 11:51:06 -08009643 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 */
9645 public void setLayoutParams(ViewGroup.LayoutParams params) {
9646 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009647 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 }
9649 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009650 if (mParent instanceof ViewGroup) {
9651 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 requestLayout();
9654 }
9655
9656 /**
9657 * Set the scrolled position of your view. This will cause a call to
9658 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9659 * invalidated.
9660 * @param x the x position to scroll to
9661 * @param y the y position to scroll to
9662 */
9663 public void scrollTo(int x, int y) {
9664 if (mScrollX != x || mScrollY != y) {
9665 int oldX = mScrollX;
9666 int oldY = mScrollY;
9667 mScrollX = x;
9668 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009669 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009670 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009671 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009672 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 }
9675 }
9676
9677 /**
9678 * Move the scrolled position of your view. This will cause a call to
9679 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9680 * invalidated.
9681 * @param x the amount of pixels to scroll by horizontally
9682 * @param y the amount of pixels to scroll by vertically
9683 */
9684 public void scrollBy(int x, int y) {
9685 scrollTo(mScrollX + x, mScrollY + y);
9686 }
9687
9688 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009689 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9690 * animation to fade the scrollbars out after a default delay. If a subclass
9691 * provides animated scrolling, the start delay should equal the duration
9692 * of the scrolling animation.</p>
9693 *
9694 * <p>The animation starts only if at least one of the scrollbars is
9695 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9696 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9697 * this method returns true, and false otherwise. If the animation is
9698 * started, this method calls {@link #invalidate()}; in that case the
9699 * caller should not call {@link #invalidate()}.</p>
9700 *
9701 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009702 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009703 *
9704 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9705 * and {@link #scrollTo(int, int)}.</p>
9706 *
9707 * @return true if the animation is played, false otherwise
9708 *
9709 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009710 * @see #scrollBy(int, int)
9711 * @see #scrollTo(int, int)
9712 * @see #isHorizontalScrollBarEnabled()
9713 * @see #isVerticalScrollBarEnabled()
9714 * @see #setHorizontalScrollBarEnabled(boolean)
9715 * @see #setVerticalScrollBarEnabled(boolean)
9716 */
9717 protected boolean awakenScrollBars() {
9718 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009719 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009720 }
9721
9722 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009723 * Trigger the scrollbars to draw.
9724 * This method differs from awakenScrollBars() only in its default duration.
9725 * initialAwakenScrollBars() will show the scroll bars for longer than
9726 * usual to give the user more of a chance to notice them.
9727 *
9728 * @return true if the animation is played, false otherwise.
9729 */
9730 private boolean initialAwakenScrollBars() {
9731 return mScrollCache != null &&
9732 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9733 }
9734
9735 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009736 * <p>
9737 * Trigger the scrollbars to draw. When invoked this method starts an
9738 * animation to fade the scrollbars out after a fixed delay. If a subclass
9739 * provides animated scrolling, the start delay should equal the duration of
9740 * the scrolling animation.
9741 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009742 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009743 * <p>
9744 * The animation starts only if at least one of the scrollbars is enabled,
9745 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9746 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9747 * this method returns true, and false otherwise. If the animation is
9748 * started, this method calls {@link #invalidate()}; in that case the caller
9749 * should not call {@link #invalidate()}.
9750 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009751 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009752 * <p>
9753 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009754 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009755 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009756 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009757 * @param startDelay the delay, in milliseconds, after which the animation
9758 * should start; when the delay is 0, the animation starts
9759 * immediately
9760 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009761 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009762 * @see #scrollBy(int, int)
9763 * @see #scrollTo(int, int)
9764 * @see #isHorizontalScrollBarEnabled()
9765 * @see #isVerticalScrollBarEnabled()
9766 * @see #setHorizontalScrollBarEnabled(boolean)
9767 * @see #setVerticalScrollBarEnabled(boolean)
9768 */
9769 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009770 return awakenScrollBars(startDelay, true);
9771 }
Joe Malin32736f02011-01-19 16:14:20 -08009772
Mike Cleron290947b2009-09-29 18:34:32 -07009773 /**
9774 * <p>
9775 * Trigger the scrollbars to draw. When invoked this method starts an
9776 * animation to fade the scrollbars out after a fixed delay. If a subclass
9777 * provides animated scrolling, the start delay should equal the duration of
9778 * the scrolling animation.
9779 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009780 *
Mike Cleron290947b2009-09-29 18:34:32 -07009781 * <p>
9782 * The animation starts only if at least one of the scrollbars is enabled,
9783 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9784 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9785 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009786 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009787 * is set to true; in that case the caller
9788 * should not call {@link #invalidate()}.
9789 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009790 *
Mike Cleron290947b2009-09-29 18:34:32 -07009791 * <p>
9792 * This method should be invoked everytime a subclass directly updates the
9793 * scroll parameters.
9794 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009795 *
Mike Cleron290947b2009-09-29 18:34:32 -07009796 * @param startDelay the delay, in milliseconds, after which the animation
9797 * should start; when the delay is 0, the animation starts
9798 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009799 *
Mike Cleron290947b2009-09-29 18:34:32 -07009800 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009801 *
Mike Cleron290947b2009-09-29 18:34:32 -07009802 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009803 *
Mike Cleron290947b2009-09-29 18:34:32 -07009804 * @see #scrollBy(int, int)
9805 * @see #scrollTo(int, int)
9806 * @see #isHorizontalScrollBarEnabled()
9807 * @see #isVerticalScrollBarEnabled()
9808 * @see #setHorizontalScrollBarEnabled(boolean)
9809 * @see #setVerticalScrollBarEnabled(boolean)
9810 */
9811 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009812 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009813
Mike Cleronf116bf82009-09-27 19:14:12 -07009814 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9815 return false;
9816 }
9817
9818 if (scrollCache.scrollBar == null) {
9819 scrollCache.scrollBar = new ScrollBarDrawable();
9820 }
9821
9822 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9823
Mike Cleron290947b2009-09-29 18:34:32 -07009824 if (invalidate) {
9825 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009826 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009827 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009828
9829 if (scrollCache.state == ScrollabilityCache.OFF) {
9830 // FIXME: this is copied from WindowManagerService.
9831 // We should get this value from the system when it
9832 // is possible to do so.
9833 final int KEY_REPEAT_FIRST_DELAY = 750;
9834 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9835 }
9836
9837 // Tell mScrollCache when we should start fading. This may
9838 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009839 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009840 scrollCache.fadeStartTime = fadeStartTime;
9841 scrollCache.state = ScrollabilityCache.ON;
9842
9843 // Schedule our fader to run, unscheduling any old ones first
9844 if (mAttachInfo != null) {
9845 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9846 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9847 }
9848
9849 return true;
9850 }
9851
9852 return false;
9853 }
9854
9855 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009856 * Do not invalidate views which are not visible and which are not running an animation. They
9857 * will not get drawn and they should not set dirty flags as if they will be drawn
9858 */
9859 private boolean skipInvalidate() {
9860 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9861 (!(mParent instanceof ViewGroup) ||
9862 !((ViewGroup) mParent).isViewTransitioning(this));
9863 }
9864 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009865 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009866 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9867 * in the future. This must be called from a UI thread. To call from a non-UI
9868 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 *
9870 * WARNING: This method is destructive to dirty.
9871 * @param dirty the rectangle representing the bounds of the dirty region
9872 */
9873 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009874 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009875 return;
9876 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009877 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009878 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9879 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009881 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009882 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 final ViewParent p = mParent;
9884 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009885 //noinspection PointlessBooleanExpression,ConstantConditions
9886 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9887 if (p != null && ai != null && ai.mHardwareAccelerated) {
9888 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009889 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009890 p.invalidateChild(this, null);
9891 return;
9892 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 if (p != null && ai != null) {
9895 final int scrollX = mScrollX;
9896 final int scrollY = mScrollY;
9897 final Rect r = ai.mTmpInvalRect;
9898 r.set(dirty.left - scrollX, dirty.top - scrollY,
9899 dirty.right - scrollX, dirty.bottom - scrollY);
9900 mParent.invalidateChild(this, r);
9901 }
9902 }
9903 }
9904
9905 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009906 * 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 -08009907 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009908 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9909 * will be called at some point in the future. This must be called from
9910 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 * @param l the left position of the dirty region
9912 * @param t the top position of the dirty region
9913 * @param r the right position of the dirty region
9914 * @param b the bottom position of the dirty region
9915 */
9916 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009917 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009918 return;
9919 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009920 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009921 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9922 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009924 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009925 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 final ViewParent p = mParent;
9927 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009928 //noinspection PointlessBooleanExpression,ConstantConditions
9929 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9930 if (p != null && ai != null && ai.mHardwareAccelerated) {
9931 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009932 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009933 p.invalidateChild(this, null);
9934 return;
9935 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009937 if (p != null && ai != null && l < r && t < b) {
9938 final int scrollX = mScrollX;
9939 final int scrollY = mScrollY;
9940 final Rect tmpr = ai.mTmpInvalRect;
9941 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9942 p.invalidateChild(this, tmpr);
9943 }
9944 }
9945 }
9946
9947 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009948 * Invalidate the whole view. If the view is visible,
9949 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9950 * the future. This must be called from a UI thread. To call from a non-UI thread,
9951 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 */
9953 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009954 invalidate(true);
9955 }
Joe Malin32736f02011-01-19 16:14:20 -08009956
Chet Haaseed032702010-10-01 14:05:54 -07009957 /**
9958 * This is where the invalidate() work actually happens. A full invalidate()
9959 * causes the drawing cache to be invalidated, but this function can be called with
9960 * invalidateCache set to false to skip that invalidation step for cases that do not
9961 * need it (for example, a component that remains at the same dimensions with the same
9962 * content).
9963 *
9964 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9965 * well. This is usually true for a full invalidate, but may be set to false if the
9966 * View's contents or dimensions have not changed.
9967 */
Romain Guy849d0a32011-02-01 17:20:48 -08009968 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009969 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009970 return;
9971 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009972 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009973 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009974 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9975 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009976 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009977 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009978 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009979 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009980 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009983 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009984 //noinspection PointlessBooleanExpression,ConstantConditions
9985 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9986 if (p != null && ai != null && ai.mHardwareAccelerated) {
9987 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009988 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009989 p.invalidateChild(this, null);
9990 return;
9991 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009992 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009994 if (p != null && ai != null) {
9995 final Rect r = ai.mTmpInvalRect;
9996 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9997 // Don't call invalidate -- we don't want to internally scroll
9998 // our own bounds
9999 p.invalidateChild(this, r);
10000 }
10001 }
10002 }
10003
10004 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010005 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10006 * set any flags or handle all of the cases handled by the default invalidation methods.
10007 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10008 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10009 * walk up the hierarchy, transforming the dirty rect as necessary.
10010 *
10011 * The method also handles normal invalidation logic if display list properties are not
10012 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10013 * backup approach, to handle these cases used in the various property-setting methods.
10014 *
10015 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10016 * are not being used in this view
10017 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10018 * list properties are not being used in this view
10019 */
10020 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010021 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010022 if (invalidateParent) {
10023 invalidateParentCaches();
10024 }
10025 if (forceRedraw) {
10026 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10027 }
10028 invalidate(false);
10029 } else {
10030 final AttachInfo ai = mAttachInfo;
10031 final ViewParent p = mParent;
10032 if (p != null && ai != null) {
10033 final Rect r = ai.mTmpInvalRect;
10034 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10035 if (mParent instanceof ViewGroup) {
10036 ((ViewGroup) mParent).invalidateChildFast(this, r);
10037 } else {
10038 mParent.invalidateChild(this, r);
10039 }
10040 }
10041 }
10042 }
10043
10044 /**
10045 * Utility method to transform a given Rect by the current matrix of this view.
10046 */
10047 void transformRect(final Rect rect) {
10048 if (!getMatrix().isIdentity()) {
10049 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10050 boundingRect.set(rect);
10051 getMatrix().mapRect(boundingRect);
10052 rect.set((int) (boundingRect.left - 0.5f),
10053 (int) (boundingRect.top - 0.5f),
10054 (int) (boundingRect.right + 0.5f),
10055 (int) (boundingRect.bottom + 0.5f));
10056 }
10057 }
10058
10059 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010060 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010061 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10062 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010063 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10064 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010065 *
10066 * @hide
10067 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010068 protected void invalidateParentCaches() {
10069 if (mParent instanceof View) {
10070 ((View) mParent).mPrivateFlags |= INVALIDATED;
10071 }
10072 }
Joe Malin32736f02011-01-19 16:14:20 -080010073
Romain Guy0fd89bf2011-01-26 15:41:30 -080010074 /**
10075 * Used to indicate that the parent of this view should be invalidated. This functionality
10076 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10077 * which is necessary when various parent-managed properties of the view change, such as
10078 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10079 * an invalidation event to the parent.
10080 *
10081 * @hide
10082 */
10083 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010084 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010085 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010086 }
10087 }
10088
10089 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010090 * Indicates whether this View is opaque. An opaque View guarantees that it will
10091 * draw all the pixels overlapping its bounds using a fully opaque color.
10092 *
10093 * Subclasses of View should override this method whenever possible to indicate
10094 * whether an instance is opaque. Opaque Views are treated in a special way by
10095 * the View hierarchy, possibly allowing it to perform optimizations during
10096 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010097 *
Romain Guy24443ea2009-05-11 11:56:30 -070010098 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010099 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010100 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010101 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010102 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010103 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010104 }
10105
Adam Powell20232d02010-12-08 21:08:53 -080010106 /**
10107 * @hide
10108 */
10109 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010110 // Opaque if:
10111 // - Has a background
10112 // - Background is opaque
10113 // - Doesn't have scrollbars or scrollbars are inside overlay
10114
Philip Milne6c8ea062012-04-03 17:38:43 -070010115 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010116 mPrivateFlags |= OPAQUE_BACKGROUND;
10117 } else {
10118 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10119 }
10120
10121 final int flags = mViewFlags;
10122 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10123 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10124 mPrivateFlags |= OPAQUE_SCROLLBARS;
10125 } else {
10126 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10127 }
10128 }
10129
10130 /**
10131 * @hide
10132 */
10133 protected boolean hasOpaqueScrollbars() {
10134 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010135 }
10136
10137 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 * @return A handler associated with the thread running the View. This
10139 * handler can be used to pump events in the UI events queue.
10140 */
10141 public Handler getHandler() {
10142 if (mAttachInfo != null) {
10143 return mAttachInfo.mHandler;
10144 }
10145 return null;
10146 }
10147
10148 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010149 * Gets the view root associated with the View.
10150 * @return The view root, or null if none.
10151 * @hide
10152 */
10153 public ViewRootImpl getViewRootImpl() {
10154 if (mAttachInfo != null) {
10155 return mAttachInfo.mViewRootImpl;
10156 }
10157 return null;
10158 }
10159
10160 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010161 * <p>Causes the Runnable to be added to the message queue.
10162 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010163 *
Romain Guye63a4f32011-08-11 11:33:31 -070010164 * <p>This method can be invoked from outside of the UI thread
10165 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010166 *
10167 * @param action The Runnable that will be executed.
10168 *
10169 * @return Returns true if the Runnable was successfully placed in to the
10170 * message queue. Returns false on failure, usually because the
10171 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010172 *
10173 * @see #postDelayed
10174 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010175 */
10176 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010177 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010178 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010179 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010181 // Assume that post will succeed later
10182 ViewRootImpl.getRunQueue().post(action);
10183 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 }
10185
10186 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010187 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010189 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010190 *
Romain Guye63a4f32011-08-11 11:33:31 -070010191 * <p>This method can be invoked from outside of the UI thread
10192 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193 *
10194 * @param action The Runnable that will be executed.
10195 * @param delayMillis The delay (in milliseconds) until the Runnable
10196 * will be executed.
10197 *
10198 * @return true if the Runnable was successfully placed in to the
10199 * message queue. Returns false on failure, usually because the
10200 * looper processing the message queue is exiting. Note that a
10201 * result of true does not mean the Runnable will be processed --
10202 * if the looper is quit before the delivery time of the message
10203 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010204 *
10205 * @see #post
10206 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 */
10208 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010209 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010210 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010211 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010213 // Assume that post will succeed later
10214 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10215 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 }
10217
10218 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010219 * <p>Causes the Runnable to execute on the next animation time step.
10220 * The runnable will be run on the user interface thread.</p>
10221 *
10222 * <p>This method can be invoked from outside of the UI thread
10223 * only when this View is attached to a window.</p>
10224 *
10225 * @param action The Runnable that will be executed.
10226 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010227 * @see #postOnAnimationDelayed
10228 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010229 */
10230 public void postOnAnimation(Runnable action) {
10231 final AttachInfo attachInfo = mAttachInfo;
10232 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010233 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10234 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010235 } else {
10236 // Assume that post will succeed later
10237 ViewRootImpl.getRunQueue().post(action);
10238 }
10239 }
10240
10241 /**
10242 * <p>Causes the Runnable to execute on the next animation time step,
10243 * after the specified amount of time elapses.
10244 * The runnable will be run on the user interface thread.</p>
10245 *
10246 * <p>This method can be invoked from outside of the UI thread
10247 * only when this View is attached to a window.</p>
10248 *
10249 * @param action The Runnable that will be executed.
10250 * @param delayMillis The delay (in milliseconds) until the Runnable
10251 * will be executed.
10252 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010253 * @see #postOnAnimation
10254 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010255 */
10256 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10257 final AttachInfo attachInfo = mAttachInfo;
10258 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010259 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10260 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010261 } else {
10262 // Assume that post will succeed later
10263 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10264 }
10265 }
10266
10267 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010268 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010269 *
Romain Guye63a4f32011-08-11 11:33:31 -070010270 * <p>This method can be invoked from outside of the UI thread
10271 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272 *
10273 * @param action The Runnable to remove from the message handling queue
10274 *
10275 * @return true if this view could ask the Handler to remove the Runnable,
10276 * false otherwise. When the returned value is true, the Runnable
10277 * may or may not have been actually removed from the message queue
10278 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010279 *
10280 * @see #post
10281 * @see #postDelayed
10282 * @see #postOnAnimation
10283 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010284 */
10285 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010286 if (action != null) {
10287 final AttachInfo attachInfo = mAttachInfo;
10288 if (attachInfo != null) {
10289 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010290 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10291 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010292 } else {
10293 // Assume that post will succeed later
10294 ViewRootImpl.getRunQueue().removeCallbacks(action);
10295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 return true;
10298 }
10299
10300 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010301 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10302 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010303 *
Romain Guye63a4f32011-08-11 11:33:31 -070010304 * <p>This method can be invoked from outside of the UI thread
10305 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010308 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 */
10310 public void postInvalidate() {
10311 postInvalidateDelayed(0);
10312 }
10313
10314 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010315 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10316 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010317 *
Romain Guye63a4f32011-08-11 11:33:31 -070010318 * <p>This method can be invoked from outside of the UI thread
10319 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 *
10321 * @param left The left coordinate of the rectangle to invalidate.
10322 * @param top The top coordinate of the rectangle to invalidate.
10323 * @param right The right coordinate of the rectangle to invalidate.
10324 * @param bottom The bottom coordinate of the rectangle to invalidate.
10325 *
10326 * @see #invalidate(int, int, int, int)
10327 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010328 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 */
10330 public void postInvalidate(int left, int top, int right, int bottom) {
10331 postInvalidateDelayed(0, left, top, right, bottom);
10332 }
10333
10334 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010335 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10336 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010337 *
Romain Guye63a4f32011-08-11 11:33:31 -070010338 * <p>This method can be invoked from outside of the UI thread
10339 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 *
10341 * @param delayMilliseconds the duration in milliseconds to delay the
10342 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010343 *
10344 * @see #invalidate()
10345 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 */
10347 public void postInvalidateDelayed(long delayMilliseconds) {
10348 // We try only with the AttachInfo because there's no point in invalidating
10349 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010350 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010351 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010352 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 }
10354 }
10355
10356 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010357 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10358 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010359 *
Romain Guye63a4f32011-08-11 11:33:31 -070010360 * <p>This method can be invoked from outside of the UI thread
10361 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 *
10363 * @param delayMilliseconds the duration in milliseconds to delay the
10364 * invalidation by
10365 * @param left The left coordinate of the rectangle to invalidate.
10366 * @param top The top coordinate of the rectangle to invalidate.
10367 * @param right The right coordinate of the rectangle to invalidate.
10368 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010369 *
10370 * @see #invalidate(int, int, int, int)
10371 * @see #invalidate(Rect)
10372 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010373 */
10374 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10375 int right, int bottom) {
10376
10377 // We try only with the AttachInfo because there's no point in invalidating
10378 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010379 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010380 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10382 info.target = this;
10383 info.left = left;
10384 info.top = top;
10385 info.right = right;
10386 info.bottom = bottom;
10387
Jeff Browna175a5b2012-02-15 19:18:31 -080010388 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 }
10390 }
10391
10392 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010393 * <p>Cause an invalidate to happen on the next animation time step, typically the
10394 * next display frame.</p>
10395 *
10396 * <p>This method can be invoked from outside of the UI thread
10397 * only when this View is attached to a window.</p>
10398 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010399 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010400 */
10401 public void postInvalidateOnAnimation() {
10402 // We try only with the AttachInfo because there's no point in invalidating
10403 // if we are not attached to our window
10404 final AttachInfo attachInfo = mAttachInfo;
10405 if (attachInfo != null) {
10406 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10407 }
10408 }
10409
10410 /**
10411 * <p>Cause an invalidate of the specified area to happen on the next animation
10412 * time step, typically the next display frame.</p>
10413 *
10414 * <p>This method can be invoked from outside of the UI thread
10415 * only when this View is attached to a window.</p>
10416 *
10417 * @param left The left coordinate of the rectangle to invalidate.
10418 * @param top The top coordinate of the rectangle to invalidate.
10419 * @param right The right coordinate of the rectangle to invalidate.
10420 * @param bottom The bottom coordinate of the rectangle to invalidate.
10421 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010422 * @see #invalidate(int, int, int, int)
10423 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010424 */
10425 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10426 // We try only with the AttachInfo because there's no point in invalidating
10427 // if we are not attached to our window
10428 final AttachInfo attachInfo = mAttachInfo;
10429 if (attachInfo != null) {
10430 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10431 info.target = this;
10432 info.left = left;
10433 info.top = top;
10434 info.right = right;
10435 info.bottom = bottom;
10436
10437 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10438 }
10439 }
10440
10441 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010442 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10443 * This event is sent at most once every
10444 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10445 */
10446 private void postSendViewScrolledAccessibilityEventCallback() {
10447 if (mSendViewScrolledAccessibilityEvent == null) {
10448 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10449 }
10450 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10451 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10452 postDelayed(mSendViewScrolledAccessibilityEvent,
10453 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10454 }
10455 }
10456
10457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 * Called by a parent to request that a child update its values for mScrollX
10459 * and mScrollY if necessary. This will typically be done if the child is
10460 * animating a scroll using a {@link android.widget.Scroller Scroller}
10461 * object.
10462 */
10463 public void computeScroll() {
10464 }
10465
10466 /**
10467 * <p>Indicate whether the horizontal edges are faded when the view is
10468 * scrolled horizontally.</p>
10469 *
10470 * @return true if the horizontal edges should are faded on scroll, false
10471 * otherwise
10472 *
10473 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010474 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010475 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 */
10477 public boolean isHorizontalFadingEdgeEnabled() {
10478 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10479 }
10480
10481 /**
10482 * <p>Define whether the horizontal edges should be faded when this view
10483 * is scrolled horizontally.</p>
10484 *
10485 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10486 * be faded when the view is scrolled
10487 * horizontally
10488 *
10489 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010490 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010491 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 */
10493 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10494 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10495 if (horizontalFadingEdgeEnabled) {
10496 initScrollCache();
10497 }
10498
10499 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10500 }
10501 }
10502
10503 /**
10504 * <p>Indicate whether the vertical edges are faded when the view is
10505 * scrolled horizontally.</p>
10506 *
10507 * @return true if the vertical edges should are faded on scroll, false
10508 * otherwise
10509 *
10510 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010511 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010512 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010513 */
10514 public boolean isVerticalFadingEdgeEnabled() {
10515 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10516 }
10517
10518 /**
10519 * <p>Define whether the vertical edges should be faded when this view
10520 * is scrolled vertically.</p>
10521 *
10522 * @param verticalFadingEdgeEnabled true if the vertical edges should
10523 * be faded when the view is scrolled
10524 * vertically
10525 *
10526 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010527 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010528 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 */
10530 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10531 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10532 if (verticalFadingEdgeEnabled) {
10533 initScrollCache();
10534 }
10535
10536 mViewFlags ^= FADING_EDGE_VERTICAL;
10537 }
10538 }
10539
10540 /**
10541 * Returns the strength, or intensity, of the top faded edge. The strength is
10542 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10543 * returns 0.0 or 1.0 but no value in between.
10544 *
10545 * Subclasses should override this method to provide a smoother fade transition
10546 * when scrolling occurs.
10547 *
10548 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10549 */
10550 protected float getTopFadingEdgeStrength() {
10551 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10552 }
10553
10554 /**
10555 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10556 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10557 * returns 0.0 or 1.0 but no value in between.
10558 *
10559 * Subclasses should override this method to provide a smoother fade transition
10560 * when scrolling occurs.
10561 *
10562 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10563 */
10564 protected float getBottomFadingEdgeStrength() {
10565 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10566 computeVerticalScrollRange() ? 1.0f : 0.0f;
10567 }
10568
10569 /**
10570 * Returns the strength, or intensity, of the left faded edge. The strength is
10571 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10572 * returns 0.0 or 1.0 but no value in between.
10573 *
10574 * Subclasses should override this method to provide a smoother fade transition
10575 * when scrolling occurs.
10576 *
10577 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10578 */
10579 protected float getLeftFadingEdgeStrength() {
10580 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10581 }
10582
10583 /**
10584 * Returns the strength, or intensity, of the right faded edge. The strength is
10585 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10586 * returns 0.0 or 1.0 but no value in between.
10587 *
10588 * Subclasses should override this method to provide a smoother fade transition
10589 * when scrolling occurs.
10590 *
10591 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10592 */
10593 protected float getRightFadingEdgeStrength() {
10594 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10595 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10596 }
10597
10598 /**
10599 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10600 * scrollbar is not drawn by default.</p>
10601 *
10602 * @return true if the horizontal scrollbar should be painted, false
10603 * otherwise
10604 *
10605 * @see #setHorizontalScrollBarEnabled(boolean)
10606 */
10607 public boolean isHorizontalScrollBarEnabled() {
10608 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10609 }
10610
10611 /**
10612 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10613 * scrollbar is not drawn by default.</p>
10614 *
10615 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10616 * be painted
10617 *
10618 * @see #isHorizontalScrollBarEnabled()
10619 */
10620 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10621 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10622 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010623 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010624 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 }
10626 }
10627
10628 /**
10629 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10630 * scrollbar is not drawn by default.</p>
10631 *
10632 * @return true if the vertical scrollbar should be painted, false
10633 * otherwise
10634 *
10635 * @see #setVerticalScrollBarEnabled(boolean)
10636 */
10637 public boolean isVerticalScrollBarEnabled() {
10638 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10639 }
10640
10641 /**
10642 * <p>Define whether the vertical scrollbar should be drawn or not. The
10643 * scrollbar is not drawn by default.</p>
10644 *
10645 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10646 * be painted
10647 *
10648 * @see #isVerticalScrollBarEnabled()
10649 */
10650 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10651 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10652 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010653 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010654 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 }
10656 }
10657
Adam Powell20232d02010-12-08 21:08:53 -080010658 /**
10659 * @hide
10660 */
10661 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010662 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 }
Joe Malin32736f02011-01-19 16:14:20 -080010664
Mike Cleronfe81d382009-09-28 14:22:16 -070010665 /**
10666 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010667 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010668 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010669 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010670 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010671 */
10672 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10673 initScrollCache();
10674 final ScrollabilityCache scrollabilityCache = mScrollCache;
10675 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010676 if (fadeScrollbars) {
10677 scrollabilityCache.state = ScrollabilityCache.OFF;
10678 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010679 scrollabilityCache.state = ScrollabilityCache.ON;
10680 }
10681 }
Joe Malin32736f02011-01-19 16:14:20 -080010682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683 /**
Joe Malin32736f02011-01-19 16:14:20 -080010684 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010685 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010686 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010687 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010688 *
10689 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010690 */
10691 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010692 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010693 }
Joe Malin32736f02011-01-19 16:14:20 -080010694
Mike Cleron52f0a642009-09-28 18:21:37 -070010695 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010696 *
10697 * Returns the delay before scrollbars fade.
10698 *
10699 * @return the delay before scrollbars fade
10700 *
10701 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10702 */
10703 public int getScrollBarDefaultDelayBeforeFade() {
10704 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10705 mScrollCache.scrollBarDefaultDelayBeforeFade;
10706 }
10707
10708 /**
10709 * Define the delay before scrollbars fade.
10710 *
10711 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10712 *
10713 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10714 */
10715 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10716 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10717 }
10718
10719 /**
10720 *
10721 * Returns the scrollbar fade duration.
10722 *
10723 * @return the scrollbar fade duration
10724 *
10725 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10726 */
10727 public int getScrollBarFadeDuration() {
10728 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10729 mScrollCache.scrollBarFadeDuration;
10730 }
10731
10732 /**
10733 * Define the scrollbar fade duration.
10734 *
10735 * @param scrollBarFadeDuration - the scrollbar fade duration
10736 *
10737 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10738 */
10739 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10740 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10741 }
10742
10743 /**
10744 *
10745 * Returns the scrollbar size.
10746 *
10747 * @return the scrollbar size
10748 *
10749 * @attr ref android.R.styleable#View_scrollbarSize
10750 */
10751 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010752 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010753 mScrollCache.scrollBarSize;
10754 }
10755
10756 /**
10757 * Define the scrollbar size.
10758 *
10759 * @param scrollBarSize - the scrollbar size
10760 *
10761 * @attr ref android.R.styleable#View_scrollbarSize
10762 */
10763 public void setScrollBarSize(int scrollBarSize) {
10764 getScrollCache().scrollBarSize = scrollBarSize;
10765 }
10766
10767 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10769 * inset. When inset, they add to the padding of the view. And the scrollbars
10770 * can be drawn inside the padding area or on the edge of the view. For example,
10771 * if a view has a background drawable and you want to draw the scrollbars
10772 * inside the padding specified by the drawable, you can use
10773 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10774 * appear at the edge of the view, ignoring the padding, then you can use
10775 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10776 * @param style the style of the scrollbars. Should be one of
10777 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10778 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10779 * @see #SCROLLBARS_INSIDE_OVERLAY
10780 * @see #SCROLLBARS_INSIDE_INSET
10781 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10782 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010783 *
10784 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 */
10786 public void setScrollBarStyle(int style) {
10787 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10788 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010789 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010790 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010791 }
10792 }
10793
10794 /**
10795 * <p>Returns the current scrollbar style.</p>
10796 * @return the current scrollbar style
10797 * @see #SCROLLBARS_INSIDE_OVERLAY
10798 * @see #SCROLLBARS_INSIDE_INSET
10799 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10800 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010801 *
10802 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010803 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010804 @ViewDebug.ExportedProperty(mapping = {
10805 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10806 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10807 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10808 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10809 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 public int getScrollBarStyle() {
10811 return mViewFlags & SCROLLBARS_STYLE_MASK;
10812 }
10813
10814 /**
10815 * <p>Compute the horizontal range that the horizontal scrollbar
10816 * represents.</p>
10817 *
10818 * <p>The range is expressed in arbitrary units that must be the same as the
10819 * units used by {@link #computeHorizontalScrollExtent()} and
10820 * {@link #computeHorizontalScrollOffset()}.</p>
10821 *
10822 * <p>The default range is the drawing width of this view.</p>
10823 *
10824 * @return the total horizontal range represented by the horizontal
10825 * scrollbar
10826 *
10827 * @see #computeHorizontalScrollExtent()
10828 * @see #computeHorizontalScrollOffset()
10829 * @see android.widget.ScrollBarDrawable
10830 */
10831 protected int computeHorizontalScrollRange() {
10832 return getWidth();
10833 }
10834
10835 /**
10836 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10837 * within the horizontal range. This value is used to compute the position
10838 * of the thumb within the scrollbar's track.</p>
10839 *
10840 * <p>The range is expressed in arbitrary units that must be the same as the
10841 * units used by {@link #computeHorizontalScrollRange()} and
10842 * {@link #computeHorizontalScrollExtent()}.</p>
10843 *
10844 * <p>The default offset is the scroll offset of this view.</p>
10845 *
10846 * @return the horizontal offset of the scrollbar's thumb
10847 *
10848 * @see #computeHorizontalScrollRange()
10849 * @see #computeHorizontalScrollExtent()
10850 * @see android.widget.ScrollBarDrawable
10851 */
10852 protected int computeHorizontalScrollOffset() {
10853 return mScrollX;
10854 }
10855
10856 /**
10857 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10858 * within the horizontal range. This value is used to compute the length
10859 * of the thumb within the scrollbar's track.</p>
10860 *
10861 * <p>The range is expressed in arbitrary units that must be the same as the
10862 * units used by {@link #computeHorizontalScrollRange()} and
10863 * {@link #computeHorizontalScrollOffset()}.</p>
10864 *
10865 * <p>The default extent is the drawing width of this view.</p>
10866 *
10867 * @return the horizontal extent of the scrollbar's thumb
10868 *
10869 * @see #computeHorizontalScrollRange()
10870 * @see #computeHorizontalScrollOffset()
10871 * @see android.widget.ScrollBarDrawable
10872 */
10873 protected int computeHorizontalScrollExtent() {
10874 return getWidth();
10875 }
10876
10877 /**
10878 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10879 *
10880 * <p>The range is expressed in arbitrary units that must be the same as the
10881 * units used by {@link #computeVerticalScrollExtent()} and
10882 * {@link #computeVerticalScrollOffset()}.</p>
10883 *
10884 * @return the total vertical range represented by the vertical scrollbar
10885 *
10886 * <p>The default range is the drawing height of this view.</p>
10887 *
10888 * @see #computeVerticalScrollExtent()
10889 * @see #computeVerticalScrollOffset()
10890 * @see android.widget.ScrollBarDrawable
10891 */
10892 protected int computeVerticalScrollRange() {
10893 return getHeight();
10894 }
10895
10896 /**
10897 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10898 * within the horizontal range. This value is used to compute the position
10899 * of the thumb within the scrollbar's track.</p>
10900 *
10901 * <p>The range is expressed in arbitrary units that must be the same as the
10902 * units used by {@link #computeVerticalScrollRange()} and
10903 * {@link #computeVerticalScrollExtent()}.</p>
10904 *
10905 * <p>The default offset is the scroll offset of this view.</p>
10906 *
10907 * @return the vertical offset of the scrollbar's thumb
10908 *
10909 * @see #computeVerticalScrollRange()
10910 * @see #computeVerticalScrollExtent()
10911 * @see android.widget.ScrollBarDrawable
10912 */
10913 protected int computeVerticalScrollOffset() {
10914 return mScrollY;
10915 }
10916
10917 /**
10918 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10919 * within the vertical range. This value is used to compute the length
10920 * of the thumb within the scrollbar's track.</p>
10921 *
10922 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010923 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 * {@link #computeVerticalScrollOffset()}.</p>
10925 *
10926 * <p>The default extent is the drawing height of this view.</p>
10927 *
10928 * @return the vertical extent of the scrollbar's thumb
10929 *
10930 * @see #computeVerticalScrollRange()
10931 * @see #computeVerticalScrollOffset()
10932 * @see android.widget.ScrollBarDrawable
10933 */
10934 protected int computeVerticalScrollExtent() {
10935 return getHeight();
10936 }
10937
10938 /**
Adam Powell69159442011-06-13 17:53:06 -070010939 * Check if this view can be scrolled horizontally in a certain direction.
10940 *
10941 * @param direction Negative to check scrolling left, positive to check scrolling right.
10942 * @return true if this view can be scrolled in the specified direction, false otherwise.
10943 */
10944 public boolean canScrollHorizontally(int direction) {
10945 final int offset = computeHorizontalScrollOffset();
10946 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10947 if (range == 0) return false;
10948 if (direction < 0) {
10949 return offset > 0;
10950 } else {
10951 return offset < range - 1;
10952 }
10953 }
10954
10955 /**
10956 * Check if this view can be scrolled vertically in a certain direction.
10957 *
10958 * @param direction Negative to check scrolling up, positive to check scrolling down.
10959 * @return true if this view can be scrolled in the specified direction, false otherwise.
10960 */
10961 public boolean canScrollVertically(int direction) {
10962 final int offset = computeVerticalScrollOffset();
10963 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10964 if (range == 0) return false;
10965 if (direction < 0) {
10966 return offset > 0;
10967 } else {
10968 return offset < range - 1;
10969 }
10970 }
10971
10972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010973 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10974 * scrollbars are painted only if they have been awakened first.</p>
10975 *
10976 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010977 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010978 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010979 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010980 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 // scrollbars are drawn only when the animation is running
10982 final ScrollabilityCache cache = mScrollCache;
10983 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010984
Mike Cleronf116bf82009-09-27 19:14:12 -070010985 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010986
Mike Cleronf116bf82009-09-27 19:14:12 -070010987 if (state == ScrollabilityCache.OFF) {
10988 return;
10989 }
Joe Malin32736f02011-01-19 16:14:20 -080010990
Mike Cleronf116bf82009-09-27 19:14:12 -070010991 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010992
Mike Cleronf116bf82009-09-27 19:14:12 -070010993 if (state == ScrollabilityCache.FADING) {
10994 // We're fading -- get our fade interpolation
10995 if (cache.interpolatorValues == null) {
10996 cache.interpolatorValues = new float[1];
10997 }
Joe Malin32736f02011-01-19 16:14:20 -080010998
Mike Cleronf116bf82009-09-27 19:14:12 -070010999 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011000
Mike Cleronf116bf82009-09-27 19:14:12 -070011001 // Stops the animation if we're done
11002 if (cache.scrollBarInterpolator.timeToValues(values) ==
11003 Interpolator.Result.FREEZE_END) {
11004 cache.state = ScrollabilityCache.OFF;
11005 } else {
11006 cache.scrollBar.setAlpha(Math.round(values[0]));
11007 }
Joe Malin32736f02011-01-19 16:14:20 -080011008
11009 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011010 // drawing. We only want this when we're fading so that
11011 // we prevent excessive redraws
11012 invalidate = true;
11013 } else {
11014 // We're just on -- but we may have been fading before so
11015 // reset alpha
11016 cache.scrollBar.setAlpha(255);
11017 }
11018
Joe Malin32736f02011-01-19 16:14:20 -080011019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 final int viewFlags = mViewFlags;
11021
11022 final boolean drawHorizontalScrollBar =
11023 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11024 final boolean drawVerticalScrollBar =
11025 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11026 && !isVerticalScrollBarHidden();
11027
11028 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11029 final int width = mRight - mLeft;
11030 final int height = mBottom - mTop;
11031
11032 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033
Mike Reede8853fc2009-09-04 14:01:48 -040011034 final int scrollX = mScrollX;
11035 final int scrollY = mScrollY;
11036 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11037
Mike Cleronf116bf82009-09-27 19:14:12 -070011038 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011041 int size = scrollBar.getSize(false);
11042 if (size <= 0) {
11043 size = cache.scrollBarSize;
11044 }
11045
Mike Cleronf116bf82009-09-27 19:14:12 -070011046 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011047 computeHorizontalScrollOffset(),
11048 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011049 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011050 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011051 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011052 left = scrollX + (mPaddingLeft & inside);
11053 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11054 bottom = top + size;
11055 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11056 if (invalidate) {
11057 invalidate(left, top, right, bottom);
11058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011059 }
11060
11061 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011062 int size = scrollBar.getSize(true);
11063 if (size <= 0) {
11064 size = cache.scrollBarSize;
11065 }
11066
Mike Reede8853fc2009-09-04 14:01:48 -040011067 scrollBar.setParameters(computeVerticalScrollRange(),
11068 computeVerticalScrollOffset(),
11069 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011070 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11071 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11072 verticalScrollbarPosition = isLayoutRtl() ?
11073 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11074 }
11075 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011076 default:
Adam Powell20232d02010-12-08 21:08:53 -080011077 case SCROLLBAR_POSITION_RIGHT:
11078 left = scrollX + width - size - (mUserPaddingRight & inside);
11079 break;
11080 case SCROLLBAR_POSITION_LEFT:
11081 left = scrollX + (mUserPaddingLeft & inside);
11082 break;
11083 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011084 top = scrollY + (mPaddingTop & inside);
11085 right = left + size;
11086 bottom = scrollY + height - (mUserPaddingBottom & inside);
11087 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11088 if (invalidate) {
11089 invalidate(left, top, right, bottom);
11090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011091 }
11092 }
11093 }
11094 }
Romain Guy8506ab42009-06-11 17:35:47 -070011095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011097 * 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 -080011098 * FastScroller is visible.
11099 * @return whether to temporarily hide the vertical scrollbar
11100 * @hide
11101 */
11102 protected boolean isVerticalScrollBarHidden() {
11103 return false;
11104 }
11105
11106 /**
11107 * <p>Draw the horizontal scrollbar if
11108 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 * @param canvas the canvas on which to draw the scrollbar
11111 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 *
11113 * @see #isHorizontalScrollBarEnabled()
11114 * @see #computeHorizontalScrollRange()
11115 * @see #computeHorizontalScrollExtent()
11116 * @see #computeHorizontalScrollOffset()
11117 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011118 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011119 */
Romain Guy8fb95422010-08-17 18:38:51 -070011120 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11121 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011122 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011123 scrollBar.draw(canvas);
11124 }
Mike Reede8853fc2009-09-04 14:01:48 -040011125
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11128 * returns true.</p>
11129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011130 * @param canvas the canvas on which to draw the scrollbar
11131 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 *
11133 * @see #isVerticalScrollBarEnabled()
11134 * @see #computeVerticalScrollRange()
11135 * @see #computeVerticalScrollExtent()
11136 * @see #computeVerticalScrollOffset()
11137 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011138 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 */
Romain Guy8fb95422010-08-17 18:38:51 -070011140 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11141 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011142 scrollBar.setBounds(l, t, r, b);
11143 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 }
11145
11146 /**
11147 * Implement this to do your drawing.
11148 *
11149 * @param canvas the canvas on which the background will be drawn
11150 */
11151 protected void onDraw(Canvas canvas) {
11152 }
11153
11154 /*
11155 * Caller is responsible for calling requestLayout if necessary.
11156 * (This allows addViewInLayout to not request a new layout.)
11157 */
11158 void assignParent(ViewParent parent) {
11159 if (mParent == null) {
11160 mParent = parent;
11161 } else if (parent == null) {
11162 mParent = null;
11163 } else {
11164 throw new RuntimeException("view " + this + " being added, but"
11165 + " it already has a parent");
11166 }
11167 }
11168
11169 /**
11170 * This is called when the view is attached to a window. At this point it
11171 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011172 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11173 * however it may be called any time before the first onDraw -- including
11174 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 *
11176 * @see #onDetachedFromWindow()
11177 */
11178 protected void onAttachedToWindow() {
11179 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11180 mParent.requestTransparentRegion(this);
11181 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011182
Adam Powell8568c3a2010-04-19 14:26:11 -070011183 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11184 initialAwakenScrollBars();
11185 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11186 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011187
Chet Haasea9b61ac2010-12-20 07:40:25 -080011188 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011189
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011190 // Order is important here: LayoutDirection MUST be resolved before Padding
11191 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011192 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011193 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011194 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011195 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011196
Svetoslav Ganov42138042012-03-20 11:51:39 -070011197 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011198 if (isFocused()) {
11199 InputMethodManager imm = InputMethodManager.peekInstance();
11200 imm.focusIn(this);
11201 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011202
11203 if (mAttachInfo != null && mDisplayList != null) {
11204 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11205 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011206 }
Cibu Johny86666632010-02-22 13:01:02 -080011207
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011208 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011209 * @see #onScreenStateChanged(int)
11210 */
11211 void dispatchScreenStateChanged(int screenState) {
11212 onScreenStateChanged(screenState);
11213 }
11214
11215 /**
11216 * This method is called whenever the state of the screen this view is
11217 * attached to changes. A state change will usually occurs when the screen
11218 * turns on or off (whether it happens automatically or the user does it
11219 * manually.)
11220 *
11221 * @param screenState The new state of the screen. Can be either
11222 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11223 */
11224 public void onScreenStateChanged(int screenState) {
11225 }
11226
11227 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011228 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11229 */
11230 private boolean hasRtlSupport() {
11231 return mContext.getApplicationInfo().hasRtlSupport();
11232 }
11233
11234 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011235 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11236 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011237 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011238 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011239 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011240 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011241 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011242
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011243 if (hasRtlSupport()) {
11244 // Set resolved depending on layout direction
11245 switch (getLayoutDirection()) {
11246 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011247 // We cannot resolve yet. LTR is by default and let the resolution happen again
11248 // later to get the correct resolved value
11249 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011250
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011251 ViewGroup viewGroup = ((ViewGroup) mParent);
11252
11253 // We cannot resolve yet on the parent too. LTR is by default and let the
11254 // resolution happen again later
11255 if (!viewGroup.canResolveLayoutDirection()) return;
11256
11257 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11258 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011259 }
11260 break;
11261 case LAYOUT_DIRECTION_RTL:
11262 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11263 break;
11264 case LAYOUT_DIRECTION_LOCALE:
11265 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011266 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11267 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011268 break;
11269 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011270 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011271 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011272 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011273
11274 // Set to resolved
11275 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011276 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011277 }
11278
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011279 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011280 * Called when layout direction has been resolved.
11281 *
11282 * The default implementation does nothing.
11283 */
11284 public void onResolvedLayoutDirectionChanged() {
11285 }
11286
11287 /**
11288 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011289 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011290 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011291 // If the user specified the absolute padding (either with android:padding or
11292 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11293 // use the default padding or the padding from the background drawable
11294 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011295 int resolvedLayoutDirection = getResolvedLayoutDirection();
11296 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011297 case LAYOUT_DIRECTION_RTL:
11298 // Start user padding override Right user padding. Otherwise, if Right user
11299 // padding is not defined, use the default Right padding. If Right user padding
11300 // is defined, just use it.
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011301 if (mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011302 mUserPaddingRight = mUserPaddingStart;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011303 }
11304 if (mUserPaddingRight == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011305 mUserPaddingRight = mPaddingRight;
11306 }
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011307 if (mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011308 mUserPaddingLeft = mUserPaddingEnd;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011309 }
11310 if (mUserPaddingLeft == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011311 mUserPaddingLeft = mPaddingLeft;
11312 }
11313 break;
11314 case LAYOUT_DIRECTION_LTR:
11315 default:
11316 // Start user padding override Left user padding. Otherwise, if Left user
11317 // padding is not defined, use the default left padding. If Left user padding
11318 // is defined, just use it.
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011319 if (mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011320 mUserPaddingLeft = mUserPaddingStart;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011321 }
11322 if (mUserPaddingLeft == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011323 mUserPaddingLeft = mPaddingLeft;
11324 }
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011325 if (mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011326 mUserPaddingRight = mUserPaddingEnd;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011327 }
11328 if (mUserPaddingRight == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011329 mUserPaddingRight = mPaddingRight;
11330 }
11331 }
11332
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011333 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11334
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011335 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011336 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011337 }
11338
11339 /**
11340 * Resolve padding depending on the layout direction. Subclasses that care about
11341 * padding resolution should override this method. The default implementation does
11342 * nothing.
11343 *
11344 * @param layoutDirection the direction of the layout
11345 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011346 * @see {@link #LAYOUT_DIRECTION_LTR}
11347 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011348 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011349 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011350 }
11351
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011352 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011353 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011354 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011355 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011356 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011357 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011358 switch (getLayoutDirection()) {
11359 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011360 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011361 default:
11362 return true;
11363 }
11364 }
11365
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011366 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011367 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11368 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011369 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011370 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011371 // Reset the current resolved bits
11372 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011373 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011374 // Reset also the text direction
11375 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011376 }
11377
11378 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011379 * Called during reset of resolved layout direction.
11380 *
11381 * Subclasses need to override this method to clear cached information that depends on the
11382 * resolved layout direction, or to inform child views that inherit their layout direction.
11383 *
11384 * The default implementation does nothing.
11385 */
11386 public void onResolvedLayoutDirectionReset() {
11387 }
11388
11389 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011390 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011391 *
11392 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011393 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011394 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011395 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011396 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397 }
11398
11399 /**
11400 * This is called when the view is detached from a window. At this point it
11401 * no longer has a surface for drawing.
11402 *
11403 * @see #onAttachedToWindow()
11404 */
11405 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011406 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011407
Romain Guya440b002010-02-24 15:57:54 -080011408 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011409 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011410 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011411 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011414
Romain Guya998dff2012-03-23 18:58:36 -070011415 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011416
11417 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011418 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011419 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011420 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011421 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011422 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011423 // Should never happen
11424 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011425 }
11426
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011427 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011428
11429 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011430 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011431 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011432 }
11433
11434 /**
11435 * @return The number of times this view has been attached to a window
11436 */
11437 protected int getWindowAttachCount() {
11438 return mWindowAttachCount;
11439 }
11440
11441 /**
11442 * Retrieve a unique token identifying the window this view is attached to.
11443 * @return Return the window's token for use in
11444 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11445 */
11446 public IBinder getWindowToken() {
11447 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11448 }
11449
11450 /**
11451 * Retrieve a unique token identifying the top-level "real" window of
11452 * the window that this view is attached to. That is, this is like
11453 * {@link #getWindowToken}, except if the window this view in is a panel
11454 * window (attached to another containing window), then the token of
11455 * the containing window is returned instead.
11456 *
11457 * @return Returns the associated window token, either
11458 * {@link #getWindowToken()} or the containing window's token.
11459 */
11460 public IBinder getApplicationWindowToken() {
11461 AttachInfo ai = mAttachInfo;
11462 if (ai != null) {
11463 IBinder appWindowToken = ai.mPanelParentWindowToken;
11464 if (appWindowToken == null) {
11465 appWindowToken = ai.mWindowToken;
11466 }
11467 return appWindowToken;
11468 }
11469 return null;
11470 }
11471
11472 /**
11473 * Retrieve private session object this view hierarchy is using to
11474 * communicate with the window manager.
11475 * @return the session object to communicate with the window manager
11476 */
11477 /*package*/ IWindowSession getWindowSession() {
11478 return mAttachInfo != null ? mAttachInfo.mSession : null;
11479 }
11480
11481 /**
11482 * @param info the {@link android.view.View.AttachInfo} to associated with
11483 * this view
11484 */
11485 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11486 //System.out.println("Attached! " + this);
11487 mAttachInfo = info;
11488 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011489 // We will need to evaluate the drawable state at least once.
11490 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011491 if (mFloatingTreeObserver != null) {
11492 info.mTreeObserver.merge(mFloatingTreeObserver);
11493 mFloatingTreeObserver = null;
11494 }
11495 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11496 mAttachInfo.mScrollContainers.add(this);
11497 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11498 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011499 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011500 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011501
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011502 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011503 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011504 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011505 if (listeners != null && listeners.size() > 0) {
11506 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11507 // perform the dispatching. The iterator is a safe guard against listeners that
11508 // could mutate the list by calling the various add/remove methods. This prevents
11509 // the array from being modified while we iterate it.
11510 for (OnAttachStateChangeListener listener : listeners) {
11511 listener.onViewAttachedToWindow(this);
11512 }
11513 }
11514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011515 int vis = info.mWindowVisibility;
11516 if (vis != GONE) {
11517 onWindowVisibilityChanged(vis);
11518 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011519 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11520 // If nobody has evaluated the drawable state yet, then do it now.
11521 refreshDrawableState();
11522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011523 }
11524
11525 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011526 AttachInfo info = mAttachInfo;
11527 if (info != null) {
11528 int vis = info.mWindowVisibility;
11529 if (vis != GONE) {
11530 onWindowVisibilityChanged(GONE);
11531 }
11532 }
11533
11534 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011535
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011536 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011537 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011538 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011539 if (listeners != null && listeners.size() > 0) {
11540 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11541 // perform the dispatching. The iterator is a safe guard against listeners that
11542 // could mutate the list by calling the various add/remove methods. This prevents
11543 // the array from being modified while we iterate it.
11544 for (OnAttachStateChangeListener listener : listeners) {
11545 listener.onViewDetachedFromWindow(this);
11546 }
11547 }
11548
Romain Guy01d5edc2011-01-28 11:28:53 -080011549 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011550 mAttachInfo.mScrollContainers.remove(this);
11551 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11552 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011554 mAttachInfo = null;
11555 }
11556
11557 /**
11558 * Store this view hierarchy's frozen state into the given container.
11559 *
11560 * @param container The SparseArray in which to save the view's state.
11561 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011562 * @see #restoreHierarchyState(android.util.SparseArray)
11563 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11564 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011565 */
11566 public void saveHierarchyState(SparseArray<Parcelable> container) {
11567 dispatchSaveInstanceState(container);
11568 }
11569
11570 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011571 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11572 * this view and its children. May be overridden to modify how freezing happens to a
11573 * 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 -080011574 *
11575 * @param container The SparseArray in which to save the view's state.
11576 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011577 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11578 * @see #saveHierarchyState(android.util.SparseArray)
11579 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 */
11581 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11582 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11583 mPrivateFlags &= ~SAVE_STATE_CALLED;
11584 Parcelable state = onSaveInstanceState();
11585 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11586 throw new IllegalStateException(
11587 "Derived class did not call super.onSaveInstanceState()");
11588 }
11589 if (state != null) {
11590 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11591 // + ": " + state);
11592 container.put(mID, state);
11593 }
11594 }
11595 }
11596
11597 /**
11598 * Hook allowing a view to generate a representation of its internal state
11599 * that can later be used to create a new instance with that same state.
11600 * This state should only contain information that is not persistent or can
11601 * not be reconstructed later. For example, you will never store your
11602 * current position on screen because that will be computed again when a
11603 * new instance of the view is placed in its view hierarchy.
11604 * <p>
11605 * Some examples of things you may store here: the current cursor position
11606 * in a text view (but usually not the text itself since that is stored in a
11607 * content provider or other persistent storage), the currently selected
11608 * item in a list view.
11609 *
11610 * @return Returns a Parcelable object containing the view's current dynamic
11611 * state, or null if there is nothing interesting to save. The
11612 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011613 * @see #onRestoreInstanceState(android.os.Parcelable)
11614 * @see #saveHierarchyState(android.util.SparseArray)
11615 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011616 * @see #setSaveEnabled(boolean)
11617 */
11618 protected Parcelable onSaveInstanceState() {
11619 mPrivateFlags |= SAVE_STATE_CALLED;
11620 return BaseSavedState.EMPTY_STATE;
11621 }
11622
11623 /**
11624 * Restore this view hierarchy's frozen state from the given container.
11625 *
11626 * @param container The SparseArray which holds previously frozen states.
11627 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011628 * @see #saveHierarchyState(android.util.SparseArray)
11629 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11630 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011631 */
11632 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11633 dispatchRestoreInstanceState(container);
11634 }
11635
11636 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011637 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11638 * state for this view and its children. May be overridden to modify how restoring
11639 * happens to a view's children; for example, some views may want to not store state
11640 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011641 *
11642 * @param container The SparseArray which holds previously saved state.
11643 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011644 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11645 * @see #restoreHierarchyState(android.util.SparseArray)
11646 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 */
11648 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11649 if (mID != NO_ID) {
11650 Parcelable state = container.get(mID);
11651 if (state != null) {
11652 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11653 // + ": " + state);
11654 mPrivateFlags &= ~SAVE_STATE_CALLED;
11655 onRestoreInstanceState(state);
11656 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11657 throw new IllegalStateException(
11658 "Derived class did not call super.onRestoreInstanceState()");
11659 }
11660 }
11661 }
11662 }
11663
11664 /**
11665 * Hook allowing a view to re-apply a representation of its internal state that had previously
11666 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11667 * null state.
11668 *
11669 * @param state The frozen state that had previously been returned by
11670 * {@link #onSaveInstanceState}.
11671 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011672 * @see #onSaveInstanceState()
11673 * @see #restoreHierarchyState(android.util.SparseArray)
11674 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 */
11676 protected void onRestoreInstanceState(Parcelable state) {
11677 mPrivateFlags |= SAVE_STATE_CALLED;
11678 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011679 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11680 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011681 + "when two views of different type have the same id in the same hierarchy. "
11682 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011683 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 }
11685 }
11686
11687 /**
11688 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11689 *
11690 * @return the drawing start time in milliseconds
11691 */
11692 public long getDrawingTime() {
11693 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11694 }
11695
11696 /**
11697 * <p>Enables or disables the duplication of the parent's state into this view. When
11698 * duplication is enabled, this view gets its drawable state from its parent rather
11699 * than from its own internal properties.</p>
11700 *
11701 * <p>Note: in the current implementation, setting this property to true after the
11702 * view was added to a ViewGroup might have no effect at all. This property should
11703 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11704 *
11705 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11706 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011707 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011708 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11709 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011710 *
11711 * @param enabled True to enable duplication of the parent's drawable state, false
11712 * to disable it.
11713 *
11714 * @see #getDrawableState()
11715 * @see #isDuplicateParentStateEnabled()
11716 */
11717 public void setDuplicateParentStateEnabled(boolean enabled) {
11718 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11719 }
11720
11721 /**
11722 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11723 *
11724 * @return True if this view's drawable state is duplicated from the parent,
11725 * false otherwise
11726 *
11727 * @see #getDrawableState()
11728 * @see #setDuplicateParentStateEnabled(boolean)
11729 */
11730 public boolean isDuplicateParentStateEnabled() {
11731 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11732 }
11733
11734 /**
Romain Guy171c5922011-01-06 10:04:23 -080011735 * <p>Specifies the type of layer backing this view. The layer can be
11736 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11737 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011738 *
Romain Guy171c5922011-01-06 10:04:23 -080011739 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11740 * instance that controls how the layer is composed on screen. The following
11741 * properties of the paint are taken into account when composing the layer:</p>
11742 * <ul>
11743 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11744 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11745 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11746 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011747 *
Romain Guy171c5922011-01-06 10:04:23 -080011748 * <p>If this view has an alpha value set to < 1.0 by calling
11749 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11750 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11751 * equivalent to setting a hardware layer on this view and providing a paint with
11752 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011753 *
Romain Guy171c5922011-01-06 10:04:23 -080011754 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11755 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11756 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011757 *
Romain Guy171c5922011-01-06 10:04:23 -080011758 * @param layerType The ype of layer to use with this view, must be one of
11759 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11760 * {@link #LAYER_TYPE_HARDWARE}
11761 * @param paint The paint used to compose the layer. This argument is optional
11762 * and can be null. It is ignored when the layer type is
11763 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011764 *
11765 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011766 * @see #LAYER_TYPE_NONE
11767 * @see #LAYER_TYPE_SOFTWARE
11768 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011769 * @see #setAlpha(float)
11770 *
Romain Guy171c5922011-01-06 10:04:23 -080011771 * @attr ref android.R.styleable#View_layerType
11772 */
11773 public void setLayerType(int layerType, Paint paint) {
11774 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011775 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011776 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11777 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011778
Romain Guyd6cd5722011-01-17 14:42:41 -080011779 if (layerType == mLayerType) {
11780 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11781 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011782 invalidateParentCaches();
11783 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011784 }
11785 return;
11786 }
Romain Guy171c5922011-01-06 10:04:23 -080011787
11788 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011789 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011790 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011791 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011792 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011793 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011794 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011795 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011796 default:
11797 break;
Romain Guy171c5922011-01-06 10:04:23 -080011798 }
11799
11800 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011801 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11802 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11803 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011804
Romain Guy0fd89bf2011-01-26 15:41:30 -080011805 invalidateParentCaches();
11806 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011807 }
11808
11809 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011810 * Indicates whether this view has a static layer. A view with layer type
11811 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11812 * dynamic.
11813 */
11814 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011815 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011816 }
11817
11818 /**
Romain Guy171c5922011-01-06 10:04:23 -080011819 * Indicates what type of layer is currently associated with this view. By default
11820 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11821 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11822 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011823 *
Romain Guy171c5922011-01-06 10:04:23 -080011824 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11825 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011826 *
11827 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011828 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011829 * @see #LAYER_TYPE_NONE
11830 * @see #LAYER_TYPE_SOFTWARE
11831 * @see #LAYER_TYPE_HARDWARE
11832 */
11833 public int getLayerType() {
11834 return mLayerType;
11835 }
Joe Malin32736f02011-01-19 16:14:20 -080011836
Romain Guy6c319ca2011-01-11 14:29:25 -080011837 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011838 * Forces this view's layer to be created and this view to be rendered
11839 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11840 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011841 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011842 * This method can for instance be used to render a view into its layer before
11843 * starting an animation. If this view is complex, rendering into the layer
11844 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011845 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011846 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011847 *
11848 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011849 */
11850 public void buildLayer() {
11851 if (mLayerType == LAYER_TYPE_NONE) return;
11852
11853 if (mAttachInfo == null) {
11854 throw new IllegalStateException("This view must be attached to a window first");
11855 }
11856
11857 switch (mLayerType) {
11858 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011859 if (mAttachInfo.mHardwareRenderer != null &&
11860 mAttachInfo.mHardwareRenderer.isEnabled() &&
11861 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011862 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011863 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011864 break;
11865 case LAYER_TYPE_SOFTWARE:
11866 buildDrawingCache(true);
11867 break;
11868 }
11869 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011870
Romain Guyf1ae1062011-03-02 18:16:04 -080011871 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011872 * <p>Returns a hardware layer that can be used to draw this view again
11873 * without executing its draw method.</p>
11874 *
11875 * @return A HardwareLayer ready to render, or null if an error occurred.
11876 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011877 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011878 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11879 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011880 return null;
11881 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011882
Romain Guy9c4b79a2011-11-10 19:23:58 -080011883 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011884
11885 final int width = mRight - mLeft;
11886 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011887
Romain Guy6c319ca2011-01-11 14:29:25 -080011888 if (width == 0 || height == 0) {
11889 return null;
11890 }
11891
11892 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11893 if (mHardwareLayer == null) {
11894 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11895 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011896 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070011897 } else {
11898 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11899 mHardwareLayer.resize(width, height);
11900 mLocalDirtyRect.set(0, 0, width, height);
11901 }
11902
11903 // This should not be necessary but applications that change
11904 // the parameters of their background drawable without calling
11905 // this.setBackground(Drawable) can leave the view in a bad state
11906 // (for instance isOpaque() returns true, but the background is
11907 // not opaque.)
11908 computeOpaqueFlags();
11909
11910 final boolean opaque = isOpaque();
11911 if (mHardwareLayer.isOpaque() != opaque) {
11912 mHardwareLayer.setOpaque(opaque);
11913 mLocalDirtyRect.set(0, 0, width, height);
11914 }
Romain Guy6c319ca2011-01-11 14:29:25 -080011915 }
11916
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011917 // The layer is not valid if the underlying GPU resources cannot be allocated
11918 if (!mHardwareLayer.isValid()) {
11919 return null;
11920 }
11921
Chet Haasea1cff502012-02-21 13:43:44 -080011922 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011923 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011924 }
11925
11926 return mHardwareLayer;
11927 }
Romain Guy171c5922011-01-06 10:04:23 -080011928
Romain Guy589b0bb2011-10-10 13:57:47 -070011929 /**
11930 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011931 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011932 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011933 *
11934 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011935 * @see #LAYER_TYPE_HARDWARE
11936 */
Romain Guya998dff2012-03-23 18:58:36 -070011937 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011938 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011939 AttachInfo info = mAttachInfo;
11940 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011941 info.mHardwareRenderer.isEnabled() &&
11942 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011943 mHardwareLayer.destroy();
11944 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011945
Romain Guy9c4b79a2011-11-10 19:23:58 -080011946 invalidate(true);
11947 invalidateParentCaches();
11948 }
Romain Guy65b345f2011-07-27 18:51:50 -070011949 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011950 }
Romain Guy65b345f2011-07-27 18:51:50 -070011951 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011952 }
11953
Romain Guy171c5922011-01-06 10:04:23 -080011954 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011955 * Destroys all hardware rendering resources. This method is invoked
11956 * when the system needs to reclaim resources. Upon execution of this
11957 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011958 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011959 * Note: you <strong>must</strong> call
11960 * <code>super.destroyHardwareResources()</code> when overriding
11961 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011962 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011963 * @hide
11964 */
11965 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011966 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011967 }
11968
11969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11971 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11972 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11973 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11974 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11975 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011976 *
Romain Guy171c5922011-01-06 10:04:23 -080011977 * <p>Enabling the drawing cache is similar to
11978 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011979 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11980 * drawing cache has no effect on rendering because the system uses a different mechanism
11981 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11982 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11983 * for information on how to enable software and hardware layers.</p>
11984 *
11985 * <p>This API can be used to manually generate
11986 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11987 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 *
11989 * @param enabled true to enable the drawing cache, false otherwise
11990 *
11991 * @see #isDrawingCacheEnabled()
11992 * @see #getDrawingCache()
11993 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011994 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 */
11996 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011997 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11999 }
12000
12001 /**
12002 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12003 *
12004 * @return true if the drawing cache is enabled
12005 *
12006 * @see #setDrawingCacheEnabled(boolean)
12007 * @see #getDrawingCache()
12008 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012009 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 public boolean isDrawingCacheEnabled() {
12011 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12012 }
12013
12014 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012015 * Debugging utility which recursively outputs the dirty state of a view and its
12016 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012017 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012018 * @hide
12019 */
Romain Guy676b1732011-02-14 14:45:33 -080012020 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012021 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12022 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12023 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12024 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12025 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12026 if (clear) {
12027 mPrivateFlags &= clearMask;
12028 }
12029 if (this instanceof ViewGroup) {
12030 ViewGroup parent = (ViewGroup) this;
12031 final int count = parent.getChildCount();
12032 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012033 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012034 child.outputDirtyFlags(indent + " ", clear, clearMask);
12035 }
12036 }
12037 }
12038
12039 /**
12040 * This method is used by ViewGroup to cause its children to restore or recreate their
12041 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12042 * to recreate its own display list, which would happen if it went through the normal
12043 * draw/dispatchDraw mechanisms.
12044 *
12045 * @hide
12046 */
12047 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012048
12049 /**
12050 * A view that is not attached or hardware accelerated cannot create a display list.
12051 * This method checks these conditions and returns the appropriate result.
12052 *
12053 * @return true if view has the ability to create a display list, false otherwise.
12054 *
12055 * @hide
12056 */
12057 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012058 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012059 }
Joe Malin32736f02011-01-19 16:14:20 -080012060
Chet Haasedaf98e92011-01-10 14:10:36 -080012061 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012062 * @return The HardwareRenderer associated with that view or null if hardware rendering
12063 * is not supported or this this has not been attached to a window.
12064 *
12065 * @hide
12066 */
12067 public HardwareRenderer getHardwareRenderer() {
12068 if (mAttachInfo != null) {
12069 return mAttachInfo.mHardwareRenderer;
12070 }
12071 return null;
12072 }
12073
12074 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012075 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12076 * Otherwise, the same display list will be returned (after having been rendered into
12077 * along the way, depending on the invalidation state of the view).
12078 *
12079 * @param displayList The previous version of this displayList, could be null.
12080 * @param isLayer Whether the requester of the display list is a layer. If so,
12081 * the view will avoid creating a layer inside the resulting display list.
12082 * @return A new or reused DisplayList object.
12083 */
12084 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12085 if (!canHaveDisplayList()) {
12086 return null;
12087 }
12088
12089 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12090 displayList == null || !displayList.isValid() ||
12091 (!isLayer && mRecreateDisplayList))) {
12092 // Don't need to recreate the display list, just need to tell our
12093 // children to restore/recreate theirs
12094 if (displayList != null && displayList.isValid() &&
12095 !isLayer && !mRecreateDisplayList) {
12096 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12097 mPrivateFlags &= ~DIRTY_MASK;
12098 dispatchGetDisplayList();
12099
12100 return displayList;
12101 }
12102
12103 if (!isLayer) {
12104 // If we got here, we're recreating it. Mark it as such to ensure that
12105 // we copy in child display lists into ours in drawChild()
12106 mRecreateDisplayList = true;
12107 }
12108 if (displayList == null) {
12109 final String name = getClass().getSimpleName();
12110 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12111 // If we're creating a new display list, make sure our parent gets invalidated
12112 // since they will need to recreate their display list to account for this
12113 // new child display list.
12114 invalidateParentCaches();
12115 }
12116
12117 boolean caching = false;
12118 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012119 int width = mRight - mLeft;
12120 int height = mBottom - mTop;
12121
12122 try {
12123 canvas.setViewport(width, height);
12124 // The dirty rect should always be null for a display list
12125 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012126 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012127 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012128 if (layerType == LAYER_TYPE_HARDWARE) {
12129 final HardwareLayer layer = getHardwareLayer();
12130 if (layer != null && layer.isValid()) {
12131 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12132 } else {
12133 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12134 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12135 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12136 }
12137 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012138 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012139 buildDrawingCache(true);
12140 Bitmap cache = getDrawingCache(true);
12141 if (cache != null) {
12142 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12143 caching = true;
12144 }
Chet Haasea1cff502012-02-21 13:43:44 -080012145 }
Chet Haasea1cff502012-02-21 13:43:44 -080012146 } else {
12147
12148 computeScroll();
12149
Chet Haasea1cff502012-02-21 13:43:44 -080012150 canvas.translate(-mScrollX, -mScrollY);
12151 if (!isLayer) {
12152 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12153 mPrivateFlags &= ~DIRTY_MASK;
12154 }
12155
12156 // Fast path for layouts with no backgrounds
12157 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12158 dispatchDraw(canvas);
12159 } else {
12160 draw(canvas);
12161 }
12162 }
12163 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012164 canvas.onPostDraw();
12165
12166 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012167 displayList.setCaching(caching);
12168 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012169 displayList.setLeftTopRightBottom(0, 0, width, height);
12170 } else {
12171 setDisplayListProperties(displayList);
12172 }
12173 }
12174 } else if (!isLayer) {
12175 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12176 mPrivateFlags &= ~DIRTY_MASK;
12177 }
12178
12179 return displayList;
12180 }
12181
12182 /**
12183 * Get the DisplayList for the HardwareLayer
12184 *
12185 * @param layer The HardwareLayer whose DisplayList we want
12186 * @return A DisplayList fopr the specified HardwareLayer
12187 */
12188 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12189 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12190 layer.setDisplayList(displayList);
12191 return displayList;
12192 }
12193
12194
12195 /**
Romain Guyb051e892010-09-28 19:09:36 -070012196 * <p>Returns a display list that can be used to draw this view again
12197 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012198 *
Romain Guyb051e892010-09-28 19:09:36 -070012199 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012200 *
12201 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012202 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012203 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012204 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012205 return mDisplayList;
12206 }
12207
Romain Guy38c2ece2012-05-24 14:20:56 -070012208 private void clearDisplayList() {
12209 if (mDisplayList != null) {
12210 mDisplayList.invalidate();
12211 mDisplayList.clear();
12212 }
12213 }
12214
Romain Guyb051e892010-09-28 19:09:36 -070012215 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012216 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012217 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012218 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012219 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012220 * @see #getDrawingCache(boolean)
12221 */
12222 public Bitmap getDrawingCache() {
12223 return getDrawingCache(false);
12224 }
12225
12226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012227 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12228 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12229 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12230 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12231 * request the drawing cache by calling this method and draw it on screen if the
12232 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012233 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012234 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12235 * this method will create a bitmap of the same size as this view. Because this bitmap
12236 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12237 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12238 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12239 * size than the view. This implies that your application must be able to handle this
12240 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012241 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012242 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12243 * the current density of the screen when the application is in compatibility
12244 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012245 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012246 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012248 * @see #setDrawingCacheEnabled(boolean)
12249 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012250 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012251 * @see #destroyDrawingCache()
12252 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012253 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12255 return null;
12256 }
12257 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012258 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012259 }
Romain Guy02890fd2010-08-06 17:58:44 -070012260 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012261 }
12262
12263 /**
12264 * <p>Frees the resources used by the drawing cache. If you call
12265 * {@link #buildDrawingCache()} manually without calling
12266 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12267 * should cleanup the cache with this method afterwards.</p>
12268 *
12269 * @see #setDrawingCacheEnabled(boolean)
12270 * @see #buildDrawingCache()
12271 * @see #getDrawingCache()
12272 */
12273 public void destroyDrawingCache() {
12274 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012275 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 mDrawingCache = null;
12277 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012278 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012279 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012280 mUnscaledDrawingCache = null;
12281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012282 }
12283
12284 /**
12285 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012286 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287 * view will always be drawn on top of a solid color.
12288 *
12289 * @param color The background color to use for the drawing cache's bitmap
12290 *
12291 * @see #setDrawingCacheEnabled(boolean)
12292 * @see #buildDrawingCache()
12293 * @see #getDrawingCache()
12294 */
12295 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012296 if (color != mDrawingCacheBackgroundColor) {
12297 mDrawingCacheBackgroundColor = color;
12298 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 }
12301
12302 /**
12303 * @see #setDrawingCacheBackgroundColor(int)
12304 *
12305 * @return The background color to used for the drawing cache's bitmap
12306 */
12307 public int getDrawingCacheBackgroundColor() {
12308 return mDrawingCacheBackgroundColor;
12309 }
12310
12311 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012312 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012313 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012314 * @see #buildDrawingCache(boolean)
12315 */
12316 public void buildDrawingCache() {
12317 buildDrawingCache(false);
12318 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012319
Romain Guyfbd8f692009-06-26 14:51:58 -070012320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012321 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12322 *
12323 * <p>If you call {@link #buildDrawingCache()} manually without calling
12324 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12325 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012326 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012327 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12328 * this method will create a bitmap of the same size as this view. Because this bitmap
12329 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12330 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12331 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12332 * size than the view. This implies that your application must be able to handle this
12333 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012334 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012335 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12336 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012337 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012338 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012339 *
12340 * @see #getDrawingCache()
12341 * @see #destroyDrawingCache()
12342 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012343 public void buildDrawingCache(boolean autoScale) {
12344 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012345 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012346 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012347
Romain Guy8506ab42009-06-11 17:35:47 -070012348 int width = mRight - mLeft;
12349 int height = mBottom - mTop;
12350
12351 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012352 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012353
Romain Guye1123222009-06-29 14:24:56 -070012354 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012355 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12356 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012358
12359 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012360 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012361 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012362
12363 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012364 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012365 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012366 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12367 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012368 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012369 return;
12370 }
12371
12372 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012373 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012374
12375 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012376 Bitmap.Config quality;
12377 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012378 // Never pick ARGB_4444 because it looks awful
12379 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012380 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12381 case DRAWING_CACHE_QUALITY_AUTO:
12382 quality = Bitmap.Config.ARGB_8888;
12383 break;
12384 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012385 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012386 break;
12387 case DRAWING_CACHE_QUALITY_HIGH:
12388 quality = Bitmap.Config.ARGB_8888;
12389 break;
12390 default:
12391 quality = Bitmap.Config.ARGB_8888;
12392 break;
12393 }
12394 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012395 // Optimization for translucent windows
12396 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012397 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012398 }
12399
12400 // Try to cleanup memory
12401 if (bitmap != null) bitmap.recycle();
12402
12403 try {
12404 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012405 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012406 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012407 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012408 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012409 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012410 }
Adam Powell26153a32010-11-08 15:22:27 -080012411 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012412 } catch (OutOfMemoryError e) {
12413 // If there is not enough memory to create the bitmap cache, just
12414 // ignore the issue as bitmap caches are not required to draw the
12415 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012416 if (autoScale) {
12417 mDrawingCache = null;
12418 } else {
12419 mUnscaledDrawingCache = null;
12420 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012421 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012422 return;
12423 }
12424
12425 clear = drawingCacheBackgroundColor != 0;
12426 }
12427
12428 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429 if (attachInfo != null) {
12430 canvas = attachInfo.mCanvas;
12431 if (canvas == null) {
12432 canvas = new Canvas();
12433 }
12434 canvas.setBitmap(bitmap);
12435 // Temporarily clobber the cached Canvas in case one of our children
12436 // is also using a drawing cache. Without this, the children would
12437 // steal the canvas by attaching their own bitmap to it and bad, bad
12438 // thing would happen (invisible views, corrupted drawings, etc.)
12439 attachInfo.mCanvas = null;
12440 } else {
12441 // This case should hopefully never or seldom happen
12442 canvas = new Canvas(bitmap);
12443 }
12444
12445 if (clear) {
12446 bitmap.eraseColor(drawingCacheBackgroundColor);
12447 }
12448
12449 computeScroll();
12450 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012451
Romain Guye1123222009-06-29 14:24:56 -070012452 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012453 final float scale = attachInfo.mApplicationScale;
12454 canvas.scale(scale, scale);
12455 }
Joe Malin32736f02011-01-19 16:14:20 -080012456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012457 canvas.translate(-mScrollX, -mScrollY);
12458
Romain Guy5bcdff42009-05-14 21:27:18 -070012459 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012460 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12461 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012462 mPrivateFlags |= DRAWING_CACHE_VALID;
12463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012464
12465 // Fast path for layouts with no backgrounds
12466 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012467 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 dispatchDraw(canvas);
12469 } else {
12470 draw(canvas);
12471 }
12472
12473 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012474 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475
12476 if (attachInfo != null) {
12477 // Restore the cached Canvas for our siblings
12478 attachInfo.mCanvas = canvas;
12479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012480 }
12481 }
12482
12483 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012484 * Create a snapshot of the view into a bitmap. We should probably make
12485 * some form of this public, but should think about the API.
12486 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012487 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012488 int width = mRight - mLeft;
12489 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012490
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012491 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012492 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012493 width = (int) ((width * scale) + 0.5f);
12494 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012495
Romain Guy8c11e312009-09-14 15:15:30 -070012496 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012497 if (bitmap == null) {
12498 throw new OutOfMemoryError();
12499 }
12500
Romain Guyc529d8d2011-09-06 15:01:39 -070012501 Resources resources = getResources();
12502 if (resources != null) {
12503 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12504 }
Joe Malin32736f02011-01-19 16:14:20 -080012505
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012506 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012507 if (attachInfo != null) {
12508 canvas = attachInfo.mCanvas;
12509 if (canvas == null) {
12510 canvas = new Canvas();
12511 }
12512 canvas.setBitmap(bitmap);
12513 // Temporarily clobber the cached Canvas in case one of our children
12514 // is also using a drawing cache. Without this, the children would
12515 // steal the canvas by attaching their own bitmap to it and bad, bad
12516 // things would happen (invisible views, corrupted drawings, etc.)
12517 attachInfo.mCanvas = null;
12518 } else {
12519 // This case should hopefully never or seldom happen
12520 canvas = new Canvas(bitmap);
12521 }
12522
Romain Guy5bcdff42009-05-14 21:27:18 -070012523 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012524 bitmap.eraseColor(backgroundColor);
12525 }
12526
12527 computeScroll();
12528 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012529 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012530 canvas.translate(-mScrollX, -mScrollY);
12531
Romain Guy5bcdff42009-05-14 21:27:18 -070012532 // Temporarily remove the dirty mask
12533 int flags = mPrivateFlags;
12534 mPrivateFlags &= ~DIRTY_MASK;
12535
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012536 // Fast path for layouts with no backgrounds
12537 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12538 dispatchDraw(canvas);
12539 } else {
12540 draw(canvas);
12541 }
12542
Romain Guy5bcdff42009-05-14 21:27:18 -070012543 mPrivateFlags = flags;
12544
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012545 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012546 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012547
12548 if (attachInfo != null) {
12549 // Restore the cached Canvas for our siblings
12550 attachInfo.mCanvas = canvas;
12551 }
Romain Guy8506ab42009-06-11 17:35:47 -070012552
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012553 return bitmap;
12554 }
12555
12556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012557 * Indicates whether this View is currently in edit mode. A View is usually
12558 * in edit mode when displayed within a developer tool. For instance, if
12559 * this View is being drawn by a visual user interface builder, this method
12560 * should return true.
12561 *
12562 * Subclasses should check the return value of this method to provide
12563 * different behaviors if their normal behavior might interfere with the
12564 * host environment. For instance: the class spawns a thread in its
12565 * constructor, the drawing code relies on device-specific features, etc.
12566 *
12567 * This method is usually checked in the drawing code of custom widgets.
12568 *
12569 * @return True if this View is in edit mode, false otherwise.
12570 */
12571 public boolean isInEditMode() {
12572 return false;
12573 }
12574
12575 /**
12576 * If the View draws content inside its padding and enables fading edges,
12577 * it needs to support padding offsets. Padding offsets are added to the
12578 * fading edges to extend the length of the fade so that it covers pixels
12579 * drawn inside the padding.
12580 *
12581 * Subclasses of this class should override this method if they need
12582 * to draw content inside the padding.
12583 *
12584 * @return True if padding offset must be applied, false otherwise.
12585 *
12586 * @see #getLeftPaddingOffset()
12587 * @see #getRightPaddingOffset()
12588 * @see #getTopPaddingOffset()
12589 * @see #getBottomPaddingOffset()
12590 *
12591 * @since CURRENT
12592 */
12593 protected boolean isPaddingOffsetRequired() {
12594 return false;
12595 }
12596
12597 /**
12598 * Amount by which to extend the left fading region. Called only when
12599 * {@link #isPaddingOffsetRequired()} returns true.
12600 *
12601 * @return The left padding offset in pixels.
12602 *
12603 * @see #isPaddingOffsetRequired()
12604 *
12605 * @since CURRENT
12606 */
12607 protected int getLeftPaddingOffset() {
12608 return 0;
12609 }
12610
12611 /**
12612 * Amount by which to extend the right fading region. Called only when
12613 * {@link #isPaddingOffsetRequired()} returns true.
12614 *
12615 * @return The right padding offset in pixels.
12616 *
12617 * @see #isPaddingOffsetRequired()
12618 *
12619 * @since CURRENT
12620 */
12621 protected int getRightPaddingOffset() {
12622 return 0;
12623 }
12624
12625 /**
12626 * Amount by which to extend the top fading region. Called only when
12627 * {@link #isPaddingOffsetRequired()} returns true.
12628 *
12629 * @return The top padding offset in pixels.
12630 *
12631 * @see #isPaddingOffsetRequired()
12632 *
12633 * @since CURRENT
12634 */
12635 protected int getTopPaddingOffset() {
12636 return 0;
12637 }
12638
12639 /**
12640 * Amount by which to extend the bottom fading region. Called only when
12641 * {@link #isPaddingOffsetRequired()} returns true.
12642 *
12643 * @return The bottom padding offset in pixels.
12644 *
12645 * @see #isPaddingOffsetRequired()
12646 *
12647 * @since CURRENT
12648 */
12649 protected int getBottomPaddingOffset() {
12650 return 0;
12651 }
12652
12653 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012654 * @hide
12655 * @param offsetRequired
12656 */
12657 protected int getFadeTop(boolean offsetRequired) {
12658 int top = mPaddingTop;
12659 if (offsetRequired) top += getTopPaddingOffset();
12660 return top;
12661 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012662
Romain Guyf2fc4602011-07-19 15:20:03 -070012663 /**
12664 * @hide
12665 * @param offsetRequired
12666 */
12667 protected int getFadeHeight(boolean offsetRequired) {
12668 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012669 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012670 return mBottom - mTop - mPaddingBottom - padding;
12671 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012672
Romain Guyf2fc4602011-07-19 15:20:03 -070012673 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012674 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012675 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012676 *
Romain Guy2bffd262010-09-12 17:40:02 -070012677 * <p>Even if this method returns true, it does not mean that every call
12678 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12679 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012680 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012681 * window is hardware accelerated,
12682 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12683 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012684 *
Romain Guy2bffd262010-09-12 17:40:02 -070012685 * @return True if the view is attached to a window and the window is
12686 * hardware accelerated; false in any other case.
12687 */
12688 public boolean isHardwareAccelerated() {
12689 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12690 }
Joe Malin32736f02011-01-19 16:14:20 -080012691
Romain Guy2bffd262010-09-12 17:40:02 -070012692 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012693 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12694 * case of an active Animation being run on the view.
12695 */
12696 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12697 Animation a, boolean scalingRequired) {
12698 Transformation invalidationTransform;
12699 final int flags = parent.mGroupFlags;
12700 final boolean initialized = a.isInitialized();
12701 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012702 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012703 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012704 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012705 onAnimationStart();
12706 }
12707
12708 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12709 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12710 if (parent.mInvalidationTransformation == null) {
12711 parent.mInvalidationTransformation = new Transformation();
12712 }
12713 invalidationTransform = parent.mInvalidationTransformation;
12714 a.getTransformation(drawingTime, invalidationTransform, 1f);
12715 } else {
12716 invalidationTransform = parent.mChildTransformation;
12717 }
Romain Guy393a52c2012-05-22 20:21:08 -070012718
Chet Haasebcca79a2012-02-14 08:45:14 -080012719 if (more) {
12720 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070012721 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
12722 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
12723 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
12724 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012725 // The child need to draw an animation, potentially offscreen, so
12726 // make sure we do not cancel invalidate requests
12727 parent.mPrivateFlags |= DRAW_ANIMATION;
12728 parent.invalidate(mLeft, mTop, mRight, mBottom);
12729 }
12730 } else {
12731 if (parent.mInvalidateRegion == null) {
12732 parent.mInvalidateRegion = new RectF();
12733 }
12734 final RectF region = parent.mInvalidateRegion;
12735 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12736 invalidationTransform);
12737
12738 // The child need to draw an animation, potentially offscreen, so
12739 // make sure we do not cancel invalidate requests
12740 parent.mPrivateFlags |= DRAW_ANIMATION;
12741
12742 final int left = mLeft + (int) region.left;
12743 final int top = mTop + (int) region.top;
12744 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12745 top + (int) (region.height() + .5f));
12746 }
12747 }
12748 return more;
12749 }
12750
Chet Haasea1cff502012-02-21 13:43:44 -080012751 /**
12752 * This method is called by getDisplayList() when a display list is created or re-rendered.
12753 * It sets or resets the current value of all properties on that display list (resetting is
12754 * necessary when a display list is being re-created, because we need to make sure that
12755 * previously-set transform values
12756 */
12757 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012758 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012759 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012760 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012761 if (mParent instanceof ViewGroup) {
12762 displayList.setClipChildren(
12763 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12764 }
Chet Haase9420abd2012-03-29 16:28:32 -070012765 float alpha = 1;
12766 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12767 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12768 ViewGroup parentVG = (ViewGroup) mParent;
12769 final boolean hasTransform =
12770 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12771 if (hasTransform) {
12772 Transformation transform = parentVG.mChildTransformation;
12773 final int transformType = parentVG.mChildTransformation.getTransformationType();
12774 if (transformType != Transformation.TYPE_IDENTITY) {
12775 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12776 alpha = transform.getAlpha();
12777 }
12778 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12779 displayList.setStaticMatrix(transform.getMatrix());
12780 }
12781 }
12782 }
Chet Haasea1cff502012-02-21 13:43:44 -080012783 }
12784 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012785 alpha *= mTransformationInfo.mAlpha;
12786 if (alpha < 1) {
12787 final int multipliedAlpha = (int) (255 * alpha);
12788 if (onSetAlpha(multipliedAlpha)) {
12789 alpha = 1;
12790 }
12791 }
12792 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012793 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12794 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12795 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12796 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012797 if (mTransformationInfo.mCamera == null) {
12798 mTransformationInfo.mCamera = new Camera();
12799 mTransformationInfo.matrix3D = new Matrix();
12800 }
12801 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012802 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12803 displayList.setPivotX(getPivotX());
12804 displayList.setPivotY(getPivotY());
12805 }
Chet Haase9420abd2012-03-29 16:28:32 -070012806 } else if (alpha < 1) {
12807 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012808 }
12809 }
12810 }
12811
Chet Haasebcca79a2012-02-14 08:45:14 -080012812 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012813 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12814 * This draw() method is an implementation detail and is not intended to be overridden or
12815 * to be called from anywhere else other than ViewGroup.drawChild().
12816 */
12817 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012818 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012819 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012820 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012821 final int flags = parent.mGroupFlags;
12822
Chet Haasea1cff502012-02-21 13:43:44 -080012823 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012824 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012825 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012826 }
12827
12828 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012829 boolean concatMatrix = false;
12830
12831 boolean scalingRequired = false;
12832 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070012833 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080012834
12835 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012836 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12837 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012838 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012839 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012840 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12841 } else {
12842 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12843 }
12844
Chet Haasebcca79a2012-02-14 08:45:14 -080012845 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012846 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012847 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012848 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012849 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070012850 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012851 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012852 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012853 } else {
Chet Haase21433372012-06-05 07:54:09 -070012854 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012855 mDisplayList != null) {
12856 // No longer animating: clear out old animation matrix
12857 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070012858 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012859 }
12860 if (!useDisplayListProperties &&
12861 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12862 final boolean hasTransform =
12863 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12864 if (hasTransform) {
12865 final int transformType = parent.mChildTransformation.getTransformationType();
12866 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12867 parent.mChildTransformation : null;
12868 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12869 }
Chet Haase64a48c12012-02-13 16:33:29 -080012870 }
12871 }
12872
12873 concatMatrix |= !childHasIdentityMatrix;
12874
12875 // Sets the flag as early as possible to allow draw() implementations
12876 // to call invalidate() successfully when doing animations
12877 mPrivateFlags |= DRAWN;
12878
Chet Haasebcca79a2012-02-14 08:45:14 -080012879 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012880 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012881 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012882 return more;
12883 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012884 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012885
12886 if (hardwareAccelerated) {
12887 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12888 // retain the flag's value temporarily in the mRecreateDisplayList flag
12889 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12890 mPrivateFlags &= ~INVALIDATED;
12891 }
12892
Chet Haase64a48c12012-02-13 16:33:29 -080012893 DisplayList displayList = null;
12894 Bitmap cache = null;
12895 boolean hasDisplayList = false;
12896 if (caching) {
12897 if (!hardwareAccelerated) {
12898 if (layerType != LAYER_TYPE_NONE) {
12899 layerType = LAYER_TYPE_SOFTWARE;
12900 buildDrawingCache(true);
12901 }
12902 cache = getDrawingCache(true);
12903 } else {
12904 switch (layerType) {
12905 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012906 if (useDisplayListProperties) {
12907 hasDisplayList = canHaveDisplayList();
12908 } else {
12909 buildDrawingCache(true);
12910 cache = getDrawingCache(true);
12911 }
Chet Haase64a48c12012-02-13 16:33:29 -080012912 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012913 case LAYER_TYPE_HARDWARE:
12914 if (useDisplayListProperties) {
12915 hasDisplayList = canHaveDisplayList();
12916 }
12917 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012918 case LAYER_TYPE_NONE:
12919 // Delay getting the display list until animation-driven alpha values are
12920 // set up and possibly passed on to the view
12921 hasDisplayList = canHaveDisplayList();
12922 break;
12923 }
12924 }
12925 }
Chet Haasea1cff502012-02-21 13:43:44 -080012926 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012927 if (useDisplayListProperties) {
12928 displayList = getDisplayList();
12929 if (!displayList.isValid()) {
12930 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12931 // to getDisplayList(), the display list will be marked invalid and we should not
12932 // try to use it again.
12933 displayList = null;
12934 hasDisplayList = false;
12935 useDisplayListProperties = false;
12936 }
12937 }
Chet Haase64a48c12012-02-13 16:33:29 -080012938
Chet Haase526057b2012-07-12 17:50:41 -070012939 int sx = 0;
12940 int sy = 0;
12941 if (!hasDisplayList) {
12942 computeScroll();
12943 sx = mScrollX;
12944 sy = mScrollY;
12945 }
12946
Chet Haase64a48c12012-02-13 16:33:29 -080012947 final boolean hasNoCache = cache == null || hasDisplayList;
12948 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12949 layerType != LAYER_TYPE_HARDWARE;
12950
Chet Haasea1cff502012-02-21 13:43:44 -080012951 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012952 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012953 restoreTo = canvas.save();
12954 }
Chet Haase64a48c12012-02-13 16:33:29 -080012955 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012956 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012957 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012958 if (!useDisplayListProperties) {
12959 canvas.translate(mLeft, mTop);
12960 }
Chet Haase64a48c12012-02-13 16:33:29 -080012961 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012962 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012963 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012964 restoreTo = canvas.save();
12965 }
Chet Haase64a48c12012-02-13 16:33:29 -080012966 // mAttachInfo cannot be null, otherwise scalingRequired == false
12967 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12968 canvas.scale(scale, scale);
12969 }
12970 }
12971
Chet Haasea1cff502012-02-21 13:43:44 -080012972 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070012973 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
12974 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080012975 if (transformToApply != null || !childHasIdentityMatrix) {
12976 int transX = 0;
12977 int transY = 0;
12978
12979 if (offsetForScroll) {
12980 transX = -sx;
12981 transY = -sy;
12982 }
12983
12984 if (transformToApply != null) {
12985 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012986 if (useDisplayListProperties) {
12987 displayList.setAnimationMatrix(transformToApply.getMatrix());
12988 } else {
12989 // Undo the scroll translation, apply the transformation matrix,
12990 // then redo the scroll translate to get the correct result.
12991 canvas.translate(-transX, -transY);
12992 canvas.concat(transformToApply.getMatrix());
12993 canvas.translate(transX, transY);
12994 }
Chet Haasea1cff502012-02-21 13:43:44 -080012995 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012996 }
12997
12998 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012999 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013000 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013001 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013002 }
13003 }
13004
Chet Haasea1cff502012-02-21 13:43:44 -080013005 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013006 canvas.translate(-transX, -transY);
13007 canvas.concat(getMatrix());
13008 canvas.translate(transX, transY);
13009 }
13010 }
13011
Chet Haase21433372012-06-05 07:54:09 -070013012 // Deal with alpha if it is or used to be <1
13013 if (alpha < 1 ||
13014 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13015 if (alpha < 1) {
13016 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13017 } else {
13018 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13019 }
Chet Haasea1cff502012-02-21 13:43:44 -080013020 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013021 if (hasNoCache) {
13022 final int multipliedAlpha = (int) (255 * alpha);
13023 if (!onSetAlpha(multipliedAlpha)) {
13024 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013025 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013026 layerType != LAYER_TYPE_NONE) {
13027 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13028 }
Chet Haase9420abd2012-03-29 16:28:32 -070013029 if (useDisplayListProperties) {
13030 displayList.setAlpha(alpha * getAlpha());
13031 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013032 final int scrollX = hasDisplayList ? 0 : sx;
13033 final int scrollY = hasDisplayList ? 0 : sy;
13034 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13035 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013036 }
13037 } else {
13038 // Alpha is handled by the child directly, clobber the layer's alpha
13039 mPrivateFlags |= ALPHA_SET;
13040 }
13041 }
13042 }
13043 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13044 onSetAlpha(255);
13045 mPrivateFlags &= ~ALPHA_SET;
13046 }
13047
Chet Haasea1cff502012-02-21 13:43:44 -080013048 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13049 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013050 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013051 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013052 } else {
13053 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013054 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013055 } else {
13056 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13057 }
13058 }
13059 }
13060
Chet Haase9420abd2012-03-29 16:28:32 -070013061 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013062 displayList = getDisplayList();
13063 if (!displayList.isValid()) {
13064 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13065 // to getDisplayList(), the display list will be marked invalid and we should not
13066 // try to use it again.
13067 displayList = null;
13068 hasDisplayList = false;
13069 }
13070 }
13071
13072 if (hasNoCache) {
13073 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013074 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013075 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013076 if (layer != null && layer.isValid()) {
13077 mLayerPaint.setAlpha((int) (alpha * 255));
13078 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13079 layerRendered = true;
13080 } else {
13081 final int scrollX = hasDisplayList ? 0 : sx;
13082 final int scrollY = hasDisplayList ? 0 : sy;
13083 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013084 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013085 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13086 }
13087 }
13088
13089 if (!layerRendered) {
13090 if (!hasDisplayList) {
13091 // Fast path for layouts with no backgrounds
13092 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013093 mPrivateFlags &= ~DIRTY_MASK;
13094 dispatchDraw(canvas);
13095 } else {
13096 draw(canvas);
13097 }
13098 } else {
13099 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013100 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013101 }
13102 }
13103 } else if (cache != null) {
13104 mPrivateFlags &= ~DIRTY_MASK;
13105 Paint cachePaint;
13106
13107 if (layerType == LAYER_TYPE_NONE) {
13108 cachePaint = parent.mCachePaint;
13109 if (cachePaint == null) {
13110 cachePaint = new Paint();
13111 cachePaint.setDither(false);
13112 parent.mCachePaint = cachePaint;
13113 }
Chet Haase9420abd2012-03-29 16:28:32 -070013114 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013115 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013116 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13117 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013118 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013119 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013120 }
13121 } else {
13122 cachePaint = mLayerPaint;
13123 cachePaint.setAlpha((int) (alpha * 255));
13124 }
13125 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13126 }
13127
Chet Haasea1cff502012-02-21 13:43:44 -080013128 if (restoreTo >= 0) {
13129 canvas.restoreToCount(restoreTo);
13130 }
Chet Haase64a48c12012-02-13 16:33:29 -080013131
13132 if (a != null && !more) {
13133 if (!hardwareAccelerated && !a.getFillAfter()) {
13134 onSetAlpha(255);
13135 }
13136 parent.finishAnimatingView(this, a);
13137 }
13138
13139 if (more && hardwareAccelerated) {
13140 // invalidation is the trigger to recreate display lists, so if we're using
13141 // display lists to render, force an invalidate to allow the animation to
13142 // continue drawing another frame
13143 parent.invalidate(true);
13144 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13145 // alpha animations should cause the child to recreate its display list
13146 invalidate(true);
13147 }
13148 }
13149
13150 mRecreateDisplayList = false;
13151
13152 return more;
13153 }
13154
13155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013156 * Manually render this view (and all of its children) to the given Canvas.
13157 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013158 * called. When implementing a view, implement
13159 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13160 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013161 *
13162 * @param canvas The Canvas to which the View is rendered.
13163 */
13164 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013165 final int privateFlags = mPrivateFlags;
13166 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13167 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13168 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013170 /*
13171 * Draw traversal performs several drawing steps which must be executed
13172 * in the appropriate order:
13173 *
13174 * 1. Draw the background
13175 * 2. If necessary, save the canvas' layers to prepare for fading
13176 * 3. Draw view's content
13177 * 4. Draw children
13178 * 5. If necessary, draw the fading edges and restore layers
13179 * 6. Draw decorations (scrollbars for instance)
13180 */
13181
13182 // Step 1, draw the background, if needed
13183 int saveCount;
13184
Romain Guy24443ea2009-05-11 11:56:30 -070013185 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013186 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013187 if (background != null) {
13188 final int scrollX = mScrollX;
13189 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013190
Romain Guy24443ea2009-05-11 11:56:30 -070013191 if (mBackgroundSizeChanged) {
13192 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13193 mBackgroundSizeChanged = false;
13194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013195
Romain Guy24443ea2009-05-11 11:56:30 -070013196 if ((scrollX | scrollY) == 0) {
13197 background.draw(canvas);
13198 } else {
13199 canvas.translate(scrollX, scrollY);
13200 background.draw(canvas);
13201 canvas.translate(-scrollX, -scrollY);
13202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013203 }
13204 }
13205
13206 // skip step 2 & 5 if possible (common case)
13207 final int viewFlags = mViewFlags;
13208 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13209 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13210 if (!verticalEdges && !horizontalEdges) {
13211 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013212 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013213
13214 // Step 4, draw the children
13215 dispatchDraw(canvas);
13216
13217 // Step 6, draw decorations (scrollbars)
13218 onDrawScrollBars(canvas);
13219
13220 // we're done...
13221 return;
13222 }
13223
13224 /*
13225 * Here we do the full fledged routine...
13226 * (this is an uncommon case where speed matters less,
13227 * this is why we repeat some of the tests that have been
13228 * done above)
13229 */
13230
13231 boolean drawTop = false;
13232 boolean drawBottom = false;
13233 boolean drawLeft = false;
13234 boolean drawRight = false;
13235
13236 float topFadeStrength = 0.0f;
13237 float bottomFadeStrength = 0.0f;
13238 float leftFadeStrength = 0.0f;
13239 float rightFadeStrength = 0.0f;
13240
13241 // Step 2, save the canvas' layers
13242 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013243
13244 final boolean offsetRequired = isPaddingOffsetRequired();
13245 if (offsetRequired) {
13246 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013247 }
13248
13249 int left = mScrollX + paddingLeft;
13250 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013251 int top = mScrollY + getFadeTop(offsetRequired);
13252 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013253
13254 if (offsetRequired) {
13255 right += getRightPaddingOffset();
13256 bottom += getBottomPaddingOffset();
13257 }
13258
13259 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013260 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013261 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013262
13263 // clip the fade length if top and bottom fades overlap
13264 // overlapping fades produce odd-looking artifacts
13265 if (verticalEdges && (top + length > bottom - length)) {
13266 length = (bottom - top) / 2;
13267 }
13268
13269 // also clip horizontal fades if necessary
13270 if (horizontalEdges && (left + length > right - length)) {
13271 length = (right - left) / 2;
13272 }
13273
13274 if (verticalEdges) {
13275 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013276 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013277 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013278 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013279 }
13280
13281 if (horizontalEdges) {
13282 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013283 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013284 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013285 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013286 }
13287
13288 saveCount = canvas.getSaveCount();
13289
13290 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013291 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013292 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13293
13294 if (drawTop) {
13295 canvas.saveLayer(left, top, right, top + length, null, flags);
13296 }
13297
13298 if (drawBottom) {
13299 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13300 }
13301
13302 if (drawLeft) {
13303 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13304 }
13305
13306 if (drawRight) {
13307 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13308 }
13309 } else {
13310 scrollabilityCache.setFadeColor(solidColor);
13311 }
13312
13313 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013314 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013315
13316 // Step 4, draw the children
13317 dispatchDraw(canvas);
13318
13319 // Step 5, draw the fade effect and restore layers
13320 final Paint p = scrollabilityCache.paint;
13321 final Matrix matrix = scrollabilityCache.matrix;
13322 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013323
13324 if (drawTop) {
13325 matrix.setScale(1, fadeHeight * topFadeStrength);
13326 matrix.postTranslate(left, top);
13327 fade.setLocalMatrix(matrix);
13328 canvas.drawRect(left, top, right, top + length, p);
13329 }
13330
13331 if (drawBottom) {
13332 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13333 matrix.postRotate(180);
13334 matrix.postTranslate(left, bottom);
13335 fade.setLocalMatrix(matrix);
13336 canvas.drawRect(left, bottom - length, right, bottom, p);
13337 }
13338
13339 if (drawLeft) {
13340 matrix.setScale(1, fadeHeight * leftFadeStrength);
13341 matrix.postRotate(-90);
13342 matrix.postTranslate(left, top);
13343 fade.setLocalMatrix(matrix);
13344 canvas.drawRect(left, top, left + length, bottom, p);
13345 }
13346
13347 if (drawRight) {
13348 matrix.setScale(1, fadeHeight * rightFadeStrength);
13349 matrix.postRotate(90);
13350 matrix.postTranslate(right, top);
13351 fade.setLocalMatrix(matrix);
13352 canvas.drawRect(right - length, top, right, bottom, p);
13353 }
13354
13355 canvas.restoreToCount(saveCount);
13356
13357 // Step 6, draw decorations (scrollbars)
13358 onDrawScrollBars(canvas);
13359 }
13360
13361 /**
13362 * Override this if your view is known to always be drawn on top of a solid color background,
13363 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13364 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13365 * should be set to 0xFF.
13366 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013367 * @see #setVerticalFadingEdgeEnabled(boolean)
13368 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013369 *
13370 * @return The known solid color background for this view, or 0 if the color may vary
13371 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013372 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373 public int getSolidColor() {
13374 return 0;
13375 }
13376
13377 /**
13378 * Build a human readable string representation of the specified view flags.
13379 *
13380 * @param flags the view flags to convert to a string
13381 * @return a String representing the supplied flags
13382 */
13383 private static String printFlags(int flags) {
13384 String output = "";
13385 int numFlags = 0;
13386 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13387 output += "TAKES_FOCUS";
13388 numFlags++;
13389 }
13390
13391 switch (flags & VISIBILITY_MASK) {
13392 case INVISIBLE:
13393 if (numFlags > 0) {
13394 output += " ";
13395 }
13396 output += "INVISIBLE";
13397 // USELESS HERE numFlags++;
13398 break;
13399 case GONE:
13400 if (numFlags > 0) {
13401 output += " ";
13402 }
13403 output += "GONE";
13404 // USELESS HERE numFlags++;
13405 break;
13406 default:
13407 break;
13408 }
13409 return output;
13410 }
13411
13412 /**
13413 * Build a human readable string representation of the specified private
13414 * view flags.
13415 *
13416 * @param privateFlags the private view flags to convert to a string
13417 * @return a String representing the supplied flags
13418 */
13419 private static String printPrivateFlags(int privateFlags) {
13420 String output = "";
13421 int numFlags = 0;
13422
13423 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13424 output += "WANTS_FOCUS";
13425 numFlags++;
13426 }
13427
13428 if ((privateFlags & FOCUSED) == FOCUSED) {
13429 if (numFlags > 0) {
13430 output += " ";
13431 }
13432 output += "FOCUSED";
13433 numFlags++;
13434 }
13435
13436 if ((privateFlags & SELECTED) == SELECTED) {
13437 if (numFlags > 0) {
13438 output += " ";
13439 }
13440 output += "SELECTED";
13441 numFlags++;
13442 }
13443
13444 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13445 if (numFlags > 0) {
13446 output += " ";
13447 }
13448 output += "IS_ROOT_NAMESPACE";
13449 numFlags++;
13450 }
13451
13452 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13453 if (numFlags > 0) {
13454 output += " ";
13455 }
13456 output += "HAS_BOUNDS";
13457 numFlags++;
13458 }
13459
13460 if ((privateFlags & DRAWN) == DRAWN) {
13461 if (numFlags > 0) {
13462 output += " ";
13463 }
13464 output += "DRAWN";
13465 // USELESS HERE numFlags++;
13466 }
13467 return output;
13468 }
13469
13470 /**
13471 * <p>Indicates whether or not this view's layout will be requested during
13472 * the next hierarchy layout pass.</p>
13473 *
13474 * @return true if the layout will be forced during next layout pass
13475 */
13476 public boolean isLayoutRequested() {
13477 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13478 }
13479
13480 /**
13481 * Assign a size and position to a view and all of its
13482 * descendants
13483 *
13484 * <p>This is the second phase of the layout mechanism.
13485 * (The first is measuring). In this phase, each parent calls
13486 * layout on all of its children to position them.
13487 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013488 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013489 *
Chet Haase9c087442011-01-12 16:20:16 -080013490 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013491 * Derived classes with children should override
13492 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013493 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494 *
13495 * @param l Left position, relative to parent
13496 * @param t Top position, relative to parent
13497 * @param r Right position, relative to parent
13498 * @param b Bottom position, relative to parent
13499 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013500 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013501 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013502 int oldL = mLeft;
13503 int oldT = mTop;
13504 int oldB = mBottom;
13505 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 boolean changed = setFrame(l, t, r, b);
13507 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013508 onLayout(changed, l, t, r, b);
13509 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013510
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013511 ListenerInfo li = mListenerInfo;
13512 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013513 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013514 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013515 int numListeners = listenersCopy.size();
13516 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013517 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013518 }
13519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013520 }
13521 mPrivateFlags &= ~FORCE_LAYOUT;
13522 }
13523
13524 /**
13525 * Called from layout when this view should
13526 * assign a size and position to each of its children.
13527 *
13528 * Derived classes with children should override
13529 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013530 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013531 * @param changed This is a new size or position for this view
13532 * @param left Left position, relative to parent
13533 * @param top Top position, relative to parent
13534 * @param right Right position, relative to parent
13535 * @param bottom Bottom position, relative to parent
13536 */
13537 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13538 }
13539
13540 /**
13541 * Assign a size and position to this view.
13542 *
13543 * This is called from layout.
13544 *
13545 * @param left Left position, relative to parent
13546 * @param top Top position, relative to parent
13547 * @param right Right position, relative to parent
13548 * @param bottom Bottom position, relative to parent
13549 * @return true if the new size and position are different than the
13550 * previous ones
13551 * {@hide}
13552 */
13553 protected boolean setFrame(int left, int top, int right, int bottom) {
13554 boolean changed = false;
13555
13556 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013557 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558 + right + "," + bottom + ")");
13559 }
13560
13561 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13562 changed = true;
13563
13564 // Remember our drawn bit
13565 int drawn = mPrivateFlags & DRAWN;
13566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013567 int oldWidth = mRight - mLeft;
13568 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013569 int newWidth = right - left;
13570 int newHeight = bottom - top;
13571 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13572
13573 // Invalidate our old position
13574 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013575
13576 mLeft = left;
13577 mTop = top;
13578 mRight = right;
13579 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013580 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013581 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013583
13584 mPrivateFlags |= HAS_BOUNDS;
13585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586
Chet Haase75755e22011-07-18 17:48:25 -070013587 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013588 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13589 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013590 if (mTransformationInfo != null) {
13591 mTransformationInfo.mMatrixDirty = true;
13592 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013594 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13595 }
13596
13597 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13598 // If we are visible, force the DRAWN bit to on so that
13599 // this invalidate will go through (at least to our parent).
13600 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013601 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602 // the DRAWN bit.
13603 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013604 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013605 // parent display list may need to be recreated based on a change in the bounds
13606 // of any child
13607 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 }
13609
13610 // Reset drawn bit to original value (invalidate turns it off)
13611 mPrivateFlags |= drawn;
13612
13613 mBackgroundSizeChanged = true;
13614 }
13615 return changed;
13616 }
13617
13618 /**
13619 * Finalize inflating a view from XML. This is called as the last phase
13620 * of inflation, after all child views have been added.
13621 *
13622 * <p>Even if the subclass overrides onFinishInflate, they should always be
13623 * sure to call the super method, so that we get called.
13624 */
13625 protected void onFinishInflate() {
13626 }
13627
13628 /**
13629 * Returns the resources associated with this view.
13630 *
13631 * @return Resources object.
13632 */
13633 public Resources getResources() {
13634 return mResources;
13635 }
13636
13637 /**
13638 * Invalidates the specified Drawable.
13639 *
13640 * @param drawable the drawable to invalidate
13641 */
13642 public void invalidateDrawable(Drawable drawable) {
13643 if (verifyDrawable(drawable)) {
13644 final Rect dirty = drawable.getBounds();
13645 final int scrollX = mScrollX;
13646 final int scrollY = mScrollY;
13647
13648 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13649 dirty.right + scrollX, dirty.bottom + scrollY);
13650 }
13651 }
13652
13653 /**
13654 * Schedules an action on a drawable to occur at a specified time.
13655 *
13656 * @param who the recipient of the action
13657 * @param what the action to run on the drawable
13658 * @param when the time at which the action must occur. Uses the
13659 * {@link SystemClock#uptimeMillis} timebase.
13660 */
13661 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013662 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013663 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013664 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013665 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13666 Choreographer.CALLBACK_ANIMATION, what, who,
13667 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013668 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013669 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 }
13672 }
13673
13674 /**
13675 * Cancels a scheduled action on a drawable.
13676 *
13677 * @param who the recipient of the action
13678 * @param what the action to cancel
13679 */
13680 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013681 if (verifyDrawable(who) && what != null) {
13682 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013683 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13684 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013685 } else {
13686 ViewRootImpl.getRunQueue().removeCallbacks(what);
13687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013688 }
13689 }
13690
13691 /**
13692 * Unschedule any events associated with the given Drawable. This can be
13693 * used when selecting a new Drawable into a view, so that the previous
13694 * one is completely unscheduled.
13695 *
13696 * @param who The Drawable to unschedule.
13697 *
13698 * @see #drawableStateChanged
13699 */
13700 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013701 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013702 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13703 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013704 }
13705 }
13706
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013707 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013708 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13709 * that the View directionality can and will be resolved before its Drawables.
13710 *
13711 * Will call {@link View#onResolveDrawables} when resolution is done.
13712 */
13713 public void resolveDrawables() {
13714 if (mBackground != null) {
13715 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13716 }
13717 onResolveDrawables(getResolvedLayoutDirection());
13718 }
13719
13720 /**
13721 * Called when layout direction has been resolved.
13722 *
13723 * The default implementation does nothing.
13724 *
13725 * @param layoutDirection The resolved layout direction.
13726 *
13727 * @see {@link #LAYOUT_DIRECTION_LTR}
13728 * @see {@link #LAYOUT_DIRECTION_RTL}
13729 */
13730 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013731 }
13732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013733 /**
13734 * If your view subclass is displaying its own Drawable objects, it should
13735 * override this function and return true for any Drawable it is
13736 * displaying. This allows animations for those drawables to be
13737 * scheduled.
13738 *
13739 * <p>Be sure to call through to the super class when overriding this
13740 * function.
13741 *
13742 * @param who The Drawable to verify. Return true if it is one you are
13743 * displaying, else return the result of calling through to the
13744 * super class.
13745 *
13746 * @return boolean If true than the Drawable is being displayed in the
13747 * view; else false and it is not allowed to animate.
13748 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013749 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13750 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 */
13752 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013753 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013754 }
13755
13756 /**
13757 * This function is called whenever the state of the view changes in such
13758 * a way that it impacts the state of drawables being shown.
13759 *
13760 * <p>Be sure to call through to the superclass when overriding this
13761 * function.
13762 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013763 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013764 */
13765 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013766 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013767 if (d != null && d.isStateful()) {
13768 d.setState(getDrawableState());
13769 }
13770 }
13771
13772 /**
13773 * Call this to force a view to update its drawable state. This will cause
13774 * drawableStateChanged to be called on this view. Views that are interested
13775 * in the new state should call getDrawableState.
13776 *
13777 * @see #drawableStateChanged
13778 * @see #getDrawableState
13779 */
13780 public void refreshDrawableState() {
13781 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13782 drawableStateChanged();
13783
13784 ViewParent parent = mParent;
13785 if (parent != null) {
13786 parent.childDrawableStateChanged(this);
13787 }
13788 }
13789
13790 /**
13791 * Return an array of resource IDs of the drawable states representing the
13792 * current state of the view.
13793 *
13794 * @return The current drawable state
13795 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013796 * @see Drawable#setState(int[])
13797 * @see #drawableStateChanged()
13798 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799 */
13800 public final int[] getDrawableState() {
13801 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13802 return mDrawableState;
13803 } else {
13804 mDrawableState = onCreateDrawableState(0);
13805 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13806 return mDrawableState;
13807 }
13808 }
13809
13810 /**
13811 * Generate the new {@link android.graphics.drawable.Drawable} state for
13812 * this view. This is called by the view
13813 * system when the cached Drawable state is determined to be invalid. To
13814 * retrieve the current state, you should use {@link #getDrawableState}.
13815 *
13816 * @param extraSpace if non-zero, this is the number of extra entries you
13817 * would like in the returned array in which you can place your own
13818 * states.
13819 *
13820 * @return Returns an array holding the current {@link Drawable} state of
13821 * the view.
13822 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013823 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824 */
13825 protected int[] onCreateDrawableState(int extraSpace) {
13826 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13827 mParent instanceof View) {
13828 return ((View) mParent).onCreateDrawableState(extraSpace);
13829 }
13830
13831 int[] drawableState;
13832
13833 int privateFlags = mPrivateFlags;
13834
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013835 int viewStateIndex = 0;
13836 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13837 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13838 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013839 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013840 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13841 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013842 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13843 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013844 // This is set if HW acceleration is requested, even if the current
13845 // process doesn't allow it. This is just to allow app preview
13846 // windows to better match their app.
13847 viewStateIndex |= VIEW_STATE_ACCELERATED;
13848 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013849 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013850
Christopher Tate3d4bf172011-03-28 16:16:46 -070013851 final int privateFlags2 = mPrivateFlags2;
13852 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13853 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013855 drawableState = VIEW_STATE_SETS[viewStateIndex];
13856
13857 //noinspection ConstantIfStatement
13858 if (false) {
13859 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13860 Log.i("View", toString()
13861 + " pressed=" + ((privateFlags & PRESSED) != 0)
13862 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13863 + " fo=" + hasFocus()
13864 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013865 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013866 + ": " + Arrays.toString(drawableState));
13867 }
13868
13869 if (extraSpace == 0) {
13870 return drawableState;
13871 }
13872
13873 final int[] fullState;
13874 if (drawableState != null) {
13875 fullState = new int[drawableState.length + extraSpace];
13876 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13877 } else {
13878 fullState = new int[extraSpace];
13879 }
13880
13881 return fullState;
13882 }
13883
13884 /**
13885 * Merge your own state values in <var>additionalState</var> into the base
13886 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013887 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013888 *
13889 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013890 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013891 * own additional state values.
13892 *
13893 * @param additionalState The additional state values you would like
13894 * added to <var>baseState</var>; this array is not modified.
13895 *
13896 * @return As a convenience, the <var>baseState</var> array you originally
13897 * passed into the function is returned.
13898 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013899 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 */
13901 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13902 final int N = baseState.length;
13903 int i = N - 1;
13904 while (i >= 0 && baseState[i] == 0) {
13905 i--;
13906 }
13907 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13908 return baseState;
13909 }
13910
13911 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013912 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13913 * on all Drawable objects associated with this view.
13914 */
13915 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013916 if (mBackground != null) {
13917 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013918 }
13919 }
13920
13921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013922 * Sets the background color for this view.
13923 * @param color the color of the background
13924 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013925 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013927 if (mBackground instanceof ColorDrawable) {
13928 ((ColorDrawable) mBackground).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070013929 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070013930 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013931 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013933 }
13934
13935 /**
13936 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013937 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013938 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013940 * @attr ref android.R.styleable#View_background
13941 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013942 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013943 public void setBackgroundResource(int resid) {
13944 if (resid != 0 && resid == mBackgroundResource) {
13945 return;
13946 }
13947
13948 Drawable d= null;
13949 if (resid != 0) {
13950 d = mResources.getDrawable(resid);
13951 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013952 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013953
13954 mBackgroundResource = resid;
13955 }
13956
13957 /**
13958 * Set the background to a given Drawable, or remove the background. If the
13959 * background has padding, this View's padding is set to the background's
13960 * padding. However, when a background is removed, this View's padding isn't
13961 * touched. If setting the padding is desired, please use
13962 * {@link #setPadding(int, int, int, int)}.
13963 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013964 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013965 * background
13966 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013967 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013968 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013969 setBackgroundDrawable(background);
13970 }
13971
13972 /**
13973 * @deprecated use {@link #setBackground(Drawable)} instead
13974 */
13975 @Deprecated
13976 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070013977 computeOpaqueFlags();
13978
Philip Milne6c8ea062012-04-03 17:38:43 -070013979 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013980 return;
13981 }
13982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 boolean requestLayout = false;
13984
13985 mBackgroundResource = 0;
13986
13987 /*
13988 * Regardless of whether we're setting a new background or not, we want
13989 * to clear the previous drawable.
13990 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013991 if (mBackground != null) {
13992 mBackground.setCallback(null);
13993 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013994 }
13995
Philip Milne6c8ea062012-04-03 17:38:43 -070013996 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013997 Rect padding = sThreadLocal.get();
13998 if (padding == null) {
13999 padding = new Rect();
14000 sThreadLocal.set(padding);
14001 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014002 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014003 if (background.getPadding(padding)) {
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014004 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014005 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014006 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014007 break;
14008 case LAYOUT_DIRECTION_LTR:
14009 default:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014010 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 }
14013
14014 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14015 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014016 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14017 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 requestLayout = true;
14019 }
14020
Philip Milne6c8ea062012-04-03 17:38:43 -070014021 background.setCallback(this);
14022 if (background.isStateful()) {
14023 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014024 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014025 background.setVisible(getVisibility() == VISIBLE, false);
14026 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027
14028 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14029 mPrivateFlags &= ~SKIP_DRAW;
14030 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14031 requestLayout = true;
14032 }
14033 } else {
14034 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014035 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014036
14037 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14038 /*
14039 * This view ONLY drew the background before and we're removing
14040 * the background, so now it won't draw anything
14041 * (hence we SKIP_DRAW)
14042 */
14043 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14044 mPrivateFlags |= SKIP_DRAW;
14045 }
14046
14047 /*
14048 * When the background is set, we try to apply its padding to this
14049 * View. When the background is removed, we don't touch this View's
14050 * padding. This is noted in the Javadocs. Hence, we don't need to
14051 * requestLayout(), the invalidate() below is sufficient.
14052 */
14053
14054 // The old background's minimum size could have affected this
14055 // View's layout, so let's requestLayout
14056 requestLayout = true;
14057 }
14058
Romain Guy8f1344f52009-05-15 16:03:59 -070014059 computeOpaqueFlags();
14060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 if (requestLayout) {
14062 requestLayout();
14063 }
14064
14065 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014066 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 }
14068
14069 /**
14070 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014073 *
14074 * @see #setBackground(Drawable)
14075 *
14076 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 */
14078 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014079 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 }
14081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 /**
14083 * Sets the padding. The view may add on the space required to display
14084 * the scrollbars, depending on the style and visibility of the scrollbars.
14085 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14086 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14087 * from the values set in this call.
14088 *
14089 * @attr ref android.R.styleable#View_padding
14090 * @attr ref android.R.styleable#View_paddingBottom
14091 * @attr ref android.R.styleable#View_paddingLeft
14092 * @attr ref android.R.styleable#View_paddingRight
14093 * @attr ref android.R.styleable#View_paddingTop
14094 * @param left the left padding in pixels
14095 * @param top the top padding in pixels
14096 * @param right the right padding in pixels
14097 * @param bottom the bottom padding in pixels
14098 */
14099 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014100 mUserPaddingStart = UNDEFINED_PADDING;
14101 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014102
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014103 internalSetPadding(left, top, right, bottom);
14104 }
14105
14106 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014107 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108 mUserPaddingRight = right;
14109 mUserPaddingBottom = bottom;
14110
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014111 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014112 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014113
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014114 // Common case is there are no scroll bars.
14115 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014116 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014117 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014118 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014119 switch (mVerticalScrollbarPosition) {
14120 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014121 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014122 left += offset;
14123 } else {
14124 right += offset;
14125 }
14126 break;
Adam Powell20232d02010-12-08 21:08:53 -080014127 case SCROLLBAR_POSITION_RIGHT:
14128 right += offset;
14129 break;
14130 case SCROLLBAR_POSITION_LEFT:
14131 left += offset;
14132 break;
14133 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014134 }
Adam Powell20232d02010-12-08 21:08:53 -080014135 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014136 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14137 ? 0 : getHorizontalScrollbarHeight();
14138 }
14139 }
Romain Guy8506ab42009-06-11 17:35:47 -070014140
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014141 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014142 changed = true;
14143 mPaddingLeft = left;
14144 }
14145 if (mPaddingTop != top) {
14146 changed = true;
14147 mPaddingTop = top;
14148 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014149 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014151 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014153 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014155 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014156 }
14157
14158 if (changed) {
14159 requestLayout();
14160 }
14161 }
14162
14163 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014164 * Sets the relative padding. The view may add on the space required to display
14165 * the scrollbars, depending on the style and visibility of the scrollbars.
14166 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14167 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14168 * from the values set in this call.
14169 *
14170 * @attr ref android.R.styleable#View_padding
14171 * @attr ref android.R.styleable#View_paddingBottom
14172 * @attr ref android.R.styleable#View_paddingStart
14173 * @attr ref android.R.styleable#View_paddingEnd
14174 * @attr ref android.R.styleable#View_paddingTop
14175 * @param start the start padding in pixels
14176 * @param top the top padding in pixels
14177 * @param end the end padding in pixels
14178 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014179 */
14180 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014181 mUserPaddingStart = start;
14182 mUserPaddingEnd = end;
14183
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014184 switch(getResolvedLayoutDirection()) {
14185 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014186 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014187 break;
14188 case LAYOUT_DIRECTION_LTR:
14189 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014190 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014191 }
14192 }
14193
14194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014195 * Returns the top padding of this view.
14196 *
14197 * @return the top padding in pixels
14198 */
14199 public int getPaddingTop() {
14200 return mPaddingTop;
14201 }
14202
14203 /**
14204 * Returns the bottom padding of this view. If there are inset and enabled
14205 * scrollbars, this value may include the space required to display the
14206 * scrollbars as well.
14207 *
14208 * @return the bottom padding in pixels
14209 */
14210 public int getPaddingBottom() {
14211 return mPaddingBottom;
14212 }
14213
14214 /**
14215 * Returns the left padding of this view. If there are inset and enabled
14216 * scrollbars, this value may include the space required to display the
14217 * scrollbars as well.
14218 *
14219 * @return the left padding in pixels
14220 */
14221 public int getPaddingLeft() {
14222 return mPaddingLeft;
14223 }
14224
14225 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014226 * Returns the start padding of this view depending on its resolved layout direction.
14227 * If there are inset and enabled scrollbars, this value may include the space
14228 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014229 *
14230 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014231 */
14232 public int getPaddingStart() {
14233 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14234 mPaddingRight : mPaddingLeft;
14235 }
14236
14237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014238 * Returns the right padding of this view. If there are inset and enabled
14239 * scrollbars, this value may include the space required to display the
14240 * scrollbars as well.
14241 *
14242 * @return the right padding in pixels
14243 */
14244 public int getPaddingRight() {
14245 return mPaddingRight;
14246 }
14247
14248 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014249 * Returns the end padding of this view depending on its resolved layout direction.
14250 * If there are inset and enabled scrollbars, this value may include the space
14251 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014252 *
14253 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014254 */
14255 public int getPaddingEnd() {
14256 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14257 mPaddingLeft : mPaddingRight;
14258 }
14259
14260 /**
14261 * Return if the padding as been set thru relative values
14262 * {@link #setPaddingRelative(int, int, int, int)} or thru
14263 * @attr ref android.R.styleable#View_paddingStart or
14264 * @attr ref android.R.styleable#View_paddingEnd
14265 *
14266 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014267 */
14268 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014269 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014270 }
14271
14272 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014273 * @hide
14274 */
Philip Milne7a23b492012-04-24 22:12:36 -070014275 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014276 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014277 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014278 }
14279 return mLayoutInsets;
14280 }
14281
14282 /**
14283 * @hide
14284 */
14285 public void setLayoutInsets(Insets layoutInsets) {
14286 mLayoutInsets = layoutInsets;
14287 }
14288
14289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014290 * Changes the selection state of this view. A view can be selected or not.
14291 * Note that selection is not the same as focus. Views are typically
14292 * selected in the context of an AdapterView like ListView or GridView;
14293 * the selected view is the view that is highlighted.
14294 *
14295 * @param selected true if the view must be selected, false otherwise
14296 */
14297 public void setSelected(boolean selected) {
14298 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14299 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014300 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014301 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 refreshDrawableState();
14303 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014304 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14305 notifyAccessibilityStateChanged();
14306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014307 }
14308 }
14309
14310 /**
14311 * Dispatch setSelected to all of this View's children.
14312 *
14313 * @see #setSelected(boolean)
14314 *
14315 * @param selected The new selected state
14316 */
14317 protected void dispatchSetSelected(boolean selected) {
14318 }
14319
14320 /**
14321 * Indicates the selection state of this view.
14322 *
14323 * @return true if the view is selected, false otherwise
14324 */
14325 @ViewDebug.ExportedProperty
14326 public boolean isSelected() {
14327 return (mPrivateFlags & SELECTED) != 0;
14328 }
14329
14330 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014331 * Changes the activated state of this view. A view can be activated or not.
14332 * Note that activation is not the same as selection. Selection is
14333 * a transient property, representing the view (hierarchy) the user is
14334 * currently interacting with. Activation is a longer-term state that the
14335 * user can move views in and out of. For example, in a list view with
14336 * single or multiple selection enabled, the views in the current selection
14337 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14338 * here.) The activated state is propagated down to children of the view it
14339 * is set on.
14340 *
14341 * @param activated true if the view must be activated, false otherwise
14342 */
14343 public void setActivated(boolean activated) {
14344 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14345 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014346 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014347 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014348 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014349 }
14350 }
14351
14352 /**
14353 * Dispatch setActivated to all of this View's children.
14354 *
14355 * @see #setActivated(boolean)
14356 *
14357 * @param activated The new activated state
14358 */
14359 protected void dispatchSetActivated(boolean activated) {
14360 }
14361
14362 /**
14363 * Indicates the activation state of this view.
14364 *
14365 * @return true if the view is activated, false otherwise
14366 */
14367 @ViewDebug.ExportedProperty
14368 public boolean isActivated() {
14369 return (mPrivateFlags & ACTIVATED) != 0;
14370 }
14371
14372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014373 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14374 * observer can be used to get notifications when global events, like
14375 * layout, happen.
14376 *
14377 * The returned ViewTreeObserver observer is not guaranteed to remain
14378 * valid for the lifetime of this View. If the caller of this method keeps
14379 * a long-lived reference to ViewTreeObserver, it should always check for
14380 * the return value of {@link ViewTreeObserver#isAlive()}.
14381 *
14382 * @return The ViewTreeObserver for this view's hierarchy.
14383 */
14384 public ViewTreeObserver getViewTreeObserver() {
14385 if (mAttachInfo != null) {
14386 return mAttachInfo.mTreeObserver;
14387 }
14388 if (mFloatingTreeObserver == null) {
14389 mFloatingTreeObserver = new ViewTreeObserver();
14390 }
14391 return mFloatingTreeObserver;
14392 }
14393
14394 /**
14395 * <p>Finds the topmost view in the current view hierarchy.</p>
14396 *
14397 * @return the topmost view containing this view
14398 */
14399 public View getRootView() {
14400 if (mAttachInfo != null) {
14401 final View v = mAttachInfo.mRootView;
14402 if (v != null) {
14403 return v;
14404 }
14405 }
Romain Guy8506ab42009-06-11 17:35:47 -070014406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 View parent = this;
14408
14409 while (parent.mParent != null && parent.mParent instanceof View) {
14410 parent = (View) parent.mParent;
14411 }
14412
14413 return parent;
14414 }
14415
14416 /**
14417 * <p>Computes the coordinates of this view on the screen. The argument
14418 * must be an array of two integers. After the method returns, the array
14419 * contains the x and y location in that order.</p>
14420 *
14421 * @param location an array of two integers in which to hold the coordinates
14422 */
14423 public void getLocationOnScreen(int[] location) {
14424 getLocationInWindow(location);
14425
14426 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014427 if (info != null) {
14428 location[0] += info.mWindowLeft;
14429 location[1] += info.mWindowTop;
14430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014431 }
14432
14433 /**
14434 * <p>Computes the coordinates of this view in its window. The argument
14435 * must be an array of two integers. After the method returns, the array
14436 * contains the x and y location in that order.</p>
14437 *
14438 * @param location an array of two integers in which to hold the coordinates
14439 */
14440 public void getLocationInWindow(int[] location) {
14441 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014442 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014443 }
14444
Gilles Debunne6583ce52011-12-06 18:09:02 -080014445 if (mAttachInfo == null) {
14446 // When the view is not attached to a window, this method does not make sense
14447 location[0] = location[1] = 0;
14448 return;
14449 }
14450
Gilles Debunnecea45132011-11-24 02:19:27 +010014451 float[] position = mAttachInfo.mTmpTransformLocation;
14452 position[0] = position[1] = 0.0f;
14453
14454 if (!hasIdentityMatrix()) {
14455 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014457
Gilles Debunnecea45132011-11-24 02:19:27 +010014458 position[0] += mLeft;
14459 position[1] += mTop;
14460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014461 ViewParent viewParent = mParent;
14462 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014463 final View view = (View) viewParent;
14464
14465 position[0] -= view.mScrollX;
14466 position[1] -= view.mScrollY;
14467
14468 if (!view.hasIdentityMatrix()) {
14469 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014470 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014471
14472 position[0] += view.mLeft;
14473 position[1] += view.mTop;
14474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014475 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014476 }
Romain Guy8506ab42009-06-11 17:35:47 -070014477
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014478 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014480 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14481 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014482 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014483
14484 location[0] = (int) (position[0] + 0.5f);
14485 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014486 }
14487
14488 /**
14489 * {@hide}
14490 * @param id the id of the view to be found
14491 * @return the view of the specified id, null if cannot be found
14492 */
14493 protected View findViewTraversal(int id) {
14494 if (id == mID) {
14495 return this;
14496 }
14497 return null;
14498 }
14499
14500 /**
14501 * {@hide}
14502 * @param tag the tag of the view to be found
14503 * @return the view of specified tag, null if cannot be found
14504 */
14505 protected View findViewWithTagTraversal(Object tag) {
14506 if (tag != null && tag.equals(mTag)) {
14507 return this;
14508 }
14509 return null;
14510 }
14511
14512 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014513 * {@hide}
14514 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014515 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014516 * @return The first view that matches the predicate or null.
14517 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014518 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014519 if (predicate.apply(this)) {
14520 return this;
14521 }
14522 return null;
14523 }
14524
14525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 * Look for a child view with the given id. If this view has the given
14527 * id, return this view.
14528 *
14529 * @param id The id to search for.
14530 * @return The view that has the given id in the hierarchy or null
14531 */
14532 public final View findViewById(int id) {
14533 if (id < 0) {
14534 return null;
14535 }
14536 return findViewTraversal(id);
14537 }
14538
14539 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014540 * Finds a view by its unuque and stable accessibility id.
14541 *
14542 * @param accessibilityId The searched accessibility id.
14543 * @return The found view.
14544 */
14545 final View findViewByAccessibilityId(int accessibilityId) {
14546 if (accessibilityId < 0) {
14547 return null;
14548 }
14549 return findViewByAccessibilityIdTraversal(accessibilityId);
14550 }
14551
14552 /**
14553 * Performs the traversal to find a view by its unuque and stable accessibility id.
14554 *
14555 * <strong>Note:</strong>This method does not stop at the root namespace
14556 * boundary since the user can touch the screen at an arbitrary location
14557 * potentially crossing the root namespace bounday which will send an
14558 * accessibility event to accessibility services and they should be able
14559 * to obtain the event source. Also accessibility ids are guaranteed to be
14560 * unique in the window.
14561 *
14562 * @param accessibilityId The accessibility id.
14563 * @return The found view.
14564 */
14565 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14566 if (getAccessibilityViewId() == accessibilityId) {
14567 return this;
14568 }
14569 return null;
14570 }
14571
14572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014573 * Look for a child view with the given tag. If this view has the given
14574 * tag, return this view.
14575 *
14576 * @param tag The tag to search for, using "tag.equals(getTag())".
14577 * @return The View that has the given tag in the hierarchy or null
14578 */
14579 public final View findViewWithTag(Object tag) {
14580 if (tag == null) {
14581 return null;
14582 }
14583 return findViewWithTagTraversal(tag);
14584 }
14585
14586 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014587 * {@hide}
14588 * Look for a child view that matches the specified predicate.
14589 * If this view matches the predicate, return this view.
14590 *
14591 * @param predicate The predicate to evaluate.
14592 * @return The first view that matches the predicate or null.
14593 */
14594 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014595 return findViewByPredicateTraversal(predicate, null);
14596 }
14597
14598 /**
14599 * {@hide}
14600 * Look for a child view that matches the specified predicate,
14601 * starting with the specified view and its descendents and then
14602 * recusively searching the ancestors and siblings of that view
14603 * until this view is reached.
14604 *
14605 * This method is useful in cases where the predicate does not match
14606 * a single unique view (perhaps multiple views use the same id)
14607 * and we are trying to find the view that is "closest" in scope to the
14608 * starting view.
14609 *
14610 * @param start The view to start from.
14611 * @param predicate The predicate to evaluate.
14612 * @return The first view that matches the predicate or null.
14613 */
14614 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14615 View childToSkip = null;
14616 for (;;) {
14617 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14618 if (view != null || start == this) {
14619 return view;
14620 }
14621
14622 ViewParent parent = start.getParent();
14623 if (parent == null || !(parent instanceof View)) {
14624 return null;
14625 }
14626
14627 childToSkip = start;
14628 start = (View) parent;
14629 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014630 }
14631
14632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014633 * Sets the identifier for this view. The identifier does not have to be
14634 * unique in this view's hierarchy. The identifier should be a positive
14635 * number.
14636 *
14637 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014638 * @see #getId()
14639 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014640 *
14641 * @param id a number used to identify the view
14642 *
14643 * @attr ref android.R.styleable#View_id
14644 */
14645 public void setId(int id) {
14646 mID = id;
14647 }
14648
14649 /**
14650 * {@hide}
14651 *
14652 * @param isRoot true if the view belongs to the root namespace, false
14653 * otherwise
14654 */
14655 public void setIsRootNamespace(boolean isRoot) {
14656 if (isRoot) {
14657 mPrivateFlags |= IS_ROOT_NAMESPACE;
14658 } else {
14659 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14660 }
14661 }
14662
14663 /**
14664 * {@hide}
14665 *
14666 * @return true if the view belongs to the root namespace, false otherwise
14667 */
14668 public boolean isRootNamespace() {
14669 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14670 }
14671
14672 /**
14673 * Returns this view's identifier.
14674 *
14675 * @return a positive integer used to identify the view or {@link #NO_ID}
14676 * if the view has no ID
14677 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014678 * @see #setId(int)
14679 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014680 * @attr ref android.R.styleable#View_id
14681 */
14682 @ViewDebug.CapturedViewProperty
14683 public int getId() {
14684 return mID;
14685 }
14686
14687 /**
14688 * Returns this view's tag.
14689 *
14690 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014691 *
14692 * @see #setTag(Object)
14693 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014694 */
14695 @ViewDebug.ExportedProperty
14696 public Object getTag() {
14697 return mTag;
14698 }
14699
14700 /**
14701 * Sets the tag associated with this view. A tag can be used to mark
14702 * a view in its hierarchy and does not have to be unique within the
14703 * hierarchy. Tags can also be used to store data within a view without
14704 * resorting to another data structure.
14705 *
14706 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014707 *
14708 * @see #getTag()
14709 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014710 */
14711 public void setTag(final Object tag) {
14712 mTag = tag;
14713 }
14714
14715 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014716 * Returns the tag associated with this view and the specified key.
14717 *
14718 * @param key The key identifying the tag
14719 *
14720 * @return the Object stored in this view as a tag
14721 *
14722 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014723 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014724 */
14725 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014726 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014727 return null;
14728 }
14729
14730 /**
14731 * Sets a tag associated with this view and a key. A tag can be used
14732 * to mark a view in its hierarchy and does not have to be unique within
14733 * the hierarchy. Tags can also be used to store data within a view
14734 * without resorting to another data structure.
14735 *
14736 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014737 * application to ensure it is unique (see the <a
14738 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14739 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014740 * the Android framework or not associated with any package will cause
14741 * an {@link IllegalArgumentException} to be thrown.
14742 *
14743 * @param key The key identifying the tag
14744 * @param tag An Object to tag the view with
14745 *
14746 * @throws IllegalArgumentException If they specified key is not valid
14747 *
14748 * @see #setTag(Object)
14749 * @see #getTag(int)
14750 */
14751 public void setTag(int key, final Object tag) {
14752 // If the package id is 0x00 or 0x01, it's either an undefined package
14753 // or a framework id
14754 if ((key >>> 24) < 2) {
14755 throw new IllegalArgumentException("The key must be an application-specific "
14756 + "resource id.");
14757 }
14758
Adam Powell2b2f6d62011-09-23 11:15:39 -070014759 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014760 }
14761
14762 /**
14763 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14764 * framework id.
14765 *
14766 * @hide
14767 */
14768 public void setTagInternal(int key, Object tag) {
14769 if ((key >>> 24) != 0x1) {
14770 throw new IllegalArgumentException("The key must be a framework-specific "
14771 + "resource id.");
14772 }
14773
Adam Powell2b2f6d62011-09-23 11:15:39 -070014774 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014775 }
14776
Adam Powell2b2f6d62011-09-23 11:15:39 -070014777 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014778 if (mKeyedTags == null) {
14779 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014780 }
14781
Adam Powell7db82ac2011-09-22 19:44:04 -070014782 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014783 }
14784
14785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 * Prints information about this view in the log output, with the tag
14787 * {@link #VIEW_LOG_TAG}.
14788 *
14789 * @hide
14790 */
14791 public void debug() {
14792 debug(0);
14793 }
14794
14795 /**
14796 * Prints information about this view in the log output, with the tag
14797 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14798 * indentation defined by the <code>depth</code>.
14799 *
14800 * @param depth the indentation level
14801 *
14802 * @hide
14803 */
14804 protected void debug(int depth) {
14805 String output = debugIndent(depth - 1);
14806
14807 output += "+ " + this;
14808 int id = getId();
14809 if (id != -1) {
14810 output += " (id=" + id + ")";
14811 }
14812 Object tag = getTag();
14813 if (tag != null) {
14814 output += " (tag=" + tag + ")";
14815 }
14816 Log.d(VIEW_LOG_TAG, output);
14817
14818 if ((mPrivateFlags & FOCUSED) != 0) {
14819 output = debugIndent(depth) + " FOCUSED";
14820 Log.d(VIEW_LOG_TAG, output);
14821 }
14822
14823 output = debugIndent(depth);
14824 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14825 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14826 + "} ";
14827 Log.d(VIEW_LOG_TAG, output);
14828
14829 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14830 || mPaddingBottom != 0) {
14831 output = debugIndent(depth);
14832 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14833 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14834 Log.d(VIEW_LOG_TAG, output);
14835 }
14836
14837 output = debugIndent(depth);
14838 output += "mMeasureWidth=" + mMeasuredWidth +
14839 " mMeasureHeight=" + mMeasuredHeight;
14840 Log.d(VIEW_LOG_TAG, output);
14841
14842 output = debugIndent(depth);
14843 if (mLayoutParams == null) {
14844 output += "BAD! no layout params";
14845 } else {
14846 output = mLayoutParams.debug(output);
14847 }
14848 Log.d(VIEW_LOG_TAG, output);
14849
14850 output = debugIndent(depth);
14851 output += "flags={";
14852 output += View.printFlags(mViewFlags);
14853 output += "}";
14854 Log.d(VIEW_LOG_TAG, output);
14855
14856 output = debugIndent(depth);
14857 output += "privateFlags={";
14858 output += View.printPrivateFlags(mPrivateFlags);
14859 output += "}";
14860 Log.d(VIEW_LOG_TAG, output);
14861 }
14862
14863 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014864 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014865 *
14866 * @param depth the indentation level
14867 * @return a String containing (depth * 2 + 3) * 2 white spaces
14868 *
14869 * @hide
14870 */
14871 protected static String debugIndent(int depth) {
14872 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14873 for (int i = 0; i < (depth * 2) + 3; i++) {
14874 spaces.append(' ').append(' ');
14875 }
14876 return spaces.toString();
14877 }
14878
14879 /**
14880 * <p>Return the offset of the widget's text baseline from the widget's top
14881 * boundary. If this widget does not support baseline alignment, this
14882 * method returns -1. </p>
14883 *
14884 * @return the offset of the baseline within the widget's bounds or -1
14885 * if baseline alignment is not supported
14886 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014887 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 public int getBaseline() {
14889 return -1;
14890 }
14891
14892 /**
14893 * Call this when something has changed which has invalidated the
14894 * layout of this view. This will schedule a layout pass of the view
14895 * tree.
14896 */
14897 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014898 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014899 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014900
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014901 if (mLayoutParams != null) {
14902 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14903 }
14904
14905 if (mParent != null && !mParent.isLayoutRequested()) {
14906 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014907 }
14908 }
14909
14910 /**
14911 * Forces this view to be laid out during the next layout pass.
14912 * This method does not call requestLayout() or forceLayout()
14913 * on the parent.
14914 */
14915 public void forceLayout() {
14916 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014917 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014918 }
14919
14920 /**
14921 * <p>
14922 * This is called to find out how big a view should be. The parent
14923 * supplies constraint information in the width and height parameters.
14924 * </p>
14925 *
14926 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014927 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014928 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014929 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014930 * </p>
14931 *
14932 *
14933 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14934 * parent
14935 * @param heightMeasureSpec Vertical space requirements as imposed by the
14936 * parent
14937 *
14938 * @see #onMeasure(int, int)
14939 */
14940 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14941 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14942 widthMeasureSpec != mOldWidthMeasureSpec ||
14943 heightMeasureSpec != mOldHeightMeasureSpec) {
14944
14945 // first clears the measured dimension flag
14946 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014948 // measure ourselves, this should set the measured dimension flag back
14949 onMeasure(widthMeasureSpec, heightMeasureSpec);
14950
14951 // flag not set, setMeasuredDimension() was not invoked, we raise
14952 // an exception to warn the developer
14953 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14954 throw new IllegalStateException("onMeasure() did not set the"
14955 + " measured dimension by calling"
14956 + " setMeasuredDimension()");
14957 }
14958
14959 mPrivateFlags |= LAYOUT_REQUIRED;
14960 }
14961
14962 mOldWidthMeasureSpec = widthMeasureSpec;
14963 mOldHeightMeasureSpec = heightMeasureSpec;
14964 }
14965
14966 /**
14967 * <p>
14968 * Measure the view and its content to determine the measured width and the
14969 * measured height. This method is invoked by {@link #measure(int, int)} and
14970 * should be overriden by subclasses to provide accurate and efficient
14971 * measurement of their contents.
14972 * </p>
14973 *
14974 * <p>
14975 * <strong>CONTRACT:</strong> When overriding this method, you
14976 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14977 * measured width and height of this view. Failure to do so will trigger an
14978 * <code>IllegalStateException</code>, thrown by
14979 * {@link #measure(int, int)}. Calling the superclass'
14980 * {@link #onMeasure(int, int)} is a valid use.
14981 * </p>
14982 *
14983 * <p>
14984 * The base class implementation of measure defaults to the background size,
14985 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14986 * override {@link #onMeasure(int, int)} to provide better measurements of
14987 * their content.
14988 * </p>
14989 *
14990 * <p>
14991 * If this method is overridden, it is the subclass's responsibility to make
14992 * sure the measured height and width are at least the view's minimum height
14993 * and width ({@link #getSuggestedMinimumHeight()} and
14994 * {@link #getSuggestedMinimumWidth()}).
14995 * </p>
14996 *
14997 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14998 * The requirements are encoded with
14999 * {@link android.view.View.MeasureSpec}.
15000 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15001 * The requirements are encoded with
15002 * {@link android.view.View.MeasureSpec}.
15003 *
15004 * @see #getMeasuredWidth()
15005 * @see #getMeasuredHeight()
15006 * @see #setMeasuredDimension(int, int)
15007 * @see #getSuggestedMinimumHeight()
15008 * @see #getSuggestedMinimumWidth()
15009 * @see android.view.View.MeasureSpec#getMode(int)
15010 * @see android.view.View.MeasureSpec#getSize(int)
15011 */
15012 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15013 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15014 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15015 }
15016
15017 /**
15018 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15019 * measured width and measured height. Failing to do so will trigger an
15020 * exception at measurement time.</p>
15021 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015022 * @param measuredWidth The measured width of this view. May be a complex
15023 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15024 * {@link #MEASURED_STATE_TOO_SMALL}.
15025 * @param measuredHeight The measured height of this view. May be a complex
15026 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15027 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015028 */
15029 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15030 mMeasuredWidth = measuredWidth;
15031 mMeasuredHeight = measuredHeight;
15032
15033 mPrivateFlags |= MEASURED_DIMENSION_SET;
15034 }
15035
15036 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015037 * Merge two states as returned by {@link #getMeasuredState()}.
15038 * @param curState The current state as returned from a view or the result
15039 * of combining multiple views.
15040 * @param newState The new view state to combine.
15041 * @return Returns a new integer reflecting the combination of the two
15042 * states.
15043 */
15044 public static int combineMeasuredStates(int curState, int newState) {
15045 return curState | newState;
15046 }
15047
15048 /**
15049 * Version of {@link #resolveSizeAndState(int, int, int)}
15050 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15051 */
15052 public static int resolveSize(int size, int measureSpec) {
15053 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15054 }
15055
15056 /**
15057 * Utility to reconcile a desired size and state, with constraints imposed
15058 * by a MeasureSpec. Will take the desired size, unless a different size
15059 * is imposed by the constraints. The returned value is a compound integer,
15060 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15061 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15062 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015063 *
15064 * @param size How big the view wants to be
15065 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015066 * @return Size information bit mask as defined by
15067 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015068 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015069 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015070 int result = size;
15071 int specMode = MeasureSpec.getMode(measureSpec);
15072 int specSize = MeasureSpec.getSize(measureSpec);
15073 switch (specMode) {
15074 case MeasureSpec.UNSPECIFIED:
15075 result = size;
15076 break;
15077 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015078 if (specSize < size) {
15079 result = specSize | MEASURED_STATE_TOO_SMALL;
15080 } else {
15081 result = size;
15082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015083 break;
15084 case MeasureSpec.EXACTLY:
15085 result = specSize;
15086 break;
15087 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015088 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015089 }
15090
15091 /**
15092 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015093 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 * by the MeasureSpec.
15095 *
15096 * @param size Default size for this view
15097 * @param measureSpec Constraints imposed by the parent
15098 * @return The size this view should be.
15099 */
15100 public static int getDefaultSize(int size, int measureSpec) {
15101 int result = size;
15102 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015103 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104
15105 switch (specMode) {
15106 case MeasureSpec.UNSPECIFIED:
15107 result = size;
15108 break;
15109 case MeasureSpec.AT_MOST:
15110 case MeasureSpec.EXACTLY:
15111 result = specSize;
15112 break;
15113 }
15114 return result;
15115 }
15116
15117 /**
15118 * Returns the suggested minimum height that the view should use. This
15119 * returns the maximum of the view's minimum height
15120 * and the background's minimum height
15121 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15122 * <p>
15123 * When being used in {@link #onMeasure(int, int)}, the caller should still
15124 * ensure the returned height is within the requirements of the parent.
15125 *
15126 * @return The suggested minimum height of the view.
15127 */
15128 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015129 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015131 }
15132
15133 /**
15134 * Returns the suggested minimum width that the view should use. This
15135 * returns the maximum of the view's minimum width)
15136 * and the background's minimum width
15137 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15138 * <p>
15139 * When being used in {@link #onMeasure(int, int)}, the caller should still
15140 * ensure the returned width is within the requirements of the parent.
15141 *
15142 * @return The suggested minimum width of the view.
15143 */
15144 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015145 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015147
Philip Milne6c8ea062012-04-03 17:38:43 -070015148 /**
15149 * Returns the minimum height of the view.
15150 *
15151 * @return the minimum height the view will try to be.
15152 *
15153 * @see #setMinimumHeight(int)
15154 *
15155 * @attr ref android.R.styleable#View_minHeight
15156 */
15157 public int getMinimumHeight() {
15158 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015159 }
15160
15161 /**
15162 * Sets the minimum height of the view. It is not guaranteed the view will
15163 * be able to achieve this minimum height (for example, if its parent layout
15164 * constrains it with less available height).
15165 *
15166 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015167 *
15168 * @see #getMinimumHeight()
15169 *
15170 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015171 */
15172 public void setMinimumHeight(int minHeight) {
15173 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015174 requestLayout();
15175 }
15176
15177 /**
15178 * Returns the minimum width of the view.
15179 *
15180 * @return the minimum width the view will try to be.
15181 *
15182 * @see #setMinimumWidth(int)
15183 *
15184 * @attr ref android.R.styleable#View_minWidth
15185 */
15186 public int getMinimumWidth() {
15187 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015188 }
15189
15190 /**
15191 * Sets the minimum width of the view. It is not guaranteed the view will
15192 * be able to achieve this minimum width (for example, if its parent layout
15193 * constrains it with less available width).
15194 *
15195 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015196 *
15197 * @see #getMinimumWidth()
15198 *
15199 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015200 */
15201 public void setMinimumWidth(int minWidth) {
15202 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015203 requestLayout();
15204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015205 }
15206
15207 /**
15208 * Get the animation currently associated with this view.
15209 *
15210 * @return The animation that is currently playing or
15211 * scheduled to play for this view.
15212 */
15213 public Animation getAnimation() {
15214 return mCurrentAnimation;
15215 }
15216
15217 /**
15218 * Start the specified animation now.
15219 *
15220 * @param animation the animation to start now
15221 */
15222 public void startAnimation(Animation animation) {
15223 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15224 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015225 invalidateParentCaches();
15226 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015227 }
15228
15229 /**
15230 * Cancels any animations for this view.
15231 */
15232 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015233 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015234 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015237 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015238 }
15239
15240 /**
15241 * Sets the next animation to play for this view.
15242 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015243 * {@link #startAnimation(android.view.animation.Animation)} instead.
15244 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015245 * control over the start time and invalidation, but you
15246 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015247 * 2) the view's parent (which controls animations on its children)
15248 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015249 * start.
15250 *
15251 * @param animation The next animation, or null.
15252 */
15253 public void setAnimation(Animation animation) {
15254 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015256 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015257 // If the screen is off assume the animation start time is now instead of
15258 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15259 // would cause the animation to start when the screen turns back on
15260 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15261 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15262 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015264 animation.reset();
15265 }
15266 }
15267
15268 /**
15269 * Invoked by a parent ViewGroup to notify the start of the animation
15270 * currently associated with this view. If you override this method,
15271 * always call super.onAnimationStart();
15272 *
15273 * @see #setAnimation(android.view.animation.Animation)
15274 * @see #getAnimation()
15275 */
15276 protected void onAnimationStart() {
15277 mPrivateFlags |= ANIMATION_STARTED;
15278 }
15279
15280 /**
15281 * Invoked by a parent ViewGroup to notify the end of the animation
15282 * currently associated with this view. If you override this method,
15283 * always call super.onAnimationEnd();
15284 *
15285 * @see #setAnimation(android.view.animation.Animation)
15286 * @see #getAnimation()
15287 */
15288 protected void onAnimationEnd() {
15289 mPrivateFlags &= ~ANIMATION_STARTED;
15290 }
15291
15292 /**
15293 * Invoked if there is a Transform that involves alpha. Subclass that can
15294 * draw themselves with the specified alpha should return true, and then
15295 * respect that alpha when their onDraw() is called. If this returns false
15296 * then the view may be redirected to draw into an offscreen buffer to
15297 * fulfill the request, which will look fine, but may be slower than if the
15298 * subclass handles it internally. The default implementation returns false.
15299 *
15300 * @param alpha The alpha (0..255) to apply to the view's drawing
15301 * @return true if the view can draw with the specified alpha.
15302 */
15303 protected boolean onSetAlpha(int alpha) {
15304 return false;
15305 }
15306
15307 /**
15308 * This is used by the RootView to perform an optimization when
15309 * the view hierarchy contains one or several SurfaceView.
15310 * SurfaceView is always considered transparent, but its children are not,
15311 * therefore all View objects remove themselves from the global transparent
15312 * region (passed as a parameter to this function).
15313 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015314 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015315 *
15316 * @return Returns true if the effective visibility of the view at this
15317 * point is opaque, regardless of the transparent region; returns false
15318 * if it is possible for underlying windows to be seen behind the view.
15319 *
15320 * {@hide}
15321 */
15322 public boolean gatherTransparentRegion(Region region) {
15323 final AttachInfo attachInfo = mAttachInfo;
15324 if (region != null && attachInfo != null) {
15325 final int pflags = mPrivateFlags;
15326 if ((pflags & SKIP_DRAW) == 0) {
15327 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15328 // remove it from the transparent region.
15329 final int[] location = attachInfo.mTransparentLocation;
15330 getLocationInWindow(location);
15331 region.op(location[0], location[1], location[0] + mRight - mLeft,
15332 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015333 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015334 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15335 // exists, so we remove the background drawable's non-transparent
15336 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015337 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015338 }
15339 }
15340 return true;
15341 }
15342
15343 /**
15344 * Play a sound effect for this view.
15345 *
15346 * <p>The framework will play sound effects for some built in actions, such as
15347 * clicking, but you may wish to play these effects in your widget,
15348 * for instance, for internal navigation.
15349 *
15350 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15351 * {@link #isSoundEffectsEnabled()} is true.
15352 *
15353 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15354 */
15355 public void playSoundEffect(int soundConstant) {
15356 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15357 return;
15358 }
15359 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15360 }
15361
15362 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015363 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015364 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015365 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015366 *
15367 * <p>The framework will provide haptic feedback for some built in actions,
15368 * such as long presses, but you may wish to provide feedback for your
15369 * own widget.
15370 *
15371 * <p>The feedback will only be performed if
15372 * {@link #isHapticFeedbackEnabled()} is true.
15373 *
15374 * @param feedbackConstant One of the constants defined in
15375 * {@link HapticFeedbackConstants}
15376 */
15377 public boolean performHapticFeedback(int feedbackConstant) {
15378 return performHapticFeedback(feedbackConstant, 0);
15379 }
15380
15381 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015382 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015383 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015384 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015385 *
15386 * @param feedbackConstant One of the constants defined in
15387 * {@link HapticFeedbackConstants}
15388 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15389 */
15390 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15391 if (mAttachInfo == null) {
15392 return false;
15393 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015394 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015395 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015396 && !isHapticFeedbackEnabled()) {
15397 return false;
15398 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015399 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15400 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015401 }
15402
15403 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015404 * Request that the visibility of the status bar or other screen/window
15405 * decorations be changed.
15406 *
15407 * <p>This method is used to put the over device UI into temporary modes
15408 * where the user's attention is focused more on the application content,
15409 * by dimming or hiding surrounding system affordances. This is typically
15410 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15411 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15412 * to be placed behind the action bar (and with these flags other system
15413 * affordances) so that smooth transitions between hiding and showing them
15414 * can be done.
15415 *
15416 * <p>Two representative examples of the use of system UI visibility is
15417 * implementing a content browsing application (like a magazine reader)
15418 * and a video playing application.
15419 *
15420 * <p>The first code shows a typical implementation of a View in a content
15421 * browsing application. In this implementation, the application goes
15422 * into a content-oriented mode by hiding the status bar and action bar,
15423 * and putting the navigation elements into lights out mode. The user can
15424 * then interact with content while in this mode. Such an application should
15425 * provide an easy way for the user to toggle out of the mode (such as to
15426 * check information in the status bar or access notifications). In the
15427 * implementation here, this is done simply by tapping on the content.
15428 *
15429 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15430 * content}
15431 *
15432 * <p>This second code sample shows a typical implementation of a View
15433 * in a video playing application. In this situation, while the video is
15434 * playing the application would like to go into a complete full-screen mode,
15435 * to use as much of the display as possible for the video. When in this state
15436 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015437 * touching on the screen to pop the UI out of full screen mode. See
15438 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015439 *
15440 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15441 * content}
15442 *
15443 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15444 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15445 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15446 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015447 */
15448 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015449 if (visibility != mSystemUiVisibility) {
15450 mSystemUiVisibility = visibility;
15451 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15452 mParent.recomputeViewAttributes(this);
15453 }
Joe Onorato664644d2011-01-23 17:53:23 -080015454 }
15455 }
15456
15457 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015458 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15459 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15460 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15461 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15462 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015463 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015464 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015465 return mSystemUiVisibility;
15466 }
15467
Scott Mainec6331b2011-05-24 16:55:56 -070015468 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015469 * Returns the current system UI visibility that is currently set for
15470 * the entire window. This is the combination of the
15471 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15472 * views in the window.
15473 */
15474 public int getWindowSystemUiVisibility() {
15475 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15476 }
15477
15478 /**
15479 * Override to find out when the window's requested system UI visibility
15480 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15481 * This is different from the callbacks recieved through
15482 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15483 * in that this is only telling you about the local request of the window,
15484 * not the actual values applied by the system.
15485 */
15486 public void onWindowSystemUiVisibilityChanged(int visible) {
15487 }
15488
15489 /**
15490 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15491 * the view hierarchy.
15492 */
15493 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15494 onWindowSystemUiVisibilityChanged(visible);
15495 }
15496
15497 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015498 * Set a listener to receive callbacks when the visibility of the system bar changes.
15499 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15500 */
Joe Onorato664644d2011-01-23 17:53:23 -080015501 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015502 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015503 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15504 mParent.recomputeViewAttributes(this);
15505 }
15506 }
15507
15508 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015509 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15510 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015511 */
15512 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015513 ListenerInfo li = mListenerInfo;
15514 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15515 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015516 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015517 }
15518 }
15519
Dianne Hackborncf675782012-05-10 15:07:24 -070015520 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015521 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15522 if (val != mSystemUiVisibility) {
15523 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015524 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015525 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015526 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015527 }
15528
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015529 /** @hide */
15530 public void setDisabledSystemUiVisibility(int flags) {
15531 if (mAttachInfo != null) {
15532 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15533 mAttachInfo.mDisabledSystemUiVisibility = flags;
15534 if (mParent != null) {
15535 mParent.recomputeViewAttributes(this);
15536 }
15537 }
15538 }
15539 }
15540
Joe Onorato664644d2011-01-23 17:53:23 -080015541 /**
Joe Malin32736f02011-01-19 16:14:20 -080015542 * Creates an image that the system displays during the drag and drop
15543 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15544 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15545 * appearance as the given View. The default also positions the center of the drag shadow
15546 * directly under the touch point. If no View is provided (the constructor with no parameters
15547 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15548 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15549 * default is an invisible drag shadow.
15550 * <p>
15551 * You are not required to use the View you provide to the constructor as the basis of the
15552 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15553 * anything you want as the drag shadow.
15554 * </p>
15555 * <p>
15556 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15557 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15558 * size and position of the drag shadow. It uses this data to construct a
15559 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15560 * so that your application can draw the shadow image in the Canvas.
15561 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015562 *
15563 * <div class="special reference">
15564 * <h3>Developer Guides</h3>
15565 * <p>For a guide to implementing drag and drop features, read the
15566 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15567 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015568 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015569 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015570 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015571
15572 /**
Joe Malin32736f02011-01-19 16:14:20 -080015573 * Constructs a shadow image builder based on a View. By default, the resulting drag
15574 * shadow will have the same appearance and dimensions as the View, with the touch point
15575 * over the center of the View.
15576 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015577 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015578 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015579 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015580 }
15581
Christopher Tate17ed60c2011-01-18 12:50:26 -080015582 /**
15583 * Construct a shadow builder object with no associated View. This
15584 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15585 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15586 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015587 * reference to any View object. If they are not overridden, then the result is an
15588 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015589 */
15590 public DragShadowBuilder() {
15591 mView = new WeakReference<View>(null);
15592 }
15593
15594 /**
15595 * Returns the View object that had been passed to the
15596 * {@link #View.DragShadowBuilder(View)}
15597 * constructor. If that View parameter was {@code null} or if the
15598 * {@link #View.DragShadowBuilder()}
15599 * constructor was used to instantiate the builder object, this method will return
15600 * null.
15601 *
15602 * @return The View object associate with this builder object.
15603 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015604 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015605 final public View getView() {
15606 return mView.get();
15607 }
15608
Christopher Tate2c095f32010-10-04 14:13:40 -070015609 /**
Joe Malin32736f02011-01-19 16:14:20 -080015610 * Provides the metrics for the shadow image. These include the dimensions of
15611 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015612 * be centered under the touch location while dragging.
15613 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015614 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015615 * same as the dimensions of the View itself and centers the shadow under
15616 * the touch point.
15617 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015618 *
Joe Malin32736f02011-01-19 16:14:20 -080015619 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15620 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15621 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15622 * image.
15623 *
15624 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15625 * shadow image that should be underneath the touch point during the drag and drop
15626 * operation. Your application must set {@link android.graphics.Point#x} to the
15627 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015628 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015629 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015630 final View view = mView.get();
15631 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015632 shadowSize.set(view.getWidth(), view.getHeight());
15633 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015634 } else {
15635 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15636 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015637 }
15638
15639 /**
Joe Malin32736f02011-01-19 16:14:20 -080015640 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15641 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015642 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015643 *
Joe Malin32736f02011-01-19 16:14:20 -080015644 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015645 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015646 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015647 final View view = mView.get();
15648 if (view != null) {
15649 view.draw(canvas);
15650 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015651 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015652 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015653 }
15654 }
15655
15656 /**
Joe Malin32736f02011-01-19 16:14:20 -080015657 * Starts a drag and drop operation. When your application calls this method, it passes a
15658 * {@link android.view.View.DragShadowBuilder} object to the system. The
15659 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15660 * to get metrics for the drag shadow, and then calls the object's
15661 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15662 * <p>
15663 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15664 * drag events to all the View objects in your application that are currently visible. It does
15665 * this either by calling the View object's drag listener (an implementation of
15666 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15667 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15668 * Both are passed a {@link android.view.DragEvent} object that has a
15669 * {@link android.view.DragEvent#getAction()} value of
15670 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15671 * </p>
15672 * <p>
15673 * Your application can invoke startDrag() on any attached View object. The View object does not
15674 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15675 * be related to the View the user selected for dragging.
15676 * </p>
15677 * @param data A {@link android.content.ClipData} object pointing to the data to be
15678 * transferred by the drag and drop operation.
15679 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15680 * drag shadow.
15681 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15682 * drop operation. This Object is put into every DragEvent object sent by the system during the
15683 * current drag.
15684 * <p>
15685 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15686 * to the target Views. For example, it can contain flags that differentiate between a
15687 * a copy operation and a move operation.
15688 * </p>
15689 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15690 * so the parameter should be set to 0.
15691 * @return {@code true} if the method completes successfully, or
15692 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15693 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015694 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015695 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015696 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015697 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015698 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015699 }
15700 boolean okay = false;
15701
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015702 Point shadowSize = new Point();
15703 Point shadowTouchPoint = new Point();
15704 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015705
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015706 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15707 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15708 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015709 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015710
Chris Tatea32dcf72010-10-14 12:13:50 -070015711 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015712 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15713 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015714 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015715 Surface surface = new Surface();
15716 try {
15717 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015718 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015719 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015720 + " surface=" + surface);
15721 if (token != null) {
15722 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015723 try {
Chris Tate6b391282010-10-14 15:48:59 -070015724 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015725 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015726 } finally {
15727 surface.unlockCanvasAndPost(canvas);
15728 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015729
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015730 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015731
15732 // Cache the local state object for delivery with DragEvents
15733 root.setLocalDragState(myLocalState);
15734
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015735 // repurpose 'shadowSize' for the last touch point
15736 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015737
Christopher Tatea53146c2010-09-07 11:57:52 -070015738 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015739 shadowSize.x, shadowSize.y,
15740 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015741 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015742
15743 // Off and running! Release our local surface instance; the drag
15744 // shadow surface is now managed by the system process.
15745 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015746 }
15747 } catch (Exception e) {
15748 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15749 surface.destroy();
15750 }
15751
15752 return okay;
15753 }
15754
Christopher Tatea53146c2010-09-07 11:57:52 -070015755 /**
Joe Malin32736f02011-01-19 16:14:20 -080015756 * Handles drag events sent by the system following a call to
15757 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15758 *<p>
15759 * When the system calls this method, it passes a
15760 * {@link android.view.DragEvent} object. A call to
15761 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15762 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15763 * operation.
15764 * @param event The {@link android.view.DragEvent} sent by the system.
15765 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15766 * in DragEvent, indicating the type of drag event represented by this object.
15767 * @return {@code true} if the method was successful, otherwise {@code false}.
15768 * <p>
15769 * The method should return {@code true} in response to an action type of
15770 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15771 * operation.
15772 * </p>
15773 * <p>
15774 * The method should also return {@code true} in response to an action type of
15775 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15776 * {@code false} if it didn't.
15777 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015778 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015779 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015780 return false;
15781 }
15782
15783 /**
Joe Malin32736f02011-01-19 16:14:20 -080015784 * Detects if this View is enabled and has a drag event listener.
15785 * If both are true, then it calls the drag event listener with the
15786 * {@link android.view.DragEvent} it received. If the drag event listener returns
15787 * {@code true}, then dispatchDragEvent() returns {@code true}.
15788 * <p>
15789 * For all other cases, the method calls the
15790 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15791 * method and returns its result.
15792 * </p>
15793 * <p>
15794 * This ensures that a drag event is always consumed, even if the View does not have a drag
15795 * event listener. However, if the View has a listener and the listener returns true, then
15796 * onDragEvent() is not called.
15797 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015798 */
15799 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015800 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015801 ListenerInfo li = mListenerInfo;
15802 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15803 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015804 return true;
15805 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015806 return onDragEvent(event);
15807 }
15808
Christopher Tate3d4bf172011-03-28 16:16:46 -070015809 boolean canAcceptDrag() {
15810 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15811 }
15812
Christopher Tatea53146c2010-09-07 11:57:52 -070015813 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015814 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15815 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015816 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015817 */
15818 public void onCloseSystemDialogs(String reason) {
15819 }
Joe Malin32736f02011-01-19 16:14:20 -080015820
Dianne Hackbornffa42482009-09-23 22:20:11 -070015821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015822 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015823 * update a Region being computed for
15824 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015825 * that any non-transparent parts of the Drawable are removed from the
15826 * given transparent region.
15827 *
15828 * @param dr The Drawable whose transparency is to be applied to the region.
15829 * @param region A Region holding the current transparency information,
15830 * where any parts of the region that are set are considered to be
15831 * transparent. On return, this region will be modified to have the
15832 * transparency information reduced by the corresponding parts of the
15833 * Drawable that are not transparent.
15834 * {@hide}
15835 */
15836 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15837 if (DBG) {
15838 Log.i("View", "Getting transparent region for: " + this);
15839 }
15840 final Region r = dr.getTransparentRegion();
15841 final Rect db = dr.getBounds();
15842 final AttachInfo attachInfo = mAttachInfo;
15843 if (r != null && attachInfo != null) {
15844 final int w = getRight()-getLeft();
15845 final int h = getBottom()-getTop();
15846 if (db.left > 0) {
15847 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15848 r.op(0, 0, db.left, h, Region.Op.UNION);
15849 }
15850 if (db.right < w) {
15851 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15852 r.op(db.right, 0, w, h, Region.Op.UNION);
15853 }
15854 if (db.top > 0) {
15855 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15856 r.op(0, 0, w, db.top, Region.Op.UNION);
15857 }
15858 if (db.bottom < h) {
15859 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15860 r.op(0, db.bottom, w, h, Region.Op.UNION);
15861 }
15862 final int[] location = attachInfo.mTransparentLocation;
15863 getLocationInWindow(location);
15864 r.translate(location[0], location[1]);
15865 region.op(r, Region.Op.INTERSECT);
15866 } else {
15867 region.op(db, Region.Op.DIFFERENCE);
15868 }
15869 }
15870
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015871 private void checkForLongClick(int delayOffset) {
15872 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15873 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015874
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015875 if (mPendingCheckForLongPress == null) {
15876 mPendingCheckForLongPress = new CheckForLongPress();
15877 }
15878 mPendingCheckForLongPress.rememberWindowAttachCount();
15879 postDelayed(mPendingCheckForLongPress,
15880 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015882 }
15883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015884 /**
15885 * Inflate a view from an XML resource. This convenience method wraps the {@link
15886 * LayoutInflater} class, which provides a full range of options for view inflation.
15887 *
15888 * @param context The Context object for your activity or application.
15889 * @param resource The resource ID to inflate
15890 * @param root A view group that will be the parent. Used to properly inflate the
15891 * layout_* parameters.
15892 * @see LayoutInflater
15893 */
15894 public static View inflate(Context context, int resource, ViewGroup root) {
15895 LayoutInflater factory = LayoutInflater.from(context);
15896 return factory.inflate(resource, root);
15897 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015899 /**
Adam Powell637d3372010-08-25 14:37:03 -070015900 * Scroll the view with standard behavior for scrolling beyond the normal
15901 * content boundaries. Views that call this method should override
15902 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15903 * results of an over-scroll operation.
15904 *
15905 * Views can use this method to handle any touch or fling-based scrolling.
15906 *
15907 * @param deltaX Change in X in pixels
15908 * @param deltaY Change in Y in pixels
15909 * @param scrollX Current X scroll value in pixels before applying deltaX
15910 * @param scrollY Current Y scroll value in pixels before applying deltaY
15911 * @param scrollRangeX Maximum content scroll range along the X axis
15912 * @param scrollRangeY Maximum content scroll range along the Y axis
15913 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15914 * along the X axis.
15915 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15916 * along the Y axis.
15917 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15918 * @return true if scrolling was clamped to an over-scroll boundary along either
15919 * axis, false otherwise.
15920 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015921 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015922 protected boolean overScrollBy(int deltaX, int deltaY,
15923 int scrollX, int scrollY,
15924 int scrollRangeX, int scrollRangeY,
15925 int maxOverScrollX, int maxOverScrollY,
15926 boolean isTouchEvent) {
15927 final int overScrollMode = mOverScrollMode;
15928 final boolean canScrollHorizontal =
15929 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15930 final boolean canScrollVertical =
15931 computeVerticalScrollRange() > computeVerticalScrollExtent();
15932 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15933 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15934 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15935 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15936
15937 int newScrollX = scrollX + deltaX;
15938 if (!overScrollHorizontal) {
15939 maxOverScrollX = 0;
15940 }
15941
15942 int newScrollY = scrollY + deltaY;
15943 if (!overScrollVertical) {
15944 maxOverScrollY = 0;
15945 }
15946
15947 // Clamp values if at the limits and record
15948 final int left = -maxOverScrollX;
15949 final int right = maxOverScrollX + scrollRangeX;
15950 final int top = -maxOverScrollY;
15951 final int bottom = maxOverScrollY + scrollRangeY;
15952
15953 boolean clampedX = false;
15954 if (newScrollX > right) {
15955 newScrollX = right;
15956 clampedX = true;
15957 } else if (newScrollX < left) {
15958 newScrollX = left;
15959 clampedX = true;
15960 }
15961
15962 boolean clampedY = false;
15963 if (newScrollY > bottom) {
15964 newScrollY = bottom;
15965 clampedY = true;
15966 } else if (newScrollY < top) {
15967 newScrollY = top;
15968 clampedY = true;
15969 }
15970
15971 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15972
15973 return clampedX || clampedY;
15974 }
15975
15976 /**
15977 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15978 * respond to the results of an over-scroll operation.
15979 *
15980 * @param scrollX New X scroll value in pixels
15981 * @param scrollY New Y scroll value in pixels
15982 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15983 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15984 */
15985 protected void onOverScrolled(int scrollX, int scrollY,
15986 boolean clampedX, boolean clampedY) {
15987 // Intentionally empty.
15988 }
15989
15990 /**
15991 * Returns the over-scroll mode for this view. The result will be
15992 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15993 * (allow over-scrolling only if the view content is larger than the container),
15994 * or {@link #OVER_SCROLL_NEVER}.
15995 *
15996 * @return This view's over-scroll mode.
15997 */
15998 public int getOverScrollMode() {
15999 return mOverScrollMode;
16000 }
16001
16002 /**
16003 * Set the over-scroll mode for this view. Valid over-scroll modes are
16004 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16005 * (allow over-scrolling only if the view content is larger than the container),
16006 * or {@link #OVER_SCROLL_NEVER}.
16007 *
16008 * Setting the over-scroll mode of a view will have an effect only if the
16009 * view is capable of scrolling.
16010 *
16011 * @param overScrollMode The new over-scroll mode for this view.
16012 */
16013 public void setOverScrollMode(int overScrollMode) {
16014 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16015 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16016 overScrollMode != OVER_SCROLL_NEVER) {
16017 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16018 }
16019 mOverScrollMode = overScrollMode;
16020 }
16021
16022 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016023 * Gets a scale factor that determines the distance the view should scroll
16024 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16025 * @return The vertical scroll scale factor.
16026 * @hide
16027 */
16028 protected float getVerticalScrollFactor() {
16029 if (mVerticalScrollFactor == 0) {
16030 TypedValue outValue = new TypedValue();
16031 if (!mContext.getTheme().resolveAttribute(
16032 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16033 throw new IllegalStateException(
16034 "Expected theme to define listPreferredItemHeight.");
16035 }
16036 mVerticalScrollFactor = outValue.getDimension(
16037 mContext.getResources().getDisplayMetrics());
16038 }
16039 return mVerticalScrollFactor;
16040 }
16041
16042 /**
16043 * Gets a scale factor that determines the distance the view should scroll
16044 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16045 * @return The horizontal scroll scale factor.
16046 * @hide
16047 */
16048 protected float getHorizontalScrollFactor() {
16049 // TODO: Should use something else.
16050 return getVerticalScrollFactor();
16051 }
16052
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016053 /**
16054 * Return the value specifying the text direction or policy that was set with
16055 * {@link #setTextDirection(int)}.
16056 *
16057 * @return the defined text direction. It can be one of:
16058 *
16059 * {@link #TEXT_DIRECTION_INHERIT},
16060 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16061 * {@link #TEXT_DIRECTION_ANY_RTL},
16062 * {@link #TEXT_DIRECTION_LTR},
16063 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016064 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016065 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016066 @ViewDebug.ExportedProperty(category = "text", mapping = {
16067 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16068 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16069 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16070 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16071 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16072 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16073 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016074 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016075 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016076 }
16077
16078 /**
16079 * Set the text direction.
16080 *
16081 * @param textDirection the direction to set. Should be one of:
16082 *
16083 * {@link #TEXT_DIRECTION_INHERIT},
16084 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16085 * {@link #TEXT_DIRECTION_ANY_RTL},
16086 * {@link #TEXT_DIRECTION_LTR},
16087 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016088 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016089 */
16090 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016091 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016092 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016093 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016094 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016095 // Set the new text direction
16096 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016097 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016098 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016099 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016100 }
16101 }
16102
16103 /**
16104 * Return the resolved text direction.
16105 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016106 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16107 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16108 * up the parent chain of the view. if there is no parent, then it will return the default
16109 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16110 *
16111 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016112 *
Doug Feltcb3791202011-07-07 11:57:48 -070016113 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16114 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016115 * {@link #TEXT_DIRECTION_LTR},
16116 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016117 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016118 */
16119 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016120 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016121 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016122 resolveTextDirection();
16123 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016124 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016125 }
16126
16127 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016128 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16129 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016130 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016131 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016132 // Reset any previous text direction resolution
16133 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16134
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016135 if (hasRtlSupport()) {
16136 // Set resolved text direction flag depending on text direction flag
16137 final int textDirection = getTextDirection();
16138 switch(textDirection) {
16139 case TEXT_DIRECTION_INHERIT:
16140 if (canResolveTextDirection()) {
16141 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016142
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016143 // Set current resolved direction to the same value as the parent's one
16144 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16145 switch (parentResolvedDirection) {
16146 case TEXT_DIRECTION_FIRST_STRONG:
16147 case TEXT_DIRECTION_ANY_RTL:
16148 case TEXT_DIRECTION_LTR:
16149 case TEXT_DIRECTION_RTL:
16150 case TEXT_DIRECTION_LOCALE:
16151 mPrivateFlags2 |=
16152 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16153 break;
16154 default:
16155 // Default resolved direction is "first strong" heuristic
16156 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16157 }
16158 } else {
16159 // We cannot do the resolution if there is no parent, so use the default one
16160 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016161 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016162 break;
16163 case TEXT_DIRECTION_FIRST_STRONG:
16164 case TEXT_DIRECTION_ANY_RTL:
16165 case TEXT_DIRECTION_LTR:
16166 case TEXT_DIRECTION_RTL:
16167 case TEXT_DIRECTION_LOCALE:
16168 // Resolved direction is the same as text direction
16169 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16170 break;
16171 default:
16172 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016173 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016174 }
16175 } else {
16176 // Default resolved direction is "first strong" heuristic
16177 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016178 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016179
16180 // Set to resolved
16181 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016182 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016183 }
16184
16185 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016186 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016187 * resolution should override this method.
16188 *
16189 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016190 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016191 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016192 }
16193
16194 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016195 * Check if text direction resolution can be done.
16196 *
16197 * @return true if text direction resolution can be done otherwise return false.
16198 */
16199 public boolean canResolveTextDirection() {
16200 switch (getTextDirection()) {
16201 case TEXT_DIRECTION_INHERIT:
16202 return (mParent != null) && (mParent instanceof ViewGroup);
16203 default:
16204 return true;
16205 }
16206 }
16207
16208 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016209 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016210 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016211 * reset is done.
16212 */
16213 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016214 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016215 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016216 }
16217
16218 /**
16219 * Called when text direction is reset. Subclasses that care about text direction reset should
16220 * override this method and do a reset of the text direction of their children. The default
16221 * implementation does nothing.
16222 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016223 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016224 }
16225
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016226 /**
16227 * Return the value specifying the text alignment or policy that was set with
16228 * {@link #setTextAlignment(int)}.
16229 *
16230 * @return the defined text alignment. It can be one of:
16231 *
16232 * {@link #TEXT_ALIGNMENT_INHERIT},
16233 * {@link #TEXT_ALIGNMENT_GRAVITY},
16234 * {@link #TEXT_ALIGNMENT_CENTER},
16235 * {@link #TEXT_ALIGNMENT_TEXT_START},
16236 * {@link #TEXT_ALIGNMENT_TEXT_END},
16237 * {@link #TEXT_ALIGNMENT_VIEW_START},
16238 * {@link #TEXT_ALIGNMENT_VIEW_END}
16239 */
16240 @ViewDebug.ExportedProperty(category = "text", mapping = {
16241 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16242 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16243 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16244 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16245 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16246 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16247 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16248 })
16249 public int getTextAlignment() {
16250 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16251 }
16252
16253 /**
16254 * Set the text alignment.
16255 *
16256 * @param textAlignment The text alignment to set. Should be one of
16257 *
16258 * {@link #TEXT_ALIGNMENT_INHERIT},
16259 * {@link #TEXT_ALIGNMENT_GRAVITY},
16260 * {@link #TEXT_ALIGNMENT_CENTER},
16261 * {@link #TEXT_ALIGNMENT_TEXT_START},
16262 * {@link #TEXT_ALIGNMENT_TEXT_END},
16263 * {@link #TEXT_ALIGNMENT_VIEW_START},
16264 * {@link #TEXT_ALIGNMENT_VIEW_END}
16265 *
16266 * @attr ref android.R.styleable#View_textAlignment
16267 */
16268 public void setTextAlignment(int textAlignment) {
16269 if (textAlignment != getTextAlignment()) {
16270 // Reset the current and resolved text alignment
16271 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16272 resetResolvedTextAlignment();
16273 // Set the new text alignment
16274 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16275 // Refresh
16276 requestLayout();
16277 invalidate(true);
16278 }
16279 }
16280
16281 /**
16282 * Return the resolved text alignment.
16283 *
16284 * The resolved text alignment. This needs resolution if the value is
16285 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16286 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16287 *
16288 * @return the resolved text alignment. Returns one of:
16289 *
16290 * {@link #TEXT_ALIGNMENT_GRAVITY},
16291 * {@link #TEXT_ALIGNMENT_CENTER},
16292 * {@link #TEXT_ALIGNMENT_TEXT_START},
16293 * {@link #TEXT_ALIGNMENT_TEXT_END},
16294 * {@link #TEXT_ALIGNMENT_VIEW_START},
16295 * {@link #TEXT_ALIGNMENT_VIEW_END}
16296 */
16297 @ViewDebug.ExportedProperty(category = "text", mapping = {
16298 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16299 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16300 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16301 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16302 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16303 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16304 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16305 })
16306 public int getResolvedTextAlignment() {
16307 // If text alignment is not resolved, then resolve it
16308 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16309 resolveTextAlignment();
16310 }
16311 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16312 }
16313
16314 /**
16315 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16316 * resolution is done.
16317 */
16318 public void resolveTextAlignment() {
16319 // Reset any previous text alignment resolution
16320 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16321
16322 if (hasRtlSupport()) {
16323 // Set resolved text alignment flag depending on text alignment flag
16324 final int textAlignment = getTextAlignment();
16325 switch (textAlignment) {
16326 case TEXT_ALIGNMENT_INHERIT:
16327 // Check if we can resolve the text alignment
16328 if (canResolveLayoutDirection() && mParent instanceof View) {
16329 View view = (View) mParent;
16330
16331 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16332 switch (parentResolvedTextAlignment) {
16333 case TEXT_ALIGNMENT_GRAVITY:
16334 case TEXT_ALIGNMENT_TEXT_START:
16335 case TEXT_ALIGNMENT_TEXT_END:
16336 case TEXT_ALIGNMENT_CENTER:
16337 case TEXT_ALIGNMENT_VIEW_START:
16338 case TEXT_ALIGNMENT_VIEW_END:
16339 // Resolved text alignment is the same as the parent resolved
16340 // text alignment
16341 mPrivateFlags2 |=
16342 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16343 break;
16344 default:
16345 // Use default resolved text alignment
16346 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16347 }
16348 }
16349 else {
16350 // We cannot do the resolution if there is no parent so use the default
16351 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16352 }
16353 break;
16354 case TEXT_ALIGNMENT_GRAVITY:
16355 case TEXT_ALIGNMENT_TEXT_START:
16356 case TEXT_ALIGNMENT_TEXT_END:
16357 case TEXT_ALIGNMENT_CENTER:
16358 case TEXT_ALIGNMENT_VIEW_START:
16359 case TEXT_ALIGNMENT_VIEW_END:
16360 // Resolved text alignment is the same as text alignment
16361 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16362 break;
16363 default:
16364 // Use default resolved text alignment
16365 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16366 }
16367 } else {
16368 // Use default resolved text alignment
16369 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16370 }
16371
16372 // Set the resolved
16373 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16374 onResolvedTextAlignmentChanged();
16375 }
16376
16377 /**
16378 * Check if text alignment resolution can be done.
16379 *
16380 * @return true if text alignment resolution can be done otherwise return false.
16381 */
16382 public boolean canResolveTextAlignment() {
16383 switch (getTextAlignment()) {
16384 case TEXT_DIRECTION_INHERIT:
16385 return (mParent != null);
16386 default:
16387 return true;
16388 }
16389 }
16390
16391 /**
16392 * Called when text alignment has been resolved. Subclasses that care about text alignment
16393 * resolution should override this method.
16394 *
16395 * The default implementation does nothing.
16396 */
16397 public void onResolvedTextAlignmentChanged() {
16398 }
16399
16400 /**
16401 * Reset resolved text alignment. Text alignment can be resolved with a call to
16402 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16403 * reset is done.
16404 */
16405 public void resetResolvedTextAlignment() {
16406 // Reset any previous text alignment resolution
16407 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16408 onResolvedTextAlignmentReset();
16409 }
16410
16411 /**
16412 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16413 * override this method and do a reset of the text alignment of their children. The default
16414 * implementation does nothing.
16415 */
16416 public void onResolvedTextAlignmentReset() {
16417 }
16418
Adam Powella9108a22012-07-18 11:18:09 -070016419 /**
16420 * Generate a value suitable for use in {@link #setId(int)}.
16421 * This value will not collide with ID values generated at build time by aapt for R.id.
16422 *
16423 * @return a generated ID value
16424 */
16425 public static int generateViewId() {
16426 for (;;) {
16427 final int result = sNextGeneratedId.get();
16428 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16429 int newValue = result + 1;
16430 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16431 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16432 return result;
16433 }
16434 }
16435 }
16436
Chet Haaseb39f0512011-05-24 14:36:40 -070016437 //
16438 // Properties
16439 //
16440 /**
16441 * A Property wrapper around the <code>alpha</code> functionality handled by the
16442 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16443 */
Chet Haased47f1532011-12-16 11:18:52 -080016444 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016445 @Override
16446 public void setValue(View object, float value) {
16447 object.setAlpha(value);
16448 }
16449
16450 @Override
16451 public Float get(View object) {
16452 return object.getAlpha();
16453 }
16454 };
16455
16456 /**
16457 * A Property wrapper around the <code>translationX</code> functionality handled by the
16458 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16459 */
Chet Haased47f1532011-12-16 11:18:52 -080016460 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016461 @Override
16462 public void setValue(View object, float value) {
16463 object.setTranslationX(value);
16464 }
16465
16466 @Override
16467 public Float get(View object) {
16468 return object.getTranslationX();
16469 }
16470 };
16471
16472 /**
16473 * A Property wrapper around the <code>translationY</code> functionality handled by the
16474 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16475 */
Chet Haased47f1532011-12-16 11:18:52 -080016476 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016477 @Override
16478 public void setValue(View object, float value) {
16479 object.setTranslationY(value);
16480 }
16481
16482 @Override
16483 public Float get(View object) {
16484 return object.getTranslationY();
16485 }
16486 };
16487
16488 /**
16489 * A Property wrapper around the <code>x</code> functionality handled by the
16490 * {@link View#setX(float)} and {@link View#getX()} methods.
16491 */
Chet Haased47f1532011-12-16 11:18:52 -080016492 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016493 @Override
16494 public void setValue(View object, float value) {
16495 object.setX(value);
16496 }
16497
16498 @Override
16499 public Float get(View object) {
16500 return object.getX();
16501 }
16502 };
16503
16504 /**
16505 * A Property wrapper around the <code>y</code> functionality handled by the
16506 * {@link View#setY(float)} and {@link View#getY()} methods.
16507 */
Chet Haased47f1532011-12-16 11:18:52 -080016508 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016509 @Override
16510 public void setValue(View object, float value) {
16511 object.setY(value);
16512 }
16513
16514 @Override
16515 public Float get(View object) {
16516 return object.getY();
16517 }
16518 };
16519
16520 /**
16521 * A Property wrapper around the <code>rotation</code> functionality handled by the
16522 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16523 */
Chet Haased47f1532011-12-16 11:18:52 -080016524 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016525 @Override
16526 public void setValue(View object, float value) {
16527 object.setRotation(value);
16528 }
16529
16530 @Override
16531 public Float get(View object) {
16532 return object.getRotation();
16533 }
16534 };
16535
16536 /**
16537 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16538 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16539 */
Chet Haased47f1532011-12-16 11:18:52 -080016540 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016541 @Override
16542 public void setValue(View object, float value) {
16543 object.setRotationX(value);
16544 }
16545
16546 @Override
16547 public Float get(View object) {
16548 return object.getRotationX();
16549 }
16550 };
16551
16552 /**
16553 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16554 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16555 */
Chet Haased47f1532011-12-16 11:18:52 -080016556 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016557 @Override
16558 public void setValue(View object, float value) {
16559 object.setRotationY(value);
16560 }
16561
16562 @Override
16563 public Float get(View object) {
16564 return object.getRotationY();
16565 }
16566 };
16567
16568 /**
16569 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16570 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16571 */
Chet Haased47f1532011-12-16 11:18:52 -080016572 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016573 @Override
16574 public void setValue(View object, float value) {
16575 object.setScaleX(value);
16576 }
16577
16578 @Override
16579 public Float get(View object) {
16580 return object.getScaleX();
16581 }
16582 };
16583
16584 /**
16585 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16586 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16587 */
Chet Haased47f1532011-12-16 11:18:52 -080016588 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016589 @Override
16590 public void setValue(View object, float value) {
16591 object.setScaleY(value);
16592 }
16593
16594 @Override
16595 public Float get(View object) {
16596 return object.getScaleY();
16597 }
16598 };
16599
Jeff Brown33bbfd22011-02-24 20:55:35 -080016600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016601 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16602 * Each MeasureSpec represents a requirement for either the width or the height.
16603 * A MeasureSpec is comprised of a size and a mode. There are three possible
16604 * modes:
16605 * <dl>
16606 * <dt>UNSPECIFIED</dt>
16607 * <dd>
16608 * The parent has not imposed any constraint on the child. It can be whatever size
16609 * it wants.
16610 * </dd>
16611 *
16612 * <dt>EXACTLY</dt>
16613 * <dd>
16614 * The parent has determined an exact size for the child. The child is going to be
16615 * given those bounds regardless of how big it wants to be.
16616 * </dd>
16617 *
16618 * <dt>AT_MOST</dt>
16619 * <dd>
16620 * The child can be as large as it wants up to the specified size.
16621 * </dd>
16622 * </dl>
16623 *
16624 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16625 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16626 */
16627 public static class MeasureSpec {
16628 private static final int MODE_SHIFT = 30;
16629 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16630
16631 /**
16632 * Measure specification mode: The parent has not imposed any constraint
16633 * on the child. It can be whatever size it wants.
16634 */
16635 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16636
16637 /**
16638 * Measure specification mode: The parent has determined an exact size
16639 * for the child. The child is going to be given those bounds regardless
16640 * of how big it wants to be.
16641 */
16642 public static final int EXACTLY = 1 << MODE_SHIFT;
16643
16644 /**
16645 * Measure specification mode: The child can be as large as it wants up
16646 * to the specified size.
16647 */
16648 public static final int AT_MOST = 2 << MODE_SHIFT;
16649
16650 /**
16651 * Creates a measure specification based on the supplied size and mode.
16652 *
16653 * The mode must always be one of the following:
16654 * <ul>
16655 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16656 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16657 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16658 * </ul>
16659 *
16660 * @param size the size of the measure specification
16661 * @param mode the mode of the measure specification
16662 * @return the measure specification based on size and mode
16663 */
16664 public static int makeMeasureSpec(int size, int mode) {
16665 return size + mode;
16666 }
16667
16668 /**
16669 * Extracts the mode from the supplied measure specification.
16670 *
16671 * @param measureSpec the measure specification to extract the mode from
16672 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16673 * {@link android.view.View.MeasureSpec#AT_MOST} or
16674 * {@link android.view.View.MeasureSpec#EXACTLY}
16675 */
16676 public static int getMode(int measureSpec) {
16677 return (measureSpec & MODE_MASK);
16678 }
16679
16680 /**
16681 * Extracts the size from the supplied measure specification.
16682 *
16683 * @param measureSpec the measure specification to extract the size from
16684 * @return the size in pixels defined in the supplied measure specification
16685 */
16686 public static int getSize(int measureSpec) {
16687 return (measureSpec & ~MODE_MASK);
16688 }
16689
16690 /**
16691 * Returns a String representation of the specified measure
16692 * specification.
16693 *
16694 * @param measureSpec the measure specification to convert to a String
16695 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16696 */
16697 public static String toString(int measureSpec) {
16698 int mode = getMode(measureSpec);
16699 int size = getSize(measureSpec);
16700
16701 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16702
16703 if (mode == UNSPECIFIED)
16704 sb.append("UNSPECIFIED ");
16705 else if (mode == EXACTLY)
16706 sb.append("EXACTLY ");
16707 else if (mode == AT_MOST)
16708 sb.append("AT_MOST ");
16709 else
16710 sb.append(mode).append(" ");
16711
16712 sb.append(size);
16713 return sb.toString();
16714 }
16715 }
16716
16717 class CheckForLongPress implements Runnable {
16718
16719 private int mOriginalWindowAttachCount;
16720
16721 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016722 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016723 && mOriginalWindowAttachCount == mWindowAttachCount) {
16724 if (performLongClick()) {
16725 mHasPerformedLongPress = true;
16726 }
16727 }
16728 }
16729
16730 public void rememberWindowAttachCount() {
16731 mOriginalWindowAttachCount = mWindowAttachCount;
16732 }
16733 }
Joe Malin32736f02011-01-19 16:14:20 -080016734
Adam Powelle14579b2009-12-16 18:39:52 -080016735 private final class CheckForTap implements Runnable {
16736 public void run() {
16737 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016738 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016739 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016740 }
16741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016742
Adam Powella35d7682010-03-12 14:48:13 -080016743 private final class PerformClick implements Runnable {
16744 public void run() {
16745 performClick();
16746 }
16747 }
16748
Dianne Hackborn63042d62011-01-26 18:56:29 -080016749 /** @hide */
16750 public void hackTurnOffWindowResizeAnim(boolean off) {
16751 mAttachInfo.mTurnOffWindowResizeAnim = off;
16752 }
Joe Malin32736f02011-01-19 16:14:20 -080016753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016754 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016755 * This method returns a ViewPropertyAnimator object, which can be used to animate
16756 * specific properties on this View.
16757 *
16758 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16759 */
16760 public ViewPropertyAnimator animate() {
16761 if (mAnimator == null) {
16762 mAnimator = new ViewPropertyAnimator(this);
16763 }
16764 return mAnimator;
16765 }
16766
16767 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016768 * Interface definition for a callback to be invoked when a hardware key event is
16769 * dispatched to this view. The callback will be invoked before the key event is
16770 * given to the view. This is only useful for hardware keyboards; a software input
16771 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016772 */
16773 public interface OnKeyListener {
16774 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016775 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016776 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016777 * <p>Key presses in software keyboards will generally NOT trigger this method,
16778 * although some may elect to do so in some situations. Do not assume a
16779 * software input method has to be key-based; even if it is, it may use key presses
16780 * in a different way than you expect, so there is no way to reliably catch soft
16781 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016782 *
16783 * @param v The view the key has been dispatched to.
16784 * @param keyCode The code for the physical key that was pressed
16785 * @param event The KeyEvent object containing full information about
16786 * the event.
16787 * @return True if the listener has consumed the event, false otherwise.
16788 */
16789 boolean onKey(View v, int keyCode, KeyEvent event);
16790 }
16791
16792 /**
16793 * Interface definition for a callback to be invoked when a touch event is
16794 * dispatched to this view. The callback will be invoked before the touch
16795 * event is given to the view.
16796 */
16797 public interface OnTouchListener {
16798 /**
16799 * Called when a touch event is dispatched to a view. This allows listeners to
16800 * get a chance to respond before the target view.
16801 *
16802 * @param v The view the touch event has been dispatched to.
16803 * @param event The MotionEvent object containing full information about
16804 * the event.
16805 * @return True if the listener has consumed the event, false otherwise.
16806 */
16807 boolean onTouch(View v, MotionEvent event);
16808 }
16809
16810 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016811 * Interface definition for a callback to be invoked when a hover event is
16812 * dispatched to this view. The callback will be invoked before the hover
16813 * event is given to the view.
16814 */
16815 public interface OnHoverListener {
16816 /**
16817 * Called when a hover event is dispatched to a view. This allows listeners to
16818 * get a chance to respond before the target view.
16819 *
16820 * @param v The view the hover event has been dispatched to.
16821 * @param event The MotionEvent object containing full information about
16822 * the event.
16823 * @return True if the listener has consumed the event, false otherwise.
16824 */
16825 boolean onHover(View v, MotionEvent event);
16826 }
16827
16828 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016829 * Interface definition for a callback to be invoked when a generic motion event is
16830 * dispatched to this view. The callback will be invoked before the generic motion
16831 * event is given to the view.
16832 */
16833 public interface OnGenericMotionListener {
16834 /**
16835 * Called when a generic motion event is dispatched to a view. This allows listeners to
16836 * get a chance to respond before the target view.
16837 *
16838 * @param v The view the generic motion event has been dispatched to.
16839 * @param event The MotionEvent object containing full information about
16840 * the event.
16841 * @return True if the listener has consumed the event, false otherwise.
16842 */
16843 boolean onGenericMotion(View v, MotionEvent event);
16844 }
16845
16846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016847 * Interface definition for a callback to be invoked when a view has been clicked and held.
16848 */
16849 public interface OnLongClickListener {
16850 /**
16851 * Called when a view has been clicked and held.
16852 *
16853 * @param v The view that was clicked and held.
16854 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016855 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016856 */
16857 boolean onLongClick(View v);
16858 }
16859
16860 /**
Chris Tate32affef2010-10-18 15:29:21 -070016861 * Interface definition for a callback to be invoked when a drag is being dispatched
16862 * to this view. The callback will be invoked before the hosting view's own
16863 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16864 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016865 *
16866 * <div class="special reference">
16867 * <h3>Developer Guides</h3>
16868 * <p>For a guide to implementing drag and drop features, read the
16869 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16870 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016871 */
16872 public interface OnDragListener {
16873 /**
16874 * Called when a drag event is dispatched to a view. This allows listeners
16875 * to get a chance to override base View behavior.
16876 *
Joe Malin32736f02011-01-19 16:14:20 -080016877 * @param v The View that received the drag event.
16878 * @param event The {@link android.view.DragEvent} object for the drag event.
16879 * @return {@code true} if the drag event was handled successfully, or {@code false}
16880 * if the drag event was not handled. Note that {@code false} will trigger the View
16881 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016882 */
16883 boolean onDrag(View v, DragEvent event);
16884 }
16885
16886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016887 * Interface definition for a callback to be invoked when the focus state of
16888 * a view changed.
16889 */
16890 public interface OnFocusChangeListener {
16891 /**
16892 * Called when the focus state of a view has changed.
16893 *
16894 * @param v The view whose state has changed.
16895 * @param hasFocus The new focus state of v.
16896 */
16897 void onFocusChange(View v, boolean hasFocus);
16898 }
16899
16900 /**
16901 * Interface definition for a callback to be invoked when a view is clicked.
16902 */
16903 public interface OnClickListener {
16904 /**
16905 * Called when a view has been clicked.
16906 *
16907 * @param v The view that was clicked.
16908 */
16909 void onClick(View v);
16910 }
16911
16912 /**
16913 * Interface definition for a callback to be invoked when the context menu
16914 * for this view is being built.
16915 */
16916 public interface OnCreateContextMenuListener {
16917 /**
16918 * Called when the context menu for this view is being built. It is not
16919 * safe to hold onto the menu after this method returns.
16920 *
16921 * @param menu The context menu that is being built
16922 * @param v The view for which the context menu is being built
16923 * @param menuInfo Extra information about the item for which the
16924 * context menu should be shown. This information will vary
16925 * depending on the class of v.
16926 */
16927 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16928 }
16929
Joe Onorato664644d2011-01-23 17:53:23 -080016930 /**
16931 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016932 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016933 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016934 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016935 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016936 */
16937 public interface OnSystemUiVisibilityChangeListener {
16938 /**
16939 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016940 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016941 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016942 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16943 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16944 * This tells you the <strong>global</strong> state of these UI visibility
16945 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016946 */
16947 public void onSystemUiVisibilityChange(int visibility);
16948 }
16949
Adam Powell4afd62b2011-02-18 15:02:18 -080016950 /**
16951 * Interface definition for a callback to be invoked when this view is attached
16952 * or detached from its window.
16953 */
16954 public interface OnAttachStateChangeListener {
16955 /**
16956 * Called when the view is attached to a window.
16957 * @param v The view that was attached
16958 */
16959 public void onViewAttachedToWindow(View v);
16960 /**
16961 * Called when the view is detached from a window.
16962 * @param v The view that was detached
16963 */
16964 public void onViewDetachedFromWindow(View v);
16965 }
16966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016967 private final class UnsetPressedState implements Runnable {
16968 public void run() {
16969 setPressed(false);
16970 }
16971 }
16972
16973 /**
16974 * Base class for derived classes that want to save and restore their own
16975 * state in {@link android.view.View#onSaveInstanceState()}.
16976 */
16977 public static class BaseSavedState extends AbsSavedState {
16978 /**
16979 * Constructor used when reading from a parcel. Reads the state of the superclass.
16980 *
16981 * @param source
16982 */
16983 public BaseSavedState(Parcel source) {
16984 super(source);
16985 }
16986
16987 /**
16988 * Constructor called by derived classes when creating their SavedState objects
16989 *
16990 * @param superState The state of the superclass of this view
16991 */
16992 public BaseSavedState(Parcelable superState) {
16993 super(superState);
16994 }
16995
16996 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16997 new Parcelable.Creator<BaseSavedState>() {
16998 public BaseSavedState createFromParcel(Parcel in) {
16999 return new BaseSavedState(in);
17000 }
17001
17002 public BaseSavedState[] newArray(int size) {
17003 return new BaseSavedState[size];
17004 }
17005 };
17006 }
17007
17008 /**
17009 * A set of information given to a view when it is attached to its parent
17010 * window.
17011 */
17012 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017013 interface Callbacks {
17014 void playSoundEffect(int effectId);
17015 boolean performHapticFeedback(int effectId, boolean always);
17016 }
17017
17018 /**
17019 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17020 * to a Handler. This class contains the target (View) to invalidate and
17021 * the coordinates of the dirty rectangle.
17022 *
17023 * For performance purposes, this class also implements a pool of up to
17024 * POOL_LIMIT objects that get reused. This reduces memory allocations
17025 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017026 */
Romain Guyd928d682009-03-31 17:52:16 -070017027 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017028 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017029 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17030 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017031 public InvalidateInfo newInstance() {
17032 return new InvalidateInfo();
17033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017034
Romain Guyd928d682009-03-31 17:52:16 -070017035 public void onAcquired(InvalidateInfo element) {
17036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017037
Romain Guyd928d682009-03-31 17:52:16 -070017038 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017039 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017040 }
17041 }, POOL_LIMIT)
17042 );
17043
17044 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017045 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017046
17047 View target;
17048
17049 int left;
17050 int top;
17051 int right;
17052 int bottom;
17053
Romain Guyd928d682009-03-31 17:52:16 -070017054 public void setNextPoolable(InvalidateInfo element) {
17055 mNext = element;
17056 }
17057
17058 public InvalidateInfo getNextPoolable() {
17059 return mNext;
17060 }
17061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017062 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017063 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064 }
17065
17066 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017067 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017068 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017069
17070 public boolean isPooled() {
17071 return mIsPooled;
17072 }
17073
17074 public void setPooled(boolean isPooled) {
17075 mIsPooled = isPooled;
17076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017077 }
17078
17079 final IWindowSession mSession;
17080
17081 final IWindow mWindow;
17082
17083 final IBinder mWindowToken;
17084
17085 final Callbacks mRootCallbacks;
17086
Romain Guy59a12ca2011-06-09 17:48:21 -070017087 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017089 /**
17090 * The top view of the hierarchy.
17091 */
17092 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017094 IBinder mPanelParentWindowToken;
17095 Surface mSurface;
17096
Romain Guyb051e892010-09-28 19:09:36 -070017097 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017098 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017099 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017100
Romain Guy7e4e5612012-03-05 14:37:29 -080017101 boolean mScreenOn;
17102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017103 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017104 * Scale factor used by the compatibility mode
17105 */
17106 float mApplicationScale;
17107
17108 /**
17109 * Indicates whether the application is in compatibility mode
17110 */
17111 boolean mScalingRequired;
17112
17113 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017114 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017115 */
17116 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017117
Dianne Hackborn63042d62011-01-26 18:56:29 -080017118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017119 * Left position of this view's window
17120 */
17121 int mWindowLeft;
17122
17123 /**
17124 * Top position of this view's window
17125 */
17126 int mWindowTop;
17127
17128 /**
Adam Powell26153a32010-11-08 15:22:27 -080017129 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017130 */
Adam Powell26153a32010-11-08 15:22:27 -080017131 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017132
17133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017134 * For windows that are full-screen but using insets to layout inside
17135 * of the screen decorations, these are the current insets for the
17136 * content of the window.
17137 */
17138 final Rect mContentInsets = new Rect();
17139
17140 /**
17141 * For windows that are full-screen but using insets to layout inside
17142 * of the screen decorations, these are the current insets for the
17143 * actual visible parts of the window.
17144 */
17145 final Rect mVisibleInsets = new Rect();
17146
17147 /**
17148 * The internal insets given by this window. This value is
17149 * supplied by the client (through
17150 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17151 * be given to the window manager when changed to be used in laying
17152 * out windows behind it.
17153 */
17154 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17155 = new ViewTreeObserver.InternalInsetsInfo();
17156
17157 /**
17158 * All views in the window's hierarchy that serve as scroll containers,
17159 * used to determine if the window can be resized or must be panned
17160 * to adjust for a soft input area.
17161 */
17162 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17163
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017164 final KeyEvent.DispatcherState mKeyDispatchState
17165 = new KeyEvent.DispatcherState();
17166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017167 /**
17168 * Indicates whether the view's window currently has the focus.
17169 */
17170 boolean mHasWindowFocus;
17171
17172 /**
17173 * The current visibility of the window.
17174 */
17175 int mWindowVisibility;
17176
17177 /**
17178 * Indicates the time at which drawing started to occur.
17179 */
17180 long mDrawingTime;
17181
17182 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017183 * Indicates whether or not ignoring the DIRTY_MASK flags.
17184 */
17185 boolean mIgnoreDirtyState;
17186
17187 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017188 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17189 * to avoid clearing that flag prematurely.
17190 */
17191 boolean mSetIgnoreDirtyState = false;
17192
17193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017194 * Indicates whether the view's window is currently in touch mode.
17195 */
17196 boolean mInTouchMode;
17197
17198 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017199 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017200 * the next time it performs a traversal
17201 */
17202 boolean mRecomputeGlobalAttributes;
17203
17204 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017205 * Always report new attributes at next traversal.
17206 */
17207 boolean mForceReportNewAttributes;
17208
17209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017210 * Set during a traveral if any views want to keep the screen on.
17211 */
17212 boolean mKeepScreenOn;
17213
17214 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017215 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17216 */
17217 int mSystemUiVisibility;
17218
17219 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017220 * Hack to force certain system UI visibility flags to be cleared.
17221 */
17222 int mDisabledSystemUiVisibility;
17223
17224 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017225 * Last global system UI visibility reported by the window manager.
17226 */
17227 int mGlobalSystemUiVisibility;
17228
17229 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017230 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17231 * attached.
17232 */
17233 boolean mHasSystemUiListeners;
17234
17235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017236 * Set if the visibility of any views has changed.
17237 */
17238 boolean mViewVisibilityChanged;
17239
17240 /**
17241 * Set to true if a view has been scrolled.
17242 */
17243 boolean mViewScrollChanged;
17244
17245 /**
17246 * Global to the view hierarchy used as a temporary for dealing with
17247 * x/y points in the transparent region computations.
17248 */
17249 final int[] mTransparentLocation = new int[2];
17250
17251 /**
17252 * Global to the view hierarchy used as a temporary for dealing with
17253 * x/y points in the ViewGroup.invalidateChild implementation.
17254 */
17255 final int[] mInvalidateChildLocation = new int[2];
17256
Chet Haasec3aa3612010-06-17 08:50:37 -070017257
17258 /**
17259 * Global to the view hierarchy used as a temporary for dealing with
17260 * x/y location when view is transformed.
17261 */
17262 final float[] mTmpTransformLocation = new float[2];
17263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017264 /**
17265 * The view tree observer used to dispatch global events like
17266 * layout, pre-draw, touch mode change, etc.
17267 */
17268 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17269
17270 /**
17271 * A Canvas used by the view hierarchy to perform bitmap caching.
17272 */
17273 Canvas mCanvas;
17274
17275 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017276 * The view root impl.
17277 */
17278 final ViewRootImpl mViewRootImpl;
17279
17280 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017281 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017282 * handler can be used to pump events in the UI events queue.
17283 */
17284 final Handler mHandler;
17285
17286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017287 * Temporary for use in computing invalidate rectangles while
17288 * calling up the hierarchy.
17289 */
17290 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017291
17292 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017293 * Temporary for use in computing hit areas with transformed views
17294 */
17295 final RectF mTmpTransformRect = new RectF();
17296
17297 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017298 * Temporary list for use in collecting focusable descendents of a view.
17299 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017300 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017302 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017303 * The id of the window for accessibility purposes.
17304 */
17305 int mAccessibilityWindowId = View.NO_ID;
17306
17307 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017308 * Whether to ingore not exposed for accessibility Views when
17309 * reporting the view tree to accessibility services.
17310 */
17311 boolean mIncludeNotImportantViews;
17312
17313 /**
17314 * The drawable for highlighting accessibility focus.
17315 */
17316 Drawable mAccessibilityFocusDrawable;
17317
17318 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017319 * Show where the margins, bounds and layout bounds are for each view.
17320 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017321 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017322
17323 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017324 * Point used to compute visible regions.
17325 */
17326 final Point mPoint = new Point();
17327
17328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017329 * Creates a new set of attachment information with the specified
17330 * events handler and thread.
17331 *
17332 * @param handler the events handler the view must use
17333 */
17334 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017335 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017336 mSession = session;
17337 mWindow = window;
17338 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017339 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017340 mHandler = handler;
17341 mRootCallbacks = effectPlayer;
17342 }
17343 }
17344
17345 /**
17346 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17347 * is supported. This avoids keeping too many unused fields in most
17348 * instances of View.</p>
17349 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017350 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017351
Mike Cleronf116bf82009-09-27 19:14:12 -070017352 /**
17353 * Scrollbars are not visible
17354 */
17355 public static final int OFF = 0;
17356
17357 /**
17358 * Scrollbars are visible
17359 */
17360 public static final int ON = 1;
17361
17362 /**
17363 * Scrollbars are fading away
17364 */
17365 public static final int FADING = 2;
17366
17367 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017369 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017370 public int scrollBarDefaultDelayBeforeFade;
17371 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017372
17373 public int scrollBarSize;
17374 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017375 public float[] interpolatorValues;
17376 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017377
17378 public final Paint paint;
17379 public final Matrix matrix;
17380 public Shader shader;
17381
Mike Cleronf116bf82009-09-27 19:14:12 -070017382 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17383
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017384 private static final float[] OPAQUE = { 255 };
17385 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017386
Mike Cleronf116bf82009-09-27 19:14:12 -070017387 /**
17388 * When fading should start. This time moves into the future every time
17389 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17390 */
17391 public long fadeStartTime;
17392
17393
17394 /**
17395 * The current state of the scrollbars: ON, OFF, or FADING
17396 */
17397 public int state = OFF;
17398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017399 private int mLastColor;
17400
Mike Cleronf116bf82009-09-27 19:14:12 -070017401 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017402 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17403 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017404 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17405 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017406
17407 paint = new Paint();
17408 matrix = new Matrix();
17409 // use use a height of 1, and then wack the matrix each time we
17410 // actually use it.
17411 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017413 paint.setShader(shader);
17414 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017415 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017416 }
Romain Guy8506ab42009-06-11 17:35:47 -070017417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017418 public void setFadeColor(int color) {
17419 if (color != 0 && color != mLastColor) {
17420 mLastColor = color;
17421 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017422
Romain Guye55e1a72009-08-27 10:42:26 -070017423 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17424 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017426 paint.setShader(shader);
17427 // Restore the default transfer mode (src_over)
17428 paint.setXfermode(null);
17429 }
17430 }
Joe Malin32736f02011-01-19 16:14:20 -080017431
Mike Cleronf116bf82009-09-27 19:14:12 -070017432 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017433 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017434 if (now >= fadeStartTime) {
17435
17436 // the animation fades the scrollbars out by changing
17437 // the opacity (alpha) from fully opaque to fully
17438 // transparent
17439 int nextFrame = (int) now;
17440 int framesCount = 0;
17441
17442 Interpolator interpolator = scrollBarInterpolator;
17443
17444 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017445 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017446
17447 // End transparent
17448 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017449 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017450
17451 state = FADING;
17452
17453 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017454 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017455 }
17456 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017457 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017458
Svetoslav Ganova0156172011-06-26 17:55:44 -070017459 /**
17460 * Resuable callback for sending
17461 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17462 */
17463 private class SendViewScrolledAccessibilityEvent implements Runnable {
17464 public volatile boolean mIsPending;
17465
17466 public void run() {
17467 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17468 mIsPending = false;
17469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017470 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017471
17472 /**
17473 * <p>
17474 * This class represents a delegate that can be registered in a {@link View}
17475 * to enhance accessibility support via composition rather via inheritance.
17476 * It is specifically targeted to widget developers that extend basic View
17477 * classes i.e. classes in package android.view, that would like their
17478 * applications to be backwards compatible.
17479 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017480 * <div class="special reference">
17481 * <h3>Developer Guides</h3>
17482 * <p>For more information about making applications accessible, read the
17483 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17484 * developer guide.</p>
17485 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017486 * <p>
17487 * A scenario in which a developer would like to use an accessibility delegate
17488 * is overriding a method introduced in a later API version then the minimal API
17489 * version supported by the application. For example, the method
17490 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17491 * in API version 4 when the accessibility APIs were first introduced. If a
17492 * developer would like his application to run on API version 4 devices (assuming
17493 * all other APIs used by the application are version 4 or lower) and take advantage
17494 * of this method, instead of overriding the method which would break the application's
17495 * backwards compatibility, he can override the corresponding method in this
17496 * delegate and register the delegate in the target View if the API version of
17497 * the system is high enough i.e. the API version is same or higher to the API
17498 * version that introduced
17499 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17500 * </p>
17501 * <p>
17502 * Here is an example implementation:
17503 * </p>
17504 * <code><pre><p>
17505 * if (Build.VERSION.SDK_INT >= 14) {
17506 * // If the API version is equal of higher than the version in
17507 * // which onInitializeAccessibilityNodeInfo was introduced we
17508 * // register a delegate with a customized implementation.
17509 * View view = findViewById(R.id.view_id);
17510 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17511 * public void onInitializeAccessibilityNodeInfo(View host,
17512 * AccessibilityNodeInfo info) {
17513 * // Let the default implementation populate the info.
17514 * super.onInitializeAccessibilityNodeInfo(host, info);
17515 * // Set some other information.
17516 * info.setEnabled(host.isEnabled());
17517 * }
17518 * });
17519 * }
17520 * </code></pre></p>
17521 * <p>
17522 * This delegate contains methods that correspond to the accessibility methods
17523 * in View. If a delegate has been specified the implementation in View hands
17524 * off handling to the corresponding method in this delegate. The default
17525 * implementation the delegate methods behaves exactly as the corresponding
17526 * method in View for the case of no accessibility delegate been set. Hence,
17527 * to customize the behavior of a View method, clients can override only the
17528 * corresponding delegate method without altering the behavior of the rest
17529 * accessibility related methods of the host view.
17530 * </p>
17531 */
17532 public static class AccessibilityDelegate {
17533
17534 /**
17535 * Sends an accessibility event of the given type. If accessibility is not
17536 * enabled this method has no effect.
17537 * <p>
17538 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17539 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17540 * been set.
17541 * </p>
17542 *
17543 * @param host The View hosting the delegate.
17544 * @param eventType The type of the event to send.
17545 *
17546 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17547 */
17548 public void sendAccessibilityEvent(View host, int eventType) {
17549 host.sendAccessibilityEventInternal(eventType);
17550 }
17551
17552 /**
alanv8eeefef2012-05-07 16:57:53 -070017553 * Performs the specified accessibility action on the view. For
17554 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17555 * <p>
17556 * The default implementation behaves as
17557 * {@link View#performAccessibilityAction(int, Bundle)
17558 * View#performAccessibilityAction(int, Bundle)} for the case of
17559 * no accessibility delegate been set.
17560 * </p>
17561 *
17562 * @param action The action to perform.
17563 * @return Whether the action was performed.
17564 *
17565 * @see View#performAccessibilityAction(int, Bundle)
17566 * View#performAccessibilityAction(int, Bundle)
17567 */
17568 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17569 return host.performAccessibilityActionInternal(action, args);
17570 }
17571
17572 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017573 * Sends an accessibility event. This method behaves exactly as
17574 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17575 * empty {@link AccessibilityEvent} and does not perform a check whether
17576 * accessibility is enabled.
17577 * <p>
17578 * The default implementation behaves as
17579 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17580 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17581 * the case of no accessibility delegate been set.
17582 * </p>
17583 *
17584 * @param host The View hosting the delegate.
17585 * @param event The event to send.
17586 *
17587 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17588 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17589 */
17590 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17591 host.sendAccessibilityEventUncheckedInternal(event);
17592 }
17593
17594 /**
17595 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17596 * to its children for adding their text content to the event.
17597 * <p>
17598 * The default implementation behaves as
17599 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17600 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17601 * the case of no accessibility delegate been set.
17602 * </p>
17603 *
17604 * @param host The View hosting the delegate.
17605 * @param event The event.
17606 * @return True if the event population was completed.
17607 *
17608 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17609 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17610 */
17611 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17612 return host.dispatchPopulateAccessibilityEventInternal(event);
17613 }
17614
17615 /**
17616 * Gives a chance to the host View to populate the accessibility event with its
17617 * text content.
17618 * <p>
17619 * The default implementation behaves as
17620 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17621 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17622 * the case of no accessibility delegate been set.
17623 * </p>
17624 *
17625 * @param host The View hosting the delegate.
17626 * @param event The accessibility event which to populate.
17627 *
17628 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17629 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17630 */
17631 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17632 host.onPopulateAccessibilityEventInternal(event);
17633 }
17634
17635 /**
17636 * Initializes an {@link AccessibilityEvent} with information about the
17637 * the host View which is the event source.
17638 * <p>
17639 * The default implementation behaves as
17640 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17641 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17642 * the case of no accessibility delegate been set.
17643 * </p>
17644 *
17645 * @param host The View hosting the delegate.
17646 * @param event The event to initialize.
17647 *
17648 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17649 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17650 */
17651 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17652 host.onInitializeAccessibilityEventInternal(event);
17653 }
17654
17655 /**
17656 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17657 * <p>
17658 * The default implementation behaves as
17659 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17660 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17661 * the case of no accessibility delegate been set.
17662 * </p>
17663 *
17664 * @param host The View hosting the delegate.
17665 * @param info The instance to initialize.
17666 *
17667 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17668 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17669 */
17670 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17671 host.onInitializeAccessibilityNodeInfoInternal(info);
17672 }
17673
17674 /**
17675 * Called when a child of the host View has requested sending an
17676 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17677 * to augment the event.
17678 * <p>
17679 * The default implementation behaves as
17680 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17681 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17682 * the case of no accessibility delegate been set.
17683 * </p>
17684 *
17685 * @param host The View hosting the delegate.
17686 * @param child The child which requests sending the event.
17687 * @param event The event to be sent.
17688 * @return True if the event should be sent
17689 *
17690 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17691 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17692 */
17693 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17694 AccessibilityEvent event) {
17695 return host.onRequestSendAccessibilityEventInternal(child, event);
17696 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017697
17698 /**
17699 * Gets the provider for managing a virtual view hierarchy rooted at this View
17700 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17701 * that explore the window content.
17702 * <p>
17703 * The default implementation behaves as
17704 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17705 * the case of no accessibility delegate been set.
17706 * </p>
17707 *
17708 * @return The provider.
17709 *
17710 * @see AccessibilityNodeProvider
17711 */
17712 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17713 return null;
17714 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017716}