blob: d1e90b465d624808e2cc436e2b8437142ce0f94e [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;
42import android.os.Handler;
43import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070050import android.util.FloatProperty;
51import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070053import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070064import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070065import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070067import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080068import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.inputmethod.InputConnection;
71import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.widget.ScrollBarDrawable;
73
Romain Guy1ef3fdb2011-09-09 15:30:30 -070074import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070075import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070076
Doug Feltcb3791202011-07-07 11:57:48 -070077import com.android.internal.R;
78import com.android.internal.util.Predicate;
79import com.android.internal.view.menu.MenuBuilder;
80
Christopher Tatea0374192010-10-05 13:06:41 -070081import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070082import java.lang.reflect.InvocationTargetException;
83import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070086import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080087import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/**
90 * <p>
91 * This class represents the basic building block for user interface components. A View
92 * occupies a rectangular area on the screen and is responsible for drawing and
93 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070094 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
96 * are invisible containers that hold other Views (or other ViewGroups) and define
97 * their layout properties.
98 * </p>
99 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700100 * <div class="special reference">
101 * <h3>Developer Guides</h3>
102 * <p>For information about using this class to develop your application's user interface,
103 * 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 -0800104 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * <a name="Using"></a>
107 * <h3>Using Views</h3>
108 * <p>
109 * All of the views in a window are arranged in a single tree. You can add views
110 * either from code or by specifying a tree of views in one or more XML layout
111 * files. There are many specialized subclasses of views that act as controls or
112 * are capable of displaying text, images, or other content.
113 * </p>
114 * <p>
115 * Once you have created a tree of views, there are typically a few types of
116 * common operations you may wish to perform:
117 * <ul>
118 * <li><strong>Set properties:</strong> for example setting the text of a
119 * {@link android.widget.TextView}. The available properties and the methods
120 * that set them will vary among the different subclasses of views. Note that
121 * properties that are known at build time can be set in the XML layout
122 * files.</li>
123 * <li><strong>Set focus:</strong> The framework will handled moving focus in
124 * response to user input. To force focus to a specific view, call
125 * {@link #requestFocus}.</li>
126 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
127 * that will be notified when something interesting happens to the view. For
128 * example, all views will let you set a listener to be notified when the view
129 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700130 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700131 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * </ul>
136 * </p>
137 * <p><em>
138 * Note: The Android framework is responsible for measuring, laying out and
139 * drawing views. You should not call methods that perform these actions on
140 * views yourself unless you are actually implementing a
141 * {@link android.view.ViewGroup}.
142 * </em></p>
143 *
144 * <a name="Lifecycle"></a>
145 * <h3>Implementing a Custom View</h3>
146 *
147 * <p>
148 * To implement a custom view, you will usually begin by providing overrides for
149 * some of the standard methods that the framework calls on all views. You do
150 * not need to override all of these methods. In fact, you can start by just
151 * overriding {@link #onDraw(android.graphics.Canvas)}.
152 * <table border="2" width="85%" align="center" cellpadding="5">
153 * <thead>
154 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
155 * </thead>
156 *
157 * <tbody>
158 * <tr>
159 * <td rowspan="2">Creation</td>
160 * <td>Constructors</td>
161 * <td>There is a form of the constructor that are called when the view
162 * is created from code and a form that is called when the view is
163 * inflated from a layout file. The second form should parse and apply
164 * any attributes defined in the layout file.
165 * </td>
166 * </tr>
167 * <tr>
168 * <td><code>{@link #onFinishInflate()}</code></td>
169 * <td>Called after a view and all of its children has been inflated
170 * from XML.</td>
171 * </tr>
172 *
173 * <tr>
174 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700175 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * <td>Called to determine the size requirements for this view and all
177 * of its children.
178 * </td>
179 * </tr>
180 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700181 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <td>Called when this view should assign a size and position to all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called when the size of this view has changed.
189 * </td>
190 * </tr>
191 *
192 * <tr>
193 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the view should render its content.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when a new key event occurs.
203 * </td>
204 * </tr>
205 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a key up event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a trackball motion event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a touch screen motion event occurs.
218 * </td>
219 * </tr>
220 *
221 * <tr>
222 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when the view gains or loses focus.
225 * </td>
226 * </tr>
227 *
228 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when the window containing the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
235 * <td rowspan="3">Attaching</td>
236 * <td><code>{@link #onAttachedToWindow()}</code></td>
237 * <td>Called when the view is attached to a window.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td><code>{@link #onDetachedFromWindow}</code></td>
243 * <td>Called when the view is detached from its window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700248 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * <td>Called when the visibility of the window containing the view
250 * has changed.
251 * </td>
252 * </tr>
253 * </tbody>
254 *
255 * </table>
256 * </p>
257 *
258 * <a name="IDs"></a>
259 * <h3>IDs</h3>
260 * Views may have an integer id associated with them. These ids are typically
261 * assigned in the layout XML files, and are used to find specific views within
262 * the view tree. A common pattern is to:
263 * <ul>
264 * <li>Define a Button in the layout file and assign it a unique ID.
265 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700266 * &lt;Button
267 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * android:layout_width="wrap_content"
269 * android:layout_height="wrap_content"
270 * android:text="@string/my_button_text"/&gt;
271 * </pre></li>
272 * <li>From the onCreate method of an Activity, find the Button
273 * <pre class="prettyprint">
274 * Button myButton = (Button) findViewById(R.id.my_button);
275 * </pre></li>
276 * </ul>
277 * <p>
278 * View IDs need not be unique throughout the tree, but it is good practice to
279 * ensure that they are at least unique within the part of the tree you are
280 * searching.
281 * </p>
282 *
283 * <a name="Position"></a>
284 * <h3>Position</h3>
285 * <p>
286 * The geometry of a view is that of a rectangle. A view has a location,
287 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
288 * two dimensions, expressed as a width and a height. The unit for location
289 * and dimensions is the pixel.
290 * </p>
291 *
292 * <p>
293 * It is possible to retrieve the location of a view by invoking the methods
294 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
295 * coordinate of the rectangle representing the view. The latter returns the
296 * top, or Y, coordinate of the rectangle representing the view. These methods
297 * both return the location of the view relative to its parent. For instance,
298 * when getLeft() returns 20, that means the view is located 20 pixels to the
299 * right of the left edge of its direct parent.
300 * </p>
301 *
302 * <p>
303 * In addition, several convenience methods are offered to avoid unnecessary
304 * computations, namely {@link #getRight()} and {@link #getBottom()}.
305 * These methods return the coordinates of the right and bottom edges of the
306 * rectangle representing the view. For instance, calling {@link #getRight()}
307 * is similar to the following computation: <code>getLeft() + getWidth()</code>
308 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
309 * </p>
310 *
311 * <a name="SizePaddingMargins"></a>
312 * <h3>Size, padding and margins</h3>
313 * <p>
314 * The size of a view is expressed with a width and a height. A view actually
315 * possess two pairs of width and height values.
316 * </p>
317 *
318 * <p>
319 * The first pair is known as <em>measured width</em> and
320 * <em>measured height</em>. These dimensions define how big a view wants to be
321 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
322 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
323 * and {@link #getMeasuredHeight()}.
324 * </p>
325 *
326 * <p>
327 * The second pair is simply known as <em>width</em> and <em>height</em>, or
328 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
329 * dimensions define the actual size of the view on screen, at drawing time and
330 * after layout. These values may, but do not have to, be different from the
331 * measured width and height. The width and height can be obtained by calling
332 * {@link #getWidth()} and {@link #getHeight()}.
333 * </p>
334 *
335 * <p>
336 * To measure its dimensions, a view takes into account its padding. The padding
337 * is expressed in pixels for the left, top, right and bottom parts of the view.
338 * Padding can be used to offset the content of the view by a specific amount of
339 * pixels. For instance, a left padding of 2 will push the view's content by
340 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700341 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
342 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
343 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
344 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <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 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700425 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * 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 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700572 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
573 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700583 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700584 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700586 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700588 * @attr ref android.R.styleable#View_isScrollContainer
589 * @attr ref android.R.styleable#View_focusable
590 * @attr ref android.R.styleable#View_focusableInTouchMode
591 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
592 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800593 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700594 * @attr ref android.R.styleable#View_longClickable
595 * @attr ref android.R.styleable#View_minHeight
596 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 * @attr ref android.R.styleable#View_nextFocusDown
598 * @attr ref android.R.styleable#View_nextFocusLeft
599 * @attr ref android.R.styleable#View_nextFocusRight
600 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700601 * @attr ref android.R.styleable#View_onClick
602 * @attr ref android.R.styleable#View_padding
603 * @attr ref android.R.styleable#View_paddingBottom
604 * @attr ref android.R.styleable#View_paddingLeft
605 * @attr ref android.R.styleable#View_paddingRight
606 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800607 * @attr ref android.R.styleable#View_paddingStart
608 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800610 * @attr ref android.R.styleable#View_rotation
611 * @attr ref android.R.styleable#View_rotationX
612 * @attr ref android.R.styleable#View_rotationY
613 * @attr ref android.R.styleable#View_scaleX
614 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * @attr ref android.R.styleable#View_scrollX
616 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_scrollbarSize
618 * @attr ref android.R.styleable#View_scrollbarStyle
619 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700620 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
621 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
623 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * @attr ref android.R.styleable#View_scrollbarThumbVertical
625 * @attr ref android.R.styleable#View_scrollbarTrackVertical
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700628 * @attr ref android.R.styleable#View_soundEffectsEnabled
629 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700630 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_transformPivotX
632 * @attr ref android.R.styleable#View_transformPivotY
633 * @attr ref android.R.styleable#View_translationX
634 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 *
637 * @see android.view.ViewGroup
638 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700639public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
640 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 private static final boolean DBG = false;
642
643 /**
644 * The logging tag used by this class with android.util.Log.
645 */
646 protected static final String VIEW_LOG_TAG = "View";
647
648 /**
649 * Used to mark a View that has no ID.
650 */
651 public static final int NO_ID = -1;
652
653 /**
654 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
655 * calling setFlags.
656 */
657 private static final int NOT_FOCUSABLE = 0x00000000;
658
659 /**
660 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
661 * setFlags.
662 */
663 private static final int FOCUSABLE = 0x00000001;
664
665 /**
666 * Mask for use with setFlags indicating bits used for focus.
667 */
668 private static final int FOCUSABLE_MASK = 0x00000001;
669
670 /**
671 * This view will adjust its padding to fit sytem windows (e.g. status bar)
672 */
673 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
674
675 /**
Scott Main812634c22011-07-27 13:22:35 -0700676 * This view is visible.
677 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
678 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static final int VISIBLE = 0x00000000;
681
682 /**
683 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700684 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
685 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 */
687 public static final int INVISIBLE = 0x00000004;
688
689 /**
690 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700691 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
692 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 */
694 public static final int GONE = 0x00000008;
695
696 /**
697 * Mask for use with setFlags indicating bits used for visibility.
698 * {@hide}
699 */
700 static final int VISIBILITY_MASK = 0x0000000C;
701
702 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
703
704 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700705 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 * Use with ENABLED_MASK when calling setFlags.
707 * {@hide}
708 */
709 static final int ENABLED = 0x00000000;
710
711 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700712 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 * Use with ENABLED_MASK when calling setFlags.
714 * {@hide}
715 */
716 static final int DISABLED = 0x00000020;
717
718 /**
719 * Mask for use with setFlags indicating bits used for indicating whether
720 * this view is enabled
721 * {@hide}
722 */
723 static final int ENABLED_MASK = 0x00000020;
724
725 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700726 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
727 * called and further optimizations will be performed. It is okay to have
728 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 * {@hide}
730 */
731 static final int WILL_NOT_DRAW = 0x00000080;
732
733 /**
734 * Mask for use with setFlags indicating bits used for indicating whether
735 * this view is will draw
736 * {@hide}
737 */
738 static final int DRAW_MASK = 0x00000080;
739
740 /**
741 * <p>This view doesn't show scrollbars.</p>
742 * {@hide}
743 */
744 static final int SCROLLBARS_NONE = 0x00000000;
745
746 /**
747 * <p>This view shows horizontal scrollbars.</p>
748 * {@hide}
749 */
750 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
751
752 /**
753 * <p>This view shows vertical scrollbars.</p>
754 * {@hide}
755 */
756 static final int SCROLLBARS_VERTICAL = 0x00000200;
757
758 /**
759 * <p>Mask for use with setFlags indicating bits used for indicating which
760 * scrollbars are enabled.</p>
761 * {@hide}
762 */
763 static final int SCROLLBARS_MASK = 0x00000300;
764
Jeff Brown85a31762010-09-01 17:01:00 -0700765 /**
766 * Indicates that the view should filter touches when its window is obscured.
767 * Refer to the class comments for more information about this security feature.
768 * {@hide}
769 */
770 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
771
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700772 /**
773 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
774 * that they are optional and should be skipped if the window has
775 * requested system UI flags that ignore those insets for layout.
776 */
777 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778
779 /**
780 * <p>This view doesn't show fading edges.</p>
781 * {@hide}
782 */
783 static final int FADING_EDGE_NONE = 0x00000000;
784
785 /**
786 * <p>This view shows horizontal fading edges.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
790
791 /**
792 * <p>This view shows vertical fading edges.</p>
793 * {@hide}
794 */
795 static final int FADING_EDGE_VERTICAL = 0x00002000;
796
797 /**
798 * <p>Mask for use with setFlags indicating bits used for indicating which
799 * fading edges are enabled.</p>
800 * {@hide}
801 */
802 static final int FADING_EDGE_MASK = 0x00003000;
803
804 /**
805 * <p>Indicates this view can be clicked. When clickable, a View reacts
806 * to clicks by notifying the OnClickListener.<p>
807 * {@hide}
808 */
809 static final int CLICKABLE = 0x00004000;
810
811 /**
812 * <p>Indicates this view is caching its drawing into a bitmap.</p>
813 * {@hide}
814 */
815 static final int DRAWING_CACHE_ENABLED = 0x00008000;
816
817 /**
818 * <p>Indicates that no icicle should be saved for this view.<p>
819 * {@hide}
820 */
821 static final int SAVE_DISABLED = 0x000010000;
822
823 /**
824 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
825 * property.</p>
826 * {@hide}
827 */
828 static final int SAVE_DISABLED_MASK = 0x000010000;
829
830 /**
831 * <p>Indicates that no drawing cache should ever be created for this view.<p>
832 * {@hide}
833 */
834 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
835
836 /**
837 * <p>Indicates this view can take / keep focus when int touch mode.</p>
838 * {@hide}
839 */
840 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
841
842 /**
843 * <p>Enables low quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
846
847 /**
848 * <p>Enables high quality mode for the drawing cache.</p>
849 */
850 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
851
852 /**
853 * <p>Enables automatic quality mode for the drawing cache.</p>
854 */
855 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
856
857 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
858 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
859 };
860
861 /**
862 * <p>Mask for use with setFlags indicating bits used for the cache
863 * quality property.</p>
864 * {@hide}
865 */
866 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
867
868 /**
869 * <p>
870 * Indicates this view can be long clicked. When long clickable, a View
871 * reacts to long clicks by notifying the OnLongClickListener or showing a
872 * context menu.
873 * </p>
874 * {@hide}
875 */
876 static final int LONG_CLICKABLE = 0x00200000;
877
878 /**
879 * <p>Indicates that this view gets its drawable states from its direct parent
880 * and ignores its original internal states.</p>
881 *
882 * @hide
883 */
884 static final int DUPLICATE_PARENT_STATE = 0x00400000;
885
886 /**
887 * The scrollbar style to display the scrollbars inside the content area,
888 * without increasing the padding. The scrollbars will be overlaid with
889 * translucency on the view's content.
890 */
891 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
892
893 /**
894 * The scrollbar style to display the scrollbars inside the padded area,
895 * increasing the padding of the view. The scrollbars will not overlap the
896 * content area of the view.
897 */
898 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
899
900 /**
901 * The scrollbar style to display the scrollbars at the edge of the view,
902 * without increasing the padding. The scrollbars will be overlaid with
903 * translucency.
904 */
905 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
906
907 /**
908 * The scrollbar style to display the scrollbars at the edge of the view,
909 * increasing the padding of the view. The scrollbars will only overlap the
910 * background, if any.
911 */
912 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
913
914 /**
915 * Mask to check if the scrollbar style is overlay or inset.
916 * {@hide}
917 */
918 static final int SCROLLBARS_INSET_MASK = 0x01000000;
919
920 /**
921 * Mask to check if the scrollbar style is inside or outside.
922 * {@hide}
923 */
924 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
925
926 /**
927 * Mask for scrollbar style.
928 * {@hide}
929 */
930 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
931
932 /**
933 * View flag indicating that the screen should remain on while the
934 * window containing this view is visible to the user. This effectively
935 * takes care of automatically setting the WindowManager's
936 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
937 */
938 public static final int KEEP_SCREEN_ON = 0x04000000;
939
940 /**
941 * View flag indicating whether this view should have sound effects enabled
942 * for events such as clicking and touching.
943 */
944 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
945
946 /**
947 * View flag indicating whether this view should have haptic feedback
948 * enabled for events such as long presses.
949 */
950 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
951
952 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700953 * <p>Indicates that the view hierarchy should stop saving state when
954 * it reaches this view. If state saving is initiated immediately at
955 * the view, it will be allowed.
956 * {@hide}
957 */
958 static final int PARENT_SAVE_DISABLED = 0x20000000;
959
960 /**
961 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
962 * {@hide}
963 */
964 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
965
966 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700967 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
968 * should add all focusable Views regardless if they are focusable in touch mode.
969 */
970 public static final int FOCUSABLES_ALL = 0x00000000;
971
972 /**
973 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
974 * should add only Views focusable in touch mode.
975 */
976 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
977
978 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700979 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 * item.
981 */
982 public static final int FOCUS_BACKWARD = 0x00000001;
983
984 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700985 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 * item.
987 */
988 public static final int FOCUS_FORWARD = 0x00000002;
989
990 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700991 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 */
993 public static final int FOCUS_LEFT = 0x00000011;
994
995 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700996 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 */
998 public static final int FOCUS_UP = 0x00000021;
999
1000 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001001 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 */
1003 public static final int FOCUS_RIGHT = 0x00000042;
1004
1005 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001006 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 */
1008 public static final int FOCUS_DOWN = 0x00000082;
1009
1010 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001011 * Bits of {@link #getMeasuredWidthAndState()} and
1012 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1013 */
1014 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1015
1016 /**
1017 * Bits of {@link #getMeasuredWidthAndState()} and
1018 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1019 */
1020 public static final int MEASURED_STATE_MASK = 0xff000000;
1021
1022 /**
1023 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1024 * for functions that combine both width and height into a single int,
1025 * such as {@link #getMeasuredState()} and the childState argument of
1026 * {@link #resolveSizeAndState(int, int, int)}.
1027 */
1028 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1029
1030 /**
1031 * Bit of {@link #getMeasuredWidthAndState()} and
1032 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1033 * is smaller that the space the view would like to have.
1034 */
1035 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1036
1037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 * Base View state sets
1039 */
1040 // Singles
1041 /**
1042 * Indicates the view has no states set. States are used with
1043 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1044 * view depending on its state.
1045 *
1046 * @see android.graphics.drawable.Drawable
1047 * @see #getDrawableState()
1048 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001049 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 /**
1051 * Indicates the view is enabled. States are used with
1052 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1053 * view depending on its state.
1054 *
1055 * @see android.graphics.drawable.Drawable
1056 * @see #getDrawableState()
1057 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001058 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 /**
1060 * Indicates the view is focused. States are used with
1061 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1062 * view depending on its state.
1063 *
1064 * @see android.graphics.drawable.Drawable
1065 * @see #getDrawableState()
1066 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001067 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 /**
1069 * Indicates the view is selected. States are used with
1070 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1071 * view depending on its state.
1072 *
1073 * @see android.graphics.drawable.Drawable
1074 * @see #getDrawableState()
1075 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001076 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 /**
1078 * Indicates the view is pressed. States are used with
1079 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1080 * view depending on its state.
1081 *
1082 * @see android.graphics.drawable.Drawable
1083 * @see #getDrawableState()
1084 * @hide
1085 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001086 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 /**
1088 * Indicates the view's window has focus. States are used with
1089 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1090 * view depending on its state.
1091 *
1092 * @see android.graphics.drawable.Drawable
1093 * @see #getDrawableState()
1094 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001095 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 // Doubles
1097 /**
1098 * Indicates the view is enabled and has the focus.
1099 *
1100 * @see #ENABLED_STATE_SET
1101 * @see #FOCUSED_STATE_SET
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is enabled and selected.
1106 *
1107 * @see #ENABLED_STATE_SET
1108 * @see #SELECTED_STATE_SET
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view is enabled and that its window has focus.
1113 *
1114 * @see #ENABLED_STATE_SET
1115 * @see #WINDOW_FOCUSED_STATE_SET
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 /**
1119 * Indicates the view is focused and selected.
1120 *
1121 * @see #FOCUSED_STATE_SET
1122 * @see #SELECTED_STATE_SET
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 /**
1126 * Indicates the view has the focus and that its window has the focus.
1127 *
1128 * @see #FOCUSED_STATE_SET
1129 * @see #WINDOW_FOCUSED_STATE_SET
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is selected and that its window has the focus.
1134 *
1135 * @see #SELECTED_STATE_SET
1136 * @see #WINDOW_FOCUSED_STATE_SET
1137 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001138 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 // Triples
1140 /**
1141 * Indicates the view is enabled, focused and selected.
1142 *
1143 * @see #ENABLED_STATE_SET
1144 * @see #FOCUSED_STATE_SET
1145 * @see #SELECTED_STATE_SET
1146 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001147 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 /**
1149 * Indicates the view is enabled, focused and its window has the focus.
1150 *
1151 * @see #ENABLED_STATE_SET
1152 * @see #FOCUSED_STATE_SET
1153 * @see #WINDOW_FOCUSED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is enabled, selected and its window has the focus.
1158 *
1159 * @see #ENABLED_STATE_SET
1160 * @see #SELECTED_STATE_SET
1161 * @see #WINDOW_FOCUSED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view is focused, selected and its window has the focus.
1166 *
1167 * @see #FOCUSED_STATE_SET
1168 * @see #SELECTED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 /**
1173 * Indicates the view is enabled, focused, selected and its window
1174 * has the focus.
1175 *
1176 * @see #ENABLED_STATE_SET
1177 * @see #FOCUSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 * @see #WINDOW_FOCUSED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is pressed and its window has the focus.
1184 *
1185 * @see #PRESSED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is pressed and selected.
1191 *
1192 * @see #PRESSED_STATE_SET
1193 * @see #SELECTED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is pressed, selected and its window has the focus.
1198 *
1199 * @see #PRESSED_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[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is pressed and focused.
1206 *
1207 * @see #PRESSED_STATE_SET
1208 * @see #FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is pressed, focused and its window has the focus.
1213 *
1214 * @see #PRESSED_STATE_SET
1215 * @see #FOCUSED_STATE_SET
1216 * @see #WINDOW_FOCUSED_STATE_SET
1217 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001218 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 /**
1220 * Indicates the view is pressed, focused and selected.
1221 *
1222 * @see #PRESSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed, focused, selected and its window has the focus.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #FOCUSED_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_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed and enabled.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #ENABLED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed, enabled and its window has the focus.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #ENABLED_STATE_SET
1248 * @see #WINDOW_FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, enabled and selected.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #ENABLED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, enabled, selected and its window has the
1261 * focus.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #ENABLED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #WINDOW_FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, enabled and focused.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #ENABLED_STATE_SET
1274 * @see #FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed, enabled, focused and its window has the
1279 * focus.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 * @see #FOCUSED_STATE_SET
1284 * @see #WINDOW_FOCUSED_STATE_SET
1285 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001286 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 /**
1288 * Indicates the view is pressed, enabled, focused and selected.
1289 *
1290 * @see #PRESSED_STATE_SET
1291 * @see #ENABLED_STATE_SET
1292 * @see #SELECTED_STATE_SET
1293 * @see #FOCUSED_STATE_SET
1294 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001295 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 /**
1297 * Indicates the view is pressed, enabled, focused, selected and its window
1298 * has the focus.
1299 *
1300 * @see #PRESSED_STATE_SET
1301 * @see #ENABLED_STATE_SET
1302 * @see #SELECTED_STATE_SET
1303 * @see #FOCUSED_STATE_SET
1304 * @see #WINDOW_FOCUSED_STATE_SET
1305 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001306 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307
1308 /**
1309 * The order here is very important to {@link #getDrawableState()}
1310 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001311 private static final int[][] VIEW_STATE_SETS;
1312
Romain Guyb051e892010-09-28 19:09:36 -07001313 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1314 static final int VIEW_STATE_SELECTED = 1 << 1;
1315 static final int VIEW_STATE_FOCUSED = 1 << 2;
1316 static final int VIEW_STATE_ENABLED = 1 << 3;
1317 static final int VIEW_STATE_PRESSED = 1 << 4;
1318 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001319 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001320 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001321 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1322 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323
1324 static final int[] VIEW_STATE_IDS = new int[] {
1325 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1326 R.attr.state_selected, VIEW_STATE_SELECTED,
1327 R.attr.state_focused, VIEW_STATE_FOCUSED,
1328 R.attr.state_enabled, VIEW_STATE_ENABLED,
1329 R.attr.state_pressed, VIEW_STATE_PRESSED,
1330 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001331 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001332 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001333 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1334 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 };
1336
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001338 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1339 throw new IllegalStateException(
1340 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1341 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001343 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001345 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001347 orderedIds[i * 2] = viewState;
1348 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 }
1350 }
1351 }
Romain Guyb051e892010-09-28 19:09:36 -07001352 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1353 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1354 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001355 int numBits = Integer.bitCount(i);
1356 int[] set = new int[numBits];
1357 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001358 for (int j = 0; j < orderedIds.length; j += 2) {
1359 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001360 set[pos++] = orderedIds[j];
1361 }
1362 }
1363 VIEW_STATE_SETS[i] = set;
1364 }
1365
1366 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1367 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1368 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1369 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1370 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1371 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1372 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1373 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1374 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1376 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1378 | VIEW_STATE_FOCUSED];
1379 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1380 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1381 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1382 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1383 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1384 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1385 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1386 | VIEW_STATE_ENABLED];
1387 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1389 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1390 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1391 | VIEW_STATE_ENABLED];
1392 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1393 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1394 | VIEW_STATE_ENABLED];
1395 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1396 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1397 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1398
1399 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1400 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1402 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1404 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1406 | VIEW_STATE_PRESSED];
1407 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1409 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1411 | VIEW_STATE_PRESSED];
1412 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1414 | VIEW_STATE_PRESSED];
1415 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1418 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1420 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1422 | VIEW_STATE_PRESSED];
1423 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1425 | VIEW_STATE_PRESSED];
1426 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1429 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1431 | VIEW_STATE_PRESSED];
1432 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1434 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1435 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1437 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1438 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1440 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1441 | VIEW_STATE_PRESSED];
1442 }
1443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001445 * Accessibility event types that are dispatched for text population.
1446 */
1447 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1448 AccessibilityEvent.TYPE_VIEW_CLICKED
1449 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1450 | AccessibilityEvent.TYPE_VIEW_SELECTED
1451 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1452 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1453 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001454 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001455 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1456 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001457
1458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 * Temporary Rect currently for use in setBackground(). This will probably
1460 * be extended in the future to hold our own class with more than just
1461 * a Rect. :)
1462 */
1463 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001464
1465 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001466 * Temporary flag, used to enable processing of View properties in the native DisplayList
1467 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1468 * apps.
1469 * @hide
1470 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001471 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001472
1473 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001474 * Map used to store views' tags.
1475 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001476 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001479 * The next available accessiiblity id.
1480 */
1481 private static int sNextAccessibilityViewId;
1482
1483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 * The animation currently associated with this view.
1485 * @hide
1486 */
1487 protected Animation mCurrentAnimation = null;
1488
1489 /**
1490 * Width as measured during measure pass.
1491 * {@hide}
1492 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001493 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001494 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495
1496 /**
1497 * Height as measured during measure pass.
1498 * {@hide}
1499 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001500 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001501 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502
1503 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001504 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1505 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1506 * its display list. This flag, used only when hw accelerated, allows us to clear the
1507 * flag while retaining this information until it's needed (at getDisplayList() time and
1508 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1509 *
1510 * {@hide}
1511 */
1512 boolean mRecreateDisplayList = false;
1513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * The view's identifier.
1516 * {@hide}
1517 *
1518 * @see #setId(int)
1519 * @see #getId()
1520 */
1521 @ViewDebug.ExportedProperty(resolveId = true)
1522 int mID = NO_ID;
1523
1524 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001525 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001526 */
1527 int mAccessibilityViewId = NO_ID;
1528
1529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 * The view's tag.
1531 * {@hide}
1532 *
1533 * @see #setTag(Object)
1534 * @see #getTag()
1535 */
1536 protected Object mTag;
1537
1538 // for mPrivateFlags:
1539 /** {@hide} */
1540 static final int WANTS_FOCUS = 0x00000001;
1541 /** {@hide} */
1542 static final int FOCUSED = 0x00000002;
1543 /** {@hide} */
1544 static final int SELECTED = 0x00000004;
1545 /** {@hide} */
1546 static final int IS_ROOT_NAMESPACE = 0x00000008;
1547 /** {@hide} */
1548 static final int HAS_BOUNDS = 0x00000010;
1549 /** {@hide} */
1550 static final int DRAWN = 0x00000020;
1551 /**
1552 * When this flag is set, this view is running an animation on behalf of its
1553 * children and should therefore not cancel invalidate requests, even if they
1554 * lie outside of this view's bounds.
1555 *
1556 * {@hide}
1557 */
1558 static final int DRAW_ANIMATION = 0x00000040;
1559 /** {@hide} */
1560 static final int SKIP_DRAW = 0x00000080;
1561 /** {@hide} */
1562 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1563 /** {@hide} */
1564 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1565 /** {@hide} */
1566 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1567 /** {@hide} */
1568 static final int MEASURED_DIMENSION_SET = 0x00000800;
1569 /** {@hide} */
1570 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001571 /** {@hide} */
1572 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573
1574 private static final int PRESSED = 0x00004000;
1575
1576 /** {@hide} */
1577 static final int DRAWING_CACHE_VALID = 0x00008000;
1578 /**
1579 * Flag used to indicate that this view should be drawn once more (and only once
1580 * more) after its animation has completed.
1581 * {@hide}
1582 */
1583 static final int ANIMATION_STARTED = 0x00010000;
1584
1585 private static final int SAVE_STATE_CALLED = 0x00020000;
1586
1587 /**
1588 * Indicates that the View returned true when onSetAlpha() was called and that
1589 * the alpha must be restored.
1590 * {@hide}
1591 */
1592 static final int ALPHA_SET = 0x00040000;
1593
1594 /**
1595 * Set by {@link #setScrollContainer(boolean)}.
1596 */
1597 static final int SCROLL_CONTAINER = 0x00080000;
1598
1599 /**
1600 * Set by {@link #setScrollContainer(boolean)}.
1601 */
1602 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1603
1604 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001605 * View flag indicating whether this view was invalidated (fully or partially.)
1606 *
1607 * @hide
1608 */
1609 static final int DIRTY = 0x00200000;
1610
1611 /**
1612 * View flag indicating whether this view was invalidated by an opaque
1613 * invalidate request.
1614 *
1615 * @hide
1616 */
1617 static final int DIRTY_OPAQUE = 0x00400000;
1618
1619 /**
1620 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1621 *
1622 * @hide
1623 */
1624 static final int DIRTY_MASK = 0x00600000;
1625
1626 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001627 * Indicates whether the background is opaque.
1628 *
1629 * @hide
1630 */
1631 static final int OPAQUE_BACKGROUND = 0x00800000;
1632
1633 /**
1634 * Indicates whether the scrollbars are opaque.
1635 *
1636 * @hide
1637 */
1638 static final int OPAQUE_SCROLLBARS = 0x01000000;
1639
1640 /**
1641 * Indicates whether the view is opaque.
1642 *
1643 * @hide
1644 */
1645 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001646
Adam Powelle14579b2009-12-16 18:39:52 -08001647 /**
1648 * Indicates a prepressed state;
1649 * the short time between ACTION_DOWN and recognizing
1650 * a 'real' press. Prepressed is used to recognize quick taps
1651 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001652 *
Adam Powelle14579b2009-12-16 18:39:52 -08001653 * @hide
1654 */
1655 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001656
Adam Powellc9fbaab2010-02-16 17:16:19 -08001657 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001658 * Indicates whether the view is temporarily detached.
1659 *
1660 * @hide
1661 */
1662 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001663
Adam Powell8568c3a2010-04-19 14:26:11 -07001664 /**
1665 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001666 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001667 * @hide
1668 */
1669 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001670
1671 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001672 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1673 * @hide
1674 */
1675 private static final int HOVERED = 0x10000000;
1676
1677 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001678 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1679 * for transform operations
1680 *
1681 * @hide
1682 */
Adam Powellf37df072010-09-17 16:22:49 -07001683 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001684
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001685 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001686 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001687
Chet Haasefd2b0022010-08-06 13:08:56 -07001688 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001689 * Indicates that this view was specifically invalidated, not just dirtied because some
1690 * child view was invalidated. The flag is used to determine when we need to recreate
1691 * a view's display list (as opposed to just returning a reference to its existing
1692 * display list).
1693 *
1694 * @hide
1695 */
1696 static final int INVALIDATED = 0x80000000;
1697
Christopher Tate3d4bf172011-03-28 16:16:46 -07001698 /* Masks for mPrivateFlags2 */
1699
1700 /**
1701 * Indicates that this view has reported that it can accept the current drag's content.
1702 * Cleared when the drag operation concludes.
1703 * @hide
1704 */
1705 static final int DRAG_CAN_ACCEPT = 0x00000001;
1706
1707 /**
1708 * Indicates that this view is currently directly under the drag location in a
1709 * drag-and-drop operation involving content that it can accept. Cleared when
1710 * the drag exits the view, or when the drag operation concludes.
1711 * @hide
1712 */
1713 static final int DRAG_HOVERED = 0x00000002;
1714
Cibu Johny86666632010-02-22 13:01:02 -08001715 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001716 * Horizontal layout direction of this view is from Left to Right.
1717 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001718 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001719 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001720
1721 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001722 * Horizontal layout direction of this view is from Right to Left.
1723 * Use with {@link #setLayoutDirection}.
1724 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001725 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001726
1727 /**
1728 * Horizontal layout direction of this view is inherited from its parent.
1729 * Use with {@link #setLayoutDirection}.
1730 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001731 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001732
1733 /**
1734 * Horizontal layout direction of this view is from deduced from the default language
1735 * script for the locale. Use with {@link #setLayoutDirection}.
1736 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001737 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001738
1739 /**
1740 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001741 * @hide
1742 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001743 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1744
1745 /**
1746 * Mask for use with private flags indicating bits used for horizontal layout direction.
1747 * @hide
1748 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001749 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001750
1751 /**
1752 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1753 * right-to-left direction.
1754 * @hide
1755 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001756 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001757
1758 /**
1759 * Indicates whether the view horizontal layout direction has been resolved.
1760 * @hide
1761 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001762 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001763
1764 /**
1765 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1766 * @hide
1767 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001768 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001769
1770 /*
1771 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1772 * flag value.
1773 * @hide
1774 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001775 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1776 LAYOUT_DIRECTION_LTR,
1777 LAYOUT_DIRECTION_RTL,
1778 LAYOUT_DIRECTION_INHERIT,
1779 LAYOUT_DIRECTION_LOCALE
1780 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001781
1782 /**
1783 * Default horizontal layout direction.
1784 * @hide
1785 */
1786 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001787
Cibu Johny86666632010-02-22 13:01:02 -08001788
Adam Powell539ee872012-02-03 19:00:49 -08001789 /**
1790 * Indicates that the view is tracking some sort of transient state
1791 * that the app should not need to be aware of, but that the framework
1792 * should take special care to preserve.
1793 *
1794 * @hide
1795 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001796 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001797
1798
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 /**
1800 * Text direction is inherited thru {@link ViewGroup}
1801 */
1802 public static final int TEXT_DIRECTION_INHERIT = 0;
1803
1804 /**
1805 * Text direction is using "first strong algorithm". The first strong directional character
1806 * determines the paragraph direction. If there is no strong directional character, the
1807 * paragraph direction is the view's resolved layout direction.
1808 */
1809 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1810
1811 /**
1812 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1813 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1814 * If there are neither, the paragraph direction is the view's resolved layout direction.
1815 */
1816 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1817
1818 /**
1819 * Text direction is forced to LTR.
1820 */
1821 public static final int TEXT_DIRECTION_LTR = 3;
1822
1823 /**
1824 * Text direction is forced to RTL.
1825 */
1826 public static final int TEXT_DIRECTION_RTL = 4;
1827
1828 /**
1829 * Text direction is coming from the system Locale.
1830 */
1831 public static final int TEXT_DIRECTION_LOCALE = 5;
1832
1833 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001834 * Default text direction is inherited
1835 */
1836 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1837
1838 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001839 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1840 * @hide
1841 */
1842 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1843
1844 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001845 * Mask for use with private flags indicating bits used for text direction.
1846 * @hide
1847 */
1848 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1849
1850 /**
1851 * Array of text direction flags for mapping attribute "textDirection" to correct
1852 * flag value.
1853 * @hide
1854 */
1855 private static final int[] TEXT_DIRECTION_FLAGS = {
1856 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1857 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1858 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1859 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1860 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1861 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1862 };
1863
1864 /**
1865 * Indicates whether the view text direction has been resolved.
1866 * @hide
1867 */
1868 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1869
1870 /**
1871 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1872 * @hide
1873 */
1874 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1875
1876 /**
1877 * Mask for use with private flags indicating bits used for resolved text direction.
1878 * @hide
1879 */
1880 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1881
1882 /**
1883 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1884 * @hide
1885 */
1886 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1887 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1888
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001889 /*
1890 * Default text alignment. The text alignment of this View is inherited from its parent.
1891 * Use with {@link #setTextAlignment(int)}
1892 */
1893 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1894
1895 /**
1896 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1897 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1898 *
1899 * Use with {@link #setTextAlignment(int)}
1900 */
1901 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1902
1903 /**
1904 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1905 *
1906 * Use with {@link #setTextAlignment(int)}
1907 */
1908 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1909
1910 /**
1911 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1912 *
1913 * Use with {@link #setTextAlignment(int)}
1914 */
1915 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1916
1917 /**
1918 * Center the paragraph, e.g. ALIGN_CENTER.
1919 *
1920 * Use with {@link #setTextAlignment(int)}
1921 */
1922 public static final int TEXT_ALIGNMENT_CENTER = 4;
1923
1924 /**
1925 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1926 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1927 *
1928 * Use with {@link #setTextAlignment(int)}
1929 */
1930 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1931
1932 /**
1933 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1934 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1935 *
1936 * Use with {@link #setTextAlignment(int)}
1937 */
1938 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1939
1940 /**
1941 * Default text alignment is inherited
1942 */
1943 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1944
1945 /**
1946 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1947 * @hide
1948 */
1949 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1950
1951 /**
1952 * Mask for use with private flags indicating bits used for text alignment.
1953 * @hide
1954 */
1955 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1956
1957 /**
1958 * Array of text direction flags for mapping attribute "textAlignment" to correct
1959 * flag value.
1960 * @hide
1961 */
1962 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1963 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1964 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1965 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1966 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1967 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1968 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1969 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
1970 };
1971
1972 /**
1973 * Indicates whether the view text alignment has been resolved.
1974 * @hide
1975 */
1976 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
1977
1978 /**
1979 * Bit shift to get the resolved text alignment.
1980 * @hide
1981 */
1982 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
1983
1984 /**
1985 * Mask for use with private flags indicating bits used for text alignment.
1986 * @hide
1987 */
1988 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
1989
1990 /**
1991 * Indicates whether if the view text alignment has been resolved to gravity
1992 */
1993 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
1994 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
1995
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001996
Christopher Tate3d4bf172011-03-28 16:16:46 -07001997 /* End of masks for mPrivateFlags2 */
1998
1999 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2000
Chet Haasedaf98e92011-01-10 14:10:36 -08002001 /**
Adam Powell637d3372010-08-25 14:37:03 -07002002 * Always allow a user to over-scroll this view, provided it is a
2003 * view that can scroll.
2004 *
2005 * @see #getOverScrollMode()
2006 * @see #setOverScrollMode(int)
2007 */
2008 public static final int OVER_SCROLL_ALWAYS = 0;
2009
2010 /**
2011 * Allow a user to over-scroll this view only if the content is large
2012 * enough to meaningfully scroll, provided it is a view that can scroll.
2013 *
2014 * @see #getOverScrollMode()
2015 * @see #setOverScrollMode(int)
2016 */
2017 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2018
2019 /**
2020 * Never allow a user to over-scroll this view.
2021 *
2022 * @see #getOverScrollMode()
2023 * @see #setOverScrollMode(int)
2024 */
2025 public static final int OVER_SCROLL_NEVER = 2;
2026
2027 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002028 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2029 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002030 *
Joe Malin32736f02011-01-19 16:14:20 -08002031 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002032 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002033 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002034
2035 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002036 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2037 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002038 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002039 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002040 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002041 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002042 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002043 *
Joe Malin32736f02011-01-19 16:14:20 -08002044 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002045 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002046 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2047
2048 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002049 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2050 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002051 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002052 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002053 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2054 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2055 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002056 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002057 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2058 * window flags) for displaying content using every last pixel on the display.
2059 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002060 * <p>There is a limitation: because navigation controls are so important, the least user
2061 * interaction will cause them to reappear immediately. When this happens, both
2062 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2063 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002064 *
2065 * @see #setSystemUiVisibility(int)
2066 */
2067 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2068
2069 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002070 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2071 * into the normal fullscreen mode so that its content can take over the screen
2072 * while still allowing the user to interact with the application.
2073 *
2074 * <p>This has the same visual effect as
2075 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2076 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2077 * meaning that non-critical screen decorations (such as the status bar) will be
2078 * hidden while the user is in the View's window, focusing the experience on
2079 * that content. Unlike the window flag, if you are using ActionBar in
2080 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2081 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2082 * hide the action bar.
2083 *
2084 * <p>This approach to going fullscreen is best used over the window flag when
2085 * it is a transient state -- that is, the application does this at certain
2086 * points in its user interaction where it wants to allow the user to focus
2087 * on content, but not as a continuous state. For situations where the application
2088 * would like to simply stay full screen the entire time (such as a game that
2089 * wants to take over the screen), the
2090 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2091 * is usually a better approach. The state set here will be removed by the system
2092 * in various situations (such as the user moving to another application) like
2093 * the other system UI states.
2094 *
2095 * <p>When using this flag, the application should provide some easy facility
2096 * for the user to go out of it. A common example would be in an e-book
2097 * reader, where tapping on the screen brings back whatever screen and UI
2098 * decorations that had been hidden while the user was immersed in reading
2099 * the book.
2100 *
2101 * @see #setSystemUiVisibility(int)
2102 */
2103 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2104
2105 /**
2106 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2107 * flags, we would like a stable view of the content insets given to
2108 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2109 * will always represent the worst case that the application can expect
2110 * as a continue state. In practice this means with any of system bar,
2111 * nav bar, and status bar shown, but not the space that would be needed
2112 * for an input method.
2113 *
2114 * <p>If you are using ActionBar in
2115 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2116 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2117 * insets it adds to those given to the application.
2118 */
2119 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2120
2121 /**
2122 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2123 * to be layed out as if it has requested
2124 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2125 * allows it to avoid artifacts when switching in and out of that mode, at
2126 * the expense that some of its user interface may be covered by screen
2127 * decorations when they are shown. You can perform layout of your inner
2128 * UI elements to account for the navagation system UI through the
2129 * {@link #fitSystemWindows(Rect)} method.
2130 */
2131 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2132
2133 /**
2134 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2135 * to be layed out as if it has requested
2136 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2137 * allows it to avoid artifacts when switching in and out of that mode, at
2138 * the expense that some of its user interface may be covered by screen
2139 * decorations when they are shown. You can perform layout of your inner
2140 * UI elements to account for non-fullscreen system UI through the
2141 * {@link #fitSystemWindows(Rect)} method.
2142 */
2143 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2144
2145 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002146 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2147 */
2148 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2149
2150 /**
2151 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2152 */
2153 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002154
2155 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002156 * @hide
2157 *
2158 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2159 * out of the public fields to keep the undefined bits out of the developer's way.
2160 *
2161 * Flag to make the status bar not expandable. Unless you also
2162 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2163 */
2164 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2165
2166 /**
2167 * @hide
2168 *
2169 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2170 * out of the public fields to keep the undefined bits out of the developer's way.
2171 *
2172 * Flag to hide notification icons and scrolling ticker text.
2173 */
2174 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2175
2176 /**
2177 * @hide
2178 *
2179 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2180 * out of the public fields to keep the undefined bits out of the developer's way.
2181 *
2182 * Flag to disable incoming notification alerts. This will not block
2183 * icons, but it will block sound, vibrating and other visual or aural notifications.
2184 */
2185 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2186
2187 /**
2188 * @hide
2189 *
2190 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2191 * out of the public fields to keep the undefined bits out of the developer's way.
2192 *
2193 * Flag to hide only the scrolling ticker. Note that
2194 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2195 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2196 */
2197 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2198
2199 /**
2200 * @hide
2201 *
2202 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2203 * out of the public fields to keep the undefined bits out of the developer's way.
2204 *
2205 * Flag to hide the center system info area.
2206 */
2207 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2208
2209 /**
2210 * @hide
2211 *
2212 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2213 * out of the public fields to keep the undefined bits out of the developer's way.
2214 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002215 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002216 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2217 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002218 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002219
2220 /**
2221 * @hide
2222 *
2223 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2224 * out of the public fields to keep the undefined bits out of the developer's way.
2225 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002226 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002227 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2228 */
2229 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2230
2231 /**
2232 * @hide
2233 *
2234 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2235 * out of the public fields to keep the undefined bits out of the developer's way.
2236 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002237 * Flag to hide only the clock. You might use this if your activity has
2238 * its own clock making the status bar's clock redundant.
2239 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002240 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2241
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002242 /**
2243 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002244 *
2245 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2246 * out of the public fields to keep the undefined bits out of the developer's way.
2247 *
2248 * Flag to hide only the recent apps button. Don't use this
2249 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2250 */
2251 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2252
2253 /**
2254 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002255 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002256 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002257
2258 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002259 * These are the system UI flags that can be cleared by events outside
2260 * of an application. Currently this is just the ability to tap on the
2261 * screen while hiding the navigation bar to have it return.
2262 * @hide
2263 */
2264 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002265 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2266 | SYSTEM_UI_FLAG_FULLSCREEN;
2267
2268 /**
2269 * Flags that can impact the layout in relation to system UI.
2270 */
2271 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2272 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2273 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002274
2275 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002276 * Find views that render the specified text.
2277 *
2278 * @see #findViewsWithText(ArrayList, CharSequence, int)
2279 */
2280 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2281
2282 /**
2283 * Find find views that contain the specified content description.
2284 *
2285 * @see #findViewsWithText(ArrayList, CharSequence, int)
2286 */
2287 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2288
2289 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002290 * Find views that contain {@link AccessibilityNodeProvider}. Such
2291 * a View is a root of virtual view hierarchy and may contain the searched
2292 * text. If this flag is set Views with providers are automatically
2293 * added and it is a responsibility of the client to call the APIs of
2294 * the provider to determine whether the virtual tree rooted at this View
2295 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2296 * represeting the virtual views with this text.
2297 *
2298 * @see #findViewsWithText(ArrayList, CharSequence, int)
2299 *
2300 * @hide
2301 */
2302 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2303
2304 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002305 * Indicates that the screen has changed state and is now off.
2306 *
2307 * @see #onScreenStateChanged(int)
2308 */
2309 public static final int SCREEN_STATE_OFF = 0x0;
2310
2311 /**
2312 * Indicates that the screen has changed state and is now on.
2313 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002314 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002315 */
2316 public static final int SCREEN_STATE_ON = 0x1;
2317
2318 /**
Adam Powell637d3372010-08-25 14:37:03 -07002319 * Controls the over-scroll mode for this view.
2320 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2321 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2322 * and {@link #OVER_SCROLL_NEVER}.
2323 */
2324 private int mOverScrollMode;
2325
2326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 * The parent this view is attached to.
2328 * {@hide}
2329 *
2330 * @see #getParent()
2331 */
2332 protected ViewParent mParent;
2333
2334 /**
2335 * {@hide}
2336 */
2337 AttachInfo mAttachInfo;
2338
2339 /**
2340 * {@hide}
2341 */
Romain Guy809a7f62009-05-14 15:44:42 -07002342 @ViewDebug.ExportedProperty(flagMapping = {
2343 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2344 name = "FORCE_LAYOUT"),
2345 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2346 name = "LAYOUT_REQUIRED"),
2347 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002348 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002349 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2350 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2351 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2352 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2353 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002355 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356
2357 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002358 * This view's request for the visibility of the status bar.
2359 * @hide
2360 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002361 @ViewDebug.ExportedProperty(flagMapping = {
2362 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2363 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2364 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2365 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2366 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2367 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2368 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2369 equals = SYSTEM_UI_FLAG_VISIBLE,
2370 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2371 })
Joe Onorato664644d2011-01-23 17:53:23 -08002372 int mSystemUiVisibility;
2373
2374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 * Count of how many windows this view has been attached to.
2376 */
2377 int mWindowAttachCount;
2378
2379 /**
2380 * The layout parameters associated with this view and used by the parent
2381 * {@link android.view.ViewGroup} to determine how this view should be
2382 * laid out.
2383 * {@hide}
2384 */
2385 protected ViewGroup.LayoutParams mLayoutParams;
2386
2387 /**
2388 * The view flags hold various views states.
2389 * {@hide}
2390 */
2391 @ViewDebug.ExportedProperty
2392 int mViewFlags;
2393
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002394 static class TransformationInfo {
2395 /**
2396 * The transform matrix for the View. This transform is calculated internally
2397 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2398 * is used by default. Do *not* use this variable directly; instead call
2399 * getMatrix(), which will automatically recalculate the matrix if necessary
2400 * to get the correct matrix based on the latest rotation and scale properties.
2401 */
2402 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002403
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002404 /**
2405 * The transform matrix for the View. This transform is calculated internally
2406 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2407 * is used by default. Do *not* use this variable directly; instead call
2408 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2409 * to get the correct matrix based on the latest rotation and scale properties.
2410 */
2411 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002412
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002413 /**
2414 * An internal variable that tracks whether we need to recalculate the
2415 * transform matrix, based on whether the rotation or scaleX/Y properties
2416 * have changed since the matrix was last calculated.
2417 */
2418 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002419
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002420 /**
2421 * An internal variable that tracks whether we need to recalculate the
2422 * transform matrix, based on whether the rotation or scaleX/Y properties
2423 * have changed since the matrix was last calculated.
2424 */
2425 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002426
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002427 /**
2428 * A variable that tracks whether we need to recalculate the
2429 * transform matrix, based on whether the rotation or scaleX/Y properties
2430 * have changed since the matrix was last calculated. This variable
2431 * is only valid after a call to updateMatrix() or to a function that
2432 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2433 */
2434 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002435
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002436 /**
2437 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2438 */
2439 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002440
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002441 /**
2442 * This matrix is used when computing the matrix for 3D rotations.
2443 */
2444 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002445
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002446 /**
2447 * These prev values are used to recalculate a centered pivot point when necessary. The
2448 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2449 * set), so thes values are only used then as well.
2450 */
2451 private int mPrevWidth = -1;
2452 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002453
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002454 /**
2455 * The degrees rotation around the vertical axis through the pivot point.
2456 */
2457 @ViewDebug.ExportedProperty
2458 float mRotationY = 0f;
2459
2460 /**
2461 * The degrees rotation around the horizontal axis through the pivot point.
2462 */
2463 @ViewDebug.ExportedProperty
2464 float mRotationX = 0f;
2465
2466 /**
2467 * The degrees rotation around the pivot point.
2468 */
2469 @ViewDebug.ExportedProperty
2470 float mRotation = 0f;
2471
2472 /**
2473 * The amount of translation of the object away from its left property (post-layout).
2474 */
2475 @ViewDebug.ExportedProperty
2476 float mTranslationX = 0f;
2477
2478 /**
2479 * The amount of translation of the object away from its top property (post-layout).
2480 */
2481 @ViewDebug.ExportedProperty
2482 float mTranslationY = 0f;
2483
2484 /**
2485 * The amount of scale in the x direction around the pivot point. A
2486 * value of 1 means no scaling is applied.
2487 */
2488 @ViewDebug.ExportedProperty
2489 float mScaleX = 1f;
2490
2491 /**
2492 * The amount of scale in the y direction around the pivot point. A
2493 * value of 1 means no scaling is applied.
2494 */
2495 @ViewDebug.ExportedProperty
2496 float mScaleY = 1f;
2497
2498 /**
Chet Haasea33de552012-02-03 16:28:24 -08002499 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002500 */
2501 @ViewDebug.ExportedProperty
2502 float mPivotX = 0f;
2503
2504 /**
Chet Haasea33de552012-02-03 16:28:24 -08002505 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002506 */
2507 @ViewDebug.ExportedProperty
2508 float mPivotY = 0f;
2509
2510 /**
2511 * The opacity of the View. This is a value from 0 to 1, where 0 means
2512 * completely transparent and 1 means completely opaque.
2513 */
2514 @ViewDebug.ExportedProperty
2515 float mAlpha = 1f;
2516 }
2517
2518 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002519
Joe Malin32736f02011-01-19 16:14:20 -08002520 private boolean mLastIsOpaque;
2521
Chet Haasefd2b0022010-08-06 13:08:56 -07002522 /**
2523 * Convenience value to check for float values that are close enough to zero to be considered
2524 * zero.
2525 */
Romain Guy2542d192010-08-18 11:47:12 -07002526 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002527
2528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 * The distance in pixels from the left edge of this view's parent
2530 * to the left edge of this view.
2531 * {@hide}
2532 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002533 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 protected int mLeft;
2535 /**
2536 * The distance in pixels from the left edge of this view's parent
2537 * to the right edge of this view.
2538 * {@hide}
2539 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002540 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 protected int mRight;
2542 /**
2543 * The distance in pixels from the top edge of this view's parent
2544 * to the top edge of this view.
2545 * {@hide}
2546 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002547 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 protected int mTop;
2549 /**
2550 * The distance in pixels from the top edge of this view's parent
2551 * to the bottom edge of this view.
2552 * {@hide}
2553 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002554 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 protected int mBottom;
2556
2557 /**
2558 * The offset, in pixels, by which the content of this view is scrolled
2559 * horizontally.
2560 * {@hide}
2561 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002562 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 protected int mScrollX;
2564 /**
2565 * The offset, in pixels, by which the content of this view is scrolled
2566 * vertically.
2567 * {@hide}
2568 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002569 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 protected int mScrollY;
2571
2572 /**
2573 * The left padding in pixels, that is the distance in pixels between the
2574 * left edge of this view and the left edge of its content.
2575 * {@hide}
2576 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002577 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 protected int mPaddingLeft;
2579 /**
2580 * The right padding in pixels, that is the distance in pixels between the
2581 * right edge of this view and the right edge of its content.
2582 * {@hide}
2583 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002584 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 protected int mPaddingRight;
2586 /**
2587 * The top padding in pixels, that is the distance in pixels between the
2588 * top edge of this view and the top edge of its content.
2589 * {@hide}
2590 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002591 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 protected int mPaddingTop;
2593 /**
2594 * The bottom padding in pixels, that is the distance in pixels between the
2595 * bottom edge of this view and the bottom edge of its content.
2596 * {@hide}
2597 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002598 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 protected int mPaddingBottom;
2600
2601 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002602 * The layout insets in pixels, that is the distance in pixels between the
2603 * visible edges of this view its bounds.
2604 */
2605 private Insets mLayoutInsets;
2606
2607 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002608 * Briefly describes the view and is primarily used for accessibility support.
2609 */
2610 private CharSequence mContentDescription;
2611
2612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002614 *
2615 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002617 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002618 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619
2620 /**
2621 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002622 *
2623 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002625 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002626 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002628 /**
Adam Powell20232d02010-12-08 21:08:53 -08002629 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002630 *
2631 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002632 */
2633 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002634 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002635
2636 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002637 * Cache if the user padding is relative.
2638 *
2639 */
2640 @ViewDebug.ExportedProperty(category = "padding")
2641 boolean mUserPaddingRelative;
2642
2643 /**
2644 * Cache the paddingStart set by the user to append to the scrollbar's size.
2645 *
2646 */
2647 @ViewDebug.ExportedProperty(category = "padding")
2648 int mUserPaddingStart;
2649
2650 /**
2651 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2652 *
2653 */
2654 @ViewDebug.ExportedProperty(category = "padding")
2655 int mUserPaddingEnd;
2656
2657 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002658 * @hide
2659 */
2660 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2661 /**
2662 * @hide
2663 */
2664 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665
Philip Milne6c8ea062012-04-03 17:38:43 -07002666 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667
2668 private int mBackgroundResource;
2669 private boolean mBackgroundSizeChanged;
2670
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002671 static class ListenerInfo {
2672 /**
2673 * Listener used to dispatch focus change events.
2674 * This field should be made private, so it is hidden from the SDK.
2675 * {@hide}
2676 */
2677 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002679 /**
2680 * Listeners for layout change events.
2681 */
2682 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002683
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002684 /**
2685 * Listeners for attach events.
2686 */
2687 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002688
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002689 /**
2690 * Listener used to dispatch click events.
2691 * This field should be made private, so it is hidden from the SDK.
2692 * {@hide}
2693 */
2694 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002696 /**
2697 * Listener used to dispatch long click events.
2698 * This field should be made private, so it is hidden from the SDK.
2699 * {@hide}
2700 */
2701 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002703 /**
2704 * Listener used to build the context menu.
2705 * This field should be made private, so it is hidden from the SDK.
2706 * {@hide}
2707 */
2708 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002710 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002712 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002714 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002715
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002716 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002717
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002718 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002719
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002720 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2721 }
2722
2723 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 /**
2726 * The application environment this view lives in.
2727 * This field should be made private, so it is hidden from the SDK.
2728 * {@hide}
2729 */
2730 protected Context mContext;
2731
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002732 private final Resources mResources;
2733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 private ScrollabilityCache mScrollCache;
2735
2736 private int[] mDrawableState = null;
2737
Romain Guy0211a0a2011-02-14 16:34:59 -08002738 /**
2739 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002740 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002741 * @hide
2742 */
2743 public boolean mCachingFailed;
2744
Romain Guy02890fd2010-08-06 17:58:44 -07002745 private Bitmap mDrawingCache;
2746 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002747 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002748 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749
2750 /**
2751 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2752 * the user may specify which view to go to next.
2753 */
2754 private int mNextFocusLeftId = View.NO_ID;
2755
2756 /**
2757 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2758 * the user may specify which view to go to next.
2759 */
2760 private int mNextFocusRightId = View.NO_ID;
2761
2762 /**
2763 * When this view has focus and the next focus is {@link #FOCUS_UP},
2764 * the user may specify which view to go to next.
2765 */
2766 private int mNextFocusUpId = View.NO_ID;
2767
2768 /**
2769 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2770 * the user may specify which view to go to next.
2771 */
2772 private int mNextFocusDownId = View.NO_ID;
2773
Jeff Brown4e6319b2010-12-13 10:36:51 -08002774 /**
2775 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2776 * the user may specify which view to go to next.
2777 */
2778 int mNextFocusForwardId = View.NO_ID;
2779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002781 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002782 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002783 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 private UnsetPressedState mUnsetPressedState;
2786
2787 /**
2788 * Whether the long press's action has been invoked. The tap's action is invoked on the
2789 * up event while a long press is invoked as soon as the long press duration is reached, so
2790 * a long press could be performed before the tap is checked, in which case the tap's action
2791 * should not be invoked.
2792 */
2793 private boolean mHasPerformedLongPress;
2794
2795 /**
2796 * The minimum height of the view. We'll try our best to have the height
2797 * of this view to at least this amount.
2798 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002799 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 private int mMinHeight;
2801
2802 /**
2803 * The minimum width of the view. We'll try our best to have the width
2804 * of this view to at least this amount.
2805 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002806 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 private int mMinWidth;
2808
2809 /**
2810 * The delegate to handle touch events that are physically in this view
2811 * but should be handled by another view.
2812 */
2813 private TouchDelegate mTouchDelegate = null;
2814
2815 /**
2816 * Solid color to use as a background when creating the drawing cache. Enables
2817 * the cache to use 16 bit bitmaps instead of 32 bit.
2818 */
2819 private int mDrawingCacheBackgroundColor = 0;
2820
2821 /**
2822 * Special tree observer used when mAttachInfo is null.
2823 */
2824 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002825
Adam Powelle14579b2009-12-16 18:39:52 -08002826 /**
2827 * Cache the touch slop from the context that created the view.
2828 */
2829 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002832 * Object that handles automatic animation of view properties.
2833 */
2834 private ViewPropertyAnimator mAnimator = null;
2835
2836 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002837 * Flag indicating that a drag can cross window boundaries. When
2838 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2839 * with this flag set, all visible applications will be able to participate
2840 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002841 *
2842 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002843 */
2844 public static final int DRAG_FLAG_GLOBAL = 1;
2845
2846 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002847 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2848 */
2849 private float mVerticalScrollFactor;
2850
2851 /**
Adam Powell20232d02010-12-08 21:08:53 -08002852 * Position of the vertical scroll bar.
2853 */
2854 private int mVerticalScrollbarPosition;
2855
2856 /**
2857 * Position the scroll bar at the default position as determined by the system.
2858 */
2859 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2860
2861 /**
2862 * Position the scroll bar along the left edge.
2863 */
2864 public static final int SCROLLBAR_POSITION_LEFT = 1;
2865
2866 /**
2867 * Position the scroll bar along the right edge.
2868 */
2869 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2870
2871 /**
Romain Guy171c5922011-01-06 10:04:23 -08002872 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002873 *
2874 * @see #getLayerType()
2875 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002876 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002877 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002878 */
2879 public static final int LAYER_TYPE_NONE = 0;
2880
2881 /**
2882 * <p>Indicates that the view has a software layer. A software layer is backed
2883 * by a bitmap and causes the view to be rendered using Android's software
2884 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002885 *
Romain Guy171c5922011-01-06 10:04:23 -08002886 * <p>Software layers have various usages:</p>
2887 * <p>When the application is not using hardware acceleration, a software layer
2888 * is useful to apply a specific color filter and/or blending mode and/or
2889 * translucency to a view and all its children.</p>
2890 * <p>When the application is using hardware acceleration, a software layer
2891 * is useful to render drawing primitives not supported by the hardware
2892 * accelerated pipeline. It can also be used to cache a complex view tree
2893 * into a texture and reduce the complexity of drawing operations. For instance,
2894 * when animating a complex view tree with a translation, a software layer can
2895 * be used to render the view tree only once.</p>
2896 * <p>Software layers should be avoided when the affected view tree updates
2897 * often. Every update will require to re-render the software layer, which can
2898 * potentially be slow (particularly when hardware acceleration is turned on
2899 * since the layer will have to be uploaded into a hardware texture after every
2900 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002901 *
2902 * @see #getLayerType()
2903 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002904 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002905 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002906 */
2907 public static final int LAYER_TYPE_SOFTWARE = 1;
2908
2909 /**
2910 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2911 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2912 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2913 * rendering pipeline, but only if hardware acceleration is turned on for the
2914 * view hierarchy. When hardware acceleration is turned off, hardware layers
2915 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002916 *
Romain Guy171c5922011-01-06 10:04:23 -08002917 * <p>A hardware layer is useful to apply a specific color filter and/or
2918 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002919 * <p>A hardware layer can be used to cache a complex view tree into a
2920 * texture and reduce the complexity of drawing operations. For instance,
2921 * when animating a complex view tree with a translation, a hardware layer can
2922 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002923 * <p>A hardware layer can also be used to increase the rendering quality when
2924 * rotation transformations are applied on a view. It can also be used to
2925 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002926 *
2927 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002928 * @see #setLayerType(int, android.graphics.Paint)
2929 * @see #LAYER_TYPE_NONE
2930 * @see #LAYER_TYPE_SOFTWARE
2931 */
2932 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002933
Romain Guy3aaff3a2011-01-12 14:18:47 -08002934 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2935 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2936 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2937 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2938 })
Romain Guy171c5922011-01-06 10:04:23 -08002939 int mLayerType = LAYER_TYPE_NONE;
2940 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002941 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002942
2943 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002944 * Set to true when the view is sending hover accessibility events because it
2945 * is the innermost hovered view.
2946 */
2947 private boolean mSendingHoverAccessibilityEvents;
2948
2949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 * Simple constructor to use when creating a view from code.
2951 *
2952 * @param context The Context the view is running in, through which it can
2953 * access the current theme, resources, etc.
2954 */
2955 public View(Context context) {
2956 mContext = context;
2957 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002958 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002959 // Set layout and text direction defaults
2960 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002961 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
2962 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08002963 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002964 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002965 mUserPaddingStart = -1;
2966 mUserPaddingEnd = -1;
2967 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969
2970 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07002971 * Delegate for injecting accessiblity functionality.
2972 */
2973 AccessibilityDelegate mAccessibilityDelegate;
2974
2975 /**
2976 * Consistency verifier for debugging purposes.
2977 * @hide
2978 */
2979 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2980 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2981 new InputEventConsistencyVerifier(this, 0) : null;
2982
2983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 * Constructor that is called when inflating a view from XML. This is called
2985 * when a view is being constructed from an XML file, supplying attributes
2986 * that were specified in the XML file. This version uses a default style of
2987 * 0, so the only attribute values applied are those in the Context's Theme
2988 * and the given AttributeSet.
2989 *
2990 * <p>
2991 * The method onFinishInflate() will be called after all children have been
2992 * added.
2993 *
2994 * @param context The Context the view is running in, through which it can
2995 * access the current theme, resources, etc.
2996 * @param attrs The attributes of the XML tag that is inflating the view.
2997 * @see #View(Context, AttributeSet, int)
2998 */
2999 public View(Context context, AttributeSet attrs) {
3000 this(context, attrs, 0);
3001 }
3002
3003 /**
3004 * Perform inflation from XML and apply a class-specific base style. This
3005 * constructor of View allows subclasses to use their own base style when
3006 * they are inflating. For example, a Button class's constructor would call
3007 * this version of the super class constructor and supply
3008 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3009 * the theme's button style to modify all of the base view attributes (in
3010 * particular its background) as well as the Button class's attributes.
3011 *
3012 * @param context The Context the view is running in, through which it can
3013 * access the current theme, resources, etc.
3014 * @param attrs The attributes of the XML tag that is inflating the view.
3015 * @param defStyle The default style to apply to this view. If 0, no style
3016 * will be applied (beyond what is included in the theme). This may
3017 * either be an attribute resource, whose value will be retrieved
3018 * from the current theme, or an explicit style resource.
3019 * @see #View(Context, AttributeSet)
3020 */
3021 public View(Context context, AttributeSet attrs, int defStyle) {
3022 this(context);
3023
3024 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3025 defStyle, 0);
3026
3027 Drawable background = null;
3028
3029 int leftPadding = -1;
3030 int topPadding = -1;
3031 int rightPadding = -1;
3032 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003033 int startPadding = -1;
3034 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035
3036 int padding = -1;
3037
3038 int viewFlagValues = 0;
3039 int viewFlagMasks = 0;
3040
3041 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 int x = 0;
3044 int y = 0;
3045
Chet Haase73066682010-11-29 15:55:32 -08003046 float tx = 0;
3047 float ty = 0;
3048 float rotation = 0;
3049 float rotationX = 0;
3050 float rotationY = 0;
3051 float sx = 1f;
3052 float sy = 1f;
3053 boolean transformSet = false;
3054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3056
Adam Powell637d3372010-08-25 14:37:03 -07003057 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 final int N = a.getIndexCount();
3059 for (int i = 0; i < N; i++) {
3060 int attr = a.getIndex(i);
3061 switch (attr) {
3062 case com.android.internal.R.styleable.View_background:
3063 background = a.getDrawable(attr);
3064 break;
3065 case com.android.internal.R.styleable.View_padding:
3066 padding = a.getDimensionPixelSize(attr, -1);
3067 break;
3068 case com.android.internal.R.styleable.View_paddingLeft:
3069 leftPadding = a.getDimensionPixelSize(attr, -1);
3070 break;
3071 case com.android.internal.R.styleable.View_paddingTop:
3072 topPadding = a.getDimensionPixelSize(attr, -1);
3073 break;
3074 case com.android.internal.R.styleable.View_paddingRight:
3075 rightPadding = a.getDimensionPixelSize(attr, -1);
3076 break;
3077 case com.android.internal.R.styleable.View_paddingBottom:
3078 bottomPadding = a.getDimensionPixelSize(attr, -1);
3079 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003080 case com.android.internal.R.styleable.View_paddingStart:
3081 startPadding = a.getDimensionPixelSize(attr, -1);
3082 break;
3083 case com.android.internal.R.styleable.View_paddingEnd:
3084 endPadding = a.getDimensionPixelSize(attr, -1);
3085 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 case com.android.internal.R.styleable.View_scrollX:
3087 x = a.getDimensionPixelOffset(attr, 0);
3088 break;
3089 case com.android.internal.R.styleable.View_scrollY:
3090 y = a.getDimensionPixelOffset(attr, 0);
3091 break;
Chet Haase73066682010-11-29 15:55:32 -08003092 case com.android.internal.R.styleable.View_alpha:
3093 setAlpha(a.getFloat(attr, 1f));
3094 break;
3095 case com.android.internal.R.styleable.View_transformPivotX:
3096 setPivotX(a.getDimensionPixelOffset(attr, 0));
3097 break;
3098 case com.android.internal.R.styleable.View_transformPivotY:
3099 setPivotY(a.getDimensionPixelOffset(attr, 0));
3100 break;
3101 case com.android.internal.R.styleable.View_translationX:
3102 tx = a.getDimensionPixelOffset(attr, 0);
3103 transformSet = true;
3104 break;
3105 case com.android.internal.R.styleable.View_translationY:
3106 ty = a.getDimensionPixelOffset(attr, 0);
3107 transformSet = true;
3108 break;
3109 case com.android.internal.R.styleable.View_rotation:
3110 rotation = a.getFloat(attr, 0);
3111 transformSet = true;
3112 break;
3113 case com.android.internal.R.styleable.View_rotationX:
3114 rotationX = a.getFloat(attr, 0);
3115 transformSet = true;
3116 break;
3117 case com.android.internal.R.styleable.View_rotationY:
3118 rotationY = a.getFloat(attr, 0);
3119 transformSet = true;
3120 break;
3121 case com.android.internal.R.styleable.View_scaleX:
3122 sx = a.getFloat(attr, 1f);
3123 transformSet = true;
3124 break;
3125 case com.android.internal.R.styleable.View_scaleY:
3126 sy = a.getFloat(attr, 1f);
3127 transformSet = true;
3128 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 case com.android.internal.R.styleable.View_id:
3130 mID = a.getResourceId(attr, NO_ID);
3131 break;
3132 case com.android.internal.R.styleable.View_tag:
3133 mTag = a.getText(attr);
3134 break;
3135 case com.android.internal.R.styleable.View_fitsSystemWindows:
3136 if (a.getBoolean(attr, false)) {
3137 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3138 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3139 }
3140 break;
3141 case com.android.internal.R.styleable.View_focusable:
3142 if (a.getBoolean(attr, false)) {
3143 viewFlagValues |= FOCUSABLE;
3144 viewFlagMasks |= FOCUSABLE_MASK;
3145 }
3146 break;
3147 case com.android.internal.R.styleable.View_focusableInTouchMode:
3148 if (a.getBoolean(attr, false)) {
3149 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3150 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3151 }
3152 break;
3153 case com.android.internal.R.styleable.View_clickable:
3154 if (a.getBoolean(attr, false)) {
3155 viewFlagValues |= CLICKABLE;
3156 viewFlagMasks |= CLICKABLE;
3157 }
3158 break;
3159 case com.android.internal.R.styleable.View_longClickable:
3160 if (a.getBoolean(attr, false)) {
3161 viewFlagValues |= LONG_CLICKABLE;
3162 viewFlagMasks |= LONG_CLICKABLE;
3163 }
3164 break;
3165 case com.android.internal.R.styleable.View_saveEnabled:
3166 if (!a.getBoolean(attr, true)) {
3167 viewFlagValues |= SAVE_DISABLED;
3168 viewFlagMasks |= SAVE_DISABLED_MASK;
3169 }
3170 break;
3171 case com.android.internal.R.styleable.View_duplicateParentState:
3172 if (a.getBoolean(attr, false)) {
3173 viewFlagValues |= DUPLICATE_PARENT_STATE;
3174 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3175 }
3176 break;
3177 case com.android.internal.R.styleable.View_visibility:
3178 final int visibility = a.getInt(attr, 0);
3179 if (visibility != 0) {
3180 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3181 viewFlagMasks |= VISIBILITY_MASK;
3182 }
3183 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003184 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003185 // Clear any layout direction flags (included resolved bits) already set
3186 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3187 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003188 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003189 final int value = (layoutDirection != -1) ?
3190 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3191 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003192 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 case com.android.internal.R.styleable.View_drawingCacheQuality:
3194 final int cacheQuality = a.getInt(attr, 0);
3195 if (cacheQuality != 0) {
3196 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3197 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3198 }
3199 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003200 case com.android.internal.R.styleable.View_contentDescription:
3201 mContentDescription = a.getString(attr);
3202 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3204 if (!a.getBoolean(attr, true)) {
3205 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3206 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3207 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003208 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3210 if (!a.getBoolean(attr, true)) {
3211 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3212 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3213 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003214 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 case R.styleable.View_scrollbars:
3216 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3217 if (scrollbars != SCROLLBARS_NONE) {
3218 viewFlagValues |= scrollbars;
3219 viewFlagMasks |= SCROLLBARS_MASK;
3220 initializeScrollbars(a);
3221 }
3222 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003223 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003225 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3226 // Ignore the attribute starting with ICS
3227 break;
3228 }
3229 // With builds < ICS, fall through and apply fading edges
3230 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3232 if (fadingEdge != FADING_EDGE_NONE) {
3233 viewFlagValues |= fadingEdge;
3234 viewFlagMasks |= FADING_EDGE_MASK;
3235 initializeFadingEdge(a);
3236 }
3237 break;
3238 case R.styleable.View_scrollbarStyle:
3239 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3240 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3241 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3242 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3243 }
3244 break;
3245 case R.styleable.View_isScrollContainer:
3246 setScrollContainer = true;
3247 if (a.getBoolean(attr, false)) {
3248 setScrollContainer(true);
3249 }
3250 break;
3251 case com.android.internal.R.styleable.View_keepScreenOn:
3252 if (a.getBoolean(attr, false)) {
3253 viewFlagValues |= KEEP_SCREEN_ON;
3254 viewFlagMasks |= KEEP_SCREEN_ON;
3255 }
3256 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003257 case R.styleable.View_filterTouchesWhenObscured:
3258 if (a.getBoolean(attr, false)) {
3259 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3260 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3261 }
3262 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 case R.styleable.View_nextFocusLeft:
3264 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3265 break;
3266 case R.styleable.View_nextFocusRight:
3267 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3268 break;
3269 case R.styleable.View_nextFocusUp:
3270 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3271 break;
3272 case R.styleable.View_nextFocusDown:
3273 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3274 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003275 case R.styleable.View_nextFocusForward:
3276 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3277 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 case R.styleable.View_minWidth:
3279 mMinWidth = a.getDimensionPixelSize(attr, 0);
3280 break;
3281 case R.styleable.View_minHeight:
3282 mMinHeight = a.getDimensionPixelSize(attr, 0);
3283 break;
Romain Guy9a817362009-05-01 10:57:14 -07003284 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003285 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003286 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003287 + "be used within a restricted context");
3288 }
3289
Romain Guy9a817362009-05-01 10:57:14 -07003290 final String handlerName = a.getString(attr);
3291 if (handlerName != null) {
3292 setOnClickListener(new OnClickListener() {
3293 private Method mHandler;
3294
3295 public void onClick(View v) {
3296 if (mHandler == null) {
3297 try {
3298 mHandler = getContext().getClass().getMethod(handlerName,
3299 View.class);
3300 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003301 int id = getId();
3302 String idText = id == NO_ID ? "" : " with id '"
3303 + getContext().getResources().getResourceEntryName(
3304 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003305 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003306 handlerName + "(View) in the activity "
3307 + getContext().getClass() + " for onClick handler"
3308 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003309 }
3310 }
3311
3312 try {
3313 mHandler.invoke(getContext(), View.this);
3314 } catch (IllegalAccessException e) {
3315 throw new IllegalStateException("Could not execute non "
3316 + "public method of the activity", e);
3317 } catch (InvocationTargetException e) {
3318 throw new IllegalStateException("Could not execute "
3319 + "method of the activity", e);
3320 }
3321 }
3322 });
3323 }
3324 break;
Adam Powell637d3372010-08-25 14:37:03 -07003325 case R.styleable.View_overScrollMode:
3326 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3327 break;
Adam Powell20232d02010-12-08 21:08:53 -08003328 case R.styleable.View_verticalScrollbarPosition:
3329 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3330 break;
Romain Guy171c5922011-01-06 10:04:23 -08003331 case R.styleable.View_layerType:
3332 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3333 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003334 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003335 // Clear any text direction flag already set
3336 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3337 // Set the text direction flags depending on the value of the attribute
3338 final int textDirection = a.getInt(attr, -1);
3339 if (textDirection != -1) {
3340 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3341 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003342 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003343 case R.styleable.View_textAlignment:
3344 // Clear any text alignment flag already set
3345 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3346 // Set the text alignment flag depending on the value of the attribute
3347 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3348 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3349 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351 }
3352
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003353 a.recycle();
3354
Adam Powell637d3372010-08-25 14:37:03 -07003355 setOverScrollMode(overScrollMode);
3356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003358 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 }
3360
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003361 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3362 // layout direction). Those cached values will be used later during padding resolution.
3363 mUserPaddingStart = startPadding;
3364 mUserPaddingEnd = endPadding;
3365
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003366 updateUserPaddingRelative();
3367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 if (padding >= 0) {
3369 leftPadding = padding;
3370 topPadding = padding;
3371 rightPadding = padding;
3372 bottomPadding = padding;
3373 }
3374
3375 // If the user specified the padding (either with android:padding or
3376 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3377 // use the default padding or the padding from the background drawable
3378 // (stored at this point in mPadding*)
3379 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3380 topPadding >= 0 ? topPadding : mPaddingTop,
3381 rightPadding >= 0 ? rightPadding : mPaddingRight,
3382 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3383
3384 if (viewFlagMasks != 0) {
3385 setFlags(viewFlagValues, viewFlagMasks);
3386 }
3387
3388 // Needs to be called after mViewFlags is set
3389 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3390 recomputePadding();
3391 }
3392
3393 if (x != 0 || y != 0) {
3394 scrollTo(x, y);
3395 }
3396
Chet Haase73066682010-11-29 15:55:32 -08003397 if (transformSet) {
3398 setTranslationX(tx);
3399 setTranslationY(ty);
3400 setRotation(rotation);
3401 setRotationX(rotationX);
3402 setRotationY(rotationY);
3403 setScaleX(sx);
3404 setScaleY(sy);
3405 }
3406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3408 setScrollContainer(true);
3409 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003410
3411 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 }
3413
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003414 private void updateUserPaddingRelative() {
3415 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 /**
3419 * Non-public constructor for use in testing
3420 */
3421 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003422 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 }
3424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 /**
3426 * <p>
3427 * Initializes the fading edges from a given set of styled attributes. This
3428 * method should be called by subclasses that need fading edges and when an
3429 * instance of these subclasses is created programmatically rather than
3430 * being inflated from XML. This method is automatically called when the XML
3431 * is inflated.
3432 * </p>
3433 *
3434 * @param a the styled attributes set to initialize the fading edges from
3435 */
3436 protected void initializeFadingEdge(TypedArray a) {
3437 initScrollCache();
3438
3439 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3440 R.styleable.View_fadingEdgeLength,
3441 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3442 }
3443
3444 /**
3445 * Returns the size of the vertical faded edges used to indicate that more
3446 * content in this view is visible.
3447 *
3448 * @return The size in pixels of the vertical faded edge or 0 if vertical
3449 * faded edges are not enabled for this view.
3450 * @attr ref android.R.styleable#View_fadingEdgeLength
3451 */
3452 public int getVerticalFadingEdgeLength() {
3453 if (isVerticalFadingEdgeEnabled()) {
3454 ScrollabilityCache cache = mScrollCache;
3455 if (cache != null) {
3456 return cache.fadingEdgeLength;
3457 }
3458 }
3459 return 0;
3460 }
3461
3462 /**
3463 * Set the size of the faded edge used to indicate that more content in this
3464 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003465 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3466 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3467 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 *
3469 * @param length The size in pixels of the faded edge used to indicate that more
3470 * content in this view is visible.
3471 */
3472 public void setFadingEdgeLength(int length) {
3473 initScrollCache();
3474 mScrollCache.fadingEdgeLength = length;
3475 }
3476
3477 /**
3478 * Returns the size of the horizontal faded edges used to indicate that more
3479 * content in this view is visible.
3480 *
3481 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3482 * faded edges are not enabled for this view.
3483 * @attr ref android.R.styleable#View_fadingEdgeLength
3484 */
3485 public int getHorizontalFadingEdgeLength() {
3486 if (isHorizontalFadingEdgeEnabled()) {
3487 ScrollabilityCache cache = mScrollCache;
3488 if (cache != null) {
3489 return cache.fadingEdgeLength;
3490 }
3491 }
3492 return 0;
3493 }
3494
3495 /**
3496 * Returns the width of the vertical scrollbar.
3497 *
3498 * @return The width in pixels of the vertical scrollbar or 0 if there
3499 * is no vertical scrollbar.
3500 */
3501 public int getVerticalScrollbarWidth() {
3502 ScrollabilityCache cache = mScrollCache;
3503 if (cache != null) {
3504 ScrollBarDrawable scrollBar = cache.scrollBar;
3505 if (scrollBar != null) {
3506 int size = scrollBar.getSize(true);
3507 if (size <= 0) {
3508 size = cache.scrollBarSize;
3509 }
3510 return size;
3511 }
3512 return 0;
3513 }
3514 return 0;
3515 }
3516
3517 /**
3518 * Returns the height of the horizontal scrollbar.
3519 *
3520 * @return The height in pixels of the horizontal scrollbar or 0 if
3521 * there is no horizontal scrollbar.
3522 */
3523 protected int getHorizontalScrollbarHeight() {
3524 ScrollabilityCache cache = mScrollCache;
3525 if (cache != null) {
3526 ScrollBarDrawable scrollBar = cache.scrollBar;
3527 if (scrollBar != null) {
3528 int size = scrollBar.getSize(false);
3529 if (size <= 0) {
3530 size = cache.scrollBarSize;
3531 }
3532 return size;
3533 }
3534 return 0;
3535 }
3536 return 0;
3537 }
3538
3539 /**
3540 * <p>
3541 * Initializes the scrollbars from a given set of styled attributes. This
3542 * method should be called by subclasses that need scrollbars and when an
3543 * instance of these subclasses is created programmatically rather than
3544 * being inflated from XML. This method is automatically called when the XML
3545 * is inflated.
3546 * </p>
3547 *
3548 * @param a the styled attributes set to initialize the scrollbars from
3549 */
3550 protected void initializeScrollbars(TypedArray a) {
3551 initScrollCache();
3552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003554
Mike Cleronf116bf82009-09-27 19:14:12 -07003555 if (scrollabilityCache.scrollBar == null) {
3556 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3557 }
Joe Malin32736f02011-01-19 16:14:20 -08003558
Romain Guy8bda2482010-03-02 11:42:11 -08003559 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560
Mike Cleronf116bf82009-09-27 19:14:12 -07003561 if (!fadeScrollbars) {
3562 scrollabilityCache.state = ScrollabilityCache.ON;
3563 }
3564 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003565
3566
Mike Cleronf116bf82009-09-27 19:14:12 -07003567 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3568 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3569 .getScrollBarFadeDuration());
3570 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3571 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3572 ViewConfiguration.getScrollDefaultDelay());
3573
Joe Malin32736f02011-01-19 16:14:20 -08003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3576 com.android.internal.R.styleable.View_scrollbarSize,
3577 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3578
3579 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3580 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3581
3582 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3583 if (thumb != null) {
3584 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3585 }
3586
3587 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3588 false);
3589 if (alwaysDraw) {
3590 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3591 }
3592
3593 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3594 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3595
3596 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3597 if (thumb != null) {
3598 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3599 }
3600
3601 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3602 false);
3603 if (alwaysDraw) {
3604 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3605 }
3606
3607 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003608 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 }
3610
3611 /**
3612 * <p>
3613 * Initalizes the scrollability cache if necessary.
3614 * </p>
3615 */
3616 private void initScrollCache() {
3617 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003618 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 }
3620 }
3621
Philip Milne6c8ea062012-04-03 17:38:43 -07003622 private ScrollabilityCache getScrollCache() {
3623 initScrollCache();
3624 return mScrollCache;
3625 }
3626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 /**
Adam Powell20232d02010-12-08 21:08:53 -08003628 * Set the position of the vertical scroll bar. Should be one of
3629 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3630 * {@link #SCROLLBAR_POSITION_RIGHT}.
3631 *
3632 * @param position Where the vertical scroll bar should be positioned.
3633 */
3634 public void setVerticalScrollbarPosition(int position) {
3635 if (mVerticalScrollbarPosition != position) {
3636 mVerticalScrollbarPosition = position;
3637 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003638 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003639 }
3640 }
3641
3642 /**
3643 * @return The position where the vertical scroll bar will show, if applicable.
3644 * @see #setVerticalScrollbarPosition(int)
3645 */
3646 public int getVerticalScrollbarPosition() {
3647 return mVerticalScrollbarPosition;
3648 }
3649
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003650 ListenerInfo getListenerInfo() {
3651 if (mListenerInfo != null) {
3652 return mListenerInfo;
3653 }
3654 mListenerInfo = new ListenerInfo();
3655 return mListenerInfo;
3656 }
3657
Adam Powell20232d02010-12-08 21:08:53 -08003658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 * Register a callback to be invoked when focus of this view changed.
3660 *
3661 * @param l The callback that will run.
3662 */
3663 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003664 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 }
3666
3667 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003668 * Add a listener that will be called when the bounds of the view change due to
3669 * layout processing.
3670 *
3671 * @param listener The listener that will be called when layout bounds change.
3672 */
3673 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 ListenerInfo li = getListenerInfo();
3675 if (li.mOnLayoutChangeListeners == null) {
3676 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003677 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003678 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3679 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003680 }
Chet Haase21cd1382010-09-01 17:42:29 -07003681 }
3682
3683 /**
3684 * Remove a listener for layout changes.
3685 *
3686 * @param listener The listener for layout bounds change.
3687 */
3688 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003689 ListenerInfo li = mListenerInfo;
3690 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003691 return;
3692 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003693 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003694 }
3695
3696 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003697 * Add a listener for attach state changes.
3698 *
3699 * This listener will be called whenever this view is attached or detached
3700 * from a window. Remove the listener using
3701 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3702 *
3703 * @param listener Listener to attach
3704 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3705 */
3706 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003707 ListenerInfo li = getListenerInfo();
3708 if (li.mOnAttachStateChangeListeners == null) {
3709 li.mOnAttachStateChangeListeners
3710 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003711 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003712 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003713 }
3714
3715 /**
3716 * Remove a listener for attach state changes. The listener will receive no further
3717 * notification of window attach/detach events.
3718 *
3719 * @param listener Listener to remove
3720 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3721 */
3722 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003723 ListenerInfo li = mListenerInfo;
3724 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003725 return;
3726 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003727 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003728 }
3729
3730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 * Returns the focus-change callback registered for this view.
3732 *
3733 * @return The callback, or null if one is not registered.
3734 */
3735 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003736 ListenerInfo li = mListenerInfo;
3737 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 }
3739
3740 /**
3741 * Register a callback to be invoked when this view is clicked. If this view is not
3742 * clickable, it becomes clickable.
3743 *
3744 * @param l The callback that will run
3745 *
3746 * @see #setClickable(boolean)
3747 */
3748 public void setOnClickListener(OnClickListener l) {
3749 if (!isClickable()) {
3750 setClickable(true);
3751 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003752 getListenerInfo().mOnClickListener = l;
3753 }
3754
3755 /**
3756 * Return whether this view has an attached OnClickListener. Returns
3757 * true if there is a listener, false if there is none.
3758 */
3759 public boolean hasOnClickListeners() {
3760 ListenerInfo li = mListenerInfo;
3761 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 }
3763
3764 /**
3765 * Register a callback to be invoked when this view is clicked and held. If this view is not
3766 * long clickable, it becomes long clickable.
3767 *
3768 * @param l The callback that will run
3769 *
3770 * @see #setLongClickable(boolean)
3771 */
3772 public void setOnLongClickListener(OnLongClickListener l) {
3773 if (!isLongClickable()) {
3774 setLongClickable(true);
3775 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003776 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 }
3778
3779 /**
3780 * Register a callback to be invoked when the context menu for this view is
3781 * being built. If this view is not long clickable, it becomes long clickable.
3782 *
3783 * @param l The callback that will run
3784 *
3785 */
3786 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3787 if (!isLongClickable()) {
3788 setLongClickable(true);
3789 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792
3793 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003794 * Call this view's OnClickListener, if it is defined. Performs all normal
3795 * actions associated with clicking: reporting accessibility event, playing
3796 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 *
3798 * @return True there was an assigned OnClickListener that was called, false
3799 * otherwise is returned.
3800 */
3801 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003802 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3803
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003804 ListenerInfo li = mListenerInfo;
3805 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003807 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 return true;
3809 }
3810
3811 return false;
3812 }
3813
3814 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003815 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3816 * this only calls the listener, and does not do any associated clicking
3817 * actions like reporting an accessibility event.
3818 *
3819 * @return True there was an assigned OnClickListener that was called, false
3820 * otherwise is returned.
3821 */
3822 public boolean callOnClick() {
3823 ListenerInfo li = mListenerInfo;
3824 if (li != null && li.mOnClickListener != null) {
3825 li.mOnClickListener.onClick(this);
3826 return true;
3827 }
3828 return false;
3829 }
3830
3831 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003832 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3833 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003835 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 */
3837 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003838 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003841 ListenerInfo li = mListenerInfo;
3842 if (li != null && li.mOnLongClickListener != null) {
3843 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 }
3845 if (!handled) {
3846 handled = showContextMenu();
3847 }
3848 if (handled) {
3849 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3850 }
3851 return handled;
3852 }
3853
3854 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003855 * Performs button-related actions during a touch down event.
3856 *
3857 * @param event The event.
3858 * @return True if the down was consumed.
3859 *
3860 * @hide
3861 */
3862 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3863 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3864 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3865 return true;
3866 }
3867 }
3868 return false;
3869 }
3870
3871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 * Bring up the context menu for this view.
3873 *
3874 * @return Whether a context menu was displayed.
3875 */
3876 public boolean showContextMenu() {
3877 return getParent().showContextMenuForChild(this);
3878 }
3879
3880 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003881 * Bring up the context menu for this view, referring to the item under the specified point.
3882 *
3883 * @param x The referenced x coordinate.
3884 * @param y The referenced y coordinate.
3885 * @param metaState The keyboard modifiers that were pressed.
3886 * @return Whether a context menu was displayed.
3887 *
3888 * @hide
3889 */
3890 public boolean showContextMenu(float x, float y, int metaState) {
3891 return showContextMenu();
3892 }
3893
3894 /**
Adam Powell6e346362010-07-23 10:18:23 -07003895 * Start an action mode.
3896 *
3897 * @param callback Callback that will control the lifecycle of the action mode
3898 * @return The new action mode if it is started, null otherwise
3899 *
3900 * @see ActionMode
3901 */
3902 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003903 ViewParent parent = getParent();
3904 if (parent == null) return null;
3905 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003906 }
3907
3908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 * Register a callback to be invoked when a key is pressed in this view.
3910 * @param l the key listener to attach to this view
3911 */
3912 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003913 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
3915
3916 /**
3917 * Register a callback to be invoked when a touch event is sent to this view.
3918 * @param l the touch listener to attach to this view
3919 */
3920 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003921 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923
3924 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003925 * Register a callback to be invoked when a generic motion event is sent to this view.
3926 * @param l the generic motion listener to attach to this view
3927 */
3928 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003929 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003930 }
3931
3932 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003933 * Register a callback to be invoked when a hover event is sent to this view.
3934 * @param l the hover listener to attach to this view
3935 */
3936 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003937 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003938 }
3939
3940 /**
Joe Malin32736f02011-01-19 16:14:20 -08003941 * Register a drag event listener callback object for this View. The parameter is
3942 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3943 * View, the system calls the
3944 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3945 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003946 */
3947 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003948 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003949 }
3950
3951 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003952 * Give this view focus. This will cause
3953 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 *
3955 * Note: this does not check whether this {@link View} should get focus, it just
3956 * gives it focus no matter what. It should only be called internally by framework
3957 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3958 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003959 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3960 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 * focus moved when requestFocus() is called. It may not always
3962 * apply, in which case use the default View.FOCUS_DOWN.
3963 * @param previouslyFocusedRect The rectangle of the view that had focus
3964 * prior in this View's coordinate system.
3965 */
3966 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3967 if (DBG) {
3968 System.out.println(this + " requestFocus()");
3969 }
3970
3971 if ((mPrivateFlags & FOCUSED) == 0) {
3972 mPrivateFlags |= FOCUSED;
3973
3974 if (mParent != null) {
3975 mParent.requestChildFocus(this, this);
3976 }
3977
3978 onFocusChanged(true, direction, previouslyFocusedRect);
3979 refreshDrawableState();
3980 }
3981 }
3982
3983 /**
3984 * Request that a rectangle of this view be visible on the screen,
3985 * scrolling if necessary just enough.
3986 *
3987 * <p>A View should call this if it maintains some notion of which part
3988 * of its content is interesting. For example, a text editing view
3989 * should call this when its cursor moves.
3990 *
3991 * @param rectangle The rectangle.
3992 * @return Whether any parent scrolled.
3993 */
3994 public boolean requestRectangleOnScreen(Rect rectangle) {
3995 return requestRectangleOnScreen(rectangle, false);
3996 }
3997
3998 /**
3999 * Request that a rectangle of this view be visible on the screen,
4000 * scrolling if necessary just enough.
4001 *
4002 * <p>A View should call this if it maintains some notion of which part
4003 * of its content is interesting. For example, a text editing view
4004 * should call this when its cursor moves.
4005 *
4006 * <p>When <code>immediate</code> is set to true, scrolling will not be
4007 * animated.
4008 *
4009 * @param rectangle The rectangle.
4010 * @param immediate True to forbid animated scrolling, false otherwise
4011 * @return Whether any parent scrolled.
4012 */
4013 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4014 View child = this;
4015 ViewParent parent = mParent;
4016 boolean scrolled = false;
4017 while (parent != null) {
4018 scrolled |= parent.requestChildRectangleOnScreen(child,
4019 rectangle, immediate);
4020
4021 // offset rect so next call has the rectangle in the
4022 // coordinate system of its direct child.
4023 rectangle.offset(child.getLeft(), child.getTop());
4024 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4025
4026 if (!(parent instanceof View)) {
4027 break;
4028 }
Romain Guy8506ab42009-06-11 17:35:47 -07004029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 child = (View) parent;
4031 parent = child.getParent();
4032 }
4033 return scrolled;
4034 }
4035
4036 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004037 * Called when this view wants to give up focus. If focus is cleared
4038 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4039 * <p>
4040 * <strong>Note:</strong> When a View clears focus the framework is trying
4041 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004042 * View is the first from the top that can take focus, then all callbacks
4043 * related to clearing focus will be invoked after wich the framework will
4044 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004045 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 */
4047 public void clearFocus() {
4048 if (DBG) {
4049 System.out.println(this + " clearFocus()");
4050 }
4051
4052 if ((mPrivateFlags & FOCUSED) != 0) {
4053 mPrivateFlags &= ~FOCUSED;
4054
4055 if (mParent != null) {
4056 mParent.clearChildFocus(this);
4057 }
4058
4059 onFocusChanged(false, 0, null);
4060 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004061
4062 ensureInputFocusOnFirstFocusable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064 }
4065
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004066 void ensureInputFocusOnFirstFocusable() {
4067 View root = getRootView();
4068 if (root != null) {
Svetoslav Ganov5012ebb2012-04-05 17:08:46 -07004069 root.requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 }
4071 }
4072
4073 /**
4074 * Called internally by the view system when a new view is getting focus.
4075 * This is what clears the old focus.
4076 */
4077 void unFocus() {
4078 if (DBG) {
4079 System.out.println(this + " unFocus()");
4080 }
4081
4082 if ((mPrivateFlags & FOCUSED) != 0) {
4083 mPrivateFlags &= ~FOCUSED;
4084
4085 onFocusChanged(false, 0, null);
4086 refreshDrawableState();
4087 }
4088 }
4089
4090 /**
4091 * Returns true if this view has focus iteself, or is the ancestor of the
4092 * view that has focus.
4093 *
4094 * @return True if this view has or contains focus, false otherwise.
4095 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004096 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 public boolean hasFocus() {
4098 return (mPrivateFlags & FOCUSED) != 0;
4099 }
4100
4101 /**
4102 * Returns true if this view is focusable or if it contains a reachable View
4103 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4104 * is a View whose parents do not block descendants focus.
4105 *
4106 * Only {@link #VISIBLE} views are considered focusable.
4107 *
4108 * @return True if the view is focusable or if the view contains a focusable
4109 * View, false otherwise.
4110 *
4111 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4112 */
4113 public boolean hasFocusable() {
4114 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4115 }
4116
4117 /**
4118 * Called by the view system when the focus state of this view changes.
4119 * When the focus change event is caused by directional navigation, direction
4120 * and previouslyFocusedRect provide insight into where the focus is coming from.
4121 * When overriding, be sure to call up through to the super class so that
4122 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 * @param gainFocus True if the View has focus; false otherwise.
4125 * @param direction The direction focus has moved when requestFocus()
4126 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004127 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4128 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4129 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4131 * system, of the previously focused view. If applicable, this will be
4132 * passed in as finer grained information about where the focus is coming
4133 * from (in addition to direction). Will be <code>null</code> otherwise.
4134 */
4135 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004136 if (gainFocus) {
4137 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4138 }
4139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 InputMethodManager imm = InputMethodManager.peekInstance();
4141 if (!gainFocus) {
4142 if (isPressed()) {
4143 setPressed(false);
4144 }
4145 if (imm != null && mAttachInfo != null
4146 && mAttachInfo.mHasWindowFocus) {
4147 imm.focusOut(this);
4148 }
Romain Guya2431d02009-04-30 16:30:00 -07004149 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 } else if (imm != null && mAttachInfo != null
4151 && mAttachInfo.mHasWindowFocus) {
4152 imm.focusIn(this);
4153 }
Romain Guy8506ab42009-06-11 17:35:47 -07004154
Romain Guy0fd89bf2011-01-26 15:41:30 -08004155 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004156 ListenerInfo li = mListenerInfo;
4157 if (li != null && li.mOnFocusChangeListener != null) {
4158 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
Joe Malin32736f02011-01-19 16:14:20 -08004160
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004161 if (mAttachInfo != null) {
4162 mAttachInfo.mKeyDispatchState.reset(this);
4163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 }
4165
4166 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004167 * Sends an accessibility event of the given type. If accessiiblity is
4168 * not enabled this method has no effect. The default implementation calls
4169 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4170 * to populate information about the event source (this View), then calls
4171 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4172 * populate the text content of the event source including its descendants,
4173 * and last calls
4174 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4175 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004176 * <p>
4177 * If an {@link AccessibilityDelegate} has been specified via calling
4178 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4179 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4180 * responsible for handling this call.
4181 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004182 *
Scott Mainb303d832011-10-12 16:45:18 -07004183 * @param eventType The type of the event to send, as defined by several types from
4184 * {@link android.view.accessibility.AccessibilityEvent}, such as
4185 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4186 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004187 *
4188 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4189 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4190 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004191 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004192 */
4193 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004194 if (mAccessibilityDelegate != null) {
4195 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4196 } else {
4197 sendAccessibilityEventInternal(eventType);
4198 }
4199 }
4200
4201 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004202 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4203 * {@link AccessibilityEvent} to make an announcement which is related to some
4204 * sort of a context change for which none of the events representing UI transitions
4205 * is a good fit. For example, announcing a new page in a book. If accessibility
4206 * is not enabled this method does nothing.
4207 *
4208 * @param text The announcement text.
4209 */
4210 public void announceForAccessibility(CharSequence text) {
4211 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4212 AccessibilityEvent event = AccessibilityEvent.obtain(
4213 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4214 event.getText().add(text);
4215 sendAccessibilityEventUnchecked(event);
4216 }
4217 }
4218
4219 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004220 * @see #sendAccessibilityEvent(int)
4221 *
4222 * Note: Called from the default {@link AccessibilityDelegate}.
4223 */
4224 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004225 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4226 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4227 }
4228 }
4229
4230 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004231 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4232 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004233 * perform a check whether accessibility is enabled.
4234 * <p>
4235 * If an {@link AccessibilityDelegate} has been specified via calling
4236 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4237 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4238 * is responsible for handling this call.
4239 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004240 *
4241 * @param event The event to send.
4242 *
4243 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004244 */
4245 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004246 if (mAccessibilityDelegate != null) {
4247 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4248 } else {
4249 sendAccessibilityEventUncheckedInternal(event);
4250 }
4251 }
4252
4253 /**
4254 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4255 *
4256 * Note: Called from the default {@link AccessibilityDelegate}.
4257 */
4258 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004259 if (!isShown()) {
4260 return;
4261 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004262 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004263 // Only a subset of accessibility events populates text content.
4264 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4265 dispatchPopulateAccessibilityEvent(event);
4266 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004267 // In the beginning we called #isShown(), so we know that getParent() is not null.
4268 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004269 }
4270
4271 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004272 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4273 * to its children for adding their text content to the event. Note that the
4274 * event text is populated in a separate dispatch path since we add to the
4275 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004276 * A typical implementation will call
4277 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4278 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4279 * on each child. Override this method if custom population of the event text
4280 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004281 * <p>
4282 * If an {@link AccessibilityDelegate} has been specified via calling
4283 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4284 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4285 * is responsible for handling this call.
4286 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004287 * <p>
4288 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4289 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4290 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004291 *
4292 * @param event The event.
4293 *
4294 * @return True if the event population was completed.
4295 */
4296 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004297 if (mAccessibilityDelegate != null) {
4298 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4299 } else {
4300 return dispatchPopulateAccessibilityEventInternal(event);
4301 }
4302 }
4303
4304 /**
4305 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4306 *
4307 * Note: Called from the default {@link AccessibilityDelegate}.
4308 */
4309 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004310 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004311 return false;
4312 }
4313
4314 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004315 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004316 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004317 * text content. While this method is free to modify event
4318 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004319 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4320 * <p>
4321 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004322 * to the text added by the super implementation:
4323 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004324 * super.onPopulateAccessibilityEvent(event);
4325 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4326 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4327 * mCurrentDate.getTimeInMillis(), flags);
4328 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004329 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004330 * <p>
4331 * If an {@link AccessibilityDelegate} has been specified via calling
4332 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4333 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4334 * is responsible for handling this call.
4335 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004336 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4337 * information to the event, in case the default implementation has basic information to add.
4338 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004339 *
4340 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004341 *
4342 * @see #sendAccessibilityEvent(int)
4343 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004344 */
4345 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004346 if (mAccessibilityDelegate != null) {
4347 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4348 } else {
4349 onPopulateAccessibilityEventInternal(event);
4350 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004351 }
4352
4353 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004354 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4355 *
4356 * Note: Called from the default {@link AccessibilityDelegate}.
4357 */
4358 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4359
4360 }
4361
4362 /**
4363 * Initializes an {@link AccessibilityEvent} with information about
4364 * this View which is the event source. In other words, the source of
4365 * an accessibility event is the view whose state change triggered firing
4366 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004367 * <p>
4368 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004369 * to properties set by the super implementation:
4370 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4371 * super.onInitializeAccessibilityEvent(event);
4372 * event.setPassword(true);
4373 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004374 * <p>
4375 * If an {@link AccessibilityDelegate} has been specified via calling
4376 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4377 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4378 * is responsible for handling this call.
4379 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004380 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4381 * information to the event, in case the default implementation has basic information to add.
4382 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004383 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004384 *
4385 * @see #sendAccessibilityEvent(int)
4386 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4387 */
4388 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004389 if (mAccessibilityDelegate != null) {
4390 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4391 } else {
4392 onInitializeAccessibilityEventInternal(event);
4393 }
4394 }
4395
4396 /**
4397 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4398 *
4399 * Note: Called from the default {@link AccessibilityDelegate}.
4400 */
4401 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004402 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004403 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004404 event.setPackageName(getContext().getPackageName());
4405 event.setEnabled(isEnabled());
4406 event.setContentDescription(mContentDescription);
4407
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004408 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4409 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4410 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4411 FOCUSABLES_ALL);
4412 event.setItemCount(focusablesTempList.size());
4413 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4414 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004415 }
4416 }
4417
4418 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004419 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4420 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4421 * This method is responsible for obtaining an accessibility node info from a
4422 * pool of reusable instances and calling
4423 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4424 * initialize the former.
4425 * <p>
4426 * Note: The client is responsible for recycling the obtained instance by calling
4427 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4428 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004429 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004430 * @return A populated {@link AccessibilityNodeInfo}.
4431 *
4432 * @see AccessibilityNodeInfo
4433 */
4434 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004435 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4436 if (provider != null) {
4437 return provider.createAccessibilityNodeInfo(View.NO_ID);
4438 } else {
4439 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4440 onInitializeAccessibilityNodeInfo(info);
4441 return info;
4442 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004443 }
4444
4445 /**
4446 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4447 * The base implementation sets:
4448 * <ul>
4449 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004450 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4451 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004452 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4453 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4454 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4455 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4456 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4457 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4458 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4459 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4460 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4461 * </ul>
4462 * <p>
4463 * Subclasses should override this method, call the super implementation,
4464 * and set additional attributes.
4465 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004466 * <p>
4467 * If an {@link AccessibilityDelegate} has been specified via calling
4468 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4469 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4470 * is responsible for handling this call.
4471 * </p>
4472 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004473 * @param info The instance to initialize.
4474 */
4475 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004476 if (mAccessibilityDelegate != null) {
4477 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4478 } else {
4479 onInitializeAccessibilityNodeInfoInternal(info);
4480 }
4481 }
4482
4483 /**
4484 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4485 *
4486 * Note: Called from the default {@link AccessibilityDelegate}.
4487 */
4488 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004489 Rect bounds = mAttachInfo.mTmpInvalRect;
4490 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004491 info.setBoundsInParent(bounds);
4492
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004493 getGlobalVisibleRect(bounds);
4494 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004495 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004496
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004497 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4498 ViewParent parent = getParent();
4499 if (parent instanceof View) {
4500 View parentView = (View) parent;
4501 info.setParent(parentView);
4502 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004503 }
4504
4505 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004506 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004507 info.setContentDescription(getContentDescription());
4508
4509 info.setEnabled(isEnabled());
4510 info.setClickable(isClickable());
4511 info.setFocusable(isFocusable());
4512 info.setFocused(isFocused());
4513 info.setSelected(isSelected());
4514 info.setLongClickable(isLongClickable());
4515
4516 // TODO: These make sense only if we are in an AdapterView but all
4517 // views can be selected. Maybe from accessiiblity perspective
4518 // we should report as selectable view in an AdapterView.
4519 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4520 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4521
4522 if (isFocusable()) {
4523 if (isFocused()) {
4524 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4525 } else {
4526 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4527 }
4528 }
4529 }
4530
4531 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004532 * Sets a delegate for implementing accessibility support via compositon as
4533 * opposed to inheritance. The delegate's primary use is for implementing
4534 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4535 *
4536 * @param delegate The delegate instance.
4537 *
4538 * @see AccessibilityDelegate
4539 */
4540 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4541 mAccessibilityDelegate = delegate;
4542 }
4543
4544 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004545 * Gets the provider for managing a virtual view hierarchy rooted at this View
4546 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4547 * that explore the window content.
4548 * <p>
4549 * If this method returns an instance, this instance is responsible for managing
4550 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4551 * View including the one representing the View itself. Similarly the returned
4552 * instance is responsible for performing accessibility actions on any virtual
4553 * view or the root view itself.
4554 * </p>
4555 * <p>
4556 * If an {@link AccessibilityDelegate} has been specified via calling
4557 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4558 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4559 * is responsible for handling this call.
4560 * </p>
4561 *
4562 * @return The provider.
4563 *
4564 * @see AccessibilityNodeProvider
4565 */
4566 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4567 if (mAccessibilityDelegate != null) {
4568 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4569 } else {
4570 return null;
4571 }
4572 }
4573
4574 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004575 * Gets the unique identifier of this view on the screen for accessibility purposes.
4576 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4577 *
4578 * @return The view accessibility id.
4579 *
4580 * @hide
4581 */
4582 public int getAccessibilityViewId() {
4583 if (mAccessibilityViewId == NO_ID) {
4584 mAccessibilityViewId = sNextAccessibilityViewId++;
4585 }
4586 return mAccessibilityViewId;
4587 }
4588
4589 /**
4590 * Gets the unique identifier of the window in which this View reseides.
4591 *
4592 * @return The window accessibility id.
4593 *
4594 * @hide
4595 */
4596 public int getAccessibilityWindowId() {
4597 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4598 }
4599
4600 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004601 * Gets the {@link View} description. It briefly describes the view and is
4602 * primarily used for accessibility support. Set this property to enable
4603 * better accessibility support for your application. This is especially
4604 * true for views that do not have textual representation (For example,
4605 * ImageButton).
4606 *
4607 * @return The content descriptiopn.
4608 *
4609 * @attr ref android.R.styleable#View_contentDescription
4610 */
4611 public CharSequence getContentDescription() {
4612 return mContentDescription;
4613 }
4614
4615 /**
4616 * Sets the {@link View} description. It briefly describes the view and is
4617 * primarily used for accessibility support. Set this property to enable
4618 * better accessibility support for your application. This is especially
4619 * true for views that do not have textual representation (For example,
4620 * ImageButton).
4621 *
4622 * @param contentDescription The content description.
4623 *
4624 * @attr ref android.R.styleable#View_contentDescription
4625 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004626 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004627 public void setContentDescription(CharSequence contentDescription) {
4628 mContentDescription = contentDescription;
4629 }
4630
4631 /**
Romain Guya2431d02009-04-30 16:30:00 -07004632 * Invoked whenever this view loses focus, either by losing window focus or by losing
4633 * focus within its window. This method can be used to clear any state tied to the
4634 * focus. For instance, if a button is held pressed with the trackball and the window
4635 * loses focus, this method can be used to cancel the press.
4636 *
4637 * Subclasses of View overriding this method should always call super.onFocusLost().
4638 *
4639 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004640 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004641 *
4642 * @hide pending API council approval
4643 */
4644 protected void onFocusLost() {
4645 resetPressedState();
4646 }
4647
4648 private void resetPressedState() {
4649 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4650 return;
4651 }
4652
4653 if (isPressed()) {
4654 setPressed(false);
4655
4656 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004657 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004658 }
4659 }
4660 }
4661
4662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 * Returns true if this view has focus
4664 *
4665 * @return True if this view has focus, false otherwise.
4666 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004667 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 public boolean isFocused() {
4669 return (mPrivateFlags & FOCUSED) != 0;
4670 }
4671
4672 /**
4673 * Find the view in the hierarchy rooted at this view that currently has
4674 * focus.
4675 *
4676 * @return The view that currently has focus, or null if no focused view can
4677 * be found.
4678 */
4679 public View findFocus() {
4680 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4681 }
4682
4683 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004684 * Indicates whether this view is one of the set of scrollable containers in
4685 * its window.
4686 *
4687 * @return whether this view is one of the set of scrollable containers in
4688 * its window
4689 *
4690 * @attr ref android.R.styleable#View_isScrollContainer
4691 */
4692 public boolean isScrollContainer() {
4693 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4694 }
4695
4696 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 * Change whether this view is one of the set of scrollable containers in
4698 * its window. This will be used to determine whether the window can
4699 * resize or must pan when a soft input area is open -- scrollable
4700 * containers allow the window to use resize mode since the container
4701 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004702 *
4703 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 */
4705 public void setScrollContainer(boolean isScrollContainer) {
4706 if (isScrollContainer) {
4707 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4708 mAttachInfo.mScrollContainers.add(this);
4709 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4710 }
4711 mPrivateFlags |= SCROLL_CONTAINER;
4712 } else {
4713 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4714 mAttachInfo.mScrollContainers.remove(this);
4715 }
4716 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4717 }
4718 }
4719
4720 /**
4721 * Returns the quality of the drawing cache.
4722 *
4723 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4724 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4725 *
4726 * @see #setDrawingCacheQuality(int)
4727 * @see #setDrawingCacheEnabled(boolean)
4728 * @see #isDrawingCacheEnabled()
4729 *
4730 * @attr ref android.R.styleable#View_drawingCacheQuality
4731 */
4732 public int getDrawingCacheQuality() {
4733 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4734 }
4735
4736 /**
4737 * Set the drawing cache quality of this view. This value is used only when the
4738 * drawing cache is enabled
4739 *
4740 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4741 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4742 *
4743 * @see #getDrawingCacheQuality()
4744 * @see #setDrawingCacheEnabled(boolean)
4745 * @see #isDrawingCacheEnabled()
4746 *
4747 * @attr ref android.R.styleable#View_drawingCacheQuality
4748 */
4749 public void setDrawingCacheQuality(int quality) {
4750 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4751 }
4752
4753 /**
4754 * Returns whether the screen should remain on, corresponding to the current
4755 * value of {@link #KEEP_SCREEN_ON}.
4756 *
4757 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4758 *
4759 * @see #setKeepScreenOn(boolean)
4760 *
4761 * @attr ref android.R.styleable#View_keepScreenOn
4762 */
4763 public boolean getKeepScreenOn() {
4764 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4765 }
4766
4767 /**
4768 * Controls whether the screen should remain on, modifying the
4769 * value of {@link #KEEP_SCREEN_ON}.
4770 *
4771 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4772 *
4773 * @see #getKeepScreenOn()
4774 *
4775 * @attr ref android.R.styleable#View_keepScreenOn
4776 */
4777 public void setKeepScreenOn(boolean keepScreenOn) {
4778 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4779 }
4780
4781 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004782 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4783 * @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 -08004784 *
4785 * @attr ref android.R.styleable#View_nextFocusLeft
4786 */
4787 public int getNextFocusLeftId() {
4788 return mNextFocusLeftId;
4789 }
4790
4791 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004792 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4793 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4794 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 *
4796 * @attr ref android.R.styleable#View_nextFocusLeft
4797 */
4798 public void setNextFocusLeftId(int nextFocusLeftId) {
4799 mNextFocusLeftId = nextFocusLeftId;
4800 }
4801
4802 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004803 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4804 * @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 -08004805 *
4806 * @attr ref android.R.styleable#View_nextFocusRight
4807 */
4808 public int getNextFocusRightId() {
4809 return mNextFocusRightId;
4810 }
4811
4812 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004813 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4814 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4815 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 *
4817 * @attr ref android.R.styleable#View_nextFocusRight
4818 */
4819 public void setNextFocusRightId(int nextFocusRightId) {
4820 mNextFocusRightId = nextFocusRightId;
4821 }
4822
4823 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004824 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4825 * @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 -08004826 *
4827 * @attr ref android.R.styleable#View_nextFocusUp
4828 */
4829 public int getNextFocusUpId() {
4830 return mNextFocusUpId;
4831 }
4832
4833 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004834 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4835 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4836 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 *
4838 * @attr ref android.R.styleable#View_nextFocusUp
4839 */
4840 public void setNextFocusUpId(int nextFocusUpId) {
4841 mNextFocusUpId = nextFocusUpId;
4842 }
4843
4844 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004845 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4846 * @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 -08004847 *
4848 * @attr ref android.R.styleable#View_nextFocusDown
4849 */
4850 public int getNextFocusDownId() {
4851 return mNextFocusDownId;
4852 }
4853
4854 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004855 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4856 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4857 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 *
4859 * @attr ref android.R.styleable#View_nextFocusDown
4860 */
4861 public void setNextFocusDownId(int nextFocusDownId) {
4862 mNextFocusDownId = nextFocusDownId;
4863 }
4864
4865 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004866 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4867 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4868 *
4869 * @attr ref android.R.styleable#View_nextFocusForward
4870 */
4871 public int getNextFocusForwardId() {
4872 return mNextFocusForwardId;
4873 }
4874
4875 /**
4876 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4877 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4878 * decide automatically.
4879 *
4880 * @attr ref android.R.styleable#View_nextFocusForward
4881 */
4882 public void setNextFocusForwardId(int nextFocusForwardId) {
4883 mNextFocusForwardId = nextFocusForwardId;
4884 }
4885
4886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 * Returns the visibility of this view and all of its ancestors
4888 *
4889 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4890 */
4891 public boolean isShown() {
4892 View current = this;
4893 //noinspection ConstantConditions
4894 do {
4895 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4896 return false;
4897 }
4898 ViewParent parent = current.mParent;
4899 if (parent == null) {
4900 return false; // We are not attached to the view root
4901 }
4902 if (!(parent instanceof View)) {
4903 return true;
4904 }
4905 current = (View) parent;
4906 } while (current != null);
4907
4908 return false;
4909 }
4910
4911 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004912 * Called by the view hierarchy when the content insets for a window have
4913 * changed, to allow it to adjust its content to fit within those windows.
4914 * The content insets tell you the space that the status bar, input method,
4915 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004917 * <p>You do not normally need to deal with this function, since the default
4918 * window decoration given to applications takes care of applying it to the
4919 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
4920 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
4921 * and your content can be placed under those system elements. You can then
4922 * use this method within your view hierarchy if you have parts of your UI
4923 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004925 * <p>The default implementation of this method simply applies the content
4926 * inset's to the view's padding. This can be enabled through
4927 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
4928 * the method and handle the insets however you would like. Note that the
4929 * insets provided by the framework are always relative to the far edges
4930 * of the window, not accounting for the location of the called view within
4931 * that window. (In fact when this method is called you do not yet know
4932 * where the layout will place the view, as it is done before layout happens.)
4933 *
4934 * <p>Note: unlike many View methods, there is no dispatch phase to this
4935 * call. If you are overriding it in a ViewGroup and want to allow the
4936 * call to continue to your children, you must be sure to call the super
4937 * implementation.
4938 *
4939 * @param insets Current content insets of the window. Prior to
4940 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
4941 * the insets or else you and Android will be unhappy.
4942 *
4943 * @return Return true if this view applied the insets and it should not
4944 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 */
4946 protected boolean fitSystemWindows(Rect insets) {
4947 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004948 mUserPaddingStart = -1;
4949 mUserPaddingEnd = -1;
4950 mUserPaddingRelative = false;
4951 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
4952 || mAttachInfo == null
4953 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
4954 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
4955 return true;
4956 } else {
4957 internalSetPadding(0, 0, 0, 0);
4958 return false;
4959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 }
4961 return false;
4962 }
4963
4964 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004965 * Set whether or not this view should account for system screen decorations
4966 * such as the status bar and inset its content. This allows this view to be
4967 * positioned in absolute screen coordinates and remain visible to the user.
4968 *
4969 * <p>This should only be used by top-level window decor views.
4970 *
4971 * @param fitSystemWindows true to inset content for system screen decorations, false for
4972 * default behavior.
4973 *
4974 * @attr ref android.R.styleable#View_fitsSystemWindows
4975 */
4976 public void setFitsSystemWindows(boolean fitSystemWindows) {
4977 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4978 }
4979
4980 /**
4981 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4982 * will account for system screen decorations such as the status bar and inset its
4983 * content. This allows the view to be positioned in absolute screen coordinates
4984 * and remain visible to the user.
4985 *
4986 * @return true if this view will adjust its content bounds for system screen decorations.
4987 *
4988 * @attr ref android.R.styleable#View_fitsSystemWindows
4989 */
4990 public boolean fitsSystemWindows() {
4991 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4992 }
4993
4994 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004995 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
4996 */
4997 public void requestFitSystemWindows() {
4998 if (mParent != null) {
4999 mParent.requestFitSystemWindows();
5000 }
5001 }
5002
5003 /**
5004 * For use by PhoneWindow to make its own system window fitting optional.
5005 * @hide
5006 */
5007 public void makeOptionalFitsSystemWindows() {
5008 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5009 }
5010
5011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 * Returns the visibility status for this view.
5013 *
5014 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5015 * @attr ref android.R.styleable#View_visibility
5016 */
5017 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005018 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5019 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5020 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 })
5022 public int getVisibility() {
5023 return mViewFlags & VISIBILITY_MASK;
5024 }
5025
5026 /**
5027 * Set the enabled state of this view.
5028 *
5029 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5030 * @attr ref android.R.styleable#View_visibility
5031 */
5032 @RemotableViewMethod
5033 public void setVisibility(int visibility) {
5034 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005035 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 }
5037
5038 /**
5039 * Returns the enabled status for this view. The interpretation of the
5040 * enabled state varies by subclass.
5041 *
5042 * @return True if this view is enabled, false otherwise.
5043 */
5044 @ViewDebug.ExportedProperty
5045 public boolean isEnabled() {
5046 return (mViewFlags & ENABLED_MASK) == ENABLED;
5047 }
5048
5049 /**
5050 * Set the enabled state of this view. The interpretation of the enabled
5051 * state varies by subclass.
5052 *
5053 * @param enabled True if this view is enabled, false otherwise.
5054 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005055 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005057 if (enabled == isEnabled()) return;
5058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5060
5061 /*
5062 * The View most likely has to change its appearance, so refresh
5063 * the drawable state.
5064 */
5065 refreshDrawableState();
5066
5067 // Invalidate too, since the default behavior for views is to be
5068 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005069 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
5071
5072 /**
5073 * Set whether this view can receive the focus.
5074 *
5075 * Setting this to false will also ensure that this view is not focusable
5076 * in touch mode.
5077 *
5078 * @param focusable If true, this view can receive the focus.
5079 *
5080 * @see #setFocusableInTouchMode(boolean)
5081 * @attr ref android.R.styleable#View_focusable
5082 */
5083 public void setFocusable(boolean focusable) {
5084 if (!focusable) {
5085 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5086 }
5087 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5088 }
5089
5090 /**
5091 * Set whether this view can receive focus while in touch mode.
5092 *
5093 * Setting this to true will also ensure that this view is focusable.
5094 *
5095 * @param focusableInTouchMode If true, this view can receive the focus while
5096 * in touch mode.
5097 *
5098 * @see #setFocusable(boolean)
5099 * @attr ref android.R.styleable#View_focusableInTouchMode
5100 */
5101 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5102 // Focusable in touch mode should always be set before the focusable flag
5103 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5104 // which, in touch mode, will not successfully request focus on this view
5105 // because the focusable in touch mode flag is not set
5106 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5107 if (focusableInTouchMode) {
5108 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5109 }
5110 }
5111
5112 /**
5113 * Set whether this view should have sound effects enabled for events such as
5114 * clicking and touching.
5115 *
5116 * <p>You may wish to disable sound effects for a view if you already play sounds,
5117 * for instance, a dial key that plays dtmf tones.
5118 *
5119 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5120 * @see #isSoundEffectsEnabled()
5121 * @see #playSoundEffect(int)
5122 * @attr ref android.R.styleable#View_soundEffectsEnabled
5123 */
5124 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5125 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5126 }
5127
5128 /**
5129 * @return whether this view should have sound effects enabled for events such as
5130 * clicking and touching.
5131 *
5132 * @see #setSoundEffectsEnabled(boolean)
5133 * @see #playSoundEffect(int)
5134 * @attr ref android.R.styleable#View_soundEffectsEnabled
5135 */
5136 @ViewDebug.ExportedProperty
5137 public boolean isSoundEffectsEnabled() {
5138 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5139 }
5140
5141 /**
5142 * Set whether this view should have haptic feedback for events such as
5143 * long presses.
5144 *
5145 * <p>You may wish to disable haptic feedback if your view already controls
5146 * its own haptic feedback.
5147 *
5148 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5149 * @see #isHapticFeedbackEnabled()
5150 * @see #performHapticFeedback(int)
5151 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5152 */
5153 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5154 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5155 }
5156
5157 /**
5158 * @return whether this view should have haptic feedback enabled for events
5159 * long presses.
5160 *
5161 * @see #setHapticFeedbackEnabled(boolean)
5162 * @see #performHapticFeedback(int)
5163 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5164 */
5165 @ViewDebug.ExportedProperty
5166 public boolean isHapticFeedbackEnabled() {
5167 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5168 }
5169
5170 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005171 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005172 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005173 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5174 * {@link #LAYOUT_DIRECTION_RTL},
5175 * {@link #LAYOUT_DIRECTION_INHERIT} or
5176 * {@link #LAYOUT_DIRECTION_LOCALE}.
5177 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005178 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005179 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005180 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5181 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5182 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5183 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005184 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005185 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005186 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005187 }
5188
5189 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005190 * Set the layout direction for this view. This will propagate a reset of layout direction
5191 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005192 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005193 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5194 * {@link #LAYOUT_DIRECTION_RTL},
5195 * {@link #LAYOUT_DIRECTION_INHERIT} or
5196 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005197 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005198 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005199 */
5200 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005201 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005202 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005203 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005204 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005205 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005206 // Set the new layout direction (filtered) and ask for a layout pass
5207 mPrivateFlags2 |=
5208 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5209 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005210 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005211 }
5212
5213 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005214 * Returns the resolved layout direction for this view.
5215 *
5216 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005217 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005218 */
5219 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005220 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5221 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005222 })
5223 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005224 // The layout diretion will be resolved only if needed
5225 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5226 resolveLayoutDirection();
5227 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005228 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005229 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5230 }
5231
5232 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005233 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5234 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005235 *
5236 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005237 */
5238 @ViewDebug.ExportedProperty(category = "layout")
5239 public boolean isLayoutRtl() {
5240 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5241 }
5242
5243 /**
Adam Powell539ee872012-02-03 19:00:49 -08005244 * Indicates whether the view is currently tracking transient state that the
5245 * app should not need to concern itself with saving and restoring, but that
5246 * the framework should take special note to preserve when possible.
5247 *
5248 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005249 */
5250 @ViewDebug.ExportedProperty(category = "layout")
5251 public boolean hasTransientState() {
5252 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5253 }
5254
5255 /**
5256 * Set whether this view is currently tracking transient state that the
5257 * framework should attempt to preserve when possible.
5258 *
5259 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005260 */
5261 public void setHasTransientState(boolean hasTransientState) {
5262 if (hasTransientState() == hasTransientState) return;
5263
5264 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5265 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5266 if (mParent != null) {
5267 try {
5268 mParent.childHasTransientStateChanged(this, hasTransientState);
5269 } catch (AbstractMethodError e) {
5270 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5271 " does not fully implement ViewParent", e);
5272 }
5273 }
5274 }
5275
5276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 * If this view doesn't do any drawing on its own, set this flag to
5278 * allow further optimizations. By default, this flag is not set on
5279 * View, but could be set on some View subclasses such as ViewGroup.
5280 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005281 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5282 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 *
5284 * @param willNotDraw whether or not this View draw on its own
5285 */
5286 public void setWillNotDraw(boolean willNotDraw) {
5287 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5288 }
5289
5290 /**
5291 * Returns whether or not this View draws on its own.
5292 *
5293 * @return true if this view has nothing to draw, false otherwise
5294 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005295 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 public boolean willNotDraw() {
5297 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5298 }
5299
5300 /**
5301 * When a View's drawing cache is enabled, drawing is redirected to an
5302 * offscreen bitmap. Some views, like an ImageView, must be able to
5303 * bypass this mechanism if they already draw a single bitmap, to avoid
5304 * unnecessary usage of the memory.
5305 *
5306 * @param willNotCacheDrawing true if this view does not cache its
5307 * drawing, false otherwise
5308 */
5309 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5310 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5311 }
5312
5313 /**
5314 * Returns whether or not this View can cache its drawing or not.
5315 *
5316 * @return true if this view does not cache its drawing, false otherwise
5317 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005318 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 public boolean willNotCacheDrawing() {
5320 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5321 }
5322
5323 /**
5324 * Indicates whether this view reacts to click events or not.
5325 *
5326 * @return true if the view is clickable, false otherwise
5327 *
5328 * @see #setClickable(boolean)
5329 * @attr ref android.R.styleable#View_clickable
5330 */
5331 @ViewDebug.ExportedProperty
5332 public boolean isClickable() {
5333 return (mViewFlags & CLICKABLE) == CLICKABLE;
5334 }
5335
5336 /**
5337 * Enables or disables click events for this view. When a view
5338 * is clickable it will change its state to "pressed" on every click.
5339 * Subclasses should set the view clickable to visually react to
5340 * user's clicks.
5341 *
5342 * @param clickable true to make the view clickable, false otherwise
5343 *
5344 * @see #isClickable()
5345 * @attr ref android.R.styleable#View_clickable
5346 */
5347 public void setClickable(boolean clickable) {
5348 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5349 }
5350
5351 /**
5352 * Indicates whether this view reacts to long click events or not.
5353 *
5354 * @return true if the view is long clickable, false otherwise
5355 *
5356 * @see #setLongClickable(boolean)
5357 * @attr ref android.R.styleable#View_longClickable
5358 */
5359 public boolean isLongClickable() {
5360 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5361 }
5362
5363 /**
5364 * Enables or disables long click events for this view. When a view is long
5365 * clickable it reacts to the user holding down the button for a longer
5366 * duration than a tap. This event can either launch the listener or a
5367 * context menu.
5368 *
5369 * @param longClickable true to make the view long clickable, false otherwise
5370 * @see #isLongClickable()
5371 * @attr ref android.R.styleable#View_longClickable
5372 */
5373 public void setLongClickable(boolean longClickable) {
5374 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5375 }
5376
5377 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005378 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 *
5380 * @see #isClickable()
5381 * @see #setClickable(boolean)
5382 *
5383 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5384 * the View's internal state from a previously set "pressed" state.
5385 */
5386 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005387 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 if (pressed) {
5390 mPrivateFlags |= PRESSED;
5391 } else {
5392 mPrivateFlags &= ~PRESSED;
5393 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005394
5395 if (needsRefresh) {
5396 refreshDrawableState();
5397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 dispatchSetPressed(pressed);
5399 }
5400
5401 /**
5402 * Dispatch setPressed to all of this View's children.
5403 *
5404 * @see #setPressed(boolean)
5405 *
5406 * @param pressed The new pressed state
5407 */
5408 protected void dispatchSetPressed(boolean pressed) {
5409 }
5410
5411 /**
5412 * Indicates whether the view is currently in pressed state. Unless
5413 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5414 * the pressed state.
5415 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005416 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 * @see #isClickable()
5418 * @see #setClickable(boolean)
5419 *
5420 * @return true if the view is currently pressed, false otherwise
5421 */
5422 public boolean isPressed() {
5423 return (mPrivateFlags & PRESSED) == PRESSED;
5424 }
5425
5426 /**
5427 * Indicates whether this view will save its state (that is,
5428 * whether its {@link #onSaveInstanceState} method will be called).
5429 *
5430 * @return Returns true if the view state saving is enabled, else false.
5431 *
5432 * @see #setSaveEnabled(boolean)
5433 * @attr ref android.R.styleable#View_saveEnabled
5434 */
5435 public boolean isSaveEnabled() {
5436 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5437 }
5438
5439 /**
5440 * Controls whether the saving of this view's state is
5441 * enabled (that is, whether its {@link #onSaveInstanceState} method
5442 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005443 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 * for its state to be saved. This flag can only disable the
5445 * saving of this view; any child views may still have their state saved.
5446 *
5447 * @param enabled Set to false to <em>disable</em> state saving, or true
5448 * (the default) to allow it.
5449 *
5450 * @see #isSaveEnabled()
5451 * @see #setId(int)
5452 * @see #onSaveInstanceState()
5453 * @attr ref android.R.styleable#View_saveEnabled
5454 */
5455 public void setSaveEnabled(boolean enabled) {
5456 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5457 }
5458
Jeff Brown85a31762010-09-01 17:01:00 -07005459 /**
5460 * Gets whether the framework should discard touches when the view's
5461 * window is obscured by another visible window.
5462 * Refer to the {@link View} security documentation for more details.
5463 *
5464 * @return True if touch filtering is enabled.
5465 *
5466 * @see #setFilterTouchesWhenObscured(boolean)
5467 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5468 */
5469 @ViewDebug.ExportedProperty
5470 public boolean getFilterTouchesWhenObscured() {
5471 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5472 }
5473
5474 /**
5475 * Sets whether the framework should discard touches when the view's
5476 * window is obscured by another visible window.
5477 * Refer to the {@link View} security documentation for more details.
5478 *
5479 * @param enabled True if touch filtering should be enabled.
5480 *
5481 * @see #getFilterTouchesWhenObscured
5482 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5483 */
5484 public void setFilterTouchesWhenObscured(boolean enabled) {
5485 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5486 FILTER_TOUCHES_WHEN_OBSCURED);
5487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488
5489 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005490 * Indicates whether the entire hierarchy under this view will save its
5491 * state when a state saving traversal occurs from its parent. The default
5492 * is true; if false, these views will not be saved unless
5493 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5494 *
5495 * @return Returns true if the view state saving from parent is enabled, else false.
5496 *
5497 * @see #setSaveFromParentEnabled(boolean)
5498 */
5499 public boolean isSaveFromParentEnabled() {
5500 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5501 }
5502
5503 /**
5504 * Controls whether the entire hierarchy under this view will save its
5505 * state when a state saving traversal occurs from its parent. The default
5506 * is true; if false, these views will not be saved unless
5507 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5508 *
5509 * @param enabled Set to false to <em>disable</em> state saving, or true
5510 * (the default) to allow it.
5511 *
5512 * @see #isSaveFromParentEnabled()
5513 * @see #setId(int)
5514 * @see #onSaveInstanceState()
5515 */
5516 public void setSaveFromParentEnabled(boolean enabled) {
5517 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5518 }
5519
5520
5521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 * Returns whether this View is able to take focus.
5523 *
5524 * @return True if this view can take focus, or false otherwise.
5525 * @attr ref android.R.styleable#View_focusable
5526 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005527 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 public final boolean isFocusable() {
5529 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5530 }
5531
5532 /**
5533 * When a view is focusable, it may not want to take focus when in touch mode.
5534 * For example, a button would like focus when the user is navigating via a D-pad
5535 * so that the user can click on it, but once the user starts touching the screen,
5536 * the button shouldn't take focus
5537 * @return Whether the view is focusable in touch mode.
5538 * @attr ref android.R.styleable#View_focusableInTouchMode
5539 */
5540 @ViewDebug.ExportedProperty
5541 public final boolean isFocusableInTouchMode() {
5542 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5543 }
5544
5545 /**
5546 * Find the nearest view in the specified direction that can take focus.
5547 * This does not actually give focus to that view.
5548 *
5549 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5550 *
5551 * @return The nearest focusable in the specified direction, or null if none
5552 * can be found.
5553 */
5554 public View focusSearch(int direction) {
5555 if (mParent != null) {
5556 return mParent.focusSearch(this, direction);
5557 } else {
5558 return null;
5559 }
5560 }
5561
5562 /**
5563 * This method is the last chance for the focused view and its ancestors to
5564 * respond to an arrow key. This is called when the focused view did not
5565 * consume the key internally, nor could the view system find a new view in
5566 * the requested direction to give focus to.
5567 *
5568 * @param focused The currently focused view.
5569 * @param direction The direction focus wants to move. One of FOCUS_UP,
5570 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5571 * @return True if the this view consumed this unhandled move.
5572 */
5573 public boolean dispatchUnhandledMove(View focused, int direction) {
5574 return false;
5575 }
5576
5577 /**
5578 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005579 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005581 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5582 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 * @return The user specified next view, or null if there is none.
5584 */
5585 View findUserSetNextFocus(View root, int direction) {
5586 switch (direction) {
5587 case FOCUS_LEFT:
5588 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005589 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 case FOCUS_RIGHT:
5591 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005592 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 case FOCUS_UP:
5594 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005595 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 case FOCUS_DOWN:
5597 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005598 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005599 case FOCUS_FORWARD:
5600 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005601 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005602 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005603 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005604 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005605 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005606 @Override
5607 public boolean apply(View t) {
5608 return t.mNextFocusForwardId == id;
5609 }
5610 });
5611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 }
5613 return null;
5614 }
5615
Jeff Brown4dfbec22011-08-15 14:55:37 -07005616 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5617 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5618 @Override
5619 public boolean apply(View t) {
5620 return t.mID == childViewId;
5621 }
5622 });
5623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 if (result == null) {
5625 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5626 + "by user for id " + childViewId);
5627 }
5628 return result;
5629 }
5630
5631 /**
5632 * Find and return all focusable views that are descendants of this view,
5633 * possibly including this view if it is focusable itself.
5634 *
5635 * @param direction The direction of the focus
5636 * @return A list of focusable views
5637 */
5638 public ArrayList<View> getFocusables(int direction) {
5639 ArrayList<View> result = new ArrayList<View>(24);
5640 addFocusables(result, direction);
5641 return result;
5642 }
5643
5644 /**
5645 * Add any focusable views that are descendants of this view (possibly
5646 * including this view if it is focusable itself) to views. If we are in touch mode,
5647 * only add views that are also focusable in touch mode.
5648 *
5649 * @param views Focusable views found so far
5650 * @param direction The direction of the focus
5651 */
5652 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005653 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655
svetoslavganov75986cf2009-05-14 22:28:01 -07005656 /**
5657 * Adds any focusable views that are descendants of this view (possibly
5658 * including this view if it is focusable itself) to views. This method
5659 * adds all focusable views regardless if we are in touch mode or
5660 * only views focusable in touch mode if we are in touch mode depending on
5661 * the focusable mode paramater.
5662 *
5663 * @param views Focusable views found so far or null if all we are interested is
5664 * the number of focusables.
5665 * @param direction The direction of the focus.
5666 * @param focusableMode The type of focusables to be added.
5667 *
5668 * @see #FOCUSABLES_ALL
5669 * @see #FOCUSABLES_TOUCH_MODE
5670 */
5671 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5672 if (!isFocusable()) {
5673 return;
5674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675
svetoslavganov75986cf2009-05-14 22:28:01 -07005676 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5677 isInTouchMode() && !isFocusableInTouchMode()) {
5678 return;
5679 }
5680
5681 if (views != null) {
5682 views.add(this);
5683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 }
5685
5686 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005687 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005688 * The search is performed by either the text that the View renders or the content
5689 * description that describes the view for accessibility purposes and the view does
5690 * not render or both. Clients can specify how the search is to be performed via
5691 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5692 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005693 *
5694 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005695 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005696 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005697 * @see #FIND_VIEWS_WITH_TEXT
5698 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5699 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005700 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005701 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005702 if (getAccessibilityNodeProvider() != null) {
5703 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5704 outViews.add(this);
5705 }
5706 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5707 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005708 String searchedLowerCase = searched.toString().toLowerCase();
5709 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5710 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5711 outViews.add(this);
5712 }
5713 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005714 }
5715
5716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 * Find and return all touchable views that are descendants of this view,
5718 * possibly including this view if it is touchable itself.
5719 *
5720 * @return A list of touchable views
5721 */
5722 public ArrayList<View> getTouchables() {
5723 ArrayList<View> result = new ArrayList<View>();
5724 addTouchables(result);
5725 return result;
5726 }
5727
5728 /**
5729 * Add any touchable views that are descendants of this view (possibly
5730 * including this view if it is touchable itself) to views.
5731 *
5732 * @param views Touchable views found so far
5733 */
5734 public void addTouchables(ArrayList<View> views) {
5735 final int viewFlags = mViewFlags;
5736
5737 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5738 && (viewFlags & ENABLED_MASK) == ENABLED) {
5739 views.add(this);
5740 }
5741 }
5742
5743 /**
5744 * Call this to try to give focus to a specific view or to one of its
5745 * descendants.
5746 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005747 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5748 * false), or if it is focusable and it is not focusable in touch mode
5749 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005751 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 * have focus, and you want your parent to look for the next one.
5753 *
5754 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5755 * {@link #FOCUS_DOWN} and <code>null</code>.
5756 *
5757 * @return Whether this view or one of its descendants actually took focus.
5758 */
5759 public final boolean requestFocus() {
5760 return requestFocus(View.FOCUS_DOWN);
5761 }
5762
5763
5764 /**
5765 * Call this to try to give focus to a specific view or to one of its
5766 * descendants and give it a hint about what direction focus is heading.
5767 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005768 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5769 * false), or if it is focusable and it is not focusable in touch mode
5770 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005772 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 * have focus, and you want your parent to look for the next one.
5774 *
5775 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5776 * <code>null</code> set for the previously focused rectangle.
5777 *
5778 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5779 * @return Whether this view or one of its descendants actually took focus.
5780 */
5781 public final boolean requestFocus(int direction) {
5782 return requestFocus(direction, null);
5783 }
5784
5785 /**
5786 * Call this to try to give focus to a specific view or to one of its descendants
5787 * and give it hints about the direction and a specific rectangle that the focus
5788 * is coming from. The rectangle can help give larger views a finer grained hint
5789 * about where focus is coming from, and therefore, where to show selection, or
5790 * forward focus change internally.
5791 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005792 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5793 * false), or if it is focusable and it is not focusable in touch mode
5794 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 *
5796 * A View will not take focus if it is not visible.
5797 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005798 * A View will not take focus if one of its parents has
5799 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5800 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005802 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 * have focus, and you want your parent to look for the next one.
5804 *
5805 * You may wish to override this method if your custom {@link View} has an internal
5806 * {@link View} that it wishes to forward the request to.
5807 *
5808 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5809 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5810 * to give a finer grained hint about where focus is coming from. May be null
5811 * if there is no hint.
5812 * @return Whether this view or one of its descendants actually took focus.
5813 */
5814 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5815 // need to be focusable
5816 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5817 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5818 return false;
5819 }
5820
5821 // need to be focusable in touch mode if in touch mode
5822 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005823 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5824 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 }
5826
5827 // need to not have any parents blocking us
5828 if (hasAncestorThatBlocksDescendantFocus()) {
5829 return false;
5830 }
5831
5832 handleFocusGainInternal(direction, previouslyFocusedRect);
5833 return true;
5834 }
5835
5836 /**
5837 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5838 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5839 * touch mode to request focus when they are touched.
5840 *
5841 * @return Whether this view or one of its descendants actually took focus.
5842 *
5843 * @see #isInTouchMode()
5844 *
5845 */
5846 public final boolean requestFocusFromTouch() {
5847 // Leave touch mode if we need to
5848 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005849 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005850 if (viewRoot != null) {
5851 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 }
5853 }
5854 return requestFocus(View.FOCUS_DOWN);
5855 }
5856
5857 /**
5858 * @return Whether any ancestor of this view blocks descendant focus.
5859 */
5860 private boolean hasAncestorThatBlocksDescendantFocus() {
5861 ViewParent ancestor = mParent;
5862 while (ancestor instanceof ViewGroup) {
5863 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5864 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5865 return true;
5866 } else {
5867 ancestor = vgAncestor.getParent();
5868 }
5869 }
5870 return false;
5871 }
5872
5873 /**
Romain Guya440b002010-02-24 15:57:54 -08005874 * @hide
5875 */
5876 public void dispatchStartTemporaryDetach() {
5877 onStartTemporaryDetach();
5878 }
5879
5880 /**
5881 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5883 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005884 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 */
5886 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005887 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005888 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005889 }
5890
5891 /**
5892 * @hide
5893 */
5894 public void dispatchFinishTemporaryDetach() {
5895 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 }
Romain Guy8506ab42009-06-11 17:35:47 -07005897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 /**
5899 * Called after {@link #onStartTemporaryDetach} when the container is done
5900 * changing the view.
5901 */
5902 public void onFinishTemporaryDetach() {
5903 }
Romain Guy8506ab42009-06-11 17:35:47 -07005904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005906 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5907 * for this view's window. Returns null if the view is not currently attached
5908 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005909 * just use the standard high-level event callbacks like
5910 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005911 */
5912 public KeyEvent.DispatcherState getKeyDispatcherState() {
5913 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5914 }
Joe Malin32736f02011-01-19 16:14:20 -08005915
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 * Dispatch a key event before it is processed by any input method
5918 * associated with the view hierarchy. This can be used to intercept
5919 * key events in special situations before the IME consumes them; a
5920 * typical example would be handling the BACK key to update the application's
5921 * UI instead of allowing the IME to see it and close itself.
5922 *
5923 * @param event The key event to be dispatched.
5924 * @return True if the event was handled, false otherwise.
5925 */
5926 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5927 return onKeyPreIme(event.getKeyCode(), event);
5928 }
5929
5930 /**
5931 * Dispatch a key event to the next view on the focus path. This path runs
5932 * from the top of the view tree down to the currently focused view. If this
5933 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5934 * the next node down the focus path. This method also fires any key
5935 * listeners.
5936 *
5937 * @param event The key event to be dispatched.
5938 * @return True if the event was handled, false otherwise.
5939 */
5940 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005941 if (mInputEventConsistencyVerifier != null) {
5942 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944
Jeff Brown21bc5c92011-02-28 18:27:14 -08005945 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005946 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005947 ListenerInfo li = mListenerInfo;
5948 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5949 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 return true;
5951 }
5952
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005953 if (event.dispatch(this, mAttachInfo != null
5954 ? mAttachInfo.mKeyDispatchState : null, this)) {
5955 return true;
5956 }
5957
5958 if (mInputEventConsistencyVerifier != null) {
5959 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5960 }
5961 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 }
5963
5964 /**
5965 * Dispatches a key shortcut event.
5966 *
5967 * @param event The key event to be dispatched.
5968 * @return True if the event was handled by the view, false otherwise.
5969 */
5970 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5971 return onKeyShortcut(event.getKeyCode(), event);
5972 }
5973
5974 /**
5975 * Pass the touch screen motion event down to the target view, or this
5976 * view if it is the target.
5977 *
5978 * @param event The motion event to be dispatched.
5979 * @return True if the event was handled by the view, false otherwise.
5980 */
5981 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005982 if (mInputEventConsistencyVerifier != null) {
5983 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5984 }
5985
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005986 if (onFilterTouchEventForSecurity(event)) {
5987 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005988 ListenerInfo li = mListenerInfo;
5989 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5990 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005991 return true;
5992 }
5993
5994 if (onTouchEvent(event)) {
5995 return true;
5996 }
Jeff Brown85a31762010-09-01 17:01:00 -07005997 }
5998
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005999 if (mInputEventConsistencyVerifier != null) {
6000 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006002 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 }
6004
6005 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006006 * Filter the touch event to apply security policies.
6007 *
6008 * @param event The motion event to be filtered.
6009 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006010 *
Jeff Brown85a31762010-09-01 17:01:00 -07006011 * @see #getFilterTouchesWhenObscured
6012 */
6013 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006014 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006015 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6016 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6017 // Window is obscured, drop this touch.
6018 return false;
6019 }
6020 return true;
6021 }
6022
6023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 * Pass a trackball motion event down to the focused view.
6025 *
6026 * @param event The motion event to be dispatched.
6027 * @return True if the event was handled by the view, false otherwise.
6028 */
6029 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006030 if (mInputEventConsistencyVerifier != null) {
6031 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6032 }
6033
Romain Guy02ccac62011-06-24 13:20:23 -07006034 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006035 }
6036
6037 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006038 * Dispatch a generic motion event.
6039 * <p>
6040 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6041 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006042 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006043 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006044 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006045 *
6046 * @param event The motion event to be dispatched.
6047 * @return True if the event was handled by the view, false otherwise.
6048 */
6049 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006050 if (mInputEventConsistencyVerifier != null) {
6051 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6052 }
6053
Jeff Browna032cc02011-03-07 16:56:21 -08006054 final int source = event.getSource();
6055 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6056 final int action = event.getAction();
6057 if (action == MotionEvent.ACTION_HOVER_ENTER
6058 || action == MotionEvent.ACTION_HOVER_MOVE
6059 || action == MotionEvent.ACTION_HOVER_EXIT) {
6060 if (dispatchHoverEvent(event)) {
6061 return true;
6062 }
6063 } else if (dispatchGenericPointerEvent(event)) {
6064 return true;
6065 }
6066 } else if (dispatchGenericFocusedEvent(event)) {
6067 return true;
6068 }
6069
Jeff Brown10b62902011-06-20 16:40:37 -07006070 if (dispatchGenericMotionEventInternal(event)) {
6071 return true;
6072 }
6073
6074 if (mInputEventConsistencyVerifier != null) {
6075 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6076 }
6077 return false;
6078 }
6079
6080 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006081 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006082 ListenerInfo li = mListenerInfo;
6083 if (li != null && li.mOnGenericMotionListener != null
6084 && (mViewFlags & ENABLED_MASK) == ENABLED
6085 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006086 return true;
6087 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006088
6089 if (onGenericMotionEvent(event)) {
6090 return true;
6091 }
6092
6093 if (mInputEventConsistencyVerifier != null) {
6094 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6095 }
6096 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006097 }
6098
6099 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006100 * Dispatch a hover event.
6101 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006102 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006103 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006104 * </p>
6105 *
6106 * @param event The motion event to be dispatched.
6107 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006108 */
6109 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006110 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006111 ListenerInfo li = mListenerInfo;
6112 if (li != null && li.mOnHoverListener != null
6113 && (mViewFlags & ENABLED_MASK) == ENABLED
6114 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006115 return true;
6116 }
6117
Jeff Browna032cc02011-03-07 16:56:21 -08006118 return onHoverEvent(event);
6119 }
6120
6121 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006122 * Returns true if the view has a child to which it has recently sent
6123 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6124 * it does not have a hovered child, then it must be the innermost hovered view.
6125 * @hide
6126 */
6127 protected boolean hasHoveredChild() {
6128 return false;
6129 }
6130
6131 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006132 * Dispatch a generic motion event to the view under the first pointer.
6133 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006134 * Do not call this method directly.
6135 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006136 * </p>
6137 *
6138 * @param event The motion event to be dispatched.
6139 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006140 */
6141 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6142 return false;
6143 }
6144
6145 /**
6146 * Dispatch a generic motion event to the currently focused view.
6147 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006148 * Do not call this method directly.
6149 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006150 * </p>
6151 *
6152 * @param event The motion event to be dispatched.
6153 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006154 */
6155 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6156 return false;
6157 }
6158
6159 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006160 * Dispatch a pointer event.
6161 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006162 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6163 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6164 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006165 * and should not be expected to handle other pointing device features.
6166 * </p>
6167 *
6168 * @param event The motion event to be dispatched.
6169 * @return True if the event was handled by the view, false otherwise.
6170 * @hide
6171 */
6172 public final boolean dispatchPointerEvent(MotionEvent event) {
6173 if (event.isTouchEvent()) {
6174 return dispatchTouchEvent(event);
6175 } else {
6176 return dispatchGenericMotionEvent(event);
6177 }
6178 }
6179
6180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 * Called when the window containing this view gains or loses window focus.
6182 * ViewGroups should override to route to their children.
6183 *
6184 * @param hasFocus True if the window containing this view now has focus,
6185 * false otherwise.
6186 */
6187 public void dispatchWindowFocusChanged(boolean hasFocus) {
6188 onWindowFocusChanged(hasFocus);
6189 }
6190
6191 /**
6192 * Called when the window containing this view gains or loses focus. Note
6193 * that this is separate from view focus: to receive key events, both
6194 * your view and its window must have focus. If a window is displayed
6195 * on top of yours that takes input focus, then your own window will lose
6196 * focus but the view focus will remain unchanged.
6197 *
6198 * @param hasWindowFocus True if the window containing this view now has
6199 * focus, false otherwise.
6200 */
6201 public void onWindowFocusChanged(boolean hasWindowFocus) {
6202 InputMethodManager imm = InputMethodManager.peekInstance();
6203 if (!hasWindowFocus) {
6204 if (isPressed()) {
6205 setPressed(false);
6206 }
6207 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6208 imm.focusOut(this);
6209 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006210 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006211 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006212 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6214 imm.focusIn(this);
6215 }
6216 refreshDrawableState();
6217 }
6218
6219 /**
6220 * Returns true if this view is in a window that currently has window focus.
6221 * Note that this is not the same as the view itself having focus.
6222 *
6223 * @return True if this view is in a window that currently has window focus.
6224 */
6225 public boolean hasWindowFocus() {
6226 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6227 }
6228
6229 /**
Adam Powell326d8082009-12-09 15:10:07 -08006230 * Dispatch a view visibility change down the view hierarchy.
6231 * ViewGroups should override to route to their children.
6232 * @param changedView The view whose visibility changed. Could be 'this' or
6233 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006234 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6235 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006236 */
6237 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6238 onVisibilityChanged(changedView, visibility);
6239 }
6240
6241 /**
6242 * Called when the visibility of the view or an ancestor of the view is changed.
6243 * @param changedView The view whose visibility changed. Could be 'this' or
6244 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006245 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6246 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006247 */
6248 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006249 if (visibility == VISIBLE) {
6250 if (mAttachInfo != null) {
6251 initialAwakenScrollBars();
6252 } else {
6253 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6254 }
6255 }
Adam Powell326d8082009-12-09 15:10:07 -08006256 }
6257
6258 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006259 * Dispatch a hint about whether this view is displayed. For instance, when
6260 * a View moves out of the screen, it might receives a display hint indicating
6261 * the view is not displayed. Applications should not <em>rely</em> on this hint
6262 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006263 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006264 * @param hint A hint about whether or not this view is displayed:
6265 * {@link #VISIBLE} or {@link #INVISIBLE}.
6266 */
6267 public void dispatchDisplayHint(int hint) {
6268 onDisplayHint(hint);
6269 }
6270
6271 /**
6272 * Gives this view a hint about whether is displayed or not. For instance, when
6273 * a View moves out of the screen, it might receives a display hint indicating
6274 * the view is not displayed. Applications should not <em>rely</em> on this hint
6275 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006276 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006277 * @param hint A hint about whether or not this view is displayed:
6278 * {@link #VISIBLE} or {@link #INVISIBLE}.
6279 */
6280 protected void onDisplayHint(int hint) {
6281 }
6282
6283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 * Dispatch a window visibility change down the view hierarchy.
6285 * ViewGroups should override to route to their children.
6286 *
6287 * @param visibility The new visibility of the window.
6288 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006289 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 */
6291 public void dispatchWindowVisibilityChanged(int visibility) {
6292 onWindowVisibilityChanged(visibility);
6293 }
6294
6295 /**
6296 * Called when the window containing has change its visibility
6297 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6298 * that this tells you whether or not your window is being made visible
6299 * to the window manager; this does <em>not</em> tell you whether or not
6300 * your window is obscured by other windows on the screen, even if it
6301 * is itself visible.
6302 *
6303 * @param visibility The new visibility of the window.
6304 */
6305 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006306 if (visibility == VISIBLE) {
6307 initialAwakenScrollBars();
6308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 }
6310
6311 /**
6312 * Returns the current visibility of the window this view is attached to
6313 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6314 *
6315 * @return Returns the current visibility of the view's window.
6316 */
6317 public int getWindowVisibility() {
6318 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6319 }
6320
6321 /**
6322 * Retrieve the overall visible display size in which the window this view is
6323 * attached to has been positioned in. This takes into account screen
6324 * decorations above the window, for both cases where the window itself
6325 * is being position inside of them or the window is being placed under
6326 * then and covered insets are used for the window to position its content
6327 * inside. In effect, this tells you the available area where content can
6328 * be placed and remain visible to users.
6329 *
6330 * <p>This function requires an IPC back to the window manager to retrieve
6331 * the requested information, so should not be used in performance critical
6332 * code like drawing.
6333 *
6334 * @param outRect Filled in with the visible display frame. If the view
6335 * is not attached to a window, this is simply the raw display size.
6336 */
6337 public void getWindowVisibleDisplayFrame(Rect outRect) {
6338 if (mAttachInfo != null) {
6339 try {
6340 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6341 } catch (RemoteException e) {
6342 return;
6343 }
6344 // XXX This is really broken, and probably all needs to be done
6345 // in the window manager, and we need to know more about whether
6346 // we want the area behind or in front of the IME.
6347 final Rect insets = mAttachInfo.mVisibleInsets;
6348 outRect.left += insets.left;
6349 outRect.top += insets.top;
6350 outRect.right -= insets.right;
6351 outRect.bottom -= insets.bottom;
6352 return;
6353 }
6354 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006355 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 }
6357
6358 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006359 * Dispatch a notification about a resource configuration change down
6360 * the view hierarchy.
6361 * ViewGroups should override to route to their children.
6362 *
6363 * @param newConfig The new resource configuration.
6364 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006365 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006366 */
6367 public void dispatchConfigurationChanged(Configuration newConfig) {
6368 onConfigurationChanged(newConfig);
6369 }
6370
6371 /**
6372 * Called when the current configuration of the resources being used
6373 * by the application have changed. You can use this to decide when
6374 * to reload resources that can changed based on orientation and other
6375 * configuration characterstics. You only need to use this if you are
6376 * not relying on the normal {@link android.app.Activity} mechanism of
6377 * recreating the activity instance upon a configuration change.
6378 *
6379 * @param newConfig The new resource configuration.
6380 */
6381 protected void onConfigurationChanged(Configuration newConfig) {
6382 }
6383
6384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 * Private function to aggregate all per-view attributes in to the view
6386 * root.
6387 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006388 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6389 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 }
6391
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006392 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6393 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006394 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006395 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006396 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006397 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006398 ListenerInfo li = mListenerInfo;
6399 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006400 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 }
6403 }
6404
6405 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006406 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006408 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6409 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 ai.mRecomputeGlobalAttributes = true;
6411 }
6412 }
6413 }
6414
6415 /**
6416 * Returns whether the device is currently in touch mode. Touch mode is entered
6417 * once the user begins interacting with the device by touch, and affects various
6418 * things like whether focus is always visible to the user.
6419 *
6420 * @return Whether the device is in touch mode.
6421 */
6422 @ViewDebug.ExportedProperty
6423 public boolean isInTouchMode() {
6424 if (mAttachInfo != null) {
6425 return mAttachInfo.mInTouchMode;
6426 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006427 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006428 }
6429 }
6430
6431 /**
6432 * Returns the context the view is running in, through which it can
6433 * access the current theme, resources, etc.
6434 *
6435 * @return The view's Context.
6436 */
6437 @ViewDebug.CapturedViewProperty
6438 public final Context getContext() {
6439 return mContext;
6440 }
6441
6442 /**
6443 * Handle a key event before it is processed by any input method
6444 * associated with the view hierarchy. This can be used to intercept
6445 * key events in special situations before the IME consumes them; a
6446 * typical example would be handling the BACK key to update the application's
6447 * UI instead of allowing the IME to see it and close itself.
6448 *
6449 * @param keyCode The value in event.getKeyCode().
6450 * @param event Description of the key event.
6451 * @return If you handled the event, return true. If you want to allow the
6452 * event to be handled by the next receiver, return false.
6453 */
6454 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6455 return false;
6456 }
6457
6458 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006459 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6460 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6462 * is released, if the view is enabled and clickable.
6463 *
6464 * @param keyCode A key code that represents the button pressed, from
6465 * {@link android.view.KeyEvent}.
6466 * @param event The KeyEvent object that defines the button action.
6467 */
6468 public boolean onKeyDown(int keyCode, KeyEvent event) {
6469 boolean result = false;
6470
6471 switch (keyCode) {
6472 case KeyEvent.KEYCODE_DPAD_CENTER:
6473 case KeyEvent.KEYCODE_ENTER: {
6474 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6475 return true;
6476 }
6477 // Long clickable items don't necessarily have to be clickable
6478 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6479 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6480 (event.getRepeatCount() == 0)) {
6481 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006482 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 return true;
6484 }
6485 break;
6486 }
6487 }
6488 return result;
6489 }
6490
6491 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006492 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6493 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6494 * the event).
6495 */
6496 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6497 return false;
6498 }
6499
6500 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006501 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6502 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6504 * {@link KeyEvent#KEYCODE_ENTER} is released.
6505 *
6506 * @param keyCode A key code that represents the button pressed, from
6507 * {@link android.view.KeyEvent}.
6508 * @param event The KeyEvent object that defines the button action.
6509 */
6510 public boolean onKeyUp(int keyCode, KeyEvent event) {
6511 boolean result = false;
6512
6513 switch (keyCode) {
6514 case KeyEvent.KEYCODE_DPAD_CENTER:
6515 case KeyEvent.KEYCODE_ENTER: {
6516 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6517 return true;
6518 }
6519 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6520 setPressed(false);
6521
6522 if (!mHasPerformedLongPress) {
6523 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006524 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525
6526 result = performClick();
6527 }
6528 }
6529 break;
6530 }
6531 }
6532 return result;
6533 }
6534
6535 /**
6536 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6537 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6538 * the event).
6539 *
6540 * @param keyCode A key code that represents the button pressed, from
6541 * {@link android.view.KeyEvent}.
6542 * @param repeatCount The number of times the action was made.
6543 * @param event The KeyEvent object that defines the button action.
6544 */
6545 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6546 return false;
6547 }
6548
6549 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006550 * Called on the focused view when a key shortcut event is not handled.
6551 * Override this method to implement local key shortcuts for the View.
6552 * Key shortcuts can also be implemented by setting the
6553 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 *
6555 * @param keyCode The value in event.getKeyCode().
6556 * @param event Description of the key event.
6557 * @return If you handled the event, return true. If you want to allow the
6558 * event to be handled by the next receiver, return false.
6559 */
6560 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6561 return false;
6562 }
6563
6564 /**
6565 * Check whether the called view is a text editor, in which case it
6566 * would make sense to automatically display a soft input window for
6567 * it. Subclasses should override this if they implement
6568 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006569 * a call on that method would return a non-null InputConnection, and
6570 * they are really a first-class editor that the user would normally
6571 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006572 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006573 * <p>The default implementation always returns false. This does
6574 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6575 * will not be called or the user can not otherwise perform edits on your
6576 * view; it is just a hint to the system that this is not the primary
6577 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006578 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 * @return Returns true if this view is a text editor, else false.
6580 */
6581 public boolean onCheckIsTextEditor() {
6582 return false;
6583 }
Romain Guy8506ab42009-06-11 17:35:47 -07006584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 /**
6586 * Create a new InputConnection for an InputMethod to interact
6587 * with the view. The default implementation returns null, since it doesn't
6588 * support input methods. You can override this to implement such support.
6589 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006590 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 * <p>When implementing this, you probably also want to implement
6592 * {@link #onCheckIsTextEditor()} to indicate you will return a
6593 * non-null InputConnection.
6594 *
6595 * @param outAttrs Fill in with attribute information about the connection.
6596 */
6597 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6598 return null;
6599 }
6600
6601 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006602 * Called by the {@link android.view.inputmethod.InputMethodManager}
6603 * when a view who is not the current
6604 * input connection target is trying to make a call on the manager. The
6605 * default implementation returns false; you can override this to return
6606 * true for certain views if you are performing InputConnection proxying
6607 * to them.
6608 * @param view The View that is making the InputMethodManager call.
6609 * @return Return true to allow the call, false to reject.
6610 */
6611 public boolean checkInputConnectionProxy(View view) {
6612 return false;
6613 }
Romain Guy8506ab42009-06-11 17:35:47 -07006614
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 * Show the context menu for this view. It is not safe to hold on to the
6617 * menu after returning from this method.
6618 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006619 * You should normally not overload this method. Overload
6620 * {@link #onCreateContextMenu(ContextMenu)} or define an
6621 * {@link OnCreateContextMenuListener} to add items to the context menu.
6622 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 * @param menu The context menu to populate
6624 */
6625 public void createContextMenu(ContextMenu menu) {
6626 ContextMenuInfo menuInfo = getContextMenuInfo();
6627
6628 // Sets the current menu info so all items added to menu will have
6629 // my extra info set.
6630 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6631
6632 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006633 ListenerInfo li = mListenerInfo;
6634 if (li != null && li.mOnCreateContextMenuListener != null) {
6635 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 }
6637
6638 // Clear the extra information so subsequent items that aren't mine don't
6639 // have my extra info.
6640 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6641
6642 if (mParent != null) {
6643 mParent.createContextMenu(menu);
6644 }
6645 }
6646
6647 /**
6648 * Views should implement this if they have extra information to associate
6649 * with the context menu. The return result is supplied as a parameter to
6650 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6651 * callback.
6652 *
6653 * @return Extra information about the item for which the context menu
6654 * should be shown. This information will vary across different
6655 * subclasses of View.
6656 */
6657 protected ContextMenuInfo getContextMenuInfo() {
6658 return null;
6659 }
6660
6661 /**
6662 * Views should implement this if the view itself is going to add items to
6663 * the context menu.
6664 *
6665 * @param menu the context menu to populate
6666 */
6667 protected void onCreateContextMenu(ContextMenu menu) {
6668 }
6669
6670 /**
6671 * Implement this method to handle trackball motion events. The
6672 * <em>relative</em> movement of the trackball since the last event
6673 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6674 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6675 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6676 * they will often be fractional values, representing the more fine-grained
6677 * movement information available from a trackball).
6678 *
6679 * @param event The motion event.
6680 * @return True if the event was handled, false otherwise.
6681 */
6682 public boolean onTrackballEvent(MotionEvent event) {
6683 return false;
6684 }
6685
6686 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006687 * Implement this method to handle generic motion events.
6688 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006689 * Generic motion events describe joystick movements, mouse hovers, track pad
6690 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006691 * {@link MotionEvent#getSource() source} of the motion event specifies
6692 * the class of input that was received. Implementations of this method
6693 * must examine the bits in the source before processing the event.
6694 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006695 * </p><p>
6696 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6697 * are delivered to the view under the pointer. All other generic motion events are
6698 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006699 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006700 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006701 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006702 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6703 * // process the joystick movement...
6704 * return true;
6705 * }
6706 * }
6707 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6708 * switch (event.getAction()) {
6709 * case MotionEvent.ACTION_HOVER_MOVE:
6710 * // process the mouse hover movement...
6711 * return true;
6712 * case MotionEvent.ACTION_SCROLL:
6713 * // process the scroll wheel movement...
6714 * return true;
6715 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006716 * }
6717 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006718 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006719 *
6720 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006721 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006722 */
6723 public boolean onGenericMotionEvent(MotionEvent event) {
6724 return false;
6725 }
6726
6727 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006728 * Implement this method to handle hover events.
6729 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006730 * This method is called whenever a pointer is hovering into, over, or out of the
6731 * bounds of a view and the view is not currently being touched.
6732 * Hover events are represented as pointer events with action
6733 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6734 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6735 * </p>
6736 * <ul>
6737 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6738 * when the pointer enters the bounds of the view.</li>
6739 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6740 * when the pointer has already entered the bounds of the view and has moved.</li>
6741 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6742 * when the pointer has exited the bounds of the view or when the pointer is
6743 * about to go down due to a button click, tap, or similar user action that
6744 * causes the view to be touched.</li>
6745 * </ul>
6746 * <p>
6747 * The view should implement this method to return true to indicate that it is
6748 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006749 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006750 * The default implementation calls {@link #setHovered} to update the hovered state
6751 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006752 * is enabled and is clickable. The default implementation also sends hover
6753 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006754 * </p>
6755 *
6756 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006757 * @return True if the view handled the hover event.
6758 *
6759 * @see #isHovered
6760 * @see #setHovered
6761 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006762 */
6763 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006764 // The root view may receive hover (or touch) events that are outside the bounds of
6765 // the window. This code ensures that we only send accessibility events for
6766 // hovers that are actually within the bounds of the root view.
6767 final int action = event.getAction();
6768 if (!mSendingHoverAccessibilityEvents) {
6769 if ((action == MotionEvent.ACTION_HOVER_ENTER
6770 || action == MotionEvent.ACTION_HOVER_MOVE)
6771 && !hasHoveredChild()
6772 && pointInView(event.getX(), event.getY())) {
6773 mSendingHoverAccessibilityEvents = true;
6774 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6775 }
6776 } else {
6777 if (action == MotionEvent.ACTION_HOVER_EXIT
6778 || (action == MotionEvent.ACTION_HOVER_MOVE
6779 && !pointInView(event.getX(), event.getY()))) {
6780 mSendingHoverAccessibilityEvents = false;
6781 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6782 }
Jeff Browna1b24182011-07-28 13:38:24 -07006783 }
6784
Jeff Brown87b7f802011-06-21 18:35:45 -07006785 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006786 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006787 case MotionEvent.ACTION_HOVER_ENTER:
6788 setHovered(true);
6789 break;
6790 case MotionEvent.ACTION_HOVER_EXIT:
6791 setHovered(false);
6792 break;
6793 }
Jeff Browna1b24182011-07-28 13:38:24 -07006794
6795 // Dispatch the event to onGenericMotionEvent before returning true.
6796 // This is to provide compatibility with existing applications that
6797 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6798 // break because of the new default handling for hoverable views
6799 // in onHoverEvent.
6800 // Note that onGenericMotionEvent will be called by default when
6801 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6802 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006803 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006804 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006805 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006806 }
6807
6808 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006809 * Returns true if the view should handle {@link #onHoverEvent}
6810 * by calling {@link #setHovered} to change its hovered state.
6811 *
6812 * @return True if the view is hoverable.
6813 */
6814 private boolean isHoverable() {
6815 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006816 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006817 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6818 return false;
6819 }
6820
6821 return (viewFlags & CLICKABLE) == CLICKABLE
6822 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6823 }
6824
6825 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006826 * Returns true if the view is currently hovered.
6827 *
6828 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006829 *
6830 * @see #setHovered
6831 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006832 */
Jeff Brown10b62902011-06-20 16:40:37 -07006833 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006834 public boolean isHovered() {
6835 return (mPrivateFlags & HOVERED) != 0;
6836 }
6837
6838 /**
6839 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006840 * <p>
6841 * Calling this method also changes the drawable state of the view. This
6842 * enables the view to react to hover by using different drawable resources
6843 * to change its appearance.
6844 * </p><p>
6845 * The {@link #onHoverChanged} method is called when the hovered state changes.
6846 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006847 *
6848 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006849 *
6850 * @see #isHovered
6851 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006852 */
6853 public void setHovered(boolean hovered) {
6854 if (hovered) {
6855 if ((mPrivateFlags & HOVERED) == 0) {
6856 mPrivateFlags |= HOVERED;
6857 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006858 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006859 }
6860 } else {
6861 if ((mPrivateFlags & HOVERED) != 0) {
6862 mPrivateFlags &= ~HOVERED;
6863 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006864 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006865 }
6866 }
6867 }
6868
6869 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006870 * Implement this method to handle hover state changes.
6871 * <p>
6872 * This method is called whenever the hover state changes as a result of a
6873 * call to {@link #setHovered}.
6874 * </p>
6875 *
6876 * @param hovered The current hover state, as returned by {@link #isHovered}.
6877 *
6878 * @see #isHovered
6879 * @see #setHovered
6880 */
6881 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006882 }
6883
6884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885 * Implement this method to handle touch screen motion events.
6886 *
6887 * @param event The motion event.
6888 * @return True if the event was handled, false otherwise.
6889 */
6890 public boolean onTouchEvent(MotionEvent event) {
6891 final int viewFlags = mViewFlags;
6892
6893 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006894 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006895 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006897 // A disabled view that is clickable still consumes the touch
6898 // events, it just doesn't respond to them.
6899 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6900 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6901 }
6902
6903 if (mTouchDelegate != null) {
6904 if (mTouchDelegate.onTouchEvent(event)) {
6905 return true;
6906 }
6907 }
6908
6909 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6910 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6911 switch (event.getAction()) {
6912 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006913 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6914 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 // take focus if we don't have it already and we should in
6916 // touch mode.
6917 boolean focusTaken = false;
6918 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6919 focusTaken = requestFocus();
6920 }
6921
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006922 if (prepressed) {
6923 // The button is being released before we actually
6924 // showed it as pressed. Make it show the pressed
6925 // state now (before scheduling the click) to ensure
6926 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006927 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006928 }
Joe Malin32736f02011-01-19 16:14:20 -08006929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 if (!mHasPerformedLongPress) {
6931 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006932 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933
6934 // Only perform take click actions if we were in the pressed state
6935 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006936 // Use a Runnable and post this rather than calling
6937 // performClick directly. This lets other visual state
6938 // of the view update before click actions start.
6939 if (mPerformClick == null) {
6940 mPerformClick = new PerformClick();
6941 }
6942 if (!post(mPerformClick)) {
6943 performClick();
6944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 }
6946 }
6947
6948 if (mUnsetPressedState == null) {
6949 mUnsetPressedState = new UnsetPressedState();
6950 }
6951
Adam Powelle14579b2009-12-16 18:39:52 -08006952 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006953 postDelayed(mUnsetPressedState,
6954 ViewConfiguration.getPressedStateDuration());
6955 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 // If the post failed, unpress right now
6957 mUnsetPressedState.run();
6958 }
Adam Powelle14579b2009-12-16 18:39:52 -08006959 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 }
6961 break;
6962
6963 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006964 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006965
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006966 if (performButtonActionOnTouchDown(event)) {
6967 break;
6968 }
6969
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006970 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006971 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006972
6973 // For views inside a scrolling container, delay the pressed feedback for
6974 // a short period in case this is a scroll.
6975 if (isInScrollingContainer) {
6976 mPrivateFlags |= PREPRESSED;
6977 if (mPendingCheckForTap == null) {
6978 mPendingCheckForTap = new CheckForTap();
6979 }
6980 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6981 } else {
6982 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006983 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006984 checkForLongClick(0);
6985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 break;
6987
6988 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006989 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006990 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006991 break;
6992
6993 case MotionEvent.ACTION_MOVE:
6994 final int x = (int) event.getX();
6995 final int y = (int) event.getY();
6996
6997 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006998 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007000 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007002 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007003 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004
Adam Powell4d6f0662012-02-21 15:11:11 -08007005 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 }
7008 break;
7009 }
7010 return true;
7011 }
7012
7013 return false;
7014 }
7015
7016 /**
Adam Powell10298662011-08-14 18:26:30 -07007017 * @hide
7018 */
7019 public boolean isInScrollingContainer() {
7020 ViewParent p = getParent();
7021 while (p != null && p instanceof ViewGroup) {
7022 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7023 return true;
7024 }
7025 p = p.getParent();
7026 }
7027 return false;
7028 }
7029
7030 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007031 * Remove the longpress detection timer.
7032 */
7033 private void removeLongPressCallback() {
7034 if (mPendingCheckForLongPress != null) {
7035 removeCallbacks(mPendingCheckForLongPress);
7036 }
7037 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007038
7039 /**
7040 * Remove the pending click action
7041 */
7042 private void removePerformClickCallback() {
7043 if (mPerformClick != null) {
7044 removeCallbacks(mPerformClick);
7045 }
7046 }
7047
Adam Powelle14579b2009-12-16 18:39:52 -08007048 /**
Romain Guya440b002010-02-24 15:57:54 -08007049 * Remove the prepress detection timer.
7050 */
7051 private void removeUnsetPressCallback() {
7052 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7053 setPressed(false);
7054 removeCallbacks(mUnsetPressedState);
7055 }
7056 }
7057
7058 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007059 * Remove the tap detection timer.
7060 */
7061 private void removeTapCallback() {
7062 if (mPendingCheckForTap != null) {
7063 mPrivateFlags &= ~PREPRESSED;
7064 removeCallbacks(mPendingCheckForTap);
7065 }
7066 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007067
7068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 * Cancels a pending long press. Your subclass can use this if you
7070 * want the context menu to come up if the user presses and holds
7071 * at the same place, but you don't want it to come up if they press
7072 * and then move around enough to cause scrolling.
7073 */
7074 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007075 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007076
7077 /*
7078 * The prepressed state handled by the tap callback is a display
7079 * construct, but the tap callback will post a long press callback
7080 * less its own timeout. Remove it here.
7081 */
7082 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 }
7084
7085 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007086 * Remove the pending callback for sending a
7087 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7088 */
7089 private void removeSendViewScrolledAccessibilityEventCallback() {
7090 if (mSendViewScrolledAccessibilityEvent != null) {
7091 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7092 }
7093 }
7094
7095 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 * Sets the TouchDelegate for this View.
7097 */
7098 public void setTouchDelegate(TouchDelegate delegate) {
7099 mTouchDelegate = delegate;
7100 }
7101
7102 /**
7103 * Gets the TouchDelegate for this View.
7104 */
7105 public TouchDelegate getTouchDelegate() {
7106 return mTouchDelegate;
7107 }
7108
7109 /**
7110 * Set flags controlling behavior of this view.
7111 *
7112 * @param flags Constant indicating the value which should be set
7113 * @param mask Constant indicating the bit range that should be changed
7114 */
7115 void setFlags(int flags, int mask) {
7116 int old = mViewFlags;
7117 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7118
7119 int changed = mViewFlags ^ old;
7120 if (changed == 0) {
7121 return;
7122 }
7123 int privateFlags = mPrivateFlags;
7124
7125 /* Check if the FOCUSABLE bit has changed */
7126 if (((changed & FOCUSABLE_MASK) != 0) &&
7127 ((privateFlags & HAS_BOUNDS) !=0)) {
7128 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7129 && ((privateFlags & FOCUSED) != 0)) {
7130 /* Give up focus if we are no longer focusable */
7131 clearFocus();
7132 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7133 && ((privateFlags & FOCUSED) == 0)) {
7134 /*
7135 * Tell the view system that we are now available to take focus
7136 * if no one else already has it.
7137 */
7138 if (mParent != null) mParent.focusableViewAvailable(this);
7139 }
7140 }
7141
7142 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7143 if ((changed & VISIBILITY_MASK) != 0) {
7144 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007145 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007146 * it was not visible. Marking it drawn ensures that the invalidation will
7147 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007149 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007150 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151
7152 needGlobalAttributesUpdate(true);
7153
7154 // a view becoming visible is worth notifying the parent
7155 // about in case nothing has focus. even if this specific view
7156 // isn't focusable, it may contain something that is, so let
7157 // the root view try to give this focus if nothing else does.
7158 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7159 mParent.focusableViewAvailable(this);
7160 }
7161 }
7162 }
7163
7164 /* Check if the GONE bit has changed */
7165 if ((changed & GONE) != 0) {
7166 needGlobalAttributesUpdate(false);
7167 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168
Romain Guyecd80ee2009-12-03 17:13:02 -08007169 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7170 if (hasFocus()) clearFocus();
7171 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007172 if (mParent instanceof View) {
7173 // GONE views noop invalidation, so invalidate the parent
7174 ((View) mParent).invalidate(true);
7175 }
7176 // Mark the view drawn to ensure that it gets invalidated properly the next
7177 // time it is visible and gets invalidated
7178 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 }
7180 if (mAttachInfo != null) {
7181 mAttachInfo.mViewVisibilityChanged = true;
7182 }
7183 }
7184
7185 /* Check if the VISIBLE bit has changed */
7186 if ((changed & INVISIBLE) != 0) {
7187 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007188 /*
7189 * If this view is becoming invisible, set the DRAWN flag so that
7190 * the next invalidate() will not be skipped.
7191 */
7192 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193
7194 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
7195 // root view becoming invisible shouldn't clear focus
7196 if (getRootView() != this) {
7197 clearFocus();
7198 }
7199 }
7200 if (mAttachInfo != null) {
7201 mAttachInfo.mViewVisibilityChanged = true;
7202 }
7203 }
7204
Adam Powell326d8082009-12-09 15:10:07 -08007205 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007206 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007207 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7208 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007209 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007210 } else if (mParent != null) {
7211 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007212 }
Adam Powell326d8082009-12-09 15:10:07 -08007213 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7214 }
7215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7217 destroyDrawingCache();
7218 }
7219
7220 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7221 destroyDrawingCache();
7222 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007223 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 }
7225
7226 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7227 destroyDrawingCache();
7228 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7229 }
7230
7231 if ((changed & DRAW_MASK) != 0) {
7232 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007233 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007234 mPrivateFlags &= ~SKIP_DRAW;
7235 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7236 } else {
7237 mPrivateFlags |= SKIP_DRAW;
7238 }
7239 } else {
7240 mPrivateFlags &= ~SKIP_DRAW;
7241 }
7242 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007243 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 }
7245
7246 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007247 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 mParent.recomputeViewAttributes(this);
7249 }
7250 }
7251 }
7252
7253 /**
7254 * Change the view's z order in the tree, so it's on top of other sibling
7255 * views
7256 */
7257 public void bringToFront() {
7258 if (mParent != null) {
7259 mParent.bringChildToFront(this);
7260 }
7261 }
7262
7263 /**
7264 * This is called in response to an internal scroll in this view (i.e., the
7265 * view scrolled its own contents). This is typically as a result of
7266 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7267 * called.
7268 *
7269 * @param l Current horizontal scroll origin.
7270 * @param t Current vertical scroll origin.
7271 * @param oldl Previous horizontal scroll origin.
7272 * @param oldt Previous vertical scroll origin.
7273 */
7274 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007275 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7276 postSendViewScrolledAccessibilityEventCallback();
7277 }
7278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 mBackgroundSizeChanged = true;
7280
7281 final AttachInfo ai = mAttachInfo;
7282 if (ai != null) {
7283 ai.mViewScrollChanged = true;
7284 }
7285 }
7286
7287 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007288 * Interface definition for a callback to be invoked when the layout bounds of a view
7289 * changes due to layout processing.
7290 */
7291 public interface OnLayoutChangeListener {
7292 /**
7293 * Called when the focus state of a view has changed.
7294 *
7295 * @param v The view whose state has changed.
7296 * @param left The new value of the view's left property.
7297 * @param top The new value of the view's top property.
7298 * @param right The new value of the view's right property.
7299 * @param bottom The new value of the view's bottom property.
7300 * @param oldLeft The previous value of the view's left property.
7301 * @param oldTop The previous value of the view's top property.
7302 * @param oldRight The previous value of the view's right property.
7303 * @param oldBottom The previous value of the view's bottom property.
7304 */
7305 void onLayoutChange(View v, int left, int top, int right, int bottom,
7306 int oldLeft, int oldTop, int oldRight, int oldBottom);
7307 }
7308
7309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 * This is called during layout when the size of this view has changed. If
7311 * you were just added to the view hierarchy, you're called with the old
7312 * values of 0.
7313 *
7314 * @param w Current width of this view.
7315 * @param h Current height of this view.
7316 * @param oldw Old width of this view.
7317 * @param oldh Old height of this view.
7318 */
7319 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7320 }
7321
7322 /**
7323 * Called by draw to draw the child views. This may be overridden
7324 * by derived classes to gain control just before its children are drawn
7325 * (but after its own view has been drawn).
7326 * @param canvas the canvas on which to draw the view
7327 */
7328 protected void dispatchDraw(Canvas canvas) {
7329 }
7330
7331 /**
7332 * Gets the parent of this view. Note that the parent is a
7333 * ViewParent and not necessarily a View.
7334 *
7335 * @return Parent of this view.
7336 */
7337 public final ViewParent getParent() {
7338 return mParent;
7339 }
7340
7341 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007342 * Set the horizontal scrolled position of your view. This will cause a call to
7343 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7344 * invalidated.
7345 * @param value the x position to scroll to
7346 */
7347 public void setScrollX(int value) {
7348 scrollTo(value, mScrollY);
7349 }
7350
7351 /**
7352 * Set the vertical scrolled position of your view. This will cause a call to
7353 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7354 * invalidated.
7355 * @param value the y position to scroll to
7356 */
7357 public void setScrollY(int value) {
7358 scrollTo(mScrollX, value);
7359 }
7360
7361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 * Return the scrolled left position of this view. This is the left edge of
7363 * the displayed part of your view. You do not need to draw any pixels
7364 * farther left, since those are outside of the frame of your view on
7365 * screen.
7366 *
7367 * @return The left edge of the displayed part of your view, in pixels.
7368 */
7369 public final int getScrollX() {
7370 return mScrollX;
7371 }
7372
7373 /**
7374 * Return the scrolled top position of this view. This is the top edge of
7375 * the displayed part of your view. You do not need to draw any pixels above
7376 * it, since those are outside of the frame of your view on screen.
7377 *
7378 * @return The top edge of the displayed part of your view, in pixels.
7379 */
7380 public final int getScrollY() {
7381 return mScrollY;
7382 }
7383
7384 /**
7385 * Return the width of the your view.
7386 *
7387 * @return The width of your view, in pixels.
7388 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007389 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007390 public final int getWidth() {
7391 return mRight - mLeft;
7392 }
7393
7394 /**
7395 * Return the height of your view.
7396 *
7397 * @return The height of your view, in pixels.
7398 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007399 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007400 public final int getHeight() {
7401 return mBottom - mTop;
7402 }
7403
7404 /**
7405 * Return the visible drawing bounds of your view. Fills in the output
7406 * rectangle with the values from getScrollX(), getScrollY(),
7407 * getWidth(), and getHeight().
7408 *
7409 * @param outRect The (scrolled) drawing bounds of the view.
7410 */
7411 public void getDrawingRect(Rect outRect) {
7412 outRect.left = mScrollX;
7413 outRect.top = mScrollY;
7414 outRect.right = mScrollX + (mRight - mLeft);
7415 outRect.bottom = mScrollY + (mBottom - mTop);
7416 }
7417
7418 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007419 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7420 * raw width component (that is the result is masked by
7421 * {@link #MEASURED_SIZE_MASK}).
7422 *
7423 * @return The raw measured width of this view.
7424 */
7425 public final int getMeasuredWidth() {
7426 return mMeasuredWidth & MEASURED_SIZE_MASK;
7427 }
7428
7429 /**
7430 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007431 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007432 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 * This should be used during measurement and layout calculations only. Use
7434 * {@link #getWidth()} to see how wide a view is after layout.
7435 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007436 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007438 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 return mMeasuredWidth;
7440 }
7441
7442 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007443 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7444 * raw width component (that is the result is masked by
7445 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007447 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 */
7449 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007450 return mMeasuredHeight & MEASURED_SIZE_MASK;
7451 }
7452
7453 /**
7454 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007455 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007456 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7457 * This should be used during measurement and layout calculations only. Use
7458 * {@link #getHeight()} to see how wide a view is after layout.
7459 *
7460 * @return The measured width of this view as a bit mask.
7461 */
7462 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 return mMeasuredHeight;
7464 }
7465
7466 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007467 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7468 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7469 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7470 * and the height component is at the shifted bits
7471 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7472 */
7473 public final int getMeasuredState() {
7474 return (mMeasuredWidth&MEASURED_STATE_MASK)
7475 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7476 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7477 }
7478
7479 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007480 * The transform matrix of this view, which is calculated based on the current
7481 * roation, scale, and pivot properties.
7482 *
7483 * @see #getRotation()
7484 * @see #getScaleX()
7485 * @see #getScaleY()
7486 * @see #getPivotX()
7487 * @see #getPivotY()
7488 * @return The current transform matrix for the view
7489 */
7490 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007491 if (mTransformationInfo != null) {
7492 updateMatrix();
7493 return mTransformationInfo.mMatrix;
7494 }
7495 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007496 }
7497
7498 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007499 * Utility function to determine if the value is far enough away from zero to be
7500 * considered non-zero.
7501 * @param value A floating point value to check for zero-ness
7502 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7503 */
7504 private static boolean nonzero(float value) {
7505 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7506 }
7507
7508 /**
Jeff Brown86671742010-09-30 20:00:15 -07007509 * Returns true if the transform matrix is the identity matrix.
7510 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007511 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007512 * @return True if the transform matrix is the identity matrix, false otherwise.
7513 */
Jeff Brown86671742010-09-30 20:00:15 -07007514 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007515 if (mTransformationInfo != null) {
7516 updateMatrix();
7517 return mTransformationInfo.mMatrixIsIdentity;
7518 }
7519 return true;
7520 }
7521
7522 void ensureTransformationInfo() {
7523 if (mTransformationInfo == null) {
7524 mTransformationInfo = new TransformationInfo();
7525 }
Jeff Brown86671742010-09-30 20:00:15 -07007526 }
7527
7528 /**
7529 * Recomputes the transform matrix if necessary.
7530 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007531 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007532 final TransformationInfo info = mTransformationInfo;
7533 if (info == null) {
7534 return;
7535 }
7536 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007537 // transform-related properties have changed since the last time someone
7538 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007539
7540 // Figure out if we need to update the pivot point
7541 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007542 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7543 info.mPrevWidth = mRight - mLeft;
7544 info.mPrevHeight = mBottom - mTop;
7545 info.mPivotX = info.mPrevWidth / 2f;
7546 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007547 }
7548 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007549 info.mMatrix.reset();
7550 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7551 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7552 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7553 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007554 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007555 if (info.mCamera == null) {
7556 info.mCamera = new Camera();
7557 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007558 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007559 info.mCamera.save();
7560 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7561 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7562 info.mCamera.getMatrix(info.matrix3D);
7563 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7564 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7565 info.mPivotY + info.mTranslationY);
7566 info.mMatrix.postConcat(info.matrix3D);
7567 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007568 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 info.mMatrixDirty = false;
7570 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7571 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007572 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007573 }
7574
7575 /**
7576 * Utility method to retrieve the inverse of the current mMatrix property.
7577 * We cache the matrix to avoid recalculating it when transform properties
7578 * have not changed.
7579 *
7580 * @return The inverse of the current matrix of this view.
7581 */
Jeff Brown86671742010-09-30 20:00:15 -07007582 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007583 final TransformationInfo info = mTransformationInfo;
7584 if (info != null) {
7585 updateMatrix();
7586 if (info.mInverseMatrixDirty) {
7587 if (info.mInverseMatrix == null) {
7588 info.mInverseMatrix = new Matrix();
7589 }
7590 info.mMatrix.invert(info.mInverseMatrix);
7591 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007592 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007593 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007594 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007595 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 }
7597
7598 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007599 * Gets the distance along the Z axis from the camera to this view.
7600 *
7601 * @see #setCameraDistance(float)
7602 *
7603 * @return The distance along the Z axis.
7604 */
7605 public float getCameraDistance() {
7606 ensureTransformationInfo();
7607 final float dpi = mResources.getDisplayMetrics().densityDpi;
7608 final TransformationInfo info = mTransformationInfo;
7609 if (info.mCamera == null) {
7610 info.mCamera = new Camera();
7611 info.matrix3D = new Matrix();
7612 }
7613 return -(info.mCamera.getLocationZ() * dpi);
7614 }
7615
7616 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007617 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7618 * views are drawn) from the camera to this view. The camera's distance
7619 * affects 3D transformations, for instance rotations around the X and Y
7620 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07007621 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08007622 * use a camera distance that's greater than the height (X axis rotation) or
7623 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007624 *
Romain Guya5364ee2011-02-24 14:46:04 -08007625 * <p>The distance of the camera from the view plane can have an affect on the
7626 * perspective distortion of the view when it is rotated around the x or y axis.
7627 * For example, a large distance will result in a large viewing angle, and there
7628 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07007629 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08007630 * also result in some drawing artifacts if the rotated view ends up partially
7631 * behind the camera (which is why the recommendation is to use a distance at
7632 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007633 *
Romain Guya5364ee2011-02-24 14:46:04 -08007634 * <p>The distance is expressed in "depth pixels." The default distance depends
7635 * on the screen density. For instance, on a medium density display, the
7636 * default distance is 1280. On a high density display, the default distance
7637 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007638 *
Romain Guya5364ee2011-02-24 14:46:04 -08007639 * <p>If you want to specify a distance that leads to visually consistent
7640 * results across various densities, use the following formula:</p>
7641 * <pre>
7642 * float scale = context.getResources().getDisplayMetrics().density;
7643 * view.setCameraDistance(distance * scale);
7644 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07007645 *
Romain Guya5364ee2011-02-24 14:46:04 -08007646 * <p>The density scale factor of a high density display is 1.5,
7647 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007648 *
Romain Guya5364ee2011-02-24 14:46:04 -08007649 * @param distance The distance in "depth pixels", if negative the opposite
7650 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07007651 *
7652 * @see #setRotationX(float)
7653 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08007654 */
7655 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007656 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007657
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007658 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007659 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007660 final TransformationInfo info = mTransformationInfo;
7661 if (info.mCamera == null) {
7662 info.mCamera = new Camera();
7663 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007664 }
7665
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007666 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7667 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007668
Chet Haase9d1992d2012-03-13 11:03:25 -07007669 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007670 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07007671 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08007672 }
Romain Guya5364ee2011-02-24 14:46:04 -08007673 }
7674
7675 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007676 * The degrees that the view is rotated around the pivot point.
7677 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007678 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007679 * @see #getPivotX()
7680 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007681 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007682 * @return The degrees of rotation.
7683 */
Chet Haasea5531132012-02-02 13:41:44 -08007684 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007685 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007686 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007687 }
7688
7689 /**
Chet Haase897247b2010-09-09 14:54:47 -07007690 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7691 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007692 *
7693 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007694 *
7695 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007696 * @see #getPivotX()
7697 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007698 * @see #setRotationX(float)
7699 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007700 *
7701 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007702 */
7703 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007704 ensureTransformationInfo();
7705 final TransformationInfo info = mTransformationInfo;
7706 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007707 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007708 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007709 info.mRotation = rotation;
7710 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007711 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007712 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7713 mDisplayList.setRotation(rotation);
7714 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007715 }
7716 }
7717
7718 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007719 * The degrees that the view is rotated around the vertical axis through the pivot point.
7720 *
7721 * @see #getPivotX()
7722 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007723 * @see #setRotationY(float)
7724 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007725 * @return The degrees of Y rotation.
7726 */
Chet Haasea5531132012-02-02 13:41:44 -08007727 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007728 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007729 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007730 }
7731
7732 /**
Chet Haase897247b2010-09-09 14:54:47 -07007733 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7734 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7735 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007736 *
Romain Guya5364ee2011-02-24 14:46:04 -08007737 * When rotating large views, it is recommended to adjust the camera distance
7738 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007739 *
7740 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007741 *
7742 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007743 * @see #getPivotX()
7744 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007745 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007746 * @see #setRotationX(float)
7747 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007748 *
7749 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007750 */
7751 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007752 ensureTransformationInfo();
7753 final TransformationInfo info = mTransformationInfo;
7754 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007755 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007756 info.mRotationY = rotationY;
7757 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007758 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007759 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7760 mDisplayList.setRotationY(rotationY);
7761 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007762 }
7763 }
7764
7765 /**
7766 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7767 *
7768 * @see #getPivotX()
7769 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007770 * @see #setRotationX(float)
7771 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007772 * @return The degrees of X rotation.
7773 */
Chet Haasea5531132012-02-02 13:41:44 -08007774 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007775 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007776 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007777 }
7778
7779 /**
Chet Haase897247b2010-09-09 14:54:47 -07007780 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7781 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7782 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007783 *
Romain Guya5364ee2011-02-24 14:46:04 -08007784 * When rotating large views, it is recommended to adjust the camera distance
7785 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007786 *
7787 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007788 *
7789 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007790 * @see #getPivotX()
7791 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007792 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007793 * @see #setRotationY(float)
7794 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007795 *
7796 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007797 */
7798 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007799 ensureTransformationInfo();
7800 final TransformationInfo info = mTransformationInfo;
7801 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007802 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007803 info.mRotationX = rotationX;
7804 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007805 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007806 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7807 mDisplayList.setRotationX(rotationX);
7808 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007809 }
7810 }
7811
7812 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007813 * The amount that the view is scaled in x around the pivot point, as a proportion of
7814 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7815 *
Joe Onorato93162322010-09-16 15:42:01 -04007816 * <p>By default, this is 1.0f.
7817 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007818 * @see #getPivotX()
7819 * @see #getPivotY()
7820 * @return The scaling factor.
7821 */
Chet Haasea5531132012-02-02 13:41:44 -08007822 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007823 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007824 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007825 }
7826
7827 /**
7828 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7829 * the view's unscaled width. A value of 1 means that no scaling is applied.
7830 *
7831 * @param scaleX The scaling factor.
7832 * @see #getPivotX()
7833 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007834 *
7835 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007836 */
7837 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007838 ensureTransformationInfo();
7839 final TransformationInfo info = mTransformationInfo;
7840 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007841 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007842 info.mScaleX = scaleX;
7843 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007844 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007845 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7846 mDisplayList.setScaleX(scaleX);
7847 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007848 }
7849 }
7850
7851 /**
7852 * The amount that the view is scaled in y around the pivot point, as a proportion of
7853 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7854 *
Joe Onorato93162322010-09-16 15:42:01 -04007855 * <p>By default, this is 1.0f.
7856 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007857 * @see #getPivotX()
7858 * @see #getPivotY()
7859 * @return The scaling factor.
7860 */
Chet Haasea5531132012-02-02 13:41:44 -08007861 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007862 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007863 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007864 }
7865
7866 /**
7867 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7868 * the view's unscaled width. A value of 1 means that no scaling is applied.
7869 *
7870 * @param scaleY The scaling factor.
7871 * @see #getPivotX()
7872 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007873 *
7874 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007875 */
7876 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007877 ensureTransformationInfo();
7878 final TransformationInfo info = mTransformationInfo;
7879 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007880 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007881 info.mScaleY = scaleY;
7882 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007883 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007884 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7885 mDisplayList.setScaleY(scaleY);
7886 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007887 }
7888 }
7889
7890 /**
7891 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7892 * and {@link #setScaleX(float) scaled}.
7893 *
7894 * @see #getRotation()
7895 * @see #getScaleX()
7896 * @see #getScaleY()
7897 * @see #getPivotY()
7898 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007899 *
7900 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007901 */
Chet Haasea5531132012-02-02 13:41:44 -08007902 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007903 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007904 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007905 }
7906
7907 /**
7908 * Sets the x location of the point around which the view is
7909 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007910 * By default, the pivot point is centered on the object.
7911 * Setting this property disables this behavior and causes the view to use only the
7912 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007913 *
7914 * @param pivotX The x location of the pivot point.
7915 * @see #getRotation()
7916 * @see #getScaleX()
7917 * @see #getScaleY()
7918 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007919 *
7920 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007921 */
7922 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007923 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007924 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007925 final TransformationInfo info = mTransformationInfo;
7926 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007927 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007928 info.mPivotX = pivotX;
7929 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007930 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007931 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7932 mDisplayList.setPivotX(pivotX);
7933 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007934 }
7935 }
7936
7937 /**
7938 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7939 * and {@link #setScaleY(float) scaled}.
7940 *
7941 * @see #getRotation()
7942 * @see #getScaleX()
7943 * @see #getScaleY()
7944 * @see #getPivotY()
7945 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007946 *
7947 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007948 */
Chet Haasea5531132012-02-02 13:41:44 -08007949 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007950 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007951 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007952 }
7953
7954 /**
7955 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007956 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7957 * Setting this property disables this behavior and causes the view to use only the
7958 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007959 *
7960 * @param pivotY The y location of the pivot point.
7961 * @see #getRotation()
7962 * @see #getScaleX()
7963 * @see #getScaleY()
7964 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007965 *
7966 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007967 */
7968 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007969 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007970 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007971 final TransformationInfo info = mTransformationInfo;
7972 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007973 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007974 info.mPivotY = pivotY;
7975 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007976 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007977 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7978 mDisplayList.setPivotY(pivotY);
7979 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007980 }
7981 }
7982
7983 /**
7984 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7985 * completely transparent and 1 means the view is completely opaque.
7986 *
Joe Onorato93162322010-09-16 15:42:01 -04007987 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007988 * @return The opacity of the view.
7989 */
Chet Haasea5531132012-02-02 13:41:44 -08007990 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007991 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007992 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007993 }
7994
7995 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07007996 * Returns whether this View has content which overlaps. This function, intended to be
7997 * overridden by specific View types, is an optimization when alpha is set on a view. If
7998 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
7999 * and then composited it into place, which can be expensive. If the view has no overlapping
8000 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8001 * An example of overlapping rendering is a TextView with a background image, such as a
8002 * Button. An example of non-overlapping rendering is a TextView with no background, or
8003 * an ImageView with only the foreground image. The default implementation returns true;
8004 * subclasses should override if they have cases which can be optimized.
8005 *
8006 * @return true if the content in this view might overlap, false otherwise.
8007 */
8008 public boolean hasOverlappingRendering() {
8009 return true;
8010 }
8011
8012 /**
Romain Guy171c5922011-01-06 10:04:23 -08008013 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8014 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008015 *
Romain Guy171c5922011-01-06 10:04:23 -08008016 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8017 * responsible for applying the opacity itself. Otherwise, calling this method is
8018 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008019 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008020 *
Chet Haasea5531132012-02-02 13:41:44 -08008021 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8022 * performance implications. It is generally best to use the alpha property sparingly and
8023 * transiently, as in the case of fading animations.</p>
8024 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008025 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008026 *
Joe Malin32736f02011-01-19 16:14:20 -08008027 * @see #setLayerType(int, android.graphics.Paint)
8028 *
Chet Haase73066682010-11-29 15:55:32 -08008029 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008030 */
8031 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008032 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008033 if (mTransformationInfo.mAlpha != alpha) {
8034 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008035 if (onSetAlpha((int) (alpha * 255))) {
8036 mPrivateFlags |= ALPHA_SET;
8037 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008038 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008039 invalidate(true);
8040 } else {
8041 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008042 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008043 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8044 mDisplayList.setAlpha(alpha);
8045 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008046 }
Chet Haaseed032702010-10-01 14:05:54 -07008047 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008048 }
8049
8050 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008051 * Faster version of setAlpha() which performs the same steps except there are
8052 * no calls to invalidate(). The caller of this function should perform proper invalidation
8053 * on the parent and this object. The return value indicates whether the subclass handles
8054 * alpha (the return value for onSetAlpha()).
8055 *
8056 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008057 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8058 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008059 */
8060 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008061 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008062 if (mTransformationInfo.mAlpha != alpha) {
8063 mTransformationInfo.mAlpha = alpha;
8064 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8065 if (subclassHandlesAlpha) {
8066 mPrivateFlags |= ALPHA_SET;
8067 return true;
8068 } else {
8069 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008070 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8071 mDisplayList.setAlpha(alpha);
8072 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008073 }
Chet Haasea00f3862011-02-22 06:34:40 -08008074 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008075 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008076 }
8077
8078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079 * Top position of this view relative to its parent.
8080 *
8081 * @return The top of this view, in pixels.
8082 */
8083 @ViewDebug.CapturedViewProperty
8084 public final int getTop() {
8085 return mTop;
8086 }
8087
8088 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008089 * Sets the top position of this view relative to its parent. This method is meant to be called
8090 * by the layout system and should not generally be called otherwise, because the property
8091 * may be changed at any time by the layout.
8092 *
8093 * @param top The top of this view, in pixels.
8094 */
8095 public final void setTop(int top) {
8096 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008097 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008098 final boolean matrixIsIdentity = mTransformationInfo == null
8099 || mTransformationInfo.mMatrixIsIdentity;
8100 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008101 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008102 int minTop;
8103 int yLoc;
8104 if (top < mTop) {
8105 minTop = top;
8106 yLoc = top - mTop;
8107 } else {
8108 minTop = mTop;
8109 yLoc = 0;
8110 }
Chet Haasee9140a72011-02-16 16:23:29 -08008111 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008112 }
8113 } else {
8114 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008115 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008116 }
8117
Chet Haaseed032702010-10-01 14:05:54 -07008118 int width = mRight - mLeft;
8119 int oldHeight = mBottom - mTop;
8120
Chet Haase21cd1382010-09-01 17:42:29 -07008121 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008122 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8123 mDisplayList.setTop(mTop);
8124 }
Chet Haase21cd1382010-09-01 17:42:29 -07008125
Chet Haaseed032702010-10-01 14:05:54 -07008126 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8127
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008128 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008129 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8130 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008131 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008132 }
Chet Haase21cd1382010-09-01 17:42:29 -07008133 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008134 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008135 }
Chet Haase55dbb652010-12-21 20:15:08 -08008136 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008137 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008138 }
8139 }
8140
8141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 * Bottom position of this view relative to its parent.
8143 *
8144 * @return The bottom of this view, in pixels.
8145 */
8146 @ViewDebug.CapturedViewProperty
8147 public final int getBottom() {
8148 return mBottom;
8149 }
8150
8151 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008152 * True if this view has changed since the last time being drawn.
8153 *
8154 * @return The dirty state of this view.
8155 */
8156 public boolean isDirty() {
8157 return (mPrivateFlags & DIRTY_MASK) != 0;
8158 }
8159
8160 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008161 * Sets the bottom position of this view relative to its parent. This method is meant to be
8162 * called by the layout system and should not generally be called otherwise, because the
8163 * property may be changed at any time by the layout.
8164 *
8165 * @param bottom The bottom of this view, in pixels.
8166 */
8167 public final void setBottom(int bottom) {
8168 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008169 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008170 final boolean matrixIsIdentity = mTransformationInfo == null
8171 || mTransformationInfo.mMatrixIsIdentity;
8172 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008173 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008174 int maxBottom;
8175 if (bottom < mBottom) {
8176 maxBottom = mBottom;
8177 } else {
8178 maxBottom = bottom;
8179 }
Chet Haasee9140a72011-02-16 16:23:29 -08008180 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008181 }
8182 } else {
8183 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008184 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008185 }
8186
Chet Haaseed032702010-10-01 14:05:54 -07008187 int width = mRight - mLeft;
8188 int oldHeight = mBottom - mTop;
8189
Chet Haase21cd1382010-09-01 17:42:29 -07008190 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008191 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8192 mDisplayList.setBottom(mBottom);
8193 }
Chet Haase21cd1382010-09-01 17:42:29 -07008194
Chet Haaseed032702010-10-01 14:05:54 -07008195 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8196
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008197 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008198 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8199 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008200 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008201 }
Chet Haase21cd1382010-09-01 17:42:29 -07008202 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008203 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008204 }
Chet Haase55dbb652010-12-21 20:15:08 -08008205 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008206 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008207 }
8208 }
8209
8210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 * Left position of this view relative to its parent.
8212 *
8213 * @return The left edge of this view, in pixels.
8214 */
8215 @ViewDebug.CapturedViewProperty
8216 public final int getLeft() {
8217 return mLeft;
8218 }
8219
8220 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008221 * Sets the left position of this view relative to its parent. This method is meant to be called
8222 * by the layout system and should not generally be called otherwise, because the property
8223 * may be changed at any time by the layout.
8224 *
8225 * @param left The bottom of this view, in pixels.
8226 */
8227 public final void setLeft(int left) {
8228 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008229 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008230 final boolean matrixIsIdentity = mTransformationInfo == null
8231 || mTransformationInfo.mMatrixIsIdentity;
8232 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008233 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008234 int minLeft;
8235 int xLoc;
8236 if (left < mLeft) {
8237 minLeft = left;
8238 xLoc = left - mLeft;
8239 } else {
8240 minLeft = mLeft;
8241 xLoc = 0;
8242 }
Chet Haasee9140a72011-02-16 16:23:29 -08008243 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008244 }
8245 } else {
8246 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008247 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008248 }
8249
Chet Haaseed032702010-10-01 14:05:54 -07008250 int oldWidth = mRight - mLeft;
8251 int height = mBottom - mTop;
8252
Chet Haase21cd1382010-09-01 17:42:29 -07008253 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008254 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8255 mDisplayList.setLeft(left);
8256 }
Chet Haase21cd1382010-09-01 17:42:29 -07008257
Chet Haaseed032702010-10-01 14:05:54 -07008258 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8259
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008260 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008261 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8262 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008263 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008264 }
Chet Haase21cd1382010-09-01 17:42:29 -07008265 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008266 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008267 }
Chet Haase55dbb652010-12-21 20:15:08 -08008268 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008269 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008270 if (USE_DISPLAY_LIST_PROPERTIES) {
8271
8272 }
Chet Haase21cd1382010-09-01 17:42:29 -07008273 }
8274 }
8275
8276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 * Right position of this view relative to its parent.
8278 *
8279 * @return The right edge of this view, in pixels.
8280 */
8281 @ViewDebug.CapturedViewProperty
8282 public final int getRight() {
8283 return mRight;
8284 }
8285
8286 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008287 * Sets the right position of this view relative to its parent. This method is meant to be called
8288 * by the layout system and should not generally be called otherwise, because the property
8289 * may be changed at any time by the layout.
8290 *
8291 * @param right The bottom of this view, in pixels.
8292 */
8293 public final void setRight(int right) {
8294 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008295 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008296 final boolean matrixIsIdentity = mTransformationInfo == null
8297 || mTransformationInfo.mMatrixIsIdentity;
8298 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008299 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008300 int maxRight;
8301 if (right < mRight) {
8302 maxRight = mRight;
8303 } else {
8304 maxRight = right;
8305 }
Chet Haasee9140a72011-02-16 16:23:29 -08008306 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008307 }
8308 } else {
8309 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008310 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008311 }
8312
Chet Haaseed032702010-10-01 14:05:54 -07008313 int oldWidth = mRight - mLeft;
8314 int height = mBottom - mTop;
8315
Chet Haase21cd1382010-09-01 17:42:29 -07008316 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008317 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8318 mDisplayList.setRight(mRight);
8319 }
Chet Haase21cd1382010-09-01 17:42:29 -07008320
Chet Haaseed032702010-10-01 14:05:54 -07008321 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8322
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008323 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008324 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8325 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008326 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008327 }
Chet Haase21cd1382010-09-01 17:42:29 -07008328 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008329 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008330 }
Chet Haase55dbb652010-12-21 20:15:08 -08008331 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008332 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008333 }
8334 }
8335
8336 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008337 * The visual x position of this view, in pixels. This is equivalent to the
8338 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008339 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008340 *
Chet Haasedf030d22010-07-30 17:22:38 -07008341 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008342 */
Chet Haasea5531132012-02-02 13:41:44 -08008343 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008344 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008345 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008346 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008347
Chet Haasedf030d22010-07-30 17:22:38 -07008348 /**
8349 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8350 * {@link #setTranslationX(float) translationX} property to be the difference between
8351 * the x value passed in and the current {@link #getLeft() left} property.
8352 *
8353 * @param x The visual x position of this view, in pixels.
8354 */
8355 public void setX(float x) {
8356 setTranslationX(x - mLeft);
8357 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008358
Chet Haasedf030d22010-07-30 17:22:38 -07008359 /**
8360 * The visual y position of this view, in pixels. This is equivalent to the
8361 * {@link #setTranslationY(float) translationY} property plus the current
8362 * {@link #getTop() top} property.
8363 *
8364 * @return The visual y position of this view, in pixels.
8365 */
Chet Haasea5531132012-02-02 13:41:44 -08008366 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008367 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008368 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008369 }
8370
8371 /**
8372 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8373 * {@link #setTranslationY(float) translationY} property to be the difference between
8374 * the y value passed in and the current {@link #getTop() top} property.
8375 *
8376 * @param y The visual y position of this view, in pixels.
8377 */
8378 public void setY(float y) {
8379 setTranslationY(y - mTop);
8380 }
8381
8382
8383 /**
8384 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8385 * This position is post-layout, in addition to wherever the object's
8386 * layout placed it.
8387 *
8388 * @return The horizontal position of this view relative to its left position, in pixels.
8389 */
Chet Haasea5531132012-02-02 13:41:44 -08008390 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008391 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008392 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008393 }
8394
8395 /**
8396 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8397 * This effectively positions the object post-layout, in addition to wherever the object's
8398 * layout placed it.
8399 *
8400 * @param translationX The horizontal position of this view relative to its left position,
8401 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008402 *
8403 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008404 */
8405 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008406 ensureTransformationInfo();
8407 final TransformationInfo info = mTransformationInfo;
8408 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008409 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008410 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008411 info.mTranslationX = translationX;
8412 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008413 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008414 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8415 mDisplayList.setTranslationX(translationX);
8416 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008417 }
8418 }
8419
8420 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008421 * The horizontal location of this view relative to its {@link #getTop() top} position.
8422 * This position is post-layout, in addition to wherever the object's
8423 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008424 *
Chet Haasedf030d22010-07-30 17:22:38 -07008425 * @return The vertical position of this view relative to its top position,
8426 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008427 */
Chet Haasea5531132012-02-02 13:41:44 -08008428 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008429 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008430 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008431 }
8432
8433 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008434 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8435 * This effectively positions the object post-layout, in addition to wherever the object's
8436 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008437 *
Chet Haasedf030d22010-07-30 17:22:38 -07008438 * @param translationY The vertical position of this view relative to its top position,
8439 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008440 *
8441 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008442 */
Chet Haasedf030d22010-07-30 17:22:38 -07008443 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008444 ensureTransformationInfo();
8445 final TransformationInfo info = mTransformationInfo;
8446 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008447 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008448 info.mTranslationY = translationY;
8449 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008450 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008451 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8452 mDisplayList.setTranslationY(translationY);
8453 }
Chet Haasedf030d22010-07-30 17:22:38 -07008454 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008455 }
8456
8457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 * Hit rectangle in parent's coordinates
8459 *
8460 * @param outRect The hit rectangle of the view.
8461 */
8462 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008463 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008464 final TransformationInfo info = mTransformationInfo;
8465 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008466 outRect.set(mLeft, mTop, mRight, mBottom);
8467 } else {
8468 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008469 tmpRect.set(-info.mPivotX, -info.mPivotY,
8470 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8471 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008472 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8473 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008474 }
8475 }
8476
8477 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008478 * Determines whether the given point, in local coordinates is inside the view.
8479 */
8480 /*package*/ final boolean pointInView(float localX, float localY) {
8481 return localX >= 0 && localX < (mRight - mLeft)
8482 && localY >= 0 && localY < (mBottom - mTop);
8483 }
8484
8485 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008486 * Utility method to determine whether the given point, in local coordinates,
8487 * is inside the view, where the area of the view is expanded by the slop factor.
8488 * This method is called while processing touch-move events to determine if the event
8489 * is still within the view.
8490 */
8491 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008492 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008493 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 }
8495
8496 /**
8497 * When a view has focus and the user navigates away from it, the next view is searched for
8498 * starting from the rectangle filled in by this method.
8499 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008500 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8501 * of the view. However, if your view maintains some idea of internal selection,
8502 * such as a cursor, or a selected row or column, you should override this method and
8503 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 *
8505 * @param r The rectangle to fill in, in this view's coordinates.
8506 */
8507 public void getFocusedRect(Rect r) {
8508 getDrawingRect(r);
8509 }
8510
8511 /**
8512 * If some part of this view is not clipped by any of its parents, then
8513 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008514 * coordinates (without taking possible View rotations into account), offset
8515 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8516 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 *
8518 * @param r If true is returned, r holds the global coordinates of the
8519 * visible portion of this view.
8520 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8521 * between this view and its root. globalOffet may be null.
8522 * @return true if r is non-empty (i.e. part of the view is visible at the
8523 * root level.
8524 */
8525 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8526 int width = mRight - mLeft;
8527 int height = mBottom - mTop;
8528 if (width > 0 && height > 0) {
8529 r.set(0, 0, width, height);
8530 if (globalOffset != null) {
8531 globalOffset.set(-mScrollX, -mScrollY);
8532 }
8533 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8534 }
8535 return false;
8536 }
8537
8538 public final boolean getGlobalVisibleRect(Rect r) {
8539 return getGlobalVisibleRect(r, null);
8540 }
8541
8542 public final boolean getLocalVisibleRect(Rect r) {
8543 Point offset = new Point();
8544 if (getGlobalVisibleRect(r, offset)) {
8545 r.offset(-offset.x, -offset.y); // make r local
8546 return true;
8547 }
8548 return false;
8549 }
8550
8551 /**
8552 * Offset this view's vertical location by the specified number of pixels.
8553 *
8554 * @param offset the number of pixels to offset the view by
8555 */
8556 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008557 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008558 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008559 final boolean matrixIsIdentity = mTransformationInfo == null
8560 || mTransformationInfo.mMatrixIsIdentity;
8561 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008562 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8563 invalidateViewProperty(false, false);
8564 } else {
8565 final ViewParent p = mParent;
8566 if (p != null && mAttachInfo != null) {
8567 final Rect r = mAttachInfo.mTmpInvalRect;
8568 int minTop;
8569 int maxBottom;
8570 int yLoc;
8571 if (offset < 0) {
8572 minTop = mTop + offset;
8573 maxBottom = mBottom;
8574 yLoc = offset;
8575 } else {
8576 minTop = mTop;
8577 maxBottom = mBottom + offset;
8578 yLoc = 0;
8579 }
8580 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8581 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008582 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008583 }
8584 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008585 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008586 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008587
Chet Haasec3aa3612010-06-17 08:50:37 -07008588 mTop += offset;
8589 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008590 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8591 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008592 invalidateViewProperty(false, false);
8593 } else {
8594 if (!matrixIsIdentity) {
8595 invalidateViewProperty(false, true);
8596 }
8597 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008598 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 }
8601
8602 /**
8603 * Offset this view's horizontal location by the specified amount of pixels.
8604 *
8605 * @param offset the numer of pixels to offset the view by
8606 */
8607 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008608 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008609 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008610 final boolean matrixIsIdentity = mTransformationInfo == null
8611 || mTransformationInfo.mMatrixIsIdentity;
8612 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008613 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8614 invalidateViewProperty(false, false);
8615 } else {
8616 final ViewParent p = mParent;
8617 if (p != null && mAttachInfo != null) {
8618 final Rect r = mAttachInfo.mTmpInvalRect;
8619 int minLeft;
8620 int maxRight;
8621 if (offset < 0) {
8622 minLeft = mLeft + offset;
8623 maxRight = mRight;
8624 } else {
8625 minLeft = mLeft;
8626 maxRight = mRight + offset;
8627 }
8628 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8629 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008630 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 }
8632 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008633 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008634 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008635
Chet Haasec3aa3612010-06-17 08:50:37 -07008636 mLeft += offset;
8637 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008638 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8639 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008640 invalidateViewProperty(false, false);
8641 } else {
8642 if (!matrixIsIdentity) {
8643 invalidateViewProperty(false, true);
8644 }
8645 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008646 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 }
8649
8650 /**
8651 * Get the LayoutParams associated with this view. All views should have
8652 * layout parameters. These supply parameters to the <i>parent</i> of this
8653 * view specifying how it should be arranged. There are many subclasses of
8654 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8655 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008656 *
8657 * This method may return null if this View is not attached to a parent
8658 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8659 * was not invoked successfully. When a View is attached to a parent
8660 * ViewGroup, this method must not return null.
8661 *
8662 * @return The LayoutParams associated with this view, or null if no
8663 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008665 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 public ViewGroup.LayoutParams getLayoutParams() {
8667 return mLayoutParams;
8668 }
8669
8670 /**
8671 * Set the layout parameters associated with this view. These supply
8672 * parameters to the <i>parent</i> of this view specifying how it should be
8673 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8674 * correspond to the different subclasses of ViewGroup that are responsible
8675 * for arranging their children.
8676 *
Romain Guy01c174b2011-02-22 11:51:06 -08008677 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 */
8679 public void setLayoutParams(ViewGroup.LayoutParams params) {
8680 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008681 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 }
8683 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008684 if (mParent instanceof ViewGroup) {
8685 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008687 requestLayout();
8688 }
8689
8690 /**
8691 * Set the scrolled position of your view. This will cause a call to
8692 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8693 * invalidated.
8694 * @param x the x position to scroll to
8695 * @param y the y position to scroll to
8696 */
8697 public void scrollTo(int x, int y) {
8698 if (mScrollX != x || mScrollY != y) {
8699 int oldX = mScrollX;
8700 int oldY = mScrollY;
8701 mScrollX = x;
8702 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008703 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008704 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008705 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07008706 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07008707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708 }
8709 }
8710
8711 /**
8712 * Move the scrolled position of your view. This will cause a call to
8713 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8714 * invalidated.
8715 * @param x the amount of pixels to scroll by horizontally
8716 * @param y the amount of pixels to scroll by vertically
8717 */
8718 public void scrollBy(int x, int y) {
8719 scrollTo(mScrollX + x, mScrollY + y);
8720 }
8721
8722 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008723 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8724 * animation to fade the scrollbars out after a default delay. If a subclass
8725 * provides animated scrolling, the start delay should equal the duration
8726 * of the scrolling animation.</p>
8727 *
8728 * <p>The animation starts only if at least one of the scrollbars is
8729 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8730 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8731 * this method returns true, and false otherwise. If the animation is
8732 * started, this method calls {@link #invalidate()}; in that case the
8733 * caller should not call {@link #invalidate()}.</p>
8734 *
8735 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008736 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008737 *
8738 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8739 * and {@link #scrollTo(int, int)}.</p>
8740 *
8741 * @return true if the animation is played, false otherwise
8742 *
8743 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008744 * @see #scrollBy(int, int)
8745 * @see #scrollTo(int, int)
8746 * @see #isHorizontalScrollBarEnabled()
8747 * @see #isVerticalScrollBarEnabled()
8748 * @see #setHorizontalScrollBarEnabled(boolean)
8749 * @see #setVerticalScrollBarEnabled(boolean)
8750 */
8751 protected boolean awakenScrollBars() {
8752 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008753 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008754 }
8755
8756 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008757 * Trigger the scrollbars to draw.
8758 * This method differs from awakenScrollBars() only in its default duration.
8759 * initialAwakenScrollBars() will show the scroll bars for longer than
8760 * usual to give the user more of a chance to notice them.
8761 *
8762 * @return true if the animation is played, false otherwise.
8763 */
8764 private boolean initialAwakenScrollBars() {
8765 return mScrollCache != null &&
8766 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8767 }
8768
8769 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008770 * <p>
8771 * Trigger the scrollbars to draw. When invoked this method starts an
8772 * animation to fade the scrollbars out after a fixed delay. If a subclass
8773 * provides animated scrolling, the start delay should equal the duration of
8774 * the scrolling animation.
8775 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008776 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008777 * <p>
8778 * The animation starts only if at least one of the scrollbars is enabled,
8779 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8780 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8781 * this method returns true, and false otherwise. If the animation is
8782 * started, this method calls {@link #invalidate()}; in that case the caller
8783 * should not call {@link #invalidate()}.
8784 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008785 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008786 * <p>
8787 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008788 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008789 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008790 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008791 * @param startDelay the delay, in milliseconds, after which the animation
8792 * should start; when the delay is 0, the animation starts
8793 * immediately
8794 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008795 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008796 * @see #scrollBy(int, int)
8797 * @see #scrollTo(int, int)
8798 * @see #isHorizontalScrollBarEnabled()
8799 * @see #isVerticalScrollBarEnabled()
8800 * @see #setHorizontalScrollBarEnabled(boolean)
8801 * @see #setVerticalScrollBarEnabled(boolean)
8802 */
8803 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008804 return awakenScrollBars(startDelay, true);
8805 }
Joe Malin32736f02011-01-19 16:14:20 -08008806
Mike Cleron290947b2009-09-29 18:34:32 -07008807 /**
8808 * <p>
8809 * Trigger the scrollbars to draw. When invoked this method starts an
8810 * animation to fade the scrollbars out after a fixed delay. If a subclass
8811 * provides animated scrolling, the start delay should equal the duration of
8812 * the scrolling animation.
8813 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008814 *
Mike Cleron290947b2009-09-29 18:34:32 -07008815 * <p>
8816 * The animation starts only if at least one of the scrollbars is enabled,
8817 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8818 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8819 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008820 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008821 * is set to true; in that case the caller
8822 * should not call {@link #invalidate()}.
8823 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008824 *
Mike Cleron290947b2009-09-29 18:34:32 -07008825 * <p>
8826 * This method should be invoked everytime a subclass directly updates the
8827 * scroll parameters.
8828 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008829 *
Mike Cleron290947b2009-09-29 18:34:32 -07008830 * @param startDelay the delay, in milliseconds, after which the animation
8831 * should start; when the delay is 0, the animation starts
8832 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008833 *
Mike Cleron290947b2009-09-29 18:34:32 -07008834 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008835 *
Mike Cleron290947b2009-09-29 18:34:32 -07008836 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008837 *
Mike Cleron290947b2009-09-29 18:34:32 -07008838 * @see #scrollBy(int, int)
8839 * @see #scrollTo(int, int)
8840 * @see #isHorizontalScrollBarEnabled()
8841 * @see #isVerticalScrollBarEnabled()
8842 * @see #setHorizontalScrollBarEnabled(boolean)
8843 * @see #setVerticalScrollBarEnabled(boolean)
8844 */
8845 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008846 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008847
Mike Cleronf116bf82009-09-27 19:14:12 -07008848 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8849 return false;
8850 }
8851
8852 if (scrollCache.scrollBar == null) {
8853 scrollCache.scrollBar = new ScrollBarDrawable();
8854 }
8855
8856 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8857
Mike Cleron290947b2009-09-29 18:34:32 -07008858 if (invalidate) {
8859 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07008860 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07008861 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008862
8863 if (scrollCache.state == ScrollabilityCache.OFF) {
8864 // FIXME: this is copied from WindowManagerService.
8865 // We should get this value from the system when it
8866 // is possible to do so.
8867 final int KEY_REPEAT_FIRST_DELAY = 750;
8868 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8869 }
8870
8871 // Tell mScrollCache when we should start fading. This may
8872 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008873 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008874 scrollCache.fadeStartTime = fadeStartTime;
8875 scrollCache.state = ScrollabilityCache.ON;
8876
8877 // Schedule our fader to run, unscheduling any old ones first
8878 if (mAttachInfo != null) {
8879 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8880 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8881 }
8882
8883 return true;
8884 }
8885
8886 return false;
8887 }
8888
8889 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008890 * Do not invalidate views which are not visible and which are not running an animation. They
8891 * will not get drawn and they should not set dirty flags as if they will be drawn
8892 */
8893 private boolean skipInvalidate() {
8894 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8895 (!(mParent instanceof ViewGroup) ||
8896 !((ViewGroup) mParent).isViewTransitioning(this));
8897 }
8898 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008899 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008900 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8901 * in the future. This must be called from a UI thread. To call from a non-UI
8902 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008903 *
8904 * WARNING: This method is destructive to dirty.
8905 * @param dirty the rectangle representing the bounds of the dirty region
8906 */
8907 public void invalidate(Rect dirty) {
8908 if (ViewDebug.TRACE_HIERARCHY) {
8909 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8910 }
8911
Chet Haaseaceafe62011-08-26 15:44:33 -07008912 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008913 return;
8914 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008915 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008916 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8917 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008918 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008919 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008920 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 final ViewParent p = mParent;
8922 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008923 //noinspection PointlessBooleanExpression,ConstantConditions
8924 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8925 if (p != null && ai != null && ai.mHardwareAccelerated) {
8926 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008927 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008928 p.invalidateChild(this, null);
8929 return;
8930 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 if (p != null && ai != null) {
8933 final int scrollX = mScrollX;
8934 final int scrollY = mScrollY;
8935 final Rect r = ai.mTmpInvalRect;
8936 r.set(dirty.left - scrollX, dirty.top - scrollY,
8937 dirty.right - scrollX, dirty.bottom - scrollY);
8938 mParent.invalidateChild(this, r);
8939 }
8940 }
8941 }
8942
8943 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008944 * 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 -08008945 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008946 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8947 * will be called at some point in the future. This must be called from
8948 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 * @param l the left position of the dirty region
8950 * @param t the top position of the dirty region
8951 * @param r the right position of the dirty region
8952 * @param b the bottom position of the dirty region
8953 */
8954 public void invalidate(int l, int t, int r, int b) {
8955 if (ViewDebug.TRACE_HIERARCHY) {
8956 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8957 }
8958
Chet Haaseaceafe62011-08-26 15:44:33 -07008959 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008960 return;
8961 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008962 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008963 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8964 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008966 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008967 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 final ViewParent p = mParent;
8969 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008970 //noinspection PointlessBooleanExpression,ConstantConditions
8971 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8972 if (p != null && ai != null && ai.mHardwareAccelerated) {
8973 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008974 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008975 p.invalidateChild(this, null);
8976 return;
8977 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 if (p != null && ai != null && l < r && t < b) {
8980 final int scrollX = mScrollX;
8981 final int scrollY = mScrollY;
8982 final Rect tmpr = ai.mTmpInvalRect;
8983 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8984 p.invalidateChild(this, tmpr);
8985 }
8986 }
8987 }
8988
8989 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008990 * Invalidate the whole view. If the view is visible,
8991 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8992 * the future. This must be called from a UI thread. To call from a non-UI thread,
8993 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 */
8995 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008996 invalidate(true);
8997 }
Joe Malin32736f02011-01-19 16:14:20 -08008998
Chet Haaseed032702010-10-01 14:05:54 -07008999 /**
9000 * This is where the invalidate() work actually happens. A full invalidate()
9001 * causes the drawing cache to be invalidated, but this function can be called with
9002 * invalidateCache set to false to skip that invalidation step for cases that do not
9003 * need it (for example, a component that remains at the same dimensions with the same
9004 * content).
9005 *
9006 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9007 * well. This is usually true for a full invalidate, but may be set to false if the
9008 * View's contents or dimensions have not changed.
9009 */
Romain Guy849d0a32011-02-01 17:20:48 -08009010 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 if (ViewDebug.TRACE_HIERARCHY) {
9012 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9013 }
9014
Chet Haaseaceafe62011-08-26 15:44:33 -07009015 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009016 return;
9017 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009018 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009019 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009020 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9021 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009022 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009023 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009024 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009025 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009026 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009029 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009030 //noinspection PointlessBooleanExpression,ConstantConditions
9031 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9032 if (p != null && ai != null && ai.mHardwareAccelerated) {
9033 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009034 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009035 p.invalidateChild(this, null);
9036 return;
9037 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009038 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009040 if (p != null && ai != null) {
9041 final Rect r = ai.mTmpInvalRect;
9042 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9043 // Don't call invalidate -- we don't want to internally scroll
9044 // our own bounds
9045 p.invalidateChild(this, r);
9046 }
9047 }
9048 }
9049
9050 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009051 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9052 * set any flags or handle all of the cases handled by the default invalidation methods.
9053 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9054 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9055 * walk up the hierarchy, transforming the dirty rect as necessary.
9056 *
9057 * The method also handles normal invalidation logic if display list properties are not
9058 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9059 * backup approach, to handle these cases used in the various property-setting methods.
9060 *
9061 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9062 * are not being used in this view
9063 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9064 * list properties are not being used in this view
9065 */
9066 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9067 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9068 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9069 if (invalidateParent) {
9070 invalidateParentCaches();
9071 }
9072 if (forceRedraw) {
9073 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9074 }
9075 invalidate(false);
9076 } else {
9077 final AttachInfo ai = mAttachInfo;
9078 final ViewParent p = mParent;
9079 if (p != null && ai != null) {
9080 final Rect r = ai.mTmpInvalRect;
9081 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9082 if (mParent instanceof ViewGroup) {
9083 ((ViewGroup) mParent).invalidateChildFast(this, r);
9084 } else {
9085 mParent.invalidateChild(this, r);
9086 }
9087 }
9088 }
9089 }
9090
9091 /**
9092 * Utility method to transform a given Rect by the current matrix of this view.
9093 */
9094 void transformRect(final Rect rect) {
9095 if (!getMatrix().isIdentity()) {
9096 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9097 boundingRect.set(rect);
9098 getMatrix().mapRect(boundingRect);
9099 rect.set((int) (boundingRect.left - 0.5f),
9100 (int) (boundingRect.top - 0.5f),
9101 (int) (boundingRect.right + 0.5f),
9102 (int) (boundingRect.bottom + 0.5f));
9103 }
9104 }
9105
9106 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009107 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009108 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9109 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009110 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9111 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009112 *
9113 * @hide
9114 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009115 protected void invalidateParentCaches() {
9116 if (mParent instanceof View) {
9117 ((View) mParent).mPrivateFlags |= INVALIDATED;
9118 }
9119 }
Joe Malin32736f02011-01-19 16:14:20 -08009120
Romain Guy0fd89bf2011-01-26 15:41:30 -08009121 /**
9122 * Used to indicate that the parent of this view should be invalidated. This functionality
9123 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9124 * which is necessary when various parent-managed properties of the view change, such as
9125 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9126 * an invalidation event to the parent.
9127 *
9128 * @hide
9129 */
9130 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009131 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009132 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009133 }
9134 }
9135
9136 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009137 * Indicates whether this View is opaque. An opaque View guarantees that it will
9138 * draw all the pixels overlapping its bounds using a fully opaque color.
9139 *
9140 * Subclasses of View should override this method whenever possible to indicate
9141 * whether an instance is opaque. Opaque Views are treated in a special way by
9142 * the View hierarchy, possibly allowing it to perform optimizations during
9143 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009144 *
Romain Guy24443ea2009-05-11 11:56:30 -07009145 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009146 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009147 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009148 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009149 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9151 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009152 }
9153
Adam Powell20232d02010-12-08 21:08:53 -08009154 /**
9155 * @hide
9156 */
9157 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009158 // Opaque if:
9159 // - Has a background
9160 // - Background is opaque
9161 // - Doesn't have scrollbars or scrollbars are inside overlay
9162
Philip Milne6c8ea062012-04-03 17:38:43 -07009163 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009164 mPrivateFlags |= OPAQUE_BACKGROUND;
9165 } else {
9166 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9167 }
9168
9169 final int flags = mViewFlags;
9170 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9171 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9172 mPrivateFlags |= OPAQUE_SCROLLBARS;
9173 } else {
9174 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9175 }
9176 }
9177
9178 /**
9179 * @hide
9180 */
9181 protected boolean hasOpaqueScrollbars() {
9182 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009183 }
9184
9185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 * @return A handler associated with the thread running the View. This
9187 * handler can be used to pump events in the UI events queue.
9188 */
9189 public Handler getHandler() {
9190 if (mAttachInfo != null) {
9191 return mAttachInfo.mHandler;
9192 }
9193 return null;
9194 }
9195
9196 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009197 * Gets the view root associated with the View.
9198 * @return The view root, or null if none.
9199 * @hide
9200 */
9201 public ViewRootImpl getViewRootImpl() {
9202 if (mAttachInfo != null) {
9203 return mAttachInfo.mViewRootImpl;
9204 }
9205 return null;
9206 }
9207
9208 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009209 * <p>Causes the Runnable to be added to the message queue.
9210 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009211 *
Romain Guye63a4f32011-08-11 11:33:31 -07009212 * <p>This method can be invoked from outside of the UI thread
9213 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009214 *
9215 * @param action The Runnable that will be executed.
9216 *
9217 * @return Returns true if the Runnable was successfully placed in to the
9218 * message queue. Returns false on failure, usually because the
9219 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009220 *
9221 * @see #postDelayed
9222 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009223 */
9224 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009225 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009226 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009227 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009229 // Assume that post will succeed later
9230 ViewRootImpl.getRunQueue().post(action);
9231 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 }
9233
9234 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009235 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009237 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009238 *
Romain Guye63a4f32011-08-11 11:33:31 -07009239 * <p>This method can be invoked from outside of the UI thread
9240 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 *
9242 * @param action The Runnable that will be executed.
9243 * @param delayMillis The delay (in milliseconds) until the Runnable
9244 * will be executed.
9245 *
9246 * @return true if the Runnable was successfully placed in to the
9247 * message queue. Returns false on failure, usually because the
9248 * looper processing the message queue is exiting. Note that a
9249 * result of true does not mean the Runnable will be processed --
9250 * if the looper is quit before the delivery time of the message
9251 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009252 *
9253 * @see #post
9254 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 */
9256 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009257 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009258 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009259 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009261 // Assume that post will succeed later
9262 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9263 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009264 }
9265
9266 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009267 * <p>Causes the Runnable to execute on the next animation time step.
9268 * The runnable will be run on the user interface thread.</p>
9269 *
9270 * <p>This method can be invoked from outside of the UI thread
9271 * only when this View is attached to a window.</p>
9272 *
9273 * @param action The Runnable that will be executed.
9274 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009275 * @see #postOnAnimationDelayed
9276 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009277 */
9278 public void postOnAnimation(Runnable action) {
9279 final AttachInfo attachInfo = mAttachInfo;
9280 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009281 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9282 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009283 } else {
9284 // Assume that post will succeed later
9285 ViewRootImpl.getRunQueue().post(action);
9286 }
9287 }
9288
9289 /**
9290 * <p>Causes the Runnable to execute on the next animation time step,
9291 * after the specified amount of time elapses.
9292 * The runnable will be run on the user interface thread.</p>
9293 *
9294 * <p>This method can be invoked from outside of the UI thread
9295 * only when this View is attached to a window.</p>
9296 *
9297 * @param action The Runnable that will be executed.
9298 * @param delayMillis The delay (in milliseconds) until the Runnable
9299 * will be executed.
9300 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009301 * @see #postOnAnimation
9302 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009303 */
9304 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9305 final AttachInfo attachInfo = mAttachInfo;
9306 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009307 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9308 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009309 } else {
9310 // Assume that post will succeed later
9311 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9312 }
9313 }
9314
9315 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009316 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009317 *
Romain Guye63a4f32011-08-11 11:33:31 -07009318 * <p>This method can be invoked from outside of the UI thread
9319 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 *
9321 * @param action The Runnable to remove from the message handling queue
9322 *
9323 * @return true if this view could ask the Handler to remove the Runnable,
9324 * false otherwise. When the returned value is true, the Runnable
9325 * may or may not have been actually removed from the message queue
9326 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009327 *
9328 * @see #post
9329 * @see #postDelayed
9330 * @see #postOnAnimation
9331 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009332 */
9333 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009334 if (action != null) {
9335 final AttachInfo attachInfo = mAttachInfo;
9336 if (attachInfo != null) {
9337 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009338 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9339 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009340 } else {
9341 // Assume that post will succeed later
9342 ViewRootImpl.getRunQueue().removeCallbacks(action);
9343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 return true;
9346 }
9347
9348 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009349 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9350 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009351 *
Romain Guye63a4f32011-08-11 11:33:31 -07009352 * <p>This method can be invoked from outside of the UI thread
9353 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009356 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 */
9358 public void postInvalidate() {
9359 postInvalidateDelayed(0);
9360 }
9361
9362 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009363 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9364 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009365 *
Romain Guye63a4f32011-08-11 11:33:31 -07009366 * <p>This method can be invoked from outside of the UI thread
9367 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 *
9369 * @param left The left coordinate of the rectangle to invalidate.
9370 * @param top The top coordinate of the rectangle to invalidate.
9371 * @param right The right coordinate of the rectangle to invalidate.
9372 * @param bottom The bottom coordinate of the rectangle to invalidate.
9373 *
9374 * @see #invalidate(int, int, int, int)
9375 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009376 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 */
9378 public void postInvalidate(int left, int top, int right, int bottom) {
9379 postInvalidateDelayed(0, left, top, right, bottom);
9380 }
9381
9382 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009383 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9384 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009385 *
Romain Guye63a4f32011-08-11 11:33:31 -07009386 * <p>This method can be invoked from outside of the UI thread
9387 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 *
9389 * @param delayMilliseconds the duration in milliseconds to delay the
9390 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009391 *
9392 * @see #invalidate()
9393 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 */
9395 public void postInvalidateDelayed(long delayMilliseconds) {
9396 // We try only with the AttachInfo because there's no point in invalidating
9397 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009398 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009399 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009400 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009401 }
9402 }
9403
9404 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009405 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9406 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009407 *
Romain Guye63a4f32011-08-11 11:33:31 -07009408 * <p>This method can be invoked from outside of the UI thread
9409 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009410 *
9411 * @param delayMilliseconds the duration in milliseconds to delay the
9412 * invalidation by
9413 * @param left The left coordinate of the rectangle to invalidate.
9414 * @param top The top coordinate of the rectangle to invalidate.
9415 * @param right The right coordinate of the rectangle to invalidate.
9416 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009417 *
9418 * @see #invalidate(int, int, int, int)
9419 * @see #invalidate(Rect)
9420 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009421 */
9422 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9423 int right, int bottom) {
9424
9425 // We try only with the AttachInfo because there's no point in invalidating
9426 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009427 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009428 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009429 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9430 info.target = this;
9431 info.left = left;
9432 info.top = top;
9433 info.right = right;
9434 info.bottom = bottom;
9435
Jeff Browna175a5b2012-02-15 19:18:31 -08009436 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009437 }
9438 }
9439
9440 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009441 * <p>Cause an invalidate to happen on the next animation time step, typically the
9442 * next display frame.</p>
9443 *
9444 * <p>This method can be invoked from outside of the UI thread
9445 * only when this View is attached to a window.</p>
9446 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009447 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -08009448 */
9449 public void postInvalidateOnAnimation() {
9450 // We try only with the AttachInfo because there's no point in invalidating
9451 // if we are not attached to our window
9452 final AttachInfo attachInfo = mAttachInfo;
9453 if (attachInfo != null) {
9454 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9455 }
9456 }
9457
9458 /**
9459 * <p>Cause an invalidate of the specified area to happen on the next animation
9460 * time step, typically the next display frame.</p>
9461 *
9462 * <p>This method can be invoked from outside of the UI thread
9463 * only when this View is attached to a window.</p>
9464 *
9465 * @param left The left coordinate of the rectangle to invalidate.
9466 * @param top The top coordinate of the rectangle to invalidate.
9467 * @param right The right coordinate of the rectangle to invalidate.
9468 * @param bottom The bottom coordinate of the rectangle to invalidate.
9469 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009470 * @see #invalidate(int, int, int, int)
9471 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -08009472 */
9473 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9474 // We try only with the AttachInfo because there's no point in invalidating
9475 // if we are not attached to our window
9476 final AttachInfo attachInfo = mAttachInfo;
9477 if (attachInfo != null) {
9478 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9479 info.target = this;
9480 info.left = left;
9481 info.top = top;
9482 info.right = right;
9483 info.bottom = bottom;
9484
9485 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9486 }
9487 }
9488
9489 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009490 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9491 * This event is sent at most once every
9492 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9493 */
9494 private void postSendViewScrolledAccessibilityEventCallback() {
9495 if (mSendViewScrolledAccessibilityEvent == null) {
9496 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9497 }
9498 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9499 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9500 postDelayed(mSendViewScrolledAccessibilityEvent,
9501 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9502 }
9503 }
9504
9505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 * Called by a parent to request that a child update its values for mScrollX
9507 * and mScrollY if necessary. This will typically be done if the child is
9508 * animating a scroll using a {@link android.widget.Scroller Scroller}
9509 * object.
9510 */
9511 public void computeScroll() {
9512 }
9513
9514 /**
9515 * <p>Indicate whether the horizontal edges are faded when the view is
9516 * scrolled horizontally.</p>
9517 *
9518 * @return true if the horizontal edges should are faded on scroll, false
9519 * otherwise
9520 *
9521 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009522 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009523 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 */
9525 public boolean isHorizontalFadingEdgeEnabled() {
9526 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9527 }
9528
9529 /**
9530 * <p>Define whether the horizontal edges should be faded when this view
9531 * is scrolled horizontally.</p>
9532 *
9533 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9534 * be faded when the view is scrolled
9535 * horizontally
9536 *
9537 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009538 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009539 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 */
9541 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9542 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9543 if (horizontalFadingEdgeEnabled) {
9544 initScrollCache();
9545 }
9546
9547 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9548 }
9549 }
9550
9551 /**
9552 * <p>Indicate whether the vertical edges are faded when the view is
9553 * scrolled horizontally.</p>
9554 *
9555 * @return true if the vertical edges should are faded on scroll, false
9556 * otherwise
9557 *
9558 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009559 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009560 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009561 */
9562 public boolean isVerticalFadingEdgeEnabled() {
9563 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9564 }
9565
9566 /**
9567 * <p>Define whether the vertical edges should be faded when this view
9568 * is scrolled vertically.</p>
9569 *
9570 * @param verticalFadingEdgeEnabled true if the vertical edges should
9571 * be faded when the view is scrolled
9572 * vertically
9573 *
9574 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009575 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009576 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009577 */
9578 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9579 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9580 if (verticalFadingEdgeEnabled) {
9581 initScrollCache();
9582 }
9583
9584 mViewFlags ^= FADING_EDGE_VERTICAL;
9585 }
9586 }
9587
9588 /**
9589 * Returns the strength, or intensity, of the top faded edge. The strength is
9590 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9591 * returns 0.0 or 1.0 but no value in between.
9592 *
9593 * Subclasses should override this method to provide a smoother fade transition
9594 * when scrolling occurs.
9595 *
9596 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9597 */
9598 protected float getTopFadingEdgeStrength() {
9599 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9600 }
9601
9602 /**
9603 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9604 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9605 * returns 0.0 or 1.0 but no value in between.
9606 *
9607 * Subclasses should override this method to provide a smoother fade transition
9608 * when scrolling occurs.
9609 *
9610 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9611 */
9612 protected float getBottomFadingEdgeStrength() {
9613 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9614 computeVerticalScrollRange() ? 1.0f : 0.0f;
9615 }
9616
9617 /**
9618 * Returns the strength, or intensity, of the left faded edge. The strength is
9619 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9620 * returns 0.0 or 1.0 but no value in between.
9621 *
9622 * Subclasses should override this method to provide a smoother fade transition
9623 * when scrolling occurs.
9624 *
9625 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9626 */
9627 protected float getLeftFadingEdgeStrength() {
9628 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9629 }
9630
9631 /**
9632 * Returns the strength, or intensity, of the right faded edge. The strength is
9633 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9634 * returns 0.0 or 1.0 but no value in between.
9635 *
9636 * Subclasses should override this method to provide a smoother fade transition
9637 * when scrolling occurs.
9638 *
9639 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9640 */
9641 protected float getRightFadingEdgeStrength() {
9642 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9643 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9644 }
9645
9646 /**
9647 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9648 * scrollbar is not drawn by default.</p>
9649 *
9650 * @return true if the horizontal scrollbar should be painted, false
9651 * otherwise
9652 *
9653 * @see #setHorizontalScrollBarEnabled(boolean)
9654 */
9655 public boolean isHorizontalScrollBarEnabled() {
9656 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9657 }
9658
9659 /**
9660 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9661 * scrollbar is not drawn by default.</p>
9662 *
9663 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9664 * be painted
9665 *
9666 * @see #isHorizontalScrollBarEnabled()
9667 */
9668 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9669 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9670 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009671 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009672 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009673 }
9674 }
9675
9676 /**
9677 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9678 * scrollbar is not drawn by default.</p>
9679 *
9680 * @return true if the vertical scrollbar should be painted, false
9681 * otherwise
9682 *
9683 * @see #setVerticalScrollBarEnabled(boolean)
9684 */
9685 public boolean isVerticalScrollBarEnabled() {
9686 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9687 }
9688
9689 /**
9690 * <p>Define whether the vertical scrollbar should be drawn or not. The
9691 * scrollbar is not drawn by default.</p>
9692 *
9693 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9694 * be painted
9695 *
9696 * @see #isVerticalScrollBarEnabled()
9697 */
9698 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9699 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9700 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009701 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009702 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 }
9704 }
9705
Adam Powell20232d02010-12-08 21:08:53 -08009706 /**
9707 * @hide
9708 */
9709 protected void recomputePadding() {
9710 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 }
Joe Malin32736f02011-01-19 16:14:20 -08009712
Mike Cleronfe81d382009-09-28 14:22:16 -07009713 /**
9714 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009715 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009716 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009717 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009718 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -07009719 */
9720 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9721 initScrollCache();
9722 final ScrollabilityCache scrollabilityCache = mScrollCache;
9723 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009724 if (fadeScrollbars) {
9725 scrollabilityCache.state = ScrollabilityCache.OFF;
9726 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009727 scrollabilityCache.state = ScrollabilityCache.ON;
9728 }
9729 }
Joe Malin32736f02011-01-19 16:14:20 -08009730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009731 /**
Joe Malin32736f02011-01-19 16:14:20 -08009732 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009733 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009734 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009735 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -07009736 *
9737 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -07009738 */
9739 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009740 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009741 }
Joe Malin32736f02011-01-19 16:14:20 -08009742
Mike Cleron52f0a642009-09-28 18:21:37 -07009743 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07009744 *
9745 * Returns the delay before scrollbars fade.
9746 *
9747 * @return the delay before scrollbars fade
9748 *
9749 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9750 */
9751 public int getScrollBarDefaultDelayBeforeFade() {
9752 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
9753 mScrollCache.scrollBarDefaultDelayBeforeFade;
9754 }
9755
9756 /**
9757 * Define the delay before scrollbars fade.
9758 *
9759 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
9760 *
9761 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9762 */
9763 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
9764 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
9765 }
9766
9767 /**
9768 *
9769 * Returns the scrollbar fade duration.
9770 *
9771 * @return the scrollbar fade duration
9772 *
9773 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9774 */
9775 public int getScrollBarFadeDuration() {
9776 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
9777 mScrollCache.scrollBarFadeDuration;
9778 }
9779
9780 /**
9781 * Define the scrollbar fade duration.
9782 *
9783 * @param scrollBarFadeDuration - the scrollbar fade duration
9784 *
9785 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9786 */
9787 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
9788 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
9789 }
9790
9791 /**
9792 *
9793 * Returns the scrollbar size.
9794 *
9795 * @return the scrollbar size
9796 *
9797 * @attr ref android.R.styleable#View_scrollbarSize
9798 */
9799 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -07009800 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -07009801 mScrollCache.scrollBarSize;
9802 }
9803
9804 /**
9805 * Define the scrollbar size.
9806 *
9807 * @param scrollBarSize - the scrollbar size
9808 *
9809 * @attr ref android.R.styleable#View_scrollbarSize
9810 */
9811 public void setScrollBarSize(int scrollBarSize) {
9812 getScrollCache().scrollBarSize = scrollBarSize;
9813 }
9814
9815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9817 * inset. When inset, they add to the padding of the view. And the scrollbars
9818 * can be drawn inside the padding area or on the edge of the view. For example,
9819 * if a view has a background drawable and you want to draw the scrollbars
9820 * inside the padding specified by the drawable, you can use
9821 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9822 * appear at the edge of the view, ignoring the padding, then you can use
9823 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9824 * @param style the style of the scrollbars. Should be one of
9825 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9826 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9827 * @see #SCROLLBARS_INSIDE_OVERLAY
9828 * @see #SCROLLBARS_INSIDE_INSET
9829 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9830 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009831 *
9832 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 */
9834 public void setScrollBarStyle(int style) {
9835 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9836 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009837 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009838 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 }
9840 }
9841
9842 /**
9843 * <p>Returns the current scrollbar style.</p>
9844 * @return the current scrollbar style
9845 * @see #SCROLLBARS_INSIDE_OVERLAY
9846 * @see #SCROLLBARS_INSIDE_INSET
9847 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9848 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009849 *
9850 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009852 @ViewDebug.ExportedProperty(mapping = {
9853 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9854 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9855 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9856 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9857 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 public int getScrollBarStyle() {
9859 return mViewFlags & SCROLLBARS_STYLE_MASK;
9860 }
9861
9862 /**
9863 * <p>Compute the horizontal range that the horizontal scrollbar
9864 * represents.</p>
9865 *
9866 * <p>The range is expressed in arbitrary units that must be the same as the
9867 * units used by {@link #computeHorizontalScrollExtent()} and
9868 * {@link #computeHorizontalScrollOffset()}.</p>
9869 *
9870 * <p>The default range is the drawing width of this view.</p>
9871 *
9872 * @return the total horizontal range represented by the horizontal
9873 * scrollbar
9874 *
9875 * @see #computeHorizontalScrollExtent()
9876 * @see #computeHorizontalScrollOffset()
9877 * @see android.widget.ScrollBarDrawable
9878 */
9879 protected int computeHorizontalScrollRange() {
9880 return getWidth();
9881 }
9882
9883 /**
9884 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9885 * within the horizontal range. This value is used to compute the position
9886 * of the thumb within the scrollbar's track.</p>
9887 *
9888 * <p>The range is expressed in arbitrary units that must be the same as the
9889 * units used by {@link #computeHorizontalScrollRange()} and
9890 * {@link #computeHorizontalScrollExtent()}.</p>
9891 *
9892 * <p>The default offset is the scroll offset of this view.</p>
9893 *
9894 * @return the horizontal offset of the scrollbar's thumb
9895 *
9896 * @see #computeHorizontalScrollRange()
9897 * @see #computeHorizontalScrollExtent()
9898 * @see android.widget.ScrollBarDrawable
9899 */
9900 protected int computeHorizontalScrollOffset() {
9901 return mScrollX;
9902 }
9903
9904 /**
9905 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9906 * within the horizontal range. This value is used to compute the length
9907 * of the thumb within the scrollbar's track.</p>
9908 *
9909 * <p>The range is expressed in arbitrary units that must be the same as the
9910 * units used by {@link #computeHorizontalScrollRange()} and
9911 * {@link #computeHorizontalScrollOffset()}.</p>
9912 *
9913 * <p>The default extent is the drawing width of this view.</p>
9914 *
9915 * @return the horizontal extent of the scrollbar's thumb
9916 *
9917 * @see #computeHorizontalScrollRange()
9918 * @see #computeHorizontalScrollOffset()
9919 * @see android.widget.ScrollBarDrawable
9920 */
9921 protected int computeHorizontalScrollExtent() {
9922 return getWidth();
9923 }
9924
9925 /**
9926 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9927 *
9928 * <p>The range is expressed in arbitrary units that must be the same as the
9929 * units used by {@link #computeVerticalScrollExtent()} and
9930 * {@link #computeVerticalScrollOffset()}.</p>
9931 *
9932 * @return the total vertical range represented by the vertical scrollbar
9933 *
9934 * <p>The default range is the drawing height of this view.</p>
9935 *
9936 * @see #computeVerticalScrollExtent()
9937 * @see #computeVerticalScrollOffset()
9938 * @see android.widget.ScrollBarDrawable
9939 */
9940 protected int computeVerticalScrollRange() {
9941 return getHeight();
9942 }
9943
9944 /**
9945 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9946 * within the horizontal range. This value is used to compute the position
9947 * of the thumb within the scrollbar's track.</p>
9948 *
9949 * <p>The range is expressed in arbitrary units that must be the same as the
9950 * units used by {@link #computeVerticalScrollRange()} and
9951 * {@link #computeVerticalScrollExtent()}.</p>
9952 *
9953 * <p>The default offset is the scroll offset of this view.</p>
9954 *
9955 * @return the vertical offset of the scrollbar's thumb
9956 *
9957 * @see #computeVerticalScrollRange()
9958 * @see #computeVerticalScrollExtent()
9959 * @see android.widget.ScrollBarDrawable
9960 */
9961 protected int computeVerticalScrollOffset() {
9962 return mScrollY;
9963 }
9964
9965 /**
9966 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9967 * within the vertical range. This value is used to compute the length
9968 * of the thumb within the scrollbar's track.</p>
9969 *
9970 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009971 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 * {@link #computeVerticalScrollOffset()}.</p>
9973 *
9974 * <p>The default extent is the drawing height of this view.</p>
9975 *
9976 * @return the vertical extent of the scrollbar's thumb
9977 *
9978 * @see #computeVerticalScrollRange()
9979 * @see #computeVerticalScrollOffset()
9980 * @see android.widget.ScrollBarDrawable
9981 */
9982 protected int computeVerticalScrollExtent() {
9983 return getHeight();
9984 }
9985
9986 /**
Adam Powell69159442011-06-13 17:53:06 -07009987 * Check if this view can be scrolled horizontally in a certain direction.
9988 *
9989 * @param direction Negative to check scrolling left, positive to check scrolling right.
9990 * @return true if this view can be scrolled in the specified direction, false otherwise.
9991 */
9992 public boolean canScrollHorizontally(int direction) {
9993 final int offset = computeHorizontalScrollOffset();
9994 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9995 if (range == 0) return false;
9996 if (direction < 0) {
9997 return offset > 0;
9998 } else {
9999 return offset < range - 1;
10000 }
10001 }
10002
10003 /**
10004 * Check if this view can be scrolled vertically in a certain direction.
10005 *
10006 * @param direction Negative to check scrolling up, positive to check scrolling down.
10007 * @return true if this view can be scrolled in the specified direction, false otherwise.
10008 */
10009 public boolean canScrollVertically(int direction) {
10010 final int offset = computeVerticalScrollOffset();
10011 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10012 if (range == 0) return false;
10013 if (direction < 0) {
10014 return offset > 0;
10015 } else {
10016 return offset < range - 1;
10017 }
10018 }
10019
10020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10022 * scrollbars are painted only if they have been awakened first.</p>
10023 *
10024 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010025 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010026 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010028 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 // scrollbars are drawn only when the animation is running
10030 final ScrollabilityCache cache = mScrollCache;
10031 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010032
Mike Cleronf116bf82009-09-27 19:14:12 -070010033 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010034
Mike Cleronf116bf82009-09-27 19:14:12 -070010035 if (state == ScrollabilityCache.OFF) {
10036 return;
10037 }
Joe Malin32736f02011-01-19 16:14:20 -080010038
Mike Cleronf116bf82009-09-27 19:14:12 -070010039 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010040
Mike Cleronf116bf82009-09-27 19:14:12 -070010041 if (state == ScrollabilityCache.FADING) {
10042 // We're fading -- get our fade interpolation
10043 if (cache.interpolatorValues == null) {
10044 cache.interpolatorValues = new float[1];
10045 }
Joe Malin32736f02011-01-19 16:14:20 -080010046
Mike Cleronf116bf82009-09-27 19:14:12 -070010047 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010048
Mike Cleronf116bf82009-09-27 19:14:12 -070010049 // Stops the animation if we're done
10050 if (cache.scrollBarInterpolator.timeToValues(values) ==
10051 Interpolator.Result.FREEZE_END) {
10052 cache.state = ScrollabilityCache.OFF;
10053 } else {
10054 cache.scrollBar.setAlpha(Math.round(values[0]));
10055 }
Joe Malin32736f02011-01-19 16:14:20 -080010056
10057 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010058 // drawing. We only want this when we're fading so that
10059 // we prevent excessive redraws
10060 invalidate = true;
10061 } else {
10062 // We're just on -- but we may have been fading before so
10063 // reset alpha
10064 cache.scrollBar.setAlpha(255);
10065 }
10066
Joe Malin32736f02011-01-19 16:14:20 -080010067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 final int viewFlags = mViewFlags;
10069
10070 final boolean drawHorizontalScrollBar =
10071 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10072 final boolean drawVerticalScrollBar =
10073 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10074 && !isVerticalScrollBarHidden();
10075
10076 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10077 final int width = mRight - mLeft;
10078 final int height = mBottom - mTop;
10079
10080 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081
Mike Reede8853fc2009-09-04 14:01:48 -040010082 final int scrollX = mScrollX;
10083 final int scrollY = mScrollY;
10084 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10085
Mike Cleronf116bf82009-09-27 19:14:12 -070010086 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010089 int size = scrollBar.getSize(false);
10090 if (size <= 0) {
10091 size = cache.scrollBarSize;
10092 }
10093
Mike Cleronf116bf82009-09-27 19:14:12 -070010094 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010095 computeHorizontalScrollOffset(),
10096 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010097 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010098 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010099 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010100 left = scrollX + (mPaddingLeft & inside);
10101 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10102 bottom = top + size;
10103 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10104 if (invalidate) {
10105 invalidate(left, top, right, bottom);
10106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 }
10108
10109 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010110 int size = scrollBar.getSize(true);
10111 if (size <= 0) {
10112 size = cache.scrollBarSize;
10113 }
10114
Mike Reede8853fc2009-09-04 14:01:48 -040010115 scrollBar.setParameters(computeVerticalScrollRange(),
10116 computeVerticalScrollOffset(),
10117 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010118 switch (mVerticalScrollbarPosition) {
10119 default:
10120 case SCROLLBAR_POSITION_DEFAULT:
10121 case SCROLLBAR_POSITION_RIGHT:
10122 left = scrollX + width - size - (mUserPaddingRight & inside);
10123 break;
10124 case SCROLLBAR_POSITION_LEFT:
10125 left = scrollX + (mUserPaddingLeft & inside);
10126 break;
10127 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010128 top = scrollY + (mPaddingTop & inside);
10129 right = left + size;
10130 bottom = scrollY + height - (mUserPaddingBottom & inside);
10131 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10132 if (invalidate) {
10133 invalidate(left, top, right, bottom);
10134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 }
10136 }
10137 }
10138 }
Romain Guy8506ab42009-06-11 17:35:47 -070010139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010140 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010141 * 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 -080010142 * FastScroller is visible.
10143 * @return whether to temporarily hide the vertical scrollbar
10144 * @hide
10145 */
10146 protected boolean isVerticalScrollBarHidden() {
10147 return false;
10148 }
10149
10150 /**
10151 * <p>Draw the horizontal scrollbar if
10152 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 * @param canvas the canvas on which to draw the scrollbar
10155 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 *
10157 * @see #isHorizontalScrollBarEnabled()
10158 * @see #computeHorizontalScrollRange()
10159 * @see #computeHorizontalScrollExtent()
10160 * @see #computeHorizontalScrollOffset()
10161 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010162 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010163 */
Romain Guy8fb95422010-08-17 18:38:51 -070010164 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10165 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010166 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010167 scrollBar.draw(canvas);
10168 }
Mike Reede8853fc2009-09-04 14:01:48 -040010169
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10172 * returns true.</p>
10173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 * @param canvas the canvas on which to draw the scrollbar
10175 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 *
10177 * @see #isVerticalScrollBarEnabled()
10178 * @see #computeVerticalScrollRange()
10179 * @see #computeVerticalScrollExtent()
10180 * @see #computeVerticalScrollOffset()
10181 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010182 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183 */
Romain Guy8fb95422010-08-17 18:38:51 -070010184 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10185 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010186 scrollBar.setBounds(l, t, r, b);
10187 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 }
10189
10190 /**
10191 * Implement this to do your drawing.
10192 *
10193 * @param canvas the canvas on which the background will be drawn
10194 */
10195 protected void onDraw(Canvas canvas) {
10196 }
10197
10198 /*
10199 * Caller is responsible for calling requestLayout if necessary.
10200 * (This allows addViewInLayout to not request a new layout.)
10201 */
10202 void assignParent(ViewParent parent) {
10203 if (mParent == null) {
10204 mParent = parent;
10205 } else if (parent == null) {
10206 mParent = null;
10207 } else {
10208 throw new RuntimeException("view " + this + " being added, but"
10209 + " it already has a parent");
10210 }
10211 }
10212
10213 /**
10214 * This is called when the view is attached to a window. At this point it
10215 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010216 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10217 * however it may be called any time before the first onDraw -- including
10218 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 *
10220 * @see #onDetachedFromWindow()
10221 */
10222 protected void onAttachedToWindow() {
10223 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10224 mParent.requestTransparentRegion(this);
10225 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010226 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10227 initialAwakenScrollBars();
10228 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10229 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010230 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010231 // Order is important here: LayoutDirection MUST be resolved before Padding
10232 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010233 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010234 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010235 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010236 resolveTextAlignment();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010237 if (isFocused()) {
10238 InputMethodManager imm = InputMethodManager.peekInstance();
10239 imm.focusIn(this);
10240 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010241 }
Cibu Johny86666632010-02-22 13:01:02 -080010242
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010243 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010244 * @see #onScreenStateChanged(int)
10245 */
10246 void dispatchScreenStateChanged(int screenState) {
10247 onScreenStateChanged(screenState);
10248 }
10249
10250 /**
10251 * This method is called whenever the state of the screen this view is
10252 * attached to changes. A state change will usually occurs when the screen
10253 * turns on or off (whether it happens automatically or the user does it
10254 * manually.)
10255 *
10256 * @param screenState The new state of the screen. Can be either
10257 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10258 */
10259 public void onScreenStateChanged(int screenState) {
10260 }
10261
10262 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010263 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10264 */
10265 private boolean hasRtlSupport() {
10266 return mContext.getApplicationInfo().hasRtlSupport();
10267 }
10268
10269 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010270 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10271 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010272 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010273 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010274 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010275 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010276 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010277
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010278 if (hasRtlSupport()) {
10279 // Set resolved depending on layout direction
10280 switch (getLayoutDirection()) {
10281 case LAYOUT_DIRECTION_INHERIT:
10282 // If this is root view, no need to look at parent's layout dir.
10283 if (canResolveLayoutDirection()) {
10284 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010285
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010286 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10287 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10288 }
10289 } else {
10290 // Nothing to do, LTR by default
10291 }
10292 break;
10293 case LAYOUT_DIRECTION_RTL:
10294 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10295 break;
10296 case LAYOUT_DIRECTION_LOCALE:
10297 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010298 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10299 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010300 break;
10301 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010302 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010303 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010304 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010305
10306 // Set to resolved
10307 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010308 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010309 // Resolve padding
10310 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010311 }
10312
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010313 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010314 * Called when layout direction has been resolved.
10315 *
10316 * The default implementation does nothing.
10317 */
10318 public void onResolvedLayoutDirectionChanged() {
10319 }
10320
10321 /**
10322 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010323 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010324 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010325 // If the user specified the absolute padding (either with android:padding or
10326 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10327 // use the default padding or the padding from the background drawable
10328 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010329 int resolvedLayoutDirection = getResolvedLayoutDirection();
10330 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010331 case LAYOUT_DIRECTION_RTL:
10332 // Start user padding override Right user padding. Otherwise, if Right user
10333 // padding is not defined, use the default Right padding. If Right user padding
10334 // is defined, just use it.
10335 if (mUserPaddingStart >= 0) {
10336 mUserPaddingRight = mUserPaddingStart;
10337 } else if (mUserPaddingRight < 0) {
10338 mUserPaddingRight = mPaddingRight;
10339 }
10340 if (mUserPaddingEnd >= 0) {
10341 mUserPaddingLeft = mUserPaddingEnd;
10342 } else if (mUserPaddingLeft < 0) {
10343 mUserPaddingLeft = mPaddingLeft;
10344 }
10345 break;
10346 case LAYOUT_DIRECTION_LTR:
10347 default:
10348 // Start user padding override Left user padding. Otherwise, if Left user
10349 // padding is not defined, use the default left padding. If Left user padding
10350 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010351 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010352 mUserPaddingLeft = mUserPaddingStart;
10353 } else if (mUserPaddingLeft < 0) {
10354 mUserPaddingLeft = mPaddingLeft;
10355 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010356 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010357 mUserPaddingRight = mUserPaddingEnd;
10358 } else if (mUserPaddingRight < 0) {
10359 mUserPaddingRight = mPaddingRight;
10360 }
10361 }
10362
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010363 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10364
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010365 if(isPaddingRelative()) {
10366 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10367 } else {
10368 recomputePadding();
10369 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010370 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010371 }
10372
10373 /**
10374 * Resolve padding depending on the layout direction. Subclasses that care about
10375 * padding resolution should override this method. The default implementation does
10376 * nothing.
10377 *
10378 * @param layoutDirection the direction of the layout
10379 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010380 * @see {@link #LAYOUT_DIRECTION_LTR}
10381 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010382 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010383 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010384 }
10385
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010386 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010387 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010388 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010389 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010390 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010391 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010392 switch (getLayoutDirection()) {
10393 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010394 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010395 default:
10396 return true;
10397 }
10398 }
10399
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010400 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010401 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10402 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010403 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010404 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010405 // Reset the current resolved bits
10406 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010407 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010408 // Reset also the text direction
10409 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010410 }
10411
10412 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010413 * Called during reset of resolved layout direction.
10414 *
10415 * Subclasses need to override this method to clear cached information that depends on the
10416 * resolved layout direction, or to inform child views that inherit their layout direction.
10417 *
10418 * The default implementation does nothing.
10419 */
10420 public void onResolvedLayoutDirectionReset() {
10421 }
10422
10423 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010424 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010425 *
10426 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010427 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010428 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010429 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010430 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 }
10432
10433 /**
10434 * This is called when the view is detached from a window. At this point it
10435 * no longer has a surface for drawing.
10436 *
10437 * @see #onAttachedToWindow()
10438 */
10439 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010440 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010441
Romain Guya440b002010-02-24 15:57:54 -080010442 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010443 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010444 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010445 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010448
Romain Guya998dff2012-03-23 18:58:36 -070010449 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010450
10451 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010452 if (mDisplayList != null) {
10453 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10454 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010455 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010456 } else {
10457 if (mDisplayList != null) {
10458 // Should never happen
10459 mDisplayList.invalidate();
10460 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010461 }
10462
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010463 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010464
10465 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010466 resetResolvedTextAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 }
10468
10469 /**
10470 * @return The number of times this view has been attached to a window
10471 */
10472 protected int getWindowAttachCount() {
10473 return mWindowAttachCount;
10474 }
10475
10476 /**
10477 * Retrieve a unique token identifying the window this view is attached to.
10478 * @return Return the window's token for use in
10479 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10480 */
10481 public IBinder getWindowToken() {
10482 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10483 }
10484
10485 /**
10486 * Retrieve a unique token identifying the top-level "real" window of
10487 * the window that this view is attached to. That is, this is like
10488 * {@link #getWindowToken}, except if the window this view in is a panel
10489 * window (attached to another containing window), then the token of
10490 * the containing window is returned instead.
10491 *
10492 * @return Returns the associated window token, either
10493 * {@link #getWindowToken()} or the containing window's token.
10494 */
10495 public IBinder getApplicationWindowToken() {
10496 AttachInfo ai = mAttachInfo;
10497 if (ai != null) {
10498 IBinder appWindowToken = ai.mPanelParentWindowToken;
10499 if (appWindowToken == null) {
10500 appWindowToken = ai.mWindowToken;
10501 }
10502 return appWindowToken;
10503 }
10504 return null;
10505 }
10506
10507 /**
10508 * Retrieve private session object this view hierarchy is using to
10509 * communicate with the window manager.
10510 * @return the session object to communicate with the window manager
10511 */
10512 /*package*/ IWindowSession getWindowSession() {
10513 return mAttachInfo != null ? mAttachInfo.mSession : null;
10514 }
10515
10516 /**
10517 * @param info the {@link android.view.View.AttachInfo} to associated with
10518 * this view
10519 */
10520 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10521 //System.out.println("Attached! " + this);
10522 mAttachInfo = info;
10523 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010524 // We will need to evaluate the drawable state at least once.
10525 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 if (mFloatingTreeObserver != null) {
10527 info.mTreeObserver.merge(mFloatingTreeObserver);
10528 mFloatingTreeObserver = null;
10529 }
10530 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10531 mAttachInfo.mScrollContainers.add(this);
10532 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10533 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070010534 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010536
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010537 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010538 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010539 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010540 if (listeners != null && listeners.size() > 0) {
10541 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10542 // perform the dispatching. The iterator is a safe guard against listeners that
10543 // could mutate the list by calling the various add/remove methods. This prevents
10544 // the array from being modified while we iterate it.
10545 for (OnAttachStateChangeListener listener : listeners) {
10546 listener.onViewAttachedToWindow(this);
10547 }
10548 }
10549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 int vis = info.mWindowVisibility;
10551 if (vis != GONE) {
10552 onWindowVisibilityChanged(vis);
10553 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010554 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10555 // If nobody has evaluated the drawable state yet, then do it now.
10556 refreshDrawableState();
10557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 }
10559
10560 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 AttachInfo info = mAttachInfo;
10562 if (info != null) {
10563 int vis = info.mWindowVisibility;
10564 if (vis != GONE) {
10565 onWindowVisibilityChanged(GONE);
10566 }
10567 }
10568
10569 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010570
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010571 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010572 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010573 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010574 if (listeners != null && listeners.size() > 0) {
10575 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10576 // perform the dispatching. The iterator is a safe guard against listeners that
10577 // could mutate the list by calling the various add/remove methods. This prevents
10578 // the array from being modified while we iterate it.
10579 for (OnAttachStateChangeListener listener : listeners) {
10580 listener.onViewDetachedFromWindow(this);
10581 }
10582 }
10583
Romain Guy01d5edc2011-01-28 11:28:53 -080010584 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 mAttachInfo.mScrollContainers.remove(this);
10586 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10587 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 mAttachInfo = null;
10590 }
10591
10592 /**
10593 * Store this view hierarchy's frozen state into the given container.
10594 *
10595 * @param container The SparseArray in which to save the view's state.
10596 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010597 * @see #restoreHierarchyState(android.util.SparseArray)
10598 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10599 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 */
10601 public void saveHierarchyState(SparseArray<Parcelable> container) {
10602 dispatchSaveInstanceState(container);
10603 }
10604
10605 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010606 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10607 * this view and its children. May be overridden to modify how freezing happens to a
10608 * 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 -080010609 *
10610 * @param container The SparseArray in which to save the view's state.
10611 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010612 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10613 * @see #saveHierarchyState(android.util.SparseArray)
10614 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 */
10616 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10617 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10618 mPrivateFlags &= ~SAVE_STATE_CALLED;
10619 Parcelable state = onSaveInstanceState();
10620 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10621 throw new IllegalStateException(
10622 "Derived class did not call super.onSaveInstanceState()");
10623 }
10624 if (state != null) {
10625 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10626 // + ": " + state);
10627 container.put(mID, state);
10628 }
10629 }
10630 }
10631
10632 /**
10633 * Hook allowing a view to generate a representation of its internal state
10634 * that can later be used to create a new instance with that same state.
10635 * This state should only contain information that is not persistent or can
10636 * not be reconstructed later. For example, you will never store your
10637 * current position on screen because that will be computed again when a
10638 * new instance of the view is placed in its view hierarchy.
10639 * <p>
10640 * Some examples of things you may store here: the current cursor position
10641 * in a text view (but usually not the text itself since that is stored in a
10642 * content provider or other persistent storage), the currently selected
10643 * item in a list view.
10644 *
10645 * @return Returns a Parcelable object containing the view's current dynamic
10646 * state, or null if there is nothing interesting to save. The
10647 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070010648 * @see #onRestoreInstanceState(android.os.Parcelable)
10649 * @see #saveHierarchyState(android.util.SparseArray)
10650 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 * @see #setSaveEnabled(boolean)
10652 */
10653 protected Parcelable onSaveInstanceState() {
10654 mPrivateFlags |= SAVE_STATE_CALLED;
10655 return BaseSavedState.EMPTY_STATE;
10656 }
10657
10658 /**
10659 * Restore this view hierarchy's frozen state from the given container.
10660 *
10661 * @param container The SparseArray which holds previously frozen states.
10662 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010663 * @see #saveHierarchyState(android.util.SparseArray)
10664 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10665 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 */
10667 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10668 dispatchRestoreInstanceState(container);
10669 }
10670
10671 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010672 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10673 * state for this view and its children. May be overridden to modify how restoring
10674 * happens to a view's children; for example, some views may want to not store state
10675 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 *
10677 * @param container The SparseArray which holds previously saved state.
10678 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010679 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10680 * @see #restoreHierarchyState(android.util.SparseArray)
10681 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 */
10683 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10684 if (mID != NO_ID) {
10685 Parcelable state = container.get(mID);
10686 if (state != null) {
10687 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10688 // + ": " + state);
10689 mPrivateFlags &= ~SAVE_STATE_CALLED;
10690 onRestoreInstanceState(state);
10691 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10692 throw new IllegalStateException(
10693 "Derived class did not call super.onRestoreInstanceState()");
10694 }
10695 }
10696 }
10697 }
10698
10699 /**
10700 * Hook allowing a view to re-apply a representation of its internal state that had previously
10701 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10702 * null state.
10703 *
10704 * @param state The frozen state that had previously been returned by
10705 * {@link #onSaveInstanceState}.
10706 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010707 * @see #onSaveInstanceState()
10708 * @see #restoreHierarchyState(android.util.SparseArray)
10709 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 */
10711 protected void onRestoreInstanceState(Parcelable state) {
10712 mPrivateFlags |= SAVE_STATE_CALLED;
10713 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010714 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10715 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010716 + "when two views of different type have the same id in the same hierarchy. "
10717 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010718 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 }
10720 }
10721
10722 /**
10723 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10724 *
10725 * @return the drawing start time in milliseconds
10726 */
10727 public long getDrawingTime() {
10728 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10729 }
10730
10731 /**
10732 * <p>Enables or disables the duplication of the parent's state into this view. When
10733 * duplication is enabled, this view gets its drawable state from its parent rather
10734 * than from its own internal properties.</p>
10735 *
10736 * <p>Note: in the current implementation, setting this property to true after the
10737 * view was added to a ViewGroup might have no effect at all. This property should
10738 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10739 *
10740 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10741 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010742 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010743 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10744 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 *
10746 * @param enabled True to enable duplication of the parent's drawable state, false
10747 * to disable it.
10748 *
10749 * @see #getDrawableState()
10750 * @see #isDuplicateParentStateEnabled()
10751 */
10752 public void setDuplicateParentStateEnabled(boolean enabled) {
10753 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10754 }
10755
10756 /**
10757 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10758 *
10759 * @return True if this view's drawable state is duplicated from the parent,
10760 * false otherwise
10761 *
10762 * @see #getDrawableState()
10763 * @see #setDuplicateParentStateEnabled(boolean)
10764 */
10765 public boolean isDuplicateParentStateEnabled() {
10766 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10767 }
10768
10769 /**
Romain Guy171c5922011-01-06 10:04:23 -080010770 * <p>Specifies the type of layer backing this view. The layer can be
10771 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10772 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010773 *
Romain Guy171c5922011-01-06 10:04:23 -080010774 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10775 * instance that controls how the layer is composed on screen. The following
10776 * properties of the paint are taken into account when composing the layer:</p>
10777 * <ul>
10778 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10779 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10780 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10781 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010782 *
Romain Guy171c5922011-01-06 10:04:23 -080010783 * <p>If this view has an alpha value set to < 1.0 by calling
10784 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10785 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10786 * equivalent to setting a hardware layer on this view and providing a paint with
10787 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010788 *
Romain Guy171c5922011-01-06 10:04:23 -080010789 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10790 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10791 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010792 *
Romain Guy171c5922011-01-06 10:04:23 -080010793 * @param layerType The ype of layer to use with this view, must be one of
10794 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10795 * {@link #LAYER_TYPE_HARDWARE}
10796 * @param paint The paint used to compose the layer. This argument is optional
10797 * and can be null. It is ignored when the layer type is
10798 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010799 *
10800 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010801 * @see #LAYER_TYPE_NONE
10802 * @see #LAYER_TYPE_SOFTWARE
10803 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010804 * @see #setAlpha(float)
10805 *
Romain Guy171c5922011-01-06 10:04:23 -080010806 * @attr ref android.R.styleable#View_layerType
10807 */
10808 public void setLayerType(int layerType, Paint paint) {
10809 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010810 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010811 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10812 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010813
Romain Guyd6cd5722011-01-17 14:42:41 -080010814 if (layerType == mLayerType) {
10815 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10816 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010817 invalidateParentCaches();
10818 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010819 }
10820 return;
10821 }
Romain Guy171c5922011-01-06 10:04:23 -080010822
10823 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010824 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010825 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070010826 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010827 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010828 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010829 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010830 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010831 default:
10832 break;
Romain Guy171c5922011-01-06 10:04:23 -080010833 }
10834
10835 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010836 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10837 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10838 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010839
Romain Guy0fd89bf2011-01-26 15:41:30 -080010840 invalidateParentCaches();
10841 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010842 }
10843
10844 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010845 * Indicates whether this view has a static layer. A view with layer type
10846 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10847 * dynamic.
10848 */
10849 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010850 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010851 }
10852
10853 /**
Romain Guy171c5922011-01-06 10:04:23 -080010854 * Indicates what type of layer is currently associated with this view. By default
10855 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10856 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10857 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010858 *
Romain Guy171c5922011-01-06 10:04:23 -080010859 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10860 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010861 *
10862 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070010863 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010864 * @see #LAYER_TYPE_NONE
10865 * @see #LAYER_TYPE_SOFTWARE
10866 * @see #LAYER_TYPE_HARDWARE
10867 */
10868 public int getLayerType() {
10869 return mLayerType;
10870 }
Joe Malin32736f02011-01-19 16:14:20 -080010871
Romain Guy6c319ca2011-01-11 14:29:25 -080010872 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010873 * Forces this view's layer to be created and this view to be rendered
10874 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10875 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070010876 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010877 * This method can for instance be used to render a view into its layer before
10878 * starting an animation. If this view is complex, rendering into the layer
10879 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070010880 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010881 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070010882 *
10883 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010884 */
10885 public void buildLayer() {
10886 if (mLayerType == LAYER_TYPE_NONE) return;
10887
10888 if (mAttachInfo == null) {
10889 throw new IllegalStateException("This view must be attached to a window first");
10890 }
10891
10892 switch (mLayerType) {
10893 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010894 if (mAttachInfo.mHardwareRenderer != null &&
10895 mAttachInfo.mHardwareRenderer.isEnabled() &&
10896 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010897 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010898 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010899 break;
10900 case LAYER_TYPE_SOFTWARE:
10901 buildDrawingCache(true);
10902 break;
10903 }
10904 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010905
Romain Guy9c4b79a2011-11-10 19:23:58 -080010906 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10907 void flushLayer() {
10908 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10909 mHardwareLayer.flush();
10910 }
10911 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010912
10913 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010914 * <p>Returns a hardware layer that can be used to draw this view again
10915 * without executing its draw method.</p>
10916 *
10917 * @return A HardwareLayer ready to render, or null if an error occurred.
10918 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010919 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010920 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10921 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010922 return null;
10923 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010924
Romain Guy9c4b79a2011-11-10 19:23:58 -080010925 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010926
10927 final int width = mRight - mLeft;
10928 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010929
Romain Guy6c319ca2011-01-11 14:29:25 -080010930 if (width == 0 || height == 0) {
10931 return null;
10932 }
10933
10934 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10935 if (mHardwareLayer == null) {
10936 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10937 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010938 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010939 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10940 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010941 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010942 }
10943
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010944 // The layer is not valid if the underlying GPU resources cannot be allocated
10945 if (!mHardwareLayer.isValid()) {
10946 return null;
10947 }
10948
Chet Haasea1cff502012-02-21 13:43:44 -080010949 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010950 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010951 }
10952
10953 return mHardwareLayer;
10954 }
Romain Guy171c5922011-01-06 10:04:23 -080010955
Romain Guy589b0bb2011-10-10 13:57:47 -070010956 /**
10957 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070010958 *
Romain Guy589b0bb2011-10-10 13:57:47 -070010959 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070010960 *
10961 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070010962 * @see #LAYER_TYPE_HARDWARE
10963 */
Romain Guya998dff2012-03-23 18:58:36 -070010964 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070010965 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010966 AttachInfo info = mAttachInfo;
10967 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070010968 info.mHardwareRenderer.isEnabled() &&
10969 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010970 mHardwareLayer.destroy();
10971 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010972
Romain Guy9c4b79a2011-11-10 19:23:58 -080010973 invalidate(true);
10974 invalidateParentCaches();
10975 }
Romain Guy65b345f2011-07-27 18:51:50 -070010976 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010977 }
Romain Guy65b345f2011-07-27 18:51:50 -070010978 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010979 }
10980
Romain Guy171c5922011-01-06 10:04:23 -080010981 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010982 * Destroys all hardware rendering resources. This method is invoked
10983 * when the system needs to reclaim resources. Upon execution of this
10984 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070010985 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010986 * Note: you <strong>must</strong> call
10987 * <code>super.destroyHardwareResources()</code> when overriding
10988 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070010989 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010990 * @hide
10991 */
10992 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070010993 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010994 }
10995
10996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010997 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10998 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10999 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11000 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11001 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11002 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011003 *
Romain Guy171c5922011-01-06 10:04:23 -080011004 * <p>Enabling the drawing cache is similar to
11005 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011006 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11007 * drawing cache has no effect on rendering because the system uses a different mechanism
11008 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11009 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11010 * for information on how to enable software and hardware layers.</p>
11011 *
11012 * <p>This API can be used to manually generate
11013 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11014 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011015 *
11016 * @param enabled true to enable the drawing cache, false otherwise
11017 *
11018 * @see #isDrawingCacheEnabled()
11019 * @see #getDrawingCache()
11020 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011021 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 */
11023 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011024 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011025 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11026 }
11027
11028 /**
11029 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11030 *
11031 * @return true if the drawing cache is enabled
11032 *
11033 * @see #setDrawingCacheEnabled(boolean)
11034 * @see #getDrawingCache()
11035 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011036 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 public boolean isDrawingCacheEnabled() {
11038 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11039 }
11040
11041 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011042 * Debugging utility which recursively outputs the dirty state of a view and its
11043 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011044 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011045 * @hide
11046 */
Romain Guy676b1732011-02-14 14:45:33 -080011047 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011048 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11049 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11050 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11051 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11052 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11053 if (clear) {
11054 mPrivateFlags &= clearMask;
11055 }
11056 if (this instanceof ViewGroup) {
11057 ViewGroup parent = (ViewGroup) this;
11058 final int count = parent.getChildCount();
11059 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011060 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011061 child.outputDirtyFlags(indent + " ", clear, clearMask);
11062 }
11063 }
11064 }
11065
11066 /**
11067 * This method is used by ViewGroup to cause its children to restore or recreate their
11068 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11069 * to recreate its own display list, which would happen if it went through the normal
11070 * draw/dispatchDraw mechanisms.
11071 *
11072 * @hide
11073 */
11074 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011075
11076 /**
11077 * A view that is not attached or hardware accelerated cannot create a display list.
11078 * This method checks these conditions and returns the appropriate result.
11079 *
11080 * @return true if view has the ability to create a display list, false otherwise.
11081 *
11082 * @hide
11083 */
11084 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011085 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011086 }
Joe Malin32736f02011-01-19 16:14:20 -080011087
Chet Haasedaf98e92011-01-10 14:10:36 -080011088 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011089 * @return The HardwareRenderer associated with that view or null if hardware rendering
11090 * is not supported or this this has not been attached to a window.
11091 *
11092 * @hide
11093 */
11094 public HardwareRenderer getHardwareRenderer() {
11095 if (mAttachInfo != null) {
11096 return mAttachInfo.mHardwareRenderer;
11097 }
11098 return null;
11099 }
11100
11101 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011102 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11103 * Otherwise, the same display list will be returned (after having been rendered into
11104 * along the way, depending on the invalidation state of the view).
11105 *
11106 * @param displayList The previous version of this displayList, could be null.
11107 * @param isLayer Whether the requester of the display list is a layer. If so,
11108 * the view will avoid creating a layer inside the resulting display list.
11109 * @return A new or reused DisplayList object.
11110 */
11111 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11112 if (!canHaveDisplayList()) {
11113 return null;
11114 }
11115
11116 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11117 displayList == null || !displayList.isValid() ||
11118 (!isLayer && mRecreateDisplayList))) {
11119 // Don't need to recreate the display list, just need to tell our
11120 // children to restore/recreate theirs
11121 if (displayList != null && displayList.isValid() &&
11122 !isLayer && !mRecreateDisplayList) {
11123 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11124 mPrivateFlags &= ~DIRTY_MASK;
11125 dispatchGetDisplayList();
11126
11127 return displayList;
11128 }
11129
11130 if (!isLayer) {
11131 // If we got here, we're recreating it. Mark it as such to ensure that
11132 // we copy in child display lists into ours in drawChild()
11133 mRecreateDisplayList = true;
11134 }
11135 if (displayList == null) {
11136 final String name = getClass().getSimpleName();
11137 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11138 // If we're creating a new display list, make sure our parent gets invalidated
11139 // since they will need to recreate their display list to account for this
11140 // new child display list.
11141 invalidateParentCaches();
11142 }
11143
11144 boolean caching = false;
11145 final HardwareCanvas canvas = displayList.start();
11146 int restoreCount = 0;
11147 int width = mRight - mLeft;
11148 int height = mBottom - mTop;
11149
11150 try {
11151 canvas.setViewport(width, height);
11152 // The dirty rect should always be null for a display list
11153 canvas.onPreDraw(null);
11154 int layerType = (
11155 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11156 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011157 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11158 if (layerType == LAYER_TYPE_HARDWARE) {
11159 final HardwareLayer layer = getHardwareLayer();
11160 if (layer != null && layer.isValid()) {
11161 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11162 } else {
11163 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11164 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11165 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11166 }
11167 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011168 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011169 buildDrawingCache(true);
11170 Bitmap cache = getDrawingCache(true);
11171 if (cache != null) {
11172 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11173 caching = true;
11174 }
Chet Haasea1cff502012-02-21 13:43:44 -080011175 }
Chet Haasea1cff502012-02-21 13:43:44 -080011176 } else {
11177
11178 computeScroll();
11179
11180 if (!USE_DISPLAY_LIST_PROPERTIES) {
11181 restoreCount = canvas.save();
11182 }
11183 canvas.translate(-mScrollX, -mScrollY);
11184 if (!isLayer) {
11185 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11186 mPrivateFlags &= ~DIRTY_MASK;
11187 }
11188
11189 // Fast path for layouts with no backgrounds
11190 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11191 dispatchDraw(canvas);
11192 } else {
11193 draw(canvas);
11194 }
11195 }
11196 } finally {
11197 if (USE_DISPLAY_LIST_PROPERTIES) {
11198 canvas.restoreToCount(restoreCount);
11199 }
11200 canvas.onPostDraw();
11201
11202 displayList.end();
11203 if (USE_DISPLAY_LIST_PROPERTIES) {
11204 displayList.setCaching(caching);
11205 }
11206 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11207 displayList.setLeftTopRightBottom(0, 0, width, height);
11208 } else {
11209 setDisplayListProperties(displayList);
11210 }
11211 }
11212 } else if (!isLayer) {
11213 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11214 mPrivateFlags &= ~DIRTY_MASK;
11215 }
11216
11217 return displayList;
11218 }
11219
11220 /**
11221 * Get the DisplayList for the HardwareLayer
11222 *
11223 * @param layer The HardwareLayer whose DisplayList we want
11224 * @return A DisplayList fopr the specified HardwareLayer
11225 */
11226 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11227 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11228 layer.setDisplayList(displayList);
11229 return displayList;
11230 }
11231
11232
11233 /**
Romain Guyb051e892010-09-28 19:09:36 -070011234 * <p>Returns a display list that can be used to draw this view again
11235 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011236 *
Romain Guyb051e892010-09-28 19:09:36 -070011237 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011238 *
11239 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011240 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011241 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011242 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011243 return mDisplayList;
11244 }
11245
11246 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011247 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011248 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011249 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011250 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011251 * @see #getDrawingCache(boolean)
11252 */
11253 public Bitmap getDrawingCache() {
11254 return getDrawingCache(false);
11255 }
11256
11257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11259 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11260 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11261 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11262 * request the drawing cache by calling this method and draw it on screen if the
11263 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011264 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011265 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11266 * this method will create a bitmap of the same size as this view. Because this bitmap
11267 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11268 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11269 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11270 * size than the view. This implies that your application must be able to handle this
11271 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011272 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011273 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11274 * the current density of the screen when the application is in compatibility
11275 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011276 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011277 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011279 * @see #setDrawingCacheEnabled(boolean)
11280 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011281 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011282 * @see #destroyDrawingCache()
11283 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011284 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011285 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11286 return null;
11287 }
11288 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011289 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 }
Romain Guy02890fd2010-08-06 17:58:44 -070011291 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 }
11293
11294 /**
11295 * <p>Frees the resources used by the drawing cache. If you call
11296 * {@link #buildDrawingCache()} manually without calling
11297 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11298 * should cleanup the cache with this method afterwards.</p>
11299 *
11300 * @see #setDrawingCacheEnabled(boolean)
11301 * @see #buildDrawingCache()
11302 * @see #getDrawingCache()
11303 */
11304 public void destroyDrawingCache() {
11305 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011306 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011307 mDrawingCache = null;
11308 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011309 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011310 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011311 mUnscaledDrawingCache = null;
11312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 }
11314
11315 /**
11316 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011317 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011318 * view will always be drawn on top of a solid color.
11319 *
11320 * @param color The background color to use for the drawing cache's bitmap
11321 *
11322 * @see #setDrawingCacheEnabled(boolean)
11323 * @see #buildDrawingCache()
11324 * @see #getDrawingCache()
11325 */
11326 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011327 if (color != mDrawingCacheBackgroundColor) {
11328 mDrawingCacheBackgroundColor = color;
11329 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 }
11332
11333 /**
11334 * @see #setDrawingCacheBackgroundColor(int)
11335 *
11336 * @return The background color to used for the drawing cache's bitmap
11337 */
11338 public int getDrawingCacheBackgroundColor() {
11339 return mDrawingCacheBackgroundColor;
11340 }
11341
11342 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011343 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011344 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011345 * @see #buildDrawingCache(boolean)
11346 */
11347 public void buildDrawingCache() {
11348 buildDrawingCache(false);
11349 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011350
Romain Guyfbd8f692009-06-26 14:51:58 -070011351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011352 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11353 *
11354 * <p>If you call {@link #buildDrawingCache()} manually without calling
11355 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11356 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011357 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011358 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11359 * this method will create a bitmap of the same size as this view. Because this bitmap
11360 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11361 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11362 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11363 * size than the view. This implies that your application must be able to handle this
11364 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011365 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011366 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11367 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011368 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011369 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011370 *
11371 * @see #getDrawingCache()
11372 * @see #destroyDrawingCache()
11373 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011374 public void buildDrawingCache(boolean autoScale) {
11375 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011376 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011377 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011378
11379 if (ViewDebug.TRACE_HIERARCHY) {
11380 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011382
Romain Guy8506ab42009-06-11 17:35:47 -070011383 int width = mRight - mLeft;
11384 int height = mBottom - mTop;
11385
11386 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011387 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011388
Romain Guye1123222009-06-29 14:24:56 -070011389 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011390 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11391 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393
11394 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011395 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011396 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397
11398 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011399 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011400 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011401 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11402 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011403 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404 return;
11405 }
11406
11407 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011408 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409
11410 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011411 Bitmap.Config quality;
11412 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011413 // Never pick ARGB_4444 because it looks awful
11414 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011415 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11416 case DRAWING_CACHE_QUALITY_AUTO:
11417 quality = Bitmap.Config.ARGB_8888;
11418 break;
11419 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011420 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011421 break;
11422 case DRAWING_CACHE_QUALITY_HIGH:
11423 quality = Bitmap.Config.ARGB_8888;
11424 break;
11425 default:
11426 quality = Bitmap.Config.ARGB_8888;
11427 break;
11428 }
11429 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011430 // Optimization for translucent windows
11431 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011432 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 }
11434
11435 // Try to cleanup memory
11436 if (bitmap != null) bitmap.recycle();
11437
11438 try {
11439 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011440 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011441 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011442 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011443 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011444 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011445 }
Adam Powell26153a32010-11-08 15:22:27 -080011446 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011447 } catch (OutOfMemoryError e) {
11448 // If there is not enough memory to create the bitmap cache, just
11449 // ignore the issue as bitmap caches are not required to draw the
11450 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011451 if (autoScale) {
11452 mDrawingCache = null;
11453 } else {
11454 mUnscaledDrawingCache = null;
11455 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011456 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011457 return;
11458 }
11459
11460 clear = drawingCacheBackgroundColor != 0;
11461 }
11462
11463 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 if (attachInfo != null) {
11465 canvas = attachInfo.mCanvas;
11466 if (canvas == null) {
11467 canvas = new Canvas();
11468 }
11469 canvas.setBitmap(bitmap);
11470 // Temporarily clobber the cached Canvas in case one of our children
11471 // is also using a drawing cache. Without this, the children would
11472 // steal the canvas by attaching their own bitmap to it and bad, bad
11473 // thing would happen (invisible views, corrupted drawings, etc.)
11474 attachInfo.mCanvas = null;
11475 } else {
11476 // This case should hopefully never or seldom happen
11477 canvas = new Canvas(bitmap);
11478 }
11479
11480 if (clear) {
11481 bitmap.eraseColor(drawingCacheBackgroundColor);
11482 }
11483
11484 computeScroll();
11485 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011486
Romain Guye1123222009-06-29 14:24:56 -070011487 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011488 final float scale = attachInfo.mApplicationScale;
11489 canvas.scale(scale, scale);
11490 }
Joe Malin32736f02011-01-19 16:14:20 -080011491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011492 canvas.translate(-mScrollX, -mScrollY);
11493
Romain Guy5bcdff42009-05-14 21:27:18 -070011494 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011495 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11496 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011497 mPrivateFlags |= DRAWING_CACHE_VALID;
11498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499
11500 // Fast path for layouts with no backgrounds
11501 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11502 if (ViewDebug.TRACE_HIERARCHY) {
11503 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11504 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011505 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011506 dispatchDraw(canvas);
11507 } else {
11508 draw(canvas);
11509 }
11510
11511 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011512 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513
11514 if (attachInfo != null) {
11515 // Restore the cached Canvas for our siblings
11516 attachInfo.mCanvas = canvas;
11517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011518 }
11519 }
11520
11521 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011522 * Create a snapshot of the view into a bitmap. We should probably make
11523 * some form of this public, but should think about the API.
11524 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011525 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011526 int width = mRight - mLeft;
11527 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011528
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011529 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011530 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011531 width = (int) ((width * scale) + 0.5f);
11532 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011533
Romain Guy8c11e312009-09-14 15:15:30 -070011534 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011535 if (bitmap == null) {
11536 throw new OutOfMemoryError();
11537 }
11538
Romain Guyc529d8d2011-09-06 15:01:39 -070011539 Resources resources = getResources();
11540 if (resources != null) {
11541 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11542 }
Joe Malin32736f02011-01-19 16:14:20 -080011543
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011544 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011545 if (attachInfo != null) {
11546 canvas = attachInfo.mCanvas;
11547 if (canvas == null) {
11548 canvas = new Canvas();
11549 }
11550 canvas.setBitmap(bitmap);
11551 // Temporarily clobber the cached Canvas in case one of our children
11552 // is also using a drawing cache. Without this, the children would
11553 // steal the canvas by attaching their own bitmap to it and bad, bad
11554 // things would happen (invisible views, corrupted drawings, etc.)
11555 attachInfo.mCanvas = null;
11556 } else {
11557 // This case should hopefully never or seldom happen
11558 canvas = new Canvas(bitmap);
11559 }
11560
Romain Guy5bcdff42009-05-14 21:27:18 -070011561 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011562 bitmap.eraseColor(backgroundColor);
11563 }
11564
11565 computeScroll();
11566 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011567 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011568 canvas.translate(-mScrollX, -mScrollY);
11569
Romain Guy5bcdff42009-05-14 21:27:18 -070011570 // Temporarily remove the dirty mask
11571 int flags = mPrivateFlags;
11572 mPrivateFlags &= ~DIRTY_MASK;
11573
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011574 // Fast path for layouts with no backgrounds
11575 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11576 dispatchDraw(canvas);
11577 } else {
11578 draw(canvas);
11579 }
11580
Romain Guy5bcdff42009-05-14 21:27:18 -070011581 mPrivateFlags = flags;
11582
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011583 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011584 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011585
11586 if (attachInfo != null) {
11587 // Restore the cached Canvas for our siblings
11588 attachInfo.mCanvas = canvas;
11589 }
Romain Guy8506ab42009-06-11 17:35:47 -070011590
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011591 return bitmap;
11592 }
11593
11594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011595 * Indicates whether this View is currently in edit mode. A View is usually
11596 * in edit mode when displayed within a developer tool. For instance, if
11597 * this View is being drawn by a visual user interface builder, this method
11598 * should return true.
11599 *
11600 * Subclasses should check the return value of this method to provide
11601 * different behaviors if their normal behavior might interfere with the
11602 * host environment. For instance: the class spawns a thread in its
11603 * constructor, the drawing code relies on device-specific features, etc.
11604 *
11605 * This method is usually checked in the drawing code of custom widgets.
11606 *
11607 * @return True if this View is in edit mode, false otherwise.
11608 */
11609 public boolean isInEditMode() {
11610 return false;
11611 }
11612
11613 /**
11614 * If the View draws content inside its padding and enables fading edges,
11615 * it needs to support padding offsets. Padding offsets are added to the
11616 * fading edges to extend the length of the fade so that it covers pixels
11617 * drawn inside the padding.
11618 *
11619 * Subclasses of this class should override this method if they need
11620 * to draw content inside the padding.
11621 *
11622 * @return True if padding offset must be applied, false otherwise.
11623 *
11624 * @see #getLeftPaddingOffset()
11625 * @see #getRightPaddingOffset()
11626 * @see #getTopPaddingOffset()
11627 * @see #getBottomPaddingOffset()
11628 *
11629 * @since CURRENT
11630 */
11631 protected boolean isPaddingOffsetRequired() {
11632 return false;
11633 }
11634
11635 /**
11636 * Amount by which to extend the left fading region. Called only when
11637 * {@link #isPaddingOffsetRequired()} returns true.
11638 *
11639 * @return The left padding offset in pixels.
11640 *
11641 * @see #isPaddingOffsetRequired()
11642 *
11643 * @since CURRENT
11644 */
11645 protected int getLeftPaddingOffset() {
11646 return 0;
11647 }
11648
11649 /**
11650 * Amount by which to extend the right fading region. Called only when
11651 * {@link #isPaddingOffsetRequired()} returns true.
11652 *
11653 * @return The right padding offset in pixels.
11654 *
11655 * @see #isPaddingOffsetRequired()
11656 *
11657 * @since CURRENT
11658 */
11659 protected int getRightPaddingOffset() {
11660 return 0;
11661 }
11662
11663 /**
11664 * Amount by which to extend the top fading region. Called only when
11665 * {@link #isPaddingOffsetRequired()} returns true.
11666 *
11667 * @return The top padding offset in pixels.
11668 *
11669 * @see #isPaddingOffsetRequired()
11670 *
11671 * @since CURRENT
11672 */
11673 protected int getTopPaddingOffset() {
11674 return 0;
11675 }
11676
11677 /**
11678 * Amount by which to extend the bottom fading region. Called only when
11679 * {@link #isPaddingOffsetRequired()} returns true.
11680 *
11681 * @return The bottom padding offset in pixels.
11682 *
11683 * @see #isPaddingOffsetRequired()
11684 *
11685 * @since CURRENT
11686 */
11687 protected int getBottomPaddingOffset() {
11688 return 0;
11689 }
11690
11691 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011692 * @hide
11693 * @param offsetRequired
11694 */
11695 protected int getFadeTop(boolean offsetRequired) {
11696 int top = mPaddingTop;
11697 if (offsetRequired) top += getTopPaddingOffset();
11698 return top;
11699 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011700
Romain Guyf2fc4602011-07-19 15:20:03 -070011701 /**
11702 * @hide
11703 * @param offsetRequired
11704 */
11705 protected int getFadeHeight(boolean offsetRequired) {
11706 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070011707 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070011708 return mBottom - mTop - mPaddingBottom - padding;
11709 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011710
Romain Guyf2fc4602011-07-19 15:20:03 -070011711 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011712 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011713 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011714 *
Romain Guy2bffd262010-09-12 17:40:02 -070011715 * <p>Even if this method returns true, it does not mean that every call
11716 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11717 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011718 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011719 * window is hardware accelerated,
11720 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11721 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011722 *
Romain Guy2bffd262010-09-12 17:40:02 -070011723 * @return True if the view is attached to a window and the window is
11724 * hardware accelerated; false in any other case.
11725 */
11726 public boolean isHardwareAccelerated() {
11727 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11728 }
Joe Malin32736f02011-01-19 16:14:20 -080011729
Romain Guy2bffd262010-09-12 17:40:02 -070011730 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011731 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11732 * case of an active Animation being run on the view.
11733 */
11734 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11735 Animation a, boolean scalingRequired) {
11736 Transformation invalidationTransform;
11737 final int flags = parent.mGroupFlags;
11738 final boolean initialized = a.isInitialized();
11739 if (!initialized) {
11740 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11741 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11742 onAnimationStart();
11743 }
11744
11745 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11746 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11747 if (parent.mInvalidationTransformation == null) {
11748 parent.mInvalidationTransformation = new Transformation();
11749 }
11750 invalidationTransform = parent.mInvalidationTransformation;
11751 a.getTransformation(drawingTime, invalidationTransform, 1f);
11752 } else {
11753 invalidationTransform = parent.mChildTransformation;
11754 }
11755 if (more) {
11756 if (!a.willChangeBounds()) {
11757 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11758 parent.FLAG_OPTIMIZE_INVALIDATE) {
11759 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11760 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11761 // The child need to draw an animation, potentially offscreen, so
11762 // make sure we do not cancel invalidate requests
11763 parent.mPrivateFlags |= DRAW_ANIMATION;
11764 parent.invalidate(mLeft, mTop, mRight, mBottom);
11765 }
11766 } else {
11767 if (parent.mInvalidateRegion == null) {
11768 parent.mInvalidateRegion = new RectF();
11769 }
11770 final RectF region = parent.mInvalidateRegion;
11771 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11772 invalidationTransform);
11773
11774 // The child need to draw an animation, potentially offscreen, so
11775 // make sure we do not cancel invalidate requests
11776 parent.mPrivateFlags |= DRAW_ANIMATION;
11777
11778 final int left = mLeft + (int) region.left;
11779 final int top = mTop + (int) region.top;
11780 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11781 top + (int) (region.height() + .5f));
11782 }
11783 }
11784 return more;
11785 }
11786
Chet Haasea1cff502012-02-21 13:43:44 -080011787 void setDisplayListProperties() {
11788 setDisplayListProperties(mDisplayList);
11789 }
11790
11791 /**
11792 * This method is called by getDisplayList() when a display list is created or re-rendered.
11793 * It sets or resets the current value of all properties on that display list (resetting is
11794 * necessary when a display list is being re-created, because we need to make sure that
11795 * previously-set transform values
11796 */
11797 void setDisplayListProperties(DisplayList displayList) {
11798 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11799 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070011800 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080011801 if (mParent instanceof ViewGroup) {
11802 displayList.setClipChildren(
11803 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11804 }
Chet Haase9420abd2012-03-29 16:28:32 -070011805 float alpha = 1;
11806 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
11807 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
11808 ViewGroup parentVG = (ViewGroup) mParent;
11809 final boolean hasTransform =
11810 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
11811 if (hasTransform) {
11812 Transformation transform = parentVG.mChildTransformation;
11813 final int transformType = parentVG.mChildTransformation.getTransformationType();
11814 if (transformType != Transformation.TYPE_IDENTITY) {
11815 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
11816 alpha = transform.getAlpha();
11817 }
11818 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
11819 displayList.setStaticMatrix(transform.getMatrix());
11820 }
11821 }
11822 }
Chet Haasea1cff502012-02-21 13:43:44 -080011823 }
11824 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070011825 alpha *= mTransformationInfo.mAlpha;
11826 if (alpha < 1) {
11827 final int multipliedAlpha = (int) (255 * alpha);
11828 if (onSetAlpha(multipliedAlpha)) {
11829 alpha = 1;
11830 }
11831 }
11832 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080011833 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11834 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11835 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11836 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070011837 if (mTransformationInfo.mCamera == null) {
11838 mTransformationInfo.mCamera = new Camera();
11839 mTransformationInfo.matrix3D = new Matrix();
11840 }
11841 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080011842 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11843 displayList.setPivotX(getPivotX());
11844 displayList.setPivotY(getPivotY());
11845 }
Chet Haase9420abd2012-03-29 16:28:32 -070011846 } else if (alpha < 1) {
11847 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080011848 }
11849 }
11850 }
11851
Chet Haasebcca79a2012-02-14 08:45:14 -080011852 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011853 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11854 * This draw() method is an implementation detail and is not intended to be overridden or
11855 * to be called from anywhere else other than ViewGroup.drawChild().
11856 */
11857 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011858 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11859 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011860 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011861 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011862 final int flags = parent.mGroupFlags;
11863
Chet Haasea1cff502012-02-21 13:43:44 -080011864 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011865 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011866 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011867 }
11868
11869 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011870 boolean concatMatrix = false;
11871
11872 boolean scalingRequired = false;
11873 boolean caching;
11874 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11875
11876 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011877 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11878 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011879 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070011880 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080011881 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11882 } else {
11883 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11884 }
11885
Chet Haasebcca79a2012-02-14 08:45:14 -080011886 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011887 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011888 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011889 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011890 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070011891 } else if (!useDisplayListProperties &&
11892 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011893 final boolean hasTransform =
11894 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011895 if (hasTransform) {
11896 final int transformType = parent.mChildTransformation.getTransformationType();
11897 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11898 parent.mChildTransformation : null;
11899 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11900 }
11901 }
11902
11903 concatMatrix |= !childHasIdentityMatrix;
11904
11905 // Sets the flag as early as possible to allow draw() implementations
11906 // to call invalidate() successfully when doing animations
11907 mPrivateFlags |= DRAWN;
11908
Chet Haasebcca79a2012-02-14 08:45:14 -080011909 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011910 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11911 return more;
11912 }
11913
11914 if (hardwareAccelerated) {
11915 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11916 // retain the flag's value temporarily in the mRecreateDisplayList flag
11917 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11918 mPrivateFlags &= ~INVALIDATED;
11919 }
11920
11921 computeScroll();
11922
11923 final int sx = mScrollX;
11924 final int sy = mScrollY;
11925
11926 DisplayList displayList = null;
11927 Bitmap cache = null;
11928 boolean hasDisplayList = false;
11929 if (caching) {
11930 if (!hardwareAccelerated) {
11931 if (layerType != LAYER_TYPE_NONE) {
11932 layerType = LAYER_TYPE_SOFTWARE;
11933 buildDrawingCache(true);
11934 }
11935 cache = getDrawingCache(true);
11936 } else {
11937 switch (layerType) {
11938 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070011939 if (useDisplayListProperties) {
11940 hasDisplayList = canHaveDisplayList();
11941 } else {
11942 buildDrawingCache(true);
11943 cache = getDrawingCache(true);
11944 }
Chet Haase64a48c12012-02-13 16:33:29 -080011945 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011946 case LAYER_TYPE_HARDWARE:
11947 if (useDisplayListProperties) {
11948 hasDisplayList = canHaveDisplayList();
11949 }
11950 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011951 case LAYER_TYPE_NONE:
11952 // Delay getting the display list until animation-driven alpha values are
11953 // set up and possibly passed on to the view
11954 hasDisplayList = canHaveDisplayList();
11955 break;
11956 }
11957 }
11958 }
Chet Haasea1cff502012-02-21 13:43:44 -080011959 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070011960 if (useDisplayListProperties) {
11961 displayList = getDisplayList();
11962 if (!displayList.isValid()) {
11963 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11964 // to getDisplayList(), the display list will be marked invalid and we should not
11965 // try to use it again.
11966 displayList = null;
11967 hasDisplayList = false;
11968 useDisplayListProperties = false;
11969 }
11970 }
Chet Haase64a48c12012-02-13 16:33:29 -080011971
11972 final boolean hasNoCache = cache == null || hasDisplayList;
11973 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11974 layerType != LAYER_TYPE_HARDWARE;
11975
Chet Haasea1cff502012-02-21 13:43:44 -080011976 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011977 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011978 restoreTo = canvas.save();
11979 }
Chet Haase64a48c12012-02-13 16:33:29 -080011980 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011981 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011982 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011983 if (!useDisplayListProperties) {
11984 canvas.translate(mLeft, mTop);
11985 }
Chet Haase64a48c12012-02-13 16:33:29 -080011986 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011987 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070011988 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080011989 restoreTo = canvas.save();
11990 }
Chet Haase64a48c12012-02-13 16:33:29 -080011991 // mAttachInfo cannot be null, otherwise scalingRequired == false
11992 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11993 canvas.scale(scale, scale);
11994 }
11995 }
11996
Chet Haasea1cff502012-02-21 13:43:44 -080011997 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011998 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080011999 if (transformToApply != null || !childHasIdentityMatrix) {
12000 int transX = 0;
12001 int transY = 0;
12002
12003 if (offsetForScroll) {
12004 transX = -sx;
12005 transY = -sy;
12006 }
12007
12008 if (transformToApply != null) {
12009 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012010 if (useDisplayListProperties) {
12011 displayList.setAnimationMatrix(transformToApply.getMatrix());
12012 } else {
12013 // Undo the scroll translation, apply the transformation matrix,
12014 // then redo the scroll translate to get the correct result.
12015 canvas.translate(-transX, -transY);
12016 canvas.concat(transformToApply.getMatrix());
12017 canvas.translate(transX, transY);
12018 }
Chet Haasea1cff502012-02-21 13:43:44 -080012019 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012020 }
12021
12022 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012023 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012024 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012025 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012026 }
12027 }
12028
Chet Haasea1cff502012-02-21 13:43:44 -080012029 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012030 canvas.translate(-transX, -transY);
12031 canvas.concat(getMatrix());
12032 canvas.translate(transX, transY);
12033 }
12034 }
12035
Chet Haase9420abd2012-03-29 16:28:32 -070012036 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012037 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012038 if (hasNoCache) {
12039 final int multipliedAlpha = (int) (255 * alpha);
12040 if (!onSetAlpha(multipliedAlpha)) {
12041 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012042 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012043 layerType != LAYER_TYPE_NONE) {
12044 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12045 }
Chet Haase9420abd2012-03-29 16:28:32 -070012046 if (useDisplayListProperties) {
12047 displayList.setAlpha(alpha * getAlpha());
12048 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012049 final int scrollX = hasDisplayList ? 0 : sx;
12050 final int scrollY = hasDisplayList ? 0 : sy;
12051 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12052 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012053 }
12054 } else {
12055 // Alpha is handled by the child directly, clobber the layer's alpha
12056 mPrivateFlags |= ALPHA_SET;
12057 }
12058 }
12059 }
12060 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12061 onSetAlpha(255);
12062 mPrivateFlags &= ~ALPHA_SET;
12063 }
12064
Chet Haasea1cff502012-02-21 13:43:44 -080012065 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12066 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012067 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012068 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012069 } else {
12070 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012071 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012072 } else {
12073 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12074 }
12075 }
12076 }
12077
Chet Haase9420abd2012-03-29 16:28:32 -070012078 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012079 displayList = getDisplayList();
12080 if (!displayList.isValid()) {
12081 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12082 // to getDisplayList(), the display list will be marked invalid and we should not
12083 // try to use it again.
12084 displayList = null;
12085 hasDisplayList = false;
12086 }
12087 }
12088
12089 if (hasNoCache) {
12090 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012091 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012092 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012093 if (layer != null && layer.isValid()) {
12094 mLayerPaint.setAlpha((int) (alpha * 255));
12095 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12096 layerRendered = true;
12097 } else {
12098 final int scrollX = hasDisplayList ? 0 : sx;
12099 final int scrollY = hasDisplayList ? 0 : sy;
12100 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012101 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012102 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12103 }
12104 }
12105
12106 if (!layerRendered) {
12107 if (!hasDisplayList) {
12108 // Fast path for layouts with no backgrounds
12109 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12110 if (ViewDebug.TRACE_HIERARCHY) {
12111 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12112 }
12113 mPrivateFlags &= ~DIRTY_MASK;
12114 dispatchDraw(canvas);
12115 } else {
12116 draw(canvas);
12117 }
12118 } else {
12119 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012120 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012121 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012122 }
12123 }
12124 } else if (cache != null) {
12125 mPrivateFlags &= ~DIRTY_MASK;
12126 Paint cachePaint;
12127
12128 if (layerType == LAYER_TYPE_NONE) {
12129 cachePaint = parent.mCachePaint;
12130 if (cachePaint == null) {
12131 cachePaint = new Paint();
12132 cachePaint.setDither(false);
12133 parent.mCachePaint = cachePaint;
12134 }
Chet Haase9420abd2012-03-29 16:28:32 -070012135 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012136 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012137 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12138 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012139 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012140 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012141 }
12142 } else {
12143 cachePaint = mLayerPaint;
12144 cachePaint.setAlpha((int) (alpha * 255));
12145 }
12146 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12147 }
12148
Chet Haasea1cff502012-02-21 13:43:44 -080012149 if (restoreTo >= 0) {
12150 canvas.restoreToCount(restoreTo);
12151 }
Chet Haase64a48c12012-02-13 16:33:29 -080012152
12153 if (a != null && !more) {
12154 if (!hardwareAccelerated && !a.getFillAfter()) {
12155 onSetAlpha(255);
12156 }
12157 parent.finishAnimatingView(this, a);
12158 }
12159
12160 if (more && hardwareAccelerated) {
12161 // invalidation is the trigger to recreate display lists, so if we're using
12162 // display lists to render, force an invalidate to allow the animation to
12163 // continue drawing another frame
12164 parent.invalidate(true);
12165 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12166 // alpha animations should cause the child to recreate its display list
12167 invalidate(true);
12168 }
12169 }
12170
12171 mRecreateDisplayList = false;
12172
12173 return more;
12174 }
12175
12176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177 * Manually render this view (and all of its children) to the given Canvas.
12178 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012179 * called. When implementing a view, implement
12180 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12181 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012182 *
12183 * @param canvas The Canvas to which the View is rendered.
12184 */
12185 public void draw(Canvas canvas) {
12186 if (ViewDebug.TRACE_HIERARCHY) {
12187 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12188 }
12189
Romain Guy5bcdff42009-05-14 21:27:18 -070012190 final int privateFlags = mPrivateFlags;
12191 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12192 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12193 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012195 /*
12196 * Draw traversal performs several drawing steps which must be executed
12197 * in the appropriate order:
12198 *
12199 * 1. Draw the background
12200 * 2. If necessary, save the canvas' layers to prepare for fading
12201 * 3. Draw view's content
12202 * 4. Draw children
12203 * 5. If necessary, draw the fading edges and restore layers
12204 * 6. Draw decorations (scrollbars for instance)
12205 */
12206
12207 // Step 1, draw the background, if needed
12208 int saveCount;
12209
Romain Guy24443ea2009-05-11 11:56:30 -070012210 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012211 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012212 if (background != null) {
12213 final int scrollX = mScrollX;
12214 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215
Romain Guy24443ea2009-05-11 11:56:30 -070012216 if (mBackgroundSizeChanged) {
12217 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12218 mBackgroundSizeChanged = false;
12219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012220
Romain Guy24443ea2009-05-11 11:56:30 -070012221 if ((scrollX | scrollY) == 0) {
12222 background.draw(canvas);
12223 } else {
12224 canvas.translate(scrollX, scrollY);
12225 background.draw(canvas);
12226 canvas.translate(-scrollX, -scrollY);
12227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 }
12229 }
12230
12231 // skip step 2 & 5 if possible (common case)
12232 final int viewFlags = mViewFlags;
12233 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12234 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12235 if (!verticalEdges && !horizontalEdges) {
12236 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012237 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012238
12239 // Step 4, draw the children
12240 dispatchDraw(canvas);
12241
12242 // Step 6, draw decorations (scrollbars)
12243 onDrawScrollBars(canvas);
12244
12245 // we're done...
12246 return;
12247 }
12248
12249 /*
12250 * Here we do the full fledged routine...
12251 * (this is an uncommon case where speed matters less,
12252 * this is why we repeat some of the tests that have been
12253 * done above)
12254 */
12255
12256 boolean drawTop = false;
12257 boolean drawBottom = false;
12258 boolean drawLeft = false;
12259 boolean drawRight = false;
12260
12261 float topFadeStrength = 0.0f;
12262 float bottomFadeStrength = 0.0f;
12263 float leftFadeStrength = 0.0f;
12264 float rightFadeStrength = 0.0f;
12265
12266 // Step 2, save the canvas' layers
12267 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012268
12269 final boolean offsetRequired = isPaddingOffsetRequired();
12270 if (offsetRequired) {
12271 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 }
12273
12274 int left = mScrollX + paddingLeft;
12275 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012276 int top = mScrollY + getFadeTop(offsetRequired);
12277 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012278
12279 if (offsetRequired) {
12280 right += getRightPaddingOffset();
12281 bottom += getBottomPaddingOffset();
12282 }
12283
12284 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012285 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012286 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287
12288 // clip the fade length if top and bottom fades overlap
12289 // overlapping fades produce odd-looking artifacts
12290 if (verticalEdges && (top + length > bottom - length)) {
12291 length = (bottom - top) / 2;
12292 }
12293
12294 // also clip horizontal fades if necessary
12295 if (horizontalEdges && (left + length > right - length)) {
12296 length = (right - left) / 2;
12297 }
12298
12299 if (verticalEdges) {
12300 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012301 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012302 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012303 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304 }
12305
12306 if (horizontalEdges) {
12307 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012308 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012309 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012310 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012311 }
12312
12313 saveCount = canvas.getSaveCount();
12314
12315 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012316 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12318
12319 if (drawTop) {
12320 canvas.saveLayer(left, top, right, top + length, null, flags);
12321 }
12322
12323 if (drawBottom) {
12324 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12325 }
12326
12327 if (drawLeft) {
12328 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12329 }
12330
12331 if (drawRight) {
12332 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12333 }
12334 } else {
12335 scrollabilityCache.setFadeColor(solidColor);
12336 }
12337
12338 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012339 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012340
12341 // Step 4, draw the children
12342 dispatchDraw(canvas);
12343
12344 // Step 5, draw the fade effect and restore layers
12345 final Paint p = scrollabilityCache.paint;
12346 final Matrix matrix = scrollabilityCache.matrix;
12347 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012348
12349 if (drawTop) {
12350 matrix.setScale(1, fadeHeight * topFadeStrength);
12351 matrix.postTranslate(left, top);
12352 fade.setLocalMatrix(matrix);
12353 canvas.drawRect(left, top, right, top + length, p);
12354 }
12355
12356 if (drawBottom) {
12357 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12358 matrix.postRotate(180);
12359 matrix.postTranslate(left, bottom);
12360 fade.setLocalMatrix(matrix);
12361 canvas.drawRect(left, bottom - length, right, bottom, p);
12362 }
12363
12364 if (drawLeft) {
12365 matrix.setScale(1, fadeHeight * leftFadeStrength);
12366 matrix.postRotate(-90);
12367 matrix.postTranslate(left, top);
12368 fade.setLocalMatrix(matrix);
12369 canvas.drawRect(left, top, left + length, bottom, p);
12370 }
12371
12372 if (drawRight) {
12373 matrix.setScale(1, fadeHeight * rightFadeStrength);
12374 matrix.postRotate(90);
12375 matrix.postTranslate(right, top);
12376 fade.setLocalMatrix(matrix);
12377 canvas.drawRect(right - length, top, right, bottom, p);
12378 }
12379
12380 canvas.restoreToCount(saveCount);
12381
12382 // Step 6, draw decorations (scrollbars)
12383 onDrawScrollBars(canvas);
12384 }
12385
12386 /**
12387 * Override this if your view is known to always be drawn on top of a solid color background,
12388 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12389 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12390 * should be set to 0xFF.
12391 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012392 * @see #setVerticalFadingEdgeEnabled(boolean)
12393 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012394 *
12395 * @return The known solid color background for this view, or 0 if the color may vary
12396 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012397 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012398 public int getSolidColor() {
12399 return 0;
12400 }
12401
12402 /**
12403 * Build a human readable string representation of the specified view flags.
12404 *
12405 * @param flags the view flags to convert to a string
12406 * @return a String representing the supplied flags
12407 */
12408 private static String printFlags(int flags) {
12409 String output = "";
12410 int numFlags = 0;
12411 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12412 output += "TAKES_FOCUS";
12413 numFlags++;
12414 }
12415
12416 switch (flags & VISIBILITY_MASK) {
12417 case INVISIBLE:
12418 if (numFlags > 0) {
12419 output += " ";
12420 }
12421 output += "INVISIBLE";
12422 // USELESS HERE numFlags++;
12423 break;
12424 case GONE:
12425 if (numFlags > 0) {
12426 output += " ";
12427 }
12428 output += "GONE";
12429 // USELESS HERE numFlags++;
12430 break;
12431 default:
12432 break;
12433 }
12434 return output;
12435 }
12436
12437 /**
12438 * Build a human readable string representation of the specified private
12439 * view flags.
12440 *
12441 * @param privateFlags the private view flags to convert to a string
12442 * @return a String representing the supplied flags
12443 */
12444 private static String printPrivateFlags(int privateFlags) {
12445 String output = "";
12446 int numFlags = 0;
12447
12448 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
12449 output += "WANTS_FOCUS";
12450 numFlags++;
12451 }
12452
12453 if ((privateFlags & FOCUSED) == FOCUSED) {
12454 if (numFlags > 0) {
12455 output += " ";
12456 }
12457 output += "FOCUSED";
12458 numFlags++;
12459 }
12460
12461 if ((privateFlags & SELECTED) == SELECTED) {
12462 if (numFlags > 0) {
12463 output += " ";
12464 }
12465 output += "SELECTED";
12466 numFlags++;
12467 }
12468
12469 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12470 if (numFlags > 0) {
12471 output += " ";
12472 }
12473 output += "IS_ROOT_NAMESPACE";
12474 numFlags++;
12475 }
12476
12477 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12478 if (numFlags > 0) {
12479 output += " ";
12480 }
12481 output += "HAS_BOUNDS";
12482 numFlags++;
12483 }
12484
12485 if ((privateFlags & DRAWN) == DRAWN) {
12486 if (numFlags > 0) {
12487 output += " ";
12488 }
12489 output += "DRAWN";
12490 // USELESS HERE numFlags++;
12491 }
12492 return output;
12493 }
12494
12495 /**
12496 * <p>Indicates whether or not this view's layout will be requested during
12497 * the next hierarchy layout pass.</p>
12498 *
12499 * @return true if the layout will be forced during next layout pass
12500 */
12501 public boolean isLayoutRequested() {
12502 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12503 }
12504
12505 /**
12506 * Assign a size and position to a view and all of its
12507 * descendants
12508 *
12509 * <p>This is the second phase of the layout mechanism.
12510 * (The first is measuring). In this phase, each parent calls
12511 * layout on all of its children to position them.
12512 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012513 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 *
Chet Haase9c087442011-01-12 16:20:16 -080012515 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012516 * Derived classes with children should override
12517 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012518 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519 *
12520 * @param l Left position, relative to parent
12521 * @param t Top position, relative to parent
12522 * @param r Right position, relative to parent
12523 * @param b Bottom position, relative to parent
12524 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012525 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012526 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012527 int oldL = mLeft;
12528 int oldT = mTop;
12529 int oldB = mBottom;
12530 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 boolean changed = setFrame(l, t, r, b);
12532 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12533 if (ViewDebug.TRACE_HIERARCHY) {
12534 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12535 }
12536
12537 onLayout(changed, l, t, r, b);
12538 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012539
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012540 ListenerInfo li = mListenerInfo;
12541 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012542 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012543 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012544 int numListeners = listenersCopy.size();
12545 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012546 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012547 }
12548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012549 }
12550 mPrivateFlags &= ~FORCE_LAYOUT;
12551 }
12552
12553 /**
12554 * Called from layout when this view should
12555 * assign a size and position to each of its children.
12556 *
12557 * Derived classes with children should override
12558 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012559 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 * @param changed This is a new size or position for this view
12561 * @param left Left position, relative to parent
12562 * @param top Top position, relative to parent
12563 * @param right Right position, relative to parent
12564 * @param bottom Bottom position, relative to parent
12565 */
12566 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12567 }
12568
12569 /**
12570 * Assign a size and position to this view.
12571 *
12572 * This is called from layout.
12573 *
12574 * @param left Left position, relative to parent
12575 * @param top Top position, relative to parent
12576 * @param right Right position, relative to parent
12577 * @param bottom Bottom position, relative to parent
12578 * @return true if the new size and position are different than the
12579 * previous ones
12580 * {@hide}
12581 */
12582 protected boolean setFrame(int left, int top, int right, int bottom) {
12583 boolean changed = false;
12584
12585 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012586 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587 + right + "," + bottom + ")");
12588 }
12589
12590 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12591 changed = true;
12592
12593 // Remember our drawn bit
12594 int drawn = mPrivateFlags & DRAWN;
12595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012596 int oldWidth = mRight - mLeft;
12597 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012598 int newWidth = right - left;
12599 int newHeight = bottom - top;
12600 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12601
12602 // Invalidate our old position
12603 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012604
12605 mLeft = left;
12606 mTop = top;
12607 mRight = right;
12608 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012609 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12610 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012612
12613 mPrivateFlags |= HAS_BOUNDS;
12614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012615
Chet Haase75755e22011-07-18 17:48:25 -070012616 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012617 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12618 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012619 if (mTransformationInfo != null) {
12620 mTransformationInfo.mMatrixDirty = true;
12621 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012623 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12624 }
12625
12626 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12627 // If we are visible, force the DRAWN bit to on so that
12628 // this invalidate will go through (at least to our parent).
12629 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012630 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012631 // the DRAWN bit.
12632 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012633 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012634 // parent display list may need to be recreated based on a change in the bounds
12635 // of any child
12636 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012637 }
12638
12639 // Reset drawn bit to original value (invalidate turns it off)
12640 mPrivateFlags |= drawn;
12641
12642 mBackgroundSizeChanged = true;
12643 }
12644 return changed;
12645 }
12646
12647 /**
12648 * Finalize inflating a view from XML. This is called as the last phase
12649 * of inflation, after all child views have been added.
12650 *
12651 * <p>Even if the subclass overrides onFinishInflate, they should always be
12652 * sure to call the super method, so that we get called.
12653 */
12654 protected void onFinishInflate() {
12655 }
12656
12657 /**
12658 * Returns the resources associated with this view.
12659 *
12660 * @return Resources object.
12661 */
12662 public Resources getResources() {
12663 return mResources;
12664 }
12665
12666 /**
12667 * Invalidates the specified Drawable.
12668 *
12669 * @param drawable the drawable to invalidate
12670 */
12671 public void invalidateDrawable(Drawable drawable) {
12672 if (verifyDrawable(drawable)) {
12673 final Rect dirty = drawable.getBounds();
12674 final int scrollX = mScrollX;
12675 final int scrollY = mScrollY;
12676
12677 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12678 dirty.right + scrollX, dirty.bottom + scrollY);
12679 }
12680 }
12681
12682 /**
12683 * Schedules an action on a drawable to occur at a specified time.
12684 *
12685 * @param who the recipient of the action
12686 * @param what the action to run on the drawable
12687 * @param when the time at which the action must occur. Uses the
12688 * {@link SystemClock#uptimeMillis} timebase.
12689 */
12690 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012691 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012692 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012693 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012694 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12695 Choreographer.CALLBACK_ANIMATION, what, who,
12696 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012697 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012698 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700 }
12701 }
12702
12703 /**
12704 * Cancels a scheduled action on a drawable.
12705 *
12706 * @param who the recipient of the action
12707 * @param what the action to cancel
12708 */
12709 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012710 if (verifyDrawable(who) && what != null) {
12711 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012712 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12713 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012714 } else {
12715 ViewRootImpl.getRunQueue().removeCallbacks(what);
12716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 }
12718 }
12719
12720 /**
12721 * Unschedule any events associated with the given Drawable. This can be
12722 * used when selecting a new Drawable into a view, so that the previous
12723 * one is completely unscheduled.
12724 *
12725 * @param who The Drawable to unschedule.
12726 *
12727 * @see #drawableStateChanged
12728 */
12729 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012730 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012731 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12732 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733 }
12734 }
12735
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012736 /**
12737 * Return the layout direction of a given Drawable.
12738 *
12739 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012740 */
12741 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012742 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012743 }
12744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012745 /**
12746 * If your view subclass is displaying its own Drawable objects, it should
12747 * override this function and return true for any Drawable it is
12748 * displaying. This allows animations for those drawables to be
12749 * scheduled.
12750 *
12751 * <p>Be sure to call through to the super class when overriding this
12752 * function.
12753 *
12754 * @param who The Drawable to verify. Return true if it is one you are
12755 * displaying, else return the result of calling through to the
12756 * super class.
12757 *
12758 * @return boolean If true than the Drawable is being displayed in the
12759 * view; else false and it is not allowed to animate.
12760 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012761 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12762 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 */
12764 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012765 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 }
12767
12768 /**
12769 * This function is called whenever the state of the view changes in such
12770 * a way that it impacts the state of drawables being shown.
12771 *
12772 * <p>Be sure to call through to the superclass when overriding this
12773 * function.
12774 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012775 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012776 */
12777 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012778 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012779 if (d != null && d.isStateful()) {
12780 d.setState(getDrawableState());
12781 }
12782 }
12783
12784 /**
12785 * Call this to force a view to update its drawable state. This will cause
12786 * drawableStateChanged to be called on this view. Views that are interested
12787 * in the new state should call getDrawableState.
12788 *
12789 * @see #drawableStateChanged
12790 * @see #getDrawableState
12791 */
12792 public void refreshDrawableState() {
12793 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12794 drawableStateChanged();
12795
12796 ViewParent parent = mParent;
12797 if (parent != null) {
12798 parent.childDrawableStateChanged(this);
12799 }
12800 }
12801
12802 /**
12803 * Return an array of resource IDs of the drawable states representing the
12804 * current state of the view.
12805 *
12806 * @return The current drawable state
12807 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012808 * @see Drawable#setState(int[])
12809 * @see #drawableStateChanged()
12810 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012811 */
12812 public final int[] getDrawableState() {
12813 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12814 return mDrawableState;
12815 } else {
12816 mDrawableState = onCreateDrawableState(0);
12817 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12818 return mDrawableState;
12819 }
12820 }
12821
12822 /**
12823 * Generate the new {@link android.graphics.drawable.Drawable} state for
12824 * this view. This is called by the view
12825 * system when the cached Drawable state is determined to be invalid. To
12826 * retrieve the current state, you should use {@link #getDrawableState}.
12827 *
12828 * @param extraSpace if non-zero, this is the number of extra entries you
12829 * would like in the returned array in which you can place your own
12830 * states.
12831 *
12832 * @return Returns an array holding the current {@link Drawable} state of
12833 * the view.
12834 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012835 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836 */
12837 protected int[] onCreateDrawableState(int extraSpace) {
12838 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12839 mParent instanceof View) {
12840 return ((View) mParent).onCreateDrawableState(extraSpace);
12841 }
12842
12843 int[] drawableState;
12844
12845 int privateFlags = mPrivateFlags;
12846
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012847 int viewStateIndex = 0;
12848 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12849 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12850 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012851 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012852 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12853 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012854 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12855 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012856 // This is set if HW acceleration is requested, even if the current
12857 // process doesn't allow it. This is just to allow app preview
12858 // windows to better match their app.
12859 viewStateIndex |= VIEW_STATE_ACCELERATED;
12860 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012861 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012862
Christopher Tate3d4bf172011-03-28 16:16:46 -070012863 final int privateFlags2 = mPrivateFlags2;
12864 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12865 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012867 drawableState = VIEW_STATE_SETS[viewStateIndex];
12868
12869 //noinspection ConstantIfStatement
12870 if (false) {
12871 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12872 Log.i("View", toString()
12873 + " pressed=" + ((privateFlags & PRESSED) != 0)
12874 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12875 + " fo=" + hasFocus()
12876 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012877 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012878 + ": " + Arrays.toString(drawableState));
12879 }
12880
12881 if (extraSpace == 0) {
12882 return drawableState;
12883 }
12884
12885 final int[] fullState;
12886 if (drawableState != null) {
12887 fullState = new int[drawableState.length + extraSpace];
12888 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12889 } else {
12890 fullState = new int[extraSpace];
12891 }
12892
12893 return fullState;
12894 }
12895
12896 /**
12897 * Merge your own state values in <var>additionalState</var> into the base
12898 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012899 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 *
12901 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012902 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903 * own additional state values.
12904 *
12905 * @param additionalState The additional state values you would like
12906 * added to <var>baseState</var>; this array is not modified.
12907 *
12908 * @return As a convenience, the <var>baseState</var> array you originally
12909 * passed into the function is returned.
12910 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012911 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012912 */
12913 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12914 final int N = baseState.length;
12915 int i = N - 1;
12916 while (i >= 0 && baseState[i] == 0) {
12917 i--;
12918 }
12919 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12920 return baseState;
12921 }
12922
12923 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012924 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12925 * on all Drawable objects associated with this view.
12926 */
12927 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012928 if (mBackground != null) {
12929 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070012930 }
12931 }
12932
12933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012934 * Sets the background color for this view.
12935 * @param color the color of the background
12936 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012937 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012938 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012939 if (mBackground instanceof ColorDrawable) {
12940 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070012941 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070012942 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070012943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012944 }
12945
12946 /**
12947 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012948 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012949 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070012950 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012951 * @attr ref android.R.styleable#View_background
12952 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012953 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012954 public void setBackgroundResource(int resid) {
12955 if (resid != 0 && resid == mBackgroundResource) {
12956 return;
12957 }
12958
12959 Drawable d= null;
12960 if (resid != 0) {
12961 d = mResources.getDrawable(resid);
12962 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012963 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012964
12965 mBackgroundResource = resid;
12966 }
12967
12968 /**
12969 * Set the background to a given Drawable, or remove the background. If the
12970 * background has padding, this View's padding is set to the background's
12971 * padding. However, when a background is removed, this View's padding isn't
12972 * touched. If setting the padding is desired, please use
12973 * {@link #setPadding(int, int, int, int)}.
12974 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012975 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012976 * background
12977 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012978 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070012979 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070012980 setBackgroundDrawable(background);
12981 }
12982
12983 /**
12984 * @deprecated use {@link #setBackground(Drawable)} instead
12985 */
12986 @Deprecated
12987 public void setBackgroundDrawable(Drawable background) {
12988 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012989 return;
12990 }
12991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012992 boolean requestLayout = false;
12993
12994 mBackgroundResource = 0;
12995
12996 /*
12997 * Regardless of whether we're setting a new background or not, we want
12998 * to clear the previous drawable.
12999 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013000 if (mBackground != null) {
13001 mBackground.setCallback(null);
13002 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013003 }
13004
Philip Milne6c8ea062012-04-03 17:38:43 -070013005 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013006 Rect padding = sThreadLocal.get();
13007 if (padding == null) {
13008 padding = new Rect();
13009 sThreadLocal.set(padding);
13010 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013011 if (background.getPadding(padding)) {
13012 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013013 case LAYOUT_DIRECTION_RTL:
13014 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13015 break;
13016 case LAYOUT_DIRECTION_LTR:
13017 default:
13018 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013020 }
13021
13022 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13023 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013024 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13025 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013026 requestLayout = true;
13027 }
13028
Philip Milne6c8ea062012-04-03 17:38:43 -070013029 background.setCallback(this);
13030 if (background.isStateful()) {
13031 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013032 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013033 background.setVisible(getVisibility() == VISIBLE, false);
13034 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013035
13036 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13037 mPrivateFlags &= ~SKIP_DRAW;
13038 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13039 requestLayout = true;
13040 }
13041 } else {
13042 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013043 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013044
13045 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13046 /*
13047 * This view ONLY drew the background before and we're removing
13048 * the background, so now it won't draw anything
13049 * (hence we SKIP_DRAW)
13050 */
13051 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13052 mPrivateFlags |= SKIP_DRAW;
13053 }
13054
13055 /*
13056 * When the background is set, we try to apply its padding to this
13057 * View. When the background is removed, we don't touch this View's
13058 * padding. This is noted in the Javadocs. Hence, we don't need to
13059 * requestLayout(), the invalidate() below is sufficient.
13060 */
13061
13062 // The old background's minimum size could have affected this
13063 // View's layout, so let's requestLayout
13064 requestLayout = true;
13065 }
13066
Romain Guy8f1344f52009-05-15 16:03:59 -070013067 computeOpaqueFlags();
13068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013069 if (requestLayout) {
13070 requestLayout();
13071 }
13072
13073 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013074 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013075 }
13076
13077 /**
13078 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013079 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013080 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013081 *
13082 * @see #setBackground(Drawable)
13083 *
13084 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013085 */
13086 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013087 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013088 }
13089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013090 /**
13091 * Sets the padding. The view may add on the space required to display
13092 * the scrollbars, depending on the style and visibility of the scrollbars.
13093 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13094 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13095 * from the values set in this call.
13096 *
13097 * @attr ref android.R.styleable#View_padding
13098 * @attr ref android.R.styleable#View_paddingBottom
13099 * @attr ref android.R.styleable#View_paddingLeft
13100 * @attr ref android.R.styleable#View_paddingRight
13101 * @attr ref android.R.styleable#View_paddingTop
13102 * @param left the left padding in pixels
13103 * @param top the top padding in pixels
13104 * @param right the right padding in pixels
13105 * @param bottom the bottom padding in pixels
13106 */
13107 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013108 mUserPaddingStart = -1;
13109 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013110 mUserPaddingRelative = false;
13111
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013112 internalSetPadding(left, top, right, bottom);
13113 }
13114
13115 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013116 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013117 mUserPaddingRight = right;
13118 mUserPaddingBottom = bottom;
13119
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013120 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013121 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013122
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013123 // Common case is there are no scroll bars.
13124 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013125 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013126 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013127 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013128 switch (mVerticalScrollbarPosition) {
13129 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013130 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13131 left += offset;
13132 } else {
13133 right += offset;
13134 }
13135 break;
Adam Powell20232d02010-12-08 21:08:53 -080013136 case SCROLLBAR_POSITION_RIGHT:
13137 right += offset;
13138 break;
13139 case SCROLLBAR_POSITION_LEFT:
13140 left += offset;
13141 break;
13142 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013143 }
Adam Powell20232d02010-12-08 21:08:53 -080013144 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013145 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13146 ? 0 : getHorizontalScrollbarHeight();
13147 }
13148 }
Romain Guy8506ab42009-06-11 17:35:47 -070013149
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013150 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013151 changed = true;
13152 mPaddingLeft = left;
13153 }
13154 if (mPaddingTop != top) {
13155 changed = true;
13156 mPaddingTop = top;
13157 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013158 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013159 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013160 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013161 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013162 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013163 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013164 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013165 }
13166
13167 if (changed) {
13168 requestLayout();
13169 }
13170 }
13171
13172 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013173 * Sets the relative padding. The view may add on the space required to display
13174 * the scrollbars, depending on the style and visibility of the scrollbars.
13175 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13176 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13177 * from the values set in this call.
13178 *
13179 * @attr ref android.R.styleable#View_padding
13180 * @attr ref android.R.styleable#View_paddingBottom
13181 * @attr ref android.R.styleable#View_paddingStart
13182 * @attr ref android.R.styleable#View_paddingEnd
13183 * @attr ref android.R.styleable#View_paddingTop
13184 * @param start the start padding in pixels
13185 * @param top the top padding in pixels
13186 * @param end the end padding in pixels
13187 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013188 */
13189 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013190 mUserPaddingStart = start;
13191 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013192 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013193
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013194 switch(getResolvedLayoutDirection()) {
13195 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013196 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013197 break;
13198 case LAYOUT_DIRECTION_LTR:
13199 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013200 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013201 }
13202 }
13203
13204 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013205 * Returns the top padding of this view.
13206 *
13207 * @return the top padding in pixels
13208 */
13209 public int getPaddingTop() {
13210 return mPaddingTop;
13211 }
13212
13213 /**
13214 * Returns the bottom padding of this view. If there are inset and enabled
13215 * scrollbars, this value may include the space required to display the
13216 * scrollbars as well.
13217 *
13218 * @return the bottom padding in pixels
13219 */
13220 public int getPaddingBottom() {
13221 return mPaddingBottom;
13222 }
13223
13224 /**
13225 * Returns the left padding of this view. If there are inset and enabled
13226 * scrollbars, this value may include the space required to display the
13227 * scrollbars as well.
13228 *
13229 * @return the left padding in pixels
13230 */
13231 public int getPaddingLeft() {
13232 return mPaddingLeft;
13233 }
13234
13235 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013236 * Returns the start padding of this view depending on its resolved layout direction.
13237 * If there are inset and enabled scrollbars, this value may include the space
13238 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013239 *
13240 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013241 */
13242 public int getPaddingStart() {
13243 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13244 mPaddingRight : mPaddingLeft;
13245 }
13246
13247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013248 * Returns the right padding of this view. If there are inset and enabled
13249 * scrollbars, this value may include the space required to display the
13250 * scrollbars as well.
13251 *
13252 * @return the right padding in pixels
13253 */
13254 public int getPaddingRight() {
13255 return mPaddingRight;
13256 }
13257
13258 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013259 * Returns the end padding of this view depending on its resolved layout direction.
13260 * If there are inset and enabled scrollbars, this value may include the space
13261 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013262 *
13263 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013264 */
13265 public int getPaddingEnd() {
13266 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13267 mPaddingLeft : mPaddingRight;
13268 }
13269
13270 /**
13271 * Return if the padding as been set thru relative values
13272 * {@link #setPaddingRelative(int, int, int, int)} or thru
13273 * @attr ref android.R.styleable#View_paddingStart or
13274 * @attr ref android.R.styleable#View_paddingEnd
13275 *
13276 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013277 */
13278 public boolean isPaddingRelative() {
13279 return mUserPaddingRelative;
13280 }
13281
13282 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013283 * @hide
13284 */
13285 public Insets getLayoutInsets() {
13286 if (mLayoutInsets == null) {
13287 if (mBackground == null) {
13288 mLayoutInsets = Insets.NONE;
13289 } else {
13290 Rect insetRect = new Rect();
13291 boolean hasInsets = mBackground.getLayoutInsets(insetRect);
13292 mLayoutInsets = hasInsets ? Insets.of(insetRect) : Insets.NONE;
13293 }
13294 }
13295 return mLayoutInsets;
13296 }
13297
13298 /**
13299 * @hide
13300 */
13301 public void setLayoutInsets(Insets layoutInsets) {
13302 mLayoutInsets = layoutInsets;
13303 }
13304
13305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013306 * Changes the selection state of this view. A view can be selected or not.
13307 * Note that selection is not the same as focus. Views are typically
13308 * selected in the context of an AdapterView like ListView or GridView;
13309 * the selected view is the view that is highlighted.
13310 *
13311 * @param selected true if the view must be selected, false otherwise
13312 */
13313 public void setSelected(boolean selected) {
13314 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13315 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013316 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013317 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318 refreshDrawableState();
13319 dispatchSetSelected(selected);
13320 }
13321 }
13322
13323 /**
13324 * Dispatch setSelected to all of this View's children.
13325 *
13326 * @see #setSelected(boolean)
13327 *
13328 * @param selected The new selected state
13329 */
13330 protected void dispatchSetSelected(boolean selected) {
13331 }
13332
13333 /**
13334 * Indicates the selection state of this view.
13335 *
13336 * @return true if the view is selected, false otherwise
13337 */
13338 @ViewDebug.ExportedProperty
13339 public boolean isSelected() {
13340 return (mPrivateFlags & SELECTED) != 0;
13341 }
13342
13343 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013344 * Changes the activated state of this view. A view can be activated or not.
13345 * Note that activation is not the same as selection. Selection is
13346 * a transient property, representing the view (hierarchy) the user is
13347 * currently interacting with. Activation is a longer-term state that the
13348 * user can move views in and out of. For example, in a list view with
13349 * single or multiple selection enabled, the views in the current selection
13350 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13351 * here.) The activated state is propagated down to children of the view it
13352 * is set on.
13353 *
13354 * @param activated true if the view must be activated, false otherwise
13355 */
13356 public void setActivated(boolean activated) {
13357 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13358 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013359 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013360 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013361 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013362 }
13363 }
13364
13365 /**
13366 * Dispatch setActivated to all of this View's children.
13367 *
13368 * @see #setActivated(boolean)
13369 *
13370 * @param activated The new activated state
13371 */
13372 protected void dispatchSetActivated(boolean activated) {
13373 }
13374
13375 /**
13376 * Indicates the activation state of this view.
13377 *
13378 * @return true if the view is activated, false otherwise
13379 */
13380 @ViewDebug.ExportedProperty
13381 public boolean isActivated() {
13382 return (mPrivateFlags & ACTIVATED) != 0;
13383 }
13384
13385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013386 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13387 * observer can be used to get notifications when global events, like
13388 * layout, happen.
13389 *
13390 * The returned ViewTreeObserver observer is not guaranteed to remain
13391 * valid for the lifetime of this View. If the caller of this method keeps
13392 * a long-lived reference to ViewTreeObserver, it should always check for
13393 * the return value of {@link ViewTreeObserver#isAlive()}.
13394 *
13395 * @return The ViewTreeObserver for this view's hierarchy.
13396 */
13397 public ViewTreeObserver getViewTreeObserver() {
13398 if (mAttachInfo != null) {
13399 return mAttachInfo.mTreeObserver;
13400 }
13401 if (mFloatingTreeObserver == null) {
13402 mFloatingTreeObserver = new ViewTreeObserver();
13403 }
13404 return mFloatingTreeObserver;
13405 }
13406
13407 /**
13408 * <p>Finds the topmost view in the current view hierarchy.</p>
13409 *
13410 * @return the topmost view containing this view
13411 */
13412 public View getRootView() {
13413 if (mAttachInfo != null) {
13414 final View v = mAttachInfo.mRootView;
13415 if (v != null) {
13416 return v;
13417 }
13418 }
Romain Guy8506ab42009-06-11 17:35:47 -070013419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013420 View parent = this;
13421
13422 while (parent.mParent != null && parent.mParent instanceof View) {
13423 parent = (View) parent.mParent;
13424 }
13425
13426 return parent;
13427 }
13428
13429 /**
13430 * <p>Computes the coordinates of this view on the screen. The argument
13431 * must be an array of two integers. After the method returns, the array
13432 * contains the x and y location in that order.</p>
13433 *
13434 * @param location an array of two integers in which to hold the coordinates
13435 */
13436 public void getLocationOnScreen(int[] location) {
13437 getLocationInWindow(location);
13438
13439 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013440 if (info != null) {
13441 location[0] += info.mWindowLeft;
13442 location[1] += info.mWindowTop;
13443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 }
13445
13446 /**
13447 * <p>Computes the coordinates of this view in its window. The argument
13448 * must be an array of two integers. After the method returns, the array
13449 * contains the x and y location in that order.</p>
13450 *
13451 * @param location an array of two integers in which to hold the coordinates
13452 */
13453 public void getLocationInWindow(int[] location) {
13454 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013455 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013456 }
13457
Gilles Debunne6583ce52011-12-06 18:09:02 -080013458 if (mAttachInfo == null) {
13459 // When the view is not attached to a window, this method does not make sense
13460 location[0] = location[1] = 0;
13461 return;
13462 }
13463
Gilles Debunnecea45132011-11-24 02:19:27 +010013464 float[] position = mAttachInfo.mTmpTransformLocation;
13465 position[0] = position[1] = 0.0f;
13466
13467 if (!hasIdentityMatrix()) {
13468 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013470
Gilles Debunnecea45132011-11-24 02:19:27 +010013471 position[0] += mLeft;
13472 position[1] += mTop;
13473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013474 ViewParent viewParent = mParent;
13475 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013476 final View view = (View) viewParent;
13477
13478 position[0] -= view.mScrollX;
13479 position[1] -= view.mScrollY;
13480
13481 if (!view.hasIdentityMatrix()) {
13482 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013483 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013484
13485 position[0] += view.mLeft;
13486 position[1] += view.mTop;
13487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488 viewParent = view.mParent;
13489 }
Romain Guy8506ab42009-06-11 17:35:47 -070013490
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013491 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013492 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010013493 final ViewRootImpl vr = (ViewRootImpl) viewParent;
13494 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013495 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013496
13497 location[0] = (int) (position[0] + 0.5f);
13498 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013499 }
13500
13501 /**
13502 * {@hide}
13503 * @param id the id of the view to be found
13504 * @return the view of the specified id, null if cannot be found
13505 */
13506 protected View findViewTraversal(int id) {
13507 if (id == mID) {
13508 return this;
13509 }
13510 return null;
13511 }
13512
13513 /**
13514 * {@hide}
13515 * @param tag the tag of the view to be found
13516 * @return the view of specified tag, null if cannot be found
13517 */
13518 protected View findViewWithTagTraversal(Object tag) {
13519 if (tag != null && tag.equals(mTag)) {
13520 return this;
13521 }
13522 return null;
13523 }
13524
13525 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013526 * {@hide}
13527 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013528 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013529 * @return The first view that matches the predicate or null.
13530 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013531 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013532 if (predicate.apply(this)) {
13533 return this;
13534 }
13535 return null;
13536 }
13537
13538 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013539 * Look for a child view with the given id. If this view has the given
13540 * id, return this view.
13541 *
13542 * @param id The id to search for.
13543 * @return The view that has the given id in the hierarchy or null
13544 */
13545 public final View findViewById(int id) {
13546 if (id < 0) {
13547 return null;
13548 }
13549 return findViewTraversal(id);
13550 }
13551
13552 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013553 * Finds a view by its unuque and stable accessibility id.
13554 *
13555 * @param accessibilityId The searched accessibility id.
13556 * @return The found view.
13557 */
13558 final View findViewByAccessibilityId(int accessibilityId) {
13559 if (accessibilityId < 0) {
13560 return null;
13561 }
13562 return findViewByAccessibilityIdTraversal(accessibilityId);
13563 }
13564
13565 /**
13566 * Performs the traversal to find a view by its unuque and stable accessibility id.
13567 *
13568 * <strong>Note:</strong>This method does not stop at the root namespace
13569 * boundary since the user can touch the screen at an arbitrary location
13570 * potentially crossing the root namespace bounday which will send an
13571 * accessibility event to accessibility services and they should be able
13572 * to obtain the event source. Also accessibility ids are guaranteed to be
13573 * unique in the window.
13574 *
13575 * @param accessibilityId The accessibility id.
13576 * @return The found view.
13577 */
13578 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13579 if (getAccessibilityViewId() == accessibilityId) {
13580 return this;
13581 }
13582 return null;
13583 }
13584
13585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 * Look for a child view with the given tag. If this view has the given
13587 * tag, return this view.
13588 *
13589 * @param tag The tag to search for, using "tag.equals(getTag())".
13590 * @return The View that has the given tag in the hierarchy or null
13591 */
13592 public final View findViewWithTag(Object tag) {
13593 if (tag == null) {
13594 return null;
13595 }
13596 return findViewWithTagTraversal(tag);
13597 }
13598
13599 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013600 * {@hide}
13601 * Look for a child view that matches the specified predicate.
13602 * If this view matches the predicate, return this view.
13603 *
13604 * @param predicate The predicate to evaluate.
13605 * @return The first view that matches the predicate or null.
13606 */
13607 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013608 return findViewByPredicateTraversal(predicate, null);
13609 }
13610
13611 /**
13612 * {@hide}
13613 * Look for a child view that matches the specified predicate,
13614 * starting with the specified view and its descendents and then
13615 * recusively searching the ancestors and siblings of that view
13616 * until this view is reached.
13617 *
13618 * This method is useful in cases where the predicate does not match
13619 * a single unique view (perhaps multiple views use the same id)
13620 * and we are trying to find the view that is "closest" in scope to the
13621 * starting view.
13622 *
13623 * @param start The view to start from.
13624 * @param predicate The predicate to evaluate.
13625 * @return The first view that matches the predicate or null.
13626 */
13627 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13628 View childToSkip = null;
13629 for (;;) {
13630 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13631 if (view != null || start == this) {
13632 return view;
13633 }
13634
13635 ViewParent parent = start.getParent();
13636 if (parent == null || !(parent instanceof View)) {
13637 return null;
13638 }
13639
13640 childToSkip = start;
13641 start = (View) parent;
13642 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013643 }
13644
13645 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013646 * Sets the identifier for this view. The identifier does not have to be
13647 * unique in this view's hierarchy. The identifier should be a positive
13648 * number.
13649 *
13650 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070013651 * @see #getId()
13652 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013653 *
13654 * @param id a number used to identify the view
13655 *
13656 * @attr ref android.R.styleable#View_id
13657 */
13658 public void setId(int id) {
13659 mID = id;
13660 }
13661
13662 /**
13663 * {@hide}
13664 *
13665 * @param isRoot true if the view belongs to the root namespace, false
13666 * otherwise
13667 */
13668 public void setIsRootNamespace(boolean isRoot) {
13669 if (isRoot) {
13670 mPrivateFlags |= IS_ROOT_NAMESPACE;
13671 } else {
13672 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13673 }
13674 }
13675
13676 /**
13677 * {@hide}
13678 *
13679 * @return true if the view belongs to the root namespace, false otherwise
13680 */
13681 public boolean isRootNamespace() {
13682 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13683 }
13684
13685 /**
13686 * Returns this view's identifier.
13687 *
13688 * @return a positive integer used to identify the view or {@link #NO_ID}
13689 * if the view has no ID
13690 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013691 * @see #setId(int)
13692 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013693 * @attr ref android.R.styleable#View_id
13694 */
13695 @ViewDebug.CapturedViewProperty
13696 public int getId() {
13697 return mID;
13698 }
13699
13700 /**
13701 * Returns this view's tag.
13702 *
13703 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013704 *
13705 * @see #setTag(Object)
13706 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013707 */
13708 @ViewDebug.ExportedProperty
13709 public Object getTag() {
13710 return mTag;
13711 }
13712
13713 /**
13714 * Sets the tag associated with this view. A tag can be used to mark
13715 * a view in its hierarchy and does not have to be unique within the
13716 * hierarchy. Tags can also be used to store data within a view without
13717 * resorting to another data structure.
13718 *
13719 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013720 *
13721 * @see #getTag()
13722 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013723 */
13724 public void setTag(final Object tag) {
13725 mTag = tag;
13726 }
13727
13728 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013729 * Returns the tag associated with this view and the specified key.
13730 *
13731 * @param key The key identifying the tag
13732 *
13733 * @return the Object stored in this view as a tag
13734 *
13735 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013736 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013737 */
13738 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013739 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013740 return null;
13741 }
13742
13743 /**
13744 * Sets a tag associated with this view and a key. A tag can be used
13745 * to mark a view in its hierarchy and does not have to be unique within
13746 * the hierarchy. Tags can also be used to store data within a view
13747 * without resorting to another data structure.
13748 *
13749 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013750 * application to ensure it is unique (see the <a
13751 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13752 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013753 * the Android framework or not associated with any package will cause
13754 * an {@link IllegalArgumentException} to be thrown.
13755 *
13756 * @param key The key identifying the tag
13757 * @param tag An Object to tag the view with
13758 *
13759 * @throws IllegalArgumentException If they specified key is not valid
13760 *
13761 * @see #setTag(Object)
13762 * @see #getTag(int)
13763 */
13764 public void setTag(int key, final Object tag) {
13765 // If the package id is 0x00 or 0x01, it's either an undefined package
13766 // or a framework id
13767 if ((key >>> 24) < 2) {
13768 throw new IllegalArgumentException("The key must be an application-specific "
13769 + "resource id.");
13770 }
13771
Adam Powell2b2f6d62011-09-23 11:15:39 -070013772 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013773 }
13774
13775 /**
13776 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13777 * framework id.
13778 *
13779 * @hide
13780 */
13781 public void setTagInternal(int key, Object tag) {
13782 if ((key >>> 24) != 0x1) {
13783 throw new IllegalArgumentException("The key must be a framework-specific "
13784 + "resource id.");
13785 }
13786
Adam Powell2b2f6d62011-09-23 11:15:39 -070013787 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013788 }
13789
Adam Powell2b2f6d62011-09-23 11:15:39 -070013790 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013791 if (mKeyedTags == null) {
13792 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013793 }
13794
Adam Powell7db82ac2011-09-22 19:44:04 -070013795 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013796 }
13797
13798 /**
Romain Guy13922e02009-05-12 17:56:14 -070013799 * @param consistency The type of consistency. See ViewDebug for more information.
13800 *
13801 * @hide
13802 */
13803 protected boolean dispatchConsistencyCheck(int consistency) {
13804 return onConsistencyCheck(consistency);
13805 }
13806
13807 /**
13808 * Method that subclasses should implement to check their consistency. The type of
13809 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013810 *
Romain Guy13922e02009-05-12 17:56:14 -070013811 * @param consistency The type of consistency. See ViewDebug for more information.
13812 *
13813 * @throws IllegalStateException if the view is in an inconsistent state.
13814 *
13815 * @hide
13816 */
13817 protected boolean onConsistencyCheck(int consistency) {
13818 boolean result = true;
13819
13820 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13821 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13822
13823 if (checkLayout) {
13824 if (getParent() == null) {
13825 result = false;
13826 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13827 "View " + this + " does not have a parent.");
13828 }
13829
13830 if (mAttachInfo == null) {
13831 result = false;
13832 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13833 "View " + this + " is not attached to a window.");
13834 }
13835 }
13836
13837 if (checkDrawing) {
13838 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13839 // from their draw() method
13840
13841 if ((mPrivateFlags & DRAWN) != DRAWN &&
13842 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13843 result = false;
13844 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13845 "View " + this + " was invalidated but its drawing cache is valid.");
13846 }
13847 }
13848
13849 return result;
13850 }
13851
13852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013853 * Prints information about this view in the log output, with the tag
13854 * {@link #VIEW_LOG_TAG}.
13855 *
13856 * @hide
13857 */
13858 public void debug() {
13859 debug(0);
13860 }
13861
13862 /**
13863 * Prints information about this view in the log output, with the tag
13864 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13865 * indentation defined by the <code>depth</code>.
13866 *
13867 * @param depth the indentation level
13868 *
13869 * @hide
13870 */
13871 protected void debug(int depth) {
13872 String output = debugIndent(depth - 1);
13873
13874 output += "+ " + this;
13875 int id = getId();
13876 if (id != -1) {
13877 output += " (id=" + id + ")";
13878 }
13879 Object tag = getTag();
13880 if (tag != null) {
13881 output += " (tag=" + tag + ")";
13882 }
13883 Log.d(VIEW_LOG_TAG, output);
13884
13885 if ((mPrivateFlags & FOCUSED) != 0) {
13886 output = debugIndent(depth) + " FOCUSED";
13887 Log.d(VIEW_LOG_TAG, output);
13888 }
13889
13890 output = debugIndent(depth);
13891 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13892 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13893 + "} ";
13894 Log.d(VIEW_LOG_TAG, output);
13895
13896 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13897 || mPaddingBottom != 0) {
13898 output = debugIndent(depth);
13899 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13900 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13901 Log.d(VIEW_LOG_TAG, output);
13902 }
13903
13904 output = debugIndent(depth);
13905 output += "mMeasureWidth=" + mMeasuredWidth +
13906 " mMeasureHeight=" + mMeasuredHeight;
13907 Log.d(VIEW_LOG_TAG, output);
13908
13909 output = debugIndent(depth);
13910 if (mLayoutParams == null) {
13911 output += "BAD! no layout params";
13912 } else {
13913 output = mLayoutParams.debug(output);
13914 }
13915 Log.d(VIEW_LOG_TAG, output);
13916
13917 output = debugIndent(depth);
13918 output += "flags={";
13919 output += View.printFlags(mViewFlags);
13920 output += "}";
13921 Log.d(VIEW_LOG_TAG, output);
13922
13923 output = debugIndent(depth);
13924 output += "privateFlags={";
13925 output += View.printPrivateFlags(mPrivateFlags);
13926 output += "}";
13927 Log.d(VIEW_LOG_TAG, output);
13928 }
13929
13930 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013931 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013932 *
13933 * @param depth the indentation level
13934 * @return a String containing (depth * 2 + 3) * 2 white spaces
13935 *
13936 * @hide
13937 */
13938 protected static String debugIndent(int depth) {
13939 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13940 for (int i = 0; i < (depth * 2) + 3; i++) {
13941 spaces.append(' ').append(' ');
13942 }
13943 return spaces.toString();
13944 }
13945
13946 /**
13947 * <p>Return the offset of the widget's text baseline from the widget's top
13948 * boundary. If this widget does not support baseline alignment, this
13949 * method returns -1. </p>
13950 *
13951 * @return the offset of the baseline within the widget's bounds or -1
13952 * if baseline alignment is not supported
13953 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013954 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955 public int getBaseline() {
13956 return -1;
13957 }
13958
13959 /**
13960 * Call this when something has changed which has invalidated the
13961 * layout of this view. This will schedule a layout pass of the view
13962 * tree.
13963 */
13964 public void requestLayout() {
13965 if (ViewDebug.TRACE_HIERARCHY) {
13966 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13967 }
13968
13969 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013970 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013971
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070013972 if (mLayoutParams != null) {
13973 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
13974 }
13975
13976 if (mParent != null && !mParent.isLayoutRequested()) {
13977 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013978 }
13979 }
13980
13981 /**
13982 * Forces this view to be laid out during the next layout pass.
13983 * This method does not call requestLayout() or forceLayout()
13984 * on the parent.
13985 */
13986 public void forceLayout() {
13987 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013988 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 }
13990
13991 /**
13992 * <p>
13993 * This is called to find out how big a view should be. The parent
13994 * supplies constraint information in the width and height parameters.
13995 * </p>
13996 *
13997 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013998 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014000 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014001 * </p>
14002 *
14003 *
14004 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14005 * parent
14006 * @param heightMeasureSpec Vertical space requirements as imposed by the
14007 * parent
14008 *
14009 * @see #onMeasure(int, int)
14010 */
14011 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14012 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14013 widthMeasureSpec != mOldWidthMeasureSpec ||
14014 heightMeasureSpec != mOldHeightMeasureSpec) {
14015
14016 // first clears the measured dimension flag
14017 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14018
14019 if (ViewDebug.TRACE_HIERARCHY) {
14020 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14021 }
14022
14023 // measure ourselves, this should set the measured dimension flag back
14024 onMeasure(widthMeasureSpec, heightMeasureSpec);
14025
14026 // flag not set, setMeasuredDimension() was not invoked, we raise
14027 // an exception to warn the developer
14028 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14029 throw new IllegalStateException("onMeasure() did not set the"
14030 + " measured dimension by calling"
14031 + " setMeasuredDimension()");
14032 }
14033
14034 mPrivateFlags |= LAYOUT_REQUIRED;
14035 }
14036
14037 mOldWidthMeasureSpec = widthMeasureSpec;
14038 mOldHeightMeasureSpec = heightMeasureSpec;
14039 }
14040
14041 /**
14042 * <p>
14043 * Measure the view and its content to determine the measured width and the
14044 * measured height. This method is invoked by {@link #measure(int, int)} and
14045 * should be overriden by subclasses to provide accurate and efficient
14046 * measurement of their contents.
14047 * </p>
14048 *
14049 * <p>
14050 * <strong>CONTRACT:</strong> When overriding this method, you
14051 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14052 * measured width and height of this view. Failure to do so will trigger an
14053 * <code>IllegalStateException</code>, thrown by
14054 * {@link #measure(int, int)}. Calling the superclass'
14055 * {@link #onMeasure(int, int)} is a valid use.
14056 * </p>
14057 *
14058 * <p>
14059 * The base class implementation of measure defaults to the background size,
14060 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14061 * override {@link #onMeasure(int, int)} to provide better measurements of
14062 * their content.
14063 * </p>
14064 *
14065 * <p>
14066 * If this method is overridden, it is the subclass's responsibility to make
14067 * sure the measured height and width are at least the view's minimum height
14068 * and width ({@link #getSuggestedMinimumHeight()} and
14069 * {@link #getSuggestedMinimumWidth()}).
14070 * </p>
14071 *
14072 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14073 * The requirements are encoded with
14074 * {@link android.view.View.MeasureSpec}.
14075 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14076 * The requirements are encoded with
14077 * {@link android.view.View.MeasureSpec}.
14078 *
14079 * @see #getMeasuredWidth()
14080 * @see #getMeasuredHeight()
14081 * @see #setMeasuredDimension(int, int)
14082 * @see #getSuggestedMinimumHeight()
14083 * @see #getSuggestedMinimumWidth()
14084 * @see android.view.View.MeasureSpec#getMode(int)
14085 * @see android.view.View.MeasureSpec#getSize(int)
14086 */
14087 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14088 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14089 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14090 }
14091
14092 /**
14093 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14094 * measured width and measured height. Failing to do so will trigger an
14095 * exception at measurement time.</p>
14096 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014097 * @param measuredWidth The measured width of this view. May be a complex
14098 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14099 * {@link #MEASURED_STATE_TOO_SMALL}.
14100 * @param measuredHeight The measured height of this view. May be a complex
14101 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14102 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103 */
14104 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14105 mMeasuredWidth = measuredWidth;
14106 mMeasuredHeight = measuredHeight;
14107
14108 mPrivateFlags |= MEASURED_DIMENSION_SET;
14109 }
14110
14111 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014112 * Merge two states as returned by {@link #getMeasuredState()}.
14113 * @param curState The current state as returned from a view or the result
14114 * of combining multiple views.
14115 * @param newState The new view state to combine.
14116 * @return Returns a new integer reflecting the combination of the two
14117 * states.
14118 */
14119 public static int combineMeasuredStates(int curState, int newState) {
14120 return curState | newState;
14121 }
14122
14123 /**
14124 * Version of {@link #resolveSizeAndState(int, int, int)}
14125 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14126 */
14127 public static int resolveSize(int size, int measureSpec) {
14128 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14129 }
14130
14131 /**
14132 * Utility to reconcile a desired size and state, with constraints imposed
14133 * by a MeasureSpec. Will take the desired size, unless a different size
14134 * is imposed by the constraints. The returned value is a compound integer,
14135 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14136 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14137 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014138 *
14139 * @param size How big the view wants to be
14140 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014141 * @return Size information bit mask as defined by
14142 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014143 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014144 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014145 int result = size;
14146 int specMode = MeasureSpec.getMode(measureSpec);
14147 int specSize = MeasureSpec.getSize(measureSpec);
14148 switch (specMode) {
14149 case MeasureSpec.UNSPECIFIED:
14150 result = size;
14151 break;
14152 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014153 if (specSize < size) {
14154 result = specSize | MEASURED_STATE_TOO_SMALL;
14155 } else {
14156 result = size;
14157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158 break;
14159 case MeasureSpec.EXACTLY:
14160 result = specSize;
14161 break;
14162 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014163 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014164 }
14165
14166 /**
14167 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014168 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014169 * by the MeasureSpec.
14170 *
14171 * @param size Default size for this view
14172 * @param measureSpec Constraints imposed by the parent
14173 * @return The size this view should be.
14174 */
14175 public static int getDefaultSize(int size, int measureSpec) {
14176 int result = size;
14177 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014178 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179
14180 switch (specMode) {
14181 case MeasureSpec.UNSPECIFIED:
14182 result = size;
14183 break;
14184 case MeasureSpec.AT_MOST:
14185 case MeasureSpec.EXACTLY:
14186 result = specSize;
14187 break;
14188 }
14189 return result;
14190 }
14191
14192 /**
14193 * Returns the suggested minimum height that the view should use. This
14194 * returns the maximum of the view's minimum height
14195 * and the background's minimum height
14196 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14197 * <p>
14198 * When being used in {@link #onMeasure(int, int)}, the caller should still
14199 * ensure the returned height is within the requirements of the parent.
14200 *
14201 * @return The suggested minimum height of the view.
14202 */
14203 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014204 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206 }
14207
14208 /**
14209 * Returns the suggested minimum width that the view should use. This
14210 * returns the maximum of the view's minimum width)
14211 * and the background's minimum width
14212 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14213 * <p>
14214 * When being used in {@link #onMeasure(int, int)}, the caller should still
14215 * ensure the returned width is within the requirements of the parent.
14216 *
14217 * @return The suggested minimum width of the view.
14218 */
14219 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014220 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014222
Philip Milne6c8ea062012-04-03 17:38:43 -070014223 /**
14224 * Returns the minimum height of the view.
14225 *
14226 * @return the minimum height the view will try to be.
14227 *
14228 * @see #setMinimumHeight(int)
14229 *
14230 * @attr ref android.R.styleable#View_minHeight
14231 */
14232 public int getMinimumHeight() {
14233 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014234 }
14235
14236 /**
14237 * Sets the minimum height of the view. It is not guaranteed the view will
14238 * be able to achieve this minimum height (for example, if its parent layout
14239 * constrains it with less available height).
14240 *
14241 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014242 *
14243 * @see #getMinimumHeight()
14244 *
14245 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 */
14247 public void setMinimumHeight(int minHeight) {
14248 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014249 requestLayout();
14250 }
14251
14252 /**
14253 * Returns the minimum width of the view.
14254 *
14255 * @return the minimum width the view will try to be.
14256 *
14257 * @see #setMinimumWidth(int)
14258 *
14259 * @attr ref android.R.styleable#View_minWidth
14260 */
14261 public int getMinimumWidth() {
14262 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014263 }
14264
14265 /**
14266 * Sets the minimum width of the view. It is not guaranteed the view will
14267 * be able to achieve this minimum width (for example, if its parent layout
14268 * constrains it with less available width).
14269 *
14270 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014271 *
14272 * @see #getMinimumWidth()
14273 *
14274 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014275 */
14276 public void setMinimumWidth(int minWidth) {
14277 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014278 requestLayout();
14279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014280 }
14281
14282 /**
14283 * Get the animation currently associated with this view.
14284 *
14285 * @return The animation that is currently playing or
14286 * scheduled to play for this view.
14287 */
14288 public Animation getAnimation() {
14289 return mCurrentAnimation;
14290 }
14291
14292 /**
14293 * Start the specified animation now.
14294 *
14295 * @param animation the animation to start now
14296 */
14297 public void startAnimation(Animation animation) {
14298 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14299 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014300 invalidateParentCaches();
14301 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 }
14303
14304 /**
14305 * Cancels any animations for this view.
14306 */
14307 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014308 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014309 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014311 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014312 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014313 }
14314
14315 /**
14316 * Sets the next animation to play for this view.
14317 * If you want the animation to play immediately, use
14318 * startAnimation. This method provides allows fine-grained
14319 * control over the start time and invalidation, but you
14320 * must make sure that 1) the animation has a start time set, and
14321 * 2) the view will be invalidated when the animation is supposed to
14322 * start.
14323 *
14324 * @param animation The next animation, or null.
14325 */
14326 public void setAnimation(Animation animation) {
14327 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014329 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014330 // If the screen is off assume the animation start time is now instead of
14331 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14332 // would cause the animation to start when the screen turns back on
14333 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14334 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14335 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014337 animation.reset();
14338 }
14339 }
14340
14341 /**
14342 * Invoked by a parent ViewGroup to notify the start of the animation
14343 * currently associated with this view. If you override this method,
14344 * always call super.onAnimationStart();
14345 *
14346 * @see #setAnimation(android.view.animation.Animation)
14347 * @see #getAnimation()
14348 */
14349 protected void onAnimationStart() {
14350 mPrivateFlags |= ANIMATION_STARTED;
14351 }
14352
14353 /**
14354 * Invoked by a parent ViewGroup to notify the end of the animation
14355 * currently associated with this view. If you override this method,
14356 * always call super.onAnimationEnd();
14357 *
14358 * @see #setAnimation(android.view.animation.Animation)
14359 * @see #getAnimation()
14360 */
14361 protected void onAnimationEnd() {
14362 mPrivateFlags &= ~ANIMATION_STARTED;
14363 }
14364
14365 /**
14366 * Invoked if there is a Transform that involves alpha. Subclass that can
14367 * draw themselves with the specified alpha should return true, and then
14368 * respect that alpha when their onDraw() is called. If this returns false
14369 * then the view may be redirected to draw into an offscreen buffer to
14370 * fulfill the request, which will look fine, but may be slower than if the
14371 * subclass handles it internally. The default implementation returns false.
14372 *
14373 * @param alpha The alpha (0..255) to apply to the view's drawing
14374 * @return true if the view can draw with the specified alpha.
14375 */
14376 protected boolean onSetAlpha(int alpha) {
14377 return false;
14378 }
14379
14380 /**
14381 * This is used by the RootView to perform an optimization when
14382 * the view hierarchy contains one or several SurfaceView.
14383 * SurfaceView is always considered transparent, but its children are not,
14384 * therefore all View objects remove themselves from the global transparent
14385 * region (passed as a parameter to this function).
14386 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014387 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014388 *
14389 * @return Returns true if the effective visibility of the view at this
14390 * point is opaque, regardless of the transparent region; returns false
14391 * if it is possible for underlying windows to be seen behind the view.
14392 *
14393 * {@hide}
14394 */
14395 public boolean gatherTransparentRegion(Region region) {
14396 final AttachInfo attachInfo = mAttachInfo;
14397 if (region != null && attachInfo != null) {
14398 final int pflags = mPrivateFlags;
14399 if ((pflags & SKIP_DRAW) == 0) {
14400 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14401 // remove it from the transparent region.
14402 final int[] location = attachInfo.mTransparentLocation;
14403 getLocationInWindow(location);
14404 region.op(location[0], location[1], location[0] + mRight - mLeft,
14405 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014406 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14408 // exists, so we remove the background drawable's non-transparent
14409 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014410 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 }
14412 }
14413 return true;
14414 }
14415
14416 /**
14417 * Play a sound effect for this view.
14418 *
14419 * <p>The framework will play sound effects for some built in actions, such as
14420 * clicking, but you may wish to play these effects in your widget,
14421 * for instance, for internal navigation.
14422 *
14423 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14424 * {@link #isSoundEffectsEnabled()} is true.
14425 *
14426 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14427 */
14428 public void playSoundEffect(int soundConstant) {
14429 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14430 return;
14431 }
14432 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14433 }
14434
14435 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014436 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014437 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014438 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014439 *
14440 * <p>The framework will provide haptic feedback for some built in actions,
14441 * such as long presses, but you may wish to provide feedback for your
14442 * own widget.
14443 *
14444 * <p>The feedback will only be performed if
14445 * {@link #isHapticFeedbackEnabled()} is true.
14446 *
14447 * @param feedbackConstant One of the constants defined in
14448 * {@link HapticFeedbackConstants}
14449 */
14450 public boolean performHapticFeedback(int feedbackConstant) {
14451 return performHapticFeedback(feedbackConstant, 0);
14452 }
14453
14454 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014455 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014456 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014457 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014458 *
14459 * @param feedbackConstant One of the constants defined in
14460 * {@link HapticFeedbackConstants}
14461 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14462 */
14463 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14464 if (mAttachInfo == null) {
14465 return false;
14466 }
Romain Guyf607bdc2010-09-10 19:20:06 -070014467 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070014468 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014469 && !isHapticFeedbackEnabled()) {
14470 return false;
14471 }
Romain Guy812ccbe2010-06-01 14:07:24 -070014472 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
14473 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014474 }
14475
14476 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070014477 * Request that the visibility of the status bar or other screen/window
14478 * decorations be changed.
14479 *
14480 * <p>This method is used to put the over device UI into temporary modes
14481 * where the user's attention is focused more on the application content,
14482 * by dimming or hiding surrounding system affordances. This is typically
14483 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
14484 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
14485 * to be placed behind the action bar (and with these flags other system
14486 * affordances) so that smooth transitions between hiding and showing them
14487 * can be done.
14488 *
14489 * <p>Two representative examples of the use of system UI visibility is
14490 * implementing a content browsing application (like a magazine reader)
14491 * and a video playing application.
14492 *
14493 * <p>The first code shows a typical implementation of a View in a content
14494 * browsing application. In this implementation, the application goes
14495 * into a content-oriented mode by hiding the status bar and action bar,
14496 * and putting the navigation elements into lights out mode. The user can
14497 * then interact with content while in this mode. Such an application should
14498 * provide an easy way for the user to toggle out of the mode (such as to
14499 * check information in the status bar or access notifications). In the
14500 * implementation here, this is done simply by tapping on the content.
14501 *
14502 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
14503 * content}
14504 *
14505 * <p>This second code sample shows a typical implementation of a View
14506 * in a video playing application. In this situation, while the video is
14507 * playing the application would like to go into a complete full-screen mode,
14508 * to use as much of the display as possible for the video. When in this state
14509 * the user can not interact with the application; the system intercepts
14510 * touching on the screen to pop the UI out of full screen mode.
14511 *
14512 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
14513 * content}
14514 *
14515 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
14516 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
14517 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
14518 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080014519 */
14520 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040014521 if (visibility != mSystemUiVisibility) {
14522 mSystemUiVisibility = visibility;
14523 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14524 mParent.recomputeViewAttributes(this);
14525 }
Joe Onorato664644d2011-01-23 17:53:23 -080014526 }
14527 }
14528
14529 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070014530 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
14531 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
14532 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
14533 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
14534 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080014535 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080014536 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080014537 return mSystemUiVisibility;
14538 }
14539
Scott Mainec6331b2011-05-24 16:55:56 -070014540 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070014541 * Returns the current system UI visibility that is currently set for
14542 * the entire window. This is the combination of the
14543 * {@link #setSystemUiVisibility(int)} values supplied by all of the
14544 * views in the window.
14545 */
14546 public int getWindowSystemUiVisibility() {
14547 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
14548 }
14549
14550 /**
14551 * Override to find out when the window's requested system UI visibility
14552 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
14553 * This is different from the callbacks recieved through
14554 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
14555 * in that this is only telling you about the local request of the window,
14556 * not the actual values applied by the system.
14557 */
14558 public void onWindowSystemUiVisibilityChanged(int visible) {
14559 }
14560
14561 /**
14562 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
14563 * the view hierarchy.
14564 */
14565 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
14566 onWindowSystemUiVisibilityChanged(visible);
14567 }
14568
14569 /**
Scott Mainec6331b2011-05-24 16:55:56 -070014570 * Set a listener to receive callbacks when the visibility of the system bar changes.
14571 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
14572 */
Joe Onorato664644d2011-01-23 17:53:23 -080014573 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014574 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080014575 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14576 mParent.recomputeViewAttributes(this);
14577 }
14578 }
14579
14580 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014581 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
14582 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080014583 */
14584 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014585 ListenerInfo li = mListenerInfo;
14586 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
14587 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080014588 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080014589 }
14590 }
14591
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014592 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
14593 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
14594 if (val != mSystemUiVisibility) {
14595 setSystemUiVisibility(val);
14596 }
14597 }
14598
Joe Onorato664644d2011-01-23 17:53:23 -080014599 /**
Joe Malin32736f02011-01-19 16:14:20 -080014600 * Creates an image that the system displays during the drag and drop
14601 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14602 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14603 * appearance as the given View. The default also positions the center of the drag shadow
14604 * directly under the touch point. If no View is provided (the constructor with no parameters
14605 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14606 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14607 * default is an invisible drag shadow.
14608 * <p>
14609 * You are not required to use the View you provide to the constructor as the basis of the
14610 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14611 * anything you want as the drag shadow.
14612 * </p>
14613 * <p>
14614 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14615 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14616 * size and position of the drag shadow. It uses this data to construct a
14617 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14618 * so that your application can draw the shadow image in the Canvas.
14619 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014620 *
14621 * <div class="special reference">
14622 * <h3>Developer Guides</h3>
14623 * <p>For a guide to implementing drag and drop features, read the
14624 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14625 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014626 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014627 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014628 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014629
14630 /**
Joe Malin32736f02011-01-19 16:14:20 -080014631 * Constructs a shadow image builder based on a View. By default, the resulting drag
14632 * shadow will have the same appearance and dimensions as the View, with the touch point
14633 * over the center of the View.
14634 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014635 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014636 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014637 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014638 }
14639
Christopher Tate17ed60c2011-01-18 12:50:26 -080014640 /**
14641 * Construct a shadow builder object with no associated View. This
14642 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14643 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14644 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014645 * reference to any View object. If they are not overridden, then the result is an
14646 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014647 */
14648 public DragShadowBuilder() {
14649 mView = new WeakReference<View>(null);
14650 }
14651
14652 /**
14653 * Returns the View object that had been passed to the
14654 * {@link #View.DragShadowBuilder(View)}
14655 * constructor. If that View parameter was {@code null} or if the
14656 * {@link #View.DragShadowBuilder()}
14657 * constructor was used to instantiate the builder object, this method will return
14658 * null.
14659 *
14660 * @return The View object associate with this builder object.
14661 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014662 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014663 final public View getView() {
14664 return mView.get();
14665 }
14666
Christopher Tate2c095f32010-10-04 14:13:40 -070014667 /**
Joe Malin32736f02011-01-19 16:14:20 -080014668 * Provides the metrics for the shadow image. These include the dimensions of
14669 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014670 * be centered under the touch location while dragging.
14671 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014672 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014673 * same as the dimensions of the View itself and centers the shadow under
14674 * the touch point.
14675 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014676 *
Joe Malin32736f02011-01-19 16:14:20 -080014677 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14678 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14679 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14680 * image.
14681 *
14682 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14683 * shadow image that should be underneath the touch point during the drag and drop
14684 * operation. Your application must set {@link android.graphics.Point#x} to the
14685 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014686 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014687 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014688 final View view = mView.get();
14689 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014690 shadowSize.set(view.getWidth(), view.getHeight());
14691 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014692 } else {
14693 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14694 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014695 }
14696
14697 /**
Joe Malin32736f02011-01-19 16:14:20 -080014698 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14699 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014700 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014701 *
Joe Malin32736f02011-01-19 16:14:20 -080014702 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014703 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014704 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014705 final View view = mView.get();
14706 if (view != null) {
14707 view.draw(canvas);
14708 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014709 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014710 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014711 }
14712 }
14713
14714 /**
Joe Malin32736f02011-01-19 16:14:20 -080014715 * Starts a drag and drop operation. When your application calls this method, it passes a
14716 * {@link android.view.View.DragShadowBuilder} object to the system. The
14717 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14718 * to get metrics for the drag shadow, and then calls the object's
14719 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14720 * <p>
14721 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14722 * drag events to all the View objects in your application that are currently visible. It does
14723 * this either by calling the View object's drag listener (an implementation of
14724 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14725 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14726 * Both are passed a {@link android.view.DragEvent} object that has a
14727 * {@link android.view.DragEvent#getAction()} value of
14728 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14729 * </p>
14730 * <p>
14731 * Your application can invoke startDrag() on any attached View object. The View object does not
14732 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14733 * be related to the View the user selected for dragging.
14734 * </p>
14735 * @param data A {@link android.content.ClipData} object pointing to the data to be
14736 * transferred by the drag and drop operation.
14737 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14738 * drag shadow.
14739 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14740 * drop operation. This Object is put into every DragEvent object sent by the system during the
14741 * current drag.
14742 * <p>
14743 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14744 * to the target Views. For example, it can contain flags that differentiate between a
14745 * a copy operation and a move operation.
14746 * </p>
14747 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14748 * so the parameter should be set to 0.
14749 * @return {@code true} if the method completes successfully, or
14750 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14751 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014752 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014753 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014754 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014755 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014756 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014757 }
14758 boolean okay = false;
14759
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014760 Point shadowSize = new Point();
14761 Point shadowTouchPoint = new Point();
14762 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014763
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014764 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14765 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14766 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014767 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014768
Chris Tatea32dcf72010-10-14 12:13:50 -070014769 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014770 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14771 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014772 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014773 Surface surface = new Surface();
14774 try {
14775 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014776 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014777 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014778 + " surface=" + surface);
14779 if (token != null) {
14780 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014781 try {
Chris Tate6b391282010-10-14 15:48:59 -070014782 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014783 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014784 } finally {
14785 surface.unlockCanvasAndPost(canvas);
14786 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014787
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014788 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014789
14790 // Cache the local state object for delivery with DragEvents
14791 root.setLocalDragState(myLocalState);
14792
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014793 // repurpose 'shadowSize' for the last touch point
14794 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014795
Christopher Tatea53146c2010-09-07 11:57:52 -070014796 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014797 shadowSize.x, shadowSize.y,
14798 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014799 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014800
14801 // Off and running! Release our local surface instance; the drag
14802 // shadow surface is now managed by the system process.
14803 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014804 }
14805 } catch (Exception e) {
14806 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14807 surface.destroy();
14808 }
14809
14810 return okay;
14811 }
14812
Christopher Tatea53146c2010-09-07 11:57:52 -070014813 /**
Joe Malin32736f02011-01-19 16:14:20 -080014814 * Handles drag events sent by the system following a call to
14815 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14816 *<p>
14817 * When the system calls this method, it passes a
14818 * {@link android.view.DragEvent} object. A call to
14819 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14820 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14821 * operation.
14822 * @param event The {@link android.view.DragEvent} sent by the system.
14823 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14824 * in DragEvent, indicating the type of drag event represented by this object.
14825 * @return {@code true} if the method was successful, otherwise {@code false}.
14826 * <p>
14827 * The method should return {@code true} in response to an action type of
14828 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14829 * operation.
14830 * </p>
14831 * <p>
14832 * The method should also return {@code true} in response to an action type of
14833 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14834 * {@code false} if it didn't.
14835 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014836 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014837 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014838 return false;
14839 }
14840
14841 /**
Joe Malin32736f02011-01-19 16:14:20 -080014842 * Detects if this View is enabled and has a drag event listener.
14843 * If both are true, then it calls the drag event listener with the
14844 * {@link android.view.DragEvent} it received. If the drag event listener returns
14845 * {@code true}, then dispatchDragEvent() returns {@code true}.
14846 * <p>
14847 * For all other cases, the method calls the
14848 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14849 * method and returns its result.
14850 * </p>
14851 * <p>
14852 * This ensures that a drag event is always consumed, even if the View does not have a drag
14853 * event listener. However, if the View has a listener and the listener returns true, then
14854 * onDragEvent() is not called.
14855 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014856 */
14857 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014858 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014859 ListenerInfo li = mListenerInfo;
14860 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14861 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014862 return true;
14863 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014864 return onDragEvent(event);
14865 }
14866
Christopher Tate3d4bf172011-03-28 16:16:46 -070014867 boolean canAcceptDrag() {
14868 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14869 }
14870
Christopher Tatea53146c2010-09-07 11:57:52 -070014871 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014872 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14873 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014874 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014875 */
14876 public void onCloseSystemDialogs(String reason) {
14877 }
Joe Malin32736f02011-01-19 16:14:20 -080014878
Dianne Hackbornffa42482009-09-23 22:20:11 -070014879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014881 * update a Region being computed for
14882 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014883 * that any non-transparent parts of the Drawable are removed from the
14884 * given transparent region.
14885 *
14886 * @param dr The Drawable whose transparency is to be applied to the region.
14887 * @param region A Region holding the current transparency information,
14888 * where any parts of the region that are set are considered to be
14889 * transparent. On return, this region will be modified to have the
14890 * transparency information reduced by the corresponding parts of the
14891 * Drawable that are not transparent.
14892 * {@hide}
14893 */
14894 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14895 if (DBG) {
14896 Log.i("View", "Getting transparent region for: " + this);
14897 }
14898 final Region r = dr.getTransparentRegion();
14899 final Rect db = dr.getBounds();
14900 final AttachInfo attachInfo = mAttachInfo;
14901 if (r != null && attachInfo != null) {
14902 final int w = getRight()-getLeft();
14903 final int h = getBottom()-getTop();
14904 if (db.left > 0) {
14905 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14906 r.op(0, 0, db.left, h, Region.Op.UNION);
14907 }
14908 if (db.right < w) {
14909 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14910 r.op(db.right, 0, w, h, Region.Op.UNION);
14911 }
14912 if (db.top > 0) {
14913 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14914 r.op(0, 0, w, db.top, Region.Op.UNION);
14915 }
14916 if (db.bottom < h) {
14917 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14918 r.op(0, db.bottom, w, h, Region.Op.UNION);
14919 }
14920 final int[] location = attachInfo.mTransparentLocation;
14921 getLocationInWindow(location);
14922 r.translate(location[0], location[1]);
14923 region.op(r, Region.Op.INTERSECT);
14924 } else {
14925 region.op(db, Region.Op.DIFFERENCE);
14926 }
14927 }
14928
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014929 private void checkForLongClick(int delayOffset) {
14930 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14931 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014932
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014933 if (mPendingCheckForLongPress == null) {
14934 mPendingCheckForLongPress = new CheckForLongPress();
14935 }
14936 mPendingCheckForLongPress.rememberWindowAttachCount();
14937 postDelayed(mPendingCheckForLongPress,
14938 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014940 }
14941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014942 /**
14943 * Inflate a view from an XML resource. This convenience method wraps the {@link
14944 * LayoutInflater} class, which provides a full range of options for view inflation.
14945 *
14946 * @param context The Context object for your activity or application.
14947 * @param resource The resource ID to inflate
14948 * @param root A view group that will be the parent. Used to properly inflate the
14949 * layout_* parameters.
14950 * @see LayoutInflater
14951 */
14952 public static View inflate(Context context, int resource, ViewGroup root) {
14953 LayoutInflater factory = LayoutInflater.from(context);
14954 return factory.inflate(resource, root);
14955 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014957 /**
Adam Powell637d3372010-08-25 14:37:03 -070014958 * Scroll the view with standard behavior for scrolling beyond the normal
14959 * content boundaries. Views that call this method should override
14960 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14961 * results of an over-scroll operation.
14962 *
14963 * Views can use this method to handle any touch or fling-based scrolling.
14964 *
14965 * @param deltaX Change in X in pixels
14966 * @param deltaY Change in Y in pixels
14967 * @param scrollX Current X scroll value in pixels before applying deltaX
14968 * @param scrollY Current Y scroll value in pixels before applying deltaY
14969 * @param scrollRangeX Maximum content scroll range along the X axis
14970 * @param scrollRangeY Maximum content scroll range along the Y axis
14971 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14972 * along the X axis.
14973 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14974 * along the Y axis.
14975 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14976 * @return true if scrolling was clamped to an over-scroll boundary along either
14977 * axis, false otherwise.
14978 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014979 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014980 protected boolean overScrollBy(int deltaX, int deltaY,
14981 int scrollX, int scrollY,
14982 int scrollRangeX, int scrollRangeY,
14983 int maxOverScrollX, int maxOverScrollY,
14984 boolean isTouchEvent) {
14985 final int overScrollMode = mOverScrollMode;
14986 final boolean canScrollHorizontal =
14987 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14988 final boolean canScrollVertical =
14989 computeVerticalScrollRange() > computeVerticalScrollExtent();
14990 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14991 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14992 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14993 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14994
14995 int newScrollX = scrollX + deltaX;
14996 if (!overScrollHorizontal) {
14997 maxOverScrollX = 0;
14998 }
14999
15000 int newScrollY = scrollY + deltaY;
15001 if (!overScrollVertical) {
15002 maxOverScrollY = 0;
15003 }
15004
15005 // Clamp values if at the limits and record
15006 final int left = -maxOverScrollX;
15007 final int right = maxOverScrollX + scrollRangeX;
15008 final int top = -maxOverScrollY;
15009 final int bottom = maxOverScrollY + scrollRangeY;
15010
15011 boolean clampedX = false;
15012 if (newScrollX > right) {
15013 newScrollX = right;
15014 clampedX = true;
15015 } else if (newScrollX < left) {
15016 newScrollX = left;
15017 clampedX = true;
15018 }
15019
15020 boolean clampedY = false;
15021 if (newScrollY > bottom) {
15022 newScrollY = bottom;
15023 clampedY = true;
15024 } else if (newScrollY < top) {
15025 newScrollY = top;
15026 clampedY = true;
15027 }
15028
15029 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15030
15031 return clampedX || clampedY;
15032 }
15033
15034 /**
15035 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15036 * respond to the results of an over-scroll operation.
15037 *
15038 * @param scrollX New X scroll value in pixels
15039 * @param scrollY New Y scroll value in pixels
15040 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15041 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15042 */
15043 protected void onOverScrolled(int scrollX, int scrollY,
15044 boolean clampedX, boolean clampedY) {
15045 // Intentionally empty.
15046 }
15047
15048 /**
15049 * Returns the over-scroll mode for this view. The result will be
15050 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15051 * (allow over-scrolling only if the view content is larger than the container),
15052 * or {@link #OVER_SCROLL_NEVER}.
15053 *
15054 * @return This view's over-scroll mode.
15055 */
15056 public int getOverScrollMode() {
15057 return mOverScrollMode;
15058 }
15059
15060 /**
15061 * Set the over-scroll mode for this view. Valid over-scroll modes are
15062 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15063 * (allow over-scrolling only if the view content is larger than the container),
15064 * or {@link #OVER_SCROLL_NEVER}.
15065 *
15066 * Setting the over-scroll mode of a view will have an effect only if the
15067 * view is capable of scrolling.
15068 *
15069 * @param overScrollMode The new over-scroll mode for this view.
15070 */
15071 public void setOverScrollMode(int overScrollMode) {
15072 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15073 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15074 overScrollMode != OVER_SCROLL_NEVER) {
15075 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15076 }
15077 mOverScrollMode = overScrollMode;
15078 }
15079
15080 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015081 * Gets a scale factor that determines the distance the view should scroll
15082 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15083 * @return The vertical scroll scale factor.
15084 * @hide
15085 */
15086 protected float getVerticalScrollFactor() {
15087 if (mVerticalScrollFactor == 0) {
15088 TypedValue outValue = new TypedValue();
15089 if (!mContext.getTheme().resolveAttribute(
15090 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15091 throw new IllegalStateException(
15092 "Expected theme to define listPreferredItemHeight.");
15093 }
15094 mVerticalScrollFactor = outValue.getDimension(
15095 mContext.getResources().getDisplayMetrics());
15096 }
15097 return mVerticalScrollFactor;
15098 }
15099
15100 /**
15101 * Gets a scale factor that determines the distance the view should scroll
15102 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15103 * @return The horizontal scroll scale factor.
15104 * @hide
15105 */
15106 protected float getHorizontalScrollFactor() {
15107 // TODO: Should use something else.
15108 return getVerticalScrollFactor();
15109 }
15110
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015111 /**
15112 * Return the value specifying the text direction or policy that was set with
15113 * {@link #setTextDirection(int)}.
15114 *
15115 * @return the defined text direction. It can be one of:
15116 *
15117 * {@link #TEXT_DIRECTION_INHERIT},
15118 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15119 * {@link #TEXT_DIRECTION_ANY_RTL},
15120 * {@link #TEXT_DIRECTION_LTR},
15121 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015122 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015123 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015124 @ViewDebug.ExportedProperty(category = "text", mapping = {
15125 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15126 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15127 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15128 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15129 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15130 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15131 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015132 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015133 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015134 }
15135
15136 /**
15137 * Set the text direction.
15138 *
15139 * @param textDirection the direction to set. Should be one of:
15140 *
15141 * {@link #TEXT_DIRECTION_INHERIT},
15142 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15143 * {@link #TEXT_DIRECTION_ANY_RTL},
15144 * {@link #TEXT_DIRECTION_LTR},
15145 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015146 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015147 */
15148 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015149 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015150 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015151 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015152 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015153 // Set the new text direction
15154 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015155 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015156 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015157 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015158 }
15159 }
15160
15161 /**
15162 * Return the resolved text direction.
15163 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015164 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15165 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15166 * up the parent chain of the view. if there is no parent, then it will return the default
15167 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15168 *
15169 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015170 *
Doug Feltcb3791202011-07-07 11:57:48 -070015171 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15172 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015173 * {@link #TEXT_DIRECTION_LTR},
15174 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015175 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015176 */
15177 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015178 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015179 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015180 resolveTextDirection();
15181 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015182 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015183 }
15184
15185 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015186 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15187 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015188 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015189 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015190 // Reset any previous text direction resolution
15191 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15192
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015193 if (hasRtlSupport()) {
15194 // Set resolved text direction flag depending on text direction flag
15195 final int textDirection = getTextDirection();
15196 switch(textDirection) {
15197 case TEXT_DIRECTION_INHERIT:
15198 if (canResolveTextDirection()) {
15199 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015200
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015201 // Set current resolved direction to the same value as the parent's one
15202 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15203 switch (parentResolvedDirection) {
15204 case TEXT_DIRECTION_FIRST_STRONG:
15205 case TEXT_DIRECTION_ANY_RTL:
15206 case TEXT_DIRECTION_LTR:
15207 case TEXT_DIRECTION_RTL:
15208 case TEXT_DIRECTION_LOCALE:
15209 mPrivateFlags2 |=
15210 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15211 break;
15212 default:
15213 // Default resolved direction is "first strong" heuristic
15214 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15215 }
15216 } else {
15217 // We cannot do the resolution if there is no parent, so use the default one
15218 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015219 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015220 break;
15221 case TEXT_DIRECTION_FIRST_STRONG:
15222 case TEXT_DIRECTION_ANY_RTL:
15223 case TEXT_DIRECTION_LTR:
15224 case TEXT_DIRECTION_RTL:
15225 case TEXT_DIRECTION_LOCALE:
15226 // Resolved direction is the same as text direction
15227 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15228 break;
15229 default:
15230 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015231 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015232 }
15233 } else {
15234 // Default resolved direction is "first strong" heuristic
15235 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015236 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015237
15238 // Set to resolved
15239 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015240 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015241 }
15242
15243 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015244 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015245 * resolution should override this method.
15246 *
15247 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015248 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015249 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015250 }
15251
15252 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015253 * Check if text direction resolution can be done.
15254 *
15255 * @return true if text direction resolution can be done otherwise return false.
15256 */
15257 public boolean canResolveTextDirection() {
15258 switch (getTextDirection()) {
15259 case TEXT_DIRECTION_INHERIT:
15260 return (mParent != null) && (mParent instanceof ViewGroup);
15261 default:
15262 return true;
15263 }
15264 }
15265
15266 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015267 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015268 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015269 * reset is done.
15270 */
15271 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015272 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015273 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015274 }
15275
15276 /**
15277 * Called when text direction is reset. Subclasses that care about text direction reset should
15278 * override this method and do a reset of the text direction of their children. The default
15279 * implementation does nothing.
15280 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015281 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015282 }
15283
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015284 /**
15285 * Return the value specifying the text alignment or policy that was set with
15286 * {@link #setTextAlignment(int)}.
15287 *
15288 * @return the defined text alignment. It can be one of:
15289 *
15290 * {@link #TEXT_ALIGNMENT_INHERIT},
15291 * {@link #TEXT_ALIGNMENT_GRAVITY},
15292 * {@link #TEXT_ALIGNMENT_CENTER},
15293 * {@link #TEXT_ALIGNMENT_TEXT_START},
15294 * {@link #TEXT_ALIGNMENT_TEXT_END},
15295 * {@link #TEXT_ALIGNMENT_VIEW_START},
15296 * {@link #TEXT_ALIGNMENT_VIEW_END}
15297 */
15298 @ViewDebug.ExportedProperty(category = "text", mapping = {
15299 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15300 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15301 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15302 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15303 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15304 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15305 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15306 })
15307 public int getTextAlignment() {
15308 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15309 }
15310
15311 /**
15312 * Set the text alignment.
15313 *
15314 * @param textAlignment The text alignment to set. Should be one of
15315 *
15316 * {@link #TEXT_ALIGNMENT_INHERIT},
15317 * {@link #TEXT_ALIGNMENT_GRAVITY},
15318 * {@link #TEXT_ALIGNMENT_CENTER},
15319 * {@link #TEXT_ALIGNMENT_TEXT_START},
15320 * {@link #TEXT_ALIGNMENT_TEXT_END},
15321 * {@link #TEXT_ALIGNMENT_VIEW_START},
15322 * {@link #TEXT_ALIGNMENT_VIEW_END}
15323 *
15324 * @attr ref android.R.styleable#View_textAlignment
15325 */
15326 public void setTextAlignment(int textAlignment) {
15327 if (textAlignment != getTextAlignment()) {
15328 // Reset the current and resolved text alignment
15329 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15330 resetResolvedTextAlignment();
15331 // Set the new text alignment
15332 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15333 // Refresh
15334 requestLayout();
15335 invalidate(true);
15336 }
15337 }
15338
15339 /**
15340 * Return the resolved text alignment.
15341 *
15342 * The resolved text alignment. This needs resolution if the value is
15343 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15344 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15345 *
15346 * @return the resolved text alignment. Returns one of:
15347 *
15348 * {@link #TEXT_ALIGNMENT_GRAVITY},
15349 * {@link #TEXT_ALIGNMENT_CENTER},
15350 * {@link #TEXT_ALIGNMENT_TEXT_START},
15351 * {@link #TEXT_ALIGNMENT_TEXT_END},
15352 * {@link #TEXT_ALIGNMENT_VIEW_START},
15353 * {@link #TEXT_ALIGNMENT_VIEW_END}
15354 */
15355 @ViewDebug.ExportedProperty(category = "text", mapping = {
15356 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15357 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15358 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15359 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15360 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15361 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15362 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15363 })
15364 public int getResolvedTextAlignment() {
15365 // If text alignment is not resolved, then resolve it
15366 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15367 resolveTextAlignment();
15368 }
15369 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15370 }
15371
15372 /**
15373 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15374 * resolution is done.
15375 */
15376 public void resolveTextAlignment() {
15377 // Reset any previous text alignment resolution
15378 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15379
15380 if (hasRtlSupport()) {
15381 // Set resolved text alignment flag depending on text alignment flag
15382 final int textAlignment = getTextAlignment();
15383 switch (textAlignment) {
15384 case TEXT_ALIGNMENT_INHERIT:
15385 // Check if we can resolve the text alignment
15386 if (canResolveLayoutDirection() && mParent instanceof View) {
15387 View view = (View) mParent;
15388
15389 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15390 switch (parentResolvedTextAlignment) {
15391 case TEXT_ALIGNMENT_GRAVITY:
15392 case TEXT_ALIGNMENT_TEXT_START:
15393 case TEXT_ALIGNMENT_TEXT_END:
15394 case TEXT_ALIGNMENT_CENTER:
15395 case TEXT_ALIGNMENT_VIEW_START:
15396 case TEXT_ALIGNMENT_VIEW_END:
15397 // Resolved text alignment is the same as the parent resolved
15398 // text alignment
15399 mPrivateFlags2 |=
15400 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15401 break;
15402 default:
15403 // Use default resolved text alignment
15404 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15405 }
15406 }
15407 else {
15408 // We cannot do the resolution if there is no parent so use the default
15409 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15410 }
15411 break;
15412 case TEXT_ALIGNMENT_GRAVITY:
15413 case TEXT_ALIGNMENT_TEXT_START:
15414 case TEXT_ALIGNMENT_TEXT_END:
15415 case TEXT_ALIGNMENT_CENTER:
15416 case TEXT_ALIGNMENT_VIEW_START:
15417 case TEXT_ALIGNMENT_VIEW_END:
15418 // Resolved text alignment is the same as text alignment
15419 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15420 break;
15421 default:
15422 // Use default resolved text alignment
15423 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15424 }
15425 } else {
15426 // Use default resolved text alignment
15427 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15428 }
15429
15430 // Set the resolved
15431 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
15432 onResolvedTextAlignmentChanged();
15433 }
15434
15435 /**
15436 * Check if text alignment resolution can be done.
15437 *
15438 * @return true if text alignment resolution can be done otherwise return false.
15439 */
15440 public boolean canResolveTextAlignment() {
15441 switch (getTextAlignment()) {
15442 case TEXT_DIRECTION_INHERIT:
15443 return (mParent != null);
15444 default:
15445 return true;
15446 }
15447 }
15448
15449 /**
15450 * Called when text alignment has been resolved. Subclasses that care about text alignment
15451 * resolution should override this method.
15452 *
15453 * The default implementation does nothing.
15454 */
15455 public void onResolvedTextAlignmentChanged() {
15456 }
15457
15458 /**
15459 * Reset resolved text alignment. Text alignment can be resolved with a call to
15460 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
15461 * reset is done.
15462 */
15463 public void resetResolvedTextAlignment() {
15464 // Reset any previous text alignment resolution
15465 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15466 onResolvedTextAlignmentReset();
15467 }
15468
15469 /**
15470 * Called when text alignment is reset. Subclasses that care about text alignment reset should
15471 * override this method and do a reset of the text alignment of their children. The default
15472 * implementation does nothing.
15473 */
15474 public void onResolvedTextAlignmentReset() {
15475 }
15476
Chet Haaseb39f0512011-05-24 14:36:40 -070015477 //
15478 // Properties
15479 //
15480 /**
15481 * A Property wrapper around the <code>alpha</code> functionality handled by the
15482 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
15483 */
Chet Haased47f1532011-12-16 11:18:52 -080015484 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015485 @Override
15486 public void setValue(View object, float value) {
15487 object.setAlpha(value);
15488 }
15489
15490 @Override
15491 public Float get(View object) {
15492 return object.getAlpha();
15493 }
15494 };
15495
15496 /**
15497 * A Property wrapper around the <code>translationX</code> functionality handled by the
15498 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
15499 */
Chet Haased47f1532011-12-16 11:18:52 -080015500 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015501 @Override
15502 public void setValue(View object, float value) {
15503 object.setTranslationX(value);
15504 }
15505
15506 @Override
15507 public Float get(View object) {
15508 return object.getTranslationX();
15509 }
15510 };
15511
15512 /**
15513 * A Property wrapper around the <code>translationY</code> functionality handled by the
15514 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
15515 */
Chet Haased47f1532011-12-16 11:18:52 -080015516 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015517 @Override
15518 public void setValue(View object, float value) {
15519 object.setTranslationY(value);
15520 }
15521
15522 @Override
15523 public Float get(View object) {
15524 return object.getTranslationY();
15525 }
15526 };
15527
15528 /**
15529 * A Property wrapper around the <code>x</code> functionality handled by the
15530 * {@link View#setX(float)} and {@link View#getX()} methods.
15531 */
Chet Haased47f1532011-12-16 11:18:52 -080015532 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015533 @Override
15534 public void setValue(View object, float value) {
15535 object.setX(value);
15536 }
15537
15538 @Override
15539 public Float get(View object) {
15540 return object.getX();
15541 }
15542 };
15543
15544 /**
15545 * A Property wrapper around the <code>y</code> functionality handled by the
15546 * {@link View#setY(float)} and {@link View#getY()} methods.
15547 */
Chet Haased47f1532011-12-16 11:18:52 -080015548 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015549 @Override
15550 public void setValue(View object, float value) {
15551 object.setY(value);
15552 }
15553
15554 @Override
15555 public Float get(View object) {
15556 return object.getY();
15557 }
15558 };
15559
15560 /**
15561 * A Property wrapper around the <code>rotation</code> functionality handled by the
15562 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
15563 */
Chet Haased47f1532011-12-16 11:18:52 -080015564 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015565 @Override
15566 public void setValue(View object, float value) {
15567 object.setRotation(value);
15568 }
15569
15570 @Override
15571 public Float get(View object) {
15572 return object.getRotation();
15573 }
15574 };
15575
15576 /**
15577 * A Property wrapper around the <code>rotationX</code> functionality handled by the
15578 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
15579 */
Chet Haased47f1532011-12-16 11:18:52 -080015580 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015581 @Override
15582 public void setValue(View object, float value) {
15583 object.setRotationX(value);
15584 }
15585
15586 @Override
15587 public Float get(View object) {
15588 return object.getRotationX();
15589 }
15590 };
15591
15592 /**
15593 * A Property wrapper around the <code>rotationY</code> functionality handled by the
15594 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
15595 */
Chet Haased47f1532011-12-16 11:18:52 -080015596 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015597 @Override
15598 public void setValue(View object, float value) {
15599 object.setRotationY(value);
15600 }
15601
15602 @Override
15603 public Float get(View object) {
15604 return object.getRotationY();
15605 }
15606 };
15607
15608 /**
15609 * A Property wrapper around the <code>scaleX</code> functionality handled by the
15610 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
15611 */
Chet Haased47f1532011-12-16 11:18:52 -080015612 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015613 @Override
15614 public void setValue(View object, float value) {
15615 object.setScaleX(value);
15616 }
15617
15618 @Override
15619 public Float get(View object) {
15620 return object.getScaleX();
15621 }
15622 };
15623
15624 /**
15625 * A Property wrapper around the <code>scaleY</code> functionality handled by the
15626 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
15627 */
Chet Haased47f1532011-12-16 11:18:52 -080015628 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015629 @Override
15630 public void setValue(View object, float value) {
15631 object.setScaleY(value);
15632 }
15633
15634 @Override
15635 public Float get(View object) {
15636 return object.getScaleY();
15637 }
15638 };
15639
Jeff Brown33bbfd22011-02-24 20:55:35 -080015640 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015641 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
15642 * Each MeasureSpec represents a requirement for either the width or the height.
15643 * A MeasureSpec is comprised of a size and a mode. There are three possible
15644 * modes:
15645 * <dl>
15646 * <dt>UNSPECIFIED</dt>
15647 * <dd>
15648 * The parent has not imposed any constraint on the child. It can be whatever size
15649 * it wants.
15650 * </dd>
15651 *
15652 * <dt>EXACTLY</dt>
15653 * <dd>
15654 * The parent has determined an exact size for the child. The child is going to be
15655 * given those bounds regardless of how big it wants to be.
15656 * </dd>
15657 *
15658 * <dt>AT_MOST</dt>
15659 * <dd>
15660 * The child can be as large as it wants up to the specified size.
15661 * </dd>
15662 * </dl>
15663 *
15664 * MeasureSpecs are implemented as ints to reduce object allocation. This class
15665 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
15666 */
15667 public static class MeasureSpec {
15668 private static final int MODE_SHIFT = 30;
15669 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
15670
15671 /**
15672 * Measure specification mode: The parent has not imposed any constraint
15673 * on the child. It can be whatever size it wants.
15674 */
15675 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
15676
15677 /**
15678 * Measure specification mode: The parent has determined an exact size
15679 * for the child. The child is going to be given those bounds regardless
15680 * of how big it wants to be.
15681 */
15682 public static final int EXACTLY = 1 << MODE_SHIFT;
15683
15684 /**
15685 * Measure specification mode: The child can be as large as it wants up
15686 * to the specified size.
15687 */
15688 public static final int AT_MOST = 2 << MODE_SHIFT;
15689
15690 /**
15691 * Creates a measure specification based on the supplied size and mode.
15692 *
15693 * The mode must always be one of the following:
15694 * <ul>
15695 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
15696 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
15697 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
15698 * </ul>
15699 *
15700 * @param size the size of the measure specification
15701 * @param mode the mode of the measure specification
15702 * @return the measure specification based on size and mode
15703 */
15704 public static int makeMeasureSpec(int size, int mode) {
15705 return size + mode;
15706 }
15707
15708 /**
15709 * Extracts the mode from the supplied measure specification.
15710 *
15711 * @param measureSpec the measure specification to extract the mode from
15712 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
15713 * {@link android.view.View.MeasureSpec#AT_MOST} or
15714 * {@link android.view.View.MeasureSpec#EXACTLY}
15715 */
15716 public static int getMode(int measureSpec) {
15717 return (measureSpec & MODE_MASK);
15718 }
15719
15720 /**
15721 * Extracts the size from the supplied measure specification.
15722 *
15723 * @param measureSpec the measure specification to extract the size from
15724 * @return the size in pixels defined in the supplied measure specification
15725 */
15726 public static int getSize(int measureSpec) {
15727 return (measureSpec & ~MODE_MASK);
15728 }
15729
15730 /**
15731 * Returns a String representation of the specified measure
15732 * specification.
15733 *
15734 * @param measureSpec the measure specification to convert to a String
15735 * @return a String with the following format: "MeasureSpec: MODE SIZE"
15736 */
15737 public static String toString(int measureSpec) {
15738 int mode = getMode(measureSpec);
15739 int size = getSize(measureSpec);
15740
15741 StringBuilder sb = new StringBuilder("MeasureSpec: ");
15742
15743 if (mode == UNSPECIFIED)
15744 sb.append("UNSPECIFIED ");
15745 else if (mode == EXACTLY)
15746 sb.append("EXACTLY ");
15747 else if (mode == AT_MOST)
15748 sb.append("AT_MOST ");
15749 else
15750 sb.append(mode).append(" ");
15751
15752 sb.append(size);
15753 return sb.toString();
15754 }
15755 }
15756
15757 class CheckForLongPress implements Runnable {
15758
15759 private int mOriginalWindowAttachCount;
15760
15761 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070015762 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015763 && mOriginalWindowAttachCount == mWindowAttachCount) {
15764 if (performLongClick()) {
15765 mHasPerformedLongPress = true;
15766 }
15767 }
15768 }
15769
15770 public void rememberWindowAttachCount() {
15771 mOriginalWindowAttachCount = mWindowAttachCount;
15772 }
15773 }
Joe Malin32736f02011-01-19 16:14:20 -080015774
Adam Powelle14579b2009-12-16 18:39:52 -080015775 private final class CheckForTap implements Runnable {
15776 public void run() {
15777 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080015778 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015779 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080015780 }
15781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015782
Adam Powella35d7682010-03-12 14:48:13 -080015783 private final class PerformClick implements Runnable {
15784 public void run() {
15785 performClick();
15786 }
15787 }
15788
Dianne Hackborn63042d62011-01-26 18:56:29 -080015789 /** @hide */
15790 public void hackTurnOffWindowResizeAnim(boolean off) {
15791 mAttachInfo.mTurnOffWindowResizeAnim = off;
15792 }
Joe Malin32736f02011-01-19 16:14:20 -080015793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015794 /**
Chet Haasea00f3862011-02-22 06:34:40 -080015795 * This method returns a ViewPropertyAnimator object, which can be used to animate
15796 * specific properties on this View.
15797 *
15798 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15799 */
15800 public ViewPropertyAnimator animate() {
15801 if (mAnimator == null) {
15802 mAnimator = new ViewPropertyAnimator(this);
15803 }
15804 return mAnimator;
15805 }
15806
15807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015808 * Interface definition for a callback to be invoked when a key event is
15809 * dispatched to this view. The callback will be invoked before the key
15810 * event is given to the view.
15811 */
15812 public interface OnKeyListener {
15813 /**
15814 * Called when a key is dispatched to a view. This allows listeners to
15815 * get a chance to respond before the target view.
15816 *
15817 * @param v The view the key has been dispatched to.
15818 * @param keyCode The code for the physical key that was pressed
15819 * @param event The KeyEvent object containing full information about
15820 * the event.
15821 * @return True if the listener has consumed the event, false otherwise.
15822 */
15823 boolean onKey(View v, int keyCode, KeyEvent event);
15824 }
15825
15826 /**
15827 * Interface definition for a callback to be invoked when a touch event is
15828 * dispatched to this view. The callback will be invoked before the touch
15829 * event is given to the view.
15830 */
15831 public interface OnTouchListener {
15832 /**
15833 * Called when a touch event is dispatched to a view. This allows listeners to
15834 * get a chance to respond before the target view.
15835 *
15836 * @param v The view the touch event has been dispatched to.
15837 * @param event The MotionEvent object containing full information about
15838 * the event.
15839 * @return True if the listener has consumed the event, false otherwise.
15840 */
15841 boolean onTouch(View v, MotionEvent event);
15842 }
15843
15844 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015845 * Interface definition for a callback to be invoked when a hover event is
15846 * dispatched to this view. The callback will be invoked before the hover
15847 * event is given to the view.
15848 */
15849 public interface OnHoverListener {
15850 /**
15851 * Called when a hover event is dispatched to a view. This allows listeners to
15852 * get a chance to respond before the target view.
15853 *
15854 * @param v The view the hover event has been dispatched to.
15855 * @param event The MotionEvent object containing full information about
15856 * the event.
15857 * @return True if the listener has consumed the event, false otherwise.
15858 */
15859 boolean onHover(View v, MotionEvent event);
15860 }
15861
15862 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015863 * Interface definition for a callback to be invoked when a generic motion event is
15864 * dispatched to this view. The callback will be invoked before the generic motion
15865 * event is given to the view.
15866 */
15867 public interface OnGenericMotionListener {
15868 /**
15869 * Called when a generic motion event is dispatched to a view. This allows listeners to
15870 * get a chance to respond before the target view.
15871 *
15872 * @param v The view the generic motion event has been dispatched to.
15873 * @param event The MotionEvent object containing full information about
15874 * the event.
15875 * @return True if the listener has consumed the event, false otherwise.
15876 */
15877 boolean onGenericMotion(View v, MotionEvent event);
15878 }
15879
15880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015881 * Interface definition for a callback to be invoked when a view has been clicked and held.
15882 */
15883 public interface OnLongClickListener {
15884 /**
15885 * Called when a view has been clicked and held.
15886 *
15887 * @param v The view that was clicked and held.
15888 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015889 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015890 */
15891 boolean onLongClick(View v);
15892 }
15893
15894 /**
Chris Tate32affef2010-10-18 15:29:21 -070015895 * Interface definition for a callback to be invoked when a drag is being dispatched
15896 * to this view. The callback will be invoked before the hosting view's own
15897 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15898 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015899 *
15900 * <div class="special reference">
15901 * <h3>Developer Guides</h3>
15902 * <p>For a guide to implementing drag and drop features, read the
15903 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15904 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015905 */
15906 public interface OnDragListener {
15907 /**
15908 * Called when a drag event is dispatched to a view. This allows listeners
15909 * to get a chance to override base View behavior.
15910 *
Joe Malin32736f02011-01-19 16:14:20 -080015911 * @param v The View that received the drag event.
15912 * @param event The {@link android.view.DragEvent} object for the drag event.
15913 * @return {@code true} if the drag event was handled successfully, or {@code false}
15914 * if the drag event was not handled. Note that {@code false} will trigger the View
15915 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015916 */
15917 boolean onDrag(View v, DragEvent event);
15918 }
15919
15920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015921 * Interface definition for a callback to be invoked when the focus state of
15922 * a view changed.
15923 */
15924 public interface OnFocusChangeListener {
15925 /**
15926 * Called when the focus state of a view has changed.
15927 *
15928 * @param v The view whose state has changed.
15929 * @param hasFocus The new focus state of v.
15930 */
15931 void onFocusChange(View v, boolean hasFocus);
15932 }
15933
15934 /**
15935 * Interface definition for a callback to be invoked when a view is clicked.
15936 */
15937 public interface OnClickListener {
15938 /**
15939 * Called when a view has been clicked.
15940 *
15941 * @param v The view that was clicked.
15942 */
15943 void onClick(View v);
15944 }
15945
15946 /**
15947 * Interface definition for a callback to be invoked when the context menu
15948 * for this view is being built.
15949 */
15950 public interface OnCreateContextMenuListener {
15951 /**
15952 * Called when the context menu for this view is being built. It is not
15953 * safe to hold onto the menu after this method returns.
15954 *
15955 * @param menu The context menu that is being built
15956 * @param v The view for which the context menu is being built
15957 * @param menuInfo Extra information about the item for which the
15958 * context menu should be shown. This information will vary
15959 * depending on the class of v.
15960 */
15961 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15962 }
15963
Joe Onorato664644d2011-01-23 17:53:23 -080015964 /**
15965 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015966 * visibility. This reports <strong>global</strong> changes to the system UI
15967 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015968 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015969 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015970 */
15971 public interface OnSystemUiVisibilityChangeListener {
15972 /**
15973 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015974 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015975 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015976 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015977 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15978 * <strong>global</strong> state of the UI visibility flags, not what your
15979 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015980 */
15981 public void onSystemUiVisibilityChange(int visibility);
15982 }
15983
Adam Powell4afd62b2011-02-18 15:02:18 -080015984 /**
15985 * Interface definition for a callback to be invoked when this view is attached
15986 * or detached from its window.
15987 */
15988 public interface OnAttachStateChangeListener {
15989 /**
15990 * Called when the view is attached to a window.
15991 * @param v The view that was attached
15992 */
15993 public void onViewAttachedToWindow(View v);
15994 /**
15995 * Called when the view is detached from a window.
15996 * @param v The view that was detached
15997 */
15998 public void onViewDetachedFromWindow(View v);
15999 }
16000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016001 private final class UnsetPressedState implements Runnable {
16002 public void run() {
16003 setPressed(false);
16004 }
16005 }
16006
16007 /**
16008 * Base class for derived classes that want to save and restore their own
16009 * state in {@link android.view.View#onSaveInstanceState()}.
16010 */
16011 public static class BaseSavedState extends AbsSavedState {
16012 /**
16013 * Constructor used when reading from a parcel. Reads the state of the superclass.
16014 *
16015 * @param source
16016 */
16017 public BaseSavedState(Parcel source) {
16018 super(source);
16019 }
16020
16021 /**
16022 * Constructor called by derived classes when creating their SavedState objects
16023 *
16024 * @param superState The state of the superclass of this view
16025 */
16026 public BaseSavedState(Parcelable superState) {
16027 super(superState);
16028 }
16029
16030 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16031 new Parcelable.Creator<BaseSavedState>() {
16032 public BaseSavedState createFromParcel(Parcel in) {
16033 return new BaseSavedState(in);
16034 }
16035
16036 public BaseSavedState[] newArray(int size) {
16037 return new BaseSavedState[size];
16038 }
16039 };
16040 }
16041
16042 /**
16043 * A set of information given to a view when it is attached to its parent
16044 * window.
16045 */
16046 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016047 interface Callbacks {
16048 void playSoundEffect(int effectId);
16049 boolean performHapticFeedback(int effectId, boolean always);
16050 }
16051
16052 /**
16053 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16054 * to a Handler. This class contains the target (View) to invalidate and
16055 * the coordinates of the dirty rectangle.
16056 *
16057 * For performance purposes, this class also implements a pool of up to
16058 * POOL_LIMIT objects that get reused. This reduces memory allocations
16059 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016060 */
Romain Guyd928d682009-03-31 17:52:16 -070016061 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016062 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016063 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16064 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016065 public InvalidateInfo newInstance() {
16066 return new InvalidateInfo();
16067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016068
Romain Guyd928d682009-03-31 17:52:16 -070016069 public void onAcquired(InvalidateInfo element) {
16070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016071
Romain Guyd928d682009-03-31 17:52:16 -070016072 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016073 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016074 }
16075 }, POOL_LIMIT)
16076 );
16077
16078 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016079 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016080
16081 View target;
16082
16083 int left;
16084 int top;
16085 int right;
16086 int bottom;
16087
Romain Guyd928d682009-03-31 17:52:16 -070016088 public void setNextPoolable(InvalidateInfo element) {
16089 mNext = element;
16090 }
16091
16092 public InvalidateInfo getNextPoolable() {
16093 return mNext;
16094 }
16095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016096 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016097 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016098 }
16099
16100 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016101 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016102 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016103
16104 public boolean isPooled() {
16105 return mIsPooled;
16106 }
16107
16108 public void setPooled(boolean isPooled) {
16109 mIsPooled = isPooled;
16110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016111 }
16112
16113 final IWindowSession mSession;
16114
16115 final IWindow mWindow;
16116
16117 final IBinder mWindowToken;
16118
16119 final Callbacks mRootCallbacks;
16120
Romain Guy59a12ca2011-06-09 17:48:21 -070016121 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016123 /**
16124 * The top view of the hierarchy.
16125 */
16126 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016128 IBinder mPanelParentWindowToken;
16129 Surface mSurface;
16130
Romain Guyb051e892010-09-28 19:09:36 -070016131 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016132 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016133 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016134
Romain Guy7e4e5612012-03-05 14:37:29 -080016135 boolean mScreenOn;
16136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016137 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016138 * Scale factor used by the compatibility mode
16139 */
16140 float mApplicationScale;
16141
16142 /**
16143 * Indicates whether the application is in compatibility mode
16144 */
16145 boolean mScalingRequired;
16146
16147 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016148 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016149 */
16150 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016151
Dianne Hackborn63042d62011-01-26 18:56:29 -080016152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016153 * Left position of this view's window
16154 */
16155 int mWindowLeft;
16156
16157 /**
16158 * Top position of this view's window
16159 */
16160 int mWindowTop;
16161
16162 /**
Adam Powell26153a32010-11-08 15:22:27 -080016163 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016164 */
Adam Powell26153a32010-11-08 15:22:27 -080016165 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016166
16167 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016168 * For windows that are full-screen but using insets to layout inside
16169 * of the screen decorations, these are the current insets for the
16170 * content of the window.
16171 */
16172 final Rect mContentInsets = new Rect();
16173
16174 /**
16175 * For windows that are full-screen but using insets to layout inside
16176 * of the screen decorations, these are the current insets for the
16177 * actual visible parts of the window.
16178 */
16179 final Rect mVisibleInsets = new Rect();
16180
16181 /**
16182 * The internal insets given by this window. This value is
16183 * supplied by the client (through
16184 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16185 * be given to the window manager when changed to be used in laying
16186 * out windows behind it.
16187 */
16188 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16189 = new ViewTreeObserver.InternalInsetsInfo();
16190
16191 /**
16192 * All views in the window's hierarchy that serve as scroll containers,
16193 * used to determine if the window can be resized or must be panned
16194 * to adjust for a soft input area.
16195 */
16196 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16197
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016198 final KeyEvent.DispatcherState mKeyDispatchState
16199 = new KeyEvent.DispatcherState();
16200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016201 /**
16202 * Indicates whether the view's window currently has the focus.
16203 */
16204 boolean mHasWindowFocus;
16205
16206 /**
16207 * The current visibility of the window.
16208 */
16209 int mWindowVisibility;
16210
16211 /**
16212 * Indicates the time at which drawing started to occur.
16213 */
16214 long mDrawingTime;
16215
16216 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016217 * Indicates whether or not ignoring the DIRTY_MASK flags.
16218 */
16219 boolean mIgnoreDirtyState;
16220
16221 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016222 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16223 * to avoid clearing that flag prematurely.
16224 */
16225 boolean mSetIgnoreDirtyState = false;
16226
16227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016228 * Indicates whether the view's window is currently in touch mode.
16229 */
16230 boolean mInTouchMode;
16231
16232 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016233 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016234 * the next time it performs a traversal
16235 */
16236 boolean mRecomputeGlobalAttributes;
16237
16238 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016239 * Always report new attributes at next traversal.
16240 */
16241 boolean mForceReportNewAttributes;
16242
16243 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016244 * Set during a traveral if any views want to keep the screen on.
16245 */
16246 boolean mKeepScreenOn;
16247
16248 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016249 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16250 */
16251 int mSystemUiVisibility;
16252
16253 /**
16254 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16255 * attached.
16256 */
16257 boolean mHasSystemUiListeners;
16258
16259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016260 * Set if the visibility of any views has changed.
16261 */
16262 boolean mViewVisibilityChanged;
16263
16264 /**
16265 * Set to true if a view has been scrolled.
16266 */
16267 boolean mViewScrollChanged;
16268
16269 /**
16270 * Global to the view hierarchy used as a temporary for dealing with
16271 * x/y points in the transparent region computations.
16272 */
16273 final int[] mTransparentLocation = new int[2];
16274
16275 /**
16276 * Global to the view hierarchy used as a temporary for dealing with
16277 * x/y points in the ViewGroup.invalidateChild implementation.
16278 */
16279 final int[] mInvalidateChildLocation = new int[2];
16280
Chet Haasec3aa3612010-06-17 08:50:37 -070016281
16282 /**
16283 * Global to the view hierarchy used as a temporary for dealing with
16284 * x/y location when view is transformed.
16285 */
16286 final float[] mTmpTransformLocation = new float[2];
16287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016288 /**
16289 * The view tree observer used to dispatch global events like
16290 * layout, pre-draw, touch mode change, etc.
16291 */
16292 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16293
16294 /**
16295 * A Canvas used by the view hierarchy to perform bitmap caching.
16296 */
16297 Canvas mCanvas;
16298
16299 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016300 * The view root impl.
16301 */
16302 final ViewRootImpl mViewRootImpl;
16303
16304 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016305 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016306 * handler can be used to pump events in the UI events queue.
16307 */
16308 final Handler mHandler;
16309
16310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016311 * Temporary for use in computing invalidate rectangles while
16312 * calling up the hierarchy.
16313 */
16314 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016315
16316 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016317 * Temporary for use in computing hit areas with transformed views
16318 */
16319 final RectF mTmpTransformRect = new RectF();
16320
16321 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016322 * Temporary list for use in collecting focusable descendents of a view.
16323 */
16324 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
16325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016326 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016327 * The id of the window for accessibility purposes.
16328 */
16329 int mAccessibilityWindowId = View.NO_ID;
16330
16331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016332 * Creates a new set of attachment information with the specified
16333 * events handler and thread.
16334 *
16335 * @param handler the events handler the view must use
16336 */
16337 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016338 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016339 mSession = session;
16340 mWindow = window;
16341 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016342 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016343 mHandler = handler;
16344 mRootCallbacks = effectPlayer;
16345 }
16346 }
16347
16348 /**
16349 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16350 * is supported. This avoids keeping too many unused fields in most
16351 * instances of View.</p>
16352 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016353 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016354
Mike Cleronf116bf82009-09-27 19:14:12 -070016355 /**
16356 * Scrollbars are not visible
16357 */
16358 public static final int OFF = 0;
16359
16360 /**
16361 * Scrollbars are visible
16362 */
16363 public static final int ON = 1;
16364
16365 /**
16366 * Scrollbars are fading away
16367 */
16368 public static final int FADING = 2;
16369
16370 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016372 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016373 public int scrollBarDefaultDelayBeforeFade;
16374 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016375
16376 public int scrollBarSize;
16377 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016378 public float[] interpolatorValues;
16379 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016380
16381 public final Paint paint;
16382 public final Matrix matrix;
16383 public Shader shader;
16384
Mike Cleronf116bf82009-09-27 19:14:12 -070016385 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16386
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016387 private static final float[] OPAQUE = { 255 };
16388 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016389
Mike Cleronf116bf82009-09-27 19:14:12 -070016390 /**
16391 * When fading should start. This time moves into the future every time
16392 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16393 */
16394 public long fadeStartTime;
16395
16396
16397 /**
16398 * The current state of the scrollbars: ON, OFF, or FADING
16399 */
16400 public int state = OFF;
16401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016402 private int mLastColor;
16403
Mike Cleronf116bf82009-09-27 19:14:12 -070016404 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016405 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16406 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016407 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16408 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016409
16410 paint = new Paint();
16411 matrix = new Matrix();
16412 // use use a height of 1, and then wack the matrix each time we
16413 // actually use it.
16414 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016416 paint.setShader(shader);
16417 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070016418 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016419 }
Romain Guy8506ab42009-06-11 17:35:47 -070016420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016421 public void setFadeColor(int color) {
16422 if (color != 0 && color != mLastColor) {
16423 mLastColor = color;
16424 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070016425
Romain Guye55e1a72009-08-27 10:42:26 -070016426 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
16427 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016429 paint.setShader(shader);
16430 // Restore the default transfer mode (src_over)
16431 paint.setXfermode(null);
16432 }
16433 }
Joe Malin32736f02011-01-19 16:14:20 -080016434
Mike Cleronf116bf82009-09-27 19:14:12 -070016435 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070016436 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070016437 if (now >= fadeStartTime) {
16438
16439 // the animation fades the scrollbars out by changing
16440 // the opacity (alpha) from fully opaque to fully
16441 // transparent
16442 int nextFrame = (int) now;
16443 int framesCount = 0;
16444
16445 Interpolator interpolator = scrollBarInterpolator;
16446
16447 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016448 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070016449
16450 // End transparent
16451 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016452 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070016453
16454 state = FADING;
16455
16456 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080016457 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070016458 }
16459 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070016460 }
Mike Cleronf116bf82009-09-27 19:14:12 -070016461
Svetoslav Ganova0156172011-06-26 17:55:44 -070016462 /**
16463 * Resuable callback for sending
16464 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
16465 */
16466 private class SendViewScrolledAccessibilityEvent implements Runnable {
16467 public volatile boolean mIsPending;
16468
16469 public void run() {
16470 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
16471 mIsPending = false;
16472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016473 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016474
16475 /**
16476 * <p>
16477 * This class represents a delegate that can be registered in a {@link View}
16478 * to enhance accessibility support via composition rather via inheritance.
16479 * It is specifically targeted to widget developers that extend basic View
16480 * classes i.e. classes in package android.view, that would like their
16481 * applications to be backwards compatible.
16482 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080016483 * <div class="special reference">
16484 * <h3>Developer Guides</h3>
16485 * <p>For more information about making applications accessible, read the
16486 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
16487 * developer guide.</p>
16488 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016489 * <p>
16490 * A scenario in which a developer would like to use an accessibility delegate
16491 * is overriding a method introduced in a later API version then the minimal API
16492 * version supported by the application. For example, the method
16493 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
16494 * in API version 4 when the accessibility APIs were first introduced. If a
16495 * developer would like his application to run on API version 4 devices (assuming
16496 * all other APIs used by the application are version 4 or lower) and take advantage
16497 * of this method, instead of overriding the method which would break the application's
16498 * backwards compatibility, he can override the corresponding method in this
16499 * delegate and register the delegate in the target View if the API version of
16500 * the system is high enough i.e. the API version is same or higher to the API
16501 * version that introduced
16502 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
16503 * </p>
16504 * <p>
16505 * Here is an example implementation:
16506 * </p>
16507 * <code><pre><p>
16508 * if (Build.VERSION.SDK_INT >= 14) {
16509 * // If the API version is equal of higher than the version in
16510 * // which onInitializeAccessibilityNodeInfo was introduced we
16511 * // register a delegate with a customized implementation.
16512 * View view = findViewById(R.id.view_id);
16513 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
16514 * public void onInitializeAccessibilityNodeInfo(View host,
16515 * AccessibilityNodeInfo info) {
16516 * // Let the default implementation populate the info.
16517 * super.onInitializeAccessibilityNodeInfo(host, info);
16518 * // Set some other information.
16519 * info.setEnabled(host.isEnabled());
16520 * }
16521 * });
16522 * }
16523 * </code></pre></p>
16524 * <p>
16525 * This delegate contains methods that correspond to the accessibility methods
16526 * in View. If a delegate has been specified the implementation in View hands
16527 * off handling to the corresponding method in this delegate. The default
16528 * implementation the delegate methods behaves exactly as the corresponding
16529 * method in View for the case of no accessibility delegate been set. Hence,
16530 * to customize the behavior of a View method, clients can override only the
16531 * corresponding delegate method without altering the behavior of the rest
16532 * accessibility related methods of the host view.
16533 * </p>
16534 */
16535 public static class AccessibilityDelegate {
16536
16537 /**
16538 * Sends an accessibility event of the given type. If accessibility is not
16539 * enabled this method has no effect.
16540 * <p>
16541 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
16542 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
16543 * been set.
16544 * </p>
16545 *
16546 * @param host The View hosting the delegate.
16547 * @param eventType The type of the event to send.
16548 *
16549 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
16550 */
16551 public void sendAccessibilityEvent(View host, int eventType) {
16552 host.sendAccessibilityEventInternal(eventType);
16553 }
16554
16555 /**
16556 * Sends an accessibility event. This method behaves exactly as
16557 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
16558 * empty {@link AccessibilityEvent} and does not perform a check whether
16559 * accessibility is enabled.
16560 * <p>
16561 * The default implementation behaves as
16562 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16563 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
16564 * the case of no accessibility delegate been set.
16565 * </p>
16566 *
16567 * @param host The View hosting the delegate.
16568 * @param event The event to send.
16569 *
16570 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16571 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16572 */
16573 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
16574 host.sendAccessibilityEventUncheckedInternal(event);
16575 }
16576
16577 /**
16578 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
16579 * to its children for adding their text content to the event.
16580 * <p>
16581 * The default implementation behaves as
16582 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16583 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
16584 * the case of no accessibility delegate been set.
16585 * </p>
16586 *
16587 * @param host The View hosting the delegate.
16588 * @param event The event.
16589 * @return True if the event population was completed.
16590 *
16591 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16592 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16593 */
16594 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16595 return host.dispatchPopulateAccessibilityEventInternal(event);
16596 }
16597
16598 /**
16599 * Gives a chance to the host View to populate the accessibility event with its
16600 * text content.
16601 * <p>
16602 * The default implementation behaves as
16603 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
16604 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
16605 * the case of no accessibility delegate been set.
16606 * </p>
16607 *
16608 * @param host The View hosting the delegate.
16609 * @param event The accessibility event which to populate.
16610 *
16611 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
16612 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
16613 */
16614 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16615 host.onPopulateAccessibilityEventInternal(event);
16616 }
16617
16618 /**
16619 * Initializes an {@link AccessibilityEvent} with information about the
16620 * the host View which is the event source.
16621 * <p>
16622 * The default implementation behaves as
16623 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
16624 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
16625 * the case of no accessibility delegate been set.
16626 * </p>
16627 *
16628 * @param host The View hosting the delegate.
16629 * @param event The event to initialize.
16630 *
16631 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
16632 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
16633 */
16634 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
16635 host.onInitializeAccessibilityEventInternal(event);
16636 }
16637
16638 /**
16639 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
16640 * <p>
16641 * The default implementation behaves as
16642 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16643 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
16644 * the case of no accessibility delegate been set.
16645 * </p>
16646 *
16647 * @param host The View hosting the delegate.
16648 * @param info The instance to initialize.
16649 *
16650 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16651 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16652 */
16653 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
16654 host.onInitializeAccessibilityNodeInfoInternal(info);
16655 }
16656
16657 /**
16658 * Called when a child of the host View has requested sending an
16659 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
16660 * to augment the event.
16661 * <p>
16662 * The default implementation behaves as
16663 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16664 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
16665 * the case of no accessibility delegate been set.
16666 * </p>
16667 *
16668 * @param host The View hosting the delegate.
16669 * @param child The child which requests sending the event.
16670 * @param event The event to be sent.
16671 * @return True if the event should be sent
16672 *
16673 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16674 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16675 */
16676 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
16677 AccessibilityEvent event) {
16678 return host.onRequestSendAccessibilityEventInternal(child, event);
16679 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070016680
16681 /**
16682 * Gets the provider for managing a virtual view hierarchy rooted at this View
16683 * and reported to {@link android.accessibilityservice.AccessibilityService}s
16684 * that explore the window content.
16685 * <p>
16686 * The default implementation behaves as
16687 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
16688 * the case of no accessibility delegate been set.
16689 * </p>
16690 *
16691 * @return The provider.
16692 *
16693 * @see AccessibilityNodeProvider
16694 */
16695 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
16696 return null;
16697 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016699}