blob: 30ac3f77bf78dceed43697c18d44f5f5c59b221d [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
Chet Haaseb39f0512011-05-24 14:36:40 -070019import android.util.FloatProperty;
20import android.util.Property;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080021import com.android.internal.R;
22import com.android.internal.util.Predicate;
23import com.android.internal.view.menu.MenuBuilder;
24
Christopher Tatea53146c2010-09-07 11:57:52 -070025import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080027import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070031import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070033import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.LinearGradient;
35import android.graphics.Matrix;
36import android.graphics.Paint;
37import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070038import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.PorterDuff;
40import android.graphics.PorterDuffXfermode;
41import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070042import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Region;
44import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.drawable.ColorDrawable;
46import android.graphics.drawable.Drawable;
47import android.os.Handler;
48import android.os.IBinder;
49import android.os.Message;
50import android.os.Parcel;
51import android.os.Parcelable;
52import android.os.RemoteException;
53import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070058import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.Pools;
60import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityEventSource;
65import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070066import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070068import android.view.animation.AnimationUtils;
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
Christopher Tatea0374192010-10-05 13:06:41 -070074import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import java.lang.reflect.InvocationTargetException;
76import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.ArrayList;
78import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070079import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080080import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82/**
83 * <p>
84 * This class represents the basic building block for user interface components. A View
85 * occupies a rectangular area on the screen and is responsible for drawing and
86 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070087 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
89 * are invisible containers that hold other Views (or other ViewGroups) and define
90 * their layout properties.
91 * </p>
92 *
93 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070094 * <p>For an introduction to using this class to develop your
95 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070097 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
106 * </p>
107 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700108 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * <a name="Using"></a>
110 * <h3>Using Views</h3>
111 * <p>
112 * All of the views in a window are arranged in a single tree. You can add views
113 * either from code or by specifying a tree of views in one or more XML layout
114 * files. There are many specialized subclasses of views that act as controls or
115 * are capable of displaying text, images, or other content.
116 * </p>
117 * <p>
118 * Once you have created a tree of views, there are typically a few types of
119 * common operations you may wish to perform:
120 * <ul>
121 * <li><strong>Set properties:</strong> for example setting the text of a
122 * {@link android.widget.TextView}. The available properties and the methods
123 * that set them will vary among the different subclasses of views. Note that
124 * properties that are known at build time can be set in the XML layout
125 * files.</li>
126 * <li><strong>Set focus:</strong> The framework will handled moving focus in
127 * response to user input. To force focus to a specific view, call
128 * {@link #requestFocus}.</li>
129 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
130 * that will be notified when something interesting happens to the view. For
131 * example, all views will let you set a listener to be notified when the view
132 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
134 * Other view subclasses offer more specialized listeners. For example, a Button
135 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * </ul>
139 * </p>
140 * <p><em>
141 * Note: The Android framework is responsible for measuring, laying out and
142 * drawing views. You should not call methods that perform these actions on
143 * views yourself unless you are actually implementing a
144 * {@link android.view.ViewGroup}.
145 * </em></p>
146 *
147 * <a name="Lifecycle"></a>
148 * <h3>Implementing a Custom View</h3>
149 *
150 * <p>
151 * To implement a custom view, you will usually begin by providing overrides for
152 * some of the standard methods that the framework calls on all views. You do
153 * not need to override all of these methods. In fact, you can start by just
154 * overriding {@link #onDraw(android.graphics.Canvas)}.
155 * <table border="2" width="85%" align="center" cellpadding="5">
156 * <thead>
157 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
158 * </thead>
159 *
160 * <tbody>
161 * <tr>
162 * <td rowspan="2">Creation</td>
163 * <td>Constructors</td>
164 * <td>There is a form of the constructor that are called when the view
165 * is created from code and a form that is called when the view is
166 * inflated from a layout file. The second form should parse and apply
167 * any attributes defined in the layout file.
168 * </td>
169 * </tr>
170 * <tr>
171 * <td><code>{@link #onFinishInflate()}</code></td>
172 * <td>Called after a view and all of its children has been inflated
173 * from XML.</td>
174 * </tr>
175 *
176 * <tr>
177 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700178 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * <td>Called to determine the size requirements for this view and all
180 * of its children.
181 * </td>
182 * </tr>
183 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700184 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * <td>Called when this view should assign a size and position to all
186 * of its children.
187 * </td>
188 * </tr>
189 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700190 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * <td>Called when the size of this view has changed.
192 * </td>
193 * </tr>
194 *
195 * <tr>
196 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700197 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * <td>Called when the view should render its content.
199 * </td>
200 * </tr>
201 *
202 * <tr>
203 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a new key event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a key up event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a trackball motion event occurs.
216 * </td>
217 * </tr>
218 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700219 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 * <td>Called when a touch screen motion event occurs.
221 * </td>
222 * </tr>
223 *
224 * <tr>
225 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700226 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * <td>Called when the view gains or loses focus.
228 * </td>
229 * </tr>
230 *
231 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700232 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 * <td>Called when the window containing the view gains or loses focus.
234 * </td>
235 * </tr>
236 *
237 * <tr>
238 * <td rowspan="3">Attaching</td>
239 * <td><code>{@link #onAttachedToWindow()}</code></td>
240 * <td>Called when the view is attached to a window.
241 * </td>
242 * </tr>
243 *
244 * <tr>
245 * <td><code>{@link #onDetachedFromWindow}</code></td>
246 * <td>Called when the view is detached from its window.
247 * </td>
248 * </tr>
249 *
250 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700251 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * <td>Called when the visibility of the window containing the view
253 * has changed.
254 * </td>
255 * </tr>
256 * </tbody>
257 *
258 * </table>
259 * </p>
260 *
261 * <a name="IDs"></a>
262 * <h3>IDs</h3>
263 * Views may have an integer id associated with them. These ids are typically
264 * assigned in the layout XML files, and are used to find specific views within
265 * the view tree. A common pattern is to:
266 * <ul>
267 * <li>Define a Button in the layout file and assign it a unique ID.
268 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700269 * &lt;Button
270 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 * android:layout_width="wrap_content"
272 * android:layout_height="wrap_content"
273 * android:text="@string/my_button_text"/&gt;
274 * </pre></li>
275 * <li>From the onCreate method of an Activity, find the Button
276 * <pre class="prettyprint">
277 * Button myButton = (Button) findViewById(R.id.my_button);
278 * </pre></li>
279 * </ul>
280 * <p>
281 * View IDs need not be unique throughout the tree, but it is good practice to
282 * ensure that they are at least unique within the part of the tree you are
283 * searching.
284 * </p>
285 *
286 * <a name="Position"></a>
287 * <h3>Position</h3>
288 * <p>
289 * The geometry of a view is that of a rectangle. A view has a location,
290 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
291 * two dimensions, expressed as a width and a height. The unit for location
292 * and dimensions is the pixel.
293 * </p>
294 *
295 * <p>
296 * It is possible to retrieve the location of a view by invoking the methods
297 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
298 * coordinate of the rectangle representing the view. The latter returns the
299 * top, or Y, coordinate of the rectangle representing the view. These methods
300 * both return the location of the view relative to its parent. For instance,
301 * when getLeft() returns 20, that means the view is located 20 pixels to the
302 * right of the left edge of its direct parent.
303 * </p>
304 *
305 * <p>
306 * In addition, several convenience methods are offered to avoid unnecessary
307 * computations, namely {@link #getRight()} and {@link #getBottom()}.
308 * These methods return the coordinates of the right and bottom edges of the
309 * rectangle representing the view. For instance, calling {@link #getRight()}
310 * is similar to the following computation: <code>getLeft() + getWidth()</code>
311 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
312 * </p>
313 *
314 * <a name="SizePaddingMargins"></a>
315 * <h3>Size, padding and margins</h3>
316 * <p>
317 * The size of a view is expressed with a width and a height. A view actually
318 * possess two pairs of width and height values.
319 * </p>
320 *
321 * <p>
322 * The first pair is known as <em>measured width</em> and
323 * <em>measured height</em>. These dimensions define how big a view wants to be
324 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
325 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
326 * and {@link #getMeasuredHeight()}.
327 * </p>
328 *
329 * <p>
330 * The second pair is simply known as <em>width</em> and <em>height</em>, or
331 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
332 * dimensions define the actual size of the view on screen, at drawing time and
333 * after layout. These values may, but do not have to, be different from the
334 * measured width and height. The width and height can be obtained by calling
335 * {@link #getWidth()} and {@link #getHeight()}.
336 * </p>
337 *
338 * <p>
339 * To measure its dimensions, a view takes into account its padding. The padding
340 * is expressed in pixels for the left, top, right and bottom parts of the view.
341 * Padding can be used to offset the content of the view by a specific amount of
342 * pixels. For instance, a left padding of 2 will push the view's content by
343 * 2 pixels to the right of the left edge. Padding can be set using the
344 * {@link #setPadding(int, int, int, int)} method and queried by calling
345 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
346 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
347 * </p>
348 *
349 * <p>
350 * Even though a view can define a padding, it does not provide any support for
351 * margins. However, view groups provide such a support. Refer to
352 * {@link android.view.ViewGroup} and
353 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
354 * </p>
355 *
356 * <a name="Layout"></a>
357 * <h3>Layout</h3>
358 * <p>
359 * Layout is a two pass process: a measure pass and a layout pass. The measuring
360 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
361 * of the view tree. Each view pushes dimension specifications down the tree
362 * during the recursion. At the end of the measure pass, every view has stored
363 * its measurements. The second pass happens in
364 * {@link #layout(int,int,int,int)} and is also top-down. During
365 * this pass each parent is responsible for positioning all of its children
366 * using the sizes computed in the measure pass.
367 * </p>
368 *
369 * <p>
370 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
371 * {@link #getMeasuredHeight()} values must be set, along with those for all of
372 * that view's descendants. A view's measured width and measured height values
373 * must respect the constraints imposed by the view's parents. This guarantees
374 * that at the end of the measure pass, all parents accept all of their
375 * children's measurements. A parent view may call measure() more than once on
376 * its children. For example, the parent may measure each child once with
377 * unspecified dimensions to find out how big they want to be, then call
378 * measure() on them again with actual numbers if the sum of all the children's
379 * unconstrained sizes is too big or too small.
380 * </p>
381 *
382 * <p>
383 * The measure pass uses two classes to communicate dimensions. The
384 * {@link MeasureSpec} class is used by views to tell their parents how they
385 * want to be measured and positioned. The base LayoutParams class just
386 * describes how big the view wants to be for both width and height. For each
387 * dimension, it can specify one of:
388 * <ul>
389 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800390 * <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 -0800391 * (minus padding)
392 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
393 * enclose its content (plus padding).
394 * </ul>
395 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
396 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
397 * an X and Y value.
398 * </p>
399 *
400 * <p>
401 * MeasureSpecs are used to push requirements down the tree from parent to
402 * child. A MeasureSpec can be in one of three modes:
403 * <ul>
404 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
405 * of a child view. For example, a LinearLayout may call measure() on its child
406 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
407 * tall the child view wants to be given a width of 240 pixels.
408 * <li>EXACTLY: This is used by the parent to impose an exact size on the
409 * child. The child must use this size, and guarantee that all of its
410 * descendants will fit within this size.
411 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
412 * child. The child must gurantee that it and all of its descendants will fit
413 * within this size.
414 * </ul>
415 * </p>
416 *
417 * <p>
418 * To intiate a layout, call {@link #requestLayout}. This method is typically
419 * called by a view on itself when it believes that is can no longer fit within
420 * its current bounds.
421 * </p>
422 *
423 * <a name="Drawing"></a>
424 * <h3>Drawing</h3>
425 * <p>
426 * Drawing is handled by walking the tree and rendering each view that
427 * intersects the the invalid region. Because the tree is traversed in-order,
428 * this means that parents will draw before (i.e., behind) their children, with
429 * siblings drawn in the order they appear in the tree.
430 * If you set a background drawable for a View, then the View will draw it for you
431 * before calling back to its <code>onDraw()</code> method.
432 * </p>
433 *
434 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700435 * 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 -0800436 * </p>
437 *
438 * <p>
439 * To force a view to draw, call {@link #invalidate()}.
440 * </p>
441 *
442 * <a name="EventHandlingThreading"></a>
443 * <h3>Event Handling and Threading</h3>
444 * <p>
445 * The basic cycle of a view is as follows:
446 * <ol>
447 * <li>An event comes in and is dispatched to the appropriate view. The view
448 * handles the event and notifies any listeners.</li>
449 * <li>If in the course of processing the event, the view's bounds may need
450 * to be changed, the view will call {@link #requestLayout()}.</li>
451 * <li>Similarly, if in the course of processing the event the view's appearance
452 * may need to be changed, the view will call {@link #invalidate()}.</li>
453 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
454 * the framework will take care of measuring, laying out, and drawing the tree
455 * as appropriate.</li>
456 * </ol>
457 * </p>
458 *
459 * <p><em>Note: The entire view tree is single threaded. You must always be on
460 * the UI thread when calling any method on any view.</em>
461 * If you are doing work on other threads and want to update the state of a view
462 * from that thread, you should use a {@link Handler}.
463 * </p>
464 *
465 * <a name="FocusHandling"></a>
466 * <h3>Focus Handling</h3>
467 * <p>
468 * The framework will handle routine focus movement in response to user input.
469 * This includes changing the focus as views are removed or hidden, or as new
470 * views become available. Views indicate their willingness to take focus
471 * through the {@link #isFocusable} method. To change whether a view can take
472 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
473 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
474 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
475 * </p>
476 * <p>
477 * Focus movement is based on an algorithm which finds the nearest neighbor in a
478 * given direction. In rare cases, the default algorithm may not match the
479 * intended behavior of the developer. In these situations, you can provide
480 * explicit overrides by using these XML attributes in the layout file:
481 * <pre>
482 * nextFocusDown
483 * nextFocusLeft
484 * nextFocusRight
485 * nextFocusUp
486 * </pre>
487 * </p>
488 *
489 *
490 * <p>
491 * To get a particular view to take focus, call {@link #requestFocus()}.
492 * </p>
493 *
494 * <a name="TouchMode"></a>
495 * <h3>Touch Mode</h3>
496 * <p>
497 * When a user is navigating a user interface via directional keys such as a D-pad, it is
498 * necessary to give focus to actionable items such as buttons so the user can see
499 * what will take input. If the device has touch capabilities, however, and the user
500 * begins interacting with the interface by touching it, it is no longer necessary to
501 * always highlight, or give focus to, a particular view. This motivates a mode
502 * for interaction named 'touch mode'.
503 * </p>
504 * <p>
505 * For a touch capable device, once the user touches the screen, the device
506 * will enter touch mode. From this point onward, only views for which
507 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
508 * Other views that are touchable, like buttons, will not take focus when touched; they will
509 * only fire the on click listeners.
510 * </p>
511 * <p>
512 * Any time a user hits a directional key, such as a D-pad direction, the view device will
513 * exit touch mode, and find a view to take focus, so that the user may resume interacting
514 * with the user interface without touching the screen again.
515 * </p>
516 * <p>
517 * The touch mode state is maintained across {@link android.app.Activity}s. Call
518 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
519 * </p>
520 *
521 * <a name="Scrolling"></a>
522 * <h3>Scrolling</h3>
523 * <p>
524 * The framework provides basic support for views that wish to internally
525 * scroll their content. This includes keeping track of the X and Y scroll
526 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800527 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700528 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 * </p>
530 *
531 * <a name="Tags"></a>
532 * <h3>Tags</h3>
533 * <p>
534 * Unlike IDs, tags are not used to identify views. Tags are essentially an
535 * extra piece of information that can be associated with a view. They are most
536 * often used as a convenience to store data related to views in the views
537 * themselves rather than by putting them in a separate structure.
538 * </p>
539 *
540 * <a name="Animation"></a>
541 * <h3>Animation</h3>
542 * <p>
543 * You can attach an {@link Animation} object to a view using
544 * {@link #setAnimation(Animation)} or
545 * {@link #startAnimation(Animation)}. The animation can alter the scale,
546 * rotation, translation and alpha of a view over time. If the animation is
547 * attached to a view that has children, the animation will affect the entire
548 * subtree rooted by that node. When an animation is started, the framework will
549 * take care of redrawing the appropriate views until the animation completes.
550 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800551 * <p>
552 * Starting with Android 3.0, the preferred way of animating views is to use the
553 * {@link android.animation} package APIs.
554 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 *
Jeff Brown85a31762010-09-01 17:01:00 -0700556 * <a name="Security"></a>
557 * <h3>Security</h3>
558 * <p>
559 * Sometimes it is essential that an application be able to verify that an action
560 * is being performed with the full knowledge and consent of the user, such as
561 * granting a permission request, making a purchase or clicking on an advertisement.
562 * Unfortunately, a malicious application could try to spoof the user into
563 * performing these actions, unaware, by concealing the intended purpose of the view.
564 * As a remedy, the framework offers a touch filtering mechanism that can be used to
565 * improve the security of views that provide access to sensitive functionality.
566 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700567 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800568 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700569 * will discard touches that are received whenever the view's window is obscured by
570 * another visible window. As a result, the view will not receive touches whenever a
571 * toast, dialog or other window appears above the view's window.
572 * </p><p>
573 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700574 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
575 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700576 * </p>
577 *
Romain Guy171c5922011-01-06 10:04:23 -0800578 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700579 * @attr ref android.R.styleable#View_background
580 * @attr ref android.R.styleable#View_clickable
581 * @attr ref android.R.styleable#View_contentDescription
582 * @attr ref android.R.styleable#View_drawingCacheQuality
583 * @attr ref android.R.styleable#View_duplicateParentState
584 * @attr ref android.R.styleable#View_id
585 * @attr ref android.R.styleable#View_fadingEdge
586 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700587 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700589 * @attr ref android.R.styleable#View_isScrollContainer
590 * @attr ref android.R.styleable#View_focusable
591 * @attr ref android.R.styleable#View_focusableInTouchMode
592 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
593 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800594 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700595 * @attr ref android.R.styleable#View_longClickable
596 * @attr ref android.R.styleable#View_minHeight
597 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @attr ref android.R.styleable#View_nextFocusDown
599 * @attr ref android.R.styleable#View_nextFocusLeft
600 * @attr ref android.R.styleable#View_nextFocusRight
601 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_onClick
603 * @attr ref android.R.styleable#View_padding
604 * @attr ref android.R.styleable#View_paddingBottom
605 * @attr ref android.R.styleable#View_paddingLeft
606 * @attr ref android.R.styleable#View_paddingRight
607 * @attr ref android.R.styleable#View_paddingTop
608 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800609 * @attr ref android.R.styleable#View_rotation
610 * @attr ref android.R.styleable#View_rotationX
611 * @attr ref android.R.styleable#View_rotationY
612 * @attr ref android.R.styleable#View_scaleX
613 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * @attr ref android.R.styleable#View_scrollX
615 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_scrollbarSize
617 * @attr ref android.R.styleable#View_scrollbarStyle
618 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700619 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
620 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
622 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarThumbVertical
624 * @attr ref android.R.styleable#View_scrollbarTrackVertical
625 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_soundEffectsEnabled
628 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800629 * @attr ref android.R.styleable#View_transformPivotX
630 * @attr ref android.R.styleable#View_transformPivotY
631 * @attr ref android.R.styleable#View_translationX
632 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 *
635 * @see android.view.ViewGroup
636 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700637public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 private static final boolean DBG = false;
639
640 /**
641 * The logging tag used by this class with android.util.Log.
642 */
643 protected static final String VIEW_LOG_TAG = "View";
644
645 /**
646 * Used to mark a View that has no ID.
647 */
648 public static final int NO_ID = -1;
649
650 /**
651 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
652 * calling setFlags.
653 */
654 private static final int NOT_FOCUSABLE = 0x00000000;
655
656 /**
657 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
658 * setFlags.
659 */
660 private static final int FOCUSABLE = 0x00000001;
661
662 /**
663 * Mask for use with setFlags indicating bits used for focus.
664 */
665 private static final int FOCUSABLE_MASK = 0x00000001;
666
667 /**
668 * This view will adjust its padding to fit sytem windows (e.g. status bar)
669 */
670 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
671
672 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700673 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 */
675 public static final int VISIBLE = 0x00000000;
676
677 /**
678 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700679 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 */
681 public static final int INVISIBLE = 0x00000004;
682
683 /**
684 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700685 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 */
687 public static final int GONE = 0x00000008;
688
689 /**
690 * Mask for use with setFlags indicating bits used for visibility.
691 * {@hide}
692 */
693 static final int VISIBILITY_MASK = 0x0000000C;
694
695 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
696
697 /**
698 * This view is enabled. Intrepretation varies by subclass.
699 * Use with ENABLED_MASK when calling setFlags.
700 * {@hide}
701 */
702 static final int ENABLED = 0x00000000;
703
704 /**
705 * This view is disabled. Intrepretation varies by subclass.
706 * Use with ENABLED_MASK when calling setFlags.
707 * {@hide}
708 */
709 static final int DISABLED = 0x00000020;
710
711 /**
712 * Mask for use with setFlags indicating bits used for indicating whether
713 * this view is enabled
714 * {@hide}
715 */
716 static final int ENABLED_MASK = 0x00000020;
717
718 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700719 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
720 * called and further optimizations will be performed. It is okay to have
721 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 * {@hide}
723 */
724 static final int WILL_NOT_DRAW = 0x00000080;
725
726 /**
727 * Mask for use with setFlags indicating bits used for indicating whether
728 * this view is will draw
729 * {@hide}
730 */
731 static final int DRAW_MASK = 0x00000080;
732
733 /**
734 * <p>This view doesn't show scrollbars.</p>
735 * {@hide}
736 */
737 static final int SCROLLBARS_NONE = 0x00000000;
738
739 /**
740 * <p>This view shows horizontal scrollbars.</p>
741 * {@hide}
742 */
743 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
744
745 /**
746 * <p>This view shows vertical scrollbars.</p>
747 * {@hide}
748 */
749 static final int SCROLLBARS_VERTICAL = 0x00000200;
750
751 /**
752 * <p>Mask for use with setFlags indicating bits used for indicating which
753 * scrollbars are enabled.</p>
754 * {@hide}
755 */
756 static final int SCROLLBARS_MASK = 0x00000300;
757
Jeff Brown85a31762010-09-01 17:01:00 -0700758 /**
759 * Indicates that the view should filter touches when its window is obscured.
760 * Refer to the class comments for more information about this security feature.
761 * {@hide}
762 */
763 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
764
765 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
767 /**
768 * <p>This view doesn't show fading edges.</p>
769 * {@hide}
770 */
771 static final int FADING_EDGE_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal fading edges.</p>
775 * {@hide}
776 */
777 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
778
779 /**
780 * <p>This view shows vertical fading edges.</p>
781 * {@hide}
782 */
783 static final int FADING_EDGE_VERTICAL = 0x00002000;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * fading edges are enabled.</p>
788 * {@hide}
789 */
790 static final int FADING_EDGE_MASK = 0x00003000;
791
792 /**
793 * <p>Indicates this view can be clicked. When clickable, a View reacts
794 * to clicks by notifying the OnClickListener.<p>
795 * {@hide}
796 */
797 static final int CLICKABLE = 0x00004000;
798
799 /**
800 * <p>Indicates this view is caching its drawing into a bitmap.</p>
801 * {@hide}
802 */
803 static final int DRAWING_CACHE_ENABLED = 0x00008000;
804
805 /**
806 * <p>Indicates that no icicle should be saved for this view.<p>
807 * {@hide}
808 */
809 static final int SAVE_DISABLED = 0x000010000;
810
811 /**
812 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
813 * property.</p>
814 * {@hide}
815 */
816 static final int SAVE_DISABLED_MASK = 0x000010000;
817
818 /**
819 * <p>Indicates that no drawing cache should ever be created for this view.<p>
820 * {@hide}
821 */
822 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
823
824 /**
825 * <p>Indicates this view can take / keep focus when int touch mode.</p>
826 * {@hide}
827 */
828 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
829
830 /**
831 * <p>Enables low quality mode for the drawing cache.</p>
832 */
833 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
834
835 /**
836 * <p>Enables high quality mode for the drawing cache.</p>
837 */
838 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
839
840 /**
841 * <p>Enables automatic quality mode for the drawing cache.</p>
842 */
843 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
844
845 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
846 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
847 };
848
849 /**
850 * <p>Mask for use with setFlags indicating bits used for the cache
851 * quality property.</p>
852 * {@hide}
853 */
854 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
855
856 /**
857 * <p>
858 * Indicates this view can be long clicked. When long clickable, a View
859 * reacts to long clicks by notifying the OnLongClickListener or showing a
860 * context menu.
861 * </p>
862 * {@hide}
863 */
864 static final int LONG_CLICKABLE = 0x00200000;
865
866 /**
867 * <p>Indicates that this view gets its drawable states from its direct parent
868 * and ignores its original internal states.</p>
869 *
870 * @hide
871 */
872 static final int DUPLICATE_PARENT_STATE = 0x00400000;
873
874 /**
875 * The scrollbar style to display the scrollbars inside the content area,
876 * without increasing the padding. The scrollbars will be overlaid with
877 * translucency on the view's content.
878 */
879 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
880
881 /**
882 * The scrollbar style to display the scrollbars inside the padded area,
883 * increasing the padding of the view. The scrollbars will not overlap the
884 * content area of the view.
885 */
886 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
887
888 /**
889 * The scrollbar style to display the scrollbars at the edge of the view,
890 * without increasing the padding. The scrollbars will be overlaid with
891 * translucency.
892 */
893 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
894
895 /**
896 * The scrollbar style to display the scrollbars at the edge of the view,
897 * increasing the padding of the view. The scrollbars will only overlap the
898 * background, if any.
899 */
900 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
901
902 /**
903 * Mask to check if the scrollbar style is overlay or inset.
904 * {@hide}
905 */
906 static final int SCROLLBARS_INSET_MASK = 0x01000000;
907
908 /**
909 * Mask to check if the scrollbar style is inside or outside.
910 * {@hide}
911 */
912 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
913
914 /**
915 * Mask for scrollbar style.
916 * {@hide}
917 */
918 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
919
920 /**
921 * View flag indicating that the screen should remain on while the
922 * window containing this view is visible to the user. This effectively
923 * takes care of automatically setting the WindowManager's
924 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
925 */
926 public static final int KEEP_SCREEN_ON = 0x04000000;
927
928 /**
929 * View flag indicating whether this view should have sound effects enabled
930 * for events such as clicking and touching.
931 */
932 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
933
934 /**
935 * View flag indicating whether this view should have haptic feedback
936 * enabled for events such as long presses.
937 */
938 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
939
940 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700941 * <p>Indicates that the view hierarchy should stop saving state when
942 * it reaches this view. If state saving is initiated immediately at
943 * the view, it will be allowed.
944 * {@hide}
945 */
946 static final int PARENT_SAVE_DISABLED = 0x20000000;
947
948 /**
949 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
950 * {@hide}
951 */
952 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
953
954 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800955 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700956 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * {@hide}
958 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700959 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800960
961 /**
962 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700963 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800964 * {@hide}
965 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800967
968 /**
969 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800971 * {@hide}
972 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800974
975 /**
976 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800978 * {@hide}
979 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700980 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800981
982 /**
983 * Mask for use with setFlags indicating bits used for horizontalDirection.
984 * {@hide}
985 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700986 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800987
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700988 /*
989 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
990 * flag value.
991 * {@hide}
992 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700993 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
994 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800995
996 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700997 * Default horizontalDirection.
998 * {@hide}
999 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001000 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001001
1002 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001003 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1004 * should add all focusable Views regardless if they are focusable in touch mode.
1005 */
1006 public static final int FOCUSABLES_ALL = 0x00000000;
1007
1008 /**
1009 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1010 * should add only Views focusable in touch mode.
1011 */
1012 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1013
1014 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001015 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 * item.
1017 */
1018 public static final int FOCUS_BACKWARD = 0x00000001;
1019
1020 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001021 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * item.
1023 */
1024 public static final int FOCUS_FORWARD = 0x00000002;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 */
1029 public static final int FOCUS_LEFT = 0x00000011;
1030
1031 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001032 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 */
1034 public static final int FOCUS_UP = 0x00000021;
1035
1036 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001037 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 */
1039 public static final int FOCUS_RIGHT = 0x00000042;
1040
1041 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001042 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 */
1044 public static final int FOCUS_DOWN = 0x00000082;
1045
1046 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001047 * Bits of {@link #getMeasuredWidthAndState()} and
1048 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1049 */
1050 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1051
1052 /**
1053 * Bits of {@link #getMeasuredWidthAndState()} and
1054 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1055 */
1056 public static final int MEASURED_STATE_MASK = 0xff000000;
1057
1058 /**
1059 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1060 * for functions that combine both width and height into a single int,
1061 * such as {@link #getMeasuredState()} and the childState argument of
1062 * {@link #resolveSizeAndState(int, int, int)}.
1063 */
1064 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1065
1066 /**
1067 * Bit of {@link #getMeasuredWidthAndState()} and
1068 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1069 * is smaller that the space the view would like to have.
1070 */
1071 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1072
1073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 * Base View state sets
1075 */
1076 // Singles
1077 /**
1078 * Indicates the view has no states set. 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 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001085 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 /**
1087 * Indicates the view is enabled. States are used with
1088 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1089 * view depending on its state.
1090 *
1091 * @see android.graphics.drawable.Drawable
1092 * @see #getDrawableState()
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 /**
1096 * Indicates the view is focused. States are used with
1097 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1098 * view depending on its state.
1099 *
1100 * @see android.graphics.drawable.Drawable
1101 * @see #getDrawableState()
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is selected. States are used with
1106 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1107 * view depending on its state.
1108 *
1109 * @see android.graphics.drawable.Drawable
1110 * @see #getDrawableState()
1111 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001112 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 /**
1114 * Indicates the view is pressed. States are used with
1115 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1116 * view depending on its state.
1117 *
1118 * @see android.graphics.drawable.Drawable
1119 * @see #getDrawableState()
1120 * @hide
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view's window has focus. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 // Doubles
1133 /**
1134 * Indicates the view is enabled and has the focus.
1135 *
1136 * @see #ENABLED_STATE_SET
1137 * @see #FOCUSED_STATE_SET
1138 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001139 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 /**
1141 * Indicates the view is enabled and selected.
1142 *
1143 * @see #ENABLED_STATE_SET
1144 * @see #SELECTED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is enabled and that its window has focus.
1149 *
1150 * @see #ENABLED_STATE_SET
1151 * @see #WINDOW_FOCUSED_STATE_SET
1152 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001153 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Indicates the view is focused and selected.
1156 *
1157 * @see #FOCUSED_STATE_SET
1158 * @see #SELECTED_STATE_SET
1159 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001160 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 /**
1162 * Indicates the view has the focus and that its window has the focus.
1163 *
1164 * @see #FOCUSED_STATE_SET
1165 * @see #WINDOW_FOCUSED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is selected and that its window has the focus.
1170 *
1171 * @see #SELECTED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Triples
1176 /**
1177 * Indicates the view is enabled, focused and selected.
1178 *
1179 * @see #ENABLED_STATE_SET
1180 * @see #FOCUSED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled, focused and its window has the focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #FOCUSED_STATE_SET
1189 * @see #WINDOW_FOCUSED_STATE_SET
1190 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001191 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 /**
1193 * Indicates the view is enabled, selected and its window has the focus.
1194 *
1195 * @see #ENABLED_STATE_SET
1196 * @see #SELECTED_STATE_SET
1197 * @see #WINDOW_FOCUSED_STATE_SET
1198 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001199 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 /**
1201 * Indicates the view is focused, selected and its window has the focus.
1202 *
1203 * @see #FOCUSED_STATE_SET
1204 * @see #SELECTED_STATE_SET
1205 * @see #WINDOW_FOCUSED_STATE_SET
1206 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001207 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 /**
1209 * Indicates the view is enabled, focused, selected and its window
1210 * has the focus.
1211 *
1212 * @see #ENABLED_STATE_SET
1213 * @see #FOCUSED_STATE_SET
1214 * @see #SELECTED_STATE_SET
1215 * @see #WINDOW_FOCUSED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is pressed and its window has the focus.
1220 *
1221 * @see #PRESSED_STATE_SET
1222 * @see #WINDOW_FOCUSED_STATE_SET
1223 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001224 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 /**
1226 * Indicates the view is pressed and selected.
1227 *
1228 * @see #PRESSED_STATE_SET
1229 * @see #SELECTED_STATE_SET
1230 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001231 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 /**
1233 * Indicates the view is pressed, selected and its window has the focus.
1234 *
1235 * @see #PRESSED_STATE_SET
1236 * @see #SELECTED_STATE_SET
1237 * @see #WINDOW_FOCUSED_STATE_SET
1238 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001239 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 /**
1241 * Indicates the view is pressed and focused.
1242 *
1243 * @see #PRESSED_STATE_SET
1244 * @see #FOCUSED_STATE_SET
1245 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001246 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
1248 * Indicates the view is pressed, focused and its window has the focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #FOCUSED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed, focused and selected.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #SELECTED_STATE_SET
1260 * @see #FOCUSED_STATE_SET
1261 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001262 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 /**
1264 * Indicates the view is pressed, focused, selected and its window has the focus.
1265 *
1266 * @see #PRESSED_STATE_SET
1267 * @see #FOCUSED_STATE_SET
1268 * @see #SELECTED_STATE_SET
1269 * @see #WINDOW_FOCUSED_STATE_SET
1270 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001271 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
1273 * Indicates the view is pressed and enabled.
1274 *
1275 * @see #PRESSED_STATE_SET
1276 * @see #ENABLED_STATE_SET
1277 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001278 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 /**
1280 * Indicates the view is pressed, enabled and its window has the focus.
1281 *
1282 * @see #PRESSED_STATE_SET
1283 * @see #ENABLED_STATE_SET
1284 * @see #WINDOW_FOCUSED_STATE_SET
1285 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001286 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 /**
1288 * Indicates the view is pressed, enabled and selected.
1289 *
1290 * @see #PRESSED_STATE_SET
1291 * @see #ENABLED_STATE_SET
1292 * @see #SELECTED_STATE_SET
1293 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001294 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 /**
1296 * Indicates the view is pressed, enabled, selected and its window has the
1297 * focus.
1298 *
1299 * @see #PRESSED_STATE_SET
1300 * @see #ENABLED_STATE_SET
1301 * @see #SELECTED_STATE_SET
1302 * @see #WINDOW_FOCUSED_STATE_SET
1303 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001304 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 /**
1306 * Indicates the view is pressed, enabled and focused.
1307 *
1308 * @see #PRESSED_STATE_SET
1309 * @see #ENABLED_STATE_SET
1310 * @see #FOCUSED_STATE_SET
1311 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001312 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 /**
1314 * Indicates the view is pressed, enabled, focused and its window has the
1315 * focus.
1316 *
1317 * @see #PRESSED_STATE_SET
1318 * @see #ENABLED_STATE_SET
1319 * @see #FOCUSED_STATE_SET
1320 * @see #WINDOW_FOCUSED_STATE_SET
1321 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001322 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 /**
1324 * Indicates the view is pressed, enabled, focused and selected.
1325 *
1326 * @see #PRESSED_STATE_SET
1327 * @see #ENABLED_STATE_SET
1328 * @see #SELECTED_STATE_SET
1329 * @see #FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused, selected and its window
1334 * has the focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #FOCUSED_STATE_SET
1340 * @see #WINDOW_FOCUSED_STATE_SET
1341 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343
1344 /**
1345 * The order here is very important to {@link #getDrawableState()}
1346 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 private static final int[][] VIEW_STATE_SETS;
1348
Romain Guyb051e892010-09-28 19:09:36 -07001349 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1350 static final int VIEW_STATE_SELECTED = 1 << 1;
1351 static final int VIEW_STATE_FOCUSED = 1 << 2;
1352 static final int VIEW_STATE_ENABLED = 1 << 3;
1353 static final int VIEW_STATE_PRESSED = 1 << 4;
1354 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001355 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001356 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001357 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1358 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359
1360 static final int[] VIEW_STATE_IDS = new int[] {
1361 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1362 R.attr.state_selected, VIEW_STATE_SELECTED,
1363 R.attr.state_focused, VIEW_STATE_FOCUSED,
1364 R.attr.state_enabled, VIEW_STATE_ENABLED,
1365 R.attr.state_pressed, VIEW_STATE_PRESSED,
1366 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001367 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001368 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001369 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1370 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 };
1372
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001373 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001374 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1375 throw new IllegalStateException(
1376 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1377 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001379 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001381 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001383 orderedIds[i * 2] = viewState;
1384 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 }
1386 }
1387 }
Romain Guyb051e892010-09-28 19:09:36 -07001388 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1389 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1390 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 int numBits = Integer.bitCount(i);
1392 int[] set = new int[numBits];
1393 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001394 for (int j = 0; j < orderedIds.length; j += 2) {
1395 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396 set[pos++] = orderedIds[j];
1397 }
1398 }
1399 VIEW_STATE_SETS[i] = set;
1400 }
1401
1402 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1403 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1404 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1405 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1407 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1408 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1410 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1412 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1414 | VIEW_STATE_FOCUSED];
1415 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1416 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1418 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1420 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1422 | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1427 | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1433 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1434
1435 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1436 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1438 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1440 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1442 | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1453 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1458 | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1464 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1476 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1477 | VIEW_STATE_PRESSED];
1478 }
1479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 * Temporary Rect currently for use in setBackground(). This will probably
1482 * be extended in the future to hold our own class with more than just
1483 * a Rect. :)
1484 */
1485 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001486
1487 /**
1488 * Map used to store views' tags.
1489 */
1490 private static WeakHashMap<View, SparseArray<Object>> sTags;
1491
1492 /**
1493 * Lock used to access sTags.
1494 */
1495 private static final Object sTagsLock = new Object();
1496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001498 * The next available accessiiblity id.
1499 */
1500 private static int sNextAccessibilityViewId;
1501
1502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 * The animation currently associated with this view.
1504 * @hide
1505 */
1506 protected Animation mCurrentAnimation = null;
1507
1508 /**
1509 * Width as measured during measure pass.
1510 * {@hide}
1511 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001512 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001513 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514
1515 /**
1516 * Height as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001523 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1524 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1525 * its display list. This flag, used only when hw accelerated, allows us to clear the
1526 * flag while retaining this information until it's needed (at getDisplayList() time and
1527 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1528 *
1529 * {@hide}
1530 */
1531 boolean mRecreateDisplayList = false;
1532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * The view's identifier.
1535 * {@hide}
1536 *
1537 * @see #setId(int)
1538 * @see #getId()
1539 */
1540 @ViewDebug.ExportedProperty(resolveId = true)
1541 int mID = NO_ID;
1542
1543 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001544 * The stable ID of this view for accessibility porposes.
1545 */
1546 int mAccessibilityViewId = NO_ID;
1547
1548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 * The view's tag.
1550 * {@hide}
1551 *
1552 * @see #setTag(Object)
1553 * @see #getTag()
1554 */
1555 protected Object mTag;
1556
1557 // for mPrivateFlags:
1558 /** {@hide} */
1559 static final int WANTS_FOCUS = 0x00000001;
1560 /** {@hide} */
1561 static final int FOCUSED = 0x00000002;
1562 /** {@hide} */
1563 static final int SELECTED = 0x00000004;
1564 /** {@hide} */
1565 static final int IS_ROOT_NAMESPACE = 0x00000008;
1566 /** {@hide} */
1567 static final int HAS_BOUNDS = 0x00000010;
1568 /** {@hide} */
1569 static final int DRAWN = 0x00000020;
1570 /**
1571 * When this flag is set, this view is running an animation on behalf of its
1572 * children and should therefore not cancel invalidate requests, even if they
1573 * lie outside of this view's bounds.
1574 *
1575 * {@hide}
1576 */
1577 static final int DRAW_ANIMATION = 0x00000040;
1578 /** {@hide} */
1579 static final int SKIP_DRAW = 0x00000080;
1580 /** {@hide} */
1581 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1582 /** {@hide} */
1583 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1584 /** {@hide} */
1585 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1586 /** {@hide} */
1587 static final int MEASURED_DIMENSION_SET = 0x00000800;
1588 /** {@hide} */
1589 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001590 /** {@hide} */
1591 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592
1593 private static final int PRESSED = 0x00004000;
1594
1595 /** {@hide} */
1596 static final int DRAWING_CACHE_VALID = 0x00008000;
1597 /**
1598 * Flag used to indicate that this view should be drawn once more (and only once
1599 * more) after its animation has completed.
1600 * {@hide}
1601 */
1602 static final int ANIMATION_STARTED = 0x00010000;
1603
1604 private static final int SAVE_STATE_CALLED = 0x00020000;
1605
1606 /**
1607 * Indicates that the View returned true when onSetAlpha() was called and that
1608 * the alpha must be restored.
1609 * {@hide}
1610 */
1611 static final int ALPHA_SET = 0x00040000;
1612
1613 /**
1614 * Set by {@link #setScrollContainer(boolean)}.
1615 */
1616 static final int SCROLL_CONTAINER = 0x00080000;
1617
1618 /**
1619 * Set by {@link #setScrollContainer(boolean)}.
1620 */
1621 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1622
1623 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001624 * View flag indicating whether this view was invalidated (fully or partially.)
1625 *
1626 * @hide
1627 */
1628 static final int DIRTY = 0x00200000;
1629
1630 /**
1631 * View flag indicating whether this view was invalidated by an opaque
1632 * invalidate request.
1633 *
1634 * @hide
1635 */
1636 static final int DIRTY_OPAQUE = 0x00400000;
1637
1638 /**
1639 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_MASK = 0x00600000;
1644
1645 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001646 * Indicates whether the background is opaque.
1647 *
1648 * @hide
1649 */
1650 static final int OPAQUE_BACKGROUND = 0x00800000;
1651
1652 /**
1653 * Indicates whether the scrollbars are opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_SCROLLBARS = 0x01000000;
1658
1659 /**
1660 * Indicates whether the view is opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001665
Adam Powelle14579b2009-12-16 18:39:52 -08001666 /**
1667 * Indicates a prepressed state;
1668 * the short time between ACTION_DOWN and recognizing
1669 * a 'real' press. Prepressed is used to recognize quick taps
1670 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001671 *
Adam Powelle14579b2009-12-16 18:39:52 -08001672 * @hide
1673 */
1674 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001675
Adam Powellc9fbaab2010-02-16 17:16:19 -08001676 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001677 * Indicates whether the view is temporarily detached.
1678 *
1679 * @hide
1680 */
1681 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powell8568c3a2010-04-19 14:26:11 -07001683 /**
1684 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001685 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001686 * @hide
1687 */
1688 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001689
1690 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001691 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1692 * @hide
1693 */
1694 private static final int HOVERED = 0x10000000;
1695
1696 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001697 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1698 * for transform operations
1699 *
1700 * @hide
1701 */
Adam Powellf37df072010-09-17 16:22:49 -07001702 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001703
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001704 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001705 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001706
Chet Haasefd2b0022010-08-06 13:08:56 -07001707 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001708 * Indicates that this view was specifically invalidated, not just dirtied because some
1709 * child view was invalidated. The flag is used to determine when we need to recreate
1710 * a view's display list (as opposed to just returning a reference to its existing
1711 * display list).
1712 *
1713 * @hide
1714 */
1715 static final int INVALIDATED = 0x80000000;
1716
Christopher Tate3d4bf172011-03-28 16:16:46 -07001717 /* Masks for mPrivateFlags2 */
1718
1719 /**
1720 * Indicates that this view has reported that it can accept the current drag's content.
1721 * Cleared when the drag operation concludes.
1722 * @hide
1723 */
1724 static final int DRAG_CAN_ACCEPT = 0x00000001;
1725
1726 /**
1727 * Indicates that this view is currently directly under the drag location in a
1728 * drag-and-drop operation involving content that it can accept. Cleared when
1729 * the drag exits the view, or when the drag operation concludes.
1730 * @hide
1731 */
1732 static final int DRAG_HOVERED = 0x00000002;
1733
Cibu Johny86666632010-02-22 13:01:02 -08001734 /**
1735 * Indicates whether the view is drawn in right-to-left direction.
1736 *
1737 * @hide
1738 */
1739 static final int RESOLVED_LAYOUT_RTL = 0x00000004;
1740
Christopher Tate3d4bf172011-03-28 16:16:46 -07001741 /* End of masks for mPrivateFlags2 */
1742
1743 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1744
Chet Haasedaf98e92011-01-10 14:10:36 -08001745 /**
Adam Powell637d3372010-08-25 14:37:03 -07001746 * Always allow a user to over-scroll this view, provided it is a
1747 * view that can scroll.
1748 *
1749 * @see #getOverScrollMode()
1750 * @see #setOverScrollMode(int)
1751 */
1752 public static final int OVER_SCROLL_ALWAYS = 0;
1753
1754 /**
1755 * Allow a user to over-scroll this view only if the content is large
1756 * enough to meaningfully scroll, provided it is a view that can scroll.
1757 *
1758 * @see #getOverScrollMode()
1759 * @see #setOverScrollMode(int)
1760 */
1761 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1762
1763 /**
1764 * Never allow a user to over-scroll this view.
1765 *
1766 * @see #getOverScrollMode()
1767 * @see #setOverScrollMode(int)
1768 */
1769 public static final int OVER_SCROLL_NEVER = 2;
1770
1771 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001772 * View has requested the status bar to be visible (the default).
1773 *
Joe Malin32736f02011-01-19 16:14:20 -08001774 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001775 */
1776 public static final int STATUS_BAR_VISIBLE = 0;
1777
1778 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001779 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001780 *
Joe Malin32736f02011-01-19 16:14:20 -08001781 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001782 */
1783 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1784
1785 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001786 * @hide
1787 *
1788 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1789 * out of the public fields to keep the undefined bits out of the developer's way.
1790 *
1791 * Flag to make the status bar not expandable. Unless you also
1792 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1793 */
1794 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1795
1796 /**
1797 * @hide
1798 *
1799 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1800 * out of the public fields to keep the undefined bits out of the developer's way.
1801 *
1802 * Flag to hide notification icons and scrolling ticker text.
1803 */
1804 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1805
1806 /**
1807 * @hide
1808 *
1809 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1810 * out of the public fields to keep the undefined bits out of the developer's way.
1811 *
1812 * Flag to disable incoming notification alerts. This will not block
1813 * icons, but it will block sound, vibrating and other visual or aural notifications.
1814 */
1815 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1816
1817 /**
1818 * @hide
1819 *
1820 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1821 * out of the public fields to keep the undefined bits out of the developer's way.
1822 *
1823 * Flag to hide only the scrolling ticker. Note that
1824 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1825 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1826 */
1827 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1828
1829 /**
1830 * @hide
1831 *
1832 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1833 * out of the public fields to keep the undefined bits out of the developer's way.
1834 *
1835 * Flag to hide the center system info area.
1836 */
1837 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1838
1839 /**
1840 * @hide
1841 *
1842 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1843 * out of the public fields to keep the undefined bits out of the developer's way.
1844 *
1845 * Flag to hide only the navigation buttons. Don't use this
1846 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001847 *
1848 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001849 */
1850 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1851
1852 /**
1853 * @hide
1854 *
1855 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1856 * out of the public fields to keep the undefined bits out of the developer's way.
1857 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001858 * Flag to hide only the back button. Don't use this
1859 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1860 */
1861 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1862
1863 /**
1864 * @hide
1865 *
1866 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1867 * out of the public fields to keep the undefined bits out of the developer's way.
1868 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001869 * Flag to hide only the clock. You might use this if your activity has
1870 * its own clock making the status bar's clock redundant.
1871 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001872 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1873
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001874 /**
1875 * @hide
1876 */
1877 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001878
1879 /**
Adam Powell637d3372010-08-25 14:37:03 -07001880 * Controls the over-scroll mode for this view.
1881 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1882 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1883 * and {@link #OVER_SCROLL_NEVER}.
1884 */
1885 private int mOverScrollMode;
1886
1887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 * The parent this view is attached to.
1889 * {@hide}
1890 *
1891 * @see #getParent()
1892 */
1893 protected ViewParent mParent;
1894
1895 /**
1896 * {@hide}
1897 */
1898 AttachInfo mAttachInfo;
1899
1900 /**
1901 * {@hide}
1902 */
Romain Guy809a7f62009-05-14 15:44:42 -07001903 @ViewDebug.ExportedProperty(flagMapping = {
1904 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1905 name = "FORCE_LAYOUT"),
1906 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1907 name = "LAYOUT_REQUIRED"),
1908 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001909 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001910 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1911 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1912 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1913 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1914 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001916 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917
1918 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001919 * This view's request for the visibility of the status bar.
1920 * @hide
1921 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001922 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001923 int mSystemUiVisibility;
1924
1925 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 * Count of how many windows this view has been attached to.
1927 */
1928 int mWindowAttachCount;
1929
1930 /**
1931 * The layout parameters associated with this view and used by the parent
1932 * {@link android.view.ViewGroup} to determine how this view should be
1933 * laid out.
1934 * {@hide}
1935 */
1936 protected ViewGroup.LayoutParams mLayoutParams;
1937
1938 /**
1939 * The view flags hold various views states.
1940 * {@hide}
1941 */
1942 @ViewDebug.ExportedProperty
1943 int mViewFlags;
1944
1945 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001946 * The transform matrix for the View. This transform is calculated internally
1947 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1948 * is used by default. Do *not* use this variable directly; instead call
1949 * getMatrix(), which will automatically recalculate the matrix if necessary
1950 * to get the correct matrix based on the latest rotation and scale properties.
1951 */
1952 private final Matrix mMatrix = new Matrix();
1953
1954 /**
1955 * The transform matrix for the View. This transform is calculated internally
1956 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1957 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001958 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * to get the correct matrix based on the latest rotation and scale properties.
1960 */
1961 private Matrix mInverseMatrix;
1962
1963 /**
1964 * An internal variable that tracks whether we need to recalculate the
1965 * transform matrix, based on whether the rotation or scaleX/Y properties
1966 * have changed since the matrix was last calculated.
1967 */
Chet Haasea00f3862011-02-22 06:34:40 -08001968 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001969
1970 /**
1971 * An internal variable that tracks whether we need to recalculate the
1972 * transform matrix, based on whether the rotation or scaleX/Y properties
1973 * have changed since the matrix was last calculated.
1974 */
1975 private boolean mInverseMatrixDirty = true;
1976
1977 /**
1978 * A variable that tracks whether we need to recalculate the
1979 * transform matrix, based on whether the rotation or scaleX/Y properties
1980 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001981 * is only valid after a call to updateMatrix() or to a function that
1982 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001983 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001984 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001985
1986 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001987 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1988 */
1989 private Camera mCamera = null;
1990
1991 /**
1992 * This matrix is used when computing the matrix for 3D rotations.
1993 */
1994 private Matrix matrix3D = null;
1995
1996 /**
1997 * These prev values are used to recalculate a centered pivot point when necessary. The
1998 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1999 * set), so thes values are only used then as well.
2000 */
2001 private int mPrevWidth = -1;
2002 private int mPrevHeight = -1;
2003
Joe Malin32736f02011-01-19 16:14:20 -08002004 private boolean mLastIsOpaque;
2005
Chet Haasefd2b0022010-08-06 13:08:56 -07002006 /**
2007 * Convenience value to check for float values that are close enough to zero to be considered
2008 * zero.
2009 */
Romain Guy2542d192010-08-18 11:47:12 -07002010 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002011
2012 /**
2013 * The degrees rotation around the vertical axis through the pivot point.
2014 */
2015 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002016 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002017
2018 /**
2019 * The degrees rotation around the horizontal axis through the pivot point.
2020 */
2021 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002022 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002023
2024 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002025 * The degrees rotation around the pivot point.
2026 */
2027 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002028 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002029
2030 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002031 * The amount of translation of the object away from its left property (post-layout).
2032 */
2033 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002034 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002035
2036 /**
2037 * The amount of translation of the object away from its top property (post-layout).
2038 */
2039 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002040 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002041
2042 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002043 * The amount of scale in the x direction around the pivot point. A
2044 * value of 1 means no scaling is applied.
2045 */
2046 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002047 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002048
2049 /**
2050 * The amount of scale in the y direction around the pivot point. A
2051 * value of 1 means no scaling is applied.
2052 */
2053 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002054 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002055
2056 /**
2057 * The amount of scale in the x direction around the pivot point. A
2058 * value of 1 means no scaling is applied.
2059 */
2060 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002061 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002062
2063 /**
2064 * The amount of scale in the y direction around the pivot point. A
2065 * value of 1 means no scaling is applied.
2066 */
2067 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002068 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002069
2070 /**
2071 * The opacity of the View. This is a value from 0 to 1, where 0 means
2072 * completely transparent and 1 means completely opaque.
2073 */
2074 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002075 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002076
2077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 * The distance in pixels from the left edge of this view's parent
2079 * to the left edge of this view.
2080 * {@hide}
2081 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002082 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 protected int mLeft;
2084 /**
2085 * The distance in pixels from the left edge of this view's parent
2086 * to the right edge of this view.
2087 * {@hide}
2088 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002089 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 protected int mRight;
2091 /**
2092 * The distance in pixels from the top edge of this view's parent
2093 * to the top edge of this view.
2094 * {@hide}
2095 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002096 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 protected int mTop;
2098 /**
2099 * The distance in pixels from the top edge of this view's parent
2100 * to the bottom edge of this view.
2101 * {@hide}
2102 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002103 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 protected int mBottom;
2105
2106 /**
2107 * The offset, in pixels, by which the content of this view is scrolled
2108 * horizontally.
2109 * {@hide}
2110 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002111 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 protected int mScrollX;
2113 /**
2114 * The offset, in pixels, by which the content of this view is scrolled
2115 * vertically.
2116 * {@hide}
2117 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002118 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 protected int mScrollY;
2120
2121 /**
2122 * The left padding in pixels, that is the distance in pixels between the
2123 * left edge of this view and the left edge of its content.
2124 * {@hide}
2125 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002126 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 protected int mPaddingLeft;
2128 /**
2129 * The right padding in pixels, that is the distance in pixels between the
2130 * right edge of this view and the right edge of its content.
2131 * {@hide}
2132 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002133 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 protected int mPaddingRight;
2135 /**
2136 * The top padding in pixels, that is the distance in pixels between the
2137 * top edge of this view and the top edge of its content.
2138 * {@hide}
2139 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002140 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 protected int mPaddingTop;
2142 /**
2143 * The bottom padding in pixels, that is the distance in pixels between the
2144 * bottom edge of this view and the bottom edge of its content.
2145 * {@hide}
2146 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002147 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 protected int mPaddingBottom;
2149
2150 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002151 * Briefly describes the view and is primarily used for accessibility support.
2152 */
2153 private CharSequence mContentDescription;
2154
2155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 * Cache the paddingRight set by the user to append to the scrollbar's size.
2157 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002158 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 int mUserPaddingRight;
2160
2161 /**
2162 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2163 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002164 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 int mUserPaddingBottom;
2166
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002167 /**
Adam Powell20232d02010-12-08 21:08:53 -08002168 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2169 */
2170 @ViewDebug.ExportedProperty(category = "padding")
2171 int mUserPaddingLeft;
2172
2173 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002174 * @hide
2175 */
2176 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2177 /**
2178 * @hide
2179 */
2180 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181
2182 private Resources mResources = null;
2183
2184 private Drawable mBGDrawable;
2185
2186 private int mBackgroundResource;
2187 private boolean mBackgroundSizeChanged;
2188
2189 /**
2190 * Listener used to dispatch focus change events.
2191 * This field should be made private, so it is hidden from the SDK.
2192 * {@hide}
2193 */
2194 protected OnFocusChangeListener mOnFocusChangeListener;
2195
2196 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002197 * Listeners for layout change events.
2198 */
2199 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2200
2201 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002202 * Listeners for attach events.
2203 */
2204 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2205
2206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * Listener used to dispatch click events.
2208 * This field should be made private, so it is hidden from the SDK.
2209 * {@hide}
2210 */
2211 protected OnClickListener mOnClickListener;
2212
2213 /**
2214 * Listener used to dispatch long click events.
2215 * This field should be made private, so it is hidden from the SDK.
2216 * {@hide}
2217 */
2218 protected OnLongClickListener mOnLongClickListener;
2219
2220 /**
2221 * Listener used to build the context menu.
2222 * This field should be made private, so it is hidden from the SDK.
2223 * {@hide}
2224 */
2225 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2226
2227 private OnKeyListener mOnKeyListener;
2228
2229 private OnTouchListener mOnTouchListener;
2230
Jeff Brown33bbfd22011-02-24 20:55:35 -08002231 private OnGenericMotionListener mOnGenericMotionListener;
2232
Chris Tate32affef2010-10-18 15:29:21 -07002233 private OnDragListener mOnDragListener;
2234
Joe Onorato664644d2011-01-23 17:53:23 -08002235 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 /**
2238 * The application environment this view lives in.
2239 * This field should be made private, so it is hidden from the SDK.
2240 * {@hide}
2241 */
2242 protected Context mContext;
2243
2244 private ScrollabilityCache mScrollCache;
2245
2246 private int[] mDrawableState = null;
2247
Romain Guy0211a0a2011-02-14 16:34:59 -08002248 /**
2249 * Set to true when drawing cache is enabled and cannot be created.
2250 *
2251 * @hide
2252 */
2253 public boolean mCachingFailed;
2254
Romain Guy02890fd2010-08-06 17:58:44 -07002255 private Bitmap mDrawingCache;
2256 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002257 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002258 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259
2260 /**
2261 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2262 * the user may specify which view to go to next.
2263 */
2264 private int mNextFocusLeftId = View.NO_ID;
2265
2266 /**
2267 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2268 * the user may specify which view to go to next.
2269 */
2270 private int mNextFocusRightId = View.NO_ID;
2271
2272 /**
2273 * When this view has focus and the next focus is {@link #FOCUS_UP},
2274 * the user may specify which view to go to next.
2275 */
2276 private int mNextFocusUpId = View.NO_ID;
2277
2278 /**
2279 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2280 * the user may specify which view to go to next.
2281 */
2282 private int mNextFocusDownId = View.NO_ID;
2283
Jeff Brown4e6319b2010-12-13 10:36:51 -08002284 /**
2285 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2286 * the user may specify which view to go to next.
2287 */
2288 int mNextFocusForwardId = View.NO_ID;
2289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002291 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002292 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 private UnsetPressedState mUnsetPressedState;
2295
2296 /**
2297 * Whether the long press's action has been invoked. The tap's action is invoked on the
2298 * up event while a long press is invoked as soon as the long press duration is reached, so
2299 * a long press could be performed before the tap is checked, in which case the tap's action
2300 * should not be invoked.
2301 */
2302 private boolean mHasPerformedLongPress;
2303
2304 /**
2305 * The minimum height of the view. We'll try our best to have the height
2306 * of this view to at least this amount.
2307 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002308 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 private int mMinHeight;
2310
2311 /**
2312 * The minimum width of the view. We'll try our best to have the width
2313 * of this view to at least this amount.
2314 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002315 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 private int mMinWidth;
2317
2318 /**
2319 * The delegate to handle touch events that are physically in this view
2320 * but should be handled by another view.
2321 */
2322 private TouchDelegate mTouchDelegate = null;
2323
2324 /**
2325 * Solid color to use as a background when creating the drawing cache. Enables
2326 * the cache to use 16 bit bitmaps instead of 32 bit.
2327 */
2328 private int mDrawingCacheBackgroundColor = 0;
2329
2330 /**
2331 * Special tree observer used when mAttachInfo is null.
2332 */
2333 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002334
Adam Powelle14579b2009-12-16 18:39:52 -08002335 /**
2336 * Cache the touch slop from the context that created the view.
2337 */
2338 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002341 * Object that handles automatic animation of view properties.
2342 */
2343 private ViewPropertyAnimator mAnimator = null;
2344
2345 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002346 * Flag indicating that a drag can cross window boundaries. When
2347 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2348 * with this flag set, all visible applications will be able to participate
2349 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002350 *
2351 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002352 */
2353 public static final int DRAG_FLAG_GLOBAL = 1;
2354
2355 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002356 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2357 */
2358 private float mVerticalScrollFactor;
2359
2360 /**
Adam Powell20232d02010-12-08 21:08:53 -08002361 * Position of the vertical scroll bar.
2362 */
2363 private int mVerticalScrollbarPosition;
2364
2365 /**
2366 * Position the scroll bar at the default position as determined by the system.
2367 */
2368 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2369
2370 /**
2371 * Position the scroll bar along the left edge.
2372 */
2373 public static final int SCROLLBAR_POSITION_LEFT = 1;
2374
2375 /**
2376 * Position the scroll bar along the right edge.
2377 */
2378 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2379
2380 /**
Romain Guy171c5922011-01-06 10:04:23 -08002381 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002382 *
2383 * @see #getLayerType()
2384 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002385 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002386 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002387 */
2388 public static final int LAYER_TYPE_NONE = 0;
2389
2390 /**
2391 * <p>Indicates that the view has a software layer. A software layer is backed
2392 * by a bitmap and causes the view to be rendered using Android's software
2393 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002394 *
Romain Guy171c5922011-01-06 10:04:23 -08002395 * <p>Software layers have various usages:</p>
2396 * <p>When the application is not using hardware acceleration, a software layer
2397 * is useful to apply a specific color filter and/or blending mode and/or
2398 * translucency to a view and all its children.</p>
2399 * <p>When the application is using hardware acceleration, a software layer
2400 * is useful to render drawing primitives not supported by the hardware
2401 * accelerated pipeline. It can also be used to cache a complex view tree
2402 * into a texture and reduce the complexity of drawing operations. For instance,
2403 * when animating a complex view tree with a translation, a software layer can
2404 * be used to render the view tree only once.</p>
2405 * <p>Software layers should be avoided when the affected view tree updates
2406 * often. Every update will require to re-render the software layer, which can
2407 * potentially be slow (particularly when hardware acceleration is turned on
2408 * since the layer will have to be uploaded into a hardware texture after every
2409 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002410 *
2411 * @see #getLayerType()
2412 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002413 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002414 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002415 */
2416 public static final int LAYER_TYPE_SOFTWARE = 1;
2417
2418 /**
2419 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2420 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2421 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2422 * rendering pipeline, but only if hardware acceleration is turned on for the
2423 * view hierarchy. When hardware acceleration is turned off, hardware layers
2424 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002425 *
Romain Guy171c5922011-01-06 10:04:23 -08002426 * <p>A hardware layer is useful to apply a specific color filter and/or
2427 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002428 * <p>A hardware layer can be used to cache a complex view tree into a
2429 * texture and reduce the complexity of drawing operations. For instance,
2430 * when animating a complex view tree with a translation, a hardware layer can
2431 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002432 * <p>A hardware layer can also be used to increase the rendering quality when
2433 * rotation transformations are applied on a view. It can also be used to
2434 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002435 *
2436 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002437 * @see #setLayerType(int, android.graphics.Paint)
2438 * @see #LAYER_TYPE_NONE
2439 * @see #LAYER_TYPE_SOFTWARE
2440 */
2441 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002442
Romain Guy3aaff3a2011-01-12 14:18:47 -08002443 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2444 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2445 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2446 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2447 })
Romain Guy171c5922011-01-06 10:04:23 -08002448 int mLayerType = LAYER_TYPE_NONE;
2449 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002450 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002451
2452 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002453 * Consistency verifier for debugging purposes.
2454 * @hide
2455 */
2456 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2457 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2458 new InputEventConsistencyVerifier(this, 0) : null;
2459
2460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 * Simple constructor to use when creating a view from code.
2462 *
2463 * @param context The Context the view is running in, through which it can
2464 * access the current theme, resources, etc.
2465 */
2466 public View(Context context) {
2467 mContext = context;
2468 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002469 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002470 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002471 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
2473
2474 /**
2475 * Constructor that is called when inflating a view from XML. This is called
2476 * when a view is being constructed from an XML file, supplying attributes
2477 * that were specified in the XML file. This version uses a default style of
2478 * 0, so the only attribute values applied are those in the Context's Theme
2479 * and the given AttributeSet.
2480 *
2481 * <p>
2482 * The method onFinishInflate() will be called after all children have been
2483 * added.
2484 *
2485 * @param context The Context the view is running in, through which it can
2486 * access the current theme, resources, etc.
2487 * @param attrs The attributes of the XML tag that is inflating the view.
2488 * @see #View(Context, AttributeSet, int)
2489 */
2490 public View(Context context, AttributeSet attrs) {
2491 this(context, attrs, 0);
2492 }
2493
2494 /**
2495 * Perform inflation from XML and apply a class-specific base style. This
2496 * constructor of View allows subclasses to use their own base style when
2497 * they are inflating. For example, a Button class's constructor would call
2498 * this version of the super class constructor and supply
2499 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2500 * the theme's button style to modify all of the base view attributes (in
2501 * particular its background) as well as the Button class's attributes.
2502 *
2503 * @param context The Context the view is running in, through which it can
2504 * access the current theme, resources, etc.
2505 * @param attrs The attributes of the XML tag that is inflating the view.
2506 * @param defStyle The default style to apply to this view. If 0, no style
2507 * will be applied (beyond what is included in the theme). This may
2508 * either be an attribute resource, whose value will be retrieved
2509 * from the current theme, or an explicit style resource.
2510 * @see #View(Context, AttributeSet)
2511 */
2512 public View(Context context, AttributeSet attrs, int defStyle) {
2513 this(context);
2514
2515 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2516 defStyle, 0);
2517
2518 Drawable background = null;
2519
2520 int leftPadding = -1;
2521 int topPadding = -1;
2522 int rightPadding = -1;
2523 int bottomPadding = -1;
2524
2525 int padding = -1;
2526
2527 int viewFlagValues = 0;
2528 int viewFlagMasks = 0;
2529
2530 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 int x = 0;
2533 int y = 0;
2534
Chet Haase73066682010-11-29 15:55:32 -08002535 float tx = 0;
2536 float ty = 0;
2537 float rotation = 0;
2538 float rotationX = 0;
2539 float rotationY = 0;
2540 float sx = 1f;
2541 float sy = 1f;
2542 boolean transformSet = false;
2543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2545
Adam Powell637d3372010-08-25 14:37:03 -07002546 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 final int N = a.getIndexCount();
2548 for (int i = 0; i < N; i++) {
2549 int attr = a.getIndex(i);
2550 switch (attr) {
2551 case com.android.internal.R.styleable.View_background:
2552 background = a.getDrawable(attr);
2553 break;
2554 case com.android.internal.R.styleable.View_padding:
2555 padding = a.getDimensionPixelSize(attr, -1);
2556 break;
2557 case com.android.internal.R.styleable.View_paddingLeft:
2558 leftPadding = a.getDimensionPixelSize(attr, -1);
2559 break;
2560 case com.android.internal.R.styleable.View_paddingTop:
2561 topPadding = a.getDimensionPixelSize(attr, -1);
2562 break;
2563 case com.android.internal.R.styleable.View_paddingRight:
2564 rightPadding = a.getDimensionPixelSize(attr, -1);
2565 break;
2566 case com.android.internal.R.styleable.View_paddingBottom:
2567 bottomPadding = a.getDimensionPixelSize(attr, -1);
2568 break;
2569 case com.android.internal.R.styleable.View_scrollX:
2570 x = a.getDimensionPixelOffset(attr, 0);
2571 break;
2572 case com.android.internal.R.styleable.View_scrollY:
2573 y = a.getDimensionPixelOffset(attr, 0);
2574 break;
Chet Haase73066682010-11-29 15:55:32 -08002575 case com.android.internal.R.styleable.View_alpha:
2576 setAlpha(a.getFloat(attr, 1f));
2577 break;
2578 case com.android.internal.R.styleable.View_transformPivotX:
2579 setPivotX(a.getDimensionPixelOffset(attr, 0));
2580 break;
2581 case com.android.internal.R.styleable.View_transformPivotY:
2582 setPivotY(a.getDimensionPixelOffset(attr, 0));
2583 break;
2584 case com.android.internal.R.styleable.View_translationX:
2585 tx = a.getDimensionPixelOffset(attr, 0);
2586 transformSet = true;
2587 break;
2588 case com.android.internal.R.styleable.View_translationY:
2589 ty = a.getDimensionPixelOffset(attr, 0);
2590 transformSet = true;
2591 break;
2592 case com.android.internal.R.styleable.View_rotation:
2593 rotation = a.getFloat(attr, 0);
2594 transformSet = true;
2595 break;
2596 case com.android.internal.R.styleable.View_rotationX:
2597 rotationX = a.getFloat(attr, 0);
2598 transformSet = true;
2599 break;
2600 case com.android.internal.R.styleable.View_rotationY:
2601 rotationY = a.getFloat(attr, 0);
2602 transformSet = true;
2603 break;
2604 case com.android.internal.R.styleable.View_scaleX:
2605 sx = a.getFloat(attr, 1f);
2606 transformSet = true;
2607 break;
2608 case com.android.internal.R.styleable.View_scaleY:
2609 sy = a.getFloat(attr, 1f);
2610 transformSet = true;
2611 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 case com.android.internal.R.styleable.View_id:
2613 mID = a.getResourceId(attr, NO_ID);
2614 break;
2615 case com.android.internal.R.styleable.View_tag:
2616 mTag = a.getText(attr);
2617 break;
2618 case com.android.internal.R.styleable.View_fitsSystemWindows:
2619 if (a.getBoolean(attr, false)) {
2620 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2621 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2622 }
2623 break;
2624 case com.android.internal.R.styleable.View_focusable:
2625 if (a.getBoolean(attr, false)) {
2626 viewFlagValues |= FOCUSABLE;
2627 viewFlagMasks |= FOCUSABLE_MASK;
2628 }
2629 break;
2630 case com.android.internal.R.styleable.View_focusableInTouchMode:
2631 if (a.getBoolean(attr, false)) {
2632 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2633 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2634 }
2635 break;
2636 case com.android.internal.R.styleable.View_clickable:
2637 if (a.getBoolean(attr, false)) {
2638 viewFlagValues |= CLICKABLE;
2639 viewFlagMasks |= CLICKABLE;
2640 }
2641 break;
2642 case com.android.internal.R.styleable.View_longClickable:
2643 if (a.getBoolean(attr, false)) {
2644 viewFlagValues |= LONG_CLICKABLE;
2645 viewFlagMasks |= LONG_CLICKABLE;
2646 }
2647 break;
2648 case com.android.internal.R.styleable.View_saveEnabled:
2649 if (!a.getBoolean(attr, true)) {
2650 viewFlagValues |= SAVE_DISABLED;
2651 viewFlagMasks |= SAVE_DISABLED_MASK;
2652 }
2653 break;
2654 case com.android.internal.R.styleable.View_duplicateParentState:
2655 if (a.getBoolean(attr, false)) {
2656 viewFlagValues |= DUPLICATE_PARENT_STATE;
2657 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2658 }
2659 break;
2660 case com.android.internal.R.styleable.View_visibility:
2661 final int visibility = a.getInt(attr, 0);
2662 if (visibility != 0) {
2663 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2664 viewFlagMasks |= VISIBILITY_MASK;
2665 }
2666 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002667 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002668 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002669 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002670 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002671 final int layoutDirection = a.getInt(attr, -1);
2672 if (layoutDirection != -1) {
2673 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002674 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002675 // Set to default (LAYOUT_DIRECTION_INHERIT)
2676 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002677 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002678 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002679 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 case com.android.internal.R.styleable.View_drawingCacheQuality:
2681 final int cacheQuality = a.getInt(attr, 0);
2682 if (cacheQuality != 0) {
2683 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2684 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2685 }
2686 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002687 case com.android.internal.R.styleable.View_contentDescription:
2688 mContentDescription = a.getString(attr);
2689 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2691 if (!a.getBoolean(attr, true)) {
2692 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2693 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2694 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002695 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2697 if (!a.getBoolean(attr, true)) {
2698 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2699 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2700 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002701 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 case R.styleable.View_scrollbars:
2703 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2704 if (scrollbars != SCROLLBARS_NONE) {
2705 viewFlagValues |= scrollbars;
2706 viewFlagMasks |= SCROLLBARS_MASK;
2707 initializeScrollbars(a);
2708 }
2709 break;
2710 case R.styleable.View_fadingEdge:
2711 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2712 if (fadingEdge != FADING_EDGE_NONE) {
2713 viewFlagValues |= fadingEdge;
2714 viewFlagMasks |= FADING_EDGE_MASK;
2715 initializeFadingEdge(a);
2716 }
2717 break;
2718 case R.styleable.View_scrollbarStyle:
2719 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2720 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2721 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2722 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2723 }
2724 break;
2725 case R.styleable.View_isScrollContainer:
2726 setScrollContainer = true;
2727 if (a.getBoolean(attr, false)) {
2728 setScrollContainer(true);
2729 }
2730 break;
2731 case com.android.internal.R.styleable.View_keepScreenOn:
2732 if (a.getBoolean(attr, false)) {
2733 viewFlagValues |= KEEP_SCREEN_ON;
2734 viewFlagMasks |= KEEP_SCREEN_ON;
2735 }
2736 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002737 case R.styleable.View_filterTouchesWhenObscured:
2738 if (a.getBoolean(attr, false)) {
2739 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2740 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2741 }
2742 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 case R.styleable.View_nextFocusLeft:
2744 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2745 break;
2746 case R.styleable.View_nextFocusRight:
2747 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2748 break;
2749 case R.styleable.View_nextFocusUp:
2750 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2751 break;
2752 case R.styleable.View_nextFocusDown:
2753 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2754 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002755 case R.styleable.View_nextFocusForward:
2756 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2757 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 case R.styleable.View_minWidth:
2759 mMinWidth = a.getDimensionPixelSize(attr, 0);
2760 break;
2761 case R.styleable.View_minHeight:
2762 mMinHeight = a.getDimensionPixelSize(attr, 0);
2763 break;
Romain Guy9a817362009-05-01 10:57:14 -07002764 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002765 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002766 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002767 + "be used within a restricted context");
2768 }
2769
Romain Guy9a817362009-05-01 10:57:14 -07002770 final String handlerName = a.getString(attr);
2771 if (handlerName != null) {
2772 setOnClickListener(new OnClickListener() {
2773 private Method mHandler;
2774
2775 public void onClick(View v) {
2776 if (mHandler == null) {
2777 try {
2778 mHandler = getContext().getClass().getMethod(handlerName,
2779 View.class);
2780 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002781 int id = getId();
2782 String idText = id == NO_ID ? "" : " with id '"
2783 + getContext().getResources().getResourceEntryName(
2784 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002785 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002786 handlerName + "(View) in the activity "
2787 + getContext().getClass() + " for onClick handler"
2788 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002789 }
2790 }
2791
2792 try {
2793 mHandler.invoke(getContext(), View.this);
2794 } catch (IllegalAccessException e) {
2795 throw new IllegalStateException("Could not execute non "
2796 + "public method of the activity", e);
2797 } catch (InvocationTargetException e) {
2798 throw new IllegalStateException("Could not execute "
2799 + "method of the activity", e);
2800 }
2801 }
2802 });
2803 }
2804 break;
Adam Powell637d3372010-08-25 14:37:03 -07002805 case R.styleable.View_overScrollMode:
2806 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2807 break;
Adam Powell20232d02010-12-08 21:08:53 -08002808 case R.styleable.View_verticalScrollbarPosition:
2809 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2810 break;
Romain Guy171c5922011-01-06 10:04:23 -08002811 case R.styleable.View_layerType:
2812 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2813 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 }
2815 }
2816
Adam Powell637d3372010-08-25 14:37:03 -07002817 setOverScrollMode(overScrollMode);
2818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 if (background != null) {
2820 setBackgroundDrawable(background);
2821 }
2822
2823 if (padding >= 0) {
2824 leftPadding = padding;
2825 topPadding = padding;
2826 rightPadding = padding;
2827 bottomPadding = padding;
2828 }
2829
2830 // If the user specified the padding (either with android:padding or
2831 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2832 // use the default padding or the padding from the background drawable
2833 // (stored at this point in mPadding*)
2834 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2835 topPadding >= 0 ? topPadding : mPaddingTop,
2836 rightPadding >= 0 ? rightPadding : mPaddingRight,
2837 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2838
2839 if (viewFlagMasks != 0) {
2840 setFlags(viewFlagValues, viewFlagMasks);
2841 }
2842
2843 // Needs to be called after mViewFlags is set
2844 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2845 recomputePadding();
2846 }
2847
2848 if (x != 0 || y != 0) {
2849 scrollTo(x, y);
2850 }
2851
Chet Haase73066682010-11-29 15:55:32 -08002852 if (transformSet) {
2853 setTranslationX(tx);
2854 setTranslationY(ty);
2855 setRotation(rotation);
2856 setRotationX(rotationX);
2857 setRotationY(rotationY);
2858 setScaleX(sx);
2859 setScaleY(sy);
2860 }
2861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2863 setScrollContainer(true);
2864 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002865
2866 computeOpaqueFlags();
2867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 a.recycle();
2869 }
2870
2871 /**
2872 * Non-public constructor for use in testing
2873 */
2874 View() {
2875 }
2876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 /**
2878 * <p>
2879 * Initializes the fading edges from a given set of styled attributes. This
2880 * method should be called by subclasses that need fading edges and when an
2881 * instance of these subclasses is created programmatically rather than
2882 * being inflated from XML. This method is automatically called when the XML
2883 * is inflated.
2884 * </p>
2885 *
2886 * @param a the styled attributes set to initialize the fading edges from
2887 */
2888 protected void initializeFadingEdge(TypedArray a) {
2889 initScrollCache();
2890
2891 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2892 R.styleable.View_fadingEdgeLength,
2893 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2894 }
2895
2896 /**
2897 * Returns the size of the vertical faded edges used to indicate that more
2898 * content in this view is visible.
2899 *
2900 * @return The size in pixels of the vertical faded edge or 0 if vertical
2901 * faded edges are not enabled for this view.
2902 * @attr ref android.R.styleable#View_fadingEdgeLength
2903 */
2904 public int getVerticalFadingEdgeLength() {
2905 if (isVerticalFadingEdgeEnabled()) {
2906 ScrollabilityCache cache = mScrollCache;
2907 if (cache != null) {
2908 return cache.fadingEdgeLength;
2909 }
2910 }
2911 return 0;
2912 }
2913
2914 /**
2915 * Set the size of the faded edge used to indicate that more content in this
2916 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002917 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2918 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2919 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 *
2921 * @param length The size in pixels of the faded edge used to indicate that more
2922 * content in this view is visible.
2923 */
2924 public void setFadingEdgeLength(int length) {
2925 initScrollCache();
2926 mScrollCache.fadingEdgeLength = length;
2927 }
2928
2929 /**
2930 * Returns the size of the horizontal faded edges used to indicate that more
2931 * content in this view is visible.
2932 *
2933 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2934 * faded edges are not enabled for this view.
2935 * @attr ref android.R.styleable#View_fadingEdgeLength
2936 */
2937 public int getHorizontalFadingEdgeLength() {
2938 if (isHorizontalFadingEdgeEnabled()) {
2939 ScrollabilityCache cache = mScrollCache;
2940 if (cache != null) {
2941 return cache.fadingEdgeLength;
2942 }
2943 }
2944 return 0;
2945 }
2946
2947 /**
2948 * Returns the width of the vertical scrollbar.
2949 *
2950 * @return The width in pixels of the vertical scrollbar or 0 if there
2951 * is no vertical scrollbar.
2952 */
2953 public int getVerticalScrollbarWidth() {
2954 ScrollabilityCache cache = mScrollCache;
2955 if (cache != null) {
2956 ScrollBarDrawable scrollBar = cache.scrollBar;
2957 if (scrollBar != null) {
2958 int size = scrollBar.getSize(true);
2959 if (size <= 0) {
2960 size = cache.scrollBarSize;
2961 }
2962 return size;
2963 }
2964 return 0;
2965 }
2966 return 0;
2967 }
2968
2969 /**
2970 * Returns the height of the horizontal scrollbar.
2971 *
2972 * @return The height in pixels of the horizontal scrollbar or 0 if
2973 * there is no horizontal scrollbar.
2974 */
2975 protected int getHorizontalScrollbarHeight() {
2976 ScrollabilityCache cache = mScrollCache;
2977 if (cache != null) {
2978 ScrollBarDrawable scrollBar = cache.scrollBar;
2979 if (scrollBar != null) {
2980 int size = scrollBar.getSize(false);
2981 if (size <= 0) {
2982 size = cache.scrollBarSize;
2983 }
2984 return size;
2985 }
2986 return 0;
2987 }
2988 return 0;
2989 }
2990
2991 /**
2992 * <p>
2993 * Initializes the scrollbars from a given set of styled attributes. This
2994 * method should be called by subclasses that need scrollbars and when an
2995 * instance of these subclasses is created programmatically rather than
2996 * being inflated from XML. This method is automatically called when the XML
2997 * is inflated.
2998 * </p>
2999 *
3000 * @param a the styled attributes set to initialize the scrollbars from
3001 */
3002 protected void initializeScrollbars(TypedArray a) {
3003 initScrollCache();
3004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003006
Mike Cleronf116bf82009-09-27 19:14:12 -07003007 if (scrollabilityCache.scrollBar == null) {
3008 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3009 }
Joe Malin32736f02011-01-19 16:14:20 -08003010
Romain Guy8bda2482010-03-02 11:42:11 -08003011 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012
Mike Cleronf116bf82009-09-27 19:14:12 -07003013 if (!fadeScrollbars) {
3014 scrollabilityCache.state = ScrollabilityCache.ON;
3015 }
3016 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003017
3018
Mike Cleronf116bf82009-09-27 19:14:12 -07003019 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3020 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3021 .getScrollBarFadeDuration());
3022 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3023 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3024 ViewConfiguration.getScrollDefaultDelay());
3025
Joe Malin32736f02011-01-19 16:14:20 -08003026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3028 com.android.internal.R.styleable.View_scrollbarSize,
3029 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3030
3031 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3032 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3033
3034 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3035 if (thumb != null) {
3036 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3037 }
3038
3039 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3040 false);
3041 if (alwaysDraw) {
3042 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3043 }
3044
3045 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3046 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3047
3048 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3049 if (thumb != null) {
3050 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3051 }
3052
3053 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3054 false);
3055 if (alwaysDraw) {
3056 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3057 }
3058
3059 // Re-apply user/background padding so that scrollbar(s) get added
3060 recomputePadding();
3061 }
3062
3063 /**
3064 * <p>
3065 * Initalizes the scrollability cache if necessary.
3066 * </p>
3067 */
3068 private void initScrollCache() {
3069 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003070 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
3072 }
3073
3074 /**
Adam Powell20232d02010-12-08 21:08:53 -08003075 * Set the position of the vertical scroll bar. Should be one of
3076 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3077 * {@link #SCROLLBAR_POSITION_RIGHT}.
3078 *
3079 * @param position Where the vertical scroll bar should be positioned.
3080 */
3081 public void setVerticalScrollbarPosition(int position) {
3082 if (mVerticalScrollbarPosition != position) {
3083 mVerticalScrollbarPosition = position;
3084 computeOpaqueFlags();
3085 recomputePadding();
3086 }
3087 }
3088
3089 /**
3090 * @return The position where the vertical scroll bar will show, if applicable.
3091 * @see #setVerticalScrollbarPosition(int)
3092 */
3093 public int getVerticalScrollbarPosition() {
3094 return mVerticalScrollbarPosition;
3095 }
3096
3097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 * Register a callback to be invoked when focus of this view changed.
3099 *
3100 * @param l The callback that will run.
3101 */
3102 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3103 mOnFocusChangeListener = l;
3104 }
3105
3106 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003107 * Add a listener that will be called when the bounds of the view change due to
3108 * layout processing.
3109 *
3110 * @param listener The listener that will be called when layout bounds change.
3111 */
3112 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3113 if (mOnLayoutChangeListeners == null) {
3114 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3115 }
3116 mOnLayoutChangeListeners.add(listener);
3117 }
3118
3119 /**
3120 * Remove a listener for layout changes.
3121 *
3122 * @param listener The listener for layout bounds change.
3123 */
3124 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3125 if (mOnLayoutChangeListeners == null) {
3126 return;
3127 }
3128 mOnLayoutChangeListeners.remove(listener);
3129 }
3130
3131 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003132 * Add a listener for attach state changes.
3133 *
3134 * This listener will be called whenever this view is attached or detached
3135 * from a window. Remove the listener using
3136 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3137 *
3138 * @param listener Listener to attach
3139 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3140 */
3141 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3142 if (mOnAttachStateChangeListeners == null) {
3143 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3144 }
3145 mOnAttachStateChangeListeners.add(listener);
3146 }
3147
3148 /**
3149 * Remove a listener for attach state changes. The listener will receive no further
3150 * notification of window attach/detach events.
3151 *
3152 * @param listener Listener to remove
3153 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3154 */
3155 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3156 if (mOnAttachStateChangeListeners == null) {
3157 return;
3158 }
3159 mOnAttachStateChangeListeners.remove(listener);
3160 }
3161
3162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 * Returns the focus-change callback registered for this view.
3164 *
3165 * @return The callback, or null if one is not registered.
3166 */
3167 public OnFocusChangeListener getOnFocusChangeListener() {
3168 return mOnFocusChangeListener;
3169 }
3170
3171 /**
3172 * Register a callback to be invoked when this view is clicked. If this view is not
3173 * clickable, it becomes clickable.
3174 *
3175 * @param l The callback that will run
3176 *
3177 * @see #setClickable(boolean)
3178 */
3179 public void setOnClickListener(OnClickListener l) {
3180 if (!isClickable()) {
3181 setClickable(true);
3182 }
3183 mOnClickListener = l;
3184 }
3185
3186 /**
3187 * Register a callback to be invoked when this view is clicked and held. If this view is not
3188 * long clickable, it becomes long clickable.
3189 *
3190 * @param l The callback that will run
3191 *
3192 * @see #setLongClickable(boolean)
3193 */
3194 public void setOnLongClickListener(OnLongClickListener l) {
3195 if (!isLongClickable()) {
3196 setLongClickable(true);
3197 }
3198 mOnLongClickListener = l;
3199 }
3200
3201 /**
3202 * Register a callback to be invoked when the context menu for this view is
3203 * being built. If this view is not long clickable, it becomes long clickable.
3204 *
3205 * @param l The callback that will run
3206 *
3207 */
3208 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3209 if (!isLongClickable()) {
3210 setLongClickable(true);
3211 }
3212 mOnCreateContextMenuListener = l;
3213 }
3214
3215 /**
3216 * Call this view's OnClickListener, if it is defined.
3217 *
3218 * @return True there was an assigned OnClickListener that was called, false
3219 * otherwise is returned.
3220 */
3221 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003222 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 if (mOnClickListener != null) {
3225 playSoundEffect(SoundEffectConstants.CLICK);
3226 mOnClickListener.onClick(this);
3227 return true;
3228 }
3229
3230 return false;
3231 }
3232
3233 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003234 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3235 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003237 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 */
3239 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003240 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 boolean handled = false;
3243 if (mOnLongClickListener != null) {
3244 handled = mOnLongClickListener.onLongClick(View.this);
3245 }
3246 if (!handled) {
3247 handled = showContextMenu();
3248 }
3249 if (handled) {
3250 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3251 }
3252 return handled;
3253 }
3254
3255 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003256 * Performs button-related actions during a touch down event.
3257 *
3258 * @param event The event.
3259 * @return True if the down was consumed.
3260 *
3261 * @hide
3262 */
3263 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3264 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3265 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3266 return true;
3267 }
3268 }
3269 return false;
3270 }
3271
3272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 * Bring up the context menu for this view.
3274 *
3275 * @return Whether a context menu was displayed.
3276 */
3277 public boolean showContextMenu() {
3278 return getParent().showContextMenuForChild(this);
3279 }
3280
3281 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003282 * Bring up the context menu for this view, referring to the item under the specified point.
3283 *
3284 * @param x The referenced x coordinate.
3285 * @param y The referenced y coordinate.
3286 * @param metaState The keyboard modifiers that were pressed.
3287 * @return Whether a context menu was displayed.
3288 *
3289 * @hide
3290 */
3291 public boolean showContextMenu(float x, float y, int metaState) {
3292 return showContextMenu();
3293 }
3294
3295 /**
Adam Powell6e346362010-07-23 10:18:23 -07003296 * Start an action mode.
3297 *
3298 * @param callback Callback that will control the lifecycle of the action mode
3299 * @return The new action mode if it is started, null otherwise
3300 *
3301 * @see ActionMode
3302 */
3303 public ActionMode startActionMode(ActionMode.Callback callback) {
3304 return getParent().startActionModeForChild(this, callback);
3305 }
3306
3307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 * Register a callback to be invoked when a key is pressed in this view.
3309 * @param l the key listener to attach to this view
3310 */
3311 public void setOnKeyListener(OnKeyListener l) {
3312 mOnKeyListener = l;
3313 }
3314
3315 /**
3316 * Register a callback to be invoked when a touch event is sent to this view.
3317 * @param l the touch listener to attach to this view
3318 */
3319 public void setOnTouchListener(OnTouchListener l) {
3320 mOnTouchListener = l;
3321 }
3322
3323 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003324 * Register a callback to be invoked when a generic motion event is sent to this view.
3325 * @param l the generic motion listener to attach to this view
3326 */
3327 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3328 mOnGenericMotionListener = l;
3329 }
3330
3331 /**
Joe Malin32736f02011-01-19 16:14:20 -08003332 * Register a drag event listener callback object for this View. The parameter is
3333 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3334 * View, the system calls the
3335 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3336 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003337 */
3338 public void setOnDragListener(OnDragListener l) {
3339 mOnDragListener = l;
3340 }
3341
3342 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003343 * Give this view focus. This will cause
3344 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 *
3346 * Note: this does not check whether this {@link View} should get focus, it just
3347 * gives it focus no matter what. It should only be called internally by framework
3348 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3349 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003350 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3351 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 * focus moved when requestFocus() is called. It may not always
3353 * apply, in which case use the default View.FOCUS_DOWN.
3354 * @param previouslyFocusedRect The rectangle of the view that had focus
3355 * prior in this View's coordinate system.
3356 */
3357 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3358 if (DBG) {
3359 System.out.println(this + " requestFocus()");
3360 }
3361
3362 if ((mPrivateFlags & FOCUSED) == 0) {
3363 mPrivateFlags |= FOCUSED;
3364
3365 if (mParent != null) {
3366 mParent.requestChildFocus(this, this);
3367 }
3368
3369 onFocusChanged(true, direction, previouslyFocusedRect);
3370 refreshDrawableState();
3371 }
3372 }
3373
3374 /**
3375 * Request that a rectangle of this view be visible on the screen,
3376 * scrolling if necessary just enough.
3377 *
3378 * <p>A View should call this if it maintains some notion of which part
3379 * of its content is interesting. For example, a text editing view
3380 * should call this when its cursor moves.
3381 *
3382 * @param rectangle The rectangle.
3383 * @return Whether any parent scrolled.
3384 */
3385 public boolean requestRectangleOnScreen(Rect rectangle) {
3386 return requestRectangleOnScreen(rectangle, false);
3387 }
3388
3389 /**
3390 * Request that a rectangle of this view be visible on the screen,
3391 * scrolling if necessary just enough.
3392 *
3393 * <p>A View should call this if it maintains some notion of which part
3394 * of its content is interesting. For example, a text editing view
3395 * should call this when its cursor moves.
3396 *
3397 * <p>When <code>immediate</code> is set to true, scrolling will not be
3398 * animated.
3399 *
3400 * @param rectangle The rectangle.
3401 * @param immediate True to forbid animated scrolling, false otherwise
3402 * @return Whether any parent scrolled.
3403 */
3404 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3405 View child = this;
3406 ViewParent parent = mParent;
3407 boolean scrolled = false;
3408 while (parent != null) {
3409 scrolled |= parent.requestChildRectangleOnScreen(child,
3410 rectangle, immediate);
3411
3412 // offset rect so next call has the rectangle in the
3413 // coordinate system of its direct child.
3414 rectangle.offset(child.getLeft(), child.getTop());
3415 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3416
3417 if (!(parent instanceof View)) {
3418 break;
3419 }
Romain Guy8506ab42009-06-11 17:35:47 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 child = (View) parent;
3422 parent = child.getParent();
3423 }
3424 return scrolled;
3425 }
3426
3427 /**
3428 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003429 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 */
3431 public void clearFocus() {
3432 if (DBG) {
3433 System.out.println(this + " clearFocus()");
3434 }
3435
3436 if ((mPrivateFlags & FOCUSED) != 0) {
3437 mPrivateFlags &= ~FOCUSED;
3438
3439 if (mParent != null) {
3440 mParent.clearChildFocus(this);
3441 }
3442
3443 onFocusChanged(false, 0, null);
3444 refreshDrawableState();
3445 }
3446 }
3447
3448 /**
3449 * Called to clear the focus of a view that is about to be removed.
3450 * Doesn't call clearChildFocus, which prevents this view from taking
3451 * focus again before it has been removed from the parent
3452 */
3453 void clearFocusForRemoval() {
3454 if ((mPrivateFlags & FOCUSED) != 0) {
3455 mPrivateFlags &= ~FOCUSED;
3456
3457 onFocusChanged(false, 0, null);
3458 refreshDrawableState();
3459 }
3460 }
3461
3462 /**
3463 * Called internally by the view system when a new view is getting focus.
3464 * This is what clears the old focus.
3465 */
3466 void unFocus() {
3467 if (DBG) {
3468 System.out.println(this + " unFocus()");
3469 }
3470
3471 if ((mPrivateFlags & FOCUSED) != 0) {
3472 mPrivateFlags &= ~FOCUSED;
3473
3474 onFocusChanged(false, 0, null);
3475 refreshDrawableState();
3476 }
3477 }
3478
3479 /**
3480 * Returns true if this view has focus iteself, or is the ancestor of the
3481 * view that has focus.
3482 *
3483 * @return True if this view has or contains focus, false otherwise.
3484 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003485 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 public boolean hasFocus() {
3487 return (mPrivateFlags & FOCUSED) != 0;
3488 }
3489
3490 /**
3491 * Returns true if this view is focusable or if it contains a reachable View
3492 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3493 * is a View whose parents do not block descendants focus.
3494 *
3495 * Only {@link #VISIBLE} views are considered focusable.
3496 *
3497 * @return True if the view is focusable or if the view contains a focusable
3498 * View, false otherwise.
3499 *
3500 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3501 */
3502 public boolean hasFocusable() {
3503 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3504 }
3505
3506 /**
3507 * Called by the view system when the focus state of this view changes.
3508 * When the focus change event is caused by directional navigation, direction
3509 * and previouslyFocusedRect provide insight into where the focus is coming from.
3510 * When overriding, be sure to call up through to the super class so that
3511 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 * @param gainFocus True if the View has focus; false otherwise.
3514 * @param direction The direction focus has moved when requestFocus()
3515 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003516 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3517 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3518 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3520 * system, of the previously focused view. If applicable, this will be
3521 * passed in as finer grained information about where the focus is coming
3522 * from (in addition to direction). Will be <code>null</code> otherwise.
3523 */
3524 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003525 if (gainFocus) {
3526 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3527 }
3528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 InputMethodManager imm = InputMethodManager.peekInstance();
3530 if (!gainFocus) {
3531 if (isPressed()) {
3532 setPressed(false);
3533 }
3534 if (imm != null && mAttachInfo != null
3535 && mAttachInfo.mHasWindowFocus) {
3536 imm.focusOut(this);
3537 }
Romain Guya2431d02009-04-30 16:30:00 -07003538 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 } else if (imm != null && mAttachInfo != null
3540 && mAttachInfo.mHasWindowFocus) {
3541 imm.focusIn(this);
3542 }
Romain Guy8506ab42009-06-11 17:35:47 -07003543
Romain Guy0fd89bf2011-01-26 15:41:30 -08003544 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 if (mOnFocusChangeListener != null) {
3546 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3547 }
Joe Malin32736f02011-01-19 16:14:20 -08003548
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003549 if (mAttachInfo != null) {
3550 mAttachInfo.mKeyDispatchState.reset(this);
3551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 }
3553
3554 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003555 * Sends an accessibility event of the given type. If accessiiblity is
3556 * not enabled this method has no effect. The default implementation calls
3557 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3558 * to populate information about the event source (this View), then calls
3559 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3560 * populate the text content of the event source including its descendants,
3561 * and last calls
3562 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3563 * on its parent to resuest sending of the event to interested parties.
3564 *
3565 * @param eventType The type of the event to send.
3566 *
3567 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3568 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3569 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003570 */
3571 public void sendAccessibilityEvent(int eventType) {
3572 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3573 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3574 }
3575 }
3576
3577 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003578 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3579 * takes as an argument an empty {@link AccessibilityEvent} and does not
3580 * perfrom a check whether accessibility is enabled.
3581 *
3582 * @param event The event to send.
3583 *
3584 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003585 */
3586 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003587 if (!isShown()) {
3588 return;
3589 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003590 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003591 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003592 // In the beginning we called #isShown(), so we know that getParent() is not null.
3593 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003594 }
3595
3596 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003597 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3598 * to its children for adding their text content to the event. Note that the
3599 * event text is populated in a separate dispatch path since we add to the
3600 * event not only the text of the source but also the text of all its descendants.
3601 * </p>
3602 * A typical implementation will call
3603 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3604 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3605 * on each child. Override this method if custom population of the event text
3606 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003607 *
3608 * @param event The event.
3609 *
3610 * @return True if the event population was completed.
3611 */
3612 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003613 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003614 return false;
3615 }
3616
3617 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003618 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003619 * giving a chance to this View to populate the accessibility event with its
3620 * text content. While the implementation is free to modify other event
3621 * attributes this should be performed in
3622 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3623 * <p>
3624 * Example: Adding formatted date string to an accessibility event in addition
3625 * to the text added by the super implementation.
3626 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003627 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3628 * super.onPopulateAccessibilityEvent(event);
3629 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3630 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3631 * mCurrentDate.getTimeInMillis(), flags);
3632 * event.getText().add(selectedDateUtterance);
3633 * }
3634 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003635 *
3636 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003637 *
3638 * @see #sendAccessibilityEvent(int)
3639 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003640 */
3641 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3642
3643 }
3644
3645 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003646 * Initializes an {@link AccessibilityEvent} with information about the
3647 * the type of the event and this View which is the event source. In other
3648 * words, the source of an accessibility event is the view whose state
3649 * change triggered firing the event.
3650 * <p>
3651 * Example: Setting the password property of an event in addition
3652 * to properties set by the super implementation.
3653 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003654 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3655 * super.onInitializeAccessibilityEvent(event);
3656 * event.setPassword(true);
3657 * }
3658 * </code></pre></p>
3659 * @param event The event to initialeze.
3660 *
3661 * @see #sendAccessibilityEvent(int)
3662 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3663 */
3664 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003665 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003666 event.setClassName(getClass().getName());
3667 event.setPackageName(getContext().getPackageName());
3668 event.setEnabled(isEnabled());
3669 event.setContentDescription(mContentDescription);
3670
3671 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3672 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3673 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3674 event.setItemCount(focusablesTempList.size());
3675 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3676 focusablesTempList.clear();
3677 }
3678 }
3679
3680 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003681 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3682 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3683 * This method is responsible for obtaining an accessibility node info from a
3684 * pool of reusable instances and calling
3685 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3686 * initialize the former.
3687 * <p>
3688 * Note: The client is responsible for recycling the obtained instance by calling
3689 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3690 * </p>
3691 * @return A populated {@link AccessibilityNodeInfo}.
3692 *
3693 * @see AccessibilityNodeInfo
3694 */
3695 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3696 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3697 onInitializeAccessibilityNodeInfo(info);
3698 return info;
3699 }
3700
3701 /**
3702 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3703 * The base implementation sets:
3704 * <ul>
3705 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
3706 * <li>{@link AccessibilityNodeInfo#setBounds(Rect)},</li>
3707 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3708 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3709 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3710 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3711 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3712 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3713 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3714 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3715 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3716 * </ul>
3717 * <p>
3718 * Subclasses should override this method, call the super implementation,
3719 * and set additional attributes.
3720 * </p>
3721 * @param info The instance to initialize.
3722 */
3723 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3724 Rect bounds = mAttachInfo.mTmpInvalRect;
3725 getDrawingRect(bounds);
3726 info.setBounds(bounds);
3727
3728 ViewParent parent = getParent();
3729 if (parent instanceof View) {
3730 View parentView = (View) parent;
3731 info.setParent(parentView);
3732 }
3733
3734 info.setPackageName(mContext.getPackageName());
3735 info.setClassName(getClass().getName());
3736 info.setContentDescription(getContentDescription());
3737
3738 info.setEnabled(isEnabled());
3739 info.setClickable(isClickable());
3740 info.setFocusable(isFocusable());
3741 info.setFocused(isFocused());
3742 info.setSelected(isSelected());
3743 info.setLongClickable(isLongClickable());
3744
3745 // TODO: These make sense only if we are in an AdapterView but all
3746 // views can be selected. Maybe from accessiiblity perspective
3747 // we should report as selectable view in an AdapterView.
3748 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3749 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3750
3751 if (isFocusable()) {
3752 if (isFocused()) {
3753 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3754 } else {
3755 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3756 }
3757 }
3758 }
3759
3760 /**
3761 * Gets the unique identifier of this view on the screen for accessibility purposes.
3762 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3763 *
3764 * @return The view accessibility id.
3765 *
3766 * @hide
3767 */
3768 public int getAccessibilityViewId() {
3769 if (mAccessibilityViewId == NO_ID) {
3770 mAccessibilityViewId = sNextAccessibilityViewId++;
3771 }
3772 return mAccessibilityViewId;
3773 }
3774
3775 /**
3776 * Gets the unique identifier of the window in which this View reseides.
3777 *
3778 * @return The window accessibility id.
3779 *
3780 * @hide
3781 */
3782 public int getAccessibilityWindowId() {
3783 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3784 }
3785
3786 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003787 * Gets the {@link View} description. It briefly describes the view and is
3788 * primarily used for accessibility support. Set this property to enable
3789 * better accessibility support for your application. This is especially
3790 * true for views that do not have textual representation (For example,
3791 * ImageButton).
3792 *
3793 * @return The content descriptiopn.
3794 *
3795 * @attr ref android.R.styleable#View_contentDescription
3796 */
3797 public CharSequence getContentDescription() {
3798 return mContentDescription;
3799 }
3800
3801 /**
3802 * Sets the {@link View} description. It briefly describes the view and is
3803 * primarily used for accessibility support. Set this property to enable
3804 * better accessibility support for your application. This is especially
3805 * true for views that do not have textual representation (For example,
3806 * ImageButton).
3807 *
3808 * @param contentDescription The content description.
3809 *
3810 * @attr ref android.R.styleable#View_contentDescription
3811 */
3812 public void setContentDescription(CharSequence contentDescription) {
3813 mContentDescription = contentDescription;
3814 }
3815
3816 /**
Romain Guya2431d02009-04-30 16:30:00 -07003817 * Invoked whenever this view loses focus, either by losing window focus or by losing
3818 * focus within its window. This method can be used to clear any state tied to the
3819 * focus. For instance, if a button is held pressed with the trackball and the window
3820 * loses focus, this method can be used to cancel the press.
3821 *
3822 * Subclasses of View overriding this method should always call super.onFocusLost().
3823 *
3824 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003825 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003826 *
3827 * @hide pending API council approval
3828 */
3829 protected void onFocusLost() {
3830 resetPressedState();
3831 }
3832
3833 private void resetPressedState() {
3834 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3835 return;
3836 }
3837
3838 if (isPressed()) {
3839 setPressed(false);
3840
3841 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003842 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003843 }
3844 }
3845 }
3846
3847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 * Returns true if this view has focus
3849 *
3850 * @return True if this view has focus, false otherwise.
3851 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003852 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 public boolean isFocused() {
3854 return (mPrivateFlags & FOCUSED) != 0;
3855 }
3856
3857 /**
3858 * Find the view in the hierarchy rooted at this view that currently has
3859 * focus.
3860 *
3861 * @return The view that currently has focus, or null if no focused view can
3862 * be found.
3863 */
3864 public View findFocus() {
3865 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3866 }
3867
3868 /**
3869 * Change whether this view is one of the set of scrollable containers in
3870 * its window. This will be used to determine whether the window can
3871 * resize or must pan when a soft input area is open -- scrollable
3872 * containers allow the window to use resize mode since the container
3873 * will appropriately shrink.
3874 */
3875 public void setScrollContainer(boolean isScrollContainer) {
3876 if (isScrollContainer) {
3877 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3878 mAttachInfo.mScrollContainers.add(this);
3879 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3880 }
3881 mPrivateFlags |= SCROLL_CONTAINER;
3882 } else {
3883 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3884 mAttachInfo.mScrollContainers.remove(this);
3885 }
3886 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3887 }
3888 }
3889
3890 /**
3891 * Returns the quality of the drawing cache.
3892 *
3893 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3894 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3895 *
3896 * @see #setDrawingCacheQuality(int)
3897 * @see #setDrawingCacheEnabled(boolean)
3898 * @see #isDrawingCacheEnabled()
3899 *
3900 * @attr ref android.R.styleable#View_drawingCacheQuality
3901 */
3902 public int getDrawingCacheQuality() {
3903 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3904 }
3905
3906 /**
3907 * Set the drawing cache quality of this view. This value is used only when the
3908 * drawing cache is enabled
3909 *
3910 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3911 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3912 *
3913 * @see #getDrawingCacheQuality()
3914 * @see #setDrawingCacheEnabled(boolean)
3915 * @see #isDrawingCacheEnabled()
3916 *
3917 * @attr ref android.R.styleable#View_drawingCacheQuality
3918 */
3919 public void setDrawingCacheQuality(int quality) {
3920 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3921 }
3922
3923 /**
3924 * Returns whether the screen should remain on, corresponding to the current
3925 * value of {@link #KEEP_SCREEN_ON}.
3926 *
3927 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3928 *
3929 * @see #setKeepScreenOn(boolean)
3930 *
3931 * @attr ref android.R.styleable#View_keepScreenOn
3932 */
3933 public boolean getKeepScreenOn() {
3934 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3935 }
3936
3937 /**
3938 * Controls whether the screen should remain on, modifying the
3939 * value of {@link #KEEP_SCREEN_ON}.
3940 *
3941 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3942 *
3943 * @see #getKeepScreenOn()
3944 *
3945 * @attr ref android.R.styleable#View_keepScreenOn
3946 */
3947 public void setKeepScreenOn(boolean keepScreenOn) {
3948 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3949 }
3950
3951 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003952 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3953 * @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 -08003954 *
3955 * @attr ref android.R.styleable#View_nextFocusLeft
3956 */
3957 public int getNextFocusLeftId() {
3958 return mNextFocusLeftId;
3959 }
3960
3961 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003962 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3963 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3964 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003965 *
3966 * @attr ref android.R.styleable#View_nextFocusLeft
3967 */
3968 public void setNextFocusLeftId(int nextFocusLeftId) {
3969 mNextFocusLeftId = nextFocusLeftId;
3970 }
3971
3972 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003973 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3974 * @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 -08003975 *
3976 * @attr ref android.R.styleable#View_nextFocusRight
3977 */
3978 public int getNextFocusRightId() {
3979 return mNextFocusRightId;
3980 }
3981
3982 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003983 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3984 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3985 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 *
3987 * @attr ref android.R.styleable#View_nextFocusRight
3988 */
3989 public void setNextFocusRightId(int nextFocusRightId) {
3990 mNextFocusRightId = nextFocusRightId;
3991 }
3992
3993 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003994 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3995 * @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 -08003996 *
3997 * @attr ref android.R.styleable#View_nextFocusUp
3998 */
3999 public int getNextFocusUpId() {
4000 return mNextFocusUpId;
4001 }
4002
4003 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004004 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4005 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4006 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 *
4008 * @attr ref android.R.styleable#View_nextFocusUp
4009 */
4010 public void setNextFocusUpId(int nextFocusUpId) {
4011 mNextFocusUpId = nextFocusUpId;
4012 }
4013
4014 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004015 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4016 * @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 -08004017 *
4018 * @attr ref android.R.styleable#View_nextFocusDown
4019 */
4020 public int getNextFocusDownId() {
4021 return mNextFocusDownId;
4022 }
4023
4024 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004025 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4026 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4027 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 *
4029 * @attr ref android.R.styleable#View_nextFocusDown
4030 */
4031 public void setNextFocusDownId(int nextFocusDownId) {
4032 mNextFocusDownId = nextFocusDownId;
4033 }
4034
4035 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004036 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4037 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4038 *
4039 * @attr ref android.R.styleable#View_nextFocusForward
4040 */
4041 public int getNextFocusForwardId() {
4042 return mNextFocusForwardId;
4043 }
4044
4045 /**
4046 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4047 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4048 * decide automatically.
4049 *
4050 * @attr ref android.R.styleable#View_nextFocusForward
4051 */
4052 public void setNextFocusForwardId(int nextFocusForwardId) {
4053 mNextFocusForwardId = nextFocusForwardId;
4054 }
4055
4056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 * Returns the visibility of this view and all of its ancestors
4058 *
4059 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4060 */
4061 public boolean isShown() {
4062 View current = this;
4063 //noinspection ConstantConditions
4064 do {
4065 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4066 return false;
4067 }
4068 ViewParent parent = current.mParent;
4069 if (parent == null) {
4070 return false; // We are not attached to the view root
4071 }
4072 if (!(parent instanceof View)) {
4073 return true;
4074 }
4075 current = (View) parent;
4076 } while (current != null);
4077
4078 return false;
4079 }
4080
4081 /**
4082 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4083 * is set
4084 *
4085 * @param insets Insets for system windows
4086 *
4087 * @return True if this view applied the insets, false otherwise
4088 */
4089 protected boolean fitSystemWindows(Rect insets) {
4090 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4091 mPaddingLeft = insets.left;
4092 mPaddingTop = insets.top;
4093 mPaddingRight = insets.right;
4094 mPaddingBottom = insets.bottom;
4095 requestLayout();
4096 return true;
4097 }
4098 return false;
4099 }
4100
4101 /**
4102 * Returns the visibility status for this view.
4103 *
4104 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4105 * @attr ref android.R.styleable#View_visibility
4106 */
4107 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004108 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4109 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4110 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 })
4112 public int getVisibility() {
4113 return mViewFlags & VISIBILITY_MASK;
4114 }
4115
4116 /**
4117 * Set the enabled state of this view.
4118 *
4119 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4120 * @attr ref android.R.styleable#View_visibility
4121 */
4122 @RemotableViewMethod
4123 public void setVisibility(int visibility) {
4124 setFlags(visibility, VISIBILITY_MASK);
4125 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4126 }
4127
4128 /**
4129 * Returns the enabled status for this view. The interpretation of the
4130 * enabled state varies by subclass.
4131 *
4132 * @return True if this view is enabled, false otherwise.
4133 */
4134 @ViewDebug.ExportedProperty
4135 public boolean isEnabled() {
4136 return (mViewFlags & ENABLED_MASK) == ENABLED;
4137 }
4138
4139 /**
4140 * Set the enabled state of this view. The interpretation of the enabled
4141 * state varies by subclass.
4142 *
4143 * @param enabled True if this view is enabled, false otherwise.
4144 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004145 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004147 if (enabled == isEnabled()) return;
4148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4150
4151 /*
4152 * The View most likely has to change its appearance, so refresh
4153 * the drawable state.
4154 */
4155 refreshDrawableState();
4156
4157 // Invalidate too, since the default behavior for views is to be
4158 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004159 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
4161
4162 /**
4163 * Set whether this view can receive the focus.
4164 *
4165 * Setting this to false will also ensure that this view is not focusable
4166 * in touch mode.
4167 *
4168 * @param focusable If true, this view can receive the focus.
4169 *
4170 * @see #setFocusableInTouchMode(boolean)
4171 * @attr ref android.R.styleable#View_focusable
4172 */
4173 public void setFocusable(boolean focusable) {
4174 if (!focusable) {
4175 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4176 }
4177 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4178 }
4179
4180 /**
4181 * Set whether this view can receive focus while in touch mode.
4182 *
4183 * Setting this to true will also ensure that this view is focusable.
4184 *
4185 * @param focusableInTouchMode If true, this view can receive the focus while
4186 * in touch mode.
4187 *
4188 * @see #setFocusable(boolean)
4189 * @attr ref android.R.styleable#View_focusableInTouchMode
4190 */
4191 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4192 // Focusable in touch mode should always be set before the focusable flag
4193 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4194 // which, in touch mode, will not successfully request focus on this view
4195 // because the focusable in touch mode flag is not set
4196 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4197 if (focusableInTouchMode) {
4198 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4199 }
4200 }
4201
4202 /**
4203 * Set whether this view should have sound effects enabled for events such as
4204 * clicking and touching.
4205 *
4206 * <p>You may wish to disable sound effects for a view if you already play sounds,
4207 * for instance, a dial key that plays dtmf tones.
4208 *
4209 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4210 * @see #isSoundEffectsEnabled()
4211 * @see #playSoundEffect(int)
4212 * @attr ref android.R.styleable#View_soundEffectsEnabled
4213 */
4214 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4215 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4216 }
4217
4218 /**
4219 * @return whether this view should have sound effects enabled for events such as
4220 * clicking and touching.
4221 *
4222 * @see #setSoundEffectsEnabled(boolean)
4223 * @see #playSoundEffect(int)
4224 * @attr ref android.R.styleable#View_soundEffectsEnabled
4225 */
4226 @ViewDebug.ExportedProperty
4227 public boolean isSoundEffectsEnabled() {
4228 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4229 }
4230
4231 /**
4232 * Set whether this view should have haptic feedback for events such as
4233 * long presses.
4234 *
4235 * <p>You may wish to disable haptic feedback if your view already controls
4236 * its own haptic feedback.
4237 *
4238 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4239 * @see #isHapticFeedbackEnabled()
4240 * @see #performHapticFeedback(int)
4241 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4242 */
4243 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4244 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4245 }
4246
4247 /**
4248 * @return whether this view should have haptic feedback enabled for events
4249 * long presses.
4250 *
4251 * @see #setHapticFeedbackEnabled(boolean)
4252 * @see #performHapticFeedback(int)
4253 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4254 */
4255 @ViewDebug.ExportedProperty
4256 public boolean isHapticFeedbackEnabled() {
4257 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4258 }
4259
4260 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004261 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004262 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004263 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4264 * {@link #LAYOUT_DIRECTION_RTL},
4265 * {@link #LAYOUT_DIRECTION_INHERIT} or
4266 * {@link #LAYOUT_DIRECTION_LOCALE}.
4267 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004268 * @hide
4269 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004270 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004271 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4272 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4273 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4274 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004275 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004276 public int getLayoutDirection() {
4277 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004278 }
4279
4280 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004281 * Set the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004282 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004283 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4284 * {@link #LAYOUT_DIRECTION_RTL},
4285 * {@link #LAYOUT_DIRECTION_INHERIT} or
4286 * {@link #LAYOUT_DIRECTION_LOCALE}.
4287 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004288 * @hide
4289 */
4290 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004291 public void setLayoutDirection(int layoutDirection) {
4292 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
Cibu Johny7632cb92010-02-22 13:01:02 -08004293 }
4294
4295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 * If this view doesn't do any drawing on its own, set this flag to
4297 * allow further optimizations. By default, this flag is not set on
4298 * View, but could be set on some View subclasses such as ViewGroup.
4299 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004300 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4301 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 *
4303 * @param willNotDraw whether or not this View draw on its own
4304 */
4305 public void setWillNotDraw(boolean willNotDraw) {
4306 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4307 }
4308
4309 /**
4310 * Returns whether or not this View draws on its own.
4311 *
4312 * @return true if this view has nothing to draw, false otherwise
4313 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004314 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 public boolean willNotDraw() {
4316 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4317 }
4318
4319 /**
4320 * When a View's drawing cache is enabled, drawing is redirected to an
4321 * offscreen bitmap. Some views, like an ImageView, must be able to
4322 * bypass this mechanism if they already draw a single bitmap, to avoid
4323 * unnecessary usage of the memory.
4324 *
4325 * @param willNotCacheDrawing true if this view does not cache its
4326 * drawing, false otherwise
4327 */
4328 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4329 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4330 }
4331
4332 /**
4333 * Returns whether or not this View can cache its drawing or not.
4334 *
4335 * @return true if this view does not cache its drawing, false otherwise
4336 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004337 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 public boolean willNotCacheDrawing() {
4339 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4340 }
4341
4342 /**
4343 * Indicates whether this view reacts to click events or not.
4344 *
4345 * @return true if the view is clickable, false otherwise
4346 *
4347 * @see #setClickable(boolean)
4348 * @attr ref android.R.styleable#View_clickable
4349 */
4350 @ViewDebug.ExportedProperty
4351 public boolean isClickable() {
4352 return (mViewFlags & CLICKABLE) == CLICKABLE;
4353 }
4354
4355 /**
4356 * Enables or disables click events for this view. When a view
4357 * is clickable it will change its state to "pressed" on every click.
4358 * Subclasses should set the view clickable to visually react to
4359 * user's clicks.
4360 *
4361 * @param clickable true to make the view clickable, false otherwise
4362 *
4363 * @see #isClickable()
4364 * @attr ref android.R.styleable#View_clickable
4365 */
4366 public void setClickable(boolean clickable) {
4367 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4368 }
4369
4370 /**
4371 * Indicates whether this view reacts to long click events or not.
4372 *
4373 * @return true if the view is long clickable, false otherwise
4374 *
4375 * @see #setLongClickable(boolean)
4376 * @attr ref android.R.styleable#View_longClickable
4377 */
4378 public boolean isLongClickable() {
4379 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4380 }
4381
4382 /**
4383 * Enables or disables long click events for this view. When a view is long
4384 * clickable it reacts to the user holding down the button for a longer
4385 * duration than a tap. This event can either launch the listener or a
4386 * context menu.
4387 *
4388 * @param longClickable true to make the view long clickable, false otherwise
4389 * @see #isLongClickable()
4390 * @attr ref android.R.styleable#View_longClickable
4391 */
4392 public void setLongClickable(boolean longClickable) {
4393 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4394 }
4395
4396 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004397 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 *
4399 * @see #isClickable()
4400 * @see #setClickable(boolean)
4401 *
4402 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4403 * the View's internal state from a previously set "pressed" state.
4404 */
4405 public void setPressed(boolean pressed) {
4406 if (pressed) {
4407 mPrivateFlags |= PRESSED;
4408 } else {
4409 mPrivateFlags &= ~PRESSED;
4410 }
4411 refreshDrawableState();
4412 dispatchSetPressed(pressed);
4413 }
4414
4415 /**
4416 * Dispatch setPressed to all of this View's children.
4417 *
4418 * @see #setPressed(boolean)
4419 *
4420 * @param pressed The new pressed state
4421 */
4422 protected void dispatchSetPressed(boolean pressed) {
4423 }
4424
4425 /**
4426 * Indicates whether the view is currently in pressed state. Unless
4427 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4428 * the pressed state.
4429 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004430 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 * @see #isClickable()
4432 * @see #setClickable(boolean)
4433 *
4434 * @return true if the view is currently pressed, false otherwise
4435 */
4436 public boolean isPressed() {
4437 return (mPrivateFlags & PRESSED) == PRESSED;
4438 }
4439
4440 /**
4441 * Indicates whether this view will save its state (that is,
4442 * whether its {@link #onSaveInstanceState} method will be called).
4443 *
4444 * @return Returns true if the view state saving is enabled, else false.
4445 *
4446 * @see #setSaveEnabled(boolean)
4447 * @attr ref android.R.styleable#View_saveEnabled
4448 */
4449 public boolean isSaveEnabled() {
4450 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4451 }
4452
4453 /**
4454 * Controls whether the saving of this view's state is
4455 * enabled (that is, whether its {@link #onSaveInstanceState} method
4456 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004457 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 * for its state to be saved. This flag can only disable the
4459 * saving of this view; any child views may still have their state saved.
4460 *
4461 * @param enabled Set to false to <em>disable</em> state saving, or true
4462 * (the default) to allow it.
4463 *
4464 * @see #isSaveEnabled()
4465 * @see #setId(int)
4466 * @see #onSaveInstanceState()
4467 * @attr ref android.R.styleable#View_saveEnabled
4468 */
4469 public void setSaveEnabled(boolean enabled) {
4470 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4471 }
4472
Jeff Brown85a31762010-09-01 17:01:00 -07004473 /**
4474 * Gets whether the framework should discard touches when the view's
4475 * window is obscured by another visible window.
4476 * Refer to the {@link View} security documentation for more details.
4477 *
4478 * @return True if touch filtering is enabled.
4479 *
4480 * @see #setFilterTouchesWhenObscured(boolean)
4481 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4482 */
4483 @ViewDebug.ExportedProperty
4484 public boolean getFilterTouchesWhenObscured() {
4485 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4486 }
4487
4488 /**
4489 * Sets whether the framework should discard touches when the view's
4490 * window is obscured by another visible window.
4491 * Refer to the {@link View} security documentation for more details.
4492 *
4493 * @param enabled True if touch filtering should be enabled.
4494 *
4495 * @see #getFilterTouchesWhenObscured
4496 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4497 */
4498 public void setFilterTouchesWhenObscured(boolean enabled) {
4499 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4500 FILTER_TOUCHES_WHEN_OBSCURED);
4501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502
4503 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004504 * Indicates whether the entire hierarchy under this view will save its
4505 * state when a state saving traversal occurs from its parent. The default
4506 * is true; if false, these views will not be saved unless
4507 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4508 *
4509 * @return Returns true if the view state saving from parent is enabled, else false.
4510 *
4511 * @see #setSaveFromParentEnabled(boolean)
4512 */
4513 public boolean isSaveFromParentEnabled() {
4514 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4515 }
4516
4517 /**
4518 * Controls whether the entire hierarchy under this view will save its
4519 * state when a state saving traversal occurs from its parent. The default
4520 * is true; if false, these views will not be saved unless
4521 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4522 *
4523 * @param enabled Set to false to <em>disable</em> state saving, or true
4524 * (the default) to allow it.
4525 *
4526 * @see #isSaveFromParentEnabled()
4527 * @see #setId(int)
4528 * @see #onSaveInstanceState()
4529 */
4530 public void setSaveFromParentEnabled(boolean enabled) {
4531 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4532 }
4533
4534
4535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 * Returns whether this View is able to take focus.
4537 *
4538 * @return True if this view can take focus, or false otherwise.
4539 * @attr ref android.R.styleable#View_focusable
4540 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004541 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 public final boolean isFocusable() {
4543 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4544 }
4545
4546 /**
4547 * When a view is focusable, it may not want to take focus when in touch mode.
4548 * For example, a button would like focus when the user is navigating via a D-pad
4549 * so that the user can click on it, but once the user starts touching the screen,
4550 * the button shouldn't take focus
4551 * @return Whether the view is focusable in touch mode.
4552 * @attr ref android.R.styleable#View_focusableInTouchMode
4553 */
4554 @ViewDebug.ExportedProperty
4555 public final boolean isFocusableInTouchMode() {
4556 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4557 }
4558
4559 /**
4560 * Find the nearest view in the specified direction that can take focus.
4561 * This does not actually give focus to that view.
4562 *
4563 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4564 *
4565 * @return The nearest focusable in the specified direction, or null if none
4566 * can be found.
4567 */
4568 public View focusSearch(int direction) {
4569 if (mParent != null) {
4570 return mParent.focusSearch(this, direction);
4571 } else {
4572 return null;
4573 }
4574 }
4575
4576 /**
4577 * This method is the last chance for the focused view and its ancestors to
4578 * respond to an arrow key. This is called when the focused view did not
4579 * consume the key internally, nor could the view system find a new view in
4580 * the requested direction to give focus to.
4581 *
4582 * @param focused The currently focused view.
4583 * @param direction The direction focus wants to move. One of FOCUS_UP,
4584 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4585 * @return True if the this view consumed this unhandled move.
4586 */
4587 public boolean dispatchUnhandledMove(View focused, int direction) {
4588 return false;
4589 }
4590
4591 /**
4592 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004593 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004595 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4596 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 * @return The user specified next view, or null if there is none.
4598 */
4599 View findUserSetNextFocus(View root, int direction) {
4600 switch (direction) {
4601 case FOCUS_LEFT:
4602 if (mNextFocusLeftId == View.NO_ID) return null;
4603 return findViewShouldExist(root, mNextFocusLeftId);
4604 case FOCUS_RIGHT:
4605 if (mNextFocusRightId == View.NO_ID) return null;
4606 return findViewShouldExist(root, mNextFocusRightId);
4607 case FOCUS_UP:
4608 if (mNextFocusUpId == View.NO_ID) return null;
4609 return findViewShouldExist(root, mNextFocusUpId);
4610 case FOCUS_DOWN:
4611 if (mNextFocusDownId == View.NO_ID) return null;
4612 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004613 case FOCUS_FORWARD:
4614 if (mNextFocusForwardId == View.NO_ID) return null;
4615 return findViewShouldExist(root, mNextFocusForwardId);
4616 case FOCUS_BACKWARD: {
4617 final int id = mID;
4618 return root.findViewByPredicate(new Predicate<View>() {
4619 @Override
4620 public boolean apply(View t) {
4621 return t.mNextFocusForwardId == id;
4622 }
4623 });
4624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 }
4626 return null;
4627 }
4628
4629 private static View findViewShouldExist(View root, int childViewId) {
4630 View result = root.findViewById(childViewId);
4631 if (result == null) {
4632 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4633 + "by user for id " + childViewId);
4634 }
4635 return result;
4636 }
4637
4638 /**
4639 * Find and return all focusable views that are descendants of this view,
4640 * possibly including this view if it is focusable itself.
4641 *
4642 * @param direction The direction of the focus
4643 * @return A list of focusable views
4644 */
4645 public ArrayList<View> getFocusables(int direction) {
4646 ArrayList<View> result = new ArrayList<View>(24);
4647 addFocusables(result, direction);
4648 return result;
4649 }
4650
4651 /**
4652 * Add any focusable views that are descendants of this view (possibly
4653 * including this view if it is focusable itself) to views. If we are in touch mode,
4654 * only add views that are also focusable in touch mode.
4655 *
4656 * @param views Focusable views found so far
4657 * @param direction The direction of the focus
4658 */
4659 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004660 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662
svetoslavganov75986cf2009-05-14 22:28:01 -07004663 /**
4664 * Adds any focusable views that are descendants of this view (possibly
4665 * including this view if it is focusable itself) to views. This method
4666 * adds all focusable views regardless if we are in touch mode or
4667 * only views focusable in touch mode if we are in touch mode depending on
4668 * the focusable mode paramater.
4669 *
4670 * @param views Focusable views found so far or null if all we are interested is
4671 * the number of focusables.
4672 * @param direction The direction of the focus.
4673 * @param focusableMode The type of focusables to be added.
4674 *
4675 * @see #FOCUSABLES_ALL
4676 * @see #FOCUSABLES_TOUCH_MODE
4677 */
4678 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4679 if (!isFocusable()) {
4680 return;
4681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682
svetoslavganov75986cf2009-05-14 22:28:01 -07004683 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4684 isInTouchMode() && !isFocusableInTouchMode()) {
4685 return;
4686 }
4687
4688 if (views != null) {
4689 views.add(this);
4690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 }
4692
4693 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004694 * Finds the Views that contain given text. The containment is case insensitive.
4695 * As View's text is considered any text content that View renders.
4696 *
4697 * @param outViews The output list of matching Views.
4698 * @param text The text to match against.
4699 */
4700 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4701 }
4702
4703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 * Find and return all touchable views that are descendants of this view,
4705 * possibly including this view if it is touchable itself.
4706 *
4707 * @return A list of touchable views
4708 */
4709 public ArrayList<View> getTouchables() {
4710 ArrayList<View> result = new ArrayList<View>();
4711 addTouchables(result);
4712 return result;
4713 }
4714
4715 /**
4716 * Add any touchable views that are descendants of this view (possibly
4717 * including this view if it is touchable itself) to views.
4718 *
4719 * @param views Touchable views found so far
4720 */
4721 public void addTouchables(ArrayList<View> views) {
4722 final int viewFlags = mViewFlags;
4723
4724 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4725 && (viewFlags & ENABLED_MASK) == ENABLED) {
4726 views.add(this);
4727 }
4728 }
4729
4730 /**
4731 * Call this to try to give focus to a specific view or to one of its
4732 * descendants.
4733 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004734 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4735 * false), or if it is focusable and it is not focusable in touch mode
4736 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004738 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 * have focus, and you want your parent to look for the next one.
4740 *
4741 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4742 * {@link #FOCUS_DOWN} and <code>null</code>.
4743 *
4744 * @return Whether this view or one of its descendants actually took focus.
4745 */
4746 public final boolean requestFocus() {
4747 return requestFocus(View.FOCUS_DOWN);
4748 }
4749
4750
4751 /**
4752 * Call this to try to give focus to a specific view or to one of its
4753 * descendants and give it a hint about what direction focus is heading.
4754 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004755 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4756 * false), or if it is focusable and it is not focusable in touch mode
4757 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004759 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 * have focus, and you want your parent to look for the next one.
4761 *
4762 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4763 * <code>null</code> set for the previously focused rectangle.
4764 *
4765 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4766 * @return Whether this view or one of its descendants actually took focus.
4767 */
4768 public final boolean requestFocus(int direction) {
4769 return requestFocus(direction, null);
4770 }
4771
4772 /**
4773 * Call this to try to give focus to a specific view or to one of its descendants
4774 * and give it hints about the direction and a specific rectangle that the focus
4775 * is coming from. The rectangle can help give larger views a finer grained hint
4776 * about where focus is coming from, and therefore, where to show selection, or
4777 * forward focus change internally.
4778 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004779 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4780 * false), or if it is focusable and it is not focusable in touch mode
4781 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 *
4783 * A View will not take focus if it is not visible.
4784 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004785 * A View will not take focus if one of its parents has
4786 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4787 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004789 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 * have focus, and you want your parent to look for the next one.
4791 *
4792 * You may wish to override this method if your custom {@link View} has an internal
4793 * {@link View} that it wishes to forward the request to.
4794 *
4795 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4796 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4797 * to give a finer grained hint about where focus is coming from. May be null
4798 * if there is no hint.
4799 * @return Whether this view or one of its descendants actually took focus.
4800 */
4801 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4802 // need to be focusable
4803 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4804 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4805 return false;
4806 }
4807
4808 // need to be focusable in touch mode if in touch mode
4809 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004810 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4811 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813
4814 // need to not have any parents blocking us
4815 if (hasAncestorThatBlocksDescendantFocus()) {
4816 return false;
4817 }
4818
4819 handleFocusGainInternal(direction, previouslyFocusedRect);
4820 return true;
4821 }
4822
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004823 /** Gets the ViewAncestor, or null if not attached. */
4824 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004825 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004826 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004827 }
4828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 /**
4830 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4831 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4832 * touch mode to request focus when they are touched.
4833 *
4834 * @return Whether this view or one of its descendants actually took focus.
4835 *
4836 * @see #isInTouchMode()
4837 *
4838 */
4839 public final boolean requestFocusFromTouch() {
4840 // Leave touch mode if we need to
4841 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004842 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004843 if (viewRoot != null) {
4844 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 }
4846 }
4847 return requestFocus(View.FOCUS_DOWN);
4848 }
4849
4850 /**
4851 * @return Whether any ancestor of this view blocks descendant focus.
4852 */
4853 private boolean hasAncestorThatBlocksDescendantFocus() {
4854 ViewParent ancestor = mParent;
4855 while (ancestor instanceof ViewGroup) {
4856 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4857 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4858 return true;
4859 } else {
4860 ancestor = vgAncestor.getParent();
4861 }
4862 }
4863 return false;
4864 }
4865
4866 /**
Romain Guya440b002010-02-24 15:57:54 -08004867 * @hide
4868 */
4869 public void dispatchStartTemporaryDetach() {
4870 onStartTemporaryDetach();
4871 }
4872
4873 /**
4874 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004875 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4876 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004877 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 */
4879 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004880 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004881 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004882 }
4883
4884 /**
4885 * @hide
4886 */
4887 public void dispatchFinishTemporaryDetach() {
4888 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 }
Romain Guy8506ab42009-06-11 17:35:47 -07004890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 /**
4892 * Called after {@link #onStartTemporaryDetach} when the container is done
4893 * changing the view.
4894 */
4895 public void onFinishTemporaryDetach() {
4896 }
Romain Guy8506ab42009-06-11 17:35:47 -07004897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004899 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4900 * for this view's window. Returns null if the view is not currently attached
4901 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004902 * just use the standard high-level event callbacks like
4903 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004904 */
4905 public KeyEvent.DispatcherState getKeyDispatcherState() {
4906 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4907 }
Joe Malin32736f02011-01-19 16:14:20 -08004908
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004909 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 * Dispatch a key event before it is processed by any input method
4911 * associated with the view hierarchy. This can be used to intercept
4912 * key events in special situations before the IME consumes them; a
4913 * typical example would be handling the BACK key to update the application's
4914 * UI instead of allowing the IME to see it and close itself.
4915 *
4916 * @param event The key event to be dispatched.
4917 * @return True if the event was handled, false otherwise.
4918 */
4919 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4920 return onKeyPreIme(event.getKeyCode(), event);
4921 }
4922
4923 /**
4924 * Dispatch a key event to the next view on the focus path. This path runs
4925 * from the top of the view tree down to the currently focused view. If this
4926 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4927 * the next node down the focus path. This method also fires any key
4928 * listeners.
4929 *
4930 * @param event The key event to be dispatched.
4931 * @return True if the event was handled, false otherwise.
4932 */
4933 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004934 if (mInputEventConsistencyVerifier != null) {
4935 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937
Jeff Brown21bc5c92011-02-28 18:27:14 -08004938 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004939 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4941 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4942 return true;
4943 }
4944
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004945 if (event.dispatch(this, mAttachInfo != null
4946 ? mAttachInfo.mKeyDispatchState : null, this)) {
4947 return true;
4948 }
4949
4950 if (mInputEventConsistencyVerifier != null) {
4951 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4952 }
4953 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 }
4955
4956 /**
4957 * Dispatches a key shortcut event.
4958 *
4959 * @param event The key event to be dispatched.
4960 * @return True if the event was handled by the view, false otherwise.
4961 */
4962 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4963 return onKeyShortcut(event.getKeyCode(), event);
4964 }
4965
4966 /**
4967 * Pass the touch screen motion event down to the target view, or this
4968 * view if it is the target.
4969 *
4970 * @param event The motion event to be dispatched.
4971 * @return True if the event was handled by the view, false otherwise.
4972 */
4973 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004974 if (mInputEventConsistencyVerifier != null) {
4975 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4976 }
4977
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004978 if (onFilterTouchEventForSecurity(event)) {
4979 //noinspection SimplifiableIfStatement
4980 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4981 mOnTouchListener.onTouch(this, event)) {
4982 return true;
4983 }
4984
4985 if (onTouchEvent(event)) {
4986 return true;
4987 }
Jeff Brown85a31762010-09-01 17:01:00 -07004988 }
4989
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004990 if (mInputEventConsistencyVerifier != null) {
4991 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004993 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 }
4995
4996 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004997 * Filter the touch event to apply security policies.
4998 *
4999 * @param event The motion event to be filtered.
5000 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005001 *
Jeff Brown85a31762010-09-01 17:01:00 -07005002 * @see #getFilterTouchesWhenObscured
5003 */
5004 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005005 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005006 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5007 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5008 // Window is obscured, drop this touch.
5009 return false;
5010 }
5011 return true;
5012 }
5013
5014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 * Pass a trackball motion event down to the focused view.
5016 *
5017 * @param event The motion event to be dispatched.
5018 * @return True if the event was handled by the view, false otherwise.
5019 */
5020 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005021 if (mInputEventConsistencyVerifier != null) {
5022 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5023 }
5024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005026 if (onTrackballEvent(event)) {
5027 return true;
5028 }
5029
5030 if (mInputEventConsistencyVerifier != null) {
5031 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5032 }
5033 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 }
5035
5036 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005037 * Dispatch a generic motion event.
5038 * <p>
5039 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5040 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005041 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005042 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005043 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005044 *
5045 * @param event The motion event to be dispatched.
5046 * @return True if the event was handled by the view, false otherwise.
5047 */
5048 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005049 if (mInputEventConsistencyVerifier != null) {
5050 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5051 }
5052
Jeff Browna032cc02011-03-07 16:56:21 -08005053 final int source = event.getSource();
5054 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5055 final int action = event.getAction();
5056 if (action == MotionEvent.ACTION_HOVER_ENTER
5057 || action == MotionEvent.ACTION_HOVER_MOVE
5058 || action == MotionEvent.ACTION_HOVER_EXIT) {
5059 if (dispatchHoverEvent(event)) {
5060 return true;
5061 }
5062 } else if (dispatchGenericPointerEvent(event)) {
5063 return true;
5064 }
5065 } else if (dispatchGenericFocusedEvent(event)) {
5066 return true;
5067 }
5068
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005069 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005070 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5071 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5072 return true;
5073 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005074
5075 if (onGenericMotionEvent(event)) {
5076 return true;
5077 }
5078
5079 if (mInputEventConsistencyVerifier != null) {
5080 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5081 }
5082 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005083 }
5084
5085 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005086 * Dispatch a hover event.
5087 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005088 * Do not call this method directly.
5089 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005090 * </p>
5091 *
5092 * @param event The motion event to be dispatched.
5093 * @return True if the event was handled by the view, false otherwise.
5094 * @hide
5095 */
5096 protected boolean dispatchHoverEvent(MotionEvent event) {
5097 return onHoverEvent(event);
5098 }
5099
5100 /**
5101 * Dispatch a generic motion event to the view under the first pointer.
5102 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005103 * Do not call this method directly.
5104 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005105 * </p>
5106 *
5107 * @param event The motion event to be dispatched.
5108 * @return True if the event was handled by the view, false otherwise.
5109 * @hide
5110 */
5111 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5112 return false;
5113 }
5114
5115 /**
5116 * Dispatch a generic motion event to the currently focused view.
5117 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005118 * Do not call this method directly.
5119 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005120 * </p>
5121 *
5122 * @param event The motion event to be dispatched.
5123 * @return True if the event was handled by the view, false otherwise.
5124 * @hide
5125 */
5126 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5127 return false;
5128 }
5129
5130 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005131 * Dispatch a pointer event.
5132 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005133 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5134 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5135 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005136 * and should not be expected to handle other pointing device features.
5137 * </p>
5138 *
5139 * @param event The motion event to be dispatched.
5140 * @return True if the event was handled by the view, false otherwise.
5141 * @hide
5142 */
5143 public final boolean dispatchPointerEvent(MotionEvent event) {
5144 if (event.isTouchEvent()) {
5145 return dispatchTouchEvent(event);
5146 } else {
5147 return dispatchGenericMotionEvent(event);
5148 }
5149 }
5150
5151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 * Called when the window containing this view gains or loses window focus.
5153 * ViewGroups should override to route to their children.
5154 *
5155 * @param hasFocus True if the window containing this view now has focus,
5156 * false otherwise.
5157 */
5158 public void dispatchWindowFocusChanged(boolean hasFocus) {
5159 onWindowFocusChanged(hasFocus);
5160 }
5161
5162 /**
5163 * Called when the window containing this view gains or loses focus. Note
5164 * that this is separate from view focus: to receive key events, both
5165 * your view and its window must have focus. If a window is displayed
5166 * on top of yours that takes input focus, then your own window will lose
5167 * focus but the view focus will remain unchanged.
5168 *
5169 * @param hasWindowFocus True if the window containing this view now has
5170 * focus, false otherwise.
5171 */
5172 public void onWindowFocusChanged(boolean hasWindowFocus) {
5173 InputMethodManager imm = InputMethodManager.peekInstance();
5174 if (!hasWindowFocus) {
5175 if (isPressed()) {
5176 setPressed(false);
5177 }
5178 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5179 imm.focusOut(this);
5180 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005181 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005182 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005183 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5185 imm.focusIn(this);
5186 }
5187 refreshDrawableState();
5188 }
5189
5190 /**
5191 * Returns true if this view is in a window that currently has window focus.
5192 * Note that this is not the same as the view itself having focus.
5193 *
5194 * @return True if this view is in a window that currently has window focus.
5195 */
5196 public boolean hasWindowFocus() {
5197 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5198 }
5199
5200 /**
Adam Powell326d8082009-12-09 15:10:07 -08005201 * Dispatch a view visibility change down the view hierarchy.
5202 * ViewGroups should override to route to their children.
5203 * @param changedView The view whose visibility changed. Could be 'this' or
5204 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005205 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5206 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005207 */
5208 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5209 onVisibilityChanged(changedView, visibility);
5210 }
5211
5212 /**
5213 * Called when the visibility of the view or an ancestor of the view is changed.
5214 * @param changedView The view whose visibility changed. Could be 'this' or
5215 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005216 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5217 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005218 */
5219 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005220 if (visibility == VISIBLE) {
5221 if (mAttachInfo != null) {
5222 initialAwakenScrollBars();
5223 } else {
5224 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5225 }
5226 }
Adam Powell326d8082009-12-09 15:10:07 -08005227 }
5228
5229 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005230 * Dispatch a hint about whether this view is displayed. For instance, when
5231 * a View moves out of the screen, it might receives a display hint indicating
5232 * the view is not displayed. Applications should not <em>rely</em> on this hint
5233 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005234 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005235 * @param hint A hint about whether or not this view is displayed:
5236 * {@link #VISIBLE} or {@link #INVISIBLE}.
5237 */
5238 public void dispatchDisplayHint(int hint) {
5239 onDisplayHint(hint);
5240 }
5241
5242 /**
5243 * Gives this view a hint about whether is displayed or not. For instance, when
5244 * a View moves out of the screen, it might receives a display hint indicating
5245 * the view is not displayed. Applications should not <em>rely</em> on this hint
5246 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005247 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005248 * @param hint A hint about whether or not this view is displayed:
5249 * {@link #VISIBLE} or {@link #INVISIBLE}.
5250 */
5251 protected void onDisplayHint(int hint) {
5252 }
5253
5254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 * Dispatch a window visibility change down the view hierarchy.
5256 * ViewGroups should override to route to their children.
5257 *
5258 * @param visibility The new visibility of the window.
5259 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005260 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 */
5262 public void dispatchWindowVisibilityChanged(int visibility) {
5263 onWindowVisibilityChanged(visibility);
5264 }
5265
5266 /**
5267 * Called when the window containing has change its visibility
5268 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5269 * that this tells you whether or not your window is being made visible
5270 * to the window manager; this does <em>not</em> tell you whether or not
5271 * your window is obscured by other windows on the screen, even if it
5272 * is itself visible.
5273 *
5274 * @param visibility The new visibility of the window.
5275 */
5276 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005277 if (visibility == VISIBLE) {
5278 initialAwakenScrollBars();
5279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 }
5281
5282 /**
5283 * Returns the current visibility of the window this view is attached to
5284 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5285 *
5286 * @return Returns the current visibility of the view's window.
5287 */
5288 public int getWindowVisibility() {
5289 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5290 }
5291
5292 /**
5293 * Retrieve the overall visible display size in which the window this view is
5294 * attached to has been positioned in. This takes into account screen
5295 * decorations above the window, for both cases where the window itself
5296 * is being position inside of them or the window is being placed under
5297 * then and covered insets are used for the window to position its content
5298 * inside. In effect, this tells you the available area where content can
5299 * be placed and remain visible to users.
5300 *
5301 * <p>This function requires an IPC back to the window manager to retrieve
5302 * the requested information, so should not be used in performance critical
5303 * code like drawing.
5304 *
5305 * @param outRect Filled in with the visible display frame. If the view
5306 * is not attached to a window, this is simply the raw display size.
5307 */
5308 public void getWindowVisibleDisplayFrame(Rect outRect) {
5309 if (mAttachInfo != null) {
5310 try {
5311 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5312 } catch (RemoteException e) {
5313 return;
5314 }
5315 // XXX This is really broken, and probably all needs to be done
5316 // in the window manager, and we need to know more about whether
5317 // we want the area behind or in front of the IME.
5318 final Rect insets = mAttachInfo.mVisibleInsets;
5319 outRect.left += insets.left;
5320 outRect.top += insets.top;
5321 outRect.right -= insets.right;
5322 outRect.bottom -= insets.bottom;
5323 return;
5324 }
5325 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005326 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327 }
5328
5329 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005330 * Dispatch a notification about a resource configuration change down
5331 * the view hierarchy.
5332 * ViewGroups should override to route to their children.
5333 *
5334 * @param newConfig The new resource configuration.
5335 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005336 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005337 */
5338 public void dispatchConfigurationChanged(Configuration newConfig) {
5339 onConfigurationChanged(newConfig);
5340 }
5341
5342 /**
5343 * Called when the current configuration of the resources being used
5344 * by the application have changed. You can use this to decide when
5345 * to reload resources that can changed based on orientation and other
5346 * configuration characterstics. You only need to use this if you are
5347 * not relying on the normal {@link android.app.Activity} mechanism of
5348 * recreating the activity instance upon a configuration change.
5349 *
5350 * @param newConfig The new resource configuration.
5351 */
5352 protected void onConfigurationChanged(Configuration newConfig) {
5353 }
5354
5355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 * Private function to aggregate all per-view attributes in to the view
5357 * root.
5358 */
5359 void dispatchCollectViewAttributes(int visibility) {
5360 performCollectViewAttributes(visibility);
5361 }
5362
5363 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005364 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005365 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5366 mAttachInfo.mKeepScreenOn = true;
5367 }
5368 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5369 if (mOnSystemUiVisibilityChangeListener != null) {
5370 mAttachInfo.mHasSystemUiListeners = true;
5371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 }
5373 }
5374
5375 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005376 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005378 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5379 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 ai.mRecomputeGlobalAttributes = true;
5381 }
5382 }
5383 }
5384
5385 /**
5386 * Returns whether the device is currently in touch mode. Touch mode is entered
5387 * once the user begins interacting with the device by touch, and affects various
5388 * things like whether focus is always visible to the user.
5389 *
5390 * @return Whether the device is in touch mode.
5391 */
5392 @ViewDebug.ExportedProperty
5393 public boolean isInTouchMode() {
5394 if (mAttachInfo != null) {
5395 return mAttachInfo.mInTouchMode;
5396 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005397 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 }
5399 }
5400
5401 /**
5402 * Returns the context the view is running in, through which it can
5403 * access the current theme, resources, etc.
5404 *
5405 * @return The view's Context.
5406 */
5407 @ViewDebug.CapturedViewProperty
5408 public final Context getContext() {
5409 return mContext;
5410 }
5411
5412 /**
5413 * Handle a key event before it is processed by any input method
5414 * associated with the view hierarchy. This can be used to intercept
5415 * key events in special situations before the IME consumes them; a
5416 * typical example would be handling the BACK key to update the application's
5417 * UI instead of allowing the IME to see it and close itself.
5418 *
5419 * @param keyCode The value in event.getKeyCode().
5420 * @param event Description of the key event.
5421 * @return If you handled the event, return true. If you want to allow the
5422 * event to be handled by the next receiver, return false.
5423 */
5424 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5425 return false;
5426 }
5427
5428 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005429 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5430 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5432 * is released, if the view is enabled and clickable.
5433 *
5434 * @param keyCode A key code that represents the button pressed, from
5435 * {@link android.view.KeyEvent}.
5436 * @param event The KeyEvent object that defines the button action.
5437 */
5438 public boolean onKeyDown(int keyCode, KeyEvent event) {
5439 boolean result = false;
5440
5441 switch (keyCode) {
5442 case KeyEvent.KEYCODE_DPAD_CENTER:
5443 case KeyEvent.KEYCODE_ENTER: {
5444 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5445 return true;
5446 }
5447 // Long clickable items don't necessarily have to be clickable
5448 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5449 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5450 (event.getRepeatCount() == 0)) {
5451 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005452 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 return true;
5454 }
5455 break;
5456 }
5457 }
5458 return result;
5459 }
5460
5461 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005462 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5463 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5464 * the event).
5465 */
5466 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5467 return false;
5468 }
5469
5470 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005471 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5472 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5474 * {@link KeyEvent#KEYCODE_ENTER} is released.
5475 *
5476 * @param keyCode A key code that represents the button pressed, from
5477 * {@link android.view.KeyEvent}.
5478 * @param event The KeyEvent object that defines the button action.
5479 */
5480 public boolean onKeyUp(int keyCode, KeyEvent event) {
5481 boolean result = false;
5482
5483 switch (keyCode) {
5484 case KeyEvent.KEYCODE_DPAD_CENTER:
5485 case KeyEvent.KEYCODE_ENTER: {
5486 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5487 return true;
5488 }
5489 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5490 setPressed(false);
5491
5492 if (!mHasPerformedLongPress) {
5493 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005494 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495
5496 result = performClick();
5497 }
5498 }
5499 break;
5500 }
5501 }
5502 return result;
5503 }
5504
5505 /**
5506 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5507 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5508 * the event).
5509 *
5510 * @param keyCode A key code that represents the button pressed, from
5511 * {@link android.view.KeyEvent}.
5512 * @param repeatCount The number of times the action was made.
5513 * @param event The KeyEvent object that defines the button action.
5514 */
5515 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5516 return false;
5517 }
5518
5519 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005520 * Called on the focused view when a key shortcut event is not handled.
5521 * Override this method to implement local key shortcuts for the View.
5522 * Key shortcuts can also be implemented by setting the
5523 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 *
5525 * @param keyCode The value in event.getKeyCode().
5526 * @param event Description of the key event.
5527 * @return If you handled the event, return true. If you want to allow the
5528 * event to be handled by the next receiver, return false.
5529 */
5530 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5531 return false;
5532 }
5533
5534 /**
5535 * Check whether the called view is a text editor, in which case it
5536 * would make sense to automatically display a soft input window for
5537 * it. Subclasses should override this if they implement
5538 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005539 * a call on that method would return a non-null InputConnection, and
5540 * they are really a first-class editor that the user would normally
5541 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005542 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005543 * <p>The default implementation always returns false. This does
5544 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5545 * will not be called or the user can not otherwise perform edits on your
5546 * view; it is just a hint to the system that this is not the primary
5547 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005548 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 * @return Returns true if this view is a text editor, else false.
5550 */
5551 public boolean onCheckIsTextEditor() {
5552 return false;
5553 }
Romain Guy8506ab42009-06-11 17:35:47 -07005554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 /**
5556 * Create a new InputConnection for an InputMethod to interact
5557 * with the view. The default implementation returns null, since it doesn't
5558 * support input methods. You can override this to implement such support.
5559 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005560 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 * <p>When implementing this, you probably also want to implement
5562 * {@link #onCheckIsTextEditor()} to indicate you will return a
5563 * non-null InputConnection.
5564 *
5565 * @param outAttrs Fill in with attribute information about the connection.
5566 */
5567 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5568 return null;
5569 }
5570
5571 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005572 * Called by the {@link android.view.inputmethod.InputMethodManager}
5573 * when a view who is not the current
5574 * input connection target is trying to make a call on the manager. The
5575 * default implementation returns false; you can override this to return
5576 * true for certain views if you are performing InputConnection proxying
5577 * to them.
5578 * @param view The View that is making the InputMethodManager call.
5579 * @return Return true to allow the call, false to reject.
5580 */
5581 public boolean checkInputConnectionProxy(View view) {
5582 return false;
5583 }
Romain Guy8506ab42009-06-11 17:35:47 -07005584
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586 * Show the context menu for this view. It is not safe to hold on to the
5587 * menu after returning from this method.
5588 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005589 * You should normally not overload this method. Overload
5590 * {@link #onCreateContextMenu(ContextMenu)} or define an
5591 * {@link OnCreateContextMenuListener} to add items to the context menu.
5592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 * @param menu The context menu to populate
5594 */
5595 public void createContextMenu(ContextMenu menu) {
5596 ContextMenuInfo menuInfo = getContextMenuInfo();
5597
5598 // Sets the current menu info so all items added to menu will have
5599 // my extra info set.
5600 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5601
5602 onCreateContextMenu(menu);
5603 if (mOnCreateContextMenuListener != null) {
5604 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5605 }
5606
5607 // Clear the extra information so subsequent items that aren't mine don't
5608 // have my extra info.
5609 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5610
5611 if (mParent != null) {
5612 mParent.createContextMenu(menu);
5613 }
5614 }
5615
5616 /**
5617 * Views should implement this if they have extra information to associate
5618 * with the context menu. The return result is supplied as a parameter to
5619 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5620 * callback.
5621 *
5622 * @return Extra information about the item for which the context menu
5623 * should be shown. This information will vary across different
5624 * subclasses of View.
5625 */
5626 protected ContextMenuInfo getContextMenuInfo() {
5627 return null;
5628 }
5629
5630 /**
5631 * Views should implement this if the view itself is going to add items to
5632 * the context menu.
5633 *
5634 * @param menu the context menu to populate
5635 */
5636 protected void onCreateContextMenu(ContextMenu menu) {
5637 }
5638
5639 /**
5640 * Implement this method to handle trackball motion events. The
5641 * <em>relative</em> movement of the trackball since the last event
5642 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5643 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5644 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5645 * they will often be fractional values, representing the more fine-grained
5646 * movement information available from a trackball).
5647 *
5648 * @param event The motion event.
5649 * @return True if the event was handled, false otherwise.
5650 */
5651 public boolean onTrackballEvent(MotionEvent event) {
5652 return false;
5653 }
5654
5655 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005656 * Implement this method to handle generic motion events.
5657 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005658 * Generic motion events describe joystick movements, mouse hovers, track pad
5659 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005660 * {@link MotionEvent#getSource() source} of the motion event specifies
5661 * the class of input that was received. Implementations of this method
5662 * must examine the bits in the source before processing the event.
5663 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005664 * </p><p>
5665 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5666 * are delivered to the view under the pointer. All other generic motion events are
5667 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005668 * </p>
5669 * <code>
5670 * public boolean onGenericMotionEvent(MotionEvent event) {
5671 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005672 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5673 * // process the joystick movement...
5674 * return true;
5675 * }
5676 * }
5677 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5678 * switch (event.getAction()) {
5679 * case MotionEvent.ACTION_HOVER_MOVE:
5680 * // process the mouse hover movement...
5681 * return true;
5682 * case MotionEvent.ACTION_SCROLL:
5683 * // process the scroll wheel movement...
5684 * return true;
5685 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005686 * }
5687 * return super.onGenericMotionEvent(event);
5688 * }
5689 * </code>
5690 *
5691 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005692 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005693 */
5694 public boolean onGenericMotionEvent(MotionEvent event) {
5695 return false;
5696 }
5697
5698 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005699 * Implement this method to handle hover events.
5700 * <p>
5701 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5702 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5703 * </p><p>
5704 * The view receives hover enter as the pointer enters the bounds of the view and hover
5705 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005706 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005707 * </p><p>
5708 * If the view would like to handle the hover event itself and prevent its children
5709 * from receiving hover, it should return true from this method. If this method returns
5710 * true and a child has already received a hover enter event, the child will
5711 * automatically receive a hover exit event.
5712 * </p><p>
5713 * The default implementation sets the hovered state of the view if the view is
5714 * clickable.
5715 * </p>
5716 *
5717 * @param event The motion event that describes the hover.
5718 * @return True if this view handled the hover event and does not want its children
5719 * to receive the hover event.
5720 */
5721 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005722 switch (event.getAction()) {
5723 case MotionEvent.ACTION_HOVER_ENTER:
5724 setHovered(true);
5725 break;
5726
5727 case MotionEvent.ACTION_HOVER_EXIT:
5728 setHovered(false);
5729 break;
5730 }
5731
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005732 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005733 }
5734
5735 /**
5736 * Returns true if the view is currently hovered.
5737 *
5738 * @return True if the view is currently hovered.
5739 */
5740 public boolean isHovered() {
5741 return (mPrivateFlags & HOVERED) != 0;
5742 }
5743
5744 /**
5745 * Sets whether the view is currently hovered.
5746 *
5747 * @param hovered True if the view is hovered.
5748 */
5749 public void setHovered(boolean hovered) {
5750 if (hovered) {
5751 if ((mPrivateFlags & HOVERED) == 0) {
5752 mPrivateFlags |= HOVERED;
5753 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005754 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005755 }
5756 } else {
5757 if ((mPrivateFlags & HOVERED) != 0) {
5758 mPrivateFlags &= ~HOVERED;
5759 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005760 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005761 }
5762 }
5763 }
5764
5765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 * Implement this method to handle touch screen motion events.
5767 *
5768 * @param event The motion event.
5769 * @return True if the event was handled, false otherwise.
5770 */
5771 public boolean onTouchEvent(MotionEvent event) {
5772 final int viewFlags = mViewFlags;
5773
5774 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005775 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5776 mPrivateFlags &= ~PRESSED;
5777 refreshDrawableState();
5778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 // A disabled view that is clickable still consumes the touch
5780 // events, it just doesn't respond to them.
5781 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5782 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5783 }
5784
5785 if (mTouchDelegate != null) {
5786 if (mTouchDelegate.onTouchEvent(event)) {
5787 return true;
5788 }
5789 }
5790
5791 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5792 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5793 switch (event.getAction()) {
5794 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005795 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5796 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 // take focus if we don't have it already and we should in
5798 // touch mode.
5799 boolean focusTaken = false;
5800 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5801 focusTaken = requestFocus();
5802 }
5803
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005804 if (prepressed) {
5805 // The button is being released before we actually
5806 // showed it as pressed. Make it show the pressed
5807 // state now (before scheduling the click) to ensure
5808 // the user sees it.
5809 mPrivateFlags |= PRESSED;
5810 refreshDrawableState();
5811 }
Joe Malin32736f02011-01-19 16:14:20 -08005812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 if (!mHasPerformedLongPress) {
5814 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005815 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816
5817 // Only perform take click actions if we were in the pressed state
5818 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005819 // Use a Runnable and post this rather than calling
5820 // performClick directly. This lets other visual state
5821 // of the view update before click actions start.
5822 if (mPerformClick == null) {
5823 mPerformClick = new PerformClick();
5824 }
5825 if (!post(mPerformClick)) {
5826 performClick();
5827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 }
5829 }
5830
5831 if (mUnsetPressedState == null) {
5832 mUnsetPressedState = new UnsetPressedState();
5833 }
5834
Adam Powelle14579b2009-12-16 18:39:52 -08005835 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005836 postDelayed(mUnsetPressedState,
5837 ViewConfiguration.getPressedStateDuration());
5838 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 // If the post failed, unpress right now
5840 mUnsetPressedState.run();
5841 }
Adam Powelle14579b2009-12-16 18:39:52 -08005842 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 }
5844 break;
5845
5846 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005847 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005848
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005849 if (performButtonActionOnTouchDown(event)) {
5850 break;
5851 }
5852
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005853 // Walk up the hierarchy to determine if we're inside a scrolling container.
5854 boolean isInScrollingContainer = false;
5855 ViewParent p = getParent();
5856 while (p != null && p instanceof ViewGroup) {
5857 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5858 isInScrollingContainer = true;
5859 break;
5860 }
5861 p = p.getParent();
5862 }
5863
5864 // For views inside a scrolling container, delay the pressed feedback for
5865 // a short period in case this is a scroll.
5866 if (isInScrollingContainer) {
5867 mPrivateFlags |= PREPRESSED;
5868 if (mPendingCheckForTap == null) {
5869 mPendingCheckForTap = new CheckForTap();
5870 }
5871 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5872 } else {
5873 // Not inside a scrolling container, so show the feedback right away
5874 mPrivateFlags |= PRESSED;
5875 refreshDrawableState();
5876 checkForLongClick(0);
5877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 break;
5879
5880 case MotionEvent.ACTION_CANCEL:
5881 mPrivateFlags &= ~PRESSED;
5882 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005883 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 break;
5885
5886 case MotionEvent.ACTION_MOVE:
5887 final int x = (int) event.getX();
5888 final int y = (int) event.getY();
5889
5890 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005891 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005893 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005895 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005896 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897
5898 // Need to switch from pressed to not pressed
5899 mPrivateFlags &= ~PRESSED;
5900 refreshDrawableState();
5901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 }
5903 break;
5904 }
5905 return true;
5906 }
5907
5908 return false;
5909 }
5910
5911 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005912 * Remove the longpress detection timer.
5913 */
5914 private void removeLongPressCallback() {
5915 if (mPendingCheckForLongPress != null) {
5916 removeCallbacks(mPendingCheckForLongPress);
5917 }
5918 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005919
5920 /**
5921 * Remove the pending click action
5922 */
5923 private void removePerformClickCallback() {
5924 if (mPerformClick != null) {
5925 removeCallbacks(mPerformClick);
5926 }
5927 }
5928
Adam Powelle14579b2009-12-16 18:39:52 -08005929 /**
Romain Guya440b002010-02-24 15:57:54 -08005930 * Remove the prepress detection timer.
5931 */
5932 private void removeUnsetPressCallback() {
5933 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5934 setPressed(false);
5935 removeCallbacks(mUnsetPressedState);
5936 }
5937 }
5938
5939 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005940 * Remove the tap detection timer.
5941 */
5942 private void removeTapCallback() {
5943 if (mPendingCheckForTap != null) {
5944 mPrivateFlags &= ~PREPRESSED;
5945 removeCallbacks(mPendingCheckForTap);
5946 }
5947 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005948
5949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 * Cancels a pending long press. Your subclass can use this if you
5951 * want the context menu to come up if the user presses and holds
5952 * at the same place, but you don't want it to come up if they press
5953 * and then move around enough to cause scrolling.
5954 */
5955 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005956 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005957
5958 /*
5959 * The prepressed state handled by the tap callback is a display
5960 * construct, but the tap callback will post a long press callback
5961 * less its own timeout. Remove it here.
5962 */
5963 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 }
5965
5966 /**
5967 * Sets the TouchDelegate for this View.
5968 */
5969 public void setTouchDelegate(TouchDelegate delegate) {
5970 mTouchDelegate = delegate;
5971 }
5972
5973 /**
5974 * Gets the TouchDelegate for this View.
5975 */
5976 public TouchDelegate getTouchDelegate() {
5977 return mTouchDelegate;
5978 }
5979
5980 /**
5981 * Set flags controlling behavior of this view.
5982 *
5983 * @param flags Constant indicating the value which should be set
5984 * @param mask Constant indicating the bit range that should be changed
5985 */
5986 void setFlags(int flags, int mask) {
5987 int old = mViewFlags;
5988 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5989
5990 int changed = mViewFlags ^ old;
5991 if (changed == 0) {
5992 return;
5993 }
5994 int privateFlags = mPrivateFlags;
5995
5996 /* Check if the FOCUSABLE bit has changed */
5997 if (((changed & FOCUSABLE_MASK) != 0) &&
5998 ((privateFlags & HAS_BOUNDS) !=0)) {
5999 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6000 && ((privateFlags & FOCUSED) != 0)) {
6001 /* Give up focus if we are no longer focusable */
6002 clearFocus();
6003 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6004 && ((privateFlags & FOCUSED) == 0)) {
6005 /*
6006 * Tell the view system that we are now available to take focus
6007 * if no one else already has it.
6008 */
6009 if (mParent != null) mParent.focusableViewAvailable(this);
6010 }
6011 }
6012
6013 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6014 if ((changed & VISIBILITY_MASK) != 0) {
6015 /*
6016 * If this view is becoming visible, set the DRAWN flag so that
6017 * the next invalidate() will not be skipped.
6018 */
6019 mPrivateFlags |= DRAWN;
6020
6021 needGlobalAttributesUpdate(true);
6022
6023 // a view becoming visible is worth notifying the parent
6024 // about in case nothing has focus. even if this specific view
6025 // isn't focusable, it may contain something that is, so let
6026 // the root view try to give this focus if nothing else does.
6027 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6028 mParent.focusableViewAvailable(this);
6029 }
6030 }
6031 }
6032
6033 /* Check if the GONE bit has changed */
6034 if ((changed & GONE) != 0) {
6035 needGlobalAttributesUpdate(false);
6036 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006037 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038
Romain Guyecd80ee2009-12-03 17:13:02 -08006039 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6040 if (hasFocus()) clearFocus();
6041 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 }
6043 if (mAttachInfo != null) {
6044 mAttachInfo.mViewVisibilityChanged = true;
6045 }
6046 }
6047
6048 /* Check if the VISIBLE bit has changed */
6049 if ((changed & INVISIBLE) != 0) {
6050 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08006051 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052
6053 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6054 // root view becoming invisible shouldn't clear focus
6055 if (getRootView() != this) {
6056 clearFocus();
6057 }
6058 }
6059 if (mAttachInfo != null) {
6060 mAttachInfo.mViewVisibilityChanged = true;
6061 }
6062 }
6063
Adam Powell326d8082009-12-09 15:10:07 -08006064 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006065 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006066 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6067 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006068 }
Adam Powell326d8082009-12-09 15:10:07 -08006069 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6070 }
6071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6073 destroyDrawingCache();
6074 }
6075
6076 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6077 destroyDrawingCache();
6078 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006079 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 }
6081
6082 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6083 destroyDrawingCache();
6084 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6085 }
6086
6087 if ((changed & DRAW_MASK) != 0) {
6088 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6089 if (mBGDrawable != null) {
6090 mPrivateFlags &= ~SKIP_DRAW;
6091 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6092 } else {
6093 mPrivateFlags |= SKIP_DRAW;
6094 }
6095 } else {
6096 mPrivateFlags &= ~SKIP_DRAW;
6097 }
6098 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006099 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 }
6101
6102 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006103 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 mParent.recomputeViewAttributes(this);
6105 }
6106 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006107
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006108 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006109 requestLayout();
6110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 }
6112
6113 /**
6114 * Change the view's z order in the tree, so it's on top of other sibling
6115 * views
6116 */
6117 public void bringToFront() {
6118 if (mParent != null) {
6119 mParent.bringChildToFront(this);
6120 }
6121 }
6122
6123 /**
6124 * This is called in response to an internal scroll in this view (i.e., the
6125 * view scrolled its own contents). This is typically as a result of
6126 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6127 * called.
6128 *
6129 * @param l Current horizontal scroll origin.
6130 * @param t Current vertical scroll origin.
6131 * @param oldl Previous horizontal scroll origin.
6132 * @param oldt Previous vertical scroll origin.
6133 */
6134 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
6135 mBackgroundSizeChanged = true;
6136
6137 final AttachInfo ai = mAttachInfo;
6138 if (ai != null) {
6139 ai.mViewScrollChanged = true;
6140 }
6141 }
6142
6143 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006144 * Interface definition for a callback to be invoked when the layout bounds of a view
6145 * changes due to layout processing.
6146 */
6147 public interface OnLayoutChangeListener {
6148 /**
6149 * Called when the focus state of a view has changed.
6150 *
6151 * @param v The view whose state has changed.
6152 * @param left The new value of the view's left property.
6153 * @param top The new value of the view's top property.
6154 * @param right The new value of the view's right property.
6155 * @param bottom The new value of the view's bottom property.
6156 * @param oldLeft The previous value of the view's left property.
6157 * @param oldTop The previous value of the view's top property.
6158 * @param oldRight The previous value of the view's right property.
6159 * @param oldBottom The previous value of the view's bottom property.
6160 */
6161 void onLayoutChange(View v, int left, int top, int right, int bottom,
6162 int oldLeft, int oldTop, int oldRight, int oldBottom);
6163 }
6164
6165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 * This is called during layout when the size of this view has changed. If
6167 * you were just added to the view hierarchy, you're called with the old
6168 * values of 0.
6169 *
6170 * @param w Current width of this view.
6171 * @param h Current height of this view.
6172 * @param oldw Old width of this view.
6173 * @param oldh Old height of this view.
6174 */
6175 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6176 }
6177
6178 /**
6179 * Called by draw to draw the child views. This may be overridden
6180 * by derived classes to gain control just before its children are drawn
6181 * (but after its own view has been drawn).
6182 * @param canvas the canvas on which to draw the view
6183 */
6184 protected void dispatchDraw(Canvas canvas) {
6185 }
6186
6187 /**
6188 * Gets the parent of this view. Note that the parent is a
6189 * ViewParent and not necessarily a View.
6190 *
6191 * @return Parent of this view.
6192 */
6193 public final ViewParent getParent() {
6194 return mParent;
6195 }
6196
6197 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006198 * Set the horizontal scrolled position of your view. This will cause a call to
6199 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6200 * invalidated.
6201 * @param value the x position to scroll to
6202 */
6203 public void setScrollX(int value) {
6204 scrollTo(value, mScrollY);
6205 }
6206
6207 /**
6208 * Set the vertical scrolled position of your view. This will cause a call to
6209 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6210 * invalidated.
6211 * @param value the y position to scroll to
6212 */
6213 public void setScrollY(int value) {
6214 scrollTo(mScrollX, value);
6215 }
6216
6217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 * Return the scrolled left position of this view. This is the left edge of
6219 * the displayed part of your view. You do not need to draw any pixels
6220 * farther left, since those are outside of the frame of your view on
6221 * screen.
6222 *
6223 * @return The left edge of the displayed part of your view, in pixels.
6224 */
6225 public final int getScrollX() {
6226 return mScrollX;
6227 }
6228
6229 /**
6230 * Return the scrolled top position of this view. This is the top edge of
6231 * the displayed part of your view. You do not need to draw any pixels above
6232 * it, since those are outside of the frame of your view on screen.
6233 *
6234 * @return The top edge of the displayed part of your view, in pixels.
6235 */
6236 public final int getScrollY() {
6237 return mScrollY;
6238 }
6239
6240 /**
6241 * Return the width of the your view.
6242 *
6243 * @return The width of your view, in pixels.
6244 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006245 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006246 public final int getWidth() {
6247 return mRight - mLeft;
6248 }
6249
6250 /**
6251 * Return the height of your view.
6252 *
6253 * @return The height of your view, in pixels.
6254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006255 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 public final int getHeight() {
6257 return mBottom - mTop;
6258 }
6259
6260 /**
6261 * Return the visible drawing bounds of your view. Fills in the output
6262 * rectangle with the values from getScrollX(), getScrollY(),
6263 * getWidth(), and getHeight().
6264 *
6265 * @param outRect The (scrolled) drawing bounds of the view.
6266 */
6267 public void getDrawingRect(Rect outRect) {
6268 outRect.left = mScrollX;
6269 outRect.top = mScrollY;
6270 outRect.right = mScrollX + (mRight - mLeft);
6271 outRect.bottom = mScrollY + (mBottom - mTop);
6272 }
6273
6274 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006275 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6276 * raw width component (that is the result is masked by
6277 * {@link #MEASURED_SIZE_MASK}).
6278 *
6279 * @return The raw measured width of this view.
6280 */
6281 public final int getMeasuredWidth() {
6282 return mMeasuredWidth & MEASURED_SIZE_MASK;
6283 }
6284
6285 /**
6286 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006287 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006288 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 * This should be used during measurement and layout calculations only. Use
6290 * {@link #getWidth()} to see how wide a view is after layout.
6291 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006292 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006294 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 return mMeasuredWidth;
6296 }
6297
6298 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006299 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6300 * raw width component (that is the result is masked by
6301 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006303 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 */
6305 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006306 return mMeasuredHeight & MEASURED_SIZE_MASK;
6307 }
6308
6309 /**
6310 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006311 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006312 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6313 * This should be used during measurement and layout calculations only. Use
6314 * {@link #getHeight()} to see how wide a view is after layout.
6315 *
6316 * @return The measured width of this view as a bit mask.
6317 */
6318 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 return mMeasuredHeight;
6320 }
6321
6322 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006323 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6324 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6325 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6326 * and the height component is at the shifted bits
6327 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6328 */
6329 public final int getMeasuredState() {
6330 return (mMeasuredWidth&MEASURED_STATE_MASK)
6331 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6332 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6333 }
6334
6335 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006336 * The transform matrix of this view, which is calculated based on the current
6337 * roation, scale, and pivot properties.
6338 *
6339 * @see #getRotation()
6340 * @see #getScaleX()
6341 * @see #getScaleY()
6342 * @see #getPivotX()
6343 * @see #getPivotY()
6344 * @return The current transform matrix for the view
6345 */
6346 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006347 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006348 return mMatrix;
6349 }
6350
6351 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006352 * Utility function to determine if the value is far enough away from zero to be
6353 * considered non-zero.
6354 * @param value A floating point value to check for zero-ness
6355 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6356 */
6357 private static boolean nonzero(float value) {
6358 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6359 }
6360
6361 /**
Jeff Brown86671742010-09-30 20:00:15 -07006362 * Returns true if the transform matrix is the identity matrix.
6363 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006364 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006365 * @return True if the transform matrix is the identity matrix, false otherwise.
6366 */
Jeff Brown86671742010-09-30 20:00:15 -07006367 final boolean hasIdentityMatrix() {
6368 updateMatrix();
6369 return mMatrixIsIdentity;
6370 }
6371
6372 /**
6373 * Recomputes the transform matrix if necessary.
6374 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006375 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006376 if (mMatrixDirty) {
6377 // transform-related properties have changed since the last time someone
6378 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006379
6380 // Figure out if we need to update the pivot point
6381 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006382 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006383 mPrevWidth = mRight - mLeft;
6384 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006385 mPivotX = mPrevWidth / 2f;
6386 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006387 }
6388 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006389 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006390 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6391 mMatrix.setTranslate(mTranslationX, mTranslationY);
6392 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6393 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6394 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006395 if (mCamera == null) {
6396 mCamera = new Camera();
6397 matrix3D = new Matrix();
6398 }
6399 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006400 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006401 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006402 mCamera.getMatrix(matrix3D);
6403 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006404 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006405 mMatrix.postConcat(matrix3D);
6406 mCamera.restore();
6407 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006408 mMatrixDirty = false;
6409 mMatrixIsIdentity = mMatrix.isIdentity();
6410 mInverseMatrixDirty = true;
6411 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006412 }
6413
6414 /**
6415 * Utility method to retrieve the inverse of the current mMatrix property.
6416 * We cache the matrix to avoid recalculating it when transform properties
6417 * have not changed.
6418 *
6419 * @return The inverse of the current matrix of this view.
6420 */
Jeff Brown86671742010-09-30 20:00:15 -07006421 final Matrix getInverseMatrix() {
6422 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006423 if (mInverseMatrixDirty) {
6424 if (mInverseMatrix == null) {
6425 mInverseMatrix = new Matrix();
6426 }
6427 mMatrix.invert(mInverseMatrix);
6428 mInverseMatrixDirty = false;
6429 }
6430 return mInverseMatrix;
6431 }
6432
6433 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006434 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6435 * views are drawn) from the camera to this view. The camera's distance
6436 * affects 3D transformations, for instance rotations around the X and Y
6437 * axis. If the rotationX or rotationY properties are changed and this view is
6438 * large (more than half the size of the screen), it is recommended to always
6439 * use a camera distance that's greater than the height (X axis rotation) or
6440 * the width (Y axis rotation) of this view.</p>
6441 *
6442 * <p>The distance of the camera from the view plane can have an affect on the
6443 * perspective distortion of the view when it is rotated around the x or y axis.
6444 * For example, a large distance will result in a large viewing angle, and there
6445 * will not be much perspective distortion of the view as it rotates. A short
6446 * distance may cause much more perspective distortion upon rotation, and can
6447 * also result in some drawing artifacts if the rotated view ends up partially
6448 * behind the camera (which is why the recommendation is to use a distance at
6449 * least as far as the size of the view, if the view is to be rotated.)</p>
6450 *
6451 * <p>The distance is expressed in "depth pixels." The default distance depends
6452 * on the screen density. For instance, on a medium density display, the
6453 * default distance is 1280. On a high density display, the default distance
6454 * is 1920.</p>
6455 *
6456 * <p>If you want to specify a distance that leads to visually consistent
6457 * results across various densities, use the following formula:</p>
6458 * <pre>
6459 * float scale = context.getResources().getDisplayMetrics().density;
6460 * view.setCameraDistance(distance * scale);
6461 * </pre>
6462 *
6463 * <p>The density scale factor of a high density display is 1.5,
6464 * and 1920 = 1280 * 1.5.</p>
6465 *
6466 * @param distance The distance in "depth pixels", if negative the opposite
6467 * value is used
6468 *
6469 * @see #setRotationX(float)
6470 * @see #setRotationY(float)
6471 */
6472 public void setCameraDistance(float distance) {
6473 invalidateParentCaches();
6474 invalidate(false);
6475
6476 final float dpi = mResources.getDisplayMetrics().densityDpi;
6477 if (mCamera == null) {
6478 mCamera = new Camera();
6479 matrix3D = new Matrix();
6480 }
6481
6482 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6483 mMatrixDirty = true;
6484
6485 invalidate(false);
6486 }
6487
6488 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006489 * The degrees that the view is rotated around the pivot point.
6490 *
Romain Guya5364ee2011-02-24 14:46:04 -08006491 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006492 * @see #getPivotX()
6493 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006494 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006495 * @return The degrees of rotation.
6496 */
6497 public float getRotation() {
6498 return mRotation;
6499 }
6500
6501 /**
Chet Haase897247b2010-09-09 14:54:47 -07006502 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6503 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006504 *
6505 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006506 *
6507 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006508 * @see #getPivotX()
6509 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006510 * @see #setRotationX(float)
6511 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006512 *
6513 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006514 */
6515 public void setRotation(float rotation) {
6516 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006517 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006518 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006519 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006520 mRotation = rotation;
6521 mMatrixDirty = true;
6522 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006523 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006524 }
6525 }
6526
6527 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006528 * The degrees that the view is rotated around the vertical axis through the pivot point.
6529 *
6530 * @see #getPivotX()
6531 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006532 * @see #setRotationY(float)
6533 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006534 * @return The degrees of Y rotation.
6535 */
6536 public float getRotationY() {
6537 return mRotationY;
6538 }
6539
6540 /**
Chet Haase897247b2010-09-09 14:54:47 -07006541 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6542 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6543 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006544 *
6545 * When rotating large views, it is recommended to adjust the camera distance
6546 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006547 *
6548 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006549 *
6550 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006551 * @see #getPivotX()
6552 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006553 * @see #setRotation(float)
6554 * @see #setRotationX(float)
6555 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006556 *
6557 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006558 */
6559 public void setRotationY(float rotationY) {
6560 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006561 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006562 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006563 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006564 mRotationY = rotationY;
6565 mMatrixDirty = true;
6566 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006567 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006568 }
6569 }
6570
6571 /**
6572 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6573 *
6574 * @see #getPivotX()
6575 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006576 * @see #setRotationX(float)
6577 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006578 * @return The degrees of X rotation.
6579 */
6580 public float getRotationX() {
6581 return mRotationX;
6582 }
6583
6584 /**
Chet Haase897247b2010-09-09 14:54:47 -07006585 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6586 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6587 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006588 *
6589 * When rotating large views, it is recommended to adjust the camera distance
6590 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006591 *
6592 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006593 *
6594 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006595 * @see #getPivotX()
6596 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006597 * @see #setRotation(float)
6598 * @see #setRotationY(float)
6599 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006600 *
6601 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006602 */
6603 public void setRotationX(float rotationX) {
6604 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006605 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006606 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006607 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006608 mRotationX = rotationX;
6609 mMatrixDirty = true;
6610 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006611 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006612 }
6613 }
6614
6615 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006616 * The amount that the view is scaled in x around the pivot point, as a proportion of
6617 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6618 *
Joe Onorato93162322010-09-16 15:42:01 -04006619 * <p>By default, this is 1.0f.
6620 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006621 * @see #getPivotX()
6622 * @see #getPivotY()
6623 * @return The scaling factor.
6624 */
6625 public float getScaleX() {
6626 return mScaleX;
6627 }
6628
6629 /**
6630 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6631 * the view's unscaled width. A value of 1 means that no scaling is applied.
6632 *
6633 * @param scaleX The scaling factor.
6634 * @see #getPivotX()
6635 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006636 *
6637 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006638 */
6639 public void setScaleX(float scaleX) {
6640 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006641 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006642 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006643 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006644 mScaleX = scaleX;
6645 mMatrixDirty = true;
6646 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006647 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006648 }
6649 }
6650
6651 /**
6652 * The amount that the view is scaled in y around the pivot point, as a proportion of
6653 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6654 *
Joe Onorato93162322010-09-16 15:42:01 -04006655 * <p>By default, this is 1.0f.
6656 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006657 * @see #getPivotX()
6658 * @see #getPivotY()
6659 * @return The scaling factor.
6660 */
6661 public float getScaleY() {
6662 return mScaleY;
6663 }
6664
6665 /**
6666 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6667 * the view's unscaled width. A value of 1 means that no scaling is applied.
6668 *
6669 * @param scaleY The scaling factor.
6670 * @see #getPivotX()
6671 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006672 *
6673 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006674 */
6675 public void setScaleY(float scaleY) {
6676 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006677 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006678 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006679 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006680 mScaleY = scaleY;
6681 mMatrixDirty = true;
6682 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006683 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006684 }
6685 }
6686
6687 /**
6688 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6689 * and {@link #setScaleX(float) scaled}.
6690 *
6691 * @see #getRotation()
6692 * @see #getScaleX()
6693 * @see #getScaleY()
6694 * @see #getPivotY()
6695 * @return The x location of the pivot point.
6696 */
6697 public float getPivotX() {
6698 return mPivotX;
6699 }
6700
6701 /**
6702 * Sets the x location of the point around which the view is
6703 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006704 * By default, the pivot point is centered on the object.
6705 * Setting this property disables this behavior and causes the view to use only the
6706 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006707 *
6708 * @param pivotX The x location of the pivot point.
6709 * @see #getRotation()
6710 * @see #getScaleX()
6711 * @see #getScaleY()
6712 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006713 *
6714 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006715 */
6716 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006717 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006718 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006719 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006720 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006721 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006722 mPivotX = pivotX;
6723 mMatrixDirty = true;
6724 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006725 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006726 }
6727 }
6728
6729 /**
6730 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6731 * and {@link #setScaleY(float) scaled}.
6732 *
6733 * @see #getRotation()
6734 * @see #getScaleX()
6735 * @see #getScaleY()
6736 * @see #getPivotY()
6737 * @return The y location of the pivot point.
6738 */
6739 public float getPivotY() {
6740 return mPivotY;
6741 }
6742
6743 /**
6744 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006745 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6746 * Setting this property disables this behavior and causes the view to use only the
6747 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006748 *
6749 * @param pivotY The y location of the pivot point.
6750 * @see #getRotation()
6751 * @see #getScaleX()
6752 * @see #getScaleY()
6753 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006754 *
6755 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006756 */
6757 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006758 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006759 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006760 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006761 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006762 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006763 mPivotY = pivotY;
6764 mMatrixDirty = true;
6765 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006766 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006767 }
6768 }
6769
6770 /**
6771 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6772 * completely transparent and 1 means the view is completely opaque.
6773 *
Joe Onorato93162322010-09-16 15:42:01 -04006774 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006775 * @return The opacity of the view.
6776 */
6777 public float getAlpha() {
6778 return mAlpha;
6779 }
6780
6781 /**
Romain Guy171c5922011-01-06 10:04:23 -08006782 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6783 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006784 *
Romain Guy171c5922011-01-06 10:04:23 -08006785 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6786 * responsible for applying the opacity itself. Otherwise, calling this method is
6787 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006788 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006789 *
6790 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006791 *
Joe Malin32736f02011-01-19 16:14:20 -08006792 * @see #setLayerType(int, android.graphics.Paint)
6793 *
Chet Haase73066682010-11-29 15:55:32 -08006794 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006795 */
6796 public void setAlpha(float alpha) {
6797 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006798 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006799 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006800 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006801 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006802 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006803 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006804 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006805 invalidate(false);
6806 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006807 }
6808
6809 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006810 * Faster version of setAlpha() which performs the same steps except there are
6811 * no calls to invalidate(). The caller of this function should perform proper invalidation
6812 * on the parent and this object. The return value indicates whether the subclass handles
6813 * alpha (the return value for onSetAlpha()).
6814 *
6815 * @param alpha The new value for the alpha property
6816 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6817 */
6818 boolean setAlphaNoInvalidation(float alpha) {
6819 mAlpha = alpha;
6820 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6821 if (subclassHandlesAlpha) {
6822 mPrivateFlags |= ALPHA_SET;
6823 } else {
6824 mPrivateFlags &= ~ALPHA_SET;
6825 }
6826 return subclassHandlesAlpha;
6827 }
6828
6829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 * Top position of this view relative to its parent.
6831 *
6832 * @return The top of this view, in pixels.
6833 */
6834 @ViewDebug.CapturedViewProperty
6835 public final int getTop() {
6836 return mTop;
6837 }
6838
6839 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006840 * Sets the top position of this view relative to its parent. This method is meant to be called
6841 * by the layout system and should not generally be called otherwise, because the property
6842 * may be changed at any time by the layout.
6843 *
6844 * @param top The top of this view, in pixels.
6845 */
6846 public final void setTop(int top) {
6847 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006848 updateMatrix();
6849 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006850 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006851 int minTop;
6852 int yLoc;
6853 if (top < mTop) {
6854 minTop = top;
6855 yLoc = top - mTop;
6856 } else {
6857 minTop = mTop;
6858 yLoc = 0;
6859 }
Chet Haasee9140a72011-02-16 16:23:29 -08006860 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006861 }
6862 } else {
6863 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006864 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006865 }
6866
Chet Haaseed032702010-10-01 14:05:54 -07006867 int width = mRight - mLeft;
6868 int oldHeight = mBottom - mTop;
6869
Chet Haase21cd1382010-09-01 17:42:29 -07006870 mTop = top;
6871
Chet Haaseed032702010-10-01 14:05:54 -07006872 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6873
Chet Haase21cd1382010-09-01 17:42:29 -07006874 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006875 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6876 // A change in dimension means an auto-centered pivot point changes, too
6877 mMatrixDirty = true;
6878 }
Chet Haase21cd1382010-09-01 17:42:29 -07006879 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006880 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006881 }
Chet Haase55dbb652010-12-21 20:15:08 -08006882 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006883 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006884 }
6885 }
6886
6887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 * Bottom position of this view relative to its parent.
6889 *
6890 * @return The bottom of this view, in pixels.
6891 */
6892 @ViewDebug.CapturedViewProperty
6893 public final int getBottom() {
6894 return mBottom;
6895 }
6896
6897 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006898 * True if this view has changed since the last time being drawn.
6899 *
6900 * @return The dirty state of this view.
6901 */
6902 public boolean isDirty() {
6903 return (mPrivateFlags & DIRTY_MASK) != 0;
6904 }
6905
6906 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006907 * Sets the bottom position of this view relative to its parent. This method is meant to be
6908 * called by the layout system and should not generally be called otherwise, because the
6909 * property may be changed at any time by the layout.
6910 *
6911 * @param bottom The bottom of this view, in pixels.
6912 */
6913 public final void setBottom(int bottom) {
6914 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006915 updateMatrix();
6916 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006917 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006918 int maxBottom;
6919 if (bottom < mBottom) {
6920 maxBottom = mBottom;
6921 } else {
6922 maxBottom = bottom;
6923 }
Chet Haasee9140a72011-02-16 16:23:29 -08006924 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006925 }
6926 } else {
6927 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006928 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006929 }
6930
Chet Haaseed032702010-10-01 14:05:54 -07006931 int width = mRight - mLeft;
6932 int oldHeight = mBottom - mTop;
6933
Chet Haase21cd1382010-09-01 17:42:29 -07006934 mBottom = bottom;
6935
Chet Haaseed032702010-10-01 14:05:54 -07006936 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6937
Chet Haase21cd1382010-09-01 17:42:29 -07006938 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006939 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6940 // A change in dimension means an auto-centered pivot point changes, too
6941 mMatrixDirty = true;
6942 }
Chet Haase21cd1382010-09-01 17:42:29 -07006943 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006944 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006945 }
Chet Haase55dbb652010-12-21 20:15:08 -08006946 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006947 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006948 }
6949 }
6950
6951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 * Left position of this view relative to its parent.
6953 *
6954 * @return The left edge of this view, in pixels.
6955 */
6956 @ViewDebug.CapturedViewProperty
6957 public final int getLeft() {
6958 return mLeft;
6959 }
6960
6961 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006962 * Sets the left position of this view relative to its parent. This method is meant to be called
6963 * by the layout system and should not generally be called otherwise, because the property
6964 * may be changed at any time by the layout.
6965 *
6966 * @param left The bottom of this view, in pixels.
6967 */
6968 public final void setLeft(int left) {
6969 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006970 updateMatrix();
6971 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006972 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006973 int minLeft;
6974 int xLoc;
6975 if (left < mLeft) {
6976 minLeft = left;
6977 xLoc = left - mLeft;
6978 } else {
6979 minLeft = mLeft;
6980 xLoc = 0;
6981 }
Chet Haasee9140a72011-02-16 16:23:29 -08006982 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006983 }
6984 } else {
6985 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006986 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006987 }
6988
Chet Haaseed032702010-10-01 14:05:54 -07006989 int oldWidth = mRight - mLeft;
6990 int height = mBottom - mTop;
6991
Chet Haase21cd1382010-09-01 17:42:29 -07006992 mLeft = left;
6993
Chet Haaseed032702010-10-01 14:05:54 -07006994 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6995
Chet Haase21cd1382010-09-01 17:42:29 -07006996 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006997 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6998 // A change in dimension means an auto-centered pivot point changes, too
6999 mMatrixDirty = true;
7000 }
Chet Haase21cd1382010-09-01 17:42:29 -07007001 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007002 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007003 }
Chet Haase55dbb652010-12-21 20:15:08 -08007004 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007005 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007006 }
7007 }
7008
7009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007010 * Right position of this view relative to its parent.
7011 *
7012 * @return The right edge of this view, in pixels.
7013 */
7014 @ViewDebug.CapturedViewProperty
7015 public final int getRight() {
7016 return mRight;
7017 }
7018
7019 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007020 * Sets the right position of this view relative to its parent. This method is meant to be called
7021 * by the layout system and should not generally be called otherwise, because the property
7022 * may be changed at any time by the layout.
7023 *
7024 * @param right The bottom of this view, in pixels.
7025 */
7026 public final void setRight(int right) {
7027 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007028 updateMatrix();
7029 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007030 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007031 int maxRight;
7032 if (right < mRight) {
7033 maxRight = mRight;
7034 } else {
7035 maxRight = right;
7036 }
Chet Haasee9140a72011-02-16 16:23:29 -08007037 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007038 }
7039 } else {
7040 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007041 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007042 }
7043
Chet Haaseed032702010-10-01 14:05:54 -07007044 int oldWidth = mRight - mLeft;
7045 int height = mBottom - mTop;
7046
Chet Haase21cd1382010-09-01 17:42:29 -07007047 mRight = right;
7048
Chet Haaseed032702010-10-01 14:05:54 -07007049 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7050
Chet Haase21cd1382010-09-01 17:42:29 -07007051 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007052 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7053 // A change in dimension means an auto-centered pivot point changes, too
7054 mMatrixDirty = true;
7055 }
Chet Haase21cd1382010-09-01 17:42:29 -07007056 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007057 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007058 }
Chet Haase55dbb652010-12-21 20:15:08 -08007059 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007060 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007061 }
7062 }
7063
7064 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007065 * The visual x position of this view, in pixels. This is equivalent to the
7066 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007067 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007068 *
Chet Haasedf030d22010-07-30 17:22:38 -07007069 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007070 */
Chet Haasedf030d22010-07-30 17:22:38 -07007071 public float getX() {
7072 return mLeft + mTranslationX;
7073 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007074
Chet Haasedf030d22010-07-30 17:22:38 -07007075 /**
7076 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7077 * {@link #setTranslationX(float) translationX} property to be the difference between
7078 * the x value passed in and the current {@link #getLeft() left} property.
7079 *
7080 * @param x The visual x position of this view, in pixels.
7081 */
7082 public void setX(float x) {
7083 setTranslationX(x - mLeft);
7084 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007085
Chet Haasedf030d22010-07-30 17:22:38 -07007086 /**
7087 * The visual y position of this view, in pixels. This is equivalent to the
7088 * {@link #setTranslationY(float) translationY} property plus the current
7089 * {@link #getTop() top} property.
7090 *
7091 * @return The visual y position of this view, in pixels.
7092 */
7093 public float getY() {
7094 return mTop + mTranslationY;
7095 }
7096
7097 /**
7098 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7099 * {@link #setTranslationY(float) translationY} property to be the difference between
7100 * the y value passed in and the current {@link #getTop() top} property.
7101 *
7102 * @param y The visual y position of this view, in pixels.
7103 */
7104 public void setY(float y) {
7105 setTranslationY(y - mTop);
7106 }
7107
7108
7109 /**
7110 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7111 * This position is post-layout, in addition to wherever the object's
7112 * layout placed it.
7113 *
7114 * @return The horizontal position of this view relative to its left position, in pixels.
7115 */
7116 public float getTranslationX() {
7117 return mTranslationX;
7118 }
7119
7120 /**
7121 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7122 * This effectively positions the object post-layout, in addition to wherever the object's
7123 * layout placed it.
7124 *
7125 * @param translationX The horizontal position of this view relative to its left position,
7126 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007127 *
7128 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007129 */
7130 public void setTranslationX(float translationX) {
7131 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007132 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007133 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007134 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007135 mTranslationX = translationX;
7136 mMatrixDirty = true;
7137 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007138 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007139 }
7140 }
7141
7142 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007143 * The horizontal location of this view relative to its {@link #getTop() top} position.
7144 * This position is post-layout, in addition to wherever the object's
7145 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007146 *
Chet Haasedf030d22010-07-30 17:22:38 -07007147 * @return The vertical position of this view relative to its top position,
7148 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 */
Chet Haasedf030d22010-07-30 17:22:38 -07007150 public float getTranslationY() {
7151 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 }
7153
7154 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007155 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7156 * This effectively positions the object post-layout, in addition to wherever the object's
7157 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007158 *
Chet Haasedf030d22010-07-30 17:22:38 -07007159 * @param translationY The vertical position of this view relative to its top position,
7160 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007161 *
7162 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007163 */
Chet Haasedf030d22010-07-30 17:22:38 -07007164 public void setTranslationY(float translationY) {
7165 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007166 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007167 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007168 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007169 mTranslationY = translationY;
7170 mMatrixDirty = true;
7171 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007172 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007173 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007174 }
7175
7176 /**
Romain Guyda489792011-02-03 01:05:15 -08007177 * @hide
7178 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007179 public void setFastTranslationX(float x) {
7180 mTranslationX = x;
7181 mMatrixDirty = true;
7182 }
7183
7184 /**
7185 * @hide
7186 */
7187 public void setFastTranslationY(float y) {
7188 mTranslationY = y;
7189 mMatrixDirty = true;
7190 }
7191
7192 /**
7193 * @hide
7194 */
Romain Guyda489792011-02-03 01:05:15 -08007195 public void setFastX(float x) {
7196 mTranslationX = x - mLeft;
7197 mMatrixDirty = true;
7198 }
7199
7200 /**
7201 * @hide
7202 */
7203 public void setFastY(float y) {
7204 mTranslationY = y - mTop;
7205 mMatrixDirty = true;
7206 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007207
Romain Guyda489792011-02-03 01:05:15 -08007208 /**
7209 * @hide
7210 */
7211 public void setFastScaleX(float x) {
7212 mScaleX = x;
7213 mMatrixDirty = true;
7214 }
7215
7216 /**
7217 * @hide
7218 */
7219 public void setFastScaleY(float y) {
7220 mScaleY = y;
7221 mMatrixDirty = true;
7222 }
7223
7224 /**
7225 * @hide
7226 */
7227 public void setFastAlpha(float alpha) {
7228 mAlpha = alpha;
7229 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007230
Romain Guyda489792011-02-03 01:05:15 -08007231 /**
7232 * @hide
7233 */
7234 public void setFastRotationY(float y) {
7235 mRotationY = y;
7236 mMatrixDirty = true;
7237 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007238
Romain Guyda489792011-02-03 01:05:15 -08007239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 * Hit rectangle in parent's coordinates
7241 *
7242 * @param outRect The hit rectangle of the view.
7243 */
7244 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007245 updateMatrix();
7246 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 outRect.set(mLeft, mTop, mRight, mBottom);
7248 } else {
7249 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007250 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007251 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007252 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7253 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007254 }
7255 }
7256
7257 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007258 * Determines whether the given point, in local coordinates is inside the view.
7259 */
7260 /*package*/ final boolean pointInView(float localX, float localY) {
7261 return localX >= 0 && localX < (mRight - mLeft)
7262 && localY >= 0 && localY < (mBottom - mTop);
7263 }
7264
7265 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007266 * Utility method to determine whether the given point, in local coordinates,
7267 * is inside the view, where the area of the view is expanded by the slop factor.
7268 * This method is called while processing touch-move events to determine if the event
7269 * is still within the view.
7270 */
7271 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007272 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007273 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 }
7275
7276 /**
7277 * When a view has focus and the user navigates away from it, the next view is searched for
7278 * starting from the rectangle filled in by this method.
7279 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007280 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7281 * of the view. However, if your view maintains some idea of internal selection,
7282 * such as a cursor, or a selected row or column, you should override this method and
7283 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 *
7285 * @param r The rectangle to fill in, in this view's coordinates.
7286 */
7287 public void getFocusedRect(Rect r) {
7288 getDrawingRect(r);
7289 }
7290
7291 /**
7292 * If some part of this view is not clipped by any of its parents, then
7293 * return that area in r in global (root) coordinates. To convert r to local
7294 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7295 * -globalOffset.y)) If the view is completely clipped or translated out,
7296 * return false.
7297 *
7298 * @param r If true is returned, r holds the global coordinates of the
7299 * visible portion of this view.
7300 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7301 * between this view and its root. globalOffet may be null.
7302 * @return true if r is non-empty (i.e. part of the view is visible at the
7303 * root level.
7304 */
7305 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7306 int width = mRight - mLeft;
7307 int height = mBottom - mTop;
7308 if (width > 0 && height > 0) {
7309 r.set(0, 0, width, height);
7310 if (globalOffset != null) {
7311 globalOffset.set(-mScrollX, -mScrollY);
7312 }
7313 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7314 }
7315 return false;
7316 }
7317
7318 public final boolean getGlobalVisibleRect(Rect r) {
7319 return getGlobalVisibleRect(r, null);
7320 }
7321
7322 public final boolean getLocalVisibleRect(Rect r) {
7323 Point offset = new Point();
7324 if (getGlobalVisibleRect(r, offset)) {
7325 r.offset(-offset.x, -offset.y); // make r local
7326 return true;
7327 }
7328 return false;
7329 }
7330
7331 /**
7332 * Offset this view's vertical location by the specified number of pixels.
7333 *
7334 * @param offset the number of pixels to offset the view by
7335 */
7336 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007337 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007338 updateMatrix();
7339 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007340 final ViewParent p = mParent;
7341 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007343 int minTop;
7344 int maxBottom;
7345 int yLoc;
7346 if (offset < 0) {
7347 minTop = mTop + offset;
7348 maxBottom = mBottom;
7349 yLoc = offset;
7350 } else {
7351 minTop = mTop;
7352 maxBottom = mBottom + offset;
7353 yLoc = 0;
7354 }
7355 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7356 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007357 }
7358 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007359 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007360 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007361
Chet Haasec3aa3612010-06-17 08:50:37 -07007362 mTop += offset;
7363 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007364
Chet Haasec3aa3612010-06-17 08:50:37 -07007365 if (!mMatrixIsIdentity) {
7366 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007367 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007368 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007369 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 }
7372
7373 /**
7374 * Offset this view's horizontal location by the specified amount of pixels.
7375 *
7376 * @param offset the numer of pixels to offset the view by
7377 */
7378 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007379 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007380 updateMatrix();
7381 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007382 final ViewParent p = mParent;
7383 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007384 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007385 int minLeft;
7386 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007387 if (offset < 0) {
7388 minLeft = mLeft + offset;
7389 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007390 } else {
7391 minLeft = mLeft;
7392 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007393 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007394 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007395 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007396 }
7397 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007398 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007399 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007400
Chet Haasec3aa3612010-06-17 08:50:37 -07007401 mLeft += offset;
7402 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007403
Chet Haasec3aa3612010-06-17 08:50:37 -07007404 if (!mMatrixIsIdentity) {
7405 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007406 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007407 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007408 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 }
7411
7412 /**
7413 * Get the LayoutParams associated with this view. All views should have
7414 * layout parameters. These supply parameters to the <i>parent</i> of this
7415 * view specifying how it should be arranged. There are many subclasses of
7416 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7417 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007418 *
7419 * This method may return null if this View is not attached to a parent
7420 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7421 * was not invoked successfully. When a View is attached to a parent
7422 * ViewGroup, this method must not return null.
7423 *
7424 * @return The LayoutParams associated with this view, or null if no
7425 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007427 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 public ViewGroup.LayoutParams getLayoutParams() {
7429 return mLayoutParams;
7430 }
7431
7432 /**
7433 * Set the layout parameters associated with this view. These supply
7434 * parameters to the <i>parent</i> of this view specifying how it should be
7435 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7436 * correspond to the different subclasses of ViewGroup that are responsible
7437 * for arranging their children.
7438 *
Romain Guy01c174b2011-02-22 11:51:06 -08007439 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 */
7441 public void setLayoutParams(ViewGroup.LayoutParams params) {
7442 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007443 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007444 }
7445 mLayoutParams = params;
7446 requestLayout();
7447 }
7448
7449 /**
7450 * Set the scrolled position of your view. This will cause a call to
7451 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7452 * invalidated.
7453 * @param x the x position to scroll to
7454 * @param y the y position to scroll to
7455 */
7456 public void scrollTo(int x, int y) {
7457 if (mScrollX != x || mScrollY != y) {
7458 int oldX = mScrollX;
7459 int oldY = mScrollY;
7460 mScrollX = x;
7461 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007462 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007464 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007465 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 }
7468 }
7469
7470 /**
7471 * Move the scrolled position of your view. This will cause a call to
7472 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7473 * invalidated.
7474 * @param x the amount of pixels to scroll by horizontally
7475 * @param y the amount of pixels to scroll by vertically
7476 */
7477 public void scrollBy(int x, int y) {
7478 scrollTo(mScrollX + x, mScrollY + y);
7479 }
7480
7481 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007482 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7483 * animation to fade the scrollbars out after a default delay. If a subclass
7484 * provides animated scrolling, the start delay should equal the duration
7485 * of the scrolling animation.</p>
7486 *
7487 * <p>The animation starts only if at least one of the scrollbars is
7488 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7489 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7490 * this method returns true, and false otherwise. If the animation is
7491 * started, this method calls {@link #invalidate()}; in that case the
7492 * caller should not call {@link #invalidate()}.</p>
7493 *
7494 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007495 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007496 *
7497 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7498 * and {@link #scrollTo(int, int)}.</p>
7499 *
7500 * @return true if the animation is played, false otherwise
7501 *
7502 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007503 * @see #scrollBy(int, int)
7504 * @see #scrollTo(int, int)
7505 * @see #isHorizontalScrollBarEnabled()
7506 * @see #isVerticalScrollBarEnabled()
7507 * @see #setHorizontalScrollBarEnabled(boolean)
7508 * @see #setVerticalScrollBarEnabled(boolean)
7509 */
7510 protected boolean awakenScrollBars() {
7511 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007512 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007513 }
7514
7515 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007516 * Trigger the scrollbars to draw.
7517 * This method differs from awakenScrollBars() only in its default duration.
7518 * initialAwakenScrollBars() will show the scroll bars for longer than
7519 * usual to give the user more of a chance to notice them.
7520 *
7521 * @return true if the animation is played, false otherwise.
7522 */
7523 private boolean initialAwakenScrollBars() {
7524 return mScrollCache != null &&
7525 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7526 }
7527
7528 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007529 * <p>
7530 * Trigger the scrollbars to draw. When invoked this method starts an
7531 * animation to fade the scrollbars out after a fixed delay. If a subclass
7532 * provides animated scrolling, the start delay should equal the duration of
7533 * the scrolling animation.
7534 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007535 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007536 * <p>
7537 * The animation starts only if at least one of the scrollbars is enabled,
7538 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7539 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7540 * this method returns true, and false otherwise. If the animation is
7541 * started, this method calls {@link #invalidate()}; in that case the caller
7542 * should not call {@link #invalidate()}.
7543 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007544 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007545 * <p>
7546 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007547 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007548 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007549 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007550 * @param startDelay the delay, in milliseconds, after which the animation
7551 * should start; when the delay is 0, the animation starts
7552 * immediately
7553 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007554 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007555 * @see #scrollBy(int, int)
7556 * @see #scrollTo(int, int)
7557 * @see #isHorizontalScrollBarEnabled()
7558 * @see #isVerticalScrollBarEnabled()
7559 * @see #setHorizontalScrollBarEnabled(boolean)
7560 * @see #setVerticalScrollBarEnabled(boolean)
7561 */
7562 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007563 return awakenScrollBars(startDelay, true);
7564 }
Joe Malin32736f02011-01-19 16:14:20 -08007565
Mike Cleron290947b2009-09-29 18:34:32 -07007566 /**
7567 * <p>
7568 * Trigger the scrollbars to draw. When invoked this method starts an
7569 * animation to fade the scrollbars out after a fixed delay. If a subclass
7570 * provides animated scrolling, the start delay should equal the duration of
7571 * the scrolling animation.
7572 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007573 *
Mike Cleron290947b2009-09-29 18:34:32 -07007574 * <p>
7575 * The animation starts only if at least one of the scrollbars is enabled,
7576 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7577 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7578 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007579 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007580 * is set to true; in that case the caller
7581 * should not call {@link #invalidate()}.
7582 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007583 *
Mike Cleron290947b2009-09-29 18:34:32 -07007584 * <p>
7585 * This method should be invoked everytime a subclass directly updates the
7586 * scroll parameters.
7587 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007588 *
Mike Cleron290947b2009-09-29 18:34:32 -07007589 * @param startDelay the delay, in milliseconds, after which the animation
7590 * should start; when the delay is 0, the animation starts
7591 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007592 *
Mike Cleron290947b2009-09-29 18:34:32 -07007593 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007594 *
Mike Cleron290947b2009-09-29 18:34:32 -07007595 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007596 *
Mike Cleron290947b2009-09-29 18:34:32 -07007597 * @see #scrollBy(int, int)
7598 * @see #scrollTo(int, int)
7599 * @see #isHorizontalScrollBarEnabled()
7600 * @see #isVerticalScrollBarEnabled()
7601 * @see #setHorizontalScrollBarEnabled(boolean)
7602 * @see #setVerticalScrollBarEnabled(boolean)
7603 */
7604 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007605 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007606
Mike Cleronf116bf82009-09-27 19:14:12 -07007607 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7608 return false;
7609 }
7610
7611 if (scrollCache.scrollBar == null) {
7612 scrollCache.scrollBar = new ScrollBarDrawable();
7613 }
7614
7615 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7616
Mike Cleron290947b2009-09-29 18:34:32 -07007617 if (invalidate) {
7618 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007619 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007620 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007621
7622 if (scrollCache.state == ScrollabilityCache.OFF) {
7623 // FIXME: this is copied from WindowManagerService.
7624 // We should get this value from the system when it
7625 // is possible to do so.
7626 final int KEY_REPEAT_FIRST_DELAY = 750;
7627 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7628 }
7629
7630 // Tell mScrollCache when we should start fading. This may
7631 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007632 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007633 scrollCache.fadeStartTime = fadeStartTime;
7634 scrollCache.state = ScrollabilityCache.ON;
7635
7636 // Schedule our fader to run, unscheduling any old ones first
7637 if (mAttachInfo != null) {
7638 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7639 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7640 }
7641
7642 return true;
7643 }
7644
7645 return false;
7646 }
7647
7648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007650 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7651 * in the future. This must be called from a UI thread. To call from a non-UI
7652 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 *
7654 * WARNING: This method is destructive to dirty.
7655 * @param dirty the rectangle representing the bounds of the dirty region
7656 */
7657 public void invalidate(Rect dirty) {
7658 if (ViewDebug.TRACE_HIERARCHY) {
7659 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7660 }
7661
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007662 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007663 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7664 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007666 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 final ViewParent p = mParent;
7668 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007669 //noinspection PointlessBooleanExpression,ConstantConditions
7670 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7671 if (p != null && ai != null && ai.mHardwareAccelerated) {
7672 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007673 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007674 p.invalidateChild(this, null);
7675 return;
7676 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 if (p != null && ai != null) {
7679 final int scrollX = mScrollX;
7680 final int scrollY = mScrollY;
7681 final Rect r = ai.mTmpInvalRect;
7682 r.set(dirty.left - scrollX, dirty.top - scrollY,
7683 dirty.right - scrollX, dirty.bottom - scrollY);
7684 mParent.invalidateChild(this, r);
7685 }
7686 }
7687 }
7688
7689 /**
7690 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7691 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007692 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7693 * will be called at some point in the future. This must be called from
7694 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 * @param l the left position of the dirty region
7696 * @param t the top position of the dirty region
7697 * @param r the right position of the dirty region
7698 * @param b the bottom position of the dirty region
7699 */
7700 public void invalidate(int l, int t, int r, int b) {
7701 if (ViewDebug.TRACE_HIERARCHY) {
7702 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7703 }
7704
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007705 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007706 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7707 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007709 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 final ViewParent p = mParent;
7711 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007712 //noinspection PointlessBooleanExpression,ConstantConditions
7713 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7714 if (p != null && ai != null && ai.mHardwareAccelerated) {
7715 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007716 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007717 p.invalidateChild(this, null);
7718 return;
7719 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 if (p != null && ai != null && l < r && t < b) {
7722 final int scrollX = mScrollX;
7723 final int scrollY = mScrollY;
7724 final Rect tmpr = ai.mTmpInvalRect;
7725 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7726 p.invalidateChild(this, tmpr);
7727 }
7728 }
7729 }
7730
7731 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007732 * Invalidate the whole view. If the view is visible,
7733 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7734 * the future. This must be called from a UI thread. To call from a non-UI thread,
7735 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 */
7737 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007738 invalidate(true);
7739 }
Joe Malin32736f02011-01-19 16:14:20 -08007740
Chet Haaseed032702010-10-01 14:05:54 -07007741 /**
7742 * This is where the invalidate() work actually happens. A full invalidate()
7743 * causes the drawing cache to be invalidated, but this function can be called with
7744 * invalidateCache set to false to skip that invalidation step for cases that do not
7745 * need it (for example, a component that remains at the same dimensions with the same
7746 * content).
7747 *
7748 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7749 * well. This is usually true for a full invalidate, but may be set to false if the
7750 * View's contents or dimensions have not changed.
7751 */
Romain Guy849d0a32011-02-01 17:20:48 -08007752 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 if (ViewDebug.TRACE_HIERARCHY) {
7754 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7755 }
7756
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007757 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007758 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007759 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7760 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007761 mPrivateFlags &= ~DRAWN;
7762 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007763 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007764 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007767 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007768 //noinspection PointlessBooleanExpression,ConstantConditions
7769 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7770 if (p != null && ai != null && ai.mHardwareAccelerated) {
7771 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007772 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007773 p.invalidateChild(this, null);
7774 return;
7775 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007776 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 if (p != null && ai != null) {
7779 final Rect r = ai.mTmpInvalRect;
7780 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7781 // Don't call invalidate -- we don't want to internally scroll
7782 // our own bounds
7783 p.invalidateChild(this, r);
7784 }
7785 }
7786 }
7787
7788 /**
Romain Guyda489792011-02-03 01:05:15 -08007789 * @hide
7790 */
7791 public void fastInvalidate() {
7792 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7793 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7794 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7795 if (mParent instanceof View) {
7796 ((View) mParent).mPrivateFlags |= INVALIDATED;
7797 }
7798 mPrivateFlags &= ~DRAWN;
7799 mPrivateFlags |= INVALIDATED;
7800 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007801 if (mParent != null && mAttachInfo != null) {
7802 if (mAttachInfo.mHardwareAccelerated) {
7803 mParent.invalidateChild(this, null);
7804 } else {
7805 final Rect r = mAttachInfo.mTmpInvalRect;
7806 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7807 // Don't call invalidate -- we don't want to internally scroll
7808 // our own bounds
7809 mParent.invalidateChild(this, r);
7810 }
Romain Guyda489792011-02-03 01:05:15 -08007811 }
7812 }
7813 }
7814
7815 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007816 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007817 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7818 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007819 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7820 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007821 *
7822 * @hide
7823 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007824 protected void invalidateParentCaches() {
7825 if (mParent instanceof View) {
7826 ((View) mParent).mPrivateFlags |= INVALIDATED;
7827 }
7828 }
Joe Malin32736f02011-01-19 16:14:20 -08007829
Romain Guy0fd89bf2011-01-26 15:41:30 -08007830 /**
7831 * Used to indicate that the parent of this view should be invalidated. This functionality
7832 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7833 * which is necessary when various parent-managed properties of the view change, such as
7834 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7835 * an invalidation event to the parent.
7836 *
7837 * @hide
7838 */
7839 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007840 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007841 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007842 }
7843 }
7844
7845 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007846 * Indicates whether this View is opaque. An opaque View guarantees that it will
7847 * draw all the pixels overlapping its bounds using a fully opaque color.
7848 *
7849 * Subclasses of View should override this method whenever possible to indicate
7850 * whether an instance is opaque. Opaque Views are treated in a special way by
7851 * the View hierarchy, possibly allowing it to perform optimizations during
7852 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007853 *
Romain Guy24443ea2009-05-11 11:56:30 -07007854 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007855 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007856 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007857 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007858 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7859 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007860 }
7861
Adam Powell20232d02010-12-08 21:08:53 -08007862 /**
7863 * @hide
7864 */
7865 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007866 // Opaque if:
7867 // - Has a background
7868 // - Background is opaque
7869 // - Doesn't have scrollbars or scrollbars are inside overlay
7870
7871 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7872 mPrivateFlags |= OPAQUE_BACKGROUND;
7873 } else {
7874 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7875 }
7876
7877 final int flags = mViewFlags;
7878 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7879 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7880 mPrivateFlags |= OPAQUE_SCROLLBARS;
7881 } else {
7882 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7883 }
7884 }
7885
7886 /**
7887 * @hide
7888 */
7889 protected boolean hasOpaqueScrollbars() {
7890 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007891 }
7892
7893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 * @return A handler associated with the thread running the View. This
7895 * handler can be used to pump events in the UI events queue.
7896 */
7897 public Handler getHandler() {
7898 if (mAttachInfo != null) {
7899 return mAttachInfo.mHandler;
7900 }
7901 return null;
7902 }
7903
7904 /**
7905 * Causes the Runnable to be added to the message queue.
7906 * The runnable will be run on the user interface thread.
7907 *
7908 * @param action The Runnable that will be executed.
7909 *
7910 * @return Returns true if the Runnable was successfully placed in to the
7911 * message queue. Returns false on failure, usually because the
7912 * looper processing the message queue is exiting.
7913 */
7914 public boolean post(Runnable action) {
7915 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007916 AttachInfo attachInfo = mAttachInfo;
7917 if (attachInfo != null) {
7918 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007919 } else {
7920 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007921 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 return true;
7923 }
7924
7925 return handler.post(action);
7926 }
7927
7928 /**
7929 * Causes the Runnable to be added to the message queue, to be run
7930 * after the specified amount of time elapses.
7931 * The runnable will be run on the user interface thread.
7932 *
7933 * @param action The Runnable that will be executed.
7934 * @param delayMillis The delay (in milliseconds) until the Runnable
7935 * will be executed.
7936 *
7937 * @return true if the Runnable was successfully placed in to the
7938 * message queue. Returns false on failure, usually because the
7939 * looper processing the message queue is exiting. Note that a
7940 * result of true does not mean the Runnable will be processed --
7941 * if the looper is quit before the delivery time of the message
7942 * occurs then the message will be dropped.
7943 */
7944 public boolean postDelayed(Runnable action, long delayMillis) {
7945 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007946 AttachInfo attachInfo = mAttachInfo;
7947 if (attachInfo != null) {
7948 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 } else {
7950 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007951 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 return true;
7953 }
7954
7955 return handler.postDelayed(action, delayMillis);
7956 }
7957
7958 /**
7959 * Removes the specified Runnable from the message queue.
7960 *
7961 * @param action The Runnable to remove from the message handling queue
7962 *
7963 * @return true if this view could ask the Handler to remove the Runnable,
7964 * false otherwise. When the returned value is true, the Runnable
7965 * may or may not have been actually removed from the message queue
7966 * (for instance, if the Runnable was not in the queue already.)
7967 */
7968 public boolean removeCallbacks(Runnable action) {
7969 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007970 AttachInfo attachInfo = mAttachInfo;
7971 if (attachInfo != null) {
7972 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 } else {
7974 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007975 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 return true;
7977 }
7978
7979 handler.removeCallbacks(action);
7980 return true;
7981 }
7982
7983 /**
7984 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7985 * Use this to invalidate the View from a non-UI thread.
7986 *
7987 * @see #invalidate()
7988 */
7989 public void postInvalidate() {
7990 postInvalidateDelayed(0);
7991 }
7992
7993 /**
7994 * Cause an invalidate of the specified area to happen on a subsequent cycle
7995 * through the event loop. Use this to invalidate the View from a non-UI thread.
7996 *
7997 * @param left The left coordinate of the rectangle to invalidate.
7998 * @param top The top coordinate of the rectangle to invalidate.
7999 * @param right The right coordinate of the rectangle to invalidate.
8000 * @param bottom The bottom coordinate of the rectangle to invalidate.
8001 *
8002 * @see #invalidate(int, int, int, int)
8003 * @see #invalidate(Rect)
8004 */
8005 public void postInvalidate(int left, int top, int right, int bottom) {
8006 postInvalidateDelayed(0, left, top, right, bottom);
8007 }
8008
8009 /**
8010 * Cause an invalidate to happen on a subsequent cycle through the event
8011 * loop. Waits for the specified amount of time.
8012 *
8013 * @param delayMilliseconds the duration in milliseconds to delay the
8014 * invalidation by
8015 */
8016 public void postInvalidateDelayed(long delayMilliseconds) {
8017 // We try only with the AttachInfo because there's no point in invalidating
8018 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008019 AttachInfo attachInfo = mAttachInfo;
8020 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 Message msg = Message.obtain();
8022 msg.what = AttachInfo.INVALIDATE_MSG;
8023 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008024 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 }
8026 }
8027
8028 /**
8029 * Cause an invalidate of the specified area to happen on a subsequent cycle
8030 * through the event loop. Waits for the specified amount of time.
8031 *
8032 * @param delayMilliseconds the duration in milliseconds to delay the
8033 * invalidation by
8034 * @param left The left coordinate of the rectangle to invalidate.
8035 * @param top The top coordinate of the rectangle to invalidate.
8036 * @param right The right coordinate of the rectangle to invalidate.
8037 * @param bottom The bottom coordinate of the rectangle to invalidate.
8038 */
8039 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8040 int right, int bottom) {
8041
8042 // We try only with the AttachInfo because there's no point in invalidating
8043 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008044 AttachInfo attachInfo = mAttachInfo;
8045 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8047 info.target = this;
8048 info.left = left;
8049 info.top = top;
8050 info.right = right;
8051 info.bottom = bottom;
8052
8053 final Message msg = Message.obtain();
8054 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8055 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008056 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 }
8058 }
8059
8060 /**
8061 * Called by a parent to request that a child update its values for mScrollX
8062 * and mScrollY if necessary. This will typically be done if the child is
8063 * animating a scroll using a {@link android.widget.Scroller Scroller}
8064 * object.
8065 */
8066 public void computeScroll() {
8067 }
8068
8069 /**
8070 * <p>Indicate whether the horizontal edges are faded when the view is
8071 * scrolled horizontally.</p>
8072 *
8073 * @return true if the horizontal edges should are faded on scroll, false
8074 * otherwise
8075 *
8076 * @see #setHorizontalFadingEdgeEnabled(boolean)
8077 * @attr ref android.R.styleable#View_fadingEdge
8078 */
8079 public boolean isHorizontalFadingEdgeEnabled() {
8080 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8081 }
8082
8083 /**
8084 * <p>Define whether the horizontal edges should be faded when this view
8085 * is scrolled horizontally.</p>
8086 *
8087 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8088 * be faded when the view is scrolled
8089 * horizontally
8090 *
8091 * @see #isHorizontalFadingEdgeEnabled()
8092 * @attr ref android.R.styleable#View_fadingEdge
8093 */
8094 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8095 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8096 if (horizontalFadingEdgeEnabled) {
8097 initScrollCache();
8098 }
8099
8100 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8101 }
8102 }
8103
8104 /**
8105 * <p>Indicate whether the vertical edges are faded when the view is
8106 * scrolled horizontally.</p>
8107 *
8108 * @return true if the vertical edges should are faded on scroll, false
8109 * otherwise
8110 *
8111 * @see #setVerticalFadingEdgeEnabled(boolean)
8112 * @attr ref android.R.styleable#View_fadingEdge
8113 */
8114 public boolean isVerticalFadingEdgeEnabled() {
8115 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8116 }
8117
8118 /**
8119 * <p>Define whether the vertical edges should be faded when this view
8120 * is scrolled vertically.</p>
8121 *
8122 * @param verticalFadingEdgeEnabled true if the vertical edges should
8123 * be faded when the view is scrolled
8124 * vertically
8125 *
8126 * @see #isVerticalFadingEdgeEnabled()
8127 * @attr ref android.R.styleable#View_fadingEdge
8128 */
8129 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8130 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8131 if (verticalFadingEdgeEnabled) {
8132 initScrollCache();
8133 }
8134
8135 mViewFlags ^= FADING_EDGE_VERTICAL;
8136 }
8137 }
8138
8139 /**
8140 * Returns the strength, or intensity, of the top faded edge. The strength is
8141 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8142 * returns 0.0 or 1.0 but no value in between.
8143 *
8144 * Subclasses should override this method to provide a smoother fade transition
8145 * when scrolling occurs.
8146 *
8147 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8148 */
8149 protected float getTopFadingEdgeStrength() {
8150 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8151 }
8152
8153 /**
8154 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8155 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8156 * returns 0.0 or 1.0 but no value in between.
8157 *
8158 * Subclasses should override this method to provide a smoother fade transition
8159 * when scrolling occurs.
8160 *
8161 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8162 */
8163 protected float getBottomFadingEdgeStrength() {
8164 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8165 computeVerticalScrollRange() ? 1.0f : 0.0f;
8166 }
8167
8168 /**
8169 * Returns the strength, or intensity, of the left faded edge. The strength is
8170 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8171 * returns 0.0 or 1.0 but no value in between.
8172 *
8173 * Subclasses should override this method to provide a smoother fade transition
8174 * when scrolling occurs.
8175 *
8176 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8177 */
8178 protected float getLeftFadingEdgeStrength() {
8179 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8180 }
8181
8182 /**
8183 * Returns the strength, or intensity, of the right faded edge. The strength is
8184 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8185 * returns 0.0 or 1.0 but no value in between.
8186 *
8187 * Subclasses should override this method to provide a smoother fade transition
8188 * when scrolling occurs.
8189 *
8190 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8191 */
8192 protected float getRightFadingEdgeStrength() {
8193 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8194 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8195 }
8196
8197 /**
8198 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8199 * scrollbar is not drawn by default.</p>
8200 *
8201 * @return true if the horizontal scrollbar should be painted, false
8202 * otherwise
8203 *
8204 * @see #setHorizontalScrollBarEnabled(boolean)
8205 */
8206 public boolean isHorizontalScrollBarEnabled() {
8207 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8208 }
8209
8210 /**
8211 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8212 * scrollbar is not drawn by default.</p>
8213 *
8214 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8215 * be painted
8216 *
8217 * @see #isHorizontalScrollBarEnabled()
8218 */
8219 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8220 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8221 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008222 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 recomputePadding();
8224 }
8225 }
8226
8227 /**
8228 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8229 * scrollbar is not drawn by default.</p>
8230 *
8231 * @return true if the vertical scrollbar should be painted, false
8232 * otherwise
8233 *
8234 * @see #setVerticalScrollBarEnabled(boolean)
8235 */
8236 public boolean isVerticalScrollBarEnabled() {
8237 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8238 }
8239
8240 /**
8241 * <p>Define whether the vertical scrollbar should be drawn or not. The
8242 * scrollbar is not drawn by default.</p>
8243 *
8244 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8245 * be painted
8246 *
8247 * @see #isVerticalScrollBarEnabled()
8248 */
8249 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8250 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8251 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008252 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 recomputePadding();
8254 }
8255 }
8256
Adam Powell20232d02010-12-08 21:08:53 -08008257 /**
8258 * @hide
8259 */
8260 protected void recomputePadding() {
8261 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 }
Joe Malin32736f02011-01-19 16:14:20 -08008263
Mike Cleronfe81d382009-09-28 14:22:16 -07008264 /**
8265 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008266 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008267 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008268 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008269 */
8270 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8271 initScrollCache();
8272 final ScrollabilityCache scrollabilityCache = mScrollCache;
8273 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008274 if (fadeScrollbars) {
8275 scrollabilityCache.state = ScrollabilityCache.OFF;
8276 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008277 scrollabilityCache.state = ScrollabilityCache.ON;
8278 }
8279 }
Joe Malin32736f02011-01-19 16:14:20 -08008280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 /**
Joe Malin32736f02011-01-19 16:14:20 -08008282 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008283 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008284 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008285 * @return true if scrollbar fading is enabled
8286 */
8287 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008288 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008289 }
Joe Malin32736f02011-01-19 16:14:20 -08008290
Mike Cleron52f0a642009-09-28 18:21:37 -07008291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8293 * inset. When inset, they add to the padding of the view. And the scrollbars
8294 * can be drawn inside the padding area or on the edge of the view. For example,
8295 * if a view has a background drawable and you want to draw the scrollbars
8296 * inside the padding specified by the drawable, you can use
8297 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8298 * appear at the edge of the view, ignoring the padding, then you can use
8299 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8300 * @param style the style of the scrollbars. Should be one of
8301 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8302 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8303 * @see #SCROLLBARS_INSIDE_OVERLAY
8304 * @see #SCROLLBARS_INSIDE_INSET
8305 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8306 * @see #SCROLLBARS_OUTSIDE_INSET
8307 */
8308 public void setScrollBarStyle(int style) {
8309 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8310 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008311 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 recomputePadding();
8313 }
8314 }
8315
8316 /**
8317 * <p>Returns the current scrollbar style.</p>
8318 * @return the current scrollbar style
8319 * @see #SCROLLBARS_INSIDE_OVERLAY
8320 * @see #SCROLLBARS_INSIDE_INSET
8321 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8322 * @see #SCROLLBARS_OUTSIDE_INSET
8323 */
8324 public int getScrollBarStyle() {
8325 return mViewFlags & SCROLLBARS_STYLE_MASK;
8326 }
8327
8328 /**
8329 * <p>Compute the horizontal range that the horizontal scrollbar
8330 * represents.</p>
8331 *
8332 * <p>The range is expressed in arbitrary units that must be the same as the
8333 * units used by {@link #computeHorizontalScrollExtent()} and
8334 * {@link #computeHorizontalScrollOffset()}.</p>
8335 *
8336 * <p>The default range is the drawing width of this view.</p>
8337 *
8338 * @return the total horizontal range represented by the horizontal
8339 * scrollbar
8340 *
8341 * @see #computeHorizontalScrollExtent()
8342 * @see #computeHorizontalScrollOffset()
8343 * @see android.widget.ScrollBarDrawable
8344 */
8345 protected int computeHorizontalScrollRange() {
8346 return getWidth();
8347 }
8348
8349 /**
8350 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8351 * within the horizontal range. This value is used to compute the position
8352 * of the thumb within the scrollbar's track.</p>
8353 *
8354 * <p>The range is expressed in arbitrary units that must be the same as the
8355 * units used by {@link #computeHorizontalScrollRange()} and
8356 * {@link #computeHorizontalScrollExtent()}.</p>
8357 *
8358 * <p>The default offset is the scroll offset of this view.</p>
8359 *
8360 * @return the horizontal offset of the scrollbar's thumb
8361 *
8362 * @see #computeHorizontalScrollRange()
8363 * @see #computeHorizontalScrollExtent()
8364 * @see android.widget.ScrollBarDrawable
8365 */
8366 protected int computeHorizontalScrollOffset() {
8367 return mScrollX;
8368 }
8369
8370 /**
8371 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8372 * within the horizontal range. This value is used to compute the length
8373 * of the thumb within the scrollbar's track.</p>
8374 *
8375 * <p>The range is expressed in arbitrary units that must be the same as the
8376 * units used by {@link #computeHorizontalScrollRange()} and
8377 * {@link #computeHorizontalScrollOffset()}.</p>
8378 *
8379 * <p>The default extent is the drawing width of this view.</p>
8380 *
8381 * @return the horizontal extent of the scrollbar's thumb
8382 *
8383 * @see #computeHorizontalScrollRange()
8384 * @see #computeHorizontalScrollOffset()
8385 * @see android.widget.ScrollBarDrawable
8386 */
8387 protected int computeHorizontalScrollExtent() {
8388 return getWidth();
8389 }
8390
8391 /**
8392 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8393 *
8394 * <p>The range is expressed in arbitrary units that must be the same as the
8395 * units used by {@link #computeVerticalScrollExtent()} and
8396 * {@link #computeVerticalScrollOffset()}.</p>
8397 *
8398 * @return the total vertical range represented by the vertical scrollbar
8399 *
8400 * <p>The default range is the drawing height of this view.</p>
8401 *
8402 * @see #computeVerticalScrollExtent()
8403 * @see #computeVerticalScrollOffset()
8404 * @see android.widget.ScrollBarDrawable
8405 */
8406 protected int computeVerticalScrollRange() {
8407 return getHeight();
8408 }
8409
8410 /**
8411 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8412 * within the horizontal range. This value is used to compute the position
8413 * of the thumb within the scrollbar's track.</p>
8414 *
8415 * <p>The range is expressed in arbitrary units that must be the same as the
8416 * units used by {@link #computeVerticalScrollRange()} and
8417 * {@link #computeVerticalScrollExtent()}.</p>
8418 *
8419 * <p>The default offset is the scroll offset of this view.</p>
8420 *
8421 * @return the vertical offset of the scrollbar's thumb
8422 *
8423 * @see #computeVerticalScrollRange()
8424 * @see #computeVerticalScrollExtent()
8425 * @see android.widget.ScrollBarDrawable
8426 */
8427 protected int computeVerticalScrollOffset() {
8428 return mScrollY;
8429 }
8430
8431 /**
8432 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8433 * within the vertical range. This value is used to compute the length
8434 * of the thumb within the scrollbar's track.</p>
8435 *
8436 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008437 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 * {@link #computeVerticalScrollOffset()}.</p>
8439 *
8440 * <p>The default extent is the drawing height of this view.</p>
8441 *
8442 * @return the vertical extent of the scrollbar's thumb
8443 *
8444 * @see #computeVerticalScrollRange()
8445 * @see #computeVerticalScrollOffset()
8446 * @see android.widget.ScrollBarDrawable
8447 */
8448 protected int computeVerticalScrollExtent() {
8449 return getHeight();
8450 }
8451
8452 /**
8453 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8454 * scrollbars are painted only if they have been awakened first.</p>
8455 *
8456 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008457 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008458 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008460 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 // scrollbars are drawn only when the animation is running
8462 final ScrollabilityCache cache = mScrollCache;
8463 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008464
Mike Cleronf116bf82009-09-27 19:14:12 -07008465 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008466
Mike Cleronf116bf82009-09-27 19:14:12 -07008467 if (state == ScrollabilityCache.OFF) {
8468 return;
8469 }
Joe Malin32736f02011-01-19 16:14:20 -08008470
Mike Cleronf116bf82009-09-27 19:14:12 -07008471 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008472
Mike Cleronf116bf82009-09-27 19:14:12 -07008473 if (state == ScrollabilityCache.FADING) {
8474 // We're fading -- get our fade interpolation
8475 if (cache.interpolatorValues == null) {
8476 cache.interpolatorValues = new float[1];
8477 }
Joe Malin32736f02011-01-19 16:14:20 -08008478
Mike Cleronf116bf82009-09-27 19:14:12 -07008479 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008480
Mike Cleronf116bf82009-09-27 19:14:12 -07008481 // Stops the animation if we're done
8482 if (cache.scrollBarInterpolator.timeToValues(values) ==
8483 Interpolator.Result.FREEZE_END) {
8484 cache.state = ScrollabilityCache.OFF;
8485 } else {
8486 cache.scrollBar.setAlpha(Math.round(values[0]));
8487 }
Joe Malin32736f02011-01-19 16:14:20 -08008488
8489 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008490 // drawing. We only want this when we're fading so that
8491 // we prevent excessive redraws
8492 invalidate = true;
8493 } else {
8494 // We're just on -- but we may have been fading before so
8495 // reset alpha
8496 cache.scrollBar.setAlpha(255);
8497 }
8498
Joe Malin32736f02011-01-19 16:14:20 -08008499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 final int viewFlags = mViewFlags;
8501
8502 final boolean drawHorizontalScrollBar =
8503 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8504 final boolean drawVerticalScrollBar =
8505 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8506 && !isVerticalScrollBarHidden();
8507
8508 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8509 final int width = mRight - mLeft;
8510 final int height = mBottom - mTop;
8511
8512 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513
Mike Reede8853fc2009-09-04 14:01:48 -04008514 final int scrollX = mScrollX;
8515 final int scrollY = mScrollY;
8516 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8517
Mike Cleronf116bf82009-09-27 19:14:12 -07008518 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008521 int size = scrollBar.getSize(false);
8522 if (size <= 0) {
8523 size = cache.scrollBarSize;
8524 }
8525
Mike Cleronf116bf82009-09-27 19:14:12 -07008526 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008527 computeHorizontalScrollOffset(),
8528 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008529 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008530 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008531 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008532 left = scrollX + (mPaddingLeft & inside);
8533 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8534 bottom = top + size;
8535 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8536 if (invalidate) {
8537 invalidate(left, top, right, bottom);
8538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 }
8540
8541 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008542 int size = scrollBar.getSize(true);
8543 if (size <= 0) {
8544 size = cache.scrollBarSize;
8545 }
8546
Mike Reede8853fc2009-09-04 14:01:48 -04008547 scrollBar.setParameters(computeVerticalScrollRange(),
8548 computeVerticalScrollOffset(),
8549 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008550 switch (mVerticalScrollbarPosition) {
8551 default:
8552 case SCROLLBAR_POSITION_DEFAULT:
8553 case SCROLLBAR_POSITION_RIGHT:
8554 left = scrollX + width - size - (mUserPaddingRight & inside);
8555 break;
8556 case SCROLLBAR_POSITION_LEFT:
8557 left = scrollX + (mUserPaddingLeft & inside);
8558 break;
8559 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008560 top = scrollY + (mPaddingTop & inside);
8561 right = left + size;
8562 bottom = scrollY + height - (mUserPaddingBottom & inside);
8563 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8564 if (invalidate) {
8565 invalidate(left, top, right, bottom);
8566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 }
8568 }
8569 }
8570 }
Romain Guy8506ab42009-06-11 17:35:47 -07008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008573 * 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 -08008574 * FastScroller is visible.
8575 * @return whether to temporarily hide the vertical scrollbar
8576 * @hide
8577 */
8578 protected boolean isVerticalScrollBarHidden() {
8579 return false;
8580 }
8581
8582 /**
8583 * <p>Draw the horizontal scrollbar if
8584 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8585 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 * @param canvas the canvas on which to draw the scrollbar
8587 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 *
8589 * @see #isHorizontalScrollBarEnabled()
8590 * @see #computeHorizontalScrollRange()
8591 * @see #computeHorizontalScrollExtent()
8592 * @see #computeHorizontalScrollOffset()
8593 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008594 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008595 */
Romain Guy8fb95422010-08-17 18:38:51 -07008596 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8597 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008598 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008599 scrollBar.draw(canvas);
8600 }
Mike Reede8853fc2009-09-04 14:01:48 -04008601
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8604 * returns true.</p>
8605 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 * @param canvas the canvas on which to draw the scrollbar
8607 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008608 *
8609 * @see #isVerticalScrollBarEnabled()
8610 * @see #computeVerticalScrollRange()
8611 * @see #computeVerticalScrollExtent()
8612 * @see #computeVerticalScrollOffset()
8613 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008614 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 */
Romain Guy8fb95422010-08-17 18:38:51 -07008616 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8617 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008618 scrollBar.setBounds(l, t, r, b);
8619 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 }
8621
8622 /**
8623 * Implement this to do your drawing.
8624 *
8625 * @param canvas the canvas on which the background will be drawn
8626 */
8627 protected void onDraw(Canvas canvas) {
8628 }
8629
8630 /*
8631 * Caller is responsible for calling requestLayout if necessary.
8632 * (This allows addViewInLayout to not request a new layout.)
8633 */
8634 void assignParent(ViewParent parent) {
8635 if (mParent == null) {
8636 mParent = parent;
8637 } else if (parent == null) {
8638 mParent = null;
8639 } else {
8640 throw new RuntimeException("view " + this + " being added, but"
8641 + " it already has a parent");
8642 }
8643 }
8644
8645 /**
8646 * This is called when the view is attached to a window. At this point it
8647 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008648 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8649 * however it may be called any time before the first onDraw -- including
8650 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008651 *
8652 * @see #onDetachedFromWindow()
8653 */
8654 protected void onAttachedToWindow() {
8655 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8656 mParent.requestTransparentRegion(this);
8657 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008658 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8659 initialAwakenScrollBars();
8660 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8661 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008662 jumpDrawablesToCurrentState();
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008663 resolveLayoutDirection();
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008664 }
Cibu Johny86666632010-02-22 13:01:02 -08008665
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008666 /**
8667 * Resolving the layout direction. LTR is set initially.
8668 * We are supposing here that the parent directionality will be resolved before its children
8669 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008670 private void resolveLayoutDirection() {
Cibu Johny86666632010-02-22 13:01:02 -08008671 mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008672 switch (getLayoutDirection()) {
8673 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008674 // If this is root view, no need to look at parent's layout dir.
8675 if (mParent != null && mParent instanceof ViewGroup &&
8676 ((ViewGroup) mParent).isLayoutRtl()) {
8677 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8678 }
8679 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008680 case LAYOUT_DIRECTION_RTL:
Cibu Johny86666632010-02-22 13:01:02 -08008681 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8682 break;
8683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 }
8685
8686 /**
8687 * This is called when the view is detached from a window. At this point it
8688 * no longer has a surface for drawing.
8689 *
8690 * @see #onAttachedToWindow()
8691 */
8692 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008693 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008694
Romain Guya440b002010-02-24 15:57:54 -08008695 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008696 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008697 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008700
8701 if (mHardwareLayer != null) {
8702 mHardwareLayer.destroy();
8703 mHardwareLayer = null;
8704 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008705
Chet Haasedaf98e92011-01-10 14:10:36 -08008706 if (mDisplayList != null) {
8707 mDisplayList.invalidate();
8708 }
8709
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008710 if (mAttachInfo != null) {
8711 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8712 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8713 }
8714
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008715 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008716 }
8717
8718 /**
8719 * @return The number of times this view has been attached to a window
8720 */
8721 protected int getWindowAttachCount() {
8722 return mWindowAttachCount;
8723 }
8724
8725 /**
8726 * Retrieve a unique token identifying the window this view is attached to.
8727 * @return Return the window's token for use in
8728 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8729 */
8730 public IBinder getWindowToken() {
8731 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8732 }
8733
8734 /**
8735 * Retrieve a unique token identifying the top-level "real" window of
8736 * the window that this view is attached to. That is, this is like
8737 * {@link #getWindowToken}, except if the window this view in is a panel
8738 * window (attached to another containing window), then the token of
8739 * the containing window is returned instead.
8740 *
8741 * @return Returns the associated window token, either
8742 * {@link #getWindowToken()} or the containing window's token.
8743 */
8744 public IBinder getApplicationWindowToken() {
8745 AttachInfo ai = mAttachInfo;
8746 if (ai != null) {
8747 IBinder appWindowToken = ai.mPanelParentWindowToken;
8748 if (appWindowToken == null) {
8749 appWindowToken = ai.mWindowToken;
8750 }
8751 return appWindowToken;
8752 }
8753 return null;
8754 }
8755
8756 /**
8757 * Retrieve private session object this view hierarchy is using to
8758 * communicate with the window manager.
8759 * @return the session object to communicate with the window manager
8760 */
8761 /*package*/ IWindowSession getWindowSession() {
8762 return mAttachInfo != null ? mAttachInfo.mSession : null;
8763 }
8764
8765 /**
8766 * @param info the {@link android.view.View.AttachInfo} to associated with
8767 * this view
8768 */
8769 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8770 //System.out.println("Attached! " + this);
8771 mAttachInfo = info;
8772 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008773 // We will need to evaluate the drawable state at least once.
8774 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008775 if (mFloatingTreeObserver != null) {
8776 info.mTreeObserver.merge(mFloatingTreeObserver);
8777 mFloatingTreeObserver = null;
8778 }
8779 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8780 mAttachInfo.mScrollContainers.add(this);
8781 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8782 }
8783 performCollectViewAttributes(visibility);
8784 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008785
8786 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8787 mOnAttachStateChangeListeners;
8788 if (listeners != null && listeners.size() > 0) {
8789 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8790 // perform the dispatching. The iterator is a safe guard against listeners that
8791 // could mutate the list by calling the various add/remove methods. This prevents
8792 // the array from being modified while we iterate it.
8793 for (OnAttachStateChangeListener listener : listeners) {
8794 listener.onViewAttachedToWindow(this);
8795 }
8796 }
8797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 int vis = info.mWindowVisibility;
8799 if (vis != GONE) {
8800 onWindowVisibilityChanged(vis);
8801 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008802 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8803 // If nobody has evaluated the drawable state yet, then do it now.
8804 refreshDrawableState();
8805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008806 }
8807
8808 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 AttachInfo info = mAttachInfo;
8810 if (info != null) {
8811 int vis = info.mWindowVisibility;
8812 if (vis != GONE) {
8813 onWindowVisibilityChanged(GONE);
8814 }
8815 }
8816
8817 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008818
Adam Powell4afd62b2011-02-18 15:02:18 -08008819 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8820 mOnAttachStateChangeListeners;
8821 if (listeners != null && listeners.size() > 0) {
8822 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8823 // perform the dispatching. The iterator is a safe guard against listeners that
8824 // could mutate the list by calling the various add/remove methods. This prevents
8825 // the array from being modified while we iterate it.
8826 for (OnAttachStateChangeListener listener : listeners) {
8827 listener.onViewDetachedFromWindow(this);
8828 }
8829 }
8830
Romain Guy01d5edc2011-01-28 11:28:53 -08008831 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 mAttachInfo.mScrollContainers.remove(this);
8833 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8834 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 mAttachInfo = null;
8837 }
8838
8839 /**
8840 * Store this view hierarchy's frozen state into the given container.
8841 *
8842 * @param container The SparseArray in which to save the view's state.
8843 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008844 * @see #restoreHierarchyState(android.util.SparseArray)
8845 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8846 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008847 */
8848 public void saveHierarchyState(SparseArray<Parcelable> container) {
8849 dispatchSaveInstanceState(container);
8850 }
8851
8852 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008853 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8854 * this view and its children. May be overridden to modify how freezing happens to a
8855 * 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 -08008856 *
8857 * @param container The SparseArray in which to save the view's state.
8858 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008859 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8860 * @see #saveHierarchyState(android.util.SparseArray)
8861 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008862 */
8863 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8864 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8865 mPrivateFlags &= ~SAVE_STATE_CALLED;
8866 Parcelable state = onSaveInstanceState();
8867 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8868 throw new IllegalStateException(
8869 "Derived class did not call super.onSaveInstanceState()");
8870 }
8871 if (state != null) {
8872 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8873 // + ": " + state);
8874 container.put(mID, state);
8875 }
8876 }
8877 }
8878
8879 /**
8880 * Hook allowing a view to generate a representation of its internal state
8881 * that can later be used to create a new instance with that same state.
8882 * This state should only contain information that is not persistent or can
8883 * not be reconstructed later. For example, you will never store your
8884 * current position on screen because that will be computed again when a
8885 * new instance of the view is placed in its view hierarchy.
8886 * <p>
8887 * Some examples of things you may store here: the current cursor position
8888 * in a text view (but usually not the text itself since that is stored in a
8889 * content provider or other persistent storage), the currently selected
8890 * item in a list view.
8891 *
8892 * @return Returns a Parcelable object containing the view's current dynamic
8893 * state, or null if there is nothing interesting to save. The
8894 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008895 * @see #onRestoreInstanceState(android.os.Parcelable)
8896 * @see #saveHierarchyState(android.util.SparseArray)
8897 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 * @see #setSaveEnabled(boolean)
8899 */
8900 protected Parcelable onSaveInstanceState() {
8901 mPrivateFlags |= SAVE_STATE_CALLED;
8902 return BaseSavedState.EMPTY_STATE;
8903 }
8904
8905 /**
8906 * Restore this view hierarchy's frozen state from the given container.
8907 *
8908 * @param container The SparseArray which holds previously frozen states.
8909 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008910 * @see #saveHierarchyState(android.util.SparseArray)
8911 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8912 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 */
8914 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8915 dispatchRestoreInstanceState(container);
8916 }
8917
8918 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008919 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
8920 * state for this view and its children. May be overridden to modify how restoring
8921 * happens to a view's children; for example, some views may want to not store state
8922 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 *
8924 * @param container The SparseArray which holds previously saved state.
8925 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008926 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8927 * @see #restoreHierarchyState(android.util.SparseArray)
8928 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929 */
8930 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8931 if (mID != NO_ID) {
8932 Parcelable state = container.get(mID);
8933 if (state != null) {
8934 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8935 // + ": " + state);
8936 mPrivateFlags &= ~SAVE_STATE_CALLED;
8937 onRestoreInstanceState(state);
8938 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8939 throw new IllegalStateException(
8940 "Derived class did not call super.onRestoreInstanceState()");
8941 }
8942 }
8943 }
8944 }
8945
8946 /**
8947 * Hook allowing a view to re-apply a representation of its internal state that had previously
8948 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8949 * null state.
8950 *
8951 * @param state The frozen state that had previously been returned by
8952 * {@link #onSaveInstanceState}.
8953 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008954 * @see #onSaveInstanceState()
8955 * @see #restoreHierarchyState(android.util.SparseArray)
8956 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 */
8958 protected void onRestoreInstanceState(Parcelable state) {
8959 mPrivateFlags |= SAVE_STATE_CALLED;
8960 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008961 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8962 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008963 + "when two views of different type have the same id in the same hierarchy. "
8964 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008965 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 }
8967 }
8968
8969 /**
8970 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8971 *
8972 * @return the drawing start time in milliseconds
8973 */
8974 public long getDrawingTime() {
8975 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8976 }
8977
8978 /**
8979 * <p>Enables or disables the duplication of the parent's state into this view. When
8980 * duplication is enabled, this view gets its drawable state from its parent rather
8981 * than from its own internal properties.</p>
8982 *
8983 * <p>Note: in the current implementation, setting this property to true after the
8984 * view was added to a ViewGroup might have no effect at all. This property should
8985 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8986 *
8987 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8988 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008989 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008990 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8991 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008992 *
8993 * @param enabled True to enable duplication of the parent's drawable state, false
8994 * to disable it.
8995 *
8996 * @see #getDrawableState()
8997 * @see #isDuplicateParentStateEnabled()
8998 */
8999 public void setDuplicateParentStateEnabled(boolean enabled) {
9000 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9001 }
9002
9003 /**
9004 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9005 *
9006 * @return True if this view's drawable state is duplicated from the parent,
9007 * false otherwise
9008 *
9009 * @see #getDrawableState()
9010 * @see #setDuplicateParentStateEnabled(boolean)
9011 */
9012 public boolean isDuplicateParentStateEnabled() {
9013 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9014 }
9015
9016 /**
Romain Guy171c5922011-01-06 10:04:23 -08009017 * <p>Specifies the type of layer backing this view. The layer can be
9018 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9019 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009020 *
Romain Guy171c5922011-01-06 10:04:23 -08009021 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9022 * instance that controls how the layer is composed on screen. The following
9023 * properties of the paint are taken into account when composing the layer:</p>
9024 * <ul>
9025 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9026 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9027 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9028 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009029 *
Romain Guy171c5922011-01-06 10:04:23 -08009030 * <p>If this view has an alpha value set to < 1.0 by calling
9031 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9032 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9033 * equivalent to setting a hardware layer on this view and providing a paint with
9034 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009035 *
Romain Guy171c5922011-01-06 10:04:23 -08009036 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9037 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9038 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009039 *
Romain Guy171c5922011-01-06 10:04:23 -08009040 * @param layerType The ype of layer to use with this view, must be one of
9041 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9042 * {@link #LAYER_TYPE_HARDWARE}
9043 * @param paint The paint used to compose the layer. This argument is optional
9044 * and can be null. It is ignored when the layer type is
9045 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009046 *
9047 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009048 * @see #LAYER_TYPE_NONE
9049 * @see #LAYER_TYPE_SOFTWARE
9050 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009051 * @see #setAlpha(float)
9052 *
Romain Guy171c5922011-01-06 10:04:23 -08009053 * @attr ref android.R.styleable#View_layerType
9054 */
9055 public void setLayerType(int layerType, Paint paint) {
9056 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009057 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009058 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9059 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009060
Romain Guyd6cd5722011-01-17 14:42:41 -08009061 if (layerType == mLayerType) {
9062 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9063 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009064 invalidateParentCaches();
9065 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009066 }
9067 return;
9068 }
Romain Guy171c5922011-01-06 10:04:23 -08009069
9070 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009071 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009072 case LAYER_TYPE_HARDWARE:
9073 if (mHardwareLayer != null) {
9074 mHardwareLayer.destroy();
9075 mHardwareLayer = null;
9076 }
9077 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009078 case LAYER_TYPE_SOFTWARE:
9079 if (mDrawingCache != null) {
9080 mDrawingCache.recycle();
9081 mDrawingCache = null;
9082 }
Joe Malin32736f02011-01-19 16:14:20 -08009083
Romain Guy6c319ca2011-01-11 14:29:25 -08009084 if (mUnscaledDrawingCache != null) {
9085 mUnscaledDrawingCache.recycle();
9086 mUnscaledDrawingCache = null;
9087 }
9088 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009089 default:
9090 break;
Romain Guy171c5922011-01-06 10:04:23 -08009091 }
9092
9093 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009094 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9095 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9096 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009097
Romain Guy0fd89bf2011-01-26 15:41:30 -08009098 invalidateParentCaches();
9099 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009100 }
9101
9102 /**
9103 * Indicates what type of layer is currently associated with this view. By default
9104 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9105 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9106 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009107 *
Romain Guy171c5922011-01-06 10:04:23 -08009108 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9109 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009110 *
9111 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009112 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009113 * @see #LAYER_TYPE_NONE
9114 * @see #LAYER_TYPE_SOFTWARE
9115 * @see #LAYER_TYPE_HARDWARE
9116 */
9117 public int getLayerType() {
9118 return mLayerType;
9119 }
Joe Malin32736f02011-01-19 16:14:20 -08009120
Romain Guy6c319ca2011-01-11 14:29:25 -08009121 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009122 * Forces this view's layer to be created and this view to be rendered
9123 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9124 * invoking this method will have no effect.
9125 *
9126 * This method can for instance be used to render a view into its layer before
9127 * starting an animation. If this view is complex, rendering into the layer
9128 * before starting the animation will avoid skipping frames.
9129 *
9130 * @throws IllegalStateException If this view is not attached to a window
9131 *
9132 * @see #setLayerType(int, android.graphics.Paint)
9133 */
9134 public void buildLayer() {
9135 if (mLayerType == LAYER_TYPE_NONE) return;
9136
9137 if (mAttachInfo == null) {
9138 throw new IllegalStateException("This view must be attached to a window first");
9139 }
9140
9141 switch (mLayerType) {
9142 case LAYER_TYPE_HARDWARE:
9143 getHardwareLayer();
9144 break;
9145 case LAYER_TYPE_SOFTWARE:
9146 buildDrawingCache(true);
9147 break;
9148 }
9149 }
9150
9151 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009152 * <p>Returns a hardware layer that can be used to draw this view again
9153 * without executing its draw method.</p>
9154 *
9155 * @return A HardwareLayer ready to render, or null if an error occurred.
9156 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009157 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009158 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9159 return null;
9160 }
9161
9162 final int width = mRight - mLeft;
9163 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009164
Romain Guy6c319ca2011-01-11 14:29:25 -08009165 if (width == 0 || height == 0) {
9166 return null;
9167 }
9168
9169 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9170 if (mHardwareLayer == null) {
9171 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9172 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009173 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009174 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9175 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009176 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009177 }
9178
Romain Guy59a12ca2011-06-09 17:48:21 -07009179 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009180 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9181 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009182 try {
9183 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009184 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009185 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009186
Chet Haase88172fe2011-03-07 17:36:33 -08009187 final int restoreCount = canvas.save();
9188
Romain Guy6c319ca2011-01-11 14:29:25 -08009189 computeScroll();
9190 canvas.translate(-mScrollX, -mScrollY);
9191
Romain Guy6c319ca2011-01-11 14:29:25 -08009192 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009193
Romain Guy6c319ca2011-01-11 14:29:25 -08009194 // Fast path for layouts with no backgrounds
9195 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9196 mPrivateFlags &= ~DIRTY_MASK;
9197 dispatchDraw(canvas);
9198 } else {
9199 draw(canvas);
9200 }
Joe Malin32736f02011-01-19 16:14:20 -08009201
Chet Haase88172fe2011-03-07 17:36:33 -08009202 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009203 } finally {
9204 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009205 mHardwareLayer.end(currentCanvas);
9206 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009207 }
9208 }
9209
9210 return mHardwareLayer;
9211 }
Romain Guy171c5922011-01-06 10:04:23 -08009212
9213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009214 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9215 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9216 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9217 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9218 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9219 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009220 *
Romain Guy171c5922011-01-06 10:04:23 -08009221 * <p>Enabling the drawing cache is similar to
9222 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009223 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9224 * drawing cache has no effect on rendering because the system uses a different mechanism
9225 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9226 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9227 * for information on how to enable software and hardware layers.</p>
9228 *
9229 * <p>This API can be used to manually generate
9230 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9231 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 *
9233 * @param enabled true to enable the drawing cache, false otherwise
9234 *
9235 * @see #isDrawingCacheEnabled()
9236 * @see #getDrawingCache()
9237 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009238 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 */
9240 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009241 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009242 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9243 }
9244
9245 /**
9246 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9247 *
9248 * @return true if the drawing cache is enabled
9249 *
9250 * @see #setDrawingCacheEnabled(boolean)
9251 * @see #getDrawingCache()
9252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009253 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009254 public boolean isDrawingCacheEnabled() {
9255 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9256 }
9257
9258 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009259 * Debugging utility which recursively outputs the dirty state of a view and its
9260 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009261 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009262 * @hide
9263 */
Romain Guy676b1732011-02-14 14:45:33 -08009264 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009265 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9266 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9267 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9268 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9269 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9270 if (clear) {
9271 mPrivateFlags &= clearMask;
9272 }
9273 if (this instanceof ViewGroup) {
9274 ViewGroup parent = (ViewGroup) this;
9275 final int count = parent.getChildCount();
9276 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009277 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009278 child.outputDirtyFlags(indent + " ", clear, clearMask);
9279 }
9280 }
9281 }
9282
9283 /**
9284 * This method is used by ViewGroup to cause its children to restore or recreate their
9285 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9286 * to recreate its own display list, which would happen if it went through the normal
9287 * draw/dispatchDraw mechanisms.
9288 *
9289 * @hide
9290 */
9291 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009292
9293 /**
9294 * A view that is not attached or hardware accelerated cannot create a display list.
9295 * This method checks these conditions and returns the appropriate result.
9296 *
9297 * @return true if view has the ability to create a display list, false otherwise.
9298 *
9299 * @hide
9300 */
9301 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009302 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009303 }
Joe Malin32736f02011-01-19 16:14:20 -08009304
Chet Haasedaf98e92011-01-10 14:10:36 -08009305 /**
Romain Guyb051e892010-09-28 19:09:36 -07009306 * <p>Returns a display list that can be used to draw this view again
9307 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009308 *
Romain Guyb051e892010-09-28 19:09:36 -07009309 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009310 *
9311 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009312 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009313 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009314 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009315 return null;
9316 }
9317
Chet Haasedaf98e92011-01-10 14:10:36 -08009318 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9319 mDisplayList == null || !mDisplayList.isValid() ||
9320 mRecreateDisplayList)) {
9321 // Don't need to recreate the display list, just need to tell our
9322 // children to restore/recreate theirs
9323 if (mDisplayList != null && mDisplayList.isValid() &&
9324 !mRecreateDisplayList) {
9325 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9326 mPrivateFlags &= ~DIRTY_MASK;
9327 dispatchGetDisplayList();
9328
9329 return mDisplayList;
9330 }
9331
9332 // If we got here, we're recreating it. Mark it as such to ensure that
9333 // we copy in child display lists into ours in drawChild()
9334 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009335 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009336 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9337 // If we're creating a new display list, make sure our parent gets invalidated
9338 // since they will need to recreate their display list to account for this
9339 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009340 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009341 }
Romain Guyb051e892010-09-28 19:09:36 -07009342
9343 final HardwareCanvas canvas = mDisplayList.start();
9344 try {
9345 int width = mRight - mLeft;
9346 int height = mBottom - mTop;
9347
9348 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009349 // The dirty rect should always be null for a display list
9350 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009351
Chet Haase88172fe2011-03-07 17:36:33 -08009352 final int restoreCount = canvas.save();
9353
Chet Haasedaf98e92011-01-10 14:10:36 -08009354 computeScroll();
9355 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009356 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009357
Romain Guyb051e892010-09-28 19:09:36 -07009358 // Fast path for layouts with no backgrounds
9359 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9360 mPrivateFlags &= ~DIRTY_MASK;
9361 dispatchDraw(canvas);
9362 } else {
9363 draw(canvas);
9364 }
Joe Malin32736f02011-01-19 16:14:20 -08009365
Chet Haase88172fe2011-03-07 17:36:33 -08009366 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009367 } finally {
9368 canvas.onPostDraw();
9369
9370 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009371 }
Chet Haase77785f92011-01-25 23:22:09 -08009372 } else {
9373 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9374 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009375 }
9376
9377 return mDisplayList;
9378 }
9379
9380 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009381 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009382 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009383 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009384 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009385 * @see #getDrawingCache(boolean)
9386 */
9387 public Bitmap getDrawingCache() {
9388 return getDrawingCache(false);
9389 }
9390
9391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9393 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9394 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9395 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9396 * request the drawing cache by calling this method and draw it on screen if the
9397 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009398 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009399 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9400 * this method will create a bitmap of the same size as this view. Because this bitmap
9401 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9402 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9403 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9404 * size than the view. This implies that your application must be able to handle this
9405 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009406 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009407 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9408 * the current density of the screen when the application is in compatibility
9409 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009410 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009411 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009412 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413 * @see #setDrawingCacheEnabled(boolean)
9414 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009415 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 * @see #destroyDrawingCache()
9417 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009418 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009419 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9420 return null;
9421 }
9422 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009423 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 }
Romain Guy02890fd2010-08-06 17:58:44 -07009425 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 }
9427
9428 /**
9429 * <p>Frees the resources used by the drawing cache. If you call
9430 * {@link #buildDrawingCache()} manually without calling
9431 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9432 * should cleanup the cache with this method afterwards.</p>
9433 *
9434 * @see #setDrawingCacheEnabled(boolean)
9435 * @see #buildDrawingCache()
9436 * @see #getDrawingCache()
9437 */
9438 public void destroyDrawingCache() {
9439 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009440 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009441 mDrawingCache = null;
9442 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009443 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009444 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009445 mUnscaledDrawingCache = null;
9446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 }
9448
9449 /**
9450 * Setting a solid background color for the drawing cache's bitmaps will improve
9451 * perfromance and memory usage. Note, though that this should only be used if this
9452 * view will always be drawn on top of a solid color.
9453 *
9454 * @param color The background color to use for the drawing cache's bitmap
9455 *
9456 * @see #setDrawingCacheEnabled(boolean)
9457 * @see #buildDrawingCache()
9458 * @see #getDrawingCache()
9459 */
9460 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009461 if (color != mDrawingCacheBackgroundColor) {
9462 mDrawingCacheBackgroundColor = color;
9463 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009465 }
9466
9467 /**
9468 * @see #setDrawingCacheBackgroundColor(int)
9469 *
9470 * @return The background color to used for the drawing cache's bitmap
9471 */
9472 public int getDrawingCacheBackgroundColor() {
9473 return mDrawingCacheBackgroundColor;
9474 }
9475
9476 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009477 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009478 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009479 * @see #buildDrawingCache(boolean)
9480 */
9481 public void buildDrawingCache() {
9482 buildDrawingCache(false);
9483 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009484
Romain Guyfbd8f692009-06-26 14:51:58 -07009485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9487 *
9488 * <p>If you call {@link #buildDrawingCache()} manually without calling
9489 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9490 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009491 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009492 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9493 * this method will create a bitmap of the same size as this view. Because this bitmap
9494 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9495 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9496 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9497 * size than the view. This implies that your application must be able to handle this
9498 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009499 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009500 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9501 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009502 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009503 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 *
9505 * @see #getDrawingCache()
9506 * @see #destroyDrawingCache()
9507 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009508 public void buildDrawingCache(boolean autoScale) {
9509 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009510 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009511 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512
9513 if (ViewDebug.TRACE_HIERARCHY) {
9514 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516
Romain Guy8506ab42009-06-11 17:35:47 -07009517 int width = mRight - mLeft;
9518 int height = mBottom - mTop;
9519
9520 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009521 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009522
Romain Guye1123222009-06-29 14:24:56 -07009523 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009524 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9525 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009527
9528 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009529 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009530 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531
9532 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009533 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009534 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9536 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009537 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009538 return;
9539 }
9540
9541 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009542 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543
9544 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 Bitmap.Config quality;
9546 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009547 // Never pick ARGB_4444 because it looks awful
9548 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9550 case DRAWING_CACHE_QUALITY_AUTO:
9551 quality = Bitmap.Config.ARGB_8888;
9552 break;
9553 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009554 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009555 break;
9556 case DRAWING_CACHE_QUALITY_HIGH:
9557 quality = Bitmap.Config.ARGB_8888;
9558 break;
9559 default:
9560 quality = Bitmap.Config.ARGB_8888;
9561 break;
9562 }
9563 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009564 // Optimization for translucent windows
9565 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009566 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 }
9568
9569 // Try to cleanup memory
9570 if (bitmap != null) bitmap.recycle();
9571
9572 try {
9573 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009574 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009575 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009576 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009577 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009578 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009579 }
Adam Powell26153a32010-11-08 15:22:27 -08009580 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009581 } catch (OutOfMemoryError e) {
9582 // If there is not enough memory to create the bitmap cache, just
9583 // ignore the issue as bitmap caches are not required to draw the
9584 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009585 if (autoScale) {
9586 mDrawingCache = null;
9587 } else {
9588 mUnscaledDrawingCache = null;
9589 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009590 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009591 return;
9592 }
9593
9594 clear = drawingCacheBackgroundColor != 0;
9595 }
9596
9597 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 if (attachInfo != null) {
9599 canvas = attachInfo.mCanvas;
9600 if (canvas == null) {
9601 canvas = new Canvas();
9602 }
9603 canvas.setBitmap(bitmap);
9604 // Temporarily clobber the cached Canvas in case one of our children
9605 // is also using a drawing cache. Without this, the children would
9606 // steal the canvas by attaching their own bitmap to it and bad, bad
9607 // thing would happen (invisible views, corrupted drawings, etc.)
9608 attachInfo.mCanvas = null;
9609 } else {
9610 // This case should hopefully never or seldom happen
9611 canvas = new Canvas(bitmap);
9612 }
9613
9614 if (clear) {
9615 bitmap.eraseColor(drawingCacheBackgroundColor);
9616 }
9617
9618 computeScroll();
9619 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009620
Romain Guye1123222009-06-29 14:24:56 -07009621 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009622 final float scale = attachInfo.mApplicationScale;
9623 canvas.scale(scale, scale);
9624 }
Joe Malin32736f02011-01-19 16:14:20 -08009625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009626 canvas.translate(-mScrollX, -mScrollY);
9627
Romain Guy5bcdff42009-05-14 21:27:18 -07009628 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009629 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9630 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009631 mPrivateFlags |= DRAWING_CACHE_VALID;
9632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633
9634 // Fast path for layouts with no backgrounds
9635 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9636 if (ViewDebug.TRACE_HIERARCHY) {
9637 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9638 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009639 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009640 dispatchDraw(canvas);
9641 } else {
9642 draw(canvas);
9643 }
9644
9645 canvas.restoreToCount(restoreCount);
9646
9647 if (attachInfo != null) {
9648 // Restore the cached Canvas for our siblings
9649 attachInfo.mCanvas = canvas;
9650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009651 }
9652 }
9653
9654 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009655 * Create a snapshot of the view into a bitmap. We should probably make
9656 * some form of this public, but should think about the API.
9657 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009658 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009659 int width = mRight - mLeft;
9660 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009661
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009662 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009663 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009664 width = (int) ((width * scale) + 0.5f);
9665 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009666
Romain Guy8c11e312009-09-14 15:15:30 -07009667 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009668 if (bitmap == null) {
9669 throw new OutOfMemoryError();
9670 }
9671
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009672 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009673
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009674 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009675 if (attachInfo != null) {
9676 canvas = attachInfo.mCanvas;
9677 if (canvas == null) {
9678 canvas = new Canvas();
9679 }
9680 canvas.setBitmap(bitmap);
9681 // Temporarily clobber the cached Canvas in case one of our children
9682 // is also using a drawing cache. Without this, the children would
9683 // steal the canvas by attaching their own bitmap to it and bad, bad
9684 // things would happen (invisible views, corrupted drawings, etc.)
9685 attachInfo.mCanvas = null;
9686 } else {
9687 // This case should hopefully never or seldom happen
9688 canvas = new Canvas(bitmap);
9689 }
9690
Romain Guy5bcdff42009-05-14 21:27:18 -07009691 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009692 bitmap.eraseColor(backgroundColor);
9693 }
9694
9695 computeScroll();
9696 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009697 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009698 canvas.translate(-mScrollX, -mScrollY);
9699
Romain Guy5bcdff42009-05-14 21:27:18 -07009700 // Temporarily remove the dirty mask
9701 int flags = mPrivateFlags;
9702 mPrivateFlags &= ~DIRTY_MASK;
9703
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009704 // Fast path for layouts with no backgrounds
9705 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9706 dispatchDraw(canvas);
9707 } else {
9708 draw(canvas);
9709 }
9710
Romain Guy5bcdff42009-05-14 21:27:18 -07009711 mPrivateFlags = flags;
9712
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009713 canvas.restoreToCount(restoreCount);
9714
9715 if (attachInfo != null) {
9716 // Restore the cached Canvas for our siblings
9717 attachInfo.mCanvas = canvas;
9718 }
Romain Guy8506ab42009-06-11 17:35:47 -07009719
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009720 return bitmap;
9721 }
9722
9723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009724 * Indicates whether this View is currently in edit mode. A View is usually
9725 * in edit mode when displayed within a developer tool. For instance, if
9726 * this View is being drawn by a visual user interface builder, this method
9727 * should return true.
9728 *
9729 * Subclasses should check the return value of this method to provide
9730 * different behaviors if their normal behavior might interfere with the
9731 * host environment. For instance: the class spawns a thread in its
9732 * constructor, the drawing code relies on device-specific features, etc.
9733 *
9734 * This method is usually checked in the drawing code of custom widgets.
9735 *
9736 * @return True if this View is in edit mode, false otherwise.
9737 */
9738 public boolean isInEditMode() {
9739 return false;
9740 }
9741
9742 /**
9743 * If the View draws content inside its padding and enables fading edges,
9744 * it needs to support padding offsets. Padding offsets are added to the
9745 * fading edges to extend the length of the fade so that it covers pixels
9746 * drawn inside the padding.
9747 *
9748 * Subclasses of this class should override this method if they need
9749 * to draw content inside the padding.
9750 *
9751 * @return True if padding offset must be applied, false otherwise.
9752 *
9753 * @see #getLeftPaddingOffset()
9754 * @see #getRightPaddingOffset()
9755 * @see #getTopPaddingOffset()
9756 * @see #getBottomPaddingOffset()
9757 *
9758 * @since CURRENT
9759 */
9760 protected boolean isPaddingOffsetRequired() {
9761 return false;
9762 }
9763
9764 /**
9765 * Amount by which to extend the left fading region. Called only when
9766 * {@link #isPaddingOffsetRequired()} returns true.
9767 *
9768 * @return The left padding offset in pixels.
9769 *
9770 * @see #isPaddingOffsetRequired()
9771 *
9772 * @since CURRENT
9773 */
9774 protected int getLeftPaddingOffset() {
9775 return 0;
9776 }
9777
9778 /**
9779 * Amount by which to extend the right fading region. Called only when
9780 * {@link #isPaddingOffsetRequired()} returns true.
9781 *
9782 * @return The right padding offset in pixels.
9783 *
9784 * @see #isPaddingOffsetRequired()
9785 *
9786 * @since CURRENT
9787 */
9788 protected int getRightPaddingOffset() {
9789 return 0;
9790 }
9791
9792 /**
9793 * Amount by which to extend the top fading region. Called only when
9794 * {@link #isPaddingOffsetRequired()} returns true.
9795 *
9796 * @return The top padding offset in pixels.
9797 *
9798 * @see #isPaddingOffsetRequired()
9799 *
9800 * @since CURRENT
9801 */
9802 protected int getTopPaddingOffset() {
9803 return 0;
9804 }
9805
9806 /**
9807 * Amount by which to extend the bottom fading region. Called only when
9808 * {@link #isPaddingOffsetRequired()} returns true.
9809 *
9810 * @return The bottom padding offset in pixels.
9811 *
9812 * @see #isPaddingOffsetRequired()
9813 *
9814 * @since CURRENT
9815 */
9816 protected int getBottomPaddingOffset() {
9817 return 0;
9818 }
9819
9820 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009821 * <p>Indicates whether this view is attached to an hardware accelerated
9822 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009823 *
Romain Guy2bffd262010-09-12 17:40:02 -07009824 * <p>Even if this method returns true, it does not mean that every call
9825 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9826 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9827 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9828 * window is hardware accelerated,
9829 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9830 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009831 *
Romain Guy2bffd262010-09-12 17:40:02 -07009832 * @return True if the view is attached to a window and the window is
9833 * hardware accelerated; false in any other case.
9834 */
9835 public boolean isHardwareAccelerated() {
9836 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9837 }
Joe Malin32736f02011-01-19 16:14:20 -08009838
Romain Guy2bffd262010-09-12 17:40:02 -07009839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 * Manually render this view (and all of its children) to the given Canvas.
9841 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009842 * called. When implementing a view, implement
9843 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9844 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 *
9846 * @param canvas The Canvas to which the View is rendered.
9847 */
9848 public void draw(Canvas canvas) {
9849 if (ViewDebug.TRACE_HIERARCHY) {
9850 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9851 }
9852
Romain Guy5bcdff42009-05-14 21:27:18 -07009853 final int privateFlags = mPrivateFlags;
9854 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9855 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9856 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 /*
9859 * Draw traversal performs several drawing steps which must be executed
9860 * in the appropriate order:
9861 *
9862 * 1. Draw the background
9863 * 2. If necessary, save the canvas' layers to prepare for fading
9864 * 3. Draw view's content
9865 * 4. Draw children
9866 * 5. If necessary, draw the fading edges and restore layers
9867 * 6. Draw decorations (scrollbars for instance)
9868 */
9869
9870 // Step 1, draw the background, if needed
9871 int saveCount;
9872
Romain Guy24443ea2009-05-11 11:56:30 -07009873 if (!dirtyOpaque) {
9874 final Drawable background = mBGDrawable;
9875 if (background != null) {
9876 final int scrollX = mScrollX;
9877 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878
Romain Guy24443ea2009-05-11 11:56:30 -07009879 if (mBackgroundSizeChanged) {
9880 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9881 mBackgroundSizeChanged = false;
9882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883
Romain Guy24443ea2009-05-11 11:56:30 -07009884 if ((scrollX | scrollY) == 0) {
9885 background.draw(canvas);
9886 } else {
9887 canvas.translate(scrollX, scrollY);
9888 background.draw(canvas);
9889 canvas.translate(-scrollX, -scrollY);
9890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 }
9892 }
9893
9894 // skip step 2 & 5 if possible (common case)
9895 final int viewFlags = mViewFlags;
9896 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9897 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9898 if (!verticalEdges && !horizontalEdges) {
9899 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009900 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901
9902 // Step 4, draw the children
9903 dispatchDraw(canvas);
9904
9905 // Step 6, draw decorations (scrollbars)
9906 onDrawScrollBars(canvas);
9907
9908 // we're done...
9909 return;
9910 }
9911
9912 /*
9913 * Here we do the full fledged routine...
9914 * (this is an uncommon case where speed matters less,
9915 * this is why we repeat some of the tests that have been
9916 * done above)
9917 */
9918
9919 boolean drawTop = false;
9920 boolean drawBottom = false;
9921 boolean drawLeft = false;
9922 boolean drawRight = false;
9923
9924 float topFadeStrength = 0.0f;
9925 float bottomFadeStrength = 0.0f;
9926 float leftFadeStrength = 0.0f;
9927 float rightFadeStrength = 0.0f;
9928
9929 // Step 2, save the canvas' layers
9930 int paddingLeft = mPaddingLeft;
9931 int paddingTop = mPaddingTop;
9932
9933 final boolean offsetRequired = isPaddingOffsetRequired();
9934 if (offsetRequired) {
9935 paddingLeft += getLeftPaddingOffset();
9936 paddingTop += getTopPaddingOffset();
9937 }
9938
9939 int left = mScrollX + paddingLeft;
9940 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9941 int top = mScrollY + paddingTop;
9942 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9943
9944 if (offsetRequired) {
9945 right += getRightPaddingOffset();
9946 bottom += getBottomPaddingOffset();
9947 }
9948
9949 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009950 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9951 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952
9953 // clip the fade length if top and bottom fades overlap
9954 // overlapping fades produce odd-looking artifacts
9955 if (verticalEdges && (top + length > bottom - length)) {
9956 length = (bottom - top) / 2;
9957 }
9958
9959 // also clip horizontal fades if necessary
9960 if (horizontalEdges && (left + length > right - length)) {
9961 length = (right - left) / 2;
9962 }
9963
9964 if (verticalEdges) {
9965 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009966 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009968 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 }
9970
9971 if (horizontalEdges) {
9972 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009973 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009975 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976 }
9977
9978 saveCount = canvas.getSaveCount();
9979
9980 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009981 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9983
9984 if (drawTop) {
9985 canvas.saveLayer(left, top, right, top + length, null, flags);
9986 }
9987
9988 if (drawBottom) {
9989 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9990 }
9991
9992 if (drawLeft) {
9993 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9994 }
9995
9996 if (drawRight) {
9997 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9998 }
9999 } else {
10000 scrollabilityCache.setFadeColor(solidColor);
10001 }
10002
10003 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010004 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005
10006 // Step 4, draw the children
10007 dispatchDraw(canvas);
10008
10009 // Step 5, draw the fade effect and restore layers
10010 final Paint p = scrollabilityCache.paint;
10011 final Matrix matrix = scrollabilityCache.matrix;
10012 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013
10014 if (drawTop) {
10015 matrix.setScale(1, fadeHeight * topFadeStrength);
10016 matrix.postTranslate(left, top);
10017 fade.setLocalMatrix(matrix);
10018 canvas.drawRect(left, top, right, top + length, p);
10019 }
10020
10021 if (drawBottom) {
10022 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10023 matrix.postRotate(180);
10024 matrix.postTranslate(left, bottom);
10025 fade.setLocalMatrix(matrix);
10026 canvas.drawRect(left, bottom - length, right, bottom, p);
10027 }
10028
10029 if (drawLeft) {
10030 matrix.setScale(1, fadeHeight * leftFadeStrength);
10031 matrix.postRotate(-90);
10032 matrix.postTranslate(left, top);
10033 fade.setLocalMatrix(matrix);
10034 canvas.drawRect(left, top, left + length, bottom, p);
10035 }
10036
10037 if (drawRight) {
10038 matrix.setScale(1, fadeHeight * rightFadeStrength);
10039 matrix.postRotate(90);
10040 matrix.postTranslate(right, top);
10041 fade.setLocalMatrix(matrix);
10042 canvas.drawRect(right - length, top, right, bottom, p);
10043 }
10044
10045 canvas.restoreToCount(saveCount);
10046
10047 // Step 6, draw decorations (scrollbars)
10048 onDrawScrollBars(canvas);
10049 }
10050
10051 /**
10052 * Override this if your view is known to always be drawn on top of a solid color background,
10053 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10054 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10055 * should be set to 0xFF.
10056 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010057 * @see #setVerticalFadingEdgeEnabled(boolean)
10058 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010059 *
10060 * @return The known solid color background for this view, or 0 if the color may vary
10061 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010062 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 public int getSolidColor() {
10064 return 0;
10065 }
10066
10067 /**
10068 * Build a human readable string representation of the specified view flags.
10069 *
10070 * @param flags the view flags to convert to a string
10071 * @return a String representing the supplied flags
10072 */
10073 private static String printFlags(int flags) {
10074 String output = "";
10075 int numFlags = 0;
10076 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10077 output += "TAKES_FOCUS";
10078 numFlags++;
10079 }
10080
10081 switch (flags & VISIBILITY_MASK) {
10082 case INVISIBLE:
10083 if (numFlags > 0) {
10084 output += " ";
10085 }
10086 output += "INVISIBLE";
10087 // USELESS HERE numFlags++;
10088 break;
10089 case GONE:
10090 if (numFlags > 0) {
10091 output += " ";
10092 }
10093 output += "GONE";
10094 // USELESS HERE numFlags++;
10095 break;
10096 default:
10097 break;
10098 }
10099 return output;
10100 }
10101
10102 /**
10103 * Build a human readable string representation of the specified private
10104 * view flags.
10105 *
10106 * @param privateFlags the private view flags to convert to a string
10107 * @return a String representing the supplied flags
10108 */
10109 private static String printPrivateFlags(int privateFlags) {
10110 String output = "";
10111 int numFlags = 0;
10112
10113 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10114 output += "WANTS_FOCUS";
10115 numFlags++;
10116 }
10117
10118 if ((privateFlags & FOCUSED) == FOCUSED) {
10119 if (numFlags > 0) {
10120 output += " ";
10121 }
10122 output += "FOCUSED";
10123 numFlags++;
10124 }
10125
10126 if ((privateFlags & SELECTED) == SELECTED) {
10127 if (numFlags > 0) {
10128 output += " ";
10129 }
10130 output += "SELECTED";
10131 numFlags++;
10132 }
10133
10134 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10135 if (numFlags > 0) {
10136 output += " ";
10137 }
10138 output += "IS_ROOT_NAMESPACE";
10139 numFlags++;
10140 }
10141
10142 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10143 if (numFlags > 0) {
10144 output += " ";
10145 }
10146 output += "HAS_BOUNDS";
10147 numFlags++;
10148 }
10149
10150 if ((privateFlags & DRAWN) == DRAWN) {
10151 if (numFlags > 0) {
10152 output += " ";
10153 }
10154 output += "DRAWN";
10155 // USELESS HERE numFlags++;
10156 }
10157 return output;
10158 }
10159
10160 /**
10161 * <p>Indicates whether or not this view's layout will be requested during
10162 * the next hierarchy layout pass.</p>
10163 *
10164 * @return true if the layout will be forced during next layout pass
10165 */
10166 public boolean isLayoutRequested() {
10167 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10168 }
10169
10170 /**
Cibu Johny86666632010-02-22 13:01:02 -080010171 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
10172 * layout attribute and/or the inherited value from the parent.</p>
10173 *
10174 * @return true if the layout is right-to-left.
10175 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -070010176 @ViewDebug.ExportedProperty(category = "layout")
Cibu Johny86666632010-02-22 13:01:02 -080010177 public boolean isLayoutRtl() {
10178 return (mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL;
10179 }
10180
10181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 * Assign a size and position to a view and all of its
10183 * descendants
10184 *
10185 * <p>This is the second phase of the layout mechanism.
10186 * (The first is measuring). In this phase, each parent calls
10187 * layout on all of its children to position them.
10188 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010189 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 *
Chet Haase9c087442011-01-12 16:20:16 -080010191 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 * Derived classes with children should override
10193 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010194 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 *
10196 * @param l Left position, relative to parent
10197 * @param t Top position, relative to parent
10198 * @param r Right position, relative to parent
10199 * @param b Bottom position, relative to parent
10200 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010201 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010202 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010203 int oldL = mLeft;
10204 int oldT = mTop;
10205 int oldB = mBottom;
10206 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 boolean changed = setFrame(l, t, r, b);
10208 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10209 if (ViewDebug.TRACE_HIERARCHY) {
10210 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10211 }
10212
10213 onLayout(changed, l, t, r, b);
10214 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010215
10216 if (mOnLayoutChangeListeners != null) {
10217 ArrayList<OnLayoutChangeListener> listenersCopy =
10218 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10219 int numListeners = listenersCopy.size();
10220 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010221 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010222 }
10223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 }
10225 mPrivateFlags &= ~FORCE_LAYOUT;
10226 }
10227
10228 /**
10229 * Called from layout when this view should
10230 * assign a size and position to each of its children.
10231 *
10232 * Derived classes with children should override
10233 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010234 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 * @param changed This is a new size or position for this view
10236 * @param left Left position, relative to parent
10237 * @param top Top position, relative to parent
10238 * @param right Right position, relative to parent
10239 * @param bottom Bottom position, relative to parent
10240 */
10241 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10242 }
10243
10244 /**
10245 * Assign a size and position to this view.
10246 *
10247 * This is called from layout.
10248 *
10249 * @param left Left position, relative to parent
10250 * @param top Top position, relative to parent
10251 * @param right Right position, relative to parent
10252 * @param bottom Bottom position, relative to parent
10253 * @return true if the new size and position are different than the
10254 * previous ones
10255 * {@hide}
10256 */
10257 protected boolean setFrame(int left, int top, int right, int bottom) {
10258 boolean changed = false;
10259
10260 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010261 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010262 + right + "," + bottom + ")");
10263 }
10264
10265 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10266 changed = true;
10267
10268 // Remember our drawn bit
10269 int drawn = mPrivateFlags & DRAWN;
10270
10271 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010272 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273
10274
10275 int oldWidth = mRight - mLeft;
10276 int oldHeight = mBottom - mTop;
10277
10278 mLeft = left;
10279 mTop = top;
10280 mRight = right;
10281 mBottom = bottom;
10282
10283 mPrivateFlags |= HAS_BOUNDS;
10284
10285 int newWidth = right - left;
10286 int newHeight = bottom - top;
10287
10288 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010289 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10290 // A change in dimension means an auto-centered pivot point changes, too
10291 mMatrixDirty = true;
10292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010293 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10294 }
10295
10296 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10297 // If we are visible, force the DRAWN bit to on so that
10298 // this invalidate will go through (at least to our parent).
10299 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010300 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 // the DRAWN bit.
10302 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010303 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010304 // parent display list may need to be recreated based on a change in the bounds
10305 // of any child
10306 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 }
10308
10309 // Reset drawn bit to original value (invalidate turns it off)
10310 mPrivateFlags |= drawn;
10311
10312 mBackgroundSizeChanged = true;
10313 }
10314 return changed;
10315 }
10316
10317 /**
10318 * Finalize inflating a view from XML. This is called as the last phase
10319 * of inflation, after all child views have been added.
10320 *
10321 * <p>Even if the subclass overrides onFinishInflate, they should always be
10322 * sure to call the super method, so that we get called.
10323 */
10324 protected void onFinishInflate() {
10325 }
10326
10327 /**
10328 * Returns the resources associated with this view.
10329 *
10330 * @return Resources object.
10331 */
10332 public Resources getResources() {
10333 return mResources;
10334 }
10335
10336 /**
10337 * Invalidates the specified Drawable.
10338 *
10339 * @param drawable the drawable to invalidate
10340 */
10341 public void invalidateDrawable(Drawable drawable) {
10342 if (verifyDrawable(drawable)) {
10343 final Rect dirty = drawable.getBounds();
10344 final int scrollX = mScrollX;
10345 final int scrollY = mScrollY;
10346
10347 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10348 dirty.right + scrollX, dirty.bottom + scrollY);
10349 }
10350 }
10351
10352 /**
10353 * Schedules an action on a drawable to occur at a specified time.
10354 *
10355 * @param who the recipient of the action
10356 * @param what the action to run on the drawable
10357 * @param when the time at which the action must occur. Uses the
10358 * {@link SystemClock#uptimeMillis} timebase.
10359 */
10360 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10361 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10362 mAttachInfo.mHandler.postAtTime(what, who, when);
10363 }
10364 }
10365
10366 /**
10367 * Cancels a scheduled action on a drawable.
10368 *
10369 * @param who the recipient of the action
10370 * @param what the action to cancel
10371 */
10372 public void unscheduleDrawable(Drawable who, Runnable what) {
10373 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10374 mAttachInfo.mHandler.removeCallbacks(what, who);
10375 }
10376 }
10377
10378 /**
10379 * Unschedule any events associated with the given Drawable. This can be
10380 * used when selecting a new Drawable into a view, so that the previous
10381 * one is completely unscheduled.
10382 *
10383 * @param who The Drawable to unschedule.
10384 *
10385 * @see #drawableStateChanged
10386 */
10387 public void unscheduleDrawable(Drawable who) {
10388 if (mAttachInfo != null) {
10389 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10390 }
10391 }
10392
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010393 /**
10394 * Check if a given Drawable is in RTL layout direction.
10395 *
10396 * @param who the recipient of the action
10397 */
10398 public boolean isLayoutRtl(Drawable who) {
10399 return (who == mBGDrawable) && isLayoutRtl();
10400 }
10401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 /**
10403 * If your view subclass is displaying its own Drawable objects, it should
10404 * override this function and return true for any Drawable it is
10405 * displaying. This allows animations for those drawables to be
10406 * scheduled.
10407 *
10408 * <p>Be sure to call through to the super class when overriding this
10409 * function.
10410 *
10411 * @param who The Drawable to verify. Return true if it is one you are
10412 * displaying, else return the result of calling through to the
10413 * super class.
10414 *
10415 * @return boolean If true than the Drawable is being displayed in the
10416 * view; else false and it is not allowed to animate.
10417 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010418 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10419 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 */
10421 protected boolean verifyDrawable(Drawable who) {
10422 return who == mBGDrawable;
10423 }
10424
10425 /**
10426 * This function is called whenever the state of the view changes in such
10427 * a way that it impacts the state of drawables being shown.
10428 *
10429 * <p>Be sure to call through to the superclass when overriding this
10430 * function.
10431 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010432 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 */
10434 protected void drawableStateChanged() {
10435 Drawable d = mBGDrawable;
10436 if (d != null && d.isStateful()) {
10437 d.setState(getDrawableState());
10438 }
10439 }
10440
10441 /**
10442 * Call this to force a view to update its drawable state. This will cause
10443 * drawableStateChanged to be called on this view. Views that are interested
10444 * in the new state should call getDrawableState.
10445 *
10446 * @see #drawableStateChanged
10447 * @see #getDrawableState
10448 */
10449 public void refreshDrawableState() {
10450 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10451 drawableStateChanged();
10452
10453 ViewParent parent = mParent;
10454 if (parent != null) {
10455 parent.childDrawableStateChanged(this);
10456 }
10457 }
10458
10459 /**
10460 * Return an array of resource IDs of the drawable states representing the
10461 * current state of the view.
10462 *
10463 * @return The current drawable state
10464 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010465 * @see Drawable#setState(int[])
10466 * @see #drawableStateChanged()
10467 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468 */
10469 public final int[] getDrawableState() {
10470 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10471 return mDrawableState;
10472 } else {
10473 mDrawableState = onCreateDrawableState(0);
10474 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10475 return mDrawableState;
10476 }
10477 }
10478
10479 /**
10480 * Generate the new {@link android.graphics.drawable.Drawable} state for
10481 * this view. This is called by the view
10482 * system when the cached Drawable state is determined to be invalid. To
10483 * retrieve the current state, you should use {@link #getDrawableState}.
10484 *
10485 * @param extraSpace if non-zero, this is the number of extra entries you
10486 * would like in the returned array in which you can place your own
10487 * states.
10488 *
10489 * @return Returns an array holding the current {@link Drawable} state of
10490 * the view.
10491 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010492 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010493 */
10494 protected int[] onCreateDrawableState(int extraSpace) {
10495 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10496 mParent instanceof View) {
10497 return ((View) mParent).onCreateDrawableState(extraSpace);
10498 }
10499
10500 int[] drawableState;
10501
10502 int privateFlags = mPrivateFlags;
10503
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010504 int viewStateIndex = 0;
10505 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10506 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10507 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010508 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010509 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10510 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010511 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10512 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010513 // This is set if HW acceleration is requested, even if the current
10514 // process doesn't allow it. This is just to allow app preview
10515 // windows to better match their app.
10516 viewStateIndex |= VIEW_STATE_ACCELERATED;
10517 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010518 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519
Christopher Tate3d4bf172011-03-28 16:16:46 -070010520 final int privateFlags2 = mPrivateFlags2;
10521 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10522 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 drawableState = VIEW_STATE_SETS[viewStateIndex];
10525
10526 //noinspection ConstantIfStatement
10527 if (false) {
10528 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10529 Log.i("View", toString()
10530 + " pressed=" + ((privateFlags & PRESSED) != 0)
10531 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10532 + " fo=" + hasFocus()
10533 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010534 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 + ": " + Arrays.toString(drawableState));
10536 }
10537
10538 if (extraSpace == 0) {
10539 return drawableState;
10540 }
10541
10542 final int[] fullState;
10543 if (drawableState != null) {
10544 fullState = new int[drawableState.length + extraSpace];
10545 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10546 } else {
10547 fullState = new int[extraSpace];
10548 }
10549
10550 return fullState;
10551 }
10552
10553 /**
10554 * Merge your own state values in <var>additionalState</var> into the base
10555 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010556 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 *
10558 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010559 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 * own additional state values.
10561 *
10562 * @param additionalState The additional state values you would like
10563 * added to <var>baseState</var>; this array is not modified.
10564 *
10565 * @return As a convenience, the <var>baseState</var> array you originally
10566 * passed into the function is returned.
10567 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010568 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 */
10570 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10571 final int N = baseState.length;
10572 int i = N - 1;
10573 while (i >= 0 && baseState[i] == 0) {
10574 i--;
10575 }
10576 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10577 return baseState;
10578 }
10579
10580 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010581 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10582 * on all Drawable objects associated with this view.
10583 */
10584 public void jumpDrawablesToCurrentState() {
10585 if (mBGDrawable != null) {
10586 mBGDrawable.jumpToCurrentState();
10587 }
10588 }
10589
10590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 * Sets the background color for this view.
10592 * @param color the color of the background
10593 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010594 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010596 if (mBGDrawable instanceof ColorDrawable) {
10597 ((ColorDrawable) mBGDrawable).setColor(color);
10598 } else {
10599 setBackgroundDrawable(new ColorDrawable(color));
10600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 }
10602
10603 /**
10604 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010605 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 * @param resid The identifier of the resource.
10607 * @attr ref android.R.styleable#View_background
10608 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010609 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 public void setBackgroundResource(int resid) {
10611 if (resid != 0 && resid == mBackgroundResource) {
10612 return;
10613 }
10614
10615 Drawable d= null;
10616 if (resid != 0) {
10617 d = mResources.getDrawable(resid);
10618 }
10619 setBackgroundDrawable(d);
10620
10621 mBackgroundResource = resid;
10622 }
10623
10624 /**
10625 * Set the background to a given Drawable, or remove the background. If the
10626 * background has padding, this View's padding is set to the background's
10627 * padding. However, when a background is removed, this View's padding isn't
10628 * touched. If setting the padding is desired, please use
10629 * {@link #setPadding(int, int, int, int)}.
10630 *
10631 * @param d The Drawable to use as the background, or null to remove the
10632 * background
10633 */
10634 public void setBackgroundDrawable(Drawable d) {
10635 boolean requestLayout = false;
10636
10637 mBackgroundResource = 0;
10638
10639 /*
10640 * Regardless of whether we're setting a new background or not, we want
10641 * to clear the previous drawable.
10642 */
10643 if (mBGDrawable != null) {
10644 mBGDrawable.setCallback(null);
10645 unscheduleDrawable(mBGDrawable);
10646 }
10647
10648 if (d != null) {
10649 Rect padding = sThreadLocal.get();
10650 if (padding == null) {
10651 padding = new Rect();
10652 sThreadLocal.set(padding);
10653 }
10654 if (d.getPadding(padding)) {
10655 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10656 }
10657
10658 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10659 // if it has a different minimum size, we should layout again
10660 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10661 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10662 requestLayout = true;
10663 }
10664
10665 d.setCallback(this);
10666 if (d.isStateful()) {
10667 d.setState(getDrawableState());
10668 }
10669 d.setVisible(getVisibility() == VISIBLE, false);
10670 mBGDrawable = d;
10671
10672 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10673 mPrivateFlags &= ~SKIP_DRAW;
10674 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10675 requestLayout = true;
10676 }
10677 } else {
10678 /* Remove the background */
10679 mBGDrawable = null;
10680
10681 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10682 /*
10683 * This view ONLY drew the background before and we're removing
10684 * the background, so now it won't draw anything
10685 * (hence we SKIP_DRAW)
10686 */
10687 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10688 mPrivateFlags |= SKIP_DRAW;
10689 }
10690
10691 /*
10692 * When the background is set, we try to apply its padding to this
10693 * View. When the background is removed, we don't touch this View's
10694 * padding. This is noted in the Javadocs. Hence, we don't need to
10695 * requestLayout(), the invalidate() below is sufficient.
10696 */
10697
10698 // The old background's minimum size could have affected this
10699 // View's layout, so let's requestLayout
10700 requestLayout = true;
10701 }
10702
Romain Guy8f1344f52009-05-15 16:03:59 -070010703 computeOpaqueFlags();
10704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 if (requestLayout) {
10706 requestLayout();
10707 }
10708
10709 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010710 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010711 }
10712
10713 /**
10714 * Gets the background drawable
10715 * @return The drawable used as the background for this view, if any.
10716 */
10717 public Drawable getBackground() {
10718 return mBGDrawable;
10719 }
10720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 /**
10722 * Sets the padding. The view may add on the space required to display
10723 * the scrollbars, depending on the style and visibility of the scrollbars.
10724 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10725 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10726 * from the values set in this call.
10727 *
10728 * @attr ref android.R.styleable#View_padding
10729 * @attr ref android.R.styleable#View_paddingBottom
10730 * @attr ref android.R.styleable#View_paddingLeft
10731 * @attr ref android.R.styleable#View_paddingRight
10732 * @attr ref android.R.styleable#View_paddingTop
10733 * @param left the left padding in pixels
10734 * @param top the top padding in pixels
10735 * @param right the right padding in pixels
10736 * @param bottom the bottom padding in pixels
10737 */
10738 public void setPadding(int left, int top, int right, int bottom) {
10739 boolean changed = false;
10740
Adam Powell20232d02010-12-08 21:08:53 -080010741 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 mUserPaddingRight = right;
10743 mUserPaddingBottom = bottom;
10744
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010745 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010746
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010747 // Common case is there are no scroll bars.
10748 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010749 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010750 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10751 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010752 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010753 switch (mVerticalScrollbarPosition) {
10754 case SCROLLBAR_POSITION_DEFAULT:
10755 case SCROLLBAR_POSITION_RIGHT:
10756 right += offset;
10757 break;
10758 case SCROLLBAR_POSITION_LEFT:
10759 left += offset;
10760 break;
10761 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010762 }
Adam Powell20232d02010-12-08 21:08:53 -080010763 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010764 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10765 ? 0 : getHorizontalScrollbarHeight();
10766 }
10767 }
Romain Guy8506ab42009-06-11 17:35:47 -070010768
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010769 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010770 changed = true;
10771 mPaddingLeft = left;
10772 }
10773 if (mPaddingTop != top) {
10774 changed = true;
10775 mPaddingTop = top;
10776 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010777 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010779 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010781 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010783 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010784 }
10785
10786 if (changed) {
10787 requestLayout();
10788 }
10789 }
10790
10791 /**
10792 * Returns the top padding of this view.
10793 *
10794 * @return the top padding in pixels
10795 */
10796 public int getPaddingTop() {
10797 return mPaddingTop;
10798 }
10799
10800 /**
10801 * Returns the bottom padding of this view. If there are inset and enabled
10802 * scrollbars, this value may include the space required to display the
10803 * scrollbars as well.
10804 *
10805 * @return the bottom padding in pixels
10806 */
10807 public int getPaddingBottom() {
10808 return mPaddingBottom;
10809 }
10810
10811 /**
10812 * Returns the left padding of this view. If there are inset and enabled
10813 * scrollbars, this value may include the space required to display the
10814 * scrollbars as well.
10815 *
10816 * @return the left padding in pixels
10817 */
10818 public int getPaddingLeft() {
10819 return mPaddingLeft;
10820 }
10821
10822 /**
10823 * Returns the right padding of this view. If there are inset and enabled
10824 * scrollbars, this value may include the space required to display the
10825 * scrollbars as well.
10826 *
10827 * @return the right padding in pixels
10828 */
10829 public int getPaddingRight() {
10830 return mPaddingRight;
10831 }
10832
10833 /**
10834 * Changes the selection state of this view. A view can be selected or not.
10835 * Note that selection is not the same as focus. Views are typically
10836 * selected in the context of an AdapterView like ListView or GridView;
10837 * the selected view is the view that is highlighted.
10838 *
10839 * @param selected true if the view must be selected, false otherwise
10840 */
10841 public void setSelected(boolean selected) {
10842 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10843 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010844 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010845 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846 refreshDrawableState();
10847 dispatchSetSelected(selected);
10848 }
10849 }
10850
10851 /**
10852 * Dispatch setSelected to all of this View's children.
10853 *
10854 * @see #setSelected(boolean)
10855 *
10856 * @param selected The new selected state
10857 */
10858 protected void dispatchSetSelected(boolean selected) {
10859 }
10860
10861 /**
10862 * Indicates the selection state of this view.
10863 *
10864 * @return true if the view is selected, false otherwise
10865 */
10866 @ViewDebug.ExportedProperty
10867 public boolean isSelected() {
10868 return (mPrivateFlags & SELECTED) != 0;
10869 }
10870
10871 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010872 * Changes the activated state of this view. A view can be activated or not.
10873 * Note that activation is not the same as selection. Selection is
10874 * a transient property, representing the view (hierarchy) the user is
10875 * currently interacting with. Activation is a longer-term state that the
10876 * user can move views in and out of. For example, in a list view with
10877 * single or multiple selection enabled, the views in the current selection
10878 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10879 * here.) The activated state is propagated down to children of the view it
10880 * is set on.
10881 *
10882 * @param activated true if the view must be activated, false otherwise
10883 */
10884 public void setActivated(boolean activated) {
10885 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10886 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010887 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010888 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010889 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010890 }
10891 }
10892
10893 /**
10894 * Dispatch setActivated to all of this View's children.
10895 *
10896 * @see #setActivated(boolean)
10897 *
10898 * @param activated The new activated state
10899 */
10900 protected void dispatchSetActivated(boolean activated) {
10901 }
10902
10903 /**
10904 * Indicates the activation state of this view.
10905 *
10906 * @return true if the view is activated, false otherwise
10907 */
10908 @ViewDebug.ExportedProperty
10909 public boolean isActivated() {
10910 return (mPrivateFlags & ACTIVATED) != 0;
10911 }
10912
10913 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010914 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10915 * observer can be used to get notifications when global events, like
10916 * layout, happen.
10917 *
10918 * The returned ViewTreeObserver observer is not guaranteed to remain
10919 * valid for the lifetime of this View. If the caller of this method keeps
10920 * a long-lived reference to ViewTreeObserver, it should always check for
10921 * the return value of {@link ViewTreeObserver#isAlive()}.
10922 *
10923 * @return The ViewTreeObserver for this view's hierarchy.
10924 */
10925 public ViewTreeObserver getViewTreeObserver() {
10926 if (mAttachInfo != null) {
10927 return mAttachInfo.mTreeObserver;
10928 }
10929 if (mFloatingTreeObserver == null) {
10930 mFloatingTreeObserver = new ViewTreeObserver();
10931 }
10932 return mFloatingTreeObserver;
10933 }
10934
10935 /**
10936 * <p>Finds the topmost view in the current view hierarchy.</p>
10937 *
10938 * @return the topmost view containing this view
10939 */
10940 public View getRootView() {
10941 if (mAttachInfo != null) {
10942 final View v = mAttachInfo.mRootView;
10943 if (v != null) {
10944 return v;
10945 }
10946 }
Romain Guy8506ab42009-06-11 17:35:47 -070010947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 View parent = this;
10949
10950 while (parent.mParent != null && parent.mParent instanceof View) {
10951 parent = (View) parent.mParent;
10952 }
10953
10954 return parent;
10955 }
10956
10957 /**
10958 * <p>Computes the coordinates of this view on the screen. The argument
10959 * must be an array of two integers. After the method returns, the array
10960 * contains the x and y location in that order.</p>
10961 *
10962 * @param location an array of two integers in which to hold the coordinates
10963 */
10964 public void getLocationOnScreen(int[] location) {
10965 getLocationInWindow(location);
10966
10967 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010968 if (info != null) {
10969 location[0] += info.mWindowLeft;
10970 location[1] += info.mWindowTop;
10971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010972 }
10973
10974 /**
10975 * <p>Computes the coordinates of this view in its window. The argument
10976 * must be an array of two integers. After the method returns, the array
10977 * contains the x and y location in that order.</p>
10978 *
10979 * @param location an array of two integers in which to hold the coordinates
10980 */
10981 public void getLocationInWindow(int[] location) {
10982 if (location == null || location.length < 2) {
10983 throw new IllegalArgumentException("location must be an array of "
10984 + "two integers");
10985 }
10986
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010987 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10988 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989
10990 ViewParent viewParent = mParent;
10991 while (viewParent instanceof View) {
10992 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010993 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10994 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995 viewParent = view.mParent;
10996 }
Romain Guy8506ab42009-06-11 17:35:47 -070010997
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010998 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011000 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 location[1] -= vr.mCurScrollY;
11002 }
11003 }
11004
11005 /**
11006 * {@hide}
11007 * @param id the id of the view to be found
11008 * @return the view of the specified id, null if cannot be found
11009 */
11010 protected View findViewTraversal(int id) {
11011 if (id == mID) {
11012 return this;
11013 }
11014 return null;
11015 }
11016
11017 /**
11018 * {@hide}
11019 * @param tag the tag of the view to be found
11020 * @return the view of specified tag, null if cannot be found
11021 */
11022 protected View findViewWithTagTraversal(Object tag) {
11023 if (tag != null && tag.equals(mTag)) {
11024 return this;
11025 }
11026 return null;
11027 }
11028
11029 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011030 * {@hide}
11031 * @param predicate The predicate to evaluate.
11032 * @return The first view that matches the predicate or null.
11033 */
11034 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11035 if (predicate.apply(this)) {
11036 return this;
11037 }
11038 return null;
11039 }
11040
11041 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011042 * Look for a child view with the given id. If this view has the given
11043 * id, return this view.
11044 *
11045 * @param id The id to search for.
11046 * @return The view that has the given id in the hierarchy or null
11047 */
11048 public final View findViewById(int id) {
11049 if (id < 0) {
11050 return null;
11051 }
11052 return findViewTraversal(id);
11053 }
11054
11055 /**
11056 * Look for a child view with the given tag. If this view has the given
11057 * tag, return this view.
11058 *
11059 * @param tag The tag to search for, using "tag.equals(getTag())".
11060 * @return The View that has the given tag in the hierarchy or null
11061 */
11062 public final View findViewWithTag(Object tag) {
11063 if (tag == null) {
11064 return null;
11065 }
11066 return findViewWithTagTraversal(tag);
11067 }
11068
11069 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011070 * {@hide}
11071 * Look for a child view that matches the specified predicate.
11072 * If this view matches the predicate, return this view.
11073 *
11074 * @param predicate The predicate to evaluate.
11075 * @return The first view that matches the predicate or null.
11076 */
11077 public final View findViewByPredicate(Predicate<View> predicate) {
11078 return findViewByPredicateTraversal(predicate);
11079 }
11080
11081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011082 * Sets the identifier for this view. The identifier does not have to be
11083 * unique in this view's hierarchy. The identifier should be a positive
11084 * number.
11085 *
11086 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011087 * @see #getId()
11088 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089 *
11090 * @param id a number used to identify the view
11091 *
11092 * @attr ref android.R.styleable#View_id
11093 */
11094 public void setId(int id) {
11095 mID = id;
11096 }
11097
11098 /**
11099 * {@hide}
11100 *
11101 * @param isRoot true if the view belongs to the root namespace, false
11102 * otherwise
11103 */
11104 public void setIsRootNamespace(boolean isRoot) {
11105 if (isRoot) {
11106 mPrivateFlags |= IS_ROOT_NAMESPACE;
11107 } else {
11108 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11109 }
11110 }
11111
11112 /**
11113 * {@hide}
11114 *
11115 * @return true if the view belongs to the root namespace, false otherwise
11116 */
11117 public boolean isRootNamespace() {
11118 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11119 }
11120
11121 /**
11122 * Returns this view's identifier.
11123 *
11124 * @return a positive integer used to identify the view or {@link #NO_ID}
11125 * if the view has no ID
11126 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011127 * @see #setId(int)
11128 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 * @attr ref android.R.styleable#View_id
11130 */
11131 @ViewDebug.CapturedViewProperty
11132 public int getId() {
11133 return mID;
11134 }
11135
11136 /**
11137 * Returns this view's tag.
11138 *
11139 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011140 *
11141 * @see #setTag(Object)
11142 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 */
11144 @ViewDebug.ExportedProperty
11145 public Object getTag() {
11146 return mTag;
11147 }
11148
11149 /**
11150 * Sets the tag associated with this view. A tag can be used to mark
11151 * a view in its hierarchy and does not have to be unique within the
11152 * hierarchy. Tags can also be used to store data within a view without
11153 * resorting to another data structure.
11154 *
11155 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011156 *
11157 * @see #getTag()
11158 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 */
11160 public void setTag(final Object tag) {
11161 mTag = tag;
11162 }
11163
11164 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011165 * Returns the tag associated with this view and the specified key.
11166 *
11167 * @param key The key identifying the tag
11168 *
11169 * @return the Object stored in this view as a tag
11170 *
11171 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011172 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011173 */
11174 public Object getTag(int key) {
11175 SparseArray<Object> tags = null;
11176 synchronized (sTagsLock) {
11177 if (sTags != null) {
11178 tags = sTags.get(this);
11179 }
11180 }
11181
11182 if (tags != null) return tags.get(key);
11183 return null;
11184 }
11185
11186 /**
11187 * Sets a tag associated with this view and a key. A tag can be used
11188 * to mark a view in its hierarchy and does not have to be unique within
11189 * the hierarchy. Tags can also be used to store data within a view
11190 * without resorting to another data structure.
11191 *
11192 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011193 * application to ensure it is unique (see the <a
11194 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11195 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011196 * the Android framework or not associated with any package will cause
11197 * an {@link IllegalArgumentException} to be thrown.
11198 *
11199 * @param key The key identifying the tag
11200 * @param tag An Object to tag the view with
11201 *
11202 * @throws IllegalArgumentException If they specified key is not valid
11203 *
11204 * @see #setTag(Object)
11205 * @see #getTag(int)
11206 */
11207 public void setTag(int key, final Object tag) {
11208 // If the package id is 0x00 or 0x01, it's either an undefined package
11209 // or a framework id
11210 if ((key >>> 24) < 2) {
11211 throw new IllegalArgumentException("The key must be an application-specific "
11212 + "resource id.");
11213 }
11214
11215 setTagInternal(this, key, tag);
11216 }
11217
11218 /**
11219 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11220 * framework id.
11221 *
11222 * @hide
11223 */
11224 public void setTagInternal(int key, Object tag) {
11225 if ((key >>> 24) != 0x1) {
11226 throw new IllegalArgumentException("The key must be a framework-specific "
11227 + "resource id.");
11228 }
11229
Romain Guy8506ab42009-06-11 17:35:47 -070011230 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011231 }
11232
11233 private static void setTagInternal(View view, int key, Object tag) {
11234 SparseArray<Object> tags = null;
11235 synchronized (sTagsLock) {
11236 if (sTags == null) {
11237 sTags = new WeakHashMap<View, SparseArray<Object>>();
11238 } else {
11239 tags = sTags.get(view);
11240 }
11241 }
11242
11243 if (tags == null) {
11244 tags = new SparseArray<Object>(2);
11245 synchronized (sTagsLock) {
11246 sTags.put(view, tags);
11247 }
11248 }
11249
11250 tags.put(key, tag);
11251 }
11252
11253 /**
Romain Guy13922e02009-05-12 17:56:14 -070011254 * @param consistency The type of consistency. See ViewDebug for more information.
11255 *
11256 * @hide
11257 */
11258 protected boolean dispatchConsistencyCheck(int consistency) {
11259 return onConsistencyCheck(consistency);
11260 }
11261
11262 /**
11263 * Method that subclasses should implement to check their consistency. The type of
11264 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011265 *
Romain Guy13922e02009-05-12 17:56:14 -070011266 * @param consistency The type of consistency. See ViewDebug for more information.
11267 *
11268 * @throws IllegalStateException if the view is in an inconsistent state.
11269 *
11270 * @hide
11271 */
11272 protected boolean onConsistencyCheck(int consistency) {
11273 boolean result = true;
11274
11275 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11276 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11277
11278 if (checkLayout) {
11279 if (getParent() == null) {
11280 result = false;
11281 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11282 "View " + this + " does not have a parent.");
11283 }
11284
11285 if (mAttachInfo == null) {
11286 result = false;
11287 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11288 "View " + this + " is not attached to a window.");
11289 }
11290 }
11291
11292 if (checkDrawing) {
11293 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11294 // from their draw() method
11295
11296 if ((mPrivateFlags & DRAWN) != DRAWN &&
11297 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11298 result = false;
11299 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11300 "View " + this + " was invalidated but its drawing cache is valid.");
11301 }
11302 }
11303
11304 return result;
11305 }
11306
11307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011308 * Prints information about this view in the log output, with the tag
11309 * {@link #VIEW_LOG_TAG}.
11310 *
11311 * @hide
11312 */
11313 public void debug() {
11314 debug(0);
11315 }
11316
11317 /**
11318 * Prints information about this view in the log output, with the tag
11319 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11320 * indentation defined by the <code>depth</code>.
11321 *
11322 * @param depth the indentation level
11323 *
11324 * @hide
11325 */
11326 protected void debug(int depth) {
11327 String output = debugIndent(depth - 1);
11328
11329 output += "+ " + this;
11330 int id = getId();
11331 if (id != -1) {
11332 output += " (id=" + id + ")";
11333 }
11334 Object tag = getTag();
11335 if (tag != null) {
11336 output += " (tag=" + tag + ")";
11337 }
11338 Log.d(VIEW_LOG_TAG, output);
11339
11340 if ((mPrivateFlags & FOCUSED) != 0) {
11341 output = debugIndent(depth) + " FOCUSED";
11342 Log.d(VIEW_LOG_TAG, output);
11343 }
11344
11345 output = debugIndent(depth);
11346 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11347 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11348 + "} ";
11349 Log.d(VIEW_LOG_TAG, output);
11350
11351 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11352 || mPaddingBottom != 0) {
11353 output = debugIndent(depth);
11354 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11355 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11356 Log.d(VIEW_LOG_TAG, output);
11357 }
11358
11359 output = debugIndent(depth);
11360 output += "mMeasureWidth=" + mMeasuredWidth +
11361 " mMeasureHeight=" + mMeasuredHeight;
11362 Log.d(VIEW_LOG_TAG, output);
11363
11364 output = debugIndent(depth);
11365 if (mLayoutParams == null) {
11366 output += "BAD! no layout params";
11367 } else {
11368 output = mLayoutParams.debug(output);
11369 }
11370 Log.d(VIEW_LOG_TAG, output);
11371
11372 output = debugIndent(depth);
11373 output += "flags={";
11374 output += View.printFlags(mViewFlags);
11375 output += "}";
11376 Log.d(VIEW_LOG_TAG, output);
11377
11378 output = debugIndent(depth);
11379 output += "privateFlags={";
11380 output += View.printPrivateFlags(mPrivateFlags);
11381 output += "}";
11382 Log.d(VIEW_LOG_TAG, output);
11383 }
11384
11385 /**
11386 * Creates an string of whitespaces used for indentation.
11387 *
11388 * @param depth the indentation level
11389 * @return a String containing (depth * 2 + 3) * 2 white spaces
11390 *
11391 * @hide
11392 */
11393 protected static String debugIndent(int depth) {
11394 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11395 for (int i = 0; i < (depth * 2) + 3; i++) {
11396 spaces.append(' ').append(' ');
11397 }
11398 return spaces.toString();
11399 }
11400
11401 /**
11402 * <p>Return the offset of the widget's text baseline from the widget's top
11403 * boundary. If this widget does not support baseline alignment, this
11404 * method returns -1. </p>
11405 *
11406 * @return the offset of the baseline within the widget's bounds or -1
11407 * if baseline alignment is not supported
11408 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011409 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 public int getBaseline() {
11411 return -1;
11412 }
11413
11414 /**
11415 * Call this when something has changed which has invalidated the
11416 * layout of this view. This will schedule a layout pass of the view
11417 * tree.
11418 */
11419 public void requestLayout() {
11420 if (ViewDebug.TRACE_HIERARCHY) {
11421 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11422 }
11423
11424 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011425 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426
11427 if (mParent != null && !mParent.isLayoutRequested()) {
11428 mParent.requestLayout();
11429 }
11430 }
11431
11432 /**
11433 * Forces this view to be laid out during the next layout pass.
11434 * This method does not call requestLayout() or forceLayout()
11435 * on the parent.
11436 */
11437 public void forceLayout() {
11438 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011439 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011440 }
11441
11442 /**
11443 * <p>
11444 * This is called to find out how big a view should be. The parent
11445 * supplies constraint information in the width and height parameters.
11446 * </p>
11447 *
11448 * <p>
11449 * The actual mesurement work of a view is performed in
11450 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11451 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11452 * </p>
11453 *
11454 *
11455 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11456 * parent
11457 * @param heightMeasureSpec Vertical space requirements as imposed by the
11458 * parent
11459 *
11460 * @see #onMeasure(int, int)
11461 */
11462 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11463 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11464 widthMeasureSpec != mOldWidthMeasureSpec ||
11465 heightMeasureSpec != mOldHeightMeasureSpec) {
11466
11467 // first clears the measured dimension flag
11468 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11469
11470 if (ViewDebug.TRACE_HIERARCHY) {
11471 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11472 }
11473
11474 // measure ourselves, this should set the measured dimension flag back
11475 onMeasure(widthMeasureSpec, heightMeasureSpec);
11476
11477 // flag not set, setMeasuredDimension() was not invoked, we raise
11478 // an exception to warn the developer
11479 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11480 throw new IllegalStateException("onMeasure() did not set the"
11481 + " measured dimension by calling"
11482 + " setMeasuredDimension()");
11483 }
11484
11485 mPrivateFlags |= LAYOUT_REQUIRED;
11486 }
11487
11488 mOldWidthMeasureSpec = widthMeasureSpec;
11489 mOldHeightMeasureSpec = heightMeasureSpec;
11490 }
11491
11492 /**
11493 * <p>
11494 * Measure the view and its content to determine the measured width and the
11495 * measured height. This method is invoked by {@link #measure(int, int)} and
11496 * should be overriden by subclasses to provide accurate and efficient
11497 * measurement of their contents.
11498 * </p>
11499 *
11500 * <p>
11501 * <strong>CONTRACT:</strong> When overriding this method, you
11502 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11503 * measured width and height of this view. Failure to do so will trigger an
11504 * <code>IllegalStateException</code>, thrown by
11505 * {@link #measure(int, int)}. Calling the superclass'
11506 * {@link #onMeasure(int, int)} is a valid use.
11507 * </p>
11508 *
11509 * <p>
11510 * The base class implementation of measure defaults to the background size,
11511 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11512 * override {@link #onMeasure(int, int)} to provide better measurements of
11513 * their content.
11514 * </p>
11515 *
11516 * <p>
11517 * If this method is overridden, it is the subclass's responsibility to make
11518 * sure the measured height and width are at least the view's minimum height
11519 * and width ({@link #getSuggestedMinimumHeight()} and
11520 * {@link #getSuggestedMinimumWidth()}).
11521 * </p>
11522 *
11523 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11524 * The requirements are encoded with
11525 * {@link android.view.View.MeasureSpec}.
11526 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11527 * The requirements are encoded with
11528 * {@link android.view.View.MeasureSpec}.
11529 *
11530 * @see #getMeasuredWidth()
11531 * @see #getMeasuredHeight()
11532 * @see #setMeasuredDimension(int, int)
11533 * @see #getSuggestedMinimumHeight()
11534 * @see #getSuggestedMinimumWidth()
11535 * @see android.view.View.MeasureSpec#getMode(int)
11536 * @see android.view.View.MeasureSpec#getSize(int)
11537 */
11538 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11539 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11540 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11541 }
11542
11543 /**
11544 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11545 * measured width and measured height. Failing to do so will trigger an
11546 * exception at measurement time.</p>
11547 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011548 * @param measuredWidth The measured width of this view. May be a complex
11549 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11550 * {@link #MEASURED_STATE_TOO_SMALL}.
11551 * @param measuredHeight The measured height of this view. May be a complex
11552 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11553 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011554 */
11555 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11556 mMeasuredWidth = measuredWidth;
11557 mMeasuredHeight = measuredHeight;
11558
11559 mPrivateFlags |= MEASURED_DIMENSION_SET;
11560 }
11561
11562 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011563 * Merge two states as returned by {@link #getMeasuredState()}.
11564 * @param curState The current state as returned from a view or the result
11565 * of combining multiple views.
11566 * @param newState The new view state to combine.
11567 * @return Returns a new integer reflecting the combination of the two
11568 * states.
11569 */
11570 public static int combineMeasuredStates(int curState, int newState) {
11571 return curState | newState;
11572 }
11573
11574 /**
11575 * Version of {@link #resolveSizeAndState(int, int, int)}
11576 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11577 */
11578 public static int resolveSize(int size, int measureSpec) {
11579 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11580 }
11581
11582 /**
11583 * Utility to reconcile a desired size and state, with constraints imposed
11584 * by a MeasureSpec. Will take the desired size, unless a different size
11585 * is imposed by the constraints. The returned value is a compound integer,
11586 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11587 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11588 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589 *
11590 * @param size How big the view wants to be
11591 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011592 * @return Size information bit mask as defined by
11593 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011594 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011595 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011596 int result = size;
11597 int specMode = MeasureSpec.getMode(measureSpec);
11598 int specSize = MeasureSpec.getSize(measureSpec);
11599 switch (specMode) {
11600 case MeasureSpec.UNSPECIFIED:
11601 result = size;
11602 break;
11603 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011604 if (specSize < size) {
11605 result = specSize | MEASURED_STATE_TOO_SMALL;
11606 } else {
11607 result = size;
11608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011609 break;
11610 case MeasureSpec.EXACTLY:
11611 result = specSize;
11612 break;
11613 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011614 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011615 }
11616
11617 /**
11618 * Utility to return a default size. Uses the supplied size if the
11619 * MeasureSpec imposed no contraints. Will get larger if allowed
11620 * by the MeasureSpec.
11621 *
11622 * @param size Default size for this view
11623 * @param measureSpec Constraints imposed by the parent
11624 * @return The size this view should be.
11625 */
11626 public static int getDefaultSize(int size, int measureSpec) {
11627 int result = size;
11628 int specMode = MeasureSpec.getMode(measureSpec);
11629 int specSize = MeasureSpec.getSize(measureSpec);
11630
11631 switch (specMode) {
11632 case MeasureSpec.UNSPECIFIED:
11633 result = size;
11634 break;
11635 case MeasureSpec.AT_MOST:
11636 case MeasureSpec.EXACTLY:
11637 result = specSize;
11638 break;
11639 }
11640 return result;
11641 }
11642
11643 /**
11644 * Returns the suggested minimum height that the view should use. This
11645 * returns the maximum of the view's minimum height
11646 * and the background's minimum height
11647 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11648 * <p>
11649 * When being used in {@link #onMeasure(int, int)}, the caller should still
11650 * ensure the returned height is within the requirements of the parent.
11651 *
11652 * @return The suggested minimum height of the view.
11653 */
11654 protected int getSuggestedMinimumHeight() {
11655 int suggestedMinHeight = mMinHeight;
11656
11657 if (mBGDrawable != null) {
11658 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11659 if (suggestedMinHeight < bgMinHeight) {
11660 suggestedMinHeight = bgMinHeight;
11661 }
11662 }
11663
11664 return suggestedMinHeight;
11665 }
11666
11667 /**
11668 * Returns the suggested minimum width that the view should use. This
11669 * returns the maximum of the view's minimum width)
11670 * and the background's minimum width
11671 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11672 * <p>
11673 * When being used in {@link #onMeasure(int, int)}, the caller should still
11674 * ensure the returned width is within the requirements of the parent.
11675 *
11676 * @return The suggested minimum width of the view.
11677 */
11678 protected int getSuggestedMinimumWidth() {
11679 int suggestedMinWidth = mMinWidth;
11680
11681 if (mBGDrawable != null) {
11682 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11683 if (suggestedMinWidth < bgMinWidth) {
11684 suggestedMinWidth = bgMinWidth;
11685 }
11686 }
11687
11688 return suggestedMinWidth;
11689 }
11690
11691 /**
11692 * Sets the minimum height of the view. It is not guaranteed the view will
11693 * be able to achieve this minimum height (for example, if its parent layout
11694 * constrains it with less available height).
11695 *
11696 * @param minHeight The minimum height the view will try to be.
11697 */
11698 public void setMinimumHeight(int minHeight) {
11699 mMinHeight = minHeight;
11700 }
11701
11702 /**
11703 * Sets the minimum width of the view. It is not guaranteed the view will
11704 * be able to achieve this minimum width (for example, if its parent layout
11705 * constrains it with less available width).
11706 *
11707 * @param minWidth The minimum width the view will try to be.
11708 */
11709 public void setMinimumWidth(int minWidth) {
11710 mMinWidth = minWidth;
11711 }
11712
11713 /**
11714 * Get the animation currently associated with this view.
11715 *
11716 * @return The animation that is currently playing or
11717 * scheduled to play for this view.
11718 */
11719 public Animation getAnimation() {
11720 return mCurrentAnimation;
11721 }
11722
11723 /**
11724 * Start the specified animation now.
11725 *
11726 * @param animation the animation to start now
11727 */
11728 public void startAnimation(Animation animation) {
11729 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11730 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011731 invalidateParentCaches();
11732 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011733 }
11734
11735 /**
11736 * Cancels any animations for this view.
11737 */
11738 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011739 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011740 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011743 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011744 }
11745
11746 /**
11747 * Sets the next animation to play for this view.
11748 * If you want the animation to play immediately, use
11749 * startAnimation. This method provides allows fine-grained
11750 * control over the start time and invalidation, but you
11751 * must make sure that 1) the animation has a start time set, and
11752 * 2) the view will be invalidated when the animation is supposed to
11753 * start.
11754 *
11755 * @param animation The next animation, or null.
11756 */
11757 public void setAnimation(Animation animation) {
11758 mCurrentAnimation = animation;
11759 if (animation != null) {
11760 animation.reset();
11761 }
11762 }
11763
11764 /**
11765 * Invoked by a parent ViewGroup to notify the start of the animation
11766 * currently associated with this view. If you override this method,
11767 * always call super.onAnimationStart();
11768 *
11769 * @see #setAnimation(android.view.animation.Animation)
11770 * @see #getAnimation()
11771 */
11772 protected void onAnimationStart() {
11773 mPrivateFlags |= ANIMATION_STARTED;
11774 }
11775
11776 /**
11777 * Invoked by a parent ViewGroup to notify the end of the animation
11778 * currently associated with this view. If you override this method,
11779 * always call super.onAnimationEnd();
11780 *
11781 * @see #setAnimation(android.view.animation.Animation)
11782 * @see #getAnimation()
11783 */
11784 protected void onAnimationEnd() {
11785 mPrivateFlags &= ~ANIMATION_STARTED;
11786 }
11787
11788 /**
11789 * Invoked if there is a Transform that involves alpha. Subclass that can
11790 * draw themselves with the specified alpha should return true, and then
11791 * respect that alpha when their onDraw() is called. If this returns false
11792 * then the view may be redirected to draw into an offscreen buffer to
11793 * fulfill the request, which will look fine, but may be slower than if the
11794 * subclass handles it internally. The default implementation returns false.
11795 *
11796 * @param alpha The alpha (0..255) to apply to the view's drawing
11797 * @return true if the view can draw with the specified alpha.
11798 */
11799 protected boolean onSetAlpha(int alpha) {
11800 return false;
11801 }
11802
11803 /**
11804 * This is used by the RootView to perform an optimization when
11805 * the view hierarchy contains one or several SurfaceView.
11806 * SurfaceView is always considered transparent, but its children are not,
11807 * therefore all View objects remove themselves from the global transparent
11808 * region (passed as a parameter to this function).
11809 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011810 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011811 *
11812 * @return Returns true if the effective visibility of the view at this
11813 * point is opaque, regardless of the transparent region; returns false
11814 * if it is possible for underlying windows to be seen behind the view.
11815 *
11816 * {@hide}
11817 */
11818 public boolean gatherTransparentRegion(Region region) {
11819 final AttachInfo attachInfo = mAttachInfo;
11820 if (region != null && attachInfo != null) {
11821 final int pflags = mPrivateFlags;
11822 if ((pflags & SKIP_DRAW) == 0) {
11823 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11824 // remove it from the transparent region.
11825 final int[] location = attachInfo.mTransparentLocation;
11826 getLocationInWindow(location);
11827 region.op(location[0], location[1], location[0] + mRight - mLeft,
11828 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11829 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11830 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11831 // exists, so we remove the background drawable's non-transparent
11832 // parts from this transparent region.
11833 applyDrawableToTransparentRegion(mBGDrawable, region);
11834 }
11835 }
11836 return true;
11837 }
11838
11839 /**
11840 * Play a sound effect for this view.
11841 *
11842 * <p>The framework will play sound effects for some built in actions, such as
11843 * clicking, but you may wish to play these effects in your widget,
11844 * for instance, for internal navigation.
11845 *
11846 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11847 * {@link #isSoundEffectsEnabled()} is true.
11848 *
11849 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11850 */
11851 public void playSoundEffect(int soundConstant) {
11852 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11853 return;
11854 }
11855 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11856 }
11857
11858 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011859 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011860 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011861 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 *
11863 * <p>The framework will provide haptic feedback for some built in actions,
11864 * such as long presses, but you may wish to provide feedback for your
11865 * own widget.
11866 *
11867 * <p>The feedback will only be performed if
11868 * {@link #isHapticFeedbackEnabled()} is true.
11869 *
11870 * @param feedbackConstant One of the constants defined in
11871 * {@link HapticFeedbackConstants}
11872 */
11873 public boolean performHapticFeedback(int feedbackConstant) {
11874 return performHapticFeedback(feedbackConstant, 0);
11875 }
11876
11877 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011878 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011879 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011880 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011881 *
11882 * @param feedbackConstant One of the constants defined in
11883 * {@link HapticFeedbackConstants}
11884 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11885 */
11886 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11887 if (mAttachInfo == null) {
11888 return false;
11889 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011890 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011891 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 && !isHapticFeedbackEnabled()) {
11893 return false;
11894 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011895 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11896 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011897 }
11898
11899 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011900 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070011901 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011902 */
11903 public void setSystemUiVisibility(int visibility) {
11904 if (visibility != mSystemUiVisibility) {
11905 mSystemUiVisibility = visibility;
11906 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11907 mParent.recomputeViewAttributes(this);
11908 }
11909 }
11910 }
11911
11912 /**
11913 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070011914 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011915 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011916 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011917 return mSystemUiVisibility;
11918 }
11919
Scott Mainec6331b2011-05-24 16:55:56 -070011920 /**
11921 * Set a listener to receive callbacks when the visibility of the system bar changes.
11922 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
11923 */
Joe Onorato664644d2011-01-23 17:53:23 -080011924 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11925 mOnSystemUiVisibilityChangeListener = l;
11926 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11927 mParent.recomputeViewAttributes(this);
11928 }
11929 }
11930
11931 /**
11932 */
11933 public void dispatchSystemUiVisibilityChanged(int visibility) {
11934 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011935 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011936 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011937 }
11938 }
11939
11940 /**
Joe Malin32736f02011-01-19 16:14:20 -080011941 * Creates an image that the system displays during the drag and drop
11942 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11943 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11944 * appearance as the given View. The default also positions the center of the drag shadow
11945 * directly under the touch point. If no View is provided (the constructor with no parameters
11946 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11947 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11948 * default is an invisible drag shadow.
11949 * <p>
11950 * You are not required to use the View you provide to the constructor as the basis of the
11951 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11952 * anything you want as the drag shadow.
11953 * </p>
11954 * <p>
11955 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11956 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11957 * size and position of the drag shadow. It uses this data to construct a
11958 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11959 * so that your application can draw the shadow image in the Canvas.
11960 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011961 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011962 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011963 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011964
11965 /**
Joe Malin32736f02011-01-19 16:14:20 -080011966 * Constructs a shadow image builder based on a View. By default, the resulting drag
11967 * shadow will have the same appearance and dimensions as the View, with the touch point
11968 * over the center of the View.
11969 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011970 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011971 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011972 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011973 }
11974
Christopher Tate17ed60c2011-01-18 12:50:26 -080011975 /**
11976 * Construct a shadow builder object with no associated View. This
11977 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11978 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11979 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011980 * reference to any View object. If they are not overridden, then the result is an
11981 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011982 */
11983 public DragShadowBuilder() {
11984 mView = new WeakReference<View>(null);
11985 }
11986
11987 /**
11988 * Returns the View object that had been passed to the
11989 * {@link #View.DragShadowBuilder(View)}
11990 * constructor. If that View parameter was {@code null} or if the
11991 * {@link #View.DragShadowBuilder()}
11992 * constructor was used to instantiate the builder object, this method will return
11993 * null.
11994 *
11995 * @return The View object associate with this builder object.
11996 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070011997 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070011998 final public View getView() {
11999 return mView.get();
12000 }
12001
Christopher Tate2c095f32010-10-04 14:13:40 -070012002 /**
Joe Malin32736f02011-01-19 16:14:20 -080012003 * Provides the metrics for the shadow image. These include the dimensions of
12004 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012005 * be centered under the touch location while dragging.
12006 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012007 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012008 * same as the dimensions of the View itself and centers the shadow under
12009 * the touch point.
12010 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012011 *
Joe Malin32736f02011-01-19 16:14:20 -080012012 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12013 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12014 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12015 * image.
12016 *
12017 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12018 * shadow image that should be underneath the touch point during the drag and drop
12019 * operation. Your application must set {@link android.graphics.Point#x} to the
12020 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012021 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012022 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012023 final View view = mView.get();
12024 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012025 shadowSize.set(view.getWidth(), view.getHeight());
12026 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012027 } else {
12028 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12029 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012030 }
12031
12032 /**
Joe Malin32736f02011-01-19 16:14:20 -080012033 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12034 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012035 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012036 *
Joe Malin32736f02011-01-19 16:14:20 -080012037 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012038 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012039 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012040 final View view = mView.get();
12041 if (view != null) {
12042 view.draw(canvas);
12043 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012044 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012045 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012046 }
12047 }
12048
12049 /**
Joe Malin32736f02011-01-19 16:14:20 -080012050 * Starts a drag and drop operation. When your application calls this method, it passes a
12051 * {@link android.view.View.DragShadowBuilder} object to the system. The
12052 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12053 * to get metrics for the drag shadow, and then calls the object's
12054 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12055 * <p>
12056 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12057 * drag events to all the View objects in your application that are currently visible. It does
12058 * this either by calling the View object's drag listener (an implementation of
12059 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12060 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12061 * Both are passed a {@link android.view.DragEvent} object that has a
12062 * {@link android.view.DragEvent#getAction()} value of
12063 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12064 * </p>
12065 * <p>
12066 * Your application can invoke startDrag() on any attached View object. The View object does not
12067 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12068 * be related to the View the user selected for dragging.
12069 * </p>
12070 * @param data A {@link android.content.ClipData} object pointing to the data to be
12071 * transferred by the drag and drop operation.
12072 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12073 * drag shadow.
12074 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12075 * drop operation. This Object is put into every DragEvent object sent by the system during the
12076 * current drag.
12077 * <p>
12078 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12079 * to the target Views. For example, it can contain flags that differentiate between a
12080 * a copy operation and a move operation.
12081 * </p>
12082 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12083 * so the parameter should be set to 0.
12084 * @return {@code true} if the method completes successfully, or
12085 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12086 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012087 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012088 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012089 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012090 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012091 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012092 }
12093 boolean okay = false;
12094
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012095 Point shadowSize = new Point();
12096 Point shadowTouchPoint = new Point();
12097 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012098
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012099 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12100 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12101 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012102 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012103
Chris Tatea32dcf72010-10-14 12:13:50 -070012104 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012105 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12106 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012107 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012108 Surface surface = new Surface();
12109 try {
12110 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012111 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012112 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012113 + " surface=" + surface);
12114 if (token != null) {
12115 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012116 try {
Chris Tate6b391282010-10-14 15:48:59 -070012117 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012118 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012119 } finally {
12120 surface.unlockCanvasAndPost(canvas);
12121 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012122
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012123 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012124
12125 // Cache the local state object for delivery with DragEvents
12126 root.setLocalDragState(myLocalState);
12127
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012128 // repurpose 'shadowSize' for the last touch point
12129 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012130
Christopher Tatea53146c2010-09-07 11:57:52 -070012131 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012132 shadowSize.x, shadowSize.y,
12133 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012134 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012135 }
12136 } catch (Exception e) {
12137 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12138 surface.destroy();
12139 }
12140
12141 return okay;
12142 }
12143
Christopher Tatea53146c2010-09-07 11:57:52 -070012144 /**
Joe Malin32736f02011-01-19 16:14:20 -080012145 * Handles drag events sent by the system following a call to
12146 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12147 *<p>
12148 * When the system calls this method, it passes a
12149 * {@link android.view.DragEvent} object. A call to
12150 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12151 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12152 * operation.
12153 * @param event The {@link android.view.DragEvent} sent by the system.
12154 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12155 * in DragEvent, indicating the type of drag event represented by this object.
12156 * @return {@code true} if the method was successful, otherwise {@code false}.
12157 * <p>
12158 * The method should return {@code true} in response to an action type of
12159 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12160 * operation.
12161 * </p>
12162 * <p>
12163 * The method should also return {@code true} in response to an action type of
12164 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12165 * {@code false} if it didn't.
12166 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012167 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012168 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012169 return false;
12170 }
12171
12172 /**
Joe Malin32736f02011-01-19 16:14:20 -080012173 * Detects if this View is enabled and has a drag event listener.
12174 * If both are true, then it calls the drag event listener with the
12175 * {@link android.view.DragEvent} it received. If the drag event listener returns
12176 * {@code true}, then dispatchDragEvent() returns {@code true}.
12177 * <p>
12178 * For all other cases, the method calls the
12179 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12180 * method and returns its result.
12181 * </p>
12182 * <p>
12183 * This ensures that a drag event is always consumed, even if the View does not have a drag
12184 * event listener. However, if the View has a listener and the listener returns true, then
12185 * onDragEvent() is not called.
12186 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012187 */
12188 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012189 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012190 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12191 && mOnDragListener.onDrag(this, event)) {
12192 return true;
12193 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012194 return onDragEvent(event);
12195 }
12196
Christopher Tate3d4bf172011-03-28 16:16:46 -070012197 boolean canAcceptDrag() {
12198 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12199 }
12200
Christopher Tatea53146c2010-09-07 11:57:52 -070012201 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012202 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12203 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012204 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012205 */
12206 public void onCloseSystemDialogs(String reason) {
12207 }
Joe Malin32736f02011-01-19 16:14:20 -080012208
Dianne Hackbornffa42482009-09-23 22:20:11 -070012209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012211 * update a Region being computed for
12212 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012213 * that any non-transparent parts of the Drawable are removed from the
12214 * given transparent region.
12215 *
12216 * @param dr The Drawable whose transparency is to be applied to the region.
12217 * @param region A Region holding the current transparency information,
12218 * where any parts of the region that are set are considered to be
12219 * transparent. On return, this region will be modified to have the
12220 * transparency information reduced by the corresponding parts of the
12221 * Drawable that are not transparent.
12222 * {@hide}
12223 */
12224 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12225 if (DBG) {
12226 Log.i("View", "Getting transparent region for: " + this);
12227 }
12228 final Region r = dr.getTransparentRegion();
12229 final Rect db = dr.getBounds();
12230 final AttachInfo attachInfo = mAttachInfo;
12231 if (r != null && attachInfo != null) {
12232 final int w = getRight()-getLeft();
12233 final int h = getBottom()-getTop();
12234 if (db.left > 0) {
12235 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12236 r.op(0, 0, db.left, h, Region.Op.UNION);
12237 }
12238 if (db.right < w) {
12239 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12240 r.op(db.right, 0, w, h, Region.Op.UNION);
12241 }
12242 if (db.top > 0) {
12243 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12244 r.op(0, 0, w, db.top, Region.Op.UNION);
12245 }
12246 if (db.bottom < h) {
12247 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12248 r.op(0, db.bottom, w, h, Region.Op.UNION);
12249 }
12250 final int[] location = attachInfo.mTransparentLocation;
12251 getLocationInWindow(location);
12252 r.translate(location[0], location[1]);
12253 region.op(r, Region.Op.INTERSECT);
12254 } else {
12255 region.op(db, Region.Op.DIFFERENCE);
12256 }
12257 }
12258
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012259 private void checkForLongClick(int delayOffset) {
12260 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12261 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012262
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012263 if (mPendingCheckForLongPress == null) {
12264 mPendingCheckForLongPress = new CheckForLongPress();
12265 }
12266 mPendingCheckForLongPress.rememberWindowAttachCount();
12267 postDelayed(mPendingCheckForLongPress,
12268 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012270 }
12271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 /**
12273 * Inflate a view from an XML resource. This convenience method wraps the {@link
12274 * LayoutInflater} class, which provides a full range of options for view inflation.
12275 *
12276 * @param context The Context object for your activity or application.
12277 * @param resource The resource ID to inflate
12278 * @param root A view group that will be the parent. Used to properly inflate the
12279 * layout_* parameters.
12280 * @see LayoutInflater
12281 */
12282 public static View inflate(Context context, int resource, ViewGroup root) {
12283 LayoutInflater factory = LayoutInflater.from(context);
12284 return factory.inflate(resource, root);
12285 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287 /**
Adam Powell637d3372010-08-25 14:37:03 -070012288 * Scroll the view with standard behavior for scrolling beyond the normal
12289 * content boundaries. Views that call this method should override
12290 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12291 * results of an over-scroll operation.
12292 *
12293 * Views can use this method to handle any touch or fling-based scrolling.
12294 *
12295 * @param deltaX Change in X in pixels
12296 * @param deltaY Change in Y in pixels
12297 * @param scrollX Current X scroll value in pixels before applying deltaX
12298 * @param scrollY Current Y scroll value in pixels before applying deltaY
12299 * @param scrollRangeX Maximum content scroll range along the X axis
12300 * @param scrollRangeY Maximum content scroll range along the Y axis
12301 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12302 * along the X axis.
12303 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12304 * along the Y axis.
12305 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12306 * @return true if scrolling was clamped to an over-scroll boundary along either
12307 * axis, false otherwise.
12308 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012309 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012310 protected boolean overScrollBy(int deltaX, int deltaY,
12311 int scrollX, int scrollY,
12312 int scrollRangeX, int scrollRangeY,
12313 int maxOverScrollX, int maxOverScrollY,
12314 boolean isTouchEvent) {
12315 final int overScrollMode = mOverScrollMode;
12316 final boolean canScrollHorizontal =
12317 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12318 final boolean canScrollVertical =
12319 computeVerticalScrollRange() > computeVerticalScrollExtent();
12320 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12321 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12322 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12323 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12324
12325 int newScrollX = scrollX + deltaX;
12326 if (!overScrollHorizontal) {
12327 maxOverScrollX = 0;
12328 }
12329
12330 int newScrollY = scrollY + deltaY;
12331 if (!overScrollVertical) {
12332 maxOverScrollY = 0;
12333 }
12334
12335 // Clamp values if at the limits and record
12336 final int left = -maxOverScrollX;
12337 final int right = maxOverScrollX + scrollRangeX;
12338 final int top = -maxOverScrollY;
12339 final int bottom = maxOverScrollY + scrollRangeY;
12340
12341 boolean clampedX = false;
12342 if (newScrollX > right) {
12343 newScrollX = right;
12344 clampedX = true;
12345 } else if (newScrollX < left) {
12346 newScrollX = left;
12347 clampedX = true;
12348 }
12349
12350 boolean clampedY = false;
12351 if (newScrollY > bottom) {
12352 newScrollY = bottom;
12353 clampedY = true;
12354 } else if (newScrollY < top) {
12355 newScrollY = top;
12356 clampedY = true;
12357 }
12358
12359 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12360
12361 return clampedX || clampedY;
12362 }
12363
12364 /**
12365 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12366 * respond to the results of an over-scroll operation.
12367 *
12368 * @param scrollX New X scroll value in pixels
12369 * @param scrollY New Y scroll value in pixels
12370 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12371 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12372 */
12373 protected void onOverScrolled(int scrollX, int scrollY,
12374 boolean clampedX, boolean clampedY) {
12375 // Intentionally empty.
12376 }
12377
12378 /**
12379 * Returns the over-scroll mode for this view. The result will be
12380 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12381 * (allow over-scrolling only if the view content is larger than the container),
12382 * or {@link #OVER_SCROLL_NEVER}.
12383 *
12384 * @return This view's over-scroll mode.
12385 */
12386 public int getOverScrollMode() {
12387 return mOverScrollMode;
12388 }
12389
12390 /**
12391 * Set the over-scroll mode for this view. Valid over-scroll modes are
12392 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12393 * (allow over-scrolling only if the view content is larger than the container),
12394 * or {@link #OVER_SCROLL_NEVER}.
12395 *
12396 * Setting the over-scroll mode of a view will have an effect only if the
12397 * view is capable of scrolling.
12398 *
12399 * @param overScrollMode The new over-scroll mode for this view.
12400 */
12401 public void setOverScrollMode(int overScrollMode) {
12402 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12403 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12404 overScrollMode != OVER_SCROLL_NEVER) {
12405 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12406 }
12407 mOverScrollMode = overScrollMode;
12408 }
12409
12410 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012411 * Gets a scale factor that determines the distance the view should scroll
12412 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12413 * @return The vertical scroll scale factor.
12414 * @hide
12415 */
12416 protected float getVerticalScrollFactor() {
12417 if (mVerticalScrollFactor == 0) {
12418 TypedValue outValue = new TypedValue();
12419 if (!mContext.getTheme().resolveAttribute(
12420 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12421 throw new IllegalStateException(
12422 "Expected theme to define listPreferredItemHeight.");
12423 }
12424 mVerticalScrollFactor = outValue.getDimension(
12425 mContext.getResources().getDisplayMetrics());
12426 }
12427 return mVerticalScrollFactor;
12428 }
12429
12430 /**
12431 * Gets a scale factor that determines the distance the view should scroll
12432 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12433 * @return The horizontal scroll scale factor.
12434 * @hide
12435 */
12436 protected float getHorizontalScrollFactor() {
12437 // TODO: Should use something else.
12438 return getVerticalScrollFactor();
12439 }
12440
Chet Haaseb39f0512011-05-24 14:36:40 -070012441 //
12442 // Properties
12443 //
12444 /**
12445 * A Property wrapper around the <code>alpha</code> functionality handled by the
12446 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12447 */
12448 static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
12449 @Override
12450 public void setValue(View object, float value) {
12451 object.setAlpha(value);
12452 }
12453
12454 @Override
12455 public Float get(View object) {
12456 return object.getAlpha();
12457 }
12458 };
12459
12460 /**
12461 * A Property wrapper around the <code>translationX</code> functionality handled by the
12462 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12463 */
12464 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12465 @Override
12466 public void setValue(View object, float value) {
12467 object.setTranslationX(value);
12468 }
12469
12470 @Override
12471 public Float get(View object) {
12472 return object.getTranslationX();
12473 }
12474 };
12475
12476 /**
12477 * A Property wrapper around the <code>translationY</code> functionality handled by the
12478 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12479 */
12480 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12481 @Override
12482 public void setValue(View object, float value) {
12483 object.setTranslationY(value);
12484 }
12485
12486 @Override
12487 public Float get(View object) {
12488 return object.getTranslationY();
12489 }
12490 };
12491
12492 /**
12493 * A Property wrapper around the <code>x</code> functionality handled by the
12494 * {@link View#setX(float)} and {@link View#getX()} methods.
12495 */
12496 public static Property<View, Float> X = new FloatProperty<View>("x") {
12497 @Override
12498 public void setValue(View object, float value) {
12499 object.setX(value);
12500 }
12501
12502 @Override
12503 public Float get(View object) {
12504 return object.getX();
12505 }
12506 };
12507
12508 /**
12509 * A Property wrapper around the <code>y</code> functionality handled by the
12510 * {@link View#setY(float)} and {@link View#getY()} methods.
12511 */
12512 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12513 @Override
12514 public void setValue(View object, float value) {
12515 object.setY(value);
12516 }
12517
12518 @Override
12519 public Float get(View object) {
12520 return object.getY();
12521 }
12522 };
12523
12524 /**
12525 * A Property wrapper around the <code>rotation</code> functionality handled by the
12526 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12527 */
12528 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12529 @Override
12530 public void setValue(View object, float value) {
12531 object.setRotation(value);
12532 }
12533
12534 @Override
12535 public Float get(View object) {
12536 return object.getRotation();
12537 }
12538 };
12539
12540 /**
12541 * A Property wrapper around the <code>rotationX</code> functionality handled by the
12542 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
12543 */
12544 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
12545 @Override
12546 public void setValue(View object, float value) {
12547 object.setRotationX(value);
12548 }
12549
12550 @Override
12551 public Float get(View object) {
12552 return object.getRotationX();
12553 }
12554 };
12555
12556 /**
12557 * A Property wrapper around the <code>rotationY</code> functionality handled by the
12558 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
12559 */
12560 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
12561 @Override
12562 public void setValue(View object, float value) {
12563 object.setRotationY(value);
12564 }
12565
12566 @Override
12567 public Float get(View object) {
12568 return object.getRotationY();
12569 }
12570 };
12571
12572 /**
12573 * A Property wrapper around the <code>scaleX</code> functionality handled by the
12574 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
12575 */
12576 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
12577 @Override
12578 public void setValue(View object, float value) {
12579 object.setScaleX(value);
12580 }
12581
12582 @Override
12583 public Float get(View object) {
12584 return object.getScaleX();
12585 }
12586 };
12587
12588 /**
12589 * A Property wrapper around the <code>scaleY</code> functionality handled by the
12590 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
12591 */
12592 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
12593 @Override
12594 public void setValue(View object, float value) {
12595 object.setScaleY(value);
12596 }
12597
12598 @Override
12599 public Float get(View object) {
12600 return object.getScaleY();
12601 }
12602 };
12603
Jeff Brown33bbfd22011-02-24 20:55:35 -080012604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012605 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12606 * Each MeasureSpec represents a requirement for either the width or the height.
12607 * A MeasureSpec is comprised of a size and a mode. There are three possible
12608 * modes:
12609 * <dl>
12610 * <dt>UNSPECIFIED</dt>
12611 * <dd>
12612 * The parent has not imposed any constraint on the child. It can be whatever size
12613 * it wants.
12614 * </dd>
12615 *
12616 * <dt>EXACTLY</dt>
12617 * <dd>
12618 * The parent has determined an exact size for the child. The child is going to be
12619 * given those bounds regardless of how big it wants to be.
12620 * </dd>
12621 *
12622 * <dt>AT_MOST</dt>
12623 * <dd>
12624 * The child can be as large as it wants up to the specified size.
12625 * </dd>
12626 * </dl>
12627 *
12628 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12629 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12630 */
12631 public static class MeasureSpec {
12632 private static final int MODE_SHIFT = 30;
12633 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12634
12635 /**
12636 * Measure specification mode: The parent has not imposed any constraint
12637 * on the child. It can be whatever size it wants.
12638 */
12639 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12640
12641 /**
12642 * Measure specification mode: The parent has determined an exact size
12643 * for the child. The child is going to be given those bounds regardless
12644 * of how big it wants to be.
12645 */
12646 public static final int EXACTLY = 1 << MODE_SHIFT;
12647
12648 /**
12649 * Measure specification mode: The child can be as large as it wants up
12650 * to the specified size.
12651 */
12652 public static final int AT_MOST = 2 << MODE_SHIFT;
12653
12654 /**
12655 * Creates a measure specification based on the supplied size and mode.
12656 *
12657 * The mode must always be one of the following:
12658 * <ul>
12659 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12660 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12661 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12662 * </ul>
12663 *
12664 * @param size the size of the measure specification
12665 * @param mode the mode of the measure specification
12666 * @return the measure specification based on size and mode
12667 */
12668 public static int makeMeasureSpec(int size, int mode) {
12669 return size + mode;
12670 }
12671
12672 /**
12673 * Extracts the mode from the supplied measure specification.
12674 *
12675 * @param measureSpec the measure specification to extract the mode from
12676 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12677 * {@link android.view.View.MeasureSpec#AT_MOST} or
12678 * {@link android.view.View.MeasureSpec#EXACTLY}
12679 */
12680 public static int getMode(int measureSpec) {
12681 return (measureSpec & MODE_MASK);
12682 }
12683
12684 /**
12685 * Extracts the size from the supplied measure specification.
12686 *
12687 * @param measureSpec the measure specification to extract the size from
12688 * @return the size in pixels defined in the supplied measure specification
12689 */
12690 public static int getSize(int measureSpec) {
12691 return (measureSpec & ~MODE_MASK);
12692 }
12693
12694 /**
12695 * Returns a String representation of the specified measure
12696 * specification.
12697 *
12698 * @param measureSpec the measure specification to convert to a String
12699 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12700 */
12701 public static String toString(int measureSpec) {
12702 int mode = getMode(measureSpec);
12703 int size = getSize(measureSpec);
12704
12705 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12706
12707 if (mode == UNSPECIFIED)
12708 sb.append("UNSPECIFIED ");
12709 else if (mode == EXACTLY)
12710 sb.append("EXACTLY ");
12711 else if (mode == AT_MOST)
12712 sb.append("AT_MOST ");
12713 else
12714 sb.append(mode).append(" ");
12715
12716 sb.append(size);
12717 return sb.toString();
12718 }
12719 }
12720
12721 class CheckForLongPress implements Runnable {
12722
12723 private int mOriginalWindowAttachCount;
12724
12725 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012726 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012727 && mOriginalWindowAttachCount == mWindowAttachCount) {
12728 if (performLongClick()) {
12729 mHasPerformedLongPress = true;
12730 }
12731 }
12732 }
12733
12734 public void rememberWindowAttachCount() {
12735 mOriginalWindowAttachCount = mWindowAttachCount;
12736 }
12737 }
Joe Malin32736f02011-01-19 16:14:20 -080012738
Adam Powelle14579b2009-12-16 18:39:52 -080012739 private final class CheckForTap implements Runnable {
12740 public void run() {
12741 mPrivateFlags &= ~PREPRESSED;
12742 mPrivateFlags |= PRESSED;
12743 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012744 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012745 }
12746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012747
Adam Powella35d7682010-03-12 14:48:13 -080012748 private final class PerformClick implements Runnable {
12749 public void run() {
12750 performClick();
12751 }
12752 }
12753
Dianne Hackborn63042d62011-01-26 18:56:29 -080012754 /** @hide */
12755 public void hackTurnOffWindowResizeAnim(boolean off) {
12756 mAttachInfo.mTurnOffWindowResizeAnim = off;
12757 }
Joe Malin32736f02011-01-19 16:14:20 -080012758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012759 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012760 * This method returns a ViewPropertyAnimator object, which can be used to animate
12761 * specific properties on this View.
12762 *
12763 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12764 */
12765 public ViewPropertyAnimator animate() {
12766 if (mAnimator == null) {
12767 mAnimator = new ViewPropertyAnimator(this);
12768 }
12769 return mAnimator;
12770 }
12771
12772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012773 * Interface definition for a callback to be invoked when a key event is
12774 * dispatched to this view. The callback will be invoked before the key
12775 * event is given to the view.
12776 */
12777 public interface OnKeyListener {
12778 /**
12779 * Called when a key is dispatched to a view. This allows listeners to
12780 * get a chance to respond before the target view.
12781 *
12782 * @param v The view the key has been dispatched to.
12783 * @param keyCode The code for the physical key that was pressed
12784 * @param event The KeyEvent object containing full information about
12785 * the event.
12786 * @return True if the listener has consumed the event, false otherwise.
12787 */
12788 boolean onKey(View v, int keyCode, KeyEvent event);
12789 }
12790
12791 /**
12792 * Interface definition for a callback to be invoked when a touch event is
12793 * dispatched to this view. The callback will be invoked before the touch
12794 * event is given to the view.
12795 */
12796 public interface OnTouchListener {
12797 /**
12798 * Called when a touch event is dispatched to a view. This allows listeners to
12799 * get a chance to respond before the target view.
12800 *
12801 * @param v The view the touch event has been dispatched to.
12802 * @param event The MotionEvent object containing full information about
12803 * the event.
12804 * @return True if the listener has consumed the event, false otherwise.
12805 */
12806 boolean onTouch(View v, MotionEvent event);
12807 }
12808
12809 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012810 * Interface definition for a callback to be invoked when a generic motion event is
12811 * dispatched to this view. The callback will be invoked before the generic motion
12812 * event is given to the view.
12813 */
12814 public interface OnGenericMotionListener {
12815 /**
12816 * Called when a generic motion event is dispatched to a view. This allows listeners to
12817 * get a chance to respond before the target view.
12818 *
12819 * @param v The view the generic motion event has been dispatched to.
12820 * @param event The MotionEvent object containing full information about
12821 * the event.
12822 * @return True if the listener has consumed the event, false otherwise.
12823 */
12824 boolean onGenericMotion(View v, MotionEvent event);
12825 }
12826
12827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 * Interface definition for a callback to be invoked when a view has been clicked and held.
12829 */
12830 public interface OnLongClickListener {
12831 /**
12832 * Called when a view has been clicked and held.
12833 *
12834 * @param v The view that was clicked and held.
12835 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012836 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012837 */
12838 boolean onLongClick(View v);
12839 }
12840
12841 /**
Chris Tate32affef2010-10-18 15:29:21 -070012842 * Interface definition for a callback to be invoked when a drag is being dispatched
12843 * to this view. The callback will be invoked before the hosting view's own
12844 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12845 * onDrag(event) behavior, it should return 'false' from this callback.
12846 */
12847 public interface OnDragListener {
12848 /**
12849 * Called when a drag event is dispatched to a view. This allows listeners
12850 * to get a chance to override base View behavior.
12851 *
Joe Malin32736f02011-01-19 16:14:20 -080012852 * @param v The View that received the drag event.
12853 * @param event The {@link android.view.DragEvent} object for the drag event.
12854 * @return {@code true} if the drag event was handled successfully, or {@code false}
12855 * if the drag event was not handled. Note that {@code false} will trigger the View
12856 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012857 */
12858 boolean onDrag(View v, DragEvent event);
12859 }
12860
12861 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012862 * Interface definition for a callback to be invoked when the focus state of
12863 * a view changed.
12864 */
12865 public interface OnFocusChangeListener {
12866 /**
12867 * Called when the focus state of a view has changed.
12868 *
12869 * @param v The view whose state has changed.
12870 * @param hasFocus The new focus state of v.
12871 */
12872 void onFocusChange(View v, boolean hasFocus);
12873 }
12874
12875 /**
12876 * Interface definition for a callback to be invoked when a view is clicked.
12877 */
12878 public interface OnClickListener {
12879 /**
12880 * Called when a view has been clicked.
12881 *
12882 * @param v The view that was clicked.
12883 */
12884 void onClick(View v);
12885 }
12886
12887 /**
12888 * Interface definition for a callback to be invoked when the context menu
12889 * for this view is being built.
12890 */
12891 public interface OnCreateContextMenuListener {
12892 /**
12893 * Called when the context menu for this view is being built. It is not
12894 * safe to hold onto the menu after this method returns.
12895 *
12896 * @param menu The context menu that is being built
12897 * @param v The view for which the context menu is being built
12898 * @param menuInfo Extra information about the item for which the
12899 * context menu should be shown. This information will vary
12900 * depending on the class of v.
12901 */
12902 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12903 }
12904
Joe Onorato664644d2011-01-23 17:53:23 -080012905 /**
12906 * Interface definition for a callback to be invoked when the status bar changes
12907 * visibility.
12908 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012909 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012910 */
12911 public interface OnSystemUiVisibilityChangeListener {
12912 /**
12913 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070012914 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080012915 *
12916 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12917 */
12918 public void onSystemUiVisibilityChange(int visibility);
12919 }
12920
Adam Powell4afd62b2011-02-18 15:02:18 -080012921 /**
12922 * Interface definition for a callback to be invoked when this view is attached
12923 * or detached from its window.
12924 */
12925 public interface OnAttachStateChangeListener {
12926 /**
12927 * Called when the view is attached to a window.
12928 * @param v The view that was attached
12929 */
12930 public void onViewAttachedToWindow(View v);
12931 /**
12932 * Called when the view is detached from a window.
12933 * @param v The view that was detached
12934 */
12935 public void onViewDetachedFromWindow(View v);
12936 }
12937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012938 private final class UnsetPressedState implements Runnable {
12939 public void run() {
12940 setPressed(false);
12941 }
12942 }
12943
12944 /**
12945 * Base class for derived classes that want to save and restore their own
12946 * state in {@link android.view.View#onSaveInstanceState()}.
12947 */
12948 public static class BaseSavedState extends AbsSavedState {
12949 /**
12950 * Constructor used when reading from a parcel. Reads the state of the superclass.
12951 *
12952 * @param source
12953 */
12954 public BaseSavedState(Parcel source) {
12955 super(source);
12956 }
12957
12958 /**
12959 * Constructor called by derived classes when creating their SavedState objects
12960 *
12961 * @param superState The state of the superclass of this view
12962 */
12963 public BaseSavedState(Parcelable superState) {
12964 super(superState);
12965 }
12966
12967 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12968 new Parcelable.Creator<BaseSavedState>() {
12969 public BaseSavedState createFromParcel(Parcel in) {
12970 return new BaseSavedState(in);
12971 }
12972
12973 public BaseSavedState[] newArray(int size) {
12974 return new BaseSavedState[size];
12975 }
12976 };
12977 }
12978
12979 /**
12980 * A set of information given to a view when it is attached to its parent
12981 * window.
12982 */
12983 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012984 interface Callbacks {
12985 void playSoundEffect(int effectId);
12986 boolean performHapticFeedback(int effectId, boolean always);
12987 }
12988
12989 /**
12990 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12991 * to a Handler. This class contains the target (View) to invalidate and
12992 * the coordinates of the dirty rectangle.
12993 *
12994 * For performance purposes, this class also implements a pool of up to
12995 * POOL_LIMIT objects that get reused. This reduces memory allocations
12996 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012997 */
Romain Guyd928d682009-03-31 17:52:16 -070012998 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012999 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013000 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13001 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013002 public InvalidateInfo newInstance() {
13003 return new InvalidateInfo();
13004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013005
Romain Guyd928d682009-03-31 17:52:16 -070013006 public void onAcquired(InvalidateInfo element) {
13007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013008
Romain Guyd928d682009-03-31 17:52:16 -070013009 public void onReleased(InvalidateInfo element) {
13010 }
13011 }, POOL_LIMIT)
13012 );
13013
13014 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013015 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013016
13017 View target;
13018
13019 int left;
13020 int top;
13021 int right;
13022 int bottom;
13023
Romain Guyd928d682009-03-31 17:52:16 -070013024 public void setNextPoolable(InvalidateInfo element) {
13025 mNext = element;
13026 }
13027
13028 public InvalidateInfo getNextPoolable() {
13029 return mNext;
13030 }
13031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013032 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013033 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013034 }
13035
13036 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013037 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013038 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013039
13040 public boolean isPooled() {
13041 return mIsPooled;
13042 }
13043
13044 public void setPooled(boolean isPooled) {
13045 mIsPooled = isPooled;
13046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013047 }
13048
13049 final IWindowSession mSession;
13050
13051 final IWindow mWindow;
13052
13053 final IBinder mWindowToken;
13054
13055 final Callbacks mRootCallbacks;
13056
Romain Guy59a12ca2011-06-09 17:48:21 -070013057 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013059 /**
13060 * The top view of the hierarchy.
13061 */
13062 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013064 IBinder mPanelParentWindowToken;
13065 Surface mSurface;
13066
Romain Guyb051e892010-09-28 19:09:36 -070013067 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013068 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013069 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013072 * Scale factor used by the compatibility mode
13073 */
13074 float mApplicationScale;
13075
13076 /**
13077 * Indicates whether the application is in compatibility mode
13078 */
13079 boolean mScalingRequired;
13080
13081 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013082 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013083 */
13084 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013085
Dianne Hackborn63042d62011-01-26 18:56:29 -080013086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013087 * Left position of this view's window
13088 */
13089 int mWindowLeft;
13090
13091 /**
13092 * Top position of this view's window
13093 */
13094 int mWindowTop;
13095
13096 /**
Adam Powell26153a32010-11-08 15:22:27 -080013097 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013098 */
Adam Powell26153a32010-11-08 15:22:27 -080013099 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013100
13101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013102 * For windows that are full-screen but using insets to layout inside
13103 * of the screen decorations, these are the current insets for the
13104 * content of the window.
13105 */
13106 final Rect mContentInsets = new Rect();
13107
13108 /**
13109 * For windows that are full-screen but using insets to layout inside
13110 * of the screen decorations, these are the current insets for the
13111 * actual visible parts of the window.
13112 */
13113 final Rect mVisibleInsets = new Rect();
13114
13115 /**
13116 * The internal insets given by this window. This value is
13117 * supplied by the client (through
13118 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13119 * be given to the window manager when changed to be used in laying
13120 * out windows behind it.
13121 */
13122 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13123 = new ViewTreeObserver.InternalInsetsInfo();
13124
13125 /**
13126 * All views in the window's hierarchy that serve as scroll containers,
13127 * used to determine if the window can be resized or must be panned
13128 * to adjust for a soft input area.
13129 */
13130 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13131
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013132 final KeyEvent.DispatcherState mKeyDispatchState
13133 = new KeyEvent.DispatcherState();
13134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013135 /**
13136 * Indicates whether the view's window currently has the focus.
13137 */
13138 boolean mHasWindowFocus;
13139
13140 /**
13141 * The current visibility of the window.
13142 */
13143 int mWindowVisibility;
13144
13145 /**
13146 * Indicates the time at which drawing started to occur.
13147 */
13148 long mDrawingTime;
13149
13150 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013151 * Indicates whether or not ignoring the DIRTY_MASK flags.
13152 */
13153 boolean mIgnoreDirtyState;
13154
13155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013156 * Indicates whether the view's window is currently in touch mode.
13157 */
13158 boolean mInTouchMode;
13159
13160 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013161 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013162 * the next time it performs a traversal
13163 */
13164 boolean mRecomputeGlobalAttributes;
13165
13166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013167 * Set during a traveral if any views want to keep the screen on.
13168 */
13169 boolean mKeepScreenOn;
13170
13171 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013172 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13173 */
13174 int mSystemUiVisibility;
13175
13176 /**
13177 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13178 * attached.
13179 */
13180 boolean mHasSystemUiListeners;
13181
13182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013183 * Set if the visibility of any views has changed.
13184 */
13185 boolean mViewVisibilityChanged;
13186
13187 /**
13188 * Set to true if a view has been scrolled.
13189 */
13190 boolean mViewScrollChanged;
13191
13192 /**
13193 * Global to the view hierarchy used as a temporary for dealing with
13194 * x/y points in the transparent region computations.
13195 */
13196 final int[] mTransparentLocation = new int[2];
13197
13198 /**
13199 * Global to the view hierarchy used as a temporary for dealing with
13200 * x/y points in the ViewGroup.invalidateChild implementation.
13201 */
13202 final int[] mInvalidateChildLocation = new int[2];
13203
Chet Haasec3aa3612010-06-17 08:50:37 -070013204
13205 /**
13206 * Global to the view hierarchy used as a temporary for dealing with
13207 * x/y location when view is transformed.
13208 */
13209 final float[] mTmpTransformLocation = new float[2];
13210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013211 /**
13212 * The view tree observer used to dispatch global events like
13213 * layout, pre-draw, touch mode change, etc.
13214 */
13215 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13216
13217 /**
13218 * A Canvas used by the view hierarchy to perform bitmap caching.
13219 */
13220 Canvas mCanvas;
13221
13222 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013223 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013224 * handler can be used to pump events in the UI events queue.
13225 */
13226 final Handler mHandler;
13227
13228 /**
13229 * Identifier for messages requesting the view to be invalidated.
13230 * Such messages should be sent to {@link #mHandler}.
13231 */
13232 static final int INVALIDATE_MSG = 0x1;
13233
13234 /**
13235 * Identifier for messages requesting the view to invalidate a region.
13236 * Such messages should be sent to {@link #mHandler}.
13237 */
13238 static final int INVALIDATE_RECT_MSG = 0x2;
13239
13240 /**
13241 * Temporary for use in computing invalidate rectangles while
13242 * calling up the hierarchy.
13243 */
13244 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013245
13246 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013247 * Temporary for use in computing hit areas with transformed views
13248 */
13249 final RectF mTmpTransformRect = new RectF();
13250
13251 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013252 * Temporary list for use in collecting focusable descendents of a view.
13253 */
13254 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013256 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013257 * The id of the window for accessibility purposes.
13258 */
13259 int mAccessibilityWindowId = View.NO_ID;
13260
13261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013262 * Creates a new set of attachment information with the specified
13263 * events handler and thread.
13264 *
13265 * @param handler the events handler the view must use
13266 */
13267 AttachInfo(IWindowSession session, IWindow window,
13268 Handler handler, Callbacks effectPlayer) {
13269 mSession = session;
13270 mWindow = window;
13271 mWindowToken = window.asBinder();
13272 mHandler = handler;
13273 mRootCallbacks = effectPlayer;
13274 }
13275 }
13276
13277 /**
13278 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13279 * is supported. This avoids keeping too many unused fields in most
13280 * instances of View.</p>
13281 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013282 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013283
Mike Cleronf116bf82009-09-27 19:14:12 -070013284 /**
13285 * Scrollbars are not visible
13286 */
13287 public static final int OFF = 0;
13288
13289 /**
13290 * Scrollbars are visible
13291 */
13292 public static final int ON = 1;
13293
13294 /**
13295 * Scrollbars are fading away
13296 */
13297 public static final int FADING = 2;
13298
13299 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013301 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013302 public int scrollBarDefaultDelayBeforeFade;
13303 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013304
13305 public int scrollBarSize;
13306 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013307 public float[] interpolatorValues;
13308 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013309
13310 public final Paint paint;
13311 public final Matrix matrix;
13312 public Shader shader;
13313
Mike Cleronf116bf82009-09-27 19:14:12 -070013314 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13315
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013316 private static final float[] OPAQUE = { 255 };
13317 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013318
Mike Cleronf116bf82009-09-27 19:14:12 -070013319 /**
13320 * When fading should start. This time moves into the future every time
13321 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13322 */
13323 public long fadeStartTime;
13324
13325
13326 /**
13327 * The current state of the scrollbars: ON, OFF, or FADING
13328 */
13329 public int state = OFF;
13330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013331 private int mLastColor;
13332
Mike Cleronf116bf82009-09-27 19:14:12 -070013333 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013334 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13335 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013336 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13337 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338
13339 paint = new Paint();
13340 matrix = new Matrix();
13341 // use use a height of 1, and then wack the matrix each time we
13342 // actually use it.
13343 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013345 paint.setShader(shader);
13346 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013347 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013348 }
Romain Guy8506ab42009-06-11 17:35:47 -070013349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013350 public void setFadeColor(int color) {
13351 if (color != 0 && color != mLastColor) {
13352 mLastColor = color;
13353 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013354
Romain Guye55e1a72009-08-27 10:42:26 -070013355 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13356 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013358 paint.setShader(shader);
13359 // Restore the default transfer mode (src_over)
13360 paint.setXfermode(null);
13361 }
13362 }
Joe Malin32736f02011-01-19 16:14:20 -080013363
Mike Cleronf116bf82009-09-27 19:14:12 -070013364 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013365 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013366 if (now >= fadeStartTime) {
13367
13368 // the animation fades the scrollbars out by changing
13369 // the opacity (alpha) from fully opaque to fully
13370 // transparent
13371 int nextFrame = (int) now;
13372 int framesCount = 0;
13373
13374 Interpolator interpolator = scrollBarInterpolator;
13375
13376 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013377 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013378
13379 // End transparent
13380 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013381 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013382
13383 state = FADING;
13384
13385 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013386 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013387 }
13388 }
13389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013390 }
13391}