blob: dc46d424450f6cde390feffce8a8d4ddeaee7859 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
43import android.os.Message;
44import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070050import android.util.FloatProperty;
51import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070053import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070064import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Romain Guy1ef3fdb2011-09-09 15:30:30 -070072import static android.os.Build.VERSION_CODES.*;
73
Doug Feltcb3791202011-07-07 11:57:48 -070074import com.android.internal.R;
75import com.android.internal.util.Predicate;
76import com.android.internal.view.menu.MenuBuilder;
77
Christopher Tatea0374192010-10-05 13:06:41 -070078import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070079import java.lang.reflect.InvocationTargetException;
80import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.ArrayList;
82import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070083import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080084import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86/**
87 * <p>
88 * This class represents the basic building block for user interface components. A View
89 * occupies a rectangular area on the screen and is responsible for drawing and
90 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070091 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
93 * are invisible containers that hold other Views (or other ViewGroups) and define
94 * their layout properties.
95 * </p>
96 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070097 * <div class="special reference">
98 * <h3>Developer Guides</h3>
99 * <p>For information about using this class to develop your application's user interface,
100 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * <a name="Using"></a>
104 * <h3>Using Views</h3>
105 * <p>
106 * All of the views in a window are arranged in a single tree. You can add views
107 * either from code or by specifying a tree of views in one or more XML layout
108 * files. There are many specialized subclasses of views that act as controls or
109 * are capable of displaying text, images, or other content.
110 * </p>
111 * <p>
112 * Once you have created a tree of views, there are typically a few types of
113 * common operations you may wish to perform:
114 * <ul>
115 * <li><strong>Set properties:</strong> for example setting the text of a
116 * {@link android.widget.TextView}. The available properties and the methods
117 * that set them will vary among the different subclasses of views. Note that
118 * properties that are known at build time can be set in the XML layout
119 * files.</li>
120 * <li><strong>Set focus:</strong> The framework will handled moving focus in
121 * response to user input. To force focus to a specific view, call
122 * {@link #requestFocus}.</li>
123 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
124 * that will be notified when something interesting happens to the view. For
125 * example, all views will let you set a listener to be notified when the view
126 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700127 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
128 * Other view subclasses offer more specialized listeners. For example, a Button
129 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * </ul>
133 * </p>
134 * <p><em>
135 * Note: The Android framework is responsible for measuring, laying out and
136 * drawing views. You should not call methods that perform these actions on
137 * views yourself unless you are actually implementing a
138 * {@link android.view.ViewGroup}.
139 * </em></p>
140 *
141 * <a name="Lifecycle"></a>
142 * <h3>Implementing a Custom View</h3>
143 *
144 * <p>
145 * To implement a custom view, you will usually begin by providing overrides for
146 * some of the standard methods that the framework calls on all views. You do
147 * not need to override all of these methods. In fact, you can start by just
148 * overriding {@link #onDraw(android.graphics.Canvas)}.
149 * <table border="2" width="85%" align="center" cellpadding="5">
150 * <thead>
151 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
152 * </thead>
153 *
154 * <tbody>
155 * <tr>
156 * <td rowspan="2">Creation</td>
157 * <td>Constructors</td>
158 * <td>There is a form of the constructor that are called when the view
159 * is created from code and a form that is called when the view is
160 * inflated from a layout file. The second form should parse and apply
161 * any attributes defined in the layout file.
162 * </td>
163 * </tr>
164 * <tr>
165 * <td><code>{@link #onFinishInflate()}</code></td>
166 * <td>Called after a view and all of its children has been inflated
167 * from XML.</td>
168 * </tr>
169 *
170 * <tr>
171 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700172 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <td>Called to determine the size requirements for this view and all
174 * of its children.
175 * </td>
176 * </tr>
177 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700178 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * <td>Called when this view should assign a size and position to all
180 * of its children.
181 * </td>
182 * </tr>
183 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700184 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * <td>Called when the size of this view has changed.
186 * </td>
187 * </tr>
188 *
189 * <tr>
190 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700191 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <td>Called when the view should render its content.
193 * </td>
194 * </tr>
195 *
196 * <tr>
197 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700198 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * <td>Called when a new key event occurs.
200 * </td>
201 * </tr>
202 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700203 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <td>Called when a key up event occurs.
205 * </td>
206 * </tr>
207 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a trackball motion event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a touch screen motion event occurs.
215 * </td>
216 * </tr>
217 *
218 * <tr>
219 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700220 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * <td>Called when the view gains or loses focus.
222 * </td>
223 * </tr>
224 *
225 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700226 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * <td>Called when the window containing the view gains or loses focus.
228 * </td>
229 * </tr>
230 *
231 * <tr>
232 * <td rowspan="3">Attaching</td>
233 * <td><code>{@link #onAttachedToWindow()}</code></td>
234 * <td>Called when the view is attached to a window.
235 * </td>
236 * </tr>
237 *
238 * <tr>
239 * <td><code>{@link #onDetachedFromWindow}</code></td>
240 * <td>Called when the view is detached from its window.
241 * </td>
242 * </tr>
243 *
244 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700245 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <td>Called when the visibility of the window containing the view
247 * has changed.
248 * </td>
249 * </tr>
250 * </tbody>
251 *
252 * </table>
253 * </p>
254 *
255 * <a name="IDs"></a>
256 * <h3>IDs</h3>
257 * Views may have an integer id associated with them. These ids are typically
258 * assigned in the layout XML files, and are used to find specific views within
259 * the view tree. A common pattern is to:
260 * <ul>
261 * <li>Define a Button in the layout file and assign it a unique ID.
262 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700263 * &lt;Button
264 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 * android:layout_width="wrap_content"
266 * android:layout_height="wrap_content"
267 * android:text="@string/my_button_text"/&gt;
268 * </pre></li>
269 * <li>From the onCreate method of an Activity, find the Button
270 * <pre class="prettyprint">
271 * Button myButton = (Button) findViewById(R.id.my_button);
272 * </pre></li>
273 * </ul>
274 * <p>
275 * View IDs need not be unique throughout the tree, but it is good practice to
276 * ensure that they are at least unique within the part of the tree you are
277 * searching.
278 * </p>
279 *
280 * <a name="Position"></a>
281 * <h3>Position</h3>
282 * <p>
283 * The geometry of a view is that of a rectangle. A view has a location,
284 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
285 * two dimensions, expressed as a width and a height. The unit for location
286 * and dimensions is the pixel.
287 * </p>
288 *
289 * <p>
290 * It is possible to retrieve the location of a view by invoking the methods
291 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
292 * coordinate of the rectangle representing the view. The latter returns the
293 * top, or Y, coordinate of the rectangle representing the view. These methods
294 * both return the location of the view relative to its parent. For instance,
295 * when getLeft() returns 20, that means the view is located 20 pixels to the
296 * right of the left edge of its direct parent.
297 * </p>
298 *
299 * <p>
300 * In addition, several convenience methods are offered to avoid unnecessary
301 * computations, namely {@link #getRight()} and {@link #getBottom()}.
302 * These methods return the coordinates of the right and bottom edges of the
303 * rectangle representing the view. For instance, calling {@link #getRight()}
304 * is similar to the following computation: <code>getLeft() + getWidth()</code>
305 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
306 * </p>
307 *
308 * <a name="SizePaddingMargins"></a>
309 * <h3>Size, padding and margins</h3>
310 * <p>
311 * The size of a view is expressed with a width and a height. A view actually
312 * possess two pairs of width and height values.
313 * </p>
314 *
315 * <p>
316 * The first pair is known as <em>measured width</em> and
317 * <em>measured height</em>. These dimensions define how big a view wants to be
318 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
319 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
320 * and {@link #getMeasuredHeight()}.
321 * </p>
322 *
323 * <p>
324 * The second pair is simply known as <em>width</em> and <em>height</em>, or
325 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
326 * dimensions define the actual size of the view on screen, at drawing time and
327 * after layout. These values may, but do not have to, be different from the
328 * measured width and height. The width and height can be obtained by calling
329 * {@link #getWidth()} and {@link #getHeight()}.
330 * </p>
331 *
332 * <p>
333 * To measure its dimensions, a view takes into account its padding. The padding
334 * is expressed in pixels for the left, top, right and bottom parts of the view.
335 * Padding can be used to offset the content of the view by a specific amount of
336 * pixels. For instance, a left padding of 2 will push the view's content by
337 * 2 pixels to the right of the left edge. Padding can be set using the
338 * {@link #setPadding(int, int, int, int)} method and queried by calling
339 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700340 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 * </p>
342 *
343 * <p>
344 * Even though a view can define a padding, it does not provide any support for
345 * margins. However, view groups provide such a support. Refer to
346 * {@link android.view.ViewGroup} and
347 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
348 * </p>
349 *
350 * <a name="Layout"></a>
351 * <h3>Layout</h3>
352 * <p>
353 * Layout is a two pass process: a measure pass and a layout pass. The measuring
354 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
355 * of the view tree. Each view pushes dimension specifications down the tree
356 * during the recursion. At the end of the measure pass, every view has stored
357 * its measurements. The second pass happens in
358 * {@link #layout(int,int,int,int)} and is also top-down. During
359 * this pass each parent is responsible for positioning all of its children
360 * using the sizes computed in the measure pass.
361 * </p>
362 *
363 * <p>
364 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
365 * {@link #getMeasuredHeight()} values must be set, along with those for all of
366 * that view's descendants. A view's measured width and measured height values
367 * must respect the constraints imposed by the view's parents. This guarantees
368 * that at the end of the measure pass, all parents accept all of their
369 * children's measurements. A parent view may call measure() more than once on
370 * its children. For example, the parent may measure each child once with
371 * unspecified dimensions to find out how big they want to be, then call
372 * measure() on them again with actual numbers if the sum of all the children's
373 * unconstrained sizes is too big or too small.
374 * </p>
375 *
376 * <p>
377 * The measure pass uses two classes to communicate dimensions. The
378 * {@link MeasureSpec} class is used by views to tell their parents how they
379 * want to be measured and positioned. The base LayoutParams class just
380 * describes how big the view wants to be for both width and height. For each
381 * dimension, it can specify one of:
382 * <ul>
383 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800384 * <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 -0800385 * (minus padding)
386 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
387 * enclose its content (plus padding).
388 * </ul>
389 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
390 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
391 * an X and Y value.
392 * </p>
393 *
394 * <p>
395 * MeasureSpecs are used to push requirements down the tree from parent to
396 * child. A MeasureSpec can be in one of three modes:
397 * <ul>
398 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
399 * of a child view. For example, a LinearLayout may call measure() on its child
400 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
401 * tall the child view wants to be given a width of 240 pixels.
402 * <li>EXACTLY: This is used by the parent to impose an exact size on the
403 * child. The child must use this size, and guarantee that all of its
404 * descendants will fit within this size.
405 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
406 * child. The child must gurantee that it and all of its descendants will fit
407 * within this size.
408 * </ul>
409 * </p>
410 *
411 * <p>
412 * To intiate a layout, call {@link #requestLayout}. This method is typically
413 * called by a view on itself when it believes that is can no longer fit within
414 * its current bounds.
415 * </p>
416 *
417 * <a name="Drawing"></a>
418 * <h3>Drawing</h3>
419 * <p>
420 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700421 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * this means that parents will draw before (i.e., behind) their children, with
423 * siblings drawn in the order they appear in the tree.
424 * If you set a background drawable for a View, then the View will draw it for you
425 * before calling back to its <code>onDraw()</code> method.
426 * </p>
427 *
428 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700429 * 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 -0800430 * </p>
431 *
432 * <p>
433 * To force a view to draw, call {@link #invalidate()}.
434 * </p>
435 *
436 * <a name="EventHandlingThreading"></a>
437 * <h3>Event Handling and Threading</h3>
438 * <p>
439 * The basic cycle of a view is as follows:
440 * <ol>
441 * <li>An event comes in and is dispatched to the appropriate view. The view
442 * handles the event and notifies any listeners.</li>
443 * <li>If in the course of processing the event, the view's bounds may need
444 * to be changed, the view will call {@link #requestLayout()}.</li>
445 * <li>Similarly, if in the course of processing the event the view's appearance
446 * may need to be changed, the view will call {@link #invalidate()}.</li>
447 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
448 * the framework will take care of measuring, laying out, and drawing the tree
449 * as appropriate.</li>
450 * </ol>
451 * </p>
452 *
453 * <p><em>Note: The entire view tree is single threaded. You must always be on
454 * the UI thread when calling any method on any view.</em>
455 * If you are doing work on other threads and want to update the state of a view
456 * from that thread, you should use a {@link Handler}.
457 * </p>
458 *
459 * <a name="FocusHandling"></a>
460 * <h3>Focus Handling</h3>
461 * <p>
462 * The framework will handle routine focus movement in response to user input.
463 * This includes changing the focus as views are removed or hidden, or as new
464 * views become available. Views indicate their willingness to take focus
465 * through the {@link #isFocusable} method. To change whether a view can take
466 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
467 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
468 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
469 * </p>
470 * <p>
471 * Focus movement is based on an algorithm which finds the nearest neighbor in a
472 * given direction. In rare cases, the default algorithm may not match the
473 * intended behavior of the developer. In these situations, you can provide
474 * explicit overrides by using these XML attributes in the layout file:
475 * <pre>
476 * nextFocusDown
477 * nextFocusLeft
478 * nextFocusRight
479 * nextFocusUp
480 * </pre>
481 * </p>
482 *
483 *
484 * <p>
485 * To get a particular view to take focus, call {@link #requestFocus()}.
486 * </p>
487 *
488 * <a name="TouchMode"></a>
489 * <h3>Touch Mode</h3>
490 * <p>
491 * When a user is navigating a user interface via directional keys such as a D-pad, it is
492 * necessary to give focus to actionable items such as buttons so the user can see
493 * what will take input. If the device has touch capabilities, however, and the user
494 * begins interacting with the interface by touching it, it is no longer necessary to
495 * always highlight, or give focus to, a particular view. This motivates a mode
496 * for interaction named 'touch mode'.
497 * </p>
498 * <p>
499 * For a touch capable device, once the user touches the screen, the device
500 * will enter touch mode. From this point onward, only views for which
501 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
502 * Other views that are touchable, like buttons, will not take focus when touched; they will
503 * only fire the on click listeners.
504 * </p>
505 * <p>
506 * Any time a user hits a directional key, such as a D-pad direction, the view device will
507 * exit touch mode, and find a view to take focus, so that the user may resume interacting
508 * with the user interface without touching the screen again.
509 * </p>
510 * <p>
511 * The touch mode state is maintained across {@link android.app.Activity}s. Call
512 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
513 * </p>
514 *
515 * <a name="Scrolling"></a>
516 * <h3>Scrolling</h3>
517 * <p>
518 * The framework provides basic support for views that wish to internally
519 * scroll their content. This includes keeping track of the X and Y scroll
520 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800521 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700522 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * </p>
524 *
525 * <a name="Tags"></a>
526 * <h3>Tags</h3>
527 * <p>
528 * Unlike IDs, tags are not used to identify views. Tags are essentially an
529 * extra piece of information that can be associated with a view. They are most
530 * often used as a convenience to store data related to views in the views
531 * themselves rather than by putting them in a separate structure.
532 * </p>
533 *
534 * <a name="Animation"></a>
535 * <h3>Animation</h3>
536 * <p>
537 * You can attach an {@link Animation} object to a view using
538 * {@link #setAnimation(Animation)} or
539 * {@link #startAnimation(Animation)}. The animation can alter the scale,
540 * rotation, translation and alpha of a view over time. If the animation is
541 * attached to a view that has children, the animation will affect the entire
542 * subtree rooted by that node. When an animation is started, the framework will
543 * take care of redrawing the appropriate views until the animation completes.
544 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800545 * <p>
546 * Starting with Android 3.0, the preferred way of animating views is to use the
547 * {@link android.animation} package APIs.
548 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 *
Jeff Brown85a31762010-09-01 17:01:00 -0700550 * <a name="Security"></a>
551 * <h3>Security</h3>
552 * <p>
553 * Sometimes it is essential that an application be able to verify that an action
554 * is being performed with the full knowledge and consent of the user, such as
555 * granting a permission request, making a purchase or clicking on an advertisement.
556 * Unfortunately, a malicious application could try to spoof the user into
557 * performing these actions, unaware, by concealing the intended purpose of the view.
558 * As a remedy, the framework offers a touch filtering mechanism that can be used to
559 * improve the security of views that provide access to sensitive functionality.
560 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700561 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800562 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700563 * will discard touches that are received whenever the view's window is obscured by
564 * another visible window. As a result, the view will not receive touches whenever a
565 * toast, dialog or other window appears above the view's window.
566 * </p><p>
567 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700568 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
569 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700570 * </p>
571 *
Romain Guy171c5922011-01-06 10:04:23 -0800572 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700573 * @attr ref android.R.styleable#View_background
574 * @attr ref android.R.styleable#View_clickable
575 * @attr ref android.R.styleable#View_contentDescription
576 * @attr ref android.R.styleable#View_drawingCacheQuality
577 * @attr ref android.R.styleable#View_duplicateParentState
578 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700579 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700580 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700581 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700583 * @attr ref android.R.styleable#View_isScrollContainer
584 * @attr ref android.R.styleable#View_focusable
585 * @attr ref android.R.styleable#View_focusableInTouchMode
586 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
587 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800588 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700589 * @attr ref android.R.styleable#View_longClickable
590 * @attr ref android.R.styleable#View_minHeight
591 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * @attr ref android.R.styleable#View_nextFocusDown
593 * @attr ref android.R.styleable#View_nextFocusLeft
594 * @attr ref android.R.styleable#View_nextFocusRight
595 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700596 * @attr ref android.R.styleable#View_onClick
597 * @attr ref android.R.styleable#View_padding
598 * @attr ref android.R.styleable#View_paddingBottom
599 * @attr ref android.R.styleable#View_paddingLeft
600 * @attr ref android.R.styleable#View_paddingRight
601 * @attr ref android.R.styleable#View_paddingTop
602 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800603 * @attr ref android.R.styleable#View_rotation
604 * @attr ref android.R.styleable#View_rotationX
605 * @attr ref android.R.styleable#View_rotationY
606 * @attr ref android.R.styleable#View_scaleX
607 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 * @attr ref android.R.styleable#View_scrollX
609 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_scrollbarSize
611 * @attr ref android.R.styleable#View_scrollbarStyle
612 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700613 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
614 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
616 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_scrollbarThumbVertical
618 * @attr ref android.R.styleable#View_scrollbarTrackVertical
619 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
620 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700621 * @attr ref android.R.styleable#View_soundEffectsEnabled
622 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800623 * @attr ref android.R.styleable#View_transformPivotX
624 * @attr ref android.R.styleable#View_transformPivotY
625 * @attr ref android.R.styleable#View_translationX
626 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 *
629 * @see android.view.ViewGroup
630 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700631public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
632 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 private static final boolean DBG = false;
634
635 /**
636 * The logging tag used by this class with android.util.Log.
637 */
638 protected static final String VIEW_LOG_TAG = "View";
639
640 /**
641 * Used to mark a View that has no ID.
642 */
643 public static final int NO_ID = -1;
644
645 /**
646 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
647 * calling setFlags.
648 */
649 private static final int NOT_FOCUSABLE = 0x00000000;
650
651 /**
652 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
653 * setFlags.
654 */
655 private static final int FOCUSABLE = 0x00000001;
656
657 /**
658 * Mask for use with setFlags indicating bits used for focus.
659 */
660 private static final int FOCUSABLE_MASK = 0x00000001;
661
662 /**
663 * This view will adjust its padding to fit sytem windows (e.g. status bar)
664 */
665 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
666
667 /**
Scott Main812634c22011-07-27 13:22:35 -0700668 * This view is visible.
669 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
670 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 */
672 public static final int VISIBLE = 0x00000000;
673
674 /**
675 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700676 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
677 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700683 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
684 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
686 public static final int GONE = 0x00000008;
687
688 /**
689 * Mask for use with setFlags indicating bits used for visibility.
690 * {@hide}
691 */
692 static final int VISIBILITY_MASK = 0x0000000C;
693
694 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
695
696 /**
697 * This view is enabled. Intrepretation varies by subclass.
698 * Use with ENABLED_MASK when calling setFlags.
699 * {@hide}
700 */
701 static final int ENABLED = 0x00000000;
702
703 /**
704 * This view is disabled. Intrepretation varies by subclass.
705 * Use with ENABLED_MASK when calling setFlags.
706 * {@hide}
707 */
708 static final int DISABLED = 0x00000020;
709
710 /**
711 * Mask for use with setFlags indicating bits used for indicating whether
712 * this view is enabled
713 * {@hide}
714 */
715 static final int ENABLED_MASK = 0x00000020;
716
717 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700718 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
719 * called and further optimizations will be performed. It is okay to have
720 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800954 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700955 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800956 * {@hide}
957 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800959
960 /**
961 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700962 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800963 * {@hide}
964 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800966
967 /**
968 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700969 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800970 * {@hide}
971 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800973
974 /**
975 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800977 * {@hide}
978 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800980
981 /**
982 * Mask for use with setFlags indicating bits used for horizontalDirection.
983 * {@hide}
984 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700985 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800986
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700987 /*
988 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
989 * flag value.
990 * {@hide}
991 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700992 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
993 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800994
995 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700996 * Default horizontalDirection.
997 * {@hide}
998 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700999 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001000
1001 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001002 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1003 * should add all focusable Views regardless if they are focusable in touch mode.
1004 */
1005 public static final int FOCUSABLES_ALL = 0x00000000;
1006
1007 /**
1008 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1009 * should add only Views focusable in touch mode.
1010 */
1011 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
1045 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001046 * Bits of {@link #getMeasuredWidthAndState()} and
1047 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1048 */
1049 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1050
1051 /**
1052 * Bits of {@link #getMeasuredWidthAndState()} and
1053 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1054 */
1055 public static final int MEASURED_STATE_MASK = 0xff000000;
1056
1057 /**
1058 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1059 * for functions that combine both width and height into a single int,
1060 * such as {@link #getMeasuredState()} and the childState argument of
1061 * {@link #resolveSizeAndState(int, int, int)}.
1062 */
1063 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1064
1065 /**
1066 * Bit of {@link #getMeasuredWidthAndState()} and
1067 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1068 * is smaller that the space the view would like to have.
1069 */
1070 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1071
1072 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 * Base View state sets
1074 */
1075 // Singles
1076 /**
1077 * Indicates the view has no states set. States are used with
1078 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1079 * view depending on its state.
1080 *
1081 * @see android.graphics.drawable.Drawable
1082 * @see #getDrawableState()
1083 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001084 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 /**
1086 * Indicates the view is enabled. States are used with
1087 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1088 * view depending on its state.
1089 *
1090 * @see android.graphics.drawable.Drawable
1091 * @see #getDrawableState()
1092 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001093 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 /**
1095 * Indicates the view is focused. States are used with
1096 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1097 * view depending on its state.
1098 *
1099 * @see android.graphics.drawable.Drawable
1100 * @see #getDrawableState()
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is selected. States are used with
1105 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1106 * view depending on its state.
1107 *
1108 * @see android.graphics.drawable.Drawable
1109 * @see #getDrawableState()
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view is pressed. States are used with
1114 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1115 * view depending on its state.
1116 *
1117 * @see android.graphics.drawable.Drawable
1118 * @see #getDrawableState()
1119 * @hide
1120 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001121 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 /**
1123 * Indicates the view's window has focus. States are used with
1124 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1125 * view depending on its state.
1126 *
1127 * @see android.graphics.drawable.Drawable
1128 * @see #getDrawableState()
1129 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001130 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 // Doubles
1132 /**
1133 * Indicates the view is enabled and has the focus.
1134 *
1135 * @see #ENABLED_STATE_SET
1136 * @see #FOCUSED_STATE_SET
1137 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001138 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 /**
1140 * Indicates the view is enabled and selected.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #SELECTED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled and that its window has focus.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #WINDOW_FOCUSED_STATE_SET
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 /**
1154 * Indicates the view is focused and selected.
1155 *
1156 * @see #FOCUSED_STATE_SET
1157 * @see #SELECTED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view has the focus and that its window has the focus.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #WINDOW_FOCUSED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view is selected and that its window has the focus.
1169 *
1170 * @see #SELECTED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 // Triples
1175 /**
1176 * Indicates the view is enabled, focused and selected.
1177 *
1178 * @see #ENABLED_STATE_SET
1179 * @see #FOCUSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is enabled, focused and its window has the focus.
1185 *
1186 * @see #ENABLED_STATE_SET
1187 * @see #FOCUSED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is enabled, selected and its window has the focus.
1193 *
1194 * @see #ENABLED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 * @see #WINDOW_FOCUSED_STATE_SET
1197 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001198 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 /**
1200 * Indicates the view is focused, selected and its window has the focus.
1201 *
1202 * @see #FOCUSED_STATE_SET
1203 * @see #SELECTED_STATE_SET
1204 * @see #WINDOW_FOCUSED_STATE_SET
1205 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001206 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
1208 * Indicates the view is enabled, focused, selected and its window
1209 * has the focus.
1210 *
1211 * @see #ENABLED_STATE_SET
1212 * @see #FOCUSED_STATE_SET
1213 * @see #SELECTED_STATE_SET
1214 * @see #WINDOW_FOCUSED_STATE_SET
1215 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001216 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 /**
1218 * Indicates the view is pressed and its window has the focus.
1219 *
1220 * @see #PRESSED_STATE_SET
1221 * @see #WINDOW_FOCUSED_STATE_SET
1222 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001223 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 /**
1225 * Indicates the view is pressed and selected.
1226 *
1227 * @see #PRESSED_STATE_SET
1228 * @see #SELECTED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is pressed, selected and its window has the focus.
1233 *
1234 * @see #PRESSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 * @see #WINDOW_FOCUSED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed and focused.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, focused and its window has the focus.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #WINDOW_FOCUSED_STATE_SET
1252 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001253 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Indicates the view is pressed, focused and selected.
1256 *
1257 * @see #PRESSED_STATE_SET
1258 * @see #SELECTED_STATE_SET
1259 * @see #FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed, focused, selected and its window has the focus.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #FOCUSED_STATE_SET
1267 * @see #SELECTED_STATE_SET
1268 * @see #WINDOW_FOCUSED_STATE_SET
1269 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001270 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 /**
1272 * Indicates the view is pressed and enabled.
1273 *
1274 * @see #PRESSED_STATE_SET
1275 * @see #ENABLED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed, enabled and its window has the focus.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 * @see #WINDOW_FOCUSED_STATE_SET
1284 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001285 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, enabled and selected.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #ENABLED_STATE_SET
1291 * @see #SELECTED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 /**
1295 * Indicates the view is pressed, enabled, selected and its window has the
1296 * focus.
1297 *
1298 * @see #PRESSED_STATE_SET
1299 * @see #ENABLED_STATE_SET
1300 * @see #SELECTED_STATE_SET
1301 * @see #WINDOW_FOCUSED_STATE_SET
1302 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001303 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 /**
1305 * Indicates the view is pressed, enabled and focused.
1306 *
1307 * @see #PRESSED_STATE_SET
1308 * @see #ENABLED_STATE_SET
1309 * @see #FOCUSED_STATE_SET
1310 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001311 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 /**
1313 * Indicates the view is pressed, enabled, focused and its window has the
1314 * focus.
1315 *
1316 * @see #PRESSED_STATE_SET
1317 * @see #ENABLED_STATE_SET
1318 * @see #FOCUSED_STATE_SET
1319 * @see #WINDOW_FOCUSED_STATE_SET
1320 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 /**
1323 * Indicates the view is pressed, enabled, focused and selected.
1324 *
1325 * @see #PRESSED_STATE_SET
1326 * @see #ENABLED_STATE_SET
1327 * @see #SELECTED_STATE_SET
1328 * @see #FOCUSED_STATE_SET
1329 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 /**
1332 * Indicates the view is pressed, enabled, focused, selected and its window
1333 * has the focus.
1334 *
1335 * @see #PRESSED_STATE_SET
1336 * @see #ENABLED_STATE_SET
1337 * @see #SELECTED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342
1343 /**
1344 * The order here is very important to {@link #getDrawableState()}
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 private static final int[][] VIEW_STATE_SETS;
1347
Romain Guyb051e892010-09-28 19:09:36 -07001348 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1349 static final int VIEW_STATE_SELECTED = 1 << 1;
1350 static final int VIEW_STATE_FOCUSED = 1 << 2;
1351 static final int VIEW_STATE_ENABLED = 1 << 3;
1352 static final int VIEW_STATE_PRESSED = 1 << 4;
1353 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001354 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001355 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001356 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1357 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001358
1359 static final int[] VIEW_STATE_IDS = new int[] {
1360 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1361 R.attr.state_selected, VIEW_STATE_SELECTED,
1362 R.attr.state_focused, VIEW_STATE_FOCUSED,
1363 R.attr.state_enabled, VIEW_STATE_ENABLED,
1364 R.attr.state_pressed, VIEW_STATE_PRESSED,
1365 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001366 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001367 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001368 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1369 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 };
1371
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001372 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001373 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1374 throw new IllegalStateException(
1375 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1376 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001378 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001380 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001382 orderedIds[i * 2] = viewState;
1383 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 }
1385 }
1386 }
Romain Guyb051e892010-09-28 19:09:36 -07001387 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1388 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1389 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001390 int numBits = Integer.bitCount(i);
1391 int[] set = new int[numBits];
1392 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001393 for (int j = 0; j < orderedIds.length; j += 2) {
1394 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001395 set[pos++] = orderedIds[j];
1396 }
1397 }
1398 VIEW_STATE_SETS[i] = set;
1399 }
1400
1401 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1402 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1403 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1404 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1406 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1407 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1409 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1411 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1413 | VIEW_STATE_FOCUSED];
1414 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1415 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1417 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1419 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_ENABLED];
1422 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1432 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1433
1434 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1435 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1437 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1439 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_PRESSED];
1442 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1460 | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1463 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1472 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1475 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1476 | VIEW_STATE_PRESSED];
1477 }
1478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001480 * Accessibility event types that are dispatched for text population.
1481 */
1482 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1483 AccessibilityEvent.TYPE_VIEW_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1485 | AccessibilityEvent.TYPE_VIEW_SELECTED
1486 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1487 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1488 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001489 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * The view's tag.
1557 * {@hide}
1558 *
1559 * @see #setTag(Object)
1560 * @see #getTag()
1561 */
1562 protected Object mTag;
1563
1564 // for mPrivateFlags:
1565 /** {@hide} */
1566 static final int WANTS_FOCUS = 0x00000001;
1567 /** {@hide} */
1568 static final int FOCUSED = 0x00000002;
1569 /** {@hide} */
1570 static final int SELECTED = 0x00000004;
1571 /** {@hide} */
1572 static final int IS_ROOT_NAMESPACE = 0x00000008;
1573 /** {@hide} */
1574 static final int HAS_BOUNDS = 0x00000010;
1575 /** {@hide} */
1576 static final int DRAWN = 0x00000020;
1577 /**
1578 * When this flag is set, this view is running an animation on behalf of its
1579 * children and should therefore not cancel invalidate requests, even if they
1580 * lie outside of this view's bounds.
1581 *
1582 * {@hide}
1583 */
1584 static final int DRAW_ANIMATION = 0x00000040;
1585 /** {@hide} */
1586 static final int SKIP_DRAW = 0x00000080;
1587 /** {@hide} */
1588 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1589 /** {@hide} */
1590 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1591 /** {@hide} */
1592 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1593 /** {@hide} */
1594 static final int MEASURED_DIMENSION_SET = 0x00000800;
1595 /** {@hide} */
1596 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001597 /** {@hide} */
1598 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 private static final int PRESSED = 0x00004000;
1601
1602 /** {@hide} */
1603 static final int DRAWING_CACHE_VALID = 0x00008000;
1604 /**
1605 * Flag used to indicate that this view should be drawn once more (and only once
1606 * more) after its animation has completed.
1607 * {@hide}
1608 */
1609 static final int ANIMATION_STARTED = 0x00010000;
1610
1611 private static final int SAVE_STATE_CALLED = 0x00020000;
1612
1613 /**
1614 * Indicates that the View returned true when onSetAlpha() was called and that
1615 * the alpha must be restored.
1616 * {@hide}
1617 */
1618 static final int ALPHA_SET = 0x00040000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER = 0x00080000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1629
1630 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001631 * View flag indicating whether this view was invalidated (fully or partially.)
1632 *
1633 * @hide
1634 */
1635 static final int DIRTY = 0x00200000;
1636
1637 /**
1638 * View flag indicating whether this view was invalidated by an opaque
1639 * invalidate request.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_OPAQUE = 0x00400000;
1644
1645 /**
1646 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1647 *
1648 * @hide
1649 */
1650 static final int DIRTY_MASK = 0x00600000;
1651
1652 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001653 * Indicates whether the background is opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_BACKGROUND = 0x00800000;
1658
1659 /**
1660 * Indicates whether the scrollbars are opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_SCROLLBARS = 0x01000000;
1665
1666 /**
1667 * Indicates whether the view is opaque.
1668 *
1669 * @hide
1670 */
1671 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001672
Adam Powelle14579b2009-12-16 18:39:52 -08001673 /**
1674 * Indicates a prepressed state;
1675 * the short time between ACTION_DOWN and recognizing
1676 * a 'real' press. Prepressed is used to recognize quick taps
1677 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001678 *
Adam Powelle14579b2009-12-16 18:39:52 -08001679 * @hide
1680 */
1681 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powellc9fbaab2010-02-16 17:16:19 -08001683 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001684 * Indicates whether the view is temporarily detached.
1685 *
1686 * @hide
1687 */
1688 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 /**
1691 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001693 * @hide
1694 */
1695 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001696
1697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001698 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1699 * @hide
1700 */
1701 private static final int HOVERED = 0x10000000;
1702
1703 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001704 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1705 * for transform operations
1706 *
1707 * @hide
1708 */
Adam Powellf37df072010-09-17 16:22:49 -07001709 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001710
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001711 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001712 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713
Chet Haasefd2b0022010-08-06 13:08:56 -07001714 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001715 * Indicates that this view was specifically invalidated, not just dirtied because some
1716 * child view was invalidated. The flag is used to determine when we need to recreate
1717 * a view's display list (as opposed to just returning a reference to its existing
1718 * display list).
1719 *
1720 * @hide
1721 */
1722 static final int INVALIDATED = 0x80000000;
1723
Christopher Tate3d4bf172011-03-28 16:16:46 -07001724 /* Masks for mPrivateFlags2 */
1725
1726 /**
1727 * Indicates that this view has reported that it can accept the current drag's content.
1728 * Cleared when the drag operation concludes.
1729 * @hide
1730 */
1731 static final int DRAG_CAN_ACCEPT = 0x00000001;
1732
1733 /**
1734 * Indicates that this view is currently directly under the drag location in a
1735 * drag-and-drop operation involving content that it can accept. Cleared when
1736 * the drag exits the view, or when the drag operation concludes.
1737 * @hide
1738 */
1739 static final int DRAG_HOVERED = 0x00000002;
1740
Cibu Johny86666632010-02-22 13:01:02 -08001741 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 * Indicates whether the view layout direction has been resolved and drawn to the
1743 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001744 *
1745 * @hide
1746 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1748
1749 /**
1750 * Indicates whether the view layout direction has been resolved.
1751 *
1752 * @hide
1753 */
1754 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1755
Cibu Johny86666632010-02-22 13:01:02 -08001756
Christopher Tate3d4bf172011-03-28 16:16:46 -07001757 /* End of masks for mPrivateFlags2 */
1758
1759 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1760
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 /**
Adam Powell637d3372010-08-25 14:37:03 -07001762 * Always allow a user to over-scroll this view, provided it is a
1763 * view that can scroll.
1764 *
1765 * @see #getOverScrollMode()
1766 * @see #setOverScrollMode(int)
1767 */
1768 public static final int OVER_SCROLL_ALWAYS = 0;
1769
1770 /**
1771 * Allow a user to over-scroll this view only if the content is large
1772 * enough to meaningfully scroll, provided it is a view that can scroll.
1773 *
1774 * @see #getOverScrollMode()
1775 * @see #setOverScrollMode(int)
1776 */
1777 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1778
1779 /**
1780 * Never allow a user to over-scroll this view.
1781 *
1782 * @see #getOverScrollMode()
1783 * @see #setOverScrollMode(int)
1784 */
1785 public static final int OVER_SCROLL_NEVER = 2;
1786
1787 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001788 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001789 *
Joe Malin32736f02011-01-19 16:14:20 -08001790 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001791 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001792 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001793
1794 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001795 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1796 *
1797 * This is for use in games, book readers, video players, or any other "immersive" application
1798 * where the usual system chrome is deemed too distracting.
1799 *
1800 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001801 *
Joe Malin32736f02011-01-19 16:14:20 -08001802 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001803 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001804 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1805
1806 /**
1807 * View has requested that the system navigation be temporarily hidden.
1808 *
1809 * This is an even less obtrusive state than that called for by
1810 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1811 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1812 * those to disappear. This is useful (in conjunction with the
1813 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1814 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1815 * window flags) for displaying content using every last pixel on the display.
1816 *
1817 * There is a limitation: because navigation controls are so important, the least user
1818 * interaction will cause them to reappear immediately.
1819 *
1820 * @see #setSystemUiVisibility(int)
1821 */
1822 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1823
1824 /**
1825 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1826 */
1827 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1828
1829 /**
1830 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1831 */
1832 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001833
1834 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001835 * @hide
1836 *
1837 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1838 * out of the public fields to keep the undefined bits out of the developer's way.
1839 *
1840 * Flag to make the status bar not expandable. Unless you also
1841 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1842 */
1843 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1844
1845 /**
1846 * @hide
1847 *
1848 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1849 * out of the public fields to keep the undefined bits out of the developer's way.
1850 *
1851 * Flag to hide notification icons and scrolling ticker text.
1852 */
1853 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1854
1855 /**
1856 * @hide
1857 *
1858 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1859 * out of the public fields to keep the undefined bits out of the developer's way.
1860 *
1861 * Flag to disable incoming notification alerts. This will not block
1862 * icons, but it will block sound, vibrating and other visual or aural notifications.
1863 */
1864 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1865
1866 /**
1867 * @hide
1868 *
1869 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1870 * out of the public fields to keep the undefined bits out of the developer's way.
1871 *
1872 * Flag to hide only the scrolling ticker. Note that
1873 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1874 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1875 */
1876 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1877
1878 /**
1879 * @hide
1880 *
1881 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1882 * out of the public fields to keep the undefined bits out of the developer's way.
1883 *
1884 * Flag to hide the center system info area.
1885 */
1886 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001894 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001895 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1896 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001897 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001898
1899 /**
1900 * @hide
1901 *
1902 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1903 * out of the public fields to keep the undefined bits out of the developer's way.
1904 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001905 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001906 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1907 */
1908 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1909
1910 /**
1911 * @hide
1912 *
1913 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1914 * out of the public fields to keep the undefined bits out of the developer's way.
1915 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001916 * Flag to hide only the clock. You might use this if your activity has
1917 * its own clock making the status bar's clock redundant.
1918 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001919 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1920
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001921 /**
1922 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001923 *
1924 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1925 * out of the public fields to keep the undefined bits out of the developer's way.
1926 *
1927 * Flag to hide only the recent apps button. Don't use this
1928 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1929 */
1930 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1931
1932 /**
1933 * @hide
1934 *
1935 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1936 *
1937 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1938 */
1939 @Deprecated
1940 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1941 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1942
1943 /**
1944 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001945 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001946 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001947
1948 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001949 * These are the system UI flags that can be cleared by events outside
1950 * of an application. Currently this is just the ability to tap on the
1951 * screen while hiding the navigation bar to have it return.
1952 * @hide
1953 */
1954 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1955 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1956
1957 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001958 * Find views that render the specified text.
1959 *
1960 * @see #findViewsWithText(ArrayList, CharSequence, int)
1961 */
1962 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1963
1964 /**
1965 * Find find views that contain the specified content description.
1966 *
1967 * @see #findViewsWithText(ArrayList, CharSequence, int)
1968 */
1969 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1970
1971 /**
Adam Powell637d3372010-08-25 14:37:03 -07001972 * Controls the over-scroll mode for this view.
1973 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1974 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1975 * and {@link #OVER_SCROLL_NEVER}.
1976 */
1977 private int mOverScrollMode;
1978
1979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 * The parent this view is attached to.
1981 * {@hide}
1982 *
1983 * @see #getParent()
1984 */
1985 protected ViewParent mParent;
1986
1987 /**
1988 * {@hide}
1989 */
1990 AttachInfo mAttachInfo;
1991
1992 /**
1993 * {@hide}
1994 */
Romain Guy809a7f62009-05-14 15:44:42 -07001995 @ViewDebug.ExportedProperty(flagMapping = {
1996 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1997 name = "FORCE_LAYOUT"),
1998 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1999 name = "LAYOUT_REQUIRED"),
2000 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002001 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002002 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2003 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2004 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2005 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2006 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002008 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009
2010 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002011 * This view's request for the visibility of the status bar.
2012 * @hide
2013 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002014 @ViewDebug.ExportedProperty(flagMapping = {
2015 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2016 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2017 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2018 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2019 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2020 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2021 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2022 equals = SYSTEM_UI_FLAG_VISIBLE,
2023 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2024 })
Joe Onorato664644d2011-01-23 17:53:23 -08002025 int mSystemUiVisibility;
2026
2027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 * Count of how many windows this view has been attached to.
2029 */
2030 int mWindowAttachCount;
2031
2032 /**
2033 * The layout parameters associated with this view and used by the parent
2034 * {@link android.view.ViewGroup} to determine how this view should be
2035 * laid out.
2036 * {@hide}
2037 */
2038 protected ViewGroup.LayoutParams mLayoutParams;
2039
2040 /**
2041 * The view flags hold various views states.
2042 * {@hide}
2043 */
2044 @ViewDebug.ExportedProperty
2045 int mViewFlags;
2046
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002047 static class TransformationInfo {
2048 /**
2049 * The transform matrix for the View. This transform is calculated internally
2050 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2051 * is used by default. Do *not* use this variable directly; instead call
2052 * getMatrix(), which will automatically recalculate the matrix if necessary
2053 * to get the correct matrix based on the latest rotation and scale properties.
2054 */
2055 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002056
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002057 /**
2058 * The transform matrix for the View. This transform is calculated internally
2059 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2060 * is used by default. Do *not* use this variable directly; instead call
2061 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2062 * to get the correct matrix based on the latest rotation and scale properties.
2063 */
2064 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002065
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002066 /**
2067 * An internal variable that tracks whether we need to recalculate the
2068 * transform matrix, based on whether the rotation or scaleX/Y properties
2069 * have changed since the matrix was last calculated.
2070 */
2071 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002072
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002073 /**
2074 * An internal variable that tracks whether we need to recalculate the
2075 * transform matrix, based on whether the rotation or scaleX/Y properties
2076 * have changed since the matrix was last calculated.
2077 */
2078 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002079
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002080 /**
2081 * A variable that tracks whether we need to recalculate the
2082 * transform matrix, based on whether the rotation or scaleX/Y properties
2083 * have changed since the matrix was last calculated. This variable
2084 * is only valid after a call to updateMatrix() or to a function that
2085 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2086 */
2087 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002088
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002089 /**
2090 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2091 */
2092 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002093
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002094 /**
2095 * This matrix is used when computing the matrix for 3D rotations.
2096 */
2097 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002098
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002099 /**
2100 * These prev values are used to recalculate a centered pivot point when necessary. The
2101 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2102 * set), so thes values are only used then as well.
2103 */
2104 private int mPrevWidth = -1;
2105 private int mPrevHeight = -1;
2106
2107 /**
2108 * The degrees rotation around the vertical axis through the pivot point.
2109 */
2110 @ViewDebug.ExportedProperty
2111 float mRotationY = 0f;
2112
2113 /**
2114 * The degrees rotation around the horizontal axis through the pivot point.
2115 */
2116 @ViewDebug.ExportedProperty
2117 float mRotationX = 0f;
2118
2119 /**
2120 * The degrees rotation around the pivot point.
2121 */
2122 @ViewDebug.ExportedProperty
2123 float mRotation = 0f;
2124
2125 /**
2126 * The amount of translation of the object away from its left property (post-layout).
2127 */
2128 @ViewDebug.ExportedProperty
2129 float mTranslationX = 0f;
2130
2131 /**
2132 * The amount of translation of the object away from its top property (post-layout).
2133 */
2134 @ViewDebug.ExportedProperty
2135 float mTranslationY = 0f;
2136
2137 /**
2138 * The amount of scale in the x direction around the pivot point. A
2139 * value of 1 means no scaling is applied.
2140 */
2141 @ViewDebug.ExportedProperty
2142 float mScaleX = 1f;
2143
2144 /**
2145 * The amount of scale in the y direction around the pivot point. A
2146 * value of 1 means no scaling is applied.
2147 */
2148 @ViewDebug.ExportedProperty
2149 float mScaleY = 1f;
2150
2151 /**
2152 * The amount of scale in the x direction around the pivot point. A
2153 * value of 1 means no scaling is applied.
2154 */
2155 @ViewDebug.ExportedProperty
2156 float mPivotX = 0f;
2157
2158 /**
2159 * The amount of scale in the y direction around the pivot point. A
2160 * value of 1 means no scaling is applied.
2161 */
2162 @ViewDebug.ExportedProperty
2163 float mPivotY = 0f;
2164
2165 /**
2166 * The opacity of the View. This is a value from 0 to 1, where 0 means
2167 * completely transparent and 1 means completely opaque.
2168 */
2169 @ViewDebug.ExportedProperty
2170 float mAlpha = 1f;
2171 }
2172
2173 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002174
Joe Malin32736f02011-01-19 16:14:20 -08002175 private boolean mLastIsOpaque;
2176
Chet Haasefd2b0022010-08-06 13:08:56 -07002177 /**
2178 * Convenience value to check for float values that are close enough to zero to be considered
2179 * zero.
2180 */
Romain Guy2542d192010-08-18 11:47:12 -07002181 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002182
2183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 * The distance in pixels from the left edge of this view's parent
2185 * to the left edge of this view.
2186 * {@hide}
2187 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002188 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 protected int mLeft;
2190 /**
2191 * The distance in pixels from the left edge of this view's parent
2192 * to the right edge of this view.
2193 * {@hide}
2194 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002195 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 protected int mRight;
2197 /**
2198 * The distance in pixels from the top edge of this view's parent
2199 * to the top edge of this view.
2200 * {@hide}
2201 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002202 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 protected int mTop;
2204 /**
2205 * The distance in pixels from the top edge of this view's parent
2206 * to the bottom edge of this view.
2207 * {@hide}
2208 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002209 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 protected int mBottom;
2211
2212 /**
2213 * The offset, in pixels, by which the content of this view is scrolled
2214 * horizontally.
2215 * {@hide}
2216 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002217 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 protected int mScrollX;
2219 /**
2220 * The offset, in pixels, by which the content of this view is scrolled
2221 * vertically.
2222 * {@hide}
2223 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002224 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 protected int mScrollY;
2226
2227 /**
2228 * The left padding in pixels, that is the distance in pixels between the
2229 * left edge of this view and the left edge of its content.
2230 * {@hide}
2231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 protected int mPaddingLeft;
2234 /**
2235 * The right padding in pixels, that is the distance in pixels between the
2236 * right edge of this view and the right edge of its content.
2237 * {@hide}
2238 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002239 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 protected int mPaddingRight;
2241 /**
2242 * The top padding in pixels, that is the distance in pixels between the
2243 * top edge of this view and the top edge of its content.
2244 * {@hide}
2245 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002246 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 protected int mPaddingTop;
2248 /**
2249 * The bottom padding in pixels, that is the distance in pixels between the
2250 * bottom edge of this view and the bottom edge of its content.
2251 * {@hide}
2252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002253 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 protected int mPaddingBottom;
2255
2256 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002257 * Briefly describes the view and is primarily used for accessibility support.
2258 */
2259 private CharSequence mContentDescription;
2260
2261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002263 *
2264 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002266 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002267 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268
2269 /**
2270 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002271 *
2272 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002274 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002275 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002277 /**
Adam Powell20232d02010-12-08 21:08:53 -08002278 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002279 *
2280 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002281 */
2282 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002283 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002284
2285 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002286 * Cache if the user padding is relative.
2287 *
2288 */
2289 @ViewDebug.ExportedProperty(category = "padding")
2290 boolean mUserPaddingRelative;
2291
2292 /**
2293 * Cache the paddingStart set by the user to append to the scrollbar's size.
2294 *
2295 */
2296 @ViewDebug.ExportedProperty(category = "padding")
2297 int mUserPaddingStart;
2298
2299 /**
2300 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2301 *
2302 */
2303 @ViewDebug.ExportedProperty(category = "padding")
2304 int mUserPaddingEnd;
2305
2306 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002307 * @hide
2308 */
2309 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2310 /**
2311 * @hide
2312 */
2313 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 private Drawable mBGDrawable;
2316
2317 private int mBackgroundResource;
2318 private boolean mBackgroundSizeChanged;
2319
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002320 static class ListenerInfo {
2321 /**
2322 * Listener used to dispatch focus change events.
2323 * This field should be made private, so it is hidden from the SDK.
2324 * {@hide}
2325 */
2326 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002328 /**
2329 * Listeners for layout change events.
2330 */
2331 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002332
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002333 /**
2334 * Listeners for attach events.
2335 */
2336 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002337
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002338 /**
2339 * Listener used to dispatch click events.
2340 * This field should be made private, so it is hidden from the SDK.
2341 * {@hide}
2342 */
2343 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002345 /**
2346 * Listener used to dispatch long click events.
2347 * This field should be made private, so it is hidden from the SDK.
2348 * {@hide}
2349 */
2350 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002352 /**
2353 * Listener used to build the context menu.
2354 * This field should be made private, so it is hidden from the SDK.
2355 * {@hide}
2356 */
2357 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002359 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002361 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002363 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002364
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002365 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002366
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002367 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002368
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002369 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2370 }
2371
2372 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 /**
2375 * The application environment this view lives in.
2376 * This field should be made private, so it is hidden from the SDK.
2377 * {@hide}
2378 */
2379 protected Context mContext;
2380
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002381 private final Resources mResources;
2382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 private ScrollabilityCache mScrollCache;
2384
2385 private int[] mDrawableState = null;
2386
Romain Guy0211a0a2011-02-14 16:34:59 -08002387 /**
2388 * Set to true when drawing cache is enabled and cannot be created.
2389 *
2390 * @hide
2391 */
2392 public boolean mCachingFailed;
2393
Romain Guy02890fd2010-08-06 17:58:44 -07002394 private Bitmap mDrawingCache;
2395 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002396 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002397 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398
2399 /**
2400 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2401 * the user may specify which view to go to next.
2402 */
2403 private int mNextFocusLeftId = View.NO_ID;
2404
2405 /**
2406 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2407 * the user may specify which view to go to next.
2408 */
2409 private int mNextFocusRightId = View.NO_ID;
2410
2411 /**
2412 * When this view has focus and the next focus is {@link #FOCUS_UP},
2413 * the user may specify which view to go to next.
2414 */
2415 private int mNextFocusUpId = View.NO_ID;
2416
2417 /**
2418 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2419 * the user may specify which view to go to next.
2420 */
2421 private int mNextFocusDownId = View.NO_ID;
2422
Jeff Brown4e6319b2010-12-13 10:36:51 -08002423 /**
2424 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2425 * the user may specify which view to go to next.
2426 */
2427 int mNextFocusForwardId = View.NO_ID;
2428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002430 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002431 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002432 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 private UnsetPressedState mUnsetPressedState;
2435
2436 /**
2437 * Whether the long press's action has been invoked. The tap's action is invoked on the
2438 * up event while a long press is invoked as soon as the long press duration is reached, so
2439 * a long press could be performed before the tap is checked, in which case the tap's action
2440 * should not be invoked.
2441 */
2442 private boolean mHasPerformedLongPress;
2443
2444 /**
2445 * The minimum height of the view. We'll try our best to have the height
2446 * of this view to at least this amount.
2447 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002448 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 private int mMinHeight;
2450
2451 /**
2452 * The minimum width of the view. We'll try our best to have the width
2453 * of this view to at least this amount.
2454 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002455 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 private int mMinWidth;
2457
2458 /**
2459 * The delegate to handle touch events that are physically in this view
2460 * but should be handled by another view.
2461 */
2462 private TouchDelegate mTouchDelegate = null;
2463
2464 /**
2465 * Solid color to use as a background when creating the drawing cache. Enables
2466 * the cache to use 16 bit bitmaps instead of 32 bit.
2467 */
2468 private int mDrawingCacheBackgroundColor = 0;
2469
2470 /**
2471 * Special tree observer used when mAttachInfo is null.
2472 */
2473 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002474
Adam Powelle14579b2009-12-16 18:39:52 -08002475 /**
2476 * Cache the touch slop from the context that created the view.
2477 */
2478 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002481 * Object that handles automatic animation of view properties.
2482 */
2483 private ViewPropertyAnimator mAnimator = null;
2484
2485 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002486 * Flag indicating that a drag can cross window boundaries. When
2487 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2488 * with this flag set, all visible applications will be able to participate
2489 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002490 *
2491 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002492 */
2493 public static final int DRAG_FLAG_GLOBAL = 1;
2494
2495 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002496 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2497 */
2498 private float mVerticalScrollFactor;
2499
2500 /**
Adam Powell20232d02010-12-08 21:08:53 -08002501 * Position of the vertical scroll bar.
2502 */
2503 private int mVerticalScrollbarPosition;
2504
2505 /**
2506 * Position the scroll bar at the default position as determined by the system.
2507 */
2508 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2509
2510 /**
2511 * Position the scroll bar along the left edge.
2512 */
2513 public static final int SCROLLBAR_POSITION_LEFT = 1;
2514
2515 /**
2516 * Position the scroll bar along the right edge.
2517 */
2518 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2519
2520 /**
Romain Guy171c5922011-01-06 10:04:23 -08002521 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002522 *
2523 * @see #getLayerType()
2524 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002525 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002526 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002527 */
2528 public static final int LAYER_TYPE_NONE = 0;
2529
2530 /**
2531 * <p>Indicates that the view has a software layer. A software layer is backed
2532 * by a bitmap and causes the view to be rendered using Android's software
2533 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002534 *
Romain Guy171c5922011-01-06 10:04:23 -08002535 * <p>Software layers have various usages:</p>
2536 * <p>When the application is not using hardware acceleration, a software layer
2537 * is useful to apply a specific color filter and/or blending mode and/or
2538 * translucency to a view and all its children.</p>
2539 * <p>When the application is using hardware acceleration, a software layer
2540 * is useful to render drawing primitives not supported by the hardware
2541 * accelerated pipeline. It can also be used to cache a complex view tree
2542 * into a texture and reduce the complexity of drawing operations. For instance,
2543 * when animating a complex view tree with a translation, a software layer can
2544 * be used to render the view tree only once.</p>
2545 * <p>Software layers should be avoided when the affected view tree updates
2546 * often. Every update will require to re-render the software layer, which can
2547 * potentially be slow (particularly when hardware acceleration is turned on
2548 * since the layer will have to be uploaded into a hardware texture after every
2549 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002550 *
2551 * @see #getLayerType()
2552 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002553 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002554 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002555 */
2556 public static final int LAYER_TYPE_SOFTWARE = 1;
2557
2558 /**
2559 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2560 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2561 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2562 * rendering pipeline, but only if hardware acceleration is turned on for the
2563 * view hierarchy. When hardware acceleration is turned off, hardware layers
2564 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002565 *
Romain Guy171c5922011-01-06 10:04:23 -08002566 * <p>A hardware layer is useful to apply a specific color filter and/or
2567 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002568 * <p>A hardware layer can be used to cache a complex view tree into a
2569 * texture and reduce the complexity of drawing operations. For instance,
2570 * when animating a complex view tree with a translation, a hardware layer can
2571 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002572 * <p>A hardware layer can also be used to increase the rendering quality when
2573 * rotation transformations are applied on a view. It can also be used to
2574 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002575 *
2576 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002577 * @see #setLayerType(int, android.graphics.Paint)
2578 * @see #LAYER_TYPE_NONE
2579 * @see #LAYER_TYPE_SOFTWARE
2580 */
2581 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002582
Romain Guy3aaff3a2011-01-12 14:18:47 -08002583 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2584 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2585 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2586 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2587 })
Romain Guy171c5922011-01-06 10:04:23 -08002588 int mLayerType = LAYER_TYPE_NONE;
2589 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002590 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002591
2592 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002593 * Set to true when the view is sending hover accessibility events because it
2594 * is the innermost hovered view.
2595 */
2596 private boolean mSendingHoverAccessibilityEvents;
2597
2598 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002599 * Delegate for injecting accessiblity functionality.
2600 */
2601 AccessibilityDelegate mAccessibilityDelegate;
2602
2603 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002604 * Text direction is inherited thru {@link ViewGroup}
2605 * @hide
2606 */
2607 public static final int TEXT_DIRECTION_INHERIT = 0;
2608
2609 /**
2610 * Text direction is using "first strong algorithm". The first strong directional character
2611 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002612 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002613 *
2614 * @hide
2615 */
2616 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2617
2618 /**
2619 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2620 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002621 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002622 *
2623 * @hide
2624 */
2625 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2626
2627 /**
2628 * Text direction is forced to LTR.
2629 *
2630 * @hide
2631 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002632 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002633
2634 /**
2635 * Text direction is forced to RTL.
2636 *
2637 * @hide
2638 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002639 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002640
2641 /**
2642 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002643 *
2644 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002645 */
2646 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2647
2648 /**
2649 * The text direction that has been defined by {@link #setTextDirection(int)}.
2650 *
2651 * {@hide}
2652 */
2653 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002654 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2655 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2656 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2657 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2658 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2659 })
Doug Feltcb3791202011-07-07 11:57:48 -07002660 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002661
2662 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002663 * The resolved text direction. This needs resolution if the value is
2664 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2665 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2666 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002667 *
2668 * {@hide}
2669 */
2670 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002671 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2672 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2673 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002674 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2675 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2676 })
Doug Feltcb3791202011-07-07 11:57:48 -07002677 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002678
2679 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002680 * Consistency verifier for debugging purposes.
2681 * @hide
2682 */
2683 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2684 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2685 new InputEventConsistencyVerifier(this, 0) : null;
2686
2687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 * Simple constructor to use when creating a view from code.
2689 *
2690 * @param context The Context the view is running in, through which it can
2691 * access the current theme, resources, etc.
2692 */
2693 public View(Context context) {
2694 mContext = context;
2695 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002696 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002697 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002698 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002699 mUserPaddingStart = -1;
2700 mUserPaddingEnd = -1;
2701 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
2703
2704 /**
2705 * Constructor that is called when inflating a view from XML. This is called
2706 * when a view is being constructed from an XML file, supplying attributes
2707 * that were specified in the XML file. This version uses a default style of
2708 * 0, so the only attribute values applied are those in the Context's Theme
2709 * and the given AttributeSet.
2710 *
2711 * <p>
2712 * The method onFinishInflate() will be called after all children have been
2713 * added.
2714 *
2715 * @param context The Context the view is running in, through which it can
2716 * access the current theme, resources, etc.
2717 * @param attrs The attributes of the XML tag that is inflating the view.
2718 * @see #View(Context, AttributeSet, int)
2719 */
2720 public View(Context context, AttributeSet attrs) {
2721 this(context, attrs, 0);
2722 }
2723
2724 /**
2725 * Perform inflation from XML and apply a class-specific base style. This
2726 * constructor of View allows subclasses to use their own base style when
2727 * they are inflating. For example, a Button class's constructor would call
2728 * this version of the super class constructor and supply
2729 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2730 * the theme's button style to modify all of the base view attributes (in
2731 * particular its background) as well as the Button class's attributes.
2732 *
2733 * @param context The Context the view is running in, through which it can
2734 * access the current theme, resources, etc.
2735 * @param attrs The attributes of the XML tag that is inflating the view.
2736 * @param defStyle The default style to apply to this view. If 0, no style
2737 * will be applied (beyond what is included in the theme). This may
2738 * either be an attribute resource, whose value will be retrieved
2739 * from the current theme, or an explicit style resource.
2740 * @see #View(Context, AttributeSet)
2741 */
2742 public View(Context context, AttributeSet attrs, int defStyle) {
2743 this(context);
2744
2745 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2746 defStyle, 0);
2747
2748 Drawable background = null;
2749
2750 int leftPadding = -1;
2751 int topPadding = -1;
2752 int rightPadding = -1;
2753 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002754 int startPadding = -1;
2755 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756
2757 int padding = -1;
2758
2759 int viewFlagValues = 0;
2760 int viewFlagMasks = 0;
2761
2762 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 int x = 0;
2765 int y = 0;
2766
Chet Haase73066682010-11-29 15:55:32 -08002767 float tx = 0;
2768 float ty = 0;
2769 float rotation = 0;
2770 float rotationX = 0;
2771 float rotationY = 0;
2772 float sx = 1f;
2773 float sy = 1f;
2774 boolean transformSet = false;
2775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2777
Adam Powell637d3372010-08-25 14:37:03 -07002778 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 final int N = a.getIndexCount();
2780 for (int i = 0; i < N; i++) {
2781 int attr = a.getIndex(i);
2782 switch (attr) {
2783 case com.android.internal.R.styleable.View_background:
2784 background = a.getDrawable(attr);
2785 break;
2786 case com.android.internal.R.styleable.View_padding:
2787 padding = a.getDimensionPixelSize(attr, -1);
2788 break;
2789 case com.android.internal.R.styleable.View_paddingLeft:
2790 leftPadding = a.getDimensionPixelSize(attr, -1);
2791 break;
2792 case com.android.internal.R.styleable.View_paddingTop:
2793 topPadding = a.getDimensionPixelSize(attr, -1);
2794 break;
2795 case com.android.internal.R.styleable.View_paddingRight:
2796 rightPadding = a.getDimensionPixelSize(attr, -1);
2797 break;
2798 case com.android.internal.R.styleable.View_paddingBottom:
2799 bottomPadding = a.getDimensionPixelSize(attr, -1);
2800 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002801 case com.android.internal.R.styleable.View_paddingStart:
2802 startPadding = a.getDimensionPixelSize(attr, -1);
2803 break;
2804 case com.android.internal.R.styleable.View_paddingEnd:
2805 endPadding = a.getDimensionPixelSize(attr, -1);
2806 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 case com.android.internal.R.styleable.View_scrollX:
2808 x = a.getDimensionPixelOffset(attr, 0);
2809 break;
2810 case com.android.internal.R.styleable.View_scrollY:
2811 y = a.getDimensionPixelOffset(attr, 0);
2812 break;
Chet Haase73066682010-11-29 15:55:32 -08002813 case com.android.internal.R.styleable.View_alpha:
2814 setAlpha(a.getFloat(attr, 1f));
2815 break;
2816 case com.android.internal.R.styleable.View_transformPivotX:
2817 setPivotX(a.getDimensionPixelOffset(attr, 0));
2818 break;
2819 case com.android.internal.R.styleable.View_transformPivotY:
2820 setPivotY(a.getDimensionPixelOffset(attr, 0));
2821 break;
2822 case com.android.internal.R.styleable.View_translationX:
2823 tx = a.getDimensionPixelOffset(attr, 0);
2824 transformSet = true;
2825 break;
2826 case com.android.internal.R.styleable.View_translationY:
2827 ty = a.getDimensionPixelOffset(attr, 0);
2828 transformSet = true;
2829 break;
2830 case com.android.internal.R.styleable.View_rotation:
2831 rotation = a.getFloat(attr, 0);
2832 transformSet = true;
2833 break;
2834 case com.android.internal.R.styleable.View_rotationX:
2835 rotationX = a.getFloat(attr, 0);
2836 transformSet = true;
2837 break;
2838 case com.android.internal.R.styleable.View_rotationY:
2839 rotationY = a.getFloat(attr, 0);
2840 transformSet = true;
2841 break;
2842 case com.android.internal.R.styleable.View_scaleX:
2843 sx = a.getFloat(attr, 1f);
2844 transformSet = true;
2845 break;
2846 case com.android.internal.R.styleable.View_scaleY:
2847 sy = a.getFloat(attr, 1f);
2848 transformSet = true;
2849 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 case com.android.internal.R.styleable.View_id:
2851 mID = a.getResourceId(attr, NO_ID);
2852 break;
2853 case com.android.internal.R.styleable.View_tag:
2854 mTag = a.getText(attr);
2855 break;
2856 case com.android.internal.R.styleable.View_fitsSystemWindows:
2857 if (a.getBoolean(attr, false)) {
2858 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2859 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2860 }
2861 break;
2862 case com.android.internal.R.styleable.View_focusable:
2863 if (a.getBoolean(attr, false)) {
2864 viewFlagValues |= FOCUSABLE;
2865 viewFlagMasks |= FOCUSABLE_MASK;
2866 }
2867 break;
2868 case com.android.internal.R.styleable.View_focusableInTouchMode:
2869 if (a.getBoolean(attr, false)) {
2870 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2871 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2872 }
2873 break;
2874 case com.android.internal.R.styleable.View_clickable:
2875 if (a.getBoolean(attr, false)) {
2876 viewFlagValues |= CLICKABLE;
2877 viewFlagMasks |= CLICKABLE;
2878 }
2879 break;
2880 case com.android.internal.R.styleable.View_longClickable:
2881 if (a.getBoolean(attr, false)) {
2882 viewFlagValues |= LONG_CLICKABLE;
2883 viewFlagMasks |= LONG_CLICKABLE;
2884 }
2885 break;
2886 case com.android.internal.R.styleable.View_saveEnabled:
2887 if (!a.getBoolean(attr, true)) {
2888 viewFlagValues |= SAVE_DISABLED;
2889 viewFlagMasks |= SAVE_DISABLED_MASK;
2890 }
2891 break;
2892 case com.android.internal.R.styleable.View_duplicateParentState:
2893 if (a.getBoolean(attr, false)) {
2894 viewFlagValues |= DUPLICATE_PARENT_STATE;
2895 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2896 }
2897 break;
2898 case com.android.internal.R.styleable.View_visibility:
2899 final int visibility = a.getInt(attr, 0);
2900 if (visibility != 0) {
2901 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2902 viewFlagMasks |= VISIBILITY_MASK;
2903 }
2904 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002905 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002906 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002907 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002908 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002909 final int layoutDirection = a.getInt(attr, -1);
2910 if (layoutDirection != -1) {
2911 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002912 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002913 // Set to default (LAYOUT_DIRECTION_INHERIT)
2914 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002915 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002916 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002917 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 case com.android.internal.R.styleable.View_drawingCacheQuality:
2919 final int cacheQuality = a.getInt(attr, 0);
2920 if (cacheQuality != 0) {
2921 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2922 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2923 }
2924 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002925 case com.android.internal.R.styleable.View_contentDescription:
2926 mContentDescription = a.getString(attr);
2927 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2929 if (!a.getBoolean(attr, true)) {
2930 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2931 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2932 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002933 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2935 if (!a.getBoolean(attr, true)) {
2936 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2937 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2938 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002939 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 case R.styleable.View_scrollbars:
2941 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2942 if (scrollbars != SCROLLBARS_NONE) {
2943 viewFlagValues |= scrollbars;
2944 viewFlagMasks |= SCROLLBARS_MASK;
2945 initializeScrollbars(a);
2946 }
2947 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002948 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002950 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2951 // Ignore the attribute starting with ICS
2952 break;
2953 }
2954 // With builds < ICS, fall through and apply fading edges
2955 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2957 if (fadingEdge != FADING_EDGE_NONE) {
2958 viewFlagValues |= fadingEdge;
2959 viewFlagMasks |= FADING_EDGE_MASK;
2960 initializeFadingEdge(a);
2961 }
2962 break;
2963 case R.styleable.View_scrollbarStyle:
2964 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2965 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2966 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2967 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2968 }
2969 break;
2970 case R.styleable.View_isScrollContainer:
2971 setScrollContainer = true;
2972 if (a.getBoolean(attr, false)) {
2973 setScrollContainer(true);
2974 }
2975 break;
2976 case com.android.internal.R.styleable.View_keepScreenOn:
2977 if (a.getBoolean(attr, false)) {
2978 viewFlagValues |= KEEP_SCREEN_ON;
2979 viewFlagMasks |= KEEP_SCREEN_ON;
2980 }
2981 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002982 case R.styleable.View_filterTouchesWhenObscured:
2983 if (a.getBoolean(attr, false)) {
2984 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2985 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2986 }
2987 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 case R.styleable.View_nextFocusLeft:
2989 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2990 break;
2991 case R.styleable.View_nextFocusRight:
2992 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2993 break;
2994 case R.styleable.View_nextFocusUp:
2995 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2996 break;
2997 case R.styleable.View_nextFocusDown:
2998 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2999 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003000 case R.styleable.View_nextFocusForward:
3001 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3002 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 case R.styleable.View_minWidth:
3004 mMinWidth = a.getDimensionPixelSize(attr, 0);
3005 break;
3006 case R.styleable.View_minHeight:
3007 mMinHeight = a.getDimensionPixelSize(attr, 0);
3008 break;
Romain Guy9a817362009-05-01 10:57:14 -07003009 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003010 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003011 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003012 + "be used within a restricted context");
3013 }
3014
Romain Guy9a817362009-05-01 10:57:14 -07003015 final String handlerName = a.getString(attr);
3016 if (handlerName != null) {
3017 setOnClickListener(new OnClickListener() {
3018 private Method mHandler;
3019
3020 public void onClick(View v) {
3021 if (mHandler == null) {
3022 try {
3023 mHandler = getContext().getClass().getMethod(handlerName,
3024 View.class);
3025 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003026 int id = getId();
3027 String idText = id == NO_ID ? "" : " with id '"
3028 + getContext().getResources().getResourceEntryName(
3029 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003030 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003031 handlerName + "(View) in the activity "
3032 + getContext().getClass() + " for onClick handler"
3033 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003034 }
3035 }
3036
3037 try {
3038 mHandler.invoke(getContext(), View.this);
3039 } catch (IllegalAccessException e) {
3040 throw new IllegalStateException("Could not execute non "
3041 + "public method of the activity", e);
3042 } catch (InvocationTargetException e) {
3043 throw new IllegalStateException("Could not execute "
3044 + "method of the activity", e);
3045 }
3046 }
3047 });
3048 }
3049 break;
Adam Powell637d3372010-08-25 14:37:03 -07003050 case R.styleable.View_overScrollMode:
3051 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3052 break;
Adam Powell20232d02010-12-08 21:08:53 -08003053 case R.styleable.View_verticalScrollbarPosition:
3054 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3055 break;
Romain Guy171c5922011-01-06 10:04:23 -08003056 case R.styleable.View_layerType:
3057 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3058 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003059 case R.styleable.View_textDirection:
3060 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3061 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 }
3063 }
3064
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003065 a.recycle();
3066
Adam Powell637d3372010-08-25 14:37:03 -07003067 setOverScrollMode(overScrollMode);
3068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 if (background != null) {
3070 setBackgroundDrawable(background);
3071 }
3072
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003073 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3074
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003075 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3076 // layout direction). Those cached values will be used later during padding resolution.
3077 mUserPaddingStart = startPadding;
3078 mUserPaddingEnd = endPadding;
3079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 if (padding >= 0) {
3081 leftPadding = padding;
3082 topPadding = padding;
3083 rightPadding = padding;
3084 bottomPadding = padding;
3085 }
3086
3087 // If the user specified the padding (either with android:padding or
3088 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3089 // use the default padding or the padding from the background drawable
3090 // (stored at this point in mPadding*)
3091 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3092 topPadding >= 0 ? topPadding : mPaddingTop,
3093 rightPadding >= 0 ? rightPadding : mPaddingRight,
3094 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3095
3096 if (viewFlagMasks != 0) {
3097 setFlags(viewFlagValues, viewFlagMasks);
3098 }
3099
3100 // Needs to be called after mViewFlags is set
3101 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3102 recomputePadding();
3103 }
3104
3105 if (x != 0 || y != 0) {
3106 scrollTo(x, y);
3107 }
3108
Chet Haase73066682010-11-29 15:55:32 -08003109 if (transformSet) {
3110 setTranslationX(tx);
3111 setTranslationY(ty);
3112 setRotation(rotation);
3113 setRotationX(rotationX);
3114 setRotationY(rotationY);
3115 setScaleX(sx);
3116 setScaleY(sy);
3117 }
3118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3120 setScrollContainer(true);
3121 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003122
3123 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 }
3125
3126 /**
3127 * Non-public constructor for use in testing
3128 */
3129 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003130 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 }
3132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 /**
3134 * <p>
3135 * Initializes the fading edges from a given set of styled attributes. This
3136 * method should be called by subclasses that need fading edges and when an
3137 * instance of these subclasses is created programmatically rather than
3138 * being inflated from XML. This method is automatically called when the XML
3139 * is inflated.
3140 * </p>
3141 *
3142 * @param a the styled attributes set to initialize the fading edges from
3143 */
3144 protected void initializeFadingEdge(TypedArray a) {
3145 initScrollCache();
3146
3147 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3148 R.styleable.View_fadingEdgeLength,
3149 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3150 }
3151
3152 /**
3153 * Returns the size of the vertical faded edges used to indicate that more
3154 * content in this view is visible.
3155 *
3156 * @return The size in pixels of the vertical faded edge or 0 if vertical
3157 * faded edges are not enabled for this view.
3158 * @attr ref android.R.styleable#View_fadingEdgeLength
3159 */
3160 public int getVerticalFadingEdgeLength() {
3161 if (isVerticalFadingEdgeEnabled()) {
3162 ScrollabilityCache cache = mScrollCache;
3163 if (cache != null) {
3164 return cache.fadingEdgeLength;
3165 }
3166 }
3167 return 0;
3168 }
3169
3170 /**
3171 * Set the size of the faded edge used to indicate that more content in this
3172 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003173 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3174 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3175 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 *
3177 * @param length The size in pixels of the faded edge used to indicate that more
3178 * content in this view is visible.
3179 */
3180 public void setFadingEdgeLength(int length) {
3181 initScrollCache();
3182 mScrollCache.fadingEdgeLength = length;
3183 }
3184
3185 /**
3186 * Returns the size of the horizontal faded edges used to indicate that more
3187 * content in this view is visible.
3188 *
3189 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3190 * faded edges are not enabled for this view.
3191 * @attr ref android.R.styleable#View_fadingEdgeLength
3192 */
3193 public int getHorizontalFadingEdgeLength() {
3194 if (isHorizontalFadingEdgeEnabled()) {
3195 ScrollabilityCache cache = mScrollCache;
3196 if (cache != null) {
3197 return cache.fadingEdgeLength;
3198 }
3199 }
3200 return 0;
3201 }
3202
3203 /**
3204 * Returns the width of the vertical scrollbar.
3205 *
3206 * @return The width in pixels of the vertical scrollbar or 0 if there
3207 * is no vertical scrollbar.
3208 */
3209 public int getVerticalScrollbarWidth() {
3210 ScrollabilityCache cache = mScrollCache;
3211 if (cache != null) {
3212 ScrollBarDrawable scrollBar = cache.scrollBar;
3213 if (scrollBar != null) {
3214 int size = scrollBar.getSize(true);
3215 if (size <= 0) {
3216 size = cache.scrollBarSize;
3217 }
3218 return size;
3219 }
3220 return 0;
3221 }
3222 return 0;
3223 }
3224
3225 /**
3226 * Returns the height of the horizontal scrollbar.
3227 *
3228 * @return The height in pixels of the horizontal scrollbar or 0 if
3229 * there is no horizontal scrollbar.
3230 */
3231 protected int getHorizontalScrollbarHeight() {
3232 ScrollabilityCache cache = mScrollCache;
3233 if (cache != null) {
3234 ScrollBarDrawable scrollBar = cache.scrollBar;
3235 if (scrollBar != null) {
3236 int size = scrollBar.getSize(false);
3237 if (size <= 0) {
3238 size = cache.scrollBarSize;
3239 }
3240 return size;
3241 }
3242 return 0;
3243 }
3244 return 0;
3245 }
3246
3247 /**
3248 * <p>
3249 * Initializes the scrollbars from a given set of styled attributes. This
3250 * method should be called by subclasses that need scrollbars and when an
3251 * instance of these subclasses is created programmatically rather than
3252 * being inflated from XML. This method is automatically called when the XML
3253 * is inflated.
3254 * </p>
3255 *
3256 * @param a the styled attributes set to initialize the scrollbars from
3257 */
3258 protected void initializeScrollbars(TypedArray a) {
3259 initScrollCache();
3260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003262
Mike Cleronf116bf82009-09-27 19:14:12 -07003263 if (scrollabilityCache.scrollBar == null) {
3264 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3265 }
Joe Malin32736f02011-01-19 16:14:20 -08003266
Romain Guy8bda2482010-03-02 11:42:11 -08003267 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268
Mike Cleronf116bf82009-09-27 19:14:12 -07003269 if (!fadeScrollbars) {
3270 scrollabilityCache.state = ScrollabilityCache.ON;
3271 }
3272 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003273
3274
Mike Cleronf116bf82009-09-27 19:14:12 -07003275 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3276 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3277 .getScrollBarFadeDuration());
3278 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3279 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3280 ViewConfiguration.getScrollDefaultDelay());
3281
Joe Malin32736f02011-01-19 16:14:20 -08003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3284 com.android.internal.R.styleable.View_scrollbarSize,
3285 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3286
3287 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3288 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3289
3290 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3291 if (thumb != null) {
3292 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3293 }
3294
3295 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3296 false);
3297 if (alwaysDraw) {
3298 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3299 }
3300
3301 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3302 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3303
3304 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3305 if (thumb != null) {
3306 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3307 }
3308
3309 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3310 false);
3311 if (alwaysDraw) {
3312 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3313 }
3314
3315 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003316 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 }
3318
3319 /**
3320 * <p>
3321 * Initalizes the scrollability cache if necessary.
3322 * </p>
3323 */
3324 private void initScrollCache() {
3325 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003326 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 }
3328 }
3329
3330 /**
Adam Powell20232d02010-12-08 21:08:53 -08003331 * Set the position of the vertical scroll bar. Should be one of
3332 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3333 * {@link #SCROLLBAR_POSITION_RIGHT}.
3334 *
3335 * @param position Where the vertical scroll bar should be positioned.
3336 */
3337 public void setVerticalScrollbarPosition(int position) {
3338 if (mVerticalScrollbarPosition != position) {
3339 mVerticalScrollbarPosition = position;
3340 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003341 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003342 }
3343 }
3344
3345 /**
3346 * @return The position where the vertical scroll bar will show, if applicable.
3347 * @see #setVerticalScrollbarPosition(int)
3348 */
3349 public int getVerticalScrollbarPosition() {
3350 return mVerticalScrollbarPosition;
3351 }
3352
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003353 ListenerInfo getListenerInfo() {
3354 if (mListenerInfo != null) {
3355 return mListenerInfo;
3356 }
3357 mListenerInfo = new ListenerInfo();
3358 return mListenerInfo;
3359 }
3360
Adam Powell20232d02010-12-08 21:08:53 -08003361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 * Register a callback to be invoked when focus of this view changed.
3363 *
3364 * @param l The callback that will run.
3365 */
3366 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003367 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 }
3369
3370 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003371 * Add a listener that will be called when the bounds of the view change due to
3372 * layout processing.
3373 *
3374 * @param listener The listener that will be called when layout bounds change.
3375 */
3376 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003377 ListenerInfo li = getListenerInfo();
3378 if (li.mOnLayoutChangeListeners == null) {
3379 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003380 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003381 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3382 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003383 }
Chet Haase21cd1382010-09-01 17:42:29 -07003384 }
3385
3386 /**
3387 * Remove a listener for layout changes.
3388 *
3389 * @param listener The listener for layout bounds change.
3390 */
3391 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003392 ListenerInfo li = mListenerInfo;
3393 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003394 return;
3395 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003396 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003397 }
3398
3399 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003400 * Add a listener for attach state changes.
3401 *
3402 * This listener will be called whenever this view is attached or detached
3403 * from a window. Remove the listener using
3404 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3405 *
3406 * @param listener Listener to attach
3407 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3408 */
3409 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003410 ListenerInfo li = getListenerInfo();
3411 if (li.mOnAttachStateChangeListeners == null) {
3412 li.mOnAttachStateChangeListeners
3413 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003414 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003415 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003416 }
3417
3418 /**
3419 * Remove a listener for attach state changes. The listener will receive no further
3420 * notification of window attach/detach events.
3421 *
3422 * @param listener Listener to remove
3423 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3424 */
3425 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003426 ListenerInfo li = mListenerInfo;
3427 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003428 return;
3429 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003430 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003431 }
3432
3433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 * Returns the focus-change callback registered for this view.
3435 *
3436 * @return The callback, or null if one is not registered.
3437 */
3438 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003439 ListenerInfo li = mListenerInfo;
3440 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 }
3442
3443 /**
3444 * Register a callback to be invoked when this view is clicked. If this view is not
3445 * clickable, it becomes clickable.
3446 *
3447 * @param l The callback that will run
3448 *
3449 * @see #setClickable(boolean)
3450 */
3451 public void setOnClickListener(OnClickListener l) {
3452 if (!isClickable()) {
3453 setClickable(true);
3454 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003455 getListenerInfo().mOnClickListener = l;
3456 }
3457
3458 /**
3459 * Return whether this view has an attached OnClickListener. Returns
3460 * true if there is a listener, false if there is none.
3461 */
3462 public boolean hasOnClickListeners() {
3463 ListenerInfo li = mListenerInfo;
3464 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 }
3466
3467 /**
3468 * Register a callback to be invoked when this view is clicked and held. If this view is not
3469 * long clickable, it becomes long clickable.
3470 *
3471 * @param l The callback that will run
3472 *
3473 * @see #setLongClickable(boolean)
3474 */
3475 public void setOnLongClickListener(OnLongClickListener l) {
3476 if (!isLongClickable()) {
3477 setLongClickable(true);
3478 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003479 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 }
3481
3482 /**
3483 * Register a callback to be invoked when the context menu for this view is
3484 * being built. If this view is not long clickable, it becomes long clickable.
3485 *
3486 * @param l The callback that will run
3487 *
3488 */
3489 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3490 if (!isLongClickable()) {
3491 setLongClickable(true);
3492 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003493 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495
3496 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003497 * Call this view's OnClickListener, if it is defined. Performs all normal
3498 * actions associated with clicking: reporting accessibility event, playing
3499 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 *
3501 * @return True there was an assigned OnClickListener that was called, false
3502 * otherwise is returned.
3503 */
3504 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003505 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3506
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003507 ListenerInfo li = mListenerInfo;
3508 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003510 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 return true;
3512 }
3513
3514 return false;
3515 }
3516
3517 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003518 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3519 * this only calls the listener, and does not do any associated clicking
3520 * actions like reporting an accessibility event.
3521 *
3522 * @return True there was an assigned OnClickListener that was called, false
3523 * otherwise is returned.
3524 */
3525 public boolean callOnClick() {
3526 ListenerInfo li = mListenerInfo;
3527 if (li != null && li.mOnClickListener != null) {
3528 li.mOnClickListener.onClick(this);
3529 return true;
3530 }
3531 return false;
3532 }
3533
3534 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003535 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3536 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003538 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 */
3540 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003541 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003544 ListenerInfo li = mListenerInfo;
3545 if (li != null && li.mOnLongClickListener != null) {
3546 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
3548 if (!handled) {
3549 handled = showContextMenu();
3550 }
3551 if (handled) {
3552 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3553 }
3554 return handled;
3555 }
3556
3557 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003558 * Performs button-related actions during a touch down event.
3559 *
3560 * @param event The event.
3561 * @return True if the down was consumed.
3562 *
3563 * @hide
3564 */
3565 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3566 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3567 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3568 return true;
3569 }
3570 }
3571 return false;
3572 }
3573
3574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 * Bring up the context menu for this view.
3576 *
3577 * @return Whether a context menu was displayed.
3578 */
3579 public boolean showContextMenu() {
3580 return getParent().showContextMenuForChild(this);
3581 }
3582
3583 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003584 * Bring up the context menu for this view, referring to the item under the specified point.
3585 *
3586 * @param x The referenced x coordinate.
3587 * @param y The referenced y coordinate.
3588 * @param metaState The keyboard modifiers that were pressed.
3589 * @return Whether a context menu was displayed.
3590 *
3591 * @hide
3592 */
3593 public boolean showContextMenu(float x, float y, int metaState) {
3594 return showContextMenu();
3595 }
3596
3597 /**
Adam Powell6e346362010-07-23 10:18:23 -07003598 * Start an action mode.
3599 *
3600 * @param callback Callback that will control the lifecycle of the action mode
3601 * @return The new action mode if it is started, null otherwise
3602 *
3603 * @see ActionMode
3604 */
3605 public ActionMode startActionMode(ActionMode.Callback callback) {
3606 return getParent().startActionModeForChild(this, callback);
3607 }
3608
3609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 * Register a callback to be invoked when a key is pressed in this view.
3611 * @param l the key listener to attach to this view
3612 */
3613 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003614 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 }
3616
3617 /**
3618 * Register a callback to be invoked when a touch event is sent to this view.
3619 * @param l the touch listener to attach to this view
3620 */
3621 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003622 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 }
3624
3625 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003626 * Register a callback to be invoked when a generic motion event is sent to this view.
3627 * @param l the generic motion listener to attach to this view
3628 */
3629 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003630 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003631 }
3632
3633 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003634 * Register a callback to be invoked when a hover event is sent to this view.
3635 * @param l the hover listener to attach to this view
3636 */
3637 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003638 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003639 }
3640
3641 /**
Joe Malin32736f02011-01-19 16:14:20 -08003642 * Register a drag event listener callback object for this View. The parameter is
3643 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3644 * View, the system calls the
3645 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3646 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003647 */
3648 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003649 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003650 }
3651
3652 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003653 * Give this view focus. This will cause
3654 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 *
3656 * Note: this does not check whether this {@link View} should get focus, it just
3657 * gives it focus no matter what. It should only be called internally by framework
3658 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3659 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003660 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3661 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 * focus moved when requestFocus() is called. It may not always
3663 * apply, in which case use the default View.FOCUS_DOWN.
3664 * @param previouslyFocusedRect The rectangle of the view that had focus
3665 * prior in this View's coordinate system.
3666 */
3667 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3668 if (DBG) {
3669 System.out.println(this + " requestFocus()");
3670 }
3671
3672 if ((mPrivateFlags & FOCUSED) == 0) {
3673 mPrivateFlags |= FOCUSED;
3674
3675 if (mParent != null) {
3676 mParent.requestChildFocus(this, this);
3677 }
3678
3679 onFocusChanged(true, direction, previouslyFocusedRect);
3680 refreshDrawableState();
3681 }
3682 }
3683
3684 /**
3685 * Request that a rectangle of this view be visible on the screen,
3686 * scrolling if necessary just enough.
3687 *
3688 * <p>A View should call this if it maintains some notion of which part
3689 * of its content is interesting. For example, a text editing view
3690 * should call this when its cursor moves.
3691 *
3692 * @param rectangle The rectangle.
3693 * @return Whether any parent scrolled.
3694 */
3695 public boolean requestRectangleOnScreen(Rect rectangle) {
3696 return requestRectangleOnScreen(rectangle, false);
3697 }
3698
3699 /**
3700 * Request that a rectangle of this view be visible on the screen,
3701 * scrolling if necessary just enough.
3702 *
3703 * <p>A View should call this if it maintains some notion of which part
3704 * of its content is interesting. For example, a text editing view
3705 * should call this when its cursor moves.
3706 *
3707 * <p>When <code>immediate</code> is set to true, scrolling will not be
3708 * animated.
3709 *
3710 * @param rectangle The rectangle.
3711 * @param immediate True to forbid animated scrolling, false otherwise
3712 * @return Whether any parent scrolled.
3713 */
3714 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3715 View child = this;
3716 ViewParent parent = mParent;
3717 boolean scrolled = false;
3718 while (parent != null) {
3719 scrolled |= parent.requestChildRectangleOnScreen(child,
3720 rectangle, immediate);
3721
3722 // offset rect so next call has the rectangle in the
3723 // coordinate system of its direct child.
3724 rectangle.offset(child.getLeft(), child.getTop());
3725 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3726
3727 if (!(parent instanceof View)) {
3728 break;
3729 }
Romain Guy8506ab42009-06-11 17:35:47 -07003730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 child = (View) parent;
3732 parent = child.getParent();
3733 }
3734 return scrolled;
3735 }
3736
3737 /**
3738 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003739 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 */
3741 public void clearFocus() {
3742 if (DBG) {
3743 System.out.println(this + " clearFocus()");
3744 }
3745
3746 if ((mPrivateFlags & FOCUSED) != 0) {
3747 mPrivateFlags &= ~FOCUSED;
3748
3749 if (mParent != null) {
3750 mParent.clearChildFocus(this);
3751 }
3752
3753 onFocusChanged(false, 0, null);
3754 refreshDrawableState();
3755 }
3756 }
3757
3758 /**
3759 * Called to clear the focus of a view that is about to be removed.
3760 * Doesn't call clearChildFocus, which prevents this view from taking
3761 * focus again before it has been removed from the parent
3762 */
3763 void clearFocusForRemoval() {
3764 if ((mPrivateFlags & FOCUSED) != 0) {
3765 mPrivateFlags &= ~FOCUSED;
3766
3767 onFocusChanged(false, 0, null);
3768 refreshDrawableState();
3769 }
3770 }
3771
3772 /**
3773 * Called internally by the view system when a new view is getting focus.
3774 * This is what clears the old focus.
3775 */
3776 void unFocus() {
3777 if (DBG) {
3778 System.out.println(this + " unFocus()");
3779 }
3780
3781 if ((mPrivateFlags & FOCUSED) != 0) {
3782 mPrivateFlags &= ~FOCUSED;
3783
3784 onFocusChanged(false, 0, null);
3785 refreshDrawableState();
3786 }
3787 }
3788
3789 /**
3790 * Returns true if this view has focus iteself, or is the ancestor of the
3791 * view that has focus.
3792 *
3793 * @return True if this view has or contains focus, false otherwise.
3794 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003795 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 public boolean hasFocus() {
3797 return (mPrivateFlags & FOCUSED) != 0;
3798 }
3799
3800 /**
3801 * Returns true if this view is focusable or if it contains a reachable View
3802 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3803 * is a View whose parents do not block descendants focus.
3804 *
3805 * Only {@link #VISIBLE} views are considered focusable.
3806 *
3807 * @return True if the view is focusable or if the view contains a focusable
3808 * View, false otherwise.
3809 *
3810 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3811 */
3812 public boolean hasFocusable() {
3813 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3814 }
3815
3816 /**
3817 * Called by the view system when the focus state of this view changes.
3818 * When the focus change event is caused by directional navigation, direction
3819 * and previouslyFocusedRect provide insight into where the focus is coming from.
3820 * When overriding, be sure to call up through to the super class so that
3821 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 * @param gainFocus True if the View has focus; false otherwise.
3824 * @param direction The direction focus has moved when requestFocus()
3825 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003826 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3827 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3828 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3830 * system, of the previously focused view. If applicable, this will be
3831 * passed in as finer grained information about where the focus is coming
3832 * from (in addition to direction). Will be <code>null</code> otherwise.
3833 */
3834 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003835 if (gainFocus) {
3836 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3837 }
3838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 InputMethodManager imm = InputMethodManager.peekInstance();
3840 if (!gainFocus) {
3841 if (isPressed()) {
3842 setPressed(false);
3843 }
3844 if (imm != null && mAttachInfo != null
3845 && mAttachInfo.mHasWindowFocus) {
3846 imm.focusOut(this);
3847 }
Romain Guya2431d02009-04-30 16:30:00 -07003848 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 } else if (imm != null && mAttachInfo != null
3850 && mAttachInfo.mHasWindowFocus) {
3851 imm.focusIn(this);
3852 }
Romain Guy8506ab42009-06-11 17:35:47 -07003853
Romain Guy0fd89bf2011-01-26 15:41:30 -08003854 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003855 ListenerInfo li = mListenerInfo;
3856 if (li != null && li.mOnFocusChangeListener != null) {
3857 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 }
Joe Malin32736f02011-01-19 16:14:20 -08003859
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003860 if (mAttachInfo != null) {
3861 mAttachInfo.mKeyDispatchState.reset(this);
3862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
3864
3865 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003866 * Sends an accessibility event of the given type. If accessiiblity is
3867 * not enabled this method has no effect. The default implementation calls
3868 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3869 * to populate information about the event source (this View), then calls
3870 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3871 * populate the text content of the event source including its descendants,
3872 * and last calls
3873 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3874 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003875 * <p>
3876 * If an {@link AccessibilityDelegate} has been specified via calling
3877 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3878 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3879 * responsible for handling this call.
3880 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003881 *
Scott Mainb303d832011-10-12 16:45:18 -07003882 * @param eventType The type of the event to send, as defined by several types from
3883 * {@link android.view.accessibility.AccessibilityEvent}, such as
3884 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3885 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003886 *
3887 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3888 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3889 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003890 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003891 */
3892 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003893 if (mAccessibilityDelegate != null) {
3894 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3895 } else {
3896 sendAccessibilityEventInternal(eventType);
3897 }
3898 }
3899
3900 /**
3901 * @see #sendAccessibilityEvent(int)
3902 *
3903 * Note: Called from the default {@link AccessibilityDelegate}.
3904 */
3905 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003906 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3907 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3908 }
3909 }
3910
3911 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003912 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3913 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003914 * perform a check whether accessibility is enabled.
3915 * <p>
3916 * If an {@link AccessibilityDelegate} has been specified via calling
3917 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3918 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3919 * is responsible for handling this call.
3920 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003921 *
3922 * @param event The event to send.
3923 *
3924 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003925 */
3926 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003927 if (mAccessibilityDelegate != null) {
3928 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3929 } else {
3930 sendAccessibilityEventUncheckedInternal(event);
3931 }
3932 }
3933
3934 /**
3935 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3936 *
3937 * Note: Called from the default {@link AccessibilityDelegate}.
3938 */
3939 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003940 if (!isShown()) {
3941 return;
3942 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003943 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003944 // Only a subset of accessibility events populates text content.
3945 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3946 dispatchPopulateAccessibilityEvent(event);
3947 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003948 // In the beginning we called #isShown(), so we know that getParent() is not null.
3949 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003950 }
3951
3952 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003953 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3954 * to its children for adding their text content to the event. Note that the
3955 * event text is populated in a separate dispatch path since we add to the
3956 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003957 * A typical implementation will call
3958 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3959 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3960 * on each child. Override this method if custom population of the event text
3961 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003962 * <p>
3963 * If an {@link AccessibilityDelegate} has been specified via calling
3964 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3965 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3966 * is responsible for handling this call.
3967 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003968 * <p>
3969 * <em>Note:</em> Accessibility events of certain types are not dispatched for
3970 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
3971 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003972 *
3973 * @param event The event.
3974 *
3975 * @return True if the event population was completed.
3976 */
3977 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003978 if (mAccessibilityDelegate != null) {
3979 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3980 } else {
3981 return dispatchPopulateAccessibilityEventInternal(event);
3982 }
3983 }
3984
3985 /**
3986 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3987 *
3988 * Note: Called from the default {@link AccessibilityDelegate}.
3989 */
3990 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003991 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003992 return false;
3993 }
3994
3995 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003996 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003997 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07003998 * text content. While this method is free to modify event
3999 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004000 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4001 * <p>
4002 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004003 * to the text added by the super implementation:
4004 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004005 * super.onPopulateAccessibilityEvent(event);
4006 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4007 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4008 * mCurrentDate.getTimeInMillis(), flags);
4009 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004010 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004011 * <p>
4012 * If an {@link AccessibilityDelegate} has been specified via calling
4013 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4014 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4015 * is responsible for handling this call.
4016 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004017 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4018 * information to the event, in case the default implementation has basic information to add.
4019 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004020 *
4021 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004022 *
4023 * @see #sendAccessibilityEvent(int)
4024 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004025 */
4026 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004027 if (mAccessibilityDelegate != null) {
4028 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4029 } else {
4030 onPopulateAccessibilityEventInternal(event);
4031 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004032 }
4033
4034 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004035 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4036 *
4037 * Note: Called from the default {@link AccessibilityDelegate}.
4038 */
4039 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4040
4041 }
4042
4043 /**
4044 * Initializes an {@link AccessibilityEvent} with information about
4045 * this View which is the event source. In other words, the source of
4046 * an accessibility event is the view whose state change triggered firing
4047 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004048 * <p>
4049 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004050 * to properties set by the super implementation:
4051 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4052 * super.onInitializeAccessibilityEvent(event);
4053 * event.setPassword(true);
4054 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004055 * <p>
4056 * If an {@link AccessibilityDelegate} has been specified via calling
4057 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4058 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4059 * is responsible for handling this call.
4060 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004061 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4062 * information to the event, in case the default implementation has basic information to add.
4063 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004064 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004065 *
4066 * @see #sendAccessibilityEvent(int)
4067 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4068 */
4069 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004070 if (mAccessibilityDelegate != null) {
4071 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4072 } else {
4073 onInitializeAccessibilityEventInternal(event);
4074 }
4075 }
4076
4077 /**
4078 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4079 *
4080 * Note: Called from the default {@link AccessibilityDelegate}.
4081 */
4082 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004083 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07004084 event.setClassName(getClass().getName());
4085 event.setPackageName(getContext().getPackageName());
4086 event.setEnabled(isEnabled());
4087 event.setContentDescription(mContentDescription);
4088
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004089 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4090 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4091 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4092 FOCUSABLES_ALL);
4093 event.setItemCount(focusablesTempList.size());
4094 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4095 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004096 }
4097 }
4098
4099 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004100 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4101 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4102 * This method is responsible for obtaining an accessibility node info from a
4103 * pool of reusable instances and calling
4104 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4105 * initialize the former.
4106 * <p>
4107 * Note: The client is responsible for recycling the obtained instance by calling
4108 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4109 * </p>
4110 * @return A populated {@link AccessibilityNodeInfo}.
4111 *
4112 * @see AccessibilityNodeInfo
4113 */
4114 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4115 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4116 onInitializeAccessibilityNodeInfo(info);
4117 return info;
4118 }
4119
4120 /**
4121 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4122 * The base implementation sets:
4123 * <ul>
4124 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004125 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4126 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004127 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4128 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4129 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4130 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4131 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4132 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4133 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4134 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4135 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4136 * </ul>
4137 * <p>
4138 * Subclasses should override this method, call the super implementation,
4139 * and set additional attributes.
4140 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004141 * <p>
4142 * If an {@link AccessibilityDelegate} has been specified via calling
4143 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4144 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4145 * is responsible for handling this call.
4146 * </p>
4147 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004148 * @param info The instance to initialize.
4149 */
4150 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004151 if (mAccessibilityDelegate != null) {
4152 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4153 } else {
4154 onInitializeAccessibilityNodeInfoInternal(info);
4155 }
4156 }
4157
4158 /**
4159 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4160 *
4161 * Note: Called from the default {@link AccessibilityDelegate}.
4162 */
4163 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004164 Rect bounds = mAttachInfo.mTmpInvalRect;
4165 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004166 info.setBoundsInParent(bounds);
4167
4168 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4169 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004170 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004171 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4172 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004173
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004174 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4175 ViewParent parent = getParent();
4176 if (parent instanceof View) {
4177 View parentView = (View) parent;
4178 info.setParent(parentView);
4179 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004180 }
4181
4182 info.setPackageName(mContext.getPackageName());
4183 info.setClassName(getClass().getName());
4184 info.setContentDescription(getContentDescription());
4185
4186 info.setEnabled(isEnabled());
4187 info.setClickable(isClickable());
4188 info.setFocusable(isFocusable());
4189 info.setFocused(isFocused());
4190 info.setSelected(isSelected());
4191 info.setLongClickable(isLongClickable());
4192
4193 // TODO: These make sense only if we are in an AdapterView but all
4194 // views can be selected. Maybe from accessiiblity perspective
4195 // we should report as selectable view in an AdapterView.
4196 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4197 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4198
4199 if (isFocusable()) {
4200 if (isFocused()) {
4201 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4202 } else {
4203 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4204 }
4205 }
4206 }
4207
4208 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004209 * Sets a delegate for implementing accessibility support via compositon as
4210 * opposed to inheritance. The delegate's primary use is for implementing
4211 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4212 *
4213 * @param delegate The delegate instance.
4214 *
4215 * @see AccessibilityDelegate
4216 */
4217 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4218 mAccessibilityDelegate = delegate;
4219 }
4220
4221 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004222 * Gets the unique identifier of this view on the screen for accessibility purposes.
4223 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4224 *
4225 * @return The view accessibility id.
4226 *
4227 * @hide
4228 */
4229 public int getAccessibilityViewId() {
4230 if (mAccessibilityViewId == NO_ID) {
4231 mAccessibilityViewId = sNextAccessibilityViewId++;
4232 }
4233 return mAccessibilityViewId;
4234 }
4235
4236 /**
4237 * Gets the unique identifier of the window in which this View reseides.
4238 *
4239 * @return The window accessibility id.
4240 *
4241 * @hide
4242 */
4243 public int getAccessibilityWindowId() {
4244 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4245 }
4246
4247 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004248 * Gets the {@link View} description. It briefly describes the view and is
4249 * primarily used for accessibility support. Set this property to enable
4250 * better accessibility support for your application. This is especially
4251 * true for views that do not have textual representation (For example,
4252 * ImageButton).
4253 *
4254 * @return The content descriptiopn.
4255 *
4256 * @attr ref android.R.styleable#View_contentDescription
4257 */
4258 public CharSequence getContentDescription() {
4259 return mContentDescription;
4260 }
4261
4262 /**
4263 * Sets the {@link View} description. It briefly describes the view and is
4264 * primarily used for accessibility support. Set this property to enable
4265 * better accessibility support for your application. This is especially
4266 * true for views that do not have textual representation (For example,
4267 * ImageButton).
4268 *
4269 * @param contentDescription The content description.
4270 *
4271 * @attr ref android.R.styleable#View_contentDescription
4272 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004273 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004274 public void setContentDescription(CharSequence contentDescription) {
4275 mContentDescription = contentDescription;
4276 }
4277
4278 /**
Romain Guya2431d02009-04-30 16:30:00 -07004279 * Invoked whenever this view loses focus, either by losing window focus or by losing
4280 * focus within its window. This method can be used to clear any state tied to the
4281 * focus. For instance, if a button is held pressed with the trackball and the window
4282 * loses focus, this method can be used to cancel the press.
4283 *
4284 * Subclasses of View overriding this method should always call super.onFocusLost().
4285 *
4286 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004287 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004288 *
4289 * @hide pending API council approval
4290 */
4291 protected void onFocusLost() {
4292 resetPressedState();
4293 }
4294
4295 private void resetPressedState() {
4296 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4297 return;
4298 }
4299
4300 if (isPressed()) {
4301 setPressed(false);
4302
4303 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004304 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004305 }
4306 }
4307 }
4308
4309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 * Returns true if this view has focus
4311 *
4312 * @return True if this view has focus, false otherwise.
4313 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004314 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 public boolean isFocused() {
4316 return (mPrivateFlags & FOCUSED) != 0;
4317 }
4318
4319 /**
4320 * Find the view in the hierarchy rooted at this view that currently has
4321 * focus.
4322 *
4323 * @return The view that currently has focus, or null if no focused view can
4324 * be found.
4325 */
4326 public View findFocus() {
4327 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4328 }
4329
4330 /**
4331 * Change whether this view is one of the set of scrollable containers in
4332 * its window. This will be used to determine whether the window can
4333 * resize or must pan when a soft input area is open -- scrollable
4334 * containers allow the window to use resize mode since the container
4335 * will appropriately shrink.
4336 */
4337 public void setScrollContainer(boolean isScrollContainer) {
4338 if (isScrollContainer) {
4339 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4340 mAttachInfo.mScrollContainers.add(this);
4341 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4342 }
4343 mPrivateFlags |= SCROLL_CONTAINER;
4344 } else {
4345 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4346 mAttachInfo.mScrollContainers.remove(this);
4347 }
4348 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4349 }
4350 }
4351
4352 /**
4353 * Returns the quality of the drawing cache.
4354 *
4355 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4356 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4357 *
4358 * @see #setDrawingCacheQuality(int)
4359 * @see #setDrawingCacheEnabled(boolean)
4360 * @see #isDrawingCacheEnabled()
4361 *
4362 * @attr ref android.R.styleable#View_drawingCacheQuality
4363 */
4364 public int getDrawingCacheQuality() {
4365 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4366 }
4367
4368 /**
4369 * Set the drawing cache quality of this view. This value is used only when the
4370 * drawing cache is enabled
4371 *
4372 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4373 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4374 *
4375 * @see #getDrawingCacheQuality()
4376 * @see #setDrawingCacheEnabled(boolean)
4377 * @see #isDrawingCacheEnabled()
4378 *
4379 * @attr ref android.R.styleable#View_drawingCacheQuality
4380 */
4381 public void setDrawingCacheQuality(int quality) {
4382 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4383 }
4384
4385 /**
4386 * Returns whether the screen should remain on, corresponding to the current
4387 * value of {@link #KEEP_SCREEN_ON}.
4388 *
4389 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4390 *
4391 * @see #setKeepScreenOn(boolean)
4392 *
4393 * @attr ref android.R.styleable#View_keepScreenOn
4394 */
4395 public boolean getKeepScreenOn() {
4396 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4397 }
4398
4399 /**
4400 * Controls whether the screen should remain on, modifying the
4401 * value of {@link #KEEP_SCREEN_ON}.
4402 *
4403 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4404 *
4405 * @see #getKeepScreenOn()
4406 *
4407 * @attr ref android.R.styleable#View_keepScreenOn
4408 */
4409 public void setKeepScreenOn(boolean keepScreenOn) {
4410 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4411 }
4412
4413 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004414 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4415 * @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 -08004416 *
4417 * @attr ref android.R.styleable#View_nextFocusLeft
4418 */
4419 public int getNextFocusLeftId() {
4420 return mNextFocusLeftId;
4421 }
4422
4423 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004424 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4425 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4426 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 *
4428 * @attr ref android.R.styleable#View_nextFocusLeft
4429 */
4430 public void setNextFocusLeftId(int nextFocusLeftId) {
4431 mNextFocusLeftId = nextFocusLeftId;
4432 }
4433
4434 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004435 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4436 * @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 -08004437 *
4438 * @attr ref android.R.styleable#View_nextFocusRight
4439 */
4440 public int getNextFocusRightId() {
4441 return mNextFocusRightId;
4442 }
4443
4444 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004445 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4446 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4447 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 *
4449 * @attr ref android.R.styleable#View_nextFocusRight
4450 */
4451 public void setNextFocusRightId(int nextFocusRightId) {
4452 mNextFocusRightId = nextFocusRightId;
4453 }
4454
4455 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004456 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4457 * @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 -08004458 *
4459 * @attr ref android.R.styleable#View_nextFocusUp
4460 */
4461 public int getNextFocusUpId() {
4462 return mNextFocusUpId;
4463 }
4464
4465 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004466 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4467 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4468 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 *
4470 * @attr ref android.R.styleable#View_nextFocusUp
4471 */
4472 public void setNextFocusUpId(int nextFocusUpId) {
4473 mNextFocusUpId = nextFocusUpId;
4474 }
4475
4476 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004477 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4478 * @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 -08004479 *
4480 * @attr ref android.R.styleable#View_nextFocusDown
4481 */
4482 public int getNextFocusDownId() {
4483 return mNextFocusDownId;
4484 }
4485
4486 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004487 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4488 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4489 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 *
4491 * @attr ref android.R.styleable#View_nextFocusDown
4492 */
4493 public void setNextFocusDownId(int nextFocusDownId) {
4494 mNextFocusDownId = nextFocusDownId;
4495 }
4496
4497 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004498 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4499 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4500 *
4501 * @attr ref android.R.styleable#View_nextFocusForward
4502 */
4503 public int getNextFocusForwardId() {
4504 return mNextFocusForwardId;
4505 }
4506
4507 /**
4508 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4509 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4510 * decide automatically.
4511 *
4512 * @attr ref android.R.styleable#View_nextFocusForward
4513 */
4514 public void setNextFocusForwardId(int nextFocusForwardId) {
4515 mNextFocusForwardId = nextFocusForwardId;
4516 }
4517
4518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 * Returns the visibility of this view and all of its ancestors
4520 *
4521 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4522 */
4523 public boolean isShown() {
4524 View current = this;
4525 //noinspection ConstantConditions
4526 do {
4527 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4528 return false;
4529 }
4530 ViewParent parent = current.mParent;
4531 if (parent == null) {
4532 return false; // We are not attached to the view root
4533 }
4534 if (!(parent instanceof View)) {
4535 return true;
4536 }
4537 current = (View) parent;
4538 } while (current != null);
4539
4540 return false;
4541 }
4542
4543 /**
4544 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4545 * is set
4546 *
4547 * @param insets Insets for system windows
4548 *
4549 * @return True if this view applied the insets, false otherwise
4550 */
4551 protected boolean fitSystemWindows(Rect insets) {
4552 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4553 mPaddingLeft = insets.left;
4554 mPaddingTop = insets.top;
4555 mPaddingRight = insets.right;
4556 mPaddingBottom = insets.bottom;
4557 requestLayout();
4558 return true;
4559 }
4560 return false;
4561 }
4562
4563 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004564 * Set whether or not this view should account for system screen decorations
4565 * such as the status bar and inset its content. This allows this view to be
4566 * positioned in absolute screen coordinates and remain visible to the user.
4567 *
4568 * <p>This should only be used by top-level window decor views.
4569 *
4570 * @param fitSystemWindows true to inset content for system screen decorations, false for
4571 * default behavior.
4572 *
4573 * @attr ref android.R.styleable#View_fitsSystemWindows
4574 */
4575 public void setFitsSystemWindows(boolean fitSystemWindows) {
4576 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4577 }
4578
4579 /**
4580 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4581 * will account for system screen decorations such as the status bar and inset its
4582 * content. This allows the view to be positioned in absolute screen coordinates
4583 * and remain visible to the user.
4584 *
4585 * @return true if this view will adjust its content bounds for system screen decorations.
4586 *
4587 * @attr ref android.R.styleable#View_fitsSystemWindows
4588 */
4589 public boolean fitsSystemWindows() {
4590 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4591 }
4592
4593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 * Returns the visibility status for this view.
4595 *
4596 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4597 * @attr ref android.R.styleable#View_visibility
4598 */
4599 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004600 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4601 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4602 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 })
4604 public int getVisibility() {
4605 return mViewFlags & VISIBILITY_MASK;
4606 }
4607
4608 /**
4609 * Set the enabled state of this view.
4610 *
4611 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4612 * @attr ref android.R.styleable#View_visibility
4613 */
4614 @RemotableViewMethod
4615 public void setVisibility(int visibility) {
4616 setFlags(visibility, VISIBILITY_MASK);
4617 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4618 }
4619
4620 /**
4621 * Returns the enabled status for this view. The interpretation of the
4622 * enabled state varies by subclass.
4623 *
4624 * @return True if this view is enabled, false otherwise.
4625 */
4626 @ViewDebug.ExportedProperty
4627 public boolean isEnabled() {
4628 return (mViewFlags & ENABLED_MASK) == ENABLED;
4629 }
4630
4631 /**
4632 * Set the enabled state of this view. The interpretation of the enabled
4633 * state varies by subclass.
4634 *
4635 * @param enabled True if this view is enabled, false otherwise.
4636 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004637 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004639 if (enabled == isEnabled()) return;
4640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4642
4643 /*
4644 * The View most likely has to change its appearance, so refresh
4645 * the drawable state.
4646 */
4647 refreshDrawableState();
4648
4649 // Invalidate too, since the default behavior for views is to be
4650 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004651 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 }
4653
4654 /**
4655 * Set whether this view can receive the focus.
4656 *
4657 * Setting this to false will also ensure that this view is not focusable
4658 * in touch mode.
4659 *
4660 * @param focusable If true, this view can receive the focus.
4661 *
4662 * @see #setFocusableInTouchMode(boolean)
4663 * @attr ref android.R.styleable#View_focusable
4664 */
4665 public void setFocusable(boolean focusable) {
4666 if (!focusable) {
4667 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4668 }
4669 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4670 }
4671
4672 /**
4673 * Set whether this view can receive focus while in touch mode.
4674 *
4675 * Setting this to true will also ensure that this view is focusable.
4676 *
4677 * @param focusableInTouchMode If true, this view can receive the focus while
4678 * in touch mode.
4679 *
4680 * @see #setFocusable(boolean)
4681 * @attr ref android.R.styleable#View_focusableInTouchMode
4682 */
4683 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4684 // Focusable in touch mode should always be set before the focusable flag
4685 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4686 // which, in touch mode, will not successfully request focus on this view
4687 // because the focusable in touch mode flag is not set
4688 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4689 if (focusableInTouchMode) {
4690 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4691 }
4692 }
4693
4694 /**
4695 * Set whether this view should have sound effects enabled for events such as
4696 * clicking and touching.
4697 *
4698 * <p>You may wish to disable sound effects for a view if you already play sounds,
4699 * for instance, a dial key that plays dtmf tones.
4700 *
4701 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4702 * @see #isSoundEffectsEnabled()
4703 * @see #playSoundEffect(int)
4704 * @attr ref android.R.styleable#View_soundEffectsEnabled
4705 */
4706 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4707 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4708 }
4709
4710 /**
4711 * @return whether this view should have sound effects enabled for events such as
4712 * clicking and touching.
4713 *
4714 * @see #setSoundEffectsEnabled(boolean)
4715 * @see #playSoundEffect(int)
4716 * @attr ref android.R.styleable#View_soundEffectsEnabled
4717 */
4718 @ViewDebug.ExportedProperty
4719 public boolean isSoundEffectsEnabled() {
4720 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4721 }
4722
4723 /**
4724 * Set whether this view should have haptic feedback for events such as
4725 * long presses.
4726 *
4727 * <p>You may wish to disable haptic feedback if your view already controls
4728 * its own haptic feedback.
4729 *
4730 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4731 * @see #isHapticFeedbackEnabled()
4732 * @see #performHapticFeedback(int)
4733 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4734 */
4735 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4736 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4737 }
4738
4739 /**
4740 * @return whether this view should have haptic feedback enabled for events
4741 * long presses.
4742 *
4743 * @see #setHapticFeedbackEnabled(boolean)
4744 * @see #performHapticFeedback(int)
4745 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4746 */
4747 @ViewDebug.ExportedProperty
4748 public boolean isHapticFeedbackEnabled() {
4749 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4750 }
4751
4752 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004753 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004754 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004755 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4756 * {@link #LAYOUT_DIRECTION_RTL},
4757 * {@link #LAYOUT_DIRECTION_INHERIT} or
4758 * {@link #LAYOUT_DIRECTION_LOCALE}.
4759 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004760 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004761 * @hide
4762 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004763 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004764 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4765 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4766 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4767 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004768 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004769 public int getLayoutDirection() {
4770 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004771 }
4772
4773 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004774 * Set the layout direction for this view. This will propagate a reset of layout direction
4775 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004776 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004777 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4778 * {@link #LAYOUT_DIRECTION_RTL},
4779 * {@link #LAYOUT_DIRECTION_INHERIT} or
4780 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004781 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004782 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004783 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004784 * @hide
4785 */
4786 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004787 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004788 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004789 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004790 // Setting the flag will also request a layout.
4791 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4792 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004793 }
4794
4795 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004796 * Returns the resolved layout direction for this view.
4797 *
4798 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4799 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4800 *
4801 * @hide
4802 */
4803 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4804 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4805 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4806 })
4807 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004808 resolveLayoutDirectionIfNeeded();
4809 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004810 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4811 }
4812
4813 /**
4814 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4815 * layout attribute and/or the inherited value from the parent.</p>
4816 *
4817 * @return true if the layout is right-to-left.
4818 *
4819 * @hide
4820 */
4821 @ViewDebug.ExportedProperty(category = "layout")
4822 public boolean isLayoutRtl() {
4823 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4824 }
4825
4826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 * If this view doesn't do any drawing on its own, set this flag to
4828 * allow further optimizations. By default, this flag is not set on
4829 * View, but could be set on some View subclasses such as ViewGroup.
4830 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004831 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4832 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 *
4834 * @param willNotDraw whether or not this View draw on its own
4835 */
4836 public void setWillNotDraw(boolean willNotDraw) {
4837 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4838 }
4839
4840 /**
4841 * Returns whether or not this View draws on its own.
4842 *
4843 * @return true if this view has nothing to draw, false otherwise
4844 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004845 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 public boolean willNotDraw() {
4847 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4848 }
4849
4850 /**
4851 * When a View's drawing cache is enabled, drawing is redirected to an
4852 * offscreen bitmap. Some views, like an ImageView, must be able to
4853 * bypass this mechanism if they already draw a single bitmap, to avoid
4854 * unnecessary usage of the memory.
4855 *
4856 * @param willNotCacheDrawing true if this view does not cache its
4857 * drawing, false otherwise
4858 */
4859 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4860 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4861 }
4862
4863 /**
4864 * Returns whether or not this View can cache its drawing or not.
4865 *
4866 * @return true if this view does not cache its drawing, false otherwise
4867 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004868 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 public boolean willNotCacheDrawing() {
4870 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4871 }
4872
4873 /**
4874 * Indicates whether this view reacts to click events or not.
4875 *
4876 * @return true if the view is clickable, false otherwise
4877 *
4878 * @see #setClickable(boolean)
4879 * @attr ref android.R.styleable#View_clickable
4880 */
4881 @ViewDebug.ExportedProperty
4882 public boolean isClickable() {
4883 return (mViewFlags & CLICKABLE) == CLICKABLE;
4884 }
4885
4886 /**
4887 * Enables or disables click events for this view. When a view
4888 * is clickable it will change its state to "pressed" on every click.
4889 * Subclasses should set the view clickable to visually react to
4890 * user's clicks.
4891 *
4892 * @param clickable true to make the view clickable, false otherwise
4893 *
4894 * @see #isClickable()
4895 * @attr ref android.R.styleable#View_clickable
4896 */
4897 public void setClickable(boolean clickable) {
4898 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4899 }
4900
4901 /**
4902 * Indicates whether this view reacts to long click events or not.
4903 *
4904 * @return true if the view is long clickable, false otherwise
4905 *
4906 * @see #setLongClickable(boolean)
4907 * @attr ref android.R.styleable#View_longClickable
4908 */
4909 public boolean isLongClickable() {
4910 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4911 }
4912
4913 /**
4914 * Enables or disables long click events for this view. When a view is long
4915 * clickable it reacts to the user holding down the button for a longer
4916 * duration than a tap. This event can either launch the listener or a
4917 * context menu.
4918 *
4919 * @param longClickable true to make the view long clickable, false otherwise
4920 * @see #isLongClickable()
4921 * @attr ref android.R.styleable#View_longClickable
4922 */
4923 public void setLongClickable(boolean longClickable) {
4924 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4925 }
4926
4927 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004928 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 *
4930 * @see #isClickable()
4931 * @see #setClickable(boolean)
4932 *
4933 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4934 * the View's internal state from a previously set "pressed" state.
4935 */
4936 public void setPressed(boolean pressed) {
4937 if (pressed) {
4938 mPrivateFlags |= PRESSED;
4939 } else {
4940 mPrivateFlags &= ~PRESSED;
4941 }
4942 refreshDrawableState();
4943 dispatchSetPressed(pressed);
4944 }
4945
4946 /**
4947 * Dispatch setPressed to all of this View's children.
4948 *
4949 * @see #setPressed(boolean)
4950 *
4951 * @param pressed The new pressed state
4952 */
4953 protected void dispatchSetPressed(boolean pressed) {
4954 }
4955
4956 /**
4957 * Indicates whether the view is currently in pressed state. Unless
4958 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4959 * the pressed state.
4960 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004961 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 * @see #isClickable()
4963 * @see #setClickable(boolean)
4964 *
4965 * @return true if the view is currently pressed, false otherwise
4966 */
4967 public boolean isPressed() {
4968 return (mPrivateFlags & PRESSED) == PRESSED;
4969 }
4970
4971 /**
4972 * Indicates whether this view will save its state (that is,
4973 * whether its {@link #onSaveInstanceState} method will be called).
4974 *
4975 * @return Returns true if the view state saving is enabled, else false.
4976 *
4977 * @see #setSaveEnabled(boolean)
4978 * @attr ref android.R.styleable#View_saveEnabled
4979 */
4980 public boolean isSaveEnabled() {
4981 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4982 }
4983
4984 /**
4985 * Controls whether the saving of this view's state is
4986 * enabled (that is, whether its {@link #onSaveInstanceState} method
4987 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004988 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 * for its state to be saved. This flag can only disable the
4990 * saving of this view; any child views may still have their state saved.
4991 *
4992 * @param enabled Set to false to <em>disable</em> state saving, or true
4993 * (the default) to allow it.
4994 *
4995 * @see #isSaveEnabled()
4996 * @see #setId(int)
4997 * @see #onSaveInstanceState()
4998 * @attr ref android.R.styleable#View_saveEnabled
4999 */
5000 public void setSaveEnabled(boolean enabled) {
5001 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5002 }
5003
Jeff Brown85a31762010-09-01 17:01:00 -07005004 /**
5005 * Gets whether the framework should discard touches when the view's
5006 * window is obscured by another visible window.
5007 * Refer to the {@link View} security documentation for more details.
5008 *
5009 * @return True if touch filtering is enabled.
5010 *
5011 * @see #setFilterTouchesWhenObscured(boolean)
5012 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5013 */
5014 @ViewDebug.ExportedProperty
5015 public boolean getFilterTouchesWhenObscured() {
5016 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5017 }
5018
5019 /**
5020 * Sets whether the framework should discard touches when the view's
5021 * window is obscured by another visible window.
5022 * Refer to the {@link View} security documentation for more details.
5023 *
5024 * @param enabled True if touch filtering should be enabled.
5025 *
5026 * @see #getFilterTouchesWhenObscured
5027 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5028 */
5029 public void setFilterTouchesWhenObscured(boolean enabled) {
5030 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5031 FILTER_TOUCHES_WHEN_OBSCURED);
5032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033
5034 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005035 * Indicates whether the entire hierarchy under this view will save its
5036 * state when a state saving traversal occurs from its parent. The default
5037 * is true; if false, these views will not be saved unless
5038 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5039 *
5040 * @return Returns true if the view state saving from parent is enabled, else false.
5041 *
5042 * @see #setSaveFromParentEnabled(boolean)
5043 */
5044 public boolean isSaveFromParentEnabled() {
5045 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5046 }
5047
5048 /**
5049 * Controls whether the entire hierarchy under this view will save its
5050 * state when a state saving traversal occurs from its parent. The default
5051 * is true; if false, these views will not be saved unless
5052 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5053 *
5054 * @param enabled Set to false to <em>disable</em> state saving, or true
5055 * (the default) to allow it.
5056 *
5057 * @see #isSaveFromParentEnabled()
5058 * @see #setId(int)
5059 * @see #onSaveInstanceState()
5060 */
5061 public void setSaveFromParentEnabled(boolean enabled) {
5062 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5063 }
5064
5065
5066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 * Returns whether this View is able to take focus.
5068 *
5069 * @return True if this view can take focus, or false otherwise.
5070 * @attr ref android.R.styleable#View_focusable
5071 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005072 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 public final boolean isFocusable() {
5074 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5075 }
5076
5077 /**
5078 * When a view is focusable, it may not want to take focus when in touch mode.
5079 * For example, a button would like focus when the user is navigating via a D-pad
5080 * so that the user can click on it, but once the user starts touching the screen,
5081 * the button shouldn't take focus
5082 * @return Whether the view is focusable in touch mode.
5083 * @attr ref android.R.styleable#View_focusableInTouchMode
5084 */
5085 @ViewDebug.ExportedProperty
5086 public final boolean isFocusableInTouchMode() {
5087 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5088 }
5089
5090 /**
5091 * Find the nearest view in the specified direction that can take focus.
5092 * This does not actually give focus to that view.
5093 *
5094 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5095 *
5096 * @return The nearest focusable in the specified direction, or null if none
5097 * can be found.
5098 */
5099 public View focusSearch(int direction) {
5100 if (mParent != null) {
5101 return mParent.focusSearch(this, direction);
5102 } else {
5103 return null;
5104 }
5105 }
5106
5107 /**
5108 * This method is the last chance for the focused view and its ancestors to
5109 * respond to an arrow key. This is called when the focused view did not
5110 * consume the key internally, nor could the view system find a new view in
5111 * the requested direction to give focus to.
5112 *
5113 * @param focused The currently focused view.
5114 * @param direction The direction focus wants to move. One of FOCUS_UP,
5115 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5116 * @return True if the this view consumed this unhandled move.
5117 */
5118 public boolean dispatchUnhandledMove(View focused, int direction) {
5119 return false;
5120 }
5121
5122 /**
5123 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005124 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005126 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5127 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 * @return The user specified next view, or null if there is none.
5129 */
5130 View findUserSetNextFocus(View root, int direction) {
5131 switch (direction) {
5132 case FOCUS_LEFT:
5133 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005134 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 case FOCUS_RIGHT:
5136 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005137 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 case FOCUS_UP:
5139 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005140 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 case FOCUS_DOWN:
5142 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005143 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005144 case FOCUS_FORWARD:
5145 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005146 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005147 case FOCUS_BACKWARD: {
5148 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005149 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005150 @Override
5151 public boolean apply(View t) {
5152 return t.mNextFocusForwardId == id;
5153 }
5154 });
5155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 }
5157 return null;
5158 }
5159
Jeff Brown4dfbec22011-08-15 14:55:37 -07005160 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5161 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5162 @Override
5163 public boolean apply(View t) {
5164 return t.mID == childViewId;
5165 }
5166 });
5167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 if (result == null) {
5169 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5170 + "by user for id " + childViewId);
5171 }
5172 return result;
5173 }
5174
5175 /**
5176 * Find and return all focusable views that are descendants of this view,
5177 * possibly including this view if it is focusable itself.
5178 *
5179 * @param direction The direction of the focus
5180 * @return A list of focusable views
5181 */
5182 public ArrayList<View> getFocusables(int direction) {
5183 ArrayList<View> result = new ArrayList<View>(24);
5184 addFocusables(result, direction);
5185 return result;
5186 }
5187
5188 /**
5189 * Add any focusable views that are descendants of this view (possibly
5190 * including this view if it is focusable itself) to views. If we are in touch mode,
5191 * only add views that are also focusable in touch mode.
5192 *
5193 * @param views Focusable views found so far
5194 * @param direction The direction of the focus
5195 */
5196 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005197 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199
svetoslavganov75986cf2009-05-14 22:28:01 -07005200 /**
5201 * Adds any focusable views that are descendants of this view (possibly
5202 * including this view if it is focusable itself) to views. This method
5203 * adds all focusable views regardless if we are in touch mode or
5204 * only views focusable in touch mode if we are in touch mode depending on
5205 * the focusable mode paramater.
5206 *
5207 * @param views Focusable views found so far or null if all we are interested is
5208 * the number of focusables.
5209 * @param direction The direction of the focus.
5210 * @param focusableMode The type of focusables to be added.
5211 *
5212 * @see #FOCUSABLES_ALL
5213 * @see #FOCUSABLES_TOUCH_MODE
5214 */
5215 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5216 if (!isFocusable()) {
5217 return;
5218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219
svetoslavganov75986cf2009-05-14 22:28:01 -07005220 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5221 isInTouchMode() && !isFocusableInTouchMode()) {
5222 return;
5223 }
5224
5225 if (views != null) {
5226 views.add(this);
5227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 }
5229
5230 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005231 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005232 * The search is performed by either the text that the View renders or the content
5233 * description that describes the view for accessibility purposes and the view does
5234 * not render or both. Clients can specify how the search is to be performed via
5235 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5236 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005237 *
5238 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005239 * @param searched The text to match against.
5240 *
5241 * @see #FIND_VIEWS_WITH_TEXT
5242 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5243 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005244 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005245 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5246 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5247 && !TextUtils.isEmpty(mContentDescription)) {
5248 String searchedLowerCase = searched.toString().toLowerCase();
5249 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5250 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5251 outViews.add(this);
5252 }
5253 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005254 }
5255
5256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 * Find and return all touchable views that are descendants of this view,
5258 * possibly including this view if it is touchable itself.
5259 *
5260 * @return A list of touchable views
5261 */
5262 public ArrayList<View> getTouchables() {
5263 ArrayList<View> result = new ArrayList<View>();
5264 addTouchables(result);
5265 return result;
5266 }
5267
5268 /**
5269 * Add any touchable views that are descendants of this view (possibly
5270 * including this view if it is touchable itself) to views.
5271 *
5272 * @param views Touchable views found so far
5273 */
5274 public void addTouchables(ArrayList<View> views) {
5275 final int viewFlags = mViewFlags;
5276
5277 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5278 && (viewFlags & ENABLED_MASK) == ENABLED) {
5279 views.add(this);
5280 }
5281 }
5282
5283 /**
5284 * Call this to try to give focus to a specific view or to one of its
5285 * descendants.
5286 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005287 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5288 * false), or if it is focusable and it is not focusable in touch mode
5289 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005291 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 * have focus, and you want your parent to look for the next one.
5293 *
5294 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5295 * {@link #FOCUS_DOWN} and <code>null</code>.
5296 *
5297 * @return Whether this view or one of its descendants actually took focus.
5298 */
5299 public final boolean requestFocus() {
5300 return requestFocus(View.FOCUS_DOWN);
5301 }
5302
5303
5304 /**
5305 * Call this to try to give focus to a specific view or to one of its
5306 * descendants and give it a hint about what direction focus is heading.
5307 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005308 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5309 * false), or if it is focusable and it is not focusable in touch mode
5310 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005312 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 * have focus, and you want your parent to look for the next one.
5314 *
5315 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5316 * <code>null</code> set for the previously focused rectangle.
5317 *
5318 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5319 * @return Whether this view or one of its descendants actually took focus.
5320 */
5321 public final boolean requestFocus(int direction) {
5322 return requestFocus(direction, null);
5323 }
5324
5325 /**
5326 * Call this to try to give focus to a specific view or to one of its descendants
5327 * and give it hints about the direction and a specific rectangle that the focus
5328 * is coming from. The rectangle can help give larger views a finer grained hint
5329 * about where focus is coming from, and therefore, where to show selection, or
5330 * forward focus change internally.
5331 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005332 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5333 * false), or if it is focusable and it is not focusable in touch mode
5334 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 *
5336 * A View will not take focus if it is not visible.
5337 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005338 * A View will not take focus if one of its parents has
5339 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5340 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005342 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 * have focus, and you want your parent to look for the next one.
5344 *
5345 * You may wish to override this method if your custom {@link View} has an internal
5346 * {@link View} that it wishes to forward the request to.
5347 *
5348 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5349 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5350 * to give a finer grained hint about where focus is coming from. May be null
5351 * if there is no hint.
5352 * @return Whether this view or one of its descendants actually took focus.
5353 */
5354 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5355 // need to be focusable
5356 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5357 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5358 return false;
5359 }
5360
5361 // need to be focusable in touch mode if in touch mode
5362 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005363 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5364 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 }
5366
5367 // need to not have any parents blocking us
5368 if (hasAncestorThatBlocksDescendantFocus()) {
5369 return false;
5370 }
5371
5372 handleFocusGainInternal(direction, previouslyFocusedRect);
5373 return true;
5374 }
5375
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005376 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005377 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005378 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005379 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005380 }
5381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 /**
5383 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5384 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5385 * touch mode to request focus when they are touched.
5386 *
5387 * @return Whether this view or one of its descendants actually took focus.
5388 *
5389 * @see #isInTouchMode()
5390 *
5391 */
5392 public final boolean requestFocusFromTouch() {
5393 // Leave touch mode if we need to
5394 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005395 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005396 if (viewRoot != null) {
5397 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 }
5399 }
5400 return requestFocus(View.FOCUS_DOWN);
5401 }
5402
5403 /**
5404 * @return Whether any ancestor of this view blocks descendant focus.
5405 */
5406 private boolean hasAncestorThatBlocksDescendantFocus() {
5407 ViewParent ancestor = mParent;
5408 while (ancestor instanceof ViewGroup) {
5409 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5410 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5411 return true;
5412 } else {
5413 ancestor = vgAncestor.getParent();
5414 }
5415 }
5416 return false;
5417 }
5418
5419 /**
Romain Guya440b002010-02-24 15:57:54 -08005420 * @hide
5421 */
5422 public void dispatchStartTemporaryDetach() {
5423 onStartTemporaryDetach();
5424 }
5425
5426 /**
5427 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5429 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005430 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 */
5432 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005433 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005434 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005435 }
5436
5437 /**
5438 * @hide
5439 */
5440 public void dispatchFinishTemporaryDetach() {
5441 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 }
Romain Guy8506ab42009-06-11 17:35:47 -07005443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 /**
5445 * Called after {@link #onStartTemporaryDetach} when the container is done
5446 * changing the view.
5447 */
5448 public void onFinishTemporaryDetach() {
5449 }
Romain Guy8506ab42009-06-11 17:35:47 -07005450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005452 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5453 * for this view's window. Returns null if the view is not currently attached
5454 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005455 * just use the standard high-level event callbacks like
5456 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005457 */
5458 public KeyEvent.DispatcherState getKeyDispatcherState() {
5459 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5460 }
Joe Malin32736f02011-01-19 16:14:20 -08005461
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 * Dispatch a key event before it is processed by any input method
5464 * associated with the view hierarchy. This can be used to intercept
5465 * key events in special situations before the IME consumes them; a
5466 * typical example would be handling the BACK key to update the application's
5467 * UI instead of allowing the IME to see it and close itself.
5468 *
5469 * @param event The key event to be dispatched.
5470 * @return True if the event was handled, false otherwise.
5471 */
5472 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5473 return onKeyPreIme(event.getKeyCode(), event);
5474 }
5475
5476 /**
5477 * Dispatch a key event to the next view on the focus path. This path runs
5478 * from the top of the view tree down to the currently focused view. If this
5479 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5480 * the next node down the focus path. This method also fires any key
5481 * listeners.
5482 *
5483 * @param event The key event to be dispatched.
5484 * @return True if the event was handled, false otherwise.
5485 */
5486 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005487 if (mInputEventConsistencyVerifier != null) {
5488 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490
Jeff Brown21bc5c92011-02-28 18:27:14 -08005491 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005492 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005493 ListenerInfo li = mListenerInfo;
5494 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5495 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 return true;
5497 }
5498
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005499 if (event.dispatch(this, mAttachInfo != null
5500 ? mAttachInfo.mKeyDispatchState : null, this)) {
5501 return true;
5502 }
5503
5504 if (mInputEventConsistencyVerifier != null) {
5505 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5506 }
5507 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 }
5509
5510 /**
5511 * Dispatches a key shortcut event.
5512 *
5513 * @param event The key event to be dispatched.
5514 * @return True if the event was handled by the view, false otherwise.
5515 */
5516 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5517 return onKeyShortcut(event.getKeyCode(), event);
5518 }
5519
5520 /**
5521 * Pass the touch screen motion event down to the target view, or this
5522 * view if it is the target.
5523 *
5524 * @param event The motion event to be dispatched.
5525 * @return True if the event was handled by the view, false otherwise.
5526 */
5527 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005528 if (mInputEventConsistencyVerifier != null) {
5529 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5530 }
5531
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005532 if (onFilterTouchEventForSecurity(event)) {
5533 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005534 ListenerInfo li = mListenerInfo;
5535 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5536 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005537 return true;
5538 }
5539
5540 if (onTouchEvent(event)) {
5541 return true;
5542 }
Jeff Brown85a31762010-09-01 17:01:00 -07005543 }
5544
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005545 if (mInputEventConsistencyVerifier != null) {
5546 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005548 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 }
5550
5551 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005552 * Filter the touch event to apply security policies.
5553 *
5554 * @param event The motion event to be filtered.
5555 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005556 *
Jeff Brown85a31762010-09-01 17:01:00 -07005557 * @see #getFilterTouchesWhenObscured
5558 */
5559 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005560 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005561 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5562 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5563 // Window is obscured, drop this touch.
5564 return false;
5565 }
5566 return true;
5567 }
5568
5569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 * Pass a trackball motion event down to the focused view.
5571 *
5572 * @param event The motion event to be dispatched.
5573 * @return True if the event was handled by the view, false otherwise.
5574 */
5575 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005576 if (mInputEventConsistencyVerifier != null) {
5577 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5578 }
5579
Romain Guy02ccac62011-06-24 13:20:23 -07005580 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 }
5582
5583 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005584 * Dispatch a generic motion event.
5585 * <p>
5586 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5587 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005588 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005589 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005590 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005591 *
5592 * @param event The motion event to be dispatched.
5593 * @return True if the event was handled by the view, false otherwise.
5594 */
5595 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005596 if (mInputEventConsistencyVerifier != null) {
5597 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5598 }
5599
Jeff Browna032cc02011-03-07 16:56:21 -08005600 final int source = event.getSource();
5601 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5602 final int action = event.getAction();
5603 if (action == MotionEvent.ACTION_HOVER_ENTER
5604 || action == MotionEvent.ACTION_HOVER_MOVE
5605 || action == MotionEvent.ACTION_HOVER_EXIT) {
5606 if (dispatchHoverEvent(event)) {
5607 return true;
5608 }
5609 } else if (dispatchGenericPointerEvent(event)) {
5610 return true;
5611 }
5612 } else if (dispatchGenericFocusedEvent(event)) {
5613 return true;
5614 }
5615
Jeff Brown10b62902011-06-20 16:40:37 -07005616 if (dispatchGenericMotionEventInternal(event)) {
5617 return true;
5618 }
5619
5620 if (mInputEventConsistencyVerifier != null) {
5621 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5622 }
5623 return false;
5624 }
5625
5626 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005627 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005628 ListenerInfo li = mListenerInfo;
5629 if (li != null && li.mOnGenericMotionListener != null
5630 && (mViewFlags & ENABLED_MASK) == ENABLED
5631 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005632 return true;
5633 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005634
5635 if (onGenericMotionEvent(event)) {
5636 return true;
5637 }
5638
5639 if (mInputEventConsistencyVerifier != null) {
5640 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5641 }
5642 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005643 }
5644
5645 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005646 * Dispatch a hover event.
5647 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005648 * Do not call this method directly.
5649 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005650 * </p>
5651 *
5652 * @param event The motion event to be dispatched.
5653 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005654 */
5655 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005656 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005657 ListenerInfo li = mListenerInfo;
5658 if (li != null && li.mOnHoverListener != null
5659 && (mViewFlags & ENABLED_MASK) == ENABLED
5660 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005661 return true;
5662 }
5663
Jeff Browna032cc02011-03-07 16:56:21 -08005664 return onHoverEvent(event);
5665 }
5666
5667 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005668 * Returns true if the view has a child to which it has recently sent
5669 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5670 * it does not have a hovered child, then it must be the innermost hovered view.
5671 * @hide
5672 */
5673 protected boolean hasHoveredChild() {
5674 return false;
5675 }
5676
5677 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005678 * Dispatch a generic motion event to the view under the first pointer.
5679 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005680 * Do not call this method directly.
5681 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005682 * </p>
5683 *
5684 * @param event The motion event to be dispatched.
5685 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005686 */
5687 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5688 return false;
5689 }
5690
5691 /**
5692 * Dispatch a generic motion event to the currently focused view.
5693 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005694 * Do not call this method directly.
5695 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005696 * </p>
5697 *
5698 * @param event The motion event to be dispatched.
5699 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005700 */
5701 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5702 return false;
5703 }
5704
5705 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005706 * Dispatch a pointer event.
5707 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005708 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5709 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5710 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005711 * and should not be expected to handle other pointing device features.
5712 * </p>
5713 *
5714 * @param event The motion event to be dispatched.
5715 * @return True if the event was handled by the view, false otherwise.
5716 * @hide
5717 */
5718 public final boolean dispatchPointerEvent(MotionEvent event) {
5719 if (event.isTouchEvent()) {
5720 return dispatchTouchEvent(event);
5721 } else {
5722 return dispatchGenericMotionEvent(event);
5723 }
5724 }
5725
5726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 * Called when the window containing this view gains or loses window focus.
5728 * ViewGroups should override to route to their children.
5729 *
5730 * @param hasFocus True if the window containing this view now has focus,
5731 * false otherwise.
5732 */
5733 public void dispatchWindowFocusChanged(boolean hasFocus) {
5734 onWindowFocusChanged(hasFocus);
5735 }
5736
5737 /**
5738 * Called when the window containing this view gains or loses focus. Note
5739 * that this is separate from view focus: to receive key events, both
5740 * your view and its window must have focus. If a window is displayed
5741 * on top of yours that takes input focus, then your own window will lose
5742 * focus but the view focus will remain unchanged.
5743 *
5744 * @param hasWindowFocus True if the window containing this view now has
5745 * focus, false otherwise.
5746 */
5747 public void onWindowFocusChanged(boolean hasWindowFocus) {
5748 InputMethodManager imm = InputMethodManager.peekInstance();
5749 if (!hasWindowFocus) {
5750 if (isPressed()) {
5751 setPressed(false);
5752 }
5753 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5754 imm.focusOut(this);
5755 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005756 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005757 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005758 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5760 imm.focusIn(this);
5761 }
5762 refreshDrawableState();
5763 }
5764
5765 /**
5766 * Returns true if this view is in a window that currently has window focus.
5767 * Note that this is not the same as the view itself having focus.
5768 *
5769 * @return True if this view is in a window that currently has window focus.
5770 */
5771 public boolean hasWindowFocus() {
5772 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5773 }
5774
5775 /**
Adam Powell326d8082009-12-09 15:10:07 -08005776 * Dispatch a view visibility change down the view hierarchy.
5777 * ViewGroups should override to route to their children.
5778 * @param changedView The view whose visibility changed. Could be 'this' or
5779 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005780 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5781 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005782 */
5783 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5784 onVisibilityChanged(changedView, visibility);
5785 }
5786
5787 /**
5788 * Called when the visibility of the view or an ancestor of the view is changed.
5789 * @param changedView The view whose visibility changed. Could be 'this' or
5790 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005791 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5792 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005793 */
5794 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005795 if (visibility == VISIBLE) {
5796 if (mAttachInfo != null) {
5797 initialAwakenScrollBars();
5798 } else {
5799 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5800 }
5801 }
Adam Powell326d8082009-12-09 15:10:07 -08005802 }
5803
5804 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005805 * Dispatch a hint about whether this view is displayed. For instance, when
5806 * a View moves out of the screen, it might receives a display hint indicating
5807 * the view is not displayed. Applications should not <em>rely</em> on this hint
5808 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005809 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005810 * @param hint A hint about whether or not this view is displayed:
5811 * {@link #VISIBLE} or {@link #INVISIBLE}.
5812 */
5813 public void dispatchDisplayHint(int hint) {
5814 onDisplayHint(hint);
5815 }
5816
5817 /**
5818 * Gives this view a hint about whether is displayed or not. For instance, when
5819 * a View moves out of the screen, it might receives a display hint indicating
5820 * the view is not displayed. Applications should not <em>rely</em> on this hint
5821 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005822 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005823 * @param hint A hint about whether or not this view is displayed:
5824 * {@link #VISIBLE} or {@link #INVISIBLE}.
5825 */
5826 protected void onDisplayHint(int hint) {
5827 }
5828
5829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 * Dispatch a window visibility change down the view hierarchy.
5831 * ViewGroups should override to route to their children.
5832 *
5833 * @param visibility The new visibility of the window.
5834 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005835 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 */
5837 public void dispatchWindowVisibilityChanged(int visibility) {
5838 onWindowVisibilityChanged(visibility);
5839 }
5840
5841 /**
5842 * Called when the window containing has change its visibility
5843 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5844 * that this tells you whether or not your window is being made visible
5845 * to the window manager; this does <em>not</em> tell you whether or not
5846 * your window is obscured by other windows on the screen, even if it
5847 * is itself visible.
5848 *
5849 * @param visibility The new visibility of the window.
5850 */
5851 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005852 if (visibility == VISIBLE) {
5853 initialAwakenScrollBars();
5854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 }
5856
5857 /**
5858 * Returns the current visibility of the window this view is attached to
5859 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5860 *
5861 * @return Returns the current visibility of the view's window.
5862 */
5863 public int getWindowVisibility() {
5864 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5865 }
5866
5867 /**
5868 * Retrieve the overall visible display size in which the window this view is
5869 * attached to has been positioned in. This takes into account screen
5870 * decorations above the window, for both cases where the window itself
5871 * is being position inside of them or the window is being placed under
5872 * then and covered insets are used for the window to position its content
5873 * inside. In effect, this tells you the available area where content can
5874 * be placed and remain visible to users.
5875 *
5876 * <p>This function requires an IPC back to the window manager to retrieve
5877 * the requested information, so should not be used in performance critical
5878 * code like drawing.
5879 *
5880 * @param outRect Filled in with the visible display frame. If the view
5881 * is not attached to a window, this is simply the raw display size.
5882 */
5883 public void getWindowVisibleDisplayFrame(Rect outRect) {
5884 if (mAttachInfo != null) {
5885 try {
5886 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5887 } catch (RemoteException e) {
5888 return;
5889 }
5890 // XXX This is really broken, and probably all needs to be done
5891 // in the window manager, and we need to know more about whether
5892 // we want the area behind or in front of the IME.
5893 final Rect insets = mAttachInfo.mVisibleInsets;
5894 outRect.left += insets.left;
5895 outRect.top += insets.top;
5896 outRect.right -= insets.right;
5897 outRect.bottom -= insets.bottom;
5898 return;
5899 }
5900 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005901 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 }
5903
5904 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005905 * Dispatch a notification about a resource configuration change down
5906 * the view hierarchy.
5907 * ViewGroups should override to route to their children.
5908 *
5909 * @param newConfig The new resource configuration.
5910 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005911 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005912 */
5913 public void dispatchConfigurationChanged(Configuration newConfig) {
5914 onConfigurationChanged(newConfig);
5915 }
5916
5917 /**
5918 * Called when the current configuration of the resources being used
5919 * by the application have changed. You can use this to decide when
5920 * to reload resources that can changed based on orientation and other
5921 * configuration characterstics. You only need to use this if you are
5922 * not relying on the normal {@link android.app.Activity} mechanism of
5923 * recreating the activity instance upon a configuration change.
5924 *
5925 * @param newConfig The new resource configuration.
5926 */
5927 protected void onConfigurationChanged(Configuration newConfig) {
5928 }
5929
5930 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 * Private function to aggregate all per-view attributes in to the view
5932 * root.
5933 */
5934 void dispatchCollectViewAttributes(int visibility) {
5935 performCollectViewAttributes(visibility);
5936 }
5937
5938 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005939 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005940 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5941 mAttachInfo.mKeepScreenOn = true;
5942 }
5943 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005944 ListenerInfo li = mListenerInfo;
5945 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005946 mAttachInfo.mHasSystemUiListeners = true;
5947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 }
5949 }
5950
5951 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005952 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005954 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5955 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 ai.mRecomputeGlobalAttributes = true;
5957 }
5958 }
5959 }
5960
5961 /**
5962 * Returns whether the device is currently in touch mode. Touch mode is entered
5963 * once the user begins interacting with the device by touch, and affects various
5964 * things like whether focus is always visible to the user.
5965 *
5966 * @return Whether the device is in touch mode.
5967 */
5968 @ViewDebug.ExportedProperty
5969 public boolean isInTouchMode() {
5970 if (mAttachInfo != null) {
5971 return mAttachInfo.mInTouchMode;
5972 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005973 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 }
5975 }
5976
5977 /**
5978 * Returns the context the view is running in, through which it can
5979 * access the current theme, resources, etc.
5980 *
5981 * @return The view's Context.
5982 */
5983 @ViewDebug.CapturedViewProperty
5984 public final Context getContext() {
5985 return mContext;
5986 }
5987
5988 /**
5989 * Handle a key event before it is processed by any input method
5990 * associated with the view hierarchy. This can be used to intercept
5991 * key events in special situations before the IME consumes them; a
5992 * typical example would be handling the BACK key to update the application's
5993 * UI instead of allowing the IME to see it and close itself.
5994 *
5995 * @param keyCode The value in event.getKeyCode().
5996 * @param event Description of the key event.
5997 * @return If you handled the event, return true. If you want to allow the
5998 * event to be handled by the next receiver, return false.
5999 */
6000 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6001 return false;
6002 }
6003
6004 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006005 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6006 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6008 * is released, if the view is enabled and clickable.
6009 *
6010 * @param keyCode A key code that represents the button pressed, from
6011 * {@link android.view.KeyEvent}.
6012 * @param event The KeyEvent object that defines the button action.
6013 */
6014 public boolean onKeyDown(int keyCode, KeyEvent event) {
6015 boolean result = false;
6016
6017 switch (keyCode) {
6018 case KeyEvent.KEYCODE_DPAD_CENTER:
6019 case KeyEvent.KEYCODE_ENTER: {
6020 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6021 return true;
6022 }
6023 // Long clickable items don't necessarily have to be clickable
6024 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6025 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6026 (event.getRepeatCount() == 0)) {
6027 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006028 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 return true;
6030 }
6031 break;
6032 }
6033 }
6034 return result;
6035 }
6036
6037 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006038 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6039 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6040 * the event).
6041 */
6042 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6043 return false;
6044 }
6045
6046 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006047 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6048 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6050 * {@link KeyEvent#KEYCODE_ENTER} is released.
6051 *
6052 * @param keyCode A key code that represents the button pressed, from
6053 * {@link android.view.KeyEvent}.
6054 * @param event The KeyEvent object that defines the button action.
6055 */
6056 public boolean onKeyUp(int keyCode, KeyEvent event) {
6057 boolean result = false;
6058
6059 switch (keyCode) {
6060 case KeyEvent.KEYCODE_DPAD_CENTER:
6061 case KeyEvent.KEYCODE_ENTER: {
6062 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6063 return true;
6064 }
6065 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6066 setPressed(false);
6067
6068 if (!mHasPerformedLongPress) {
6069 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006070 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071
6072 result = performClick();
6073 }
6074 }
6075 break;
6076 }
6077 }
6078 return result;
6079 }
6080
6081 /**
6082 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6083 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6084 * the event).
6085 *
6086 * @param keyCode A key code that represents the button pressed, from
6087 * {@link android.view.KeyEvent}.
6088 * @param repeatCount The number of times the action was made.
6089 * @param event The KeyEvent object that defines the button action.
6090 */
6091 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6092 return false;
6093 }
6094
6095 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006096 * Called on the focused view when a key shortcut event is not handled.
6097 * Override this method to implement local key shortcuts for the View.
6098 * Key shortcuts can also be implemented by setting the
6099 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 *
6101 * @param keyCode The value in event.getKeyCode().
6102 * @param event Description of the key event.
6103 * @return If you handled the event, return true. If you want to allow the
6104 * event to be handled by the next receiver, return false.
6105 */
6106 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6107 return false;
6108 }
6109
6110 /**
6111 * Check whether the called view is a text editor, in which case it
6112 * would make sense to automatically display a soft input window for
6113 * it. Subclasses should override this if they implement
6114 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006115 * a call on that method would return a non-null InputConnection, and
6116 * they are really a first-class editor that the user would normally
6117 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006118 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006119 * <p>The default implementation always returns false. This does
6120 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6121 * will not be called or the user can not otherwise perform edits on your
6122 * view; it is just a hint to the system that this is not the primary
6123 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006124 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 * @return Returns true if this view is a text editor, else false.
6126 */
6127 public boolean onCheckIsTextEditor() {
6128 return false;
6129 }
Romain Guy8506ab42009-06-11 17:35:47 -07006130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 /**
6132 * Create a new InputConnection for an InputMethod to interact
6133 * with the view. The default implementation returns null, since it doesn't
6134 * support input methods. You can override this to implement such support.
6135 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006136 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137 * <p>When implementing this, you probably also want to implement
6138 * {@link #onCheckIsTextEditor()} to indicate you will return a
6139 * non-null InputConnection.
6140 *
6141 * @param outAttrs Fill in with attribute information about the connection.
6142 */
6143 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6144 return null;
6145 }
6146
6147 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006148 * Called by the {@link android.view.inputmethod.InputMethodManager}
6149 * when a view who is not the current
6150 * input connection target is trying to make a call on the manager. The
6151 * default implementation returns false; you can override this to return
6152 * true for certain views if you are performing InputConnection proxying
6153 * to them.
6154 * @param view The View that is making the InputMethodManager call.
6155 * @return Return true to allow the call, false to reject.
6156 */
6157 public boolean checkInputConnectionProxy(View view) {
6158 return false;
6159 }
Romain Guy8506ab42009-06-11 17:35:47 -07006160
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 * Show the context menu for this view. It is not safe to hold on to the
6163 * menu after returning from this method.
6164 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006165 * You should normally not overload this method. Overload
6166 * {@link #onCreateContextMenu(ContextMenu)} or define an
6167 * {@link OnCreateContextMenuListener} to add items to the context menu.
6168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 * @param menu The context menu to populate
6170 */
6171 public void createContextMenu(ContextMenu menu) {
6172 ContextMenuInfo menuInfo = getContextMenuInfo();
6173
6174 // Sets the current menu info so all items added to menu will have
6175 // my extra info set.
6176 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6177
6178 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006179 ListenerInfo li = mListenerInfo;
6180 if (li != null && li.mOnCreateContextMenuListener != null) {
6181 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 }
6183
6184 // Clear the extra information so subsequent items that aren't mine don't
6185 // have my extra info.
6186 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6187
6188 if (mParent != null) {
6189 mParent.createContextMenu(menu);
6190 }
6191 }
6192
6193 /**
6194 * Views should implement this if they have extra information to associate
6195 * with the context menu. The return result is supplied as a parameter to
6196 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6197 * callback.
6198 *
6199 * @return Extra information about the item for which the context menu
6200 * should be shown. This information will vary across different
6201 * subclasses of View.
6202 */
6203 protected ContextMenuInfo getContextMenuInfo() {
6204 return null;
6205 }
6206
6207 /**
6208 * Views should implement this if the view itself is going to add items to
6209 * the context menu.
6210 *
6211 * @param menu the context menu to populate
6212 */
6213 protected void onCreateContextMenu(ContextMenu menu) {
6214 }
6215
6216 /**
6217 * Implement this method to handle trackball motion events. The
6218 * <em>relative</em> movement of the trackball since the last event
6219 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6220 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6221 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6222 * they will often be fractional values, representing the more fine-grained
6223 * movement information available from a trackball).
6224 *
6225 * @param event The motion event.
6226 * @return True if the event was handled, false otherwise.
6227 */
6228 public boolean onTrackballEvent(MotionEvent event) {
6229 return false;
6230 }
6231
6232 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006233 * Implement this method to handle generic motion events.
6234 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006235 * Generic motion events describe joystick movements, mouse hovers, track pad
6236 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006237 * {@link MotionEvent#getSource() source} of the motion event specifies
6238 * the class of input that was received. Implementations of this method
6239 * must examine the bits in the source before processing the event.
6240 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006241 * </p><p>
6242 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6243 * are delivered to the view under the pointer. All other generic motion events are
6244 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006245 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006246 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006247 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006248 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6249 * // process the joystick movement...
6250 * return true;
6251 * }
6252 * }
6253 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6254 * switch (event.getAction()) {
6255 * case MotionEvent.ACTION_HOVER_MOVE:
6256 * // process the mouse hover movement...
6257 * return true;
6258 * case MotionEvent.ACTION_SCROLL:
6259 * // process the scroll wheel movement...
6260 * return true;
6261 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006262 * }
6263 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006264 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006265 *
6266 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006267 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006268 */
6269 public boolean onGenericMotionEvent(MotionEvent event) {
6270 return false;
6271 }
6272
6273 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006274 * Implement this method to handle hover events.
6275 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006276 * This method is called whenever a pointer is hovering into, over, or out of the
6277 * bounds of a view and the view is not currently being touched.
6278 * Hover events are represented as pointer events with action
6279 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6280 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6281 * </p>
6282 * <ul>
6283 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6284 * when the pointer enters the bounds of the view.</li>
6285 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6286 * when the pointer has already entered the bounds of the view and has moved.</li>
6287 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6288 * when the pointer has exited the bounds of the view or when the pointer is
6289 * about to go down due to a button click, tap, or similar user action that
6290 * causes the view to be touched.</li>
6291 * </ul>
6292 * <p>
6293 * The view should implement this method to return true to indicate that it is
6294 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006295 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006296 * The default implementation calls {@link #setHovered} to update the hovered state
6297 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006298 * is enabled and is clickable. The default implementation also sends hover
6299 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006300 * </p>
6301 *
6302 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006303 * @return True if the view handled the hover event.
6304 *
6305 * @see #isHovered
6306 * @see #setHovered
6307 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006308 */
6309 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006310 // The root view may receive hover (or touch) events that are outside the bounds of
6311 // the window. This code ensures that we only send accessibility events for
6312 // hovers that are actually within the bounds of the root view.
6313 final int action = event.getAction();
6314 if (!mSendingHoverAccessibilityEvents) {
6315 if ((action == MotionEvent.ACTION_HOVER_ENTER
6316 || action == MotionEvent.ACTION_HOVER_MOVE)
6317 && !hasHoveredChild()
6318 && pointInView(event.getX(), event.getY())) {
6319 mSendingHoverAccessibilityEvents = true;
6320 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6321 }
6322 } else {
6323 if (action == MotionEvent.ACTION_HOVER_EXIT
6324 || (action == MotionEvent.ACTION_HOVER_MOVE
6325 && !pointInView(event.getX(), event.getY()))) {
6326 mSendingHoverAccessibilityEvents = false;
6327 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6328 }
Jeff Browna1b24182011-07-28 13:38:24 -07006329 }
6330
Jeff Brown87b7f802011-06-21 18:35:45 -07006331 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006332 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006333 case MotionEvent.ACTION_HOVER_ENTER:
6334 setHovered(true);
6335 break;
6336 case MotionEvent.ACTION_HOVER_EXIT:
6337 setHovered(false);
6338 break;
6339 }
Jeff Browna1b24182011-07-28 13:38:24 -07006340
6341 // Dispatch the event to onGenericMotionEvent before returning true.
6342 // This is to provide compatibility with existing applications that
6343 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6344 // break because of the new default handling for hoverable views
6345 // in onHoverEvent.
6346 // Note that onGenericMotionEvent will be called by default when
6347 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6348 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006349 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006350 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006351 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006352 }
6353
6354 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006355 * Returns true if the view should handle {@link #onHoverEvent}
6356 * by calling {@link #setHovered} to change its hovered state.
6357 *
6358 * @return True if the view is hoverable.
6359 */
6360 private boolean isHoverable() {
6361 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006362 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006363 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6364 return false;
6365 }
6366
6367 return (viewFlags & CLICKABLE) == CLICKABLE
6368 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6369 }
6370
6371 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006372 * Returns true if the view is currently hovered.
6373 *
6374 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006375 *
6376 * @see #setHovered
6377 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006378 */
Jeff Brown10b62902011-06-20 16:40:37 -07006379 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006380 public boolean isHovered() {
6381 return (mPrivateFlags & HOVERED) != 0;
6382 }
6383
6384 /**
6385 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006386 * <p>
6387 * Calling this method also changes the drawable state of the view. This
6388 * enables the view to react to hover by using different drawable resources
6389 * to change its appearance.
6390 * </p><p>
6391 * The {@link #onHoverChanged} method is called when the hovered state changes.
6392 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006393 *
6394 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006395 *
6396 * @see #isHovered
6397 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006398 */
6399 public void setHovered(boolean hovered) {
6400 if (hovered) {
6401 if ((mPrivateFlags & HOVERED) == 0) {
6402 mPrivateFlags |= HOVERED;
6403 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006404 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006405 }
6406 } else {
6407 if ((mPrivateFlags & HOVERED) != 0) {
6408 mPrivateFlags &= ~HOVERED;
6409 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006410 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006411 }
6412 }
6413 }
6414
6415 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006416 * Implement this method to handle hover state changes.
6417 * <p>
6418 * This method is called whenever the hover state changes as a result of a
6419 * call to {@link #setHovered}.
6420 * </p>
6421 *
6422 * @param hovered The current hover state, as returned by {@link #isHovered}.
6423 *
6424 * @see #isHovered
6425 * @see #setHovered
6426 */
6427 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006428 }
6429
6430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 * Implement this method to handle touch screen motion events.
6432 *
6433 * @param event The motion event.
6434 * @return True if the event was handled, false otherwise.
6435 */
6436 public boolean onTouchEvent(MotionEvent event) {
6437 final int viewFlags = mViewFlags;
6438
6439 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006440 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6441 mPrivateFlags &= ~PRESSED;
6442 refreshDrawableState();
6443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 // A disabled view that is clickable still consumes the touch
6445 // events, it just doesn't respond to them.
6446 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6447 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6448 }
6449
6450 if (mTouchDelegate != null) {
6451 if (mTouchDelegate.onTouchEvent(event)) {
6452 return true;
6453 }
6454 }
6455
6456 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6457 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6458 switch (event.getAction()) {
6459 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006460 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6461 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 // take focus if we don't have it already and we should in
6463 // touch mode.
6464 boolean focusTaken = false;
6465 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6466 focusTaken = requestFocus();
6467 }
6468
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006469 if (prepressed) {
6470 // The button is being released before we actually
6471 // showed it as pressed. Make it show the pressed
6472 // state now (before scheduling the click) to ensure
6473 // the user sees it.
6474 mPrivateFlags |= PRESSED;
6475 refreshDrawableState();
6476 }
Joe Malin32736f02011-01-19 16:14:20 -08006477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 if (!mHasPerformedLongPress) {
6479 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006480 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481
6482 // Only perform take click actions if we were in the pressed state
6483 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006484 // Use a Runnable and post this rather than calling
6485 // performClick directly. This lets other visual state
6486 // of the view update before click actions start.
6487 if (mPerformClick == null) {
6488 mPerformClick = new PerformClick();
6489 }
6490 if (!post(mPerformClick)) {
6491 performClick();
6492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 }
6494 }
6495
6496 if (mUnsetPressedState == null) {
6497 mUnsetPressedState = new UnsetPressedState();
6498 }
6499
Adam Powelle14579b2009-12-16 18:39:52 -08006500 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006501 postDelayed(mUnsetPressedState,
6502 ViewConfiguration.getPressedStateDuration());
6503 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 // If the post failed, unpress right now
6505 mUnsetPressedState.run();
6506 }
Adam Powelle14579b2009-12-16 18:39:52 -08006507 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 }
6509 break;
6510
6511 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006512 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006513
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006514 if (performButtonActionOnTouchDown(event)) {
6515 break;
6516 }
6517
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006518 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006519 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006520
6521 // For views inside a scrolling container, delay the pressed feedback for
6522 // a short period in case this is a scroll.
6523 if (isInScrollingContainer) {
6524 mPrivateFlags |= PREPRESSED;
6525 if (mPendingCheckForTap == null) {
6526 mPendingCheckForTap = new CheckForTap();
6527 }
6528 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6529 } else {
6530 // Not inside a scrolling container, so show the feedback right away
6531 mPrivateFlags |= PRESSED;
6532 refreshDrawableState();
6533 checkForLongClick(0);
6534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 break;
6536
6537 case MotionEvent.ACTION_CANCEL:
6538 mPrivateFlags &= ~PRESSED;
6539 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006540 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 break;
6542
6543 case MotionEvent.ACTION_MOVE:
6544 final int x = (int) event.getX();
6545 final int y = (int) event.getY();
6546
6547 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006548 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006550 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006552 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006553 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554
6555 // Need to switch from pressed to not pressed
6556 mPrivateFlags &= ~PRESSED;
6557 refreshDrawableState();
6558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 }
6560 break;
6561 }
6562 return true;
6563 }
6564
6565 return false;
6566 }
6567
6568 /**
Adam Powell10298662011-08-14 18:26:30 -07006569 * @hide
6570 */
6571 public boolean isInScrollingContainer() {
6572 ViewParent p = getParent();
6573 while (p != null && p instanceof ViewGroup) {
6574 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6575 return true;
6576 }
6577 p = p.getParent();
6578 }
6579 return false;
6580 }
6581
6582 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006583 * Remove the longpress detection timer.
6584 */
6585 private void removeLongPressCallback() {
6586 if (mPendingCheckForLongPress != null) {
6587 removeCallbacks(mPendingCheckForLongPress);
6588 }
6589 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006590
6591 /**
6592 * Remove the pending click action
6593 */
6594 private void removePerformClickCallback() {
6595 if (mPerformClick != null) {
6596 removeCallbacks(mPerformClick);
6597 }
6598 }
6599
Adam Powelle14579b2009-12-16 18:39:52 -08006600 /**
Romain Guya440b002010-02-24 15:57:54 -08006601 * Remove the prepress detection timer.
6602 */
6603 private void removeUnsetPressCallback() {
6604 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6605 setPressed(false);
6606 removeCallbacks(mUnsetPressedState);
6607 }
6608 }
6609
6610 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006611 * Remove the tap detection timer.
6612 */
6613 private void removeTapCallback() {
6614 if (mPendingCheckForTap != null) {
6615 mPrivateFlags &= ~PREPRESSED;
6616 removeCallbacks(mPendingCheckForTap);
6617 }
6618 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006619
6620 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 * Cancels a pending long press. Your subclass can use this if you
6622 * want the context menu to come up if the user presses and holds
6623 * at the same place, but you don't want it to come up if they press
6624 * and then move around enough to cause scrolling.
6625 */
6626 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006627 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006628
6629 /*
6630 * The prepressed state handled by the tap callback is a display
6631 * construct, but the tap callback will post a long press callback
6632 * less its own timeout. Remove it here.
6633 */
6634 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 }
6636
6637 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006638 * Remove the pending callback for sending a
6639 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6640 */
6641 private void removeSendViewScrolledAccessibilityEventCallback() {
6642 if (mSendViewScrolledAccessibilityEvent != null) {
6643 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6644 }
6645 }
6646
6647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 * Sets the TouchDelegate for this View.
6649 */
6650 public void setTouchDelegate(TouchDelegate delegate) {
6651 mTouchDelegate = delegate;
6652 }
6653
6654 /**
6655 * Gets the TouchDelegate for this View.
6656 */
6657 public TouchDelegate getTouchDelegate() {
6658 return mTouchDelegate;
6659 }
6660
6661 /**
6662 * Set flags controlling behavior of this view.
6663 *
6664 * @param flags Constant indicating the value which should be set
6665 * @param mask Constant indicating the bit range that should be changed
6666 */
6667 void setFlags(int flags, int mask) {
6668 int old = mViewFlags;
6669 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6670
6671 int changed = mViewFlags ^ old;
6672 if (changed == 0) {
6673 return;
6674 }
6675 int privateFlags = mPrivateFlags;
6676
6677 /* Check if the FOCUSABLE bit has changed */
6678 if (((changed & FOCUSABLE_MASK) != 0) &&
6679 ((privateFlags & HAS_BOUNDS) !=0)) {
6680 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6681 && ((privateFlags & FOCUSED) != 0)) {
6682 /* Give up focus if we are no longer focusable */
6683 clearFocus();
6684 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6685 && ((privateFlags & FOCUSED) == 0)) {
6686 /*
6687 * Tell the view system that we are now available to take focus
6688 * if no one else already has it.
6689 */
6690 if (mParent != null) mParent.focusableViewAvailable(this);
6691 }
6692 }
6693
6694 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6695 if ((changed & VISIBILITY_MASK) != 0) {
6696 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006697 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006698 * it was not visible. Marking it drawn ensures that the invalidation will
6699 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006700 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006701 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006702 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703
6704 needGlobalAttributesUpdate(true);
6705
6706 // a view becoming visible is worth notifying the parent
6707 // about in case nothing has focus. even if this specific view
6708 // isn't focusable, it may contain something that is, so let
6709 // the root view try to give this focus if nothing else does.
6710 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6711 mParent.focusableViewAvailable(this);
6712 }
6713 }
6714 }
6715
6716 /* Check if the GONE bit has changed */
6717 if ((changed & GONE) != 0) {
6718 needGlobalAttributesUpdate(false);
6719 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006720
Romain Guyecd80ee2009-12-03 17:13:02 -08006721 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6722 if (hasFocus()) clearFocus();
6723 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006724 if (mParent instanceof View) {
6725 // GONE views noop invalidation, so invalidate the parent
6726 ((View) mParent).invalidate(true);
6727 }
6728 // Mark the view drawn to ensure that it gets invalidated properly the next
6729 // time it is visible and gets invalidated
6730 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 }
6732 if (mAttachInfo != null) {
6733 mAttachInfo.mViewVisibilityChanged = true;
6734 }
6735 }
6736
6737 /* Check if the VISIBLE bit has changed */
6738 if ((changed & INVISIBLE) != 0) {
6739 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006740 /*
6741 * If this view is becoming invisible, set the DRAWN flag so that
6742 * the next invalidate() will not be skipped.
6743 */
6744 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745
6746 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6747 // root view becoming invisible shouldn't clear focus
6748 if (getRootView() != this) {
6749 clearFocus();
6750 }
6751 }
6752 if (mAttachInfo != null) {
6753 mAttachInfo.mViewVisibilityChanged = true;
6754 }
6755 }
6756
Adam Powell326d8082009-12-09 15:10:07 -08006757 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006758 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006759 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6760 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006761 } else if (mParent != null) {
6762 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006763 }
Adam Powell326d8082009-12-09 15:10:07 -08006764 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6765 }
6766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6768 destroyDrawingCache();
6769 }
6770
6771 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6772 destroyDrawingCache();
6773 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006774 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 }
6776
6777 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6778 destroyDrawingCache();
6779 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6780 }
6781
6782 if ((changed & DRAW_MASK) != 0) {
6783 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6784 if (mBGDrawable != null) {
6785 mPrivateFlags &= ~SKIP_DRAW;
6786 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6787 } else {
6788 mPrivateFlags |= SKIP_DRAW;
6789 }
6790 } else {
6791 mPrivateFlags &= ~SKIP_DRAW;
6792 }
6793 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006794 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 }
6796
6797 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006798 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 mParent.recomputeViewAttributes(this);
6800 }
6801 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006802
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006803 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006804 requestLayout();
6805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 }
6807
6808 /**
6809 * Change the view's z order in the tree, so it's on top of other sibling
6810 * views
6811 */
6812 public void bringToFront() {
6813 if (mParent != null) {
6814 mParent.bringChildToFront(this);
6815 }
6816 }
6817
6818 /**
6819 * This is called in response to an internal scroll in this view (i.e., the
6820 * view scrolled its own contents). This is typically as a result of
6821 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6822 * called.
6823 *
6824 * @param l Current horizontal scroll origin.
6825 * @param t Current vertical scroll origin.
6826 * @param oldl Previous horizontal scroll origin.
6827 * @param oldt Previous vertical scroll origin.
6828 */
6829 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006830 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6831 postSendViewScrolledAccessibilityEventCallback();
6832 }
6833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 mBackgroundSizeChanged = true;
6835
6836 final AttachInfo ai = mAttachInfo;
6837 if (ai != null) {
6838 ai.mViewScrollChanged = true;
6839 }
6840 }
6841
6842 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006843 * Interface definition for a callback to be invoked when the layout bounds of a view
6844 * changes due to layout processing.
6845 */
6846 public interface OnLayoutChangeListener {
6847 /**
6848 * Called when the focus state of a view has changed.
6849 *
6850 * @param v The view whose state has changed.
6851 * @param left The new value of the view's left property.
6852 * @param top The new value of the view's top property.
6853 * @param right The new value of the view's right property.
6854 * @param bottom The new value of the view's bottom property.
6855 * @param oldLeft The previous value of the view's left property.
6856 * @param oldTop The previous value of the view's top property.
6857 * @param oldRight The previous value of the view's right property.
6858 * @param oldBottom The previous value of the view's bottom property.
6859 */
6860 void onLayoutChange(View v, int left, int top, int right, int bottom,
6861 int oldLeft, int oldTop, int oldRight, int oldBottom);
6862 }
6863
6864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 * This is called during layout when the size of this view has changed. If
6866 * you were just added to the view hierarchy, you're called with the old
6867 * values of 0.
6868 *
6869 * @param w Current width of this view.
6870 * @param h Current height of this view.
6871 * @param oldw Old width of this view.
6872 * @param oldh Old height of this view.
6873 */
6874 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6875 }
6876
6877 /**
6878 * Called by draw to draw the child views. This may be overridden
6879 * by derived classes to gain control just before its children are drawn
6880 * (but after its own view has been drawn).
6881 * @param canvas the canvas on which to draw the view
6882 */
6883 protected void dispatchDraw(Canvas canvas) {
6884 }
6885
6886 /**
6887 * Gets the parent of this view. Note that the parent is a
6888 * ViewParent and not necessarily a View.
6889 *
6890 * @return Parent of this view.
6891 */
6892 public final ViewParent getParent() {
6893 return mParent;
6894 }
6895
6896 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006897 * Set the horizontal scrolled position of your view. This will cause a call to
6898 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6899 * invalidated.
6900 * @param value the x position to scroll to
6901 */
6902 public void setScrollX(int value) {
6903 scrollTo(value, mScrollY);
6904 }
6905
6906 /**
6907 * Set the vertical scrolled position of your view. This will cause a call to
6908 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6909 * invalidated.
6910 * @param value the y position to scroll to
6911 */
6912 public void setScrollY(int value) {
6913 scrollTo(mScrollX, value);
6914 }
6915
6916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 * Return the scrolled left position of this view. This is the left edge of
6918 * the displayed part of your view. You do not need to draw any pixels
6919 * farther left, since those are outside of the frame of your view on
6920 * screen.
6921 *
6922 * @return The left edge of the displayed part of your view, in pixels.
6923 */
6924 public final int getScrollX() {
6925 return mScrollX;
6926 }
6927
6928 /**
6929 * Return the scrolled top position of this view. This is the top edge of
6930 * the displayed part of your view. You do not need to draw any pixels above
6931 * it, since those are outside of the frame of your view on screen.
6932 *
6933 * @return The top edge of the displayed part of your view, in pixels.
6934 */
6935 public final int getScrollY() {
6936 return mScrollY;
6937 }
6938
6939 /**
6940 * Return the width of the your view.
6941 *
6942 * @return The width of your view, in pixels.
6943 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006944 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 public final int getWidth() {
6946 return mRight - mLeft;
6947 }
6948
6949 /**
6950 * Return the height of your view.
6951 *
6952 * @return The height of your view, in pixels.
6953 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006954 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 public final int getHeight() {
6956 return mBottom - mTop;
6957 }
6958
6959 /**
6960 * Return the visible drawing bounds of your view. Fills in the output
6961 * rectangle with the values from getScrollX(), getScrollY(),
6962 * getWidth(), and getHeight().
6963 *
6964 * @param outRect The (scrolled) drawing bounds of the view.
6965 */
6966 public void getDrawingRect(Rect outRect) {
6967 outRect.left = mScrollX;
6968 outRect.top = mScrollY;
6969 outRect.right = mScrollX + (mRight - mLeft);
6970 outRect.bottom = mScrollY + (mBottom - mTop);
6971 }
6972
6973 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006974 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6975 * raw width component (that is the result is masked by
6976 * {@link #MEASURED_SIZE_MASK}).
6977 *
6978 * @return The raw measured width of this view.
6979 */
6980 public final int getMeasuredWidth() {
6981 return mMeasuredWidth & MEASURED_SIZE_MASK;
6982 }
6983
6984 /**
6985 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006986 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006987 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 * This should be used during measurement and layout calculations only. Use
6989 * {@link #getWidth()} to see how wide a view is after layout.
6990 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006991 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006993 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 return mMeasuredWidth;
6995 }
6996
6997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006998 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6999 * raw width component (that is the result is masked by
7000 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007002 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 */
7004 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007005 return mMeasuredHeight & MEASURED_SIZE_MASK;
7006 }
7007
7008 /**
7009 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007010 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007011 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7012 * This should be used during measurement and layout calculations only. Use
7013 * {@link #getHeight()} to see how wide a view is after layout.
7014 *
7015 * @return The measured width of this view as a bit mask.
7016 */
7017 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 return mMeasuredHeight;
7019 }
7020
7021 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007022 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7023 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7024 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7025 * and the height component is at the shifted bits
7026 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7027 */
7028 public final int getMeasuredState() {
7029 return (mMeasuredWidth&MEASURED_STATE_MASK)
7030 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7031 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7032 }
7033
7034 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007035 * The transform matrix of this view, which is calculated based on the current
7036 * roation, scale, and pivot properties.
7037 *
7038 * @see #getRotation()
7039 * @see #getScaleX()
7040 * @see #getScaleY()
7041 * @see #getPivotX()
7042 * @see #getPivotY()
7043 * @return The current transform matrix for the view
7044 */
7045 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007046 if (mTransformationInfo != null) {
7047 updateMatrix();
7048 return mTransformationInfo.mMatrix;
7049 }
7050 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007051 }
7052
7053 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007054 * Utility function to determine if the value is far enough away from zero to be
7055 * considered non-zero.
7056 * @param value A floating point value to check for zero-ness
7057 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7058 */
7059 private static boolean nonzero(float value) {
7060 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7061 }
7062
7063 /**
Jeff Brown86671742010-09-30 20:00:15 -07007064 * Returns true if the transform matrix is the identity matrix.
7065 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007066 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007067 * @return True if the transform matrix is the identity matrix, false otherwise.
7068 */
Jeff Brown86671742010-09-30 20:00:15 -07007069 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007070 if (mTransformationInfo != null) {
7071 updateMatrix();
7072 return mTransformationInfo.mMatrixIsIdentity;
7073 }
7074 return true;
7075 }
7076
7077 void ensureTransformationInfo() {
7078 if (mTransformationInfo == null) {
7079 mTransformationInfo = new TransformationInfo();
7080 }
Jeff Brown86671742010-09-30 20:00:15 -07007081 }
7082
7083 /**
7084 * Recomputes the transform matrix if necessary.
7085 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007086 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007087 final TransformationInfo info = mTransformationInfo;
7088 if (info == null) {
7089 return;
7090 }
7091 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007092 // transform-related properties have changed since the last time someone
7093 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007094
7095 // Figure out if we need to update the pivot point
7096 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007097 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7098 info.mPrevWidth = mRight - mLeft;
7099 info.mPrevHeight = mBottom - mTop;
7100 info.mPivotX = info.mPrevWidth / 2f;
7101 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007102 }
7103 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007104 info.mMatrix.reset();
7105 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7106 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7107 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7108 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007109 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007110 if (info.mCamera == null) {
7111 info.mCamera = new Camera();
7112 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007113 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007114 info.mCamera.save();
7115 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7116 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7117 info.mCamera.getMatrix(info.matrix3D);
7118 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7119 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7120 info.mPivotY + info.mTranslationY);
7121 info.mMatrix.postConcat(info.matrix3D);
7122 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007123 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007124 info.mMatrixDirty = false;
7125 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7126 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007127 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007128 }
7129
7130 /**
7131 * Utility method to retrieve the inverse of the current mMatrix property.
7132 * We cache the matrix to avoid recalculating it when transform properties
7133 * have not changed.
7134 *
7135 * @return The inverse of the current matrix of this view.
7136 */
Jeff Brown86671742010-09-30 20:00:15 -07007137 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007138 final TransformationInfo info = mTransformationInfo;
7139 if (info != null) {
7140 updateMatrix();
7141 if (info.mInverseMatrixDirty) {
7142 if (info.mInverseMatrix == null) {
7143 info.mInverseMatrix = new Matrix();
7144 }
7145 info.mMatrix.invert(info.mInverseMatrix);
7146 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007147 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007148 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007150 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007151 }
7152
7153 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007154 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7155 * views are drawn) from the camera to this view. The camera's distance
7156 * affects 3D transformations, for instance rotations around the X and Y
7157 * axis. If the rotationX or rotationY properties are changed and this view is
7158 * large (more than half the size of the screen), it is recommended to always
7159 * use a camera distance that's greater than the height (X axis rotation) or
7160 * the width (Y axis rotation) of this view.</p>
7161 *
7162 * <p>The distance of the camera from the view plane can have an affect on the
7163 * perspective distortion of the view when it is rotated around the x or y axis.
7164 * For example, a large distance will result in a large viewing angle, and there
7165 * will not be much perspective distortion of the view as it rotates. A short
7166 * distance may cause much more perspective distortion upon rotation, and can
7167 * also result in some drawing artifacts if the rotated view ends up partially
7168 * behind the camera (which is why the recommendation is to use a distance at
7169 * least as far as the size of the view, if the view is to be rotated.)</p>
7170 *
7171 * <p>The distance is expressed in "depth pixels." The default distance depends
7172 * on the screen density. For instance, on a medium density display, the
7173 * default distance is 1280. On a high density display, the default distance
7174 * is 1920.</p>
7175 *
7176 * <p>If you want to specify a distance that leads to visually consistent
7177 * results across various densities, use the following formula:</p>
7178 * <pre>
7179 * float scale = context.getResources().getDisplayMetrics().density;
7180 * view.setCameraDistance(distance * scale);
7181 * </pre>
7182 *
7183 * <p>The density scale factor of a high density display is 1.5,
7184 * and 1920 = 1280 * 1.5.</p>
7185 *
7186 * @param distance The distance in "depth pixels", if negative the opposite
7187 * value is used
7188 *
7189 * @see #setRotationX(float)
7190 * @see #setRotationY(float)
7191 */
7192 public void setCameraDistance(float distance) {
7193 invalidateParentCaches();
7194 invalidate(false);
7195
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007196 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007197 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007198 final TransformationInfo info = mTransformationInfo;
7199 if (info.mCamera == null) {
7200 info.mCamera = new Camera();
7201 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007202 }
7203
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007204 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7205 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007206
7207 invalidate(false);
7208 }
7209
7210 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007211 * The degrees that the view is rotated around the pivot point.
7212 *
Romain Guya5364ee2011-02-24 14:46:04 -08007213 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007214 * @see #getPivotX()
7215 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007216 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007217 * @return The degrees of rotation.
7218 */
7219 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007220 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007221 }
7222
7223 /**
Chet Haase897247b2010-09-09 14:54:47 -07007224 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7225 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007226 *
7227 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007228 *
7229 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007230 * @see #getPivotX()
7231 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007232 * @see #setRotationX(float)
7233 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007234 *
7235 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007236 */
7237 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007238 ensureTransformationInfo();
7239 final TransformationInfo info = mTransformationInfo;
7240 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007241 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007242 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007243 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007244 info.mRotation = rotation;
7245 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007246 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007247 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007248 }
7249 }
7250
7251 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007252 * The degrees that the view is rotated around the vertical axis through the pivot point.
7253 *
7254 * @see #getPivotX()
7255 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007256 * @see #setRotationY(float)
7257 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007258 * @return The degrees of Y rotation.
7259 */
7260 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007261 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007262 }
7263
7264 /**
Chet Haase897247b2010-09-09 14:54:47 -07007265 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7266 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7267 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007268 *
7269 * When rotating large views, it is recommended to adjust the camera distance
7270 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007271 *
7272 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007273 *
7274 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007275 * @see #getPivotX()
7276 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007277 * @see #setRotation(float)
7278 * @see #setRotationX(float)
7279 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007280 *
7281 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007282 */
7283 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007284 ensureTransformationInfo();
7285 final TransformationInfo info = mTransformationInfo;
7286 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007287 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007288 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007289 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007290 info.mRotationY = rotationY;
7291 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007292 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007293 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007294 }
7295 }
7296
7297 /**
7298 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7299 *
7300 * @see #getPivotX()
7301 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007302 * @see #setRotationX(float)
7303 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007304 * @return The degrees of X rotation.
7305 */
7306 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007307 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007308 }
7309
7310 /**
Chet Haase897247b2010-09-09 14:54:47 -07007311 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7312 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7313 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007314 *
7315 * When rotating large views, it is recommended to adjust the camera distance
7316 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007317 *
7318 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007319 *
7320 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007321 * @see #getPivotX()
7322 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007323 * @see #setRotation(float)
7324 * @see #setRotationY(float)
7325 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007326 *
7327 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007328 */
7329 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007330 ensureTransformationInfo();
7331 final TransformationInfo info = mTransformationInfo;
7332 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007333 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007334 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007335 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007336 info.mRotationX = rotationX;
7337 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007338 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007339 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007340 }
7341 }
7342
7343 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007344 * The amount that the view is scaled in x around the pivot point, as a proportion of
7345 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7346 *
Joe Onorato93162322010-09-16 15:42:01 -04007347 * <p>By default, this is 1.0f.
7348 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007349 * @see #getPivotX()
7350 * @see #getPivotY()
7351 * @return The scaling factor.
7352 */
7353 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007354 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007355 }
7356
7357 /**
7358 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7359 * the view's unscaled width. A value of 1 means that no scaling is applied.
7360 *
7361 * @param scaleX The scaling factor.
7362 * @see #getPivotX()
7363 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007364 *
7365 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007366 */
7367 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007368 ensureTransformationInfo();
7369 final TransformationInfo info = mTransformationInfo;
7370 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007371 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007372 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007373 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007374 info.mScaleX = scaleX;
7375 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007376 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007377 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007378 }
7379 }
7380
7381 /**
7382 * The amount that the view is scaled in y around the pivot point, as a proportion of
7383 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7384 *
Joe Onorato93162322010-09-16 15:42:01 -04007385 * <p>By default, this is 1.0f.
7386 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007387 * @see #getPivotX()
7388 * @see #getPivotY()
7389 * @return The scaling factor.
7390 */
7391 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007392 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007393 }
7394
7395 /**
7396 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7397 * the view's unscaled width. A value of 1 means that no scaling is applied.
7398 *
7399 * @param scaleY The scaling factor.
7400 * @see #getPivotX()
7401 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007402 *
7403 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007404 */
7405 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007406 ensureTransformationInfo();
7407 final TransformationInfo info = mTransformationInfo;
7408 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007409 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007410 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007411 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007412 info.mScaleY = scaleY;
7413 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007414 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007415 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007416 }
7417 }
7418
7419 /**
7420 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7421 * and {@link #setScaleX(float) scaled}.
7422 *
7423 * @see #getRotation()
7424 * @see #getScaleX()
7425 * @see #getScaleY()
7426 * @see #getPivotY()
7427 * @return The x location of the pivot point.
7428 */
7429 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007430 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007431 }
7432
7433 /**
7434 * Sets the x location of the point around which the view is
7435 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007436 * By default, the pivot point is centered on the object.
7437 * Setting this property disables this behavior and causes the view to use only the
7438 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007439 *
7440 * @param pivotX The x location of the pivot point.
7441 * @see #getRotation()
7442 * @see #getScaleX()
7443 * @see #getScaleY()
7444 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007445 *
7446 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007447 */
7448 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007449 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007450 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007451 final TransformationInfo info = mTransformationInfo;
7452 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007453 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007454 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007455 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007456 info.mPivotX = pivotX;
7457 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007458 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007459 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007460 }
7461 }
7462
7463 /**
7464 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7465 * and {@link #setScaleY(float) scaled}.
7466 *
7467 * @see #getRotation()
7468 * @see #getScaleX()
7469 * @see #getScaleY()
7470 * @see #getPivotY()
7471 * @return The y location of the pivot point.
7472 */
7473 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007474 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007475 }
7476
7477 /**
7478 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007479 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7480 * Setting this property disables this behavior and causes the view to use only the
7481 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 *
7483 * @param pivotY The y location of the pivot point.
7484 * @see #getRotation()
7485 * @see #getScaleX()
7486 * @see #getScaleY()
7487 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007488 *
7489 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007490 */
7491 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007492 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007493 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007494 final TransformationInfo info = mTransformationInfo;
7495 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007496 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007497 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007498 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007499 info.mPivotY = pivotY;
7500 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007501 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007502 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007503 }
7504 }
7505
7506 /**
7507 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7508 * completely transparent and 1 means the view is completely opaque.
7509 *
Joe Onorato93162322010-09-16 15:42:01 -04007510 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007511 * @return The opacity of the view.
7512 */
7513 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007514 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007515 }
7516
7517 /**
Romain Guy171c5922011-01-06 10:04:23 -08007518 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7519 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007520 *
Romain Guy171c5922011-01-06 10:04:23 -08007521 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7522 * responsible for applying the opacity itself. Otherwise, calling this method is
7523 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007524 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007525 *
7526 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007527 *
Joe Malin32736f02011-01-19 16:14:20 -08007528 * @see #setLayerType(int, android.graphics.Paint)
7529 *
Chet Haase73066682010-11-29 15:55:32 -08007530 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007531 */
7532 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007533 ensureTransformationInfo();
7534 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007535 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007536 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007537 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007538 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007539 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007540 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007541 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007542 invalidate(false);
7543 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007544 }
7545
7546 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007547 * Faster version of setAlpha() which performs the same steps except there are
7548 * no calls to invalidate(). The caller of this function should perform proper invalidation
7549 * on the parent and this object. The return value indicates whether the subclass handles
7550 * alpha (the return value for onSetAlpha()).
7551 *
7552 * @param alpha The new value for the alpha property
7553 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7554 */
7555 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007556 ensureTransformationInfo();
7557 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007558 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7559 if (subclassHandlesAlpha) {
7560 mPrivateFlags |= ALPHA_SET;
7561 } else {
7562 mPrivateFlags &= ~ALPHA_SET;
7563 }
7564 return subclassHandlesAlpha;
7565 }
7566
7567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 * Top position of this view relative to its parent.
7569 *
7570 * @return The top of this view, in pixels.
7571 */
7572 @ViewDebug.CapturedViewProperty
7573 public final int getTop() {
7574 return mTop;
7575 }
7576
7577 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007578 * Sets the top position of this view relative to its parent. This method is meant to be called
7579 * by the layout system and should not generally be called otherwise, because the property
7580 * may be changed at any time by the layout.
7581 *
7582 * @param top The top of this view, in pixels.
7583 */
7584 public final void setTop(int top) {
7585 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007586 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007587 final boolean matrixIsIdentity = mTransformationInfo == null
7588 || mTransformationInfo.mMatrixIsIdentity;
7589 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007590 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007591 int minTop;
7592 int yLoc;
7593 if (top < mTop) {
7594 minTop = top;
7595 yLoc = top - mTop;
7596 } else {
7597 minTop = mTop;
7598 yLoc = 0;
7599 }
Chet Haasee9140a72011-02-16 16:23:29 -08007600 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007601 }
7602 } else {
7603 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007604 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007605 }
7606
Chet Haaseed032702010-10-01 14:05:54 -07007607 int width = mRight - mLeft;
7608 int oldHeight = mBottom - mTop;
7609
Chet Haase21cd1382010-09-01 17:42:29 -07007610 mTop = top;
7611
Chet Haaseed032702010-10-01 14:05:54 -07007612 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7613
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007614 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007615 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7616 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007617 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007618 }
Chet Haase21cd1382010-09-01 17:42:29 -07007619 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007620 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007621 }
Chet Haase55dbb652010-12-21 20:15:08 -08007622 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007623 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007624 }
7625 }
7626
7627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 * Bottom position of this view relative to its parent.
7629 *
7630 * @return The bottom of this view, in pixels.
7631 */
7632 @ViewDebug.CapturedViewProperty
7633 public final int getBottom() {
7634 return mBottom;
7635 }
7636
7637 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007638 * True if this view has changed since the last time being drawn.
7639 *
7640 * @return The dirty state of this view.
7641 */
7642 public boolean isDirty() {
7643 return (mPrivateFlags & DIRTY_MASK) != 0;
7644 }
7645
7646 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007647 * Sets the bottom position of this view relative to its parent. This method is meant to be
7648 * called by the layout system and should not generally be called otherwise, because the
7649 * property may be changed at any time by the layout.
7650 *
7651 * @param bottom The bottom of this view, in pixels.
7652 */
7653 public final void setBottom(int bottom) {
7654 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007655 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007656 final boolean matrixIsIdentity = mTransformationInfo == null
7657 || mTransformationInfo.mMatrixIsIdentity;
7658 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007659 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007660 int maxBottom;
7661 if (bottom < mBottom) {
7662 maxBottom = mBottom;
7663 } else {
7664 maxBottom = bottom;
7665 }
Chet Haasee9140a72011-02-16 16:23:29 -08007666 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007667 }
7668 } else {
7669 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007670 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007671 }
7672
Chet Haaseed032702010-10-01 14:05:54 -07007673 int width = mRight - mLeft;
7674 int oldHeight = mBottom - mTop;
7675
Chet Haase21cd1382010-09-01 17:42:29 -07007676 mBottom = bottom;
7677
Chet Haaseed032702010-10-01 14:05:54 -07007678 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7679
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007680 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007681 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7682 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007683 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007684 }
Chet Haase21cd1382010-09-01 17:42:29 -07007685 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007686 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007687 }
Chet Haase55dbb652010-12-21 20:15:08 -08007688 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007689 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007690 }
7691 }
7692
7693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 * Left position of this view relative to its parent.
7695 *
7696 * @return The left edge of this view, in pixels.
7697 */
7698 @ViewDebug.CapturedViewProperty
7699 public final int getLeft() {
7700 return mLeft;
7701 }
7702
7703 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007704 * Sets the left position of this view relative to its parent. This method is meant to be called
7705 * by the layout system and should not generally be called otherwise, because the property
7706 * may be changed at any time by the layout.
7707 *
7708 * @param left The bottom of this view, in pixels.
7709 */
7710 public final void setLeft(int left) {
7711 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007712 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007713 final boolean matrixIsIdentity = mTransformationInfo == null
7714 || mTransformationInfo.mMatrixIsIdentity;
7715 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007716 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007717 int minLeft;
7718 int xLoc;
7719 if (left < mLeft) {
7720 minLeft = left;
7721 xLoc = left - mLeft;
7722 } else {
7723 minLeft = mLeft;
7724 xLoc = 0;
7725 }
Chet Haasee9140a72011-02-16 16:23:29 -08007726 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007727 }
7728 } else {
7729 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007730 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007731 }
7732
Chet Haaseed032702010-10-01 14:05:54 -07007733 int oldWidth = mRight - mLeft;
7734 int height = mBottom - mTop;
7735
Chet Haase21cd1382010-09-01 17:42:29 -07007736 mLeft = left;
7737
Chet Haaseed032702010-10-01 14:05:54 -07007738 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7739
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007740 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007741 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7742 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007743 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007744 }
Chet Haase21cd1382010-09-01 17:42:29 -07007745 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007746 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007747 }
Chet Haase55dbb652010-12-21 20:15:08 -08007748 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007749 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007750 }
7751 }
7752
7753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 * Right position of this view relative to its parent.
7755 *
7756 * @return The right edge of this view, in pixels.
7757 */
7758 @ViewDebug.CapturedViewProperty
7759 public final int getRight() {
7760 return mRight;
7761 }
7762
7763 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007764 * Sets the right position of this view relative to its parent. This method is meant to be called
7765 * by the layout system and should not generally be called otherwise, because the property
7766 * may be changed at any time by the layout.
7767 *
7768 * @param right The bottom of this view, in pixels.
7769 */
7770 public final void setRight(int right) {
7771 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007772 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007773 final boolean matrixIsIdentity = mTransformationInfo == null
7774 || mTransformationInfo.mMatrixIsIdentity;
7775 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007776 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007777 int maxRight;
7778 if (right < mRight) {
7779 maxRight = mRight;
7780 } else {
7781 maxRight = right;
7782 }
Chet Haasee9140a72011-02-16 16:23:29 -08007783 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007784 }
7785 } else {
7786 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007787 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007788 }
7789
Chet Haaseed032702010-10-01 14:05:54 -07007790 int oldWidth = mRight - mLeft;
7791 int height = mBottom - mTop;
7792
Chet Haase21cd1382010-09-01 17:42:29 -07007793 mRight = right;
7794
Chet Haaseed032702010-10-01 14:05:54 -07007795 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7796
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007797 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007798 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7799 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007800 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007801 }
Chet Haase21cd1382010-09-01 17:42:29 -07007802 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007803 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007804 }
Chet Haase55dbb652010-12-21 20:15:08 -08007805 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007806 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007807 }
7808 }
7809
7810 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007811 * The visual x position of this view, in pixels. This is equivalent to the
7812 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007813 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007814 *
Chet Haasedf030d22010-07-30 17:22:38 -07007815 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007816 */
Chet Haasedf030d22010-07-30 17:22:38 -07007817 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007818 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007819 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007820
Chet Haasedf030d22010-07-30 17:22:38 -07007821 /**
7822 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7823 * {@link #setTranslationX(float) translationX} property to be the difference between
7824 * the x value passed in and the current {@link #getLeft() left} property.
7825 *
7826 * @param x The visual x position of this view, in pixels.
7827 */
7828 public void setX(float x) {
7829 setTranslationX(x - mLeft);
7830 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007831
Chet Haasedf030d22010-07-30 17:22:38 -07007832 /**
7833 * The visual y position of this view, in pixels. This is equivalent to the
7834 * {@link #setTranslationY(float) translationY} property plus the current
7835 * {@link #getTop() top} property.
7836 *
7837 * @return The visual y position of this view, in pixels.
7838 */
7839 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007840 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007841 }
7842
7843 /**
7844 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7845 * {@link #setTranslationY(float) translationY} property to be the difference between
7846 * the y value passed in and the current {@link #getTop() top} property.
7847 *
7848 * @param y The visual y position of this view, in pixels.
7849 */
7850 public void setY(float y) {
7851 setTranslationY(y - mTop);
7852 }
7853
7854
7855 /**
7856 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7857 * This position is post-layout, in addition to wherever the object's
7858 * layout placed it.
7859 *
7860 * @return The horizontal position of this view relative to its left position, in pixels.
7861 */
7862 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007863 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007864 }
7865
7866 /**
7867 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7868 * This effectively positions the object post-layout, in addition to wherever the object's
7869 * layout placed it.
7870 *
7871 * @param translationX The horizontal position of this view relative to its left position,
7872 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007873 *
7874 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007875 */
7876 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007877 ensureTransformationInfo();
7878 final TransformationInfo info = mTransformationInfo;
7879 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007880 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007881 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007882 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007883 info.mTranslationX = translationX;
7884 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007885 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007886 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007887 }
7888 }
7889
7890 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007891 * The horizontal location of this view relative to its {@link #getTop() top} position.
7892 * This position is post-layout, in addition to wherever the object's
7893 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007894 *
Chet Haasedf030d22010-07-30 17:22:38 -07007895 * @return The vertical position of this view relative to its top position,
7896 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007897 */
Chet Haasedf030d22010-07-30 17:22:38 -07007898 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007899 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007900 }
7901
7902 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007903 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7904 * This effectively positions the object post-layout, in addition to wherever the object's
7905 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007906 *
Chet Haasedf030d22010-07-30 17:22:38 -07007907 * @param translationY The vertical position of this view relative to its top position,
7908 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007909 *
7910 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007911 */
Chet Haasedf030d22010-07-30 17:22:38 -07007912 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007913 ensureTransformationInfo();
7914 final TransformationInfo info = mTransformationInfo;
7915 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007916 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007917 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007918 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007919 info.mTranslationY = translationY;
7920 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007921 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007922 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007923 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007924 }
7925
7926 /**
Romain Guyda489792011-02-03 01:05:15 -08007927 * @hide
7928 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007929 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007930 ensureTransformationInfo();
7931 final TransformationInfo info = mTransformationInfo;
7932 info.mTranslationX = x;
7933 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007934 }
7935
7936 /**
7937 * @hide
7938 */
7939 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007940 ensureTransformationInfo();
7941 final TransformationInfo info = mTransformationInfo;
7942 info.mTranslationY = y;
7943 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007944 }
7945
7946 /**
7947 * @hide
7948 */
Romain Guyda489792011-02-03 01:05:15 -08007949 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007950 ensureTransformationInfo();
7951 final TransformationInfo info = mTransformationInfo;
7952 info.mTranslationX = x - mLeft;
7953 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007954 }
7955
7956 /**
7957 * @hide
7958 */
7959 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007960 ensureTransformationInfo();
7961 final TransformationInfo info = mTransformationInfo;
7962 info.mTranslationY = y - mTop;
7963 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007964 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007965
Romain Guyda489792011-02-03 01:05:15 -08007966 /**
7967 * @hide
7968 */
7969 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007970 ensureTransformationInfo();
7971 final TransformationInfo info = mTransformationInfo;
7972 info.mScaleX = x;
7973 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007974 }
7975
7976 /**
7977 * @hide
7978 */
7979 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007980 ensureTransformationInfo();
7981 final TransformationInfo info = mTransformationInfo;
7982 info.mScaleY = y;
7983 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007984 }
7985
7986 /**
7987 * @hide
7988 */
7989 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007990 ensureTransformationInfo();
7991 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007992 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007993
Romain Guyda489792011-02-03 01:05:15 -08007994 /**
7995 * @hide
7996 */
7997 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007998 ensureTransformationInfo();
7999 final TransformationInfo info = mTransformationInfo;
8000 info.mRotationY = y;
8001 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008002 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008003
Romain Guyda489792011-02-03 01:05:15 -08008004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008005 * Hit rectangle in parent's coordinates
8006 *
8007 * @param outRect The hit rectangle of the view.
8008 */
8009 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008010 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008011 final TransformationInfo info = mTransformationInfo;
8012 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008013 outRect.set(mLeft, mTop, mRight, mBottom);
8014 } else {
8015 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008016 tmpRect.set(-info.mPivotX, -info.mPivotY,
8017 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8018 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008019 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8020 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008021 }
8022 }
8023
8024 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008025 * Determines whether the given point, in local coordinates is inside the view.
8026 */
8027 /*package*/ final boolean pointInView(float localX, float localY) {
8028 return localX >= 0 && localX < (mRight - mLeft)
8029 && localY >= 0 && localY < (mBottom - mTop);
8030 }
8031
8032 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008033 * Utility method to determine whether the given point, in local coordinates,
8034 * is inside the view, where the area of the view is expanded by the slop factor.
8035 * This method is called while processing touch-move events to determine if the event
8036 * is still within the view.
8037 */
8038 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008039 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008040 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 }
8042
8043 /**
8044 * When a view has focus and the user navigates away from it, the next view is searched for
8045 * starting from the rectangle filled in by this method.
8046 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008047 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8048 * of the view. However, if your view maintains some idea of internal selection,
8049 * such as a cursor, or a selected row or column, you should override this method and
8050 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 *
8052 * @param r The rectangle to fill in, in this view's coordinates.
8053 */
8054 public void getFocusedRect(Rect r) {
8055 getDrawingRect(r);
8056 }
8057
8058 /**
8059 * If some part of this view is not clipped by any of its parents, then
8060 * return that area in r in global (root) coordinates. To convert r to local
8061 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
8062 * -globalOffset.y)) If the view is completely clipped or translated out,
8063 * return false.
8064 *
8065 * @param r If true is returned, r holds the global coordinates of the
8066 * visible portion of this view.
8067 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8068 * between this view and its root. globalOffet may be null.
8069 * @return true if r is non-empty (i.e. part of the view is visible at the
8070 * root level.
8071 */
8072 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8073 int width = mRight - mLeft;
8074 int height = mBottom - mTop;
8075 if (width > 0 && height > 0) {
8076 r.set(0, 0, width, height);
8077 if (globalOffset != null) {
8078 globalOffset.set(-mScrollX, -mScrollY);
8079 }
8080 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8081 }
8082 return false;
8083 }
8084
8085 public final boolean getGlobalVisibleRect(Rect r) {
8086 return getGlobalVisibleRect(r, null);
8087 }
8088
8089 public final boolean getLocalVisibleRect(Rect r) {
8090 Point offset = new Point();
8091 if (getGlobalVisibleRect(r, offset)) {
8092 r.offset(-offset.x, -offset.y); // make r local
8093 return true;
8094 }
8095 return false;
8096 }
8097
8098 /**
8099 * Offset this view's vertical location by the specified number of pixels.
8100 *
8101 * @param offset the number of pixels to offset the view by
8102 */
8103 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008104 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008105 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008106 final boolean matrixIsIdentity = mTransformationInfo == null
8107 || mTransformationInfo.mMatrixIsIdentity;
8108 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008109 final ViewParent p = mParent;
8110 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008111 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008112 int minTop;
8113 int maxBottom;
8114 int yLoc;
8115 if (offset < 0) {
8116 minTop = mTop + offset;
8117 maxBottom = mBottom;
8118 yLoc = offset;
8119 } else {
8120 minTop = mTop;
8121 maxBottom = mBottom + offset;
8122 yLoc = 0;
8123 }
8124 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8125 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008126 }
8127 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008128 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008129 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008130
Chet Haasec3aa3612010-06-17 08:50:37 -07008131 mTop += offset;
8132 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008133
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008134 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008135 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008136 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008137 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008138 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 }
8141
8142 /**
8143 * Offset this view's horizontal location by the specified amount of pixels.
8144 *
8145 * @param offset the numer of pixels to offset the view by
8146 */
8147 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008149 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008150 final boolean matrixIsIdentity = mTransformationInfo == null
8151 || mTransformationInfo.mMatrixIsIdentity;
8152 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008153 final ViewParent p = mParent;
8154 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008155 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008156 int minLeft;
8157 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008158 if (offset < 0) {
8159 minLeft = mLeft + offset;
8160 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008161 } else {
8162 minLeft = mLeft;
8163 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008164 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008165 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008166 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008167 }
8168 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008169 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008170 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008171
Chet Haasec3aa3612010-06-17 08:50:37 -07008172 mLeft += offset;
8173 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008174
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008175 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008176 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008177 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008179 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181 }
8182
8183 /**
8184 * Get the LayoutParams associated with this view. All views should have
8185 * layout parameters. These supply parameters to the <i>parent</i> of this
8186 * view specifying how it should be arranged. There are many subclasses of
8187 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8188 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008189 *
8190 * This method may return null if this View is not attached to a parent
8191 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8192 * was not invoked successfully. When a View is attached to a parent
8193 * ViewGroup, this method must not return null.
8194 *
8195 * @return The LayoutParams associated with this view, or null if no
8196 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008198 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 public ViewGroup.LayoutParams getLayoutParams() {
8200 return mLayoutParams;
8201 }
8202
8203 /**
8204 * Set the layout parameters associated with this view. These supply
8205 * parameters to the <i>parent</i> of this view specifying how it should be
8206 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8207 * correspond to the different subclasses of ViewGroup that are responsible
8208 * for arranging their children.
8209 *
Romain Guy01c174b2011-02-22 11:51:06 -08008210 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 */
8212 public void setLayoutParams(ViewGroup.LayoutParams params) {
8213 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008214 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 }
8216 mLayoutParams = params;
8217 requestLayout();
8218 }
8219
8220 /**
8221 * Set the scrolled position of your view. This will cause a call to
8222 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8223 * invalidated.
8224 * @param x the x position to scroll to
8225 * @param y the y position to scroll to
8226 */
8227 public void scrollTo(int x, int y) {
8228 if (mScrollX != x || mScrollY != y) {
8229 int oldX = mScrollX;
8230 int oldY = mScrollY;
8231 mScrollX = x;
8232 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008233 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008234 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008235 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008236 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 }
8239 }
8240
8241 /**
8242 * Move the scrolled position of your view. This will cause a call to
8243 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8244 * invalidated.
8245 * @param x the amount of pixels to scroll by horizontally
8246 * @param y the amount of pixels to scroll by vertically
8247 */
8248 public void scrollBy(int x, int y) {
8249 scrollTo(mScrollX + x, mScrollY + y);
8250 }
8251
8252 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008253 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8254 * animation to fade the scrollbars out after a default delay. If a subclass
8255 * provides animated scrolling, the start delay should equal the duration
8256 * of the scrolling animation.</p>
8257 *
8258 * <p>The animation starts only if at least one of the scrollbars is
8259 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8260 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8261 * this method returns true, and false otherwise. If the animation is
8262 * started, this method calls {@link #invalidate()}; in that case the
8263 * caller should not call {@link #invalidate()}.</p>
8264 *
8265 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008266 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008267 *
8268 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8269 * and {@link #scrollTo(int, int)}.</p>
8270 *
8271 * @return true if the animation is played, false otherwise
8272 *
8273 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008274 * @see #scrollBy(int, int)
8275 * @see #scrollTo(int, int)
8276 * @see #isHorizontalScrollBarEnabled()
8277 * @see #isVerticalScrollBarEnabled()
8278 * @see #setHorizontalScrollBarEnabled(boolean)
8279 * @see #setVerticalScrollBarEnabled(boolean)
8280 */
8281 protected boolean awakenScrollBars() {
8282 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008283 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008284 }
8285
8286 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008287 * Trigger the scrollbars to draw.
8288 * This method differs from awakenScrollBars() only in its default duration.
8289 * initialAwakenScrollBars() will show the scroll bars for longer than
8290 * usual to give the user more of a chance to notice them.
8291 *
8292 * @return true if the animation is played, false otherwise.
8293 */
8294 private boolean initialAwakenScrollBars() {
8295 return mScrollCache != null &&
8296 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8297 }
8298
8299 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008300 * <p>
8301 * Trigger the scrollbars to draw. When invoked this method starts an
8302 * animation to fade the scrollbars out after a fixed delay. If a subclass
8303 * provides animated scrolling, the start delay should equal the duration of
8304 * the scrolling animation.
8305 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008306 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008307 * <p>
8308 * The animation starts only if at least one of the scrollbars is enabled,
8309 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8310 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8311 * this method returns true, and false otherwise. If the animation is
8312 * started, this method calls {@link #invalidate()}; in that case the caller
8313 * should not call {@link #invalidate()}.
8314 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008315 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008316 * <p>
8317 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008318 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008319 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008320 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008321 * @param startDelay the delay, in milliseconds, after which the animation
8322 * should start; when the delay is 0, the animation starts
8323 * immediately
8324 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008325 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008326 * @see #scrollBy(int, int)
8327 * @see #scrollTo(int, int)
8328 * @see #isHorizontalScrollBarEnabled()
8329 * @see #isVerticalScrollBarEnabled()
8330 * @see #setHorizontalScrollBarEnabled(boolean)
8331 * @see #setVerticalScrollBarEnabled(boolean)
8332 */
8333 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008334 return awakenScrollBars(startDelay, true);
8335 }
Joe Malin32736f02011-01-19 16:14:20 -08008336
Mike Cleron290947b2009-09-29 18:34:32 -07008337 /**
8338 * <p>
8339 * Trigger the scrollbars to draw. When invoked this method starts an
8340 * animation to fade the scrollbars out after a fixed delay. If a subclass
8341 * provides animated scrolling, the start delay should equal the duration of
8342 * the scrolling animation.
8343 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008344 *
Mike Cleron290947b2009-09-29 18:34:32 -07008345 * <p>
8346 * The animation starts only if at least one of the scrollbars is enabled,
8347 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8348 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8349 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008350 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008351 * is set to true; in that case the caller
8352 * should not call {@link #invalidate()}.
8353 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008354 *
Mike Cleron290947b2009-09-29 18:34:32 -07008355 * <p>
8356 * This method should be invoked everytime a subclass directly updates the
8357 * scroll parameters.
8358 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008359 *
Mike Cleron290947b2009-09-29 18:34:32 -07008360 * @param startDelay the delay, in milliseconds, after which the animation
8361 * should start; when the delay is 0, the animation starts
8362 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008363 *
Mike Cleron290947b2009-09-29 18:34:32 -07008364 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008365 *
Mike Cleron290947b2009-09-29 18:34:32 -07008366 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008367 *
Mike Cleron290947b2009-09-29 18:34:32 -07008368 * @see #scrollBy(int, int)
8369 * @see #scrollTo(int, int)
8370 * @see #isHorizontalScrollBarEnabled()
8371 * @see #isVerticalScrollBarEnabled()
8372 * @see #setHorizontalScrollBarEnabled(boolean)
8373 * @see #setVerticalScrollBarEnabled(boolean)
8374 */
8375 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008376 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008377
Mike Cleronf116bf82009-09-27 19:14:12 -07008378 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8379 return false;
8380 }
8381
8382 if (scrollCache.scrollBar == null) {
8383 scrollCache.scrollBar = new ScrollBarDrawable();
8384 }
8385
8386 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8387
Mike Cleron290947b2009-09-29 18:34:32 -07008388 if (invalidate) {
8389 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008390 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008391 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008392
8393 if (scrollCache.state == ScrollabilityCache.OFF) {
8394 // FIXME: this is copied from WindowManagerService.
8395 // We should get this value from the system when it
8396 // is possible to do so.
8397 final int KEY_REPEAT_FIRST_DELAY = 750;
8398 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8399 }
8400
8401 // Tell mScrollCache when we should start fading. This may
8402 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008403 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008404 scrollCache.fadeStartTime = fadeStartTime;
8405 scrollCache.state = ScrollabilityCache.ON;
8406
8407 // Schedule our fader to run, unscheduling any old ones first
8408 if (mAttachInfo != null) {
8409 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8410 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8411 }
8412
8413 return true;
8414 }
8415
8416 return false;
8417 }
8418
8419 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008420 * Do not invalidate views which are not visible and which are not running an animation. They
8421 * will not get drawn and they should not set dirty flags as if they will be drawn
8422 */
8423 private boolean skipInvalidate() {
8424 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8425 (!(mParent instanceof ViewGroup) ||
8426 !((ViewGroup) mParent).isViewTransitioning(this));
8427 }
8428 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008429 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008430 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8431 * in the future. This must be called from a UI thread. To call from a non-UI
8432 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 *
8434 * WARNING: This method is destructive to dirty.
8435 * @param dirty the rectangle representing the bounds of the dirty region
8436 */
8437 public void invalidate(Rect dirty) {
8438 if (ViewDebug.TRACE_HIERARCHY) {
8439 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8440 }
8441
Chet Haaseaceafe62011-08-26 15:44:33 -07008442 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008443 return;
8444 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008445 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008446 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8447 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008449 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008450 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 final ViewParent p = mParent;
8452 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008453 //noinspection PointlessBooleanExpression,ConstantConditions
8454 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8455 if (p != null && ai != null && ai.mHardwareAccelerated) {
8456 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008457 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008458 p.invalidateChild(this, null);
8459 return;
8460 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 if (p != null && ai != null) {
8463 final int scrollX = mScrollX;
8464 final int scrollY = mScrollY;
8465 final Rect r = ai.mTmpInvalRect;
8466 r.set(dirty.left - scrollX, dirty.top - scrollY,
8467 dirty.right - scrollX, dirty.bottom - scrollY);
8468 mParent.invalidateChild(this, r);
8469 }
8470 }
8471 }
8472
8473 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008474 * Mark the area defined by the rect (l,t,r,b) as needing to be drawn.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008476 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8477 * will be called at some point in the future. This must be called from
8478 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 * @param l the left position of the dirty region
8480 * @param t the top position of the dirty region
8481 * @param r the right position of the dirty region
8482 * @param b the bottom position of the dirty region
8483 */
8484 public void invalidate(int l, int t, int r, int b) {
8485 if (ViewDebug.TRACE_HIERARCHY) {
8486 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8487 }
8488
Chet Haaseaceafe62011-08-26 15:44:33 -07008489 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008490 return;
8491 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008492 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008493 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8494 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008495 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008496 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008497 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 final ViewParent p = mParent;
8499 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008500 //noinspection PointlessBooleanExpression,ConstantConditions
8501 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8502 if (p != null && ai != null && ai.mHardwareAccelerated) {
8503 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008504 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008505 p.invalidateChild(this, null);
8506 return;
8507 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 if (p != null && ai != null && l < r && t < b) {
8510 final int scrollX = mScrollX;
8511 final int scrollY = mScrollY;
8512 final Rect tmpr = ai.mTmpInvalRect;
8513 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8514 p.invalidateChild(this, tmpr);
8515 }
8516 }
8517 }
8518
8519 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008520 * Invalidate the whole view. If the view is visible,
8521 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8522 * the future. This must be called from a UI thread. To call from a non-UI thread,
8523 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008524 */
8525 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008526 invalidate(true);
8527 }
Joe Malin32736f02011-01-19 16:14:20 -08008528
Chet Haaseed032702010-10-01 14:05:54 -07008529 /**
8530 * This is where the invalidate() work actually happens. A full invalidate()
8531 * causes the drawing cache to be invalidated, but this function can be called with
8532 * invalidateCache set to false to skip that invalidation step for cases that do not
8533 * need it (for example, a component that remains at the same dimensions with the same
8534 * content).
8535 *
8536 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8537 * well. This is usually true for a full invalidate, but may be set to false if the
8538 * View's contents or dimensions have not changed.
8539 */
Romain Guy849d0a32011-02-01 17:20:48 -08008540 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 if (ViewDebug.TRACE_HIERARCHY) {
8542 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8543 }
8544
Chet Haaseaceafe62011-08-26 15:44:33 -07008545 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008546 return;
8547 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008548 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008549 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008550 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8551 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008552 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008553 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008554 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008555 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008556 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008559 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008560 //noinspection PointlessBooleanExpression,ConstantConditions
8561 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8562 if (p != null && ai != null && ai.mHardwareAccelerated) {
8563 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008564 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008565 p.invalidateChild(this, null);
8566 return;
8567 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008568 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 if (p != null && ai != null) {
8571 final Rect r = ai.mTmpInvalRect;
8572 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8573 // Don't call invalidate -- we don't want to internally scroll
8574 // our own bounds
8575 p.invalidateChild(this, r);
8576 }
8577 }
8578 }
8579
8580 /**
Romain Guyda489792011-02-03 01:05:15 -08008581 * @hide
8582 */
8583 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008584 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008585 return;
8586 }
Romain Guyda489792011-02-03 01:05:15 -08008587 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8588 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8589 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8590 if (mParent instanceof View) {
8591 ((View) mParent).mPrivateFlags |= INVALIDATED;
8592 }
8593 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008594 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008595 mPrivateFlags |= INVALIDATED;
8596 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008597 if (mParent != null && mAttachInfo != null) {
8598 if (mAttachInfo.mHardwareAccelerated) {
8599 mParent.invalidateChild(this, null);
8600 } else {
8601 final Rect r = mAttachInfo.mTmpInvalRect;
8602 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8603 // Don't call invalidate -- we don't want to internally scroll
8604 // our own bounds
8605 mParent.invalidateChild(this, r);
8606 }
Romain Guyda489792011-02-03 01:05:15 -08008607 }
8608 }
8609 }
8610
8611 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008612 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008613 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8614 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008615 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8616 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008617 *
8618 * @hide
8619 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008620 protected void invalidateParentCaches() {
8621 if (mParent instanceof View) {
8622 ((View) mParent).mPrivateFlags |= INVALIDATED;
8623 }
8624 }
Joe Malin32736f02011-01-19 16:14:20 -08008625
Romain Guy0fd89bf2011-01-26 15:41:30 -08008626 /**
8627 * Used to indicate that the parent of this view should be invalidated. This functionality
8628 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8629 * which is necessary when various parent-managed properties of the view change, such as
8630 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8631 * an invalidation event to the parent.
8632 *
8633 * @hide
8634 */
8635 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008636 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008637 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008638 }
8639 }
8640
8641 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008642 * Indicates whether this View is opaque. An opaque View guarantees that it will
8643 * draw all the pixels overlapping its bounds using a fully opaque color.
8644 *
8645 * Subclasses of View should override this method whenever possible to indicate
8646 * whether an instance is opaque. Opaque Views are treated in a special way by
8647 * the View hierarchy, possibly allowing it to perform optimizations during
8648 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008649 *
Romain Guy24443ea2009-05-11 11:56:30 -07008650 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008651 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008652 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008653 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008654 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008655 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8656 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008657 }
8658
Adam Powell20232d02010-12-08 21:08:53 -08008659 /**
8660 * @hide
8661 */
8662 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008663 // Opaque if:
8664 // - Has a background
8665 // - Background is opaque
8666 // - Doesn't have scrollbars or scrollbars are inside overlay
8667
8668 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8669 mPrivateFlags |= OPAQUE_BACKGROUND;
8670 } else {
8671 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8672 }
8673
8674 final int flags = mViewFlags;
8675 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8676 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8677 mPrivateFlags |= OPAQUE_SCROLLBARS;
8678 } else {
8679 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8680 }
8681 }
8682
8683 /**
8684 * @hide
8685 */
8686 protected boolean hasOpaqueScrollbars() {
8687 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008688 }
8689
8690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 * @return A handler associated with the thread running the View. This
8692 * handler can be used to pump events in the UI events queue.
8693 */
8694 public Handler getHandler() {
8695 if (mAttachInfo != null) {
8696 return mAttachInfo.mHandler;
8697 }
8698 return null;
8699 }
8700
8701 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008702 * <p>Causes the Runnable to be added to the message queue.
8703 * The runnable will be run on the user interface thread.</p>
8704 *
8705 * <p>This method can be invoked from outside of the UI thread
8706 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 *
8708 * @param action The Runnable that will be executed.
8709 *
8710 * @return Returns true if the Runnable was successfully placed in to the
8711 * message queue. Returns false on failure, usually because the
8712 * looper processing the message queue is exiting.
8713 */
8714 public boolean post(Runnable action) {
8715 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008716 AttachInfo attachInfo = mAttachInfo;
8717 if (attachInfo != null) {
8718 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 } else {
8720 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008721 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 return true;
8723 }
8724
8725 return handler.post(action);
8726 }
8727
8728 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008729 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008731 * The runnable will be run on the user interface thread.</p>
8732 *
8733 * <p>This method can be invoked from outside of the UI thread
8734 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 *
8736 * @param action The Runnable that will be executed.
8737 * @param delayMillis The delay (in milliseconds) until the Runnable
8738 * will be executed.
8739 *
8740 * @return true if the Runnable was successfully placed in to the
8741 * message queue. Returns false on failure, usually because the
8742 * looper processing the message queue is exiting. Note that a
8743 * result of true does not mean the Runnable will be processed --
8744 * if the looper is quit before the delivery time of the message
8745 * occurs then the message will be dropped.
8746 */
8747 public boolean postDelayed(Runnable action, long delayMillis) {
8748 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008749 AttachInfo attachInfo = mAttachInfo;
8750 if (attachInfo != null) {
8751 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 } else {
8753 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008754 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 return true;
8756 }
8757
8758 return handler.postDelayed(action, delayMillis);
8759 }
8760
8761 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008762 * <p>Removes the specified Runnable from the message queue.</p>
8763 *
8764 * <p>This method can be invoked from outside of the UI thread
8765 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 *
8767 * @param action The Runnable to remove from the message handling queue
8768 *
8769 * @return true if this view could ask the Handler to remove the Runnable,
8770 * false otherwise. When the returned value is true, the Runnable
8771 * may or may not have been actually removed from the message queue
8772 * (for instance, if the Runnable was not in the queue already.)
8773 */
8774 public boolean removeCallbacks(Runnable action) {
8775 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008776 AttachInfo attachInfo = mAttachInfo;
8777 if (attachInfo != null) {
8778 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 } else {
8780 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008781 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 return true;
8783 }
8784
8785 handler.removeCallbacks(action);
8786 return true;
8787 }
8788
8789 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008790 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8791 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 *
Romain Guye63a4f32011-08-11 11:33:31 -07008793 * <p>This method can be invoked from outside of the UI thread
8794 * only when this View is attached to a window.</p>
8795 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008796 * @see #invalidate()
8797 */
8798 public void postInvalidate() {
8799 postInvalidateDelayed(0);
8800 }
8801
8802 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008803 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8804 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8805 *
8806 * <p>This method can be invoked from outside of the UI thread
8807 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 *
8809 * @param left The left coordinate of the rectangle to invalidate.
8810 * @param top The top coordinate of the rectangle to invalidate.
8811 * @param right The right coordinate of the rectangle to invalidate.
8812 * @param bottom The bottom coordinate of the rectangle to invalidate.
8813 *
8814 * @see #invalidate(int, int, int, int)
8815 * @see #invalidate(Rect)
8816 */
8817 public void postInvalidate(int left, int top, int right, int bottom) {
8818 postInvalidateDelayed(0, left, top, right, bottom);
8819 }
8820
8821 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008822 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8823 * loop. Waits for the specified amount of time.</p>
8824 *
8825 * <p>This method can be invoked from outside of the UI thread
8826 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 *
8828 * @param delayMilliseconds the duration in milliseconds to delay the
8829 * invalidation by
8830 */
8831 public void postInvalidateDelayed(long delayMilliseconds) {
8832 // We try only with the AttachInfo because there's no point in invalidating
8833 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008834 AttachInfo attachInfo = mAttachInfo;
8835 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 Message msg = Message.obtain();
8837 msg.what = AttachInfo.INVALIDATE_MSG;
8838 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008839 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008840 }
8841 }
8842
8843 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008844 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8845 * through the event loop. Waits for the specified amount of time.</p>
8846 *
8847 * <p>This method can be invoked from outside of the UI thread
8848 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008849 *
8850 * @param delayMilliseconds the duration in milliseconds to delay the
8851 * invalidation by
8852 * @param left The left coordinate of the rectangle to invalidate.
8853 * @param top The top coordinate of the rectangle to invalidate.
8854 * @param right The right coordinate of the rectangle to invalidate.
8855 * @param bottom The bottom coordinate of the rectangle to invalidate.
8856 */
8857 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8858 int right, int bottom) {
8859
8860 // We try only with the AttachInfo because there's no point in invalidating
8861 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008862 AttachInfo attachInfo = mAttachInfo;
8863 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8865 info.target = this;
8866 info.left = left;
8867 info.top = top;
8868 info.right = right;
8869 info.bottom = bottom;
8870
8871 final Message msg = Message.obtain();
8872 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8873 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008874 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 }
8876 }
8877
8878 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008879 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8880 * This event is sent at most once every
8881 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8882 */
8883 private void postSendViewScrolledAccessibilityEventCallback() {
8884 if (mSendViewScrolledAccessibilityEvent == null) {
8885 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8886 }
8887 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8888 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8889 postDelayed(mSendViewScrolledAccessibilityEvent,
8890 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8891 }
8892 }
8893
8894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 * Called by a parent to request that a child update its values for mScrollX
8896 * and mScrollY if necessary. This will typically be done if the child is
8897 * animating a scroll using a {@link android.widget.Scroller Scroller}
8898 * object.
8899 */
8900 public void computeScroll() {
8901 }
8902
8903 /**
8904 * <p>Indicate whether the horizontal edges are faded when the view is
8905 * scrolled horizontally.</p>
8906 *
8907 * @return true if the horizontal edges should are faded on scroll, false
8908 * otherwise
8909 *
8910 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008911 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008912 */
8913 public boolean isHorizontalFadingEdgeEnabled() {
8914 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8915 }
8916
8917 /**
8918 * <p>Define whether the horizontal edges should be faded when this view
8919 * is scrolled horizontally.</p>
8920 *
8921 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8922 * be faded when the view is scrolled
8923 * horizontally
8924 *
8925 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008926 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 */
8928 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8929 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8930 if (horizontalFadingEdgeEnabled) {
8931 initScrollCache();
8932 }
8933
8934 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8935 }
8936 }
8937
8938 /**
8939 * <p>Indicate whether the vertical edges are faded when the view is
8940 * scrolled horizontally.</p>
8941 *
8942 * @return true if the vertical edges should are faded on scroll, false
8943 * otherwise
8944 *
8945 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008946 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008947 */
8948 public boolean isVerticalFadingEdgeEnabled() {
8949 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8950 }
8951
8952 /**
8953 * <p>Define whether the vertical edges should be faded when this view
8954 * is scrolled vertically.</p>
8955 *
8956 * @param verticalFadingEdgeEnabled true if the vertical edges should
8957 * be faded when the view is scrolled
8958 * vertically
8959 *
8960 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008961 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 */
8963 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8964 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8965 if (verticalFadingEdgeEnabled) {
8966 initScrollCache();
8967 }
8968
8969 mViewFlags ^= FADING_EDGE_VERTICAL;
8970 }
8971 }
8972
8973 /**
8974 * Returns the strength, or intensity, of the top faded edge. The strength is
8975 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8976 * returns 0.0 or 1.0 but no value in between.
8977 *
8978 * Subclasses should override this method to provide a smoother fade transition
8979 * when scrolling occurs.
8980 *
8981 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8982 */
8983 protected float getTopFadingEdgeStrength() {
8984 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8985 }
8986
8987 /**
8988 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8989 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8990 * returns 0.0 or 1.0 but no value in between.
8991 *
8992 * Subclasses should override this method to provide a smoother fade transition
8993 * when scrolling occurs.
8994 *
8995 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8996 */
8997 protected float getBottomFadingEdgeStrength() {
8998 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8999 computeVerticalScrollRange() ? 1.0f : 0.0f;
9000 }
9001
9002 /**
9003 * Returns the strength, or intensity, of the left faded edge. The strength is
9004 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9005 * returns 0.0 or 1.0 but no value in between.
9006 *
9007 * Subclasses should override this method to provide a smoother fade transition
9008 * when scrolling occurs.
9009 *
9010 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9011 */
9012 protected float getLeftFadingEdgeStrength() {
9013 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9014 }
9015
9016 /**
9017 * Returns the strength, or intensity, of the right faded edge. The strength is
9018 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9019 * returns 0.0 or 1.0 but no value in between.
9020 *
9021 * Subclasses should override this method to provide a smoother fade transition
9022 * when scrolling occurs.
9023 *
9024 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9025 */
9026 protected float getRightFadingEdgeStrength() {
9027 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9028 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9029 }
9030
9031 /**
9032 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9033 * scrollbar is not drawn by default.</p>
9034 *
9035 * @return true if the horizontal scrollbar should be painted, false
9036 * otherwise
9037 *
9038 * @see #setHorizontalScrollBarEnabled(boolean)
9039 */
9040 public boolean isHorizontalScrollBarEnabled() {
9041 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9042 }
9043
9044 /**
9045 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9046 * scrollbar is not drawn by default.</p>
9047 *
9048 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9049 * be painted
9050 *
9051 * @see #isHorizontalScrollBarEnabled()
9052 */
9053 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9054 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9055 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009056 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009057 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009058 }
9059 }
9060
9061 /**
9062 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9063 * scrollbar is not drawn by default.</p>
9064 *
9065 * @return true if the vertical scrollbar should be painted, false
9066 * otherwise
9067 *
9068 * @see #setVerticalScrollBarEnabled(boolean)
9069 */
9070 public boolean isVerticalScrollBarEnabled() {
9071 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9072 }
9073
9074 /**
9075 * <p>Define whether the vertical scrollbar should be drawn or not. The
9076 * scrollbar is not drawn by default.</p>
9077 *
9078 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9079 * be painted
9080 *
9081 * @see #isVerticalScrollBarEnabled()
9082 */
9083 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9084 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9085 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009086 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009087 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 }
9089 }
9090
Adam Powell20232d02010-12-08 21:08:53 -08009091 /**
9092 * @hide
9093 */
9094 protected void recomputePadding() {
9095 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096 }
Joe Malin32736f02011-01-19 16:14:20 -08009097
Mike Cleronfe81d382009-09-28 14:22:16 -07009098 /**
9099 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009100 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009101 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009102 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009103 */
9104 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9105 initScrollCache();
9106 final ScrollabilityCache scrollabilityCache = mScrollCache;
9107 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009108 if (fadeScrollbars) {
9109 scrollabilityCache.state = ScrollabilityCache.OFF;
9110 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009111 scrollabilityCache.state = ScrollabilityCache.ON;
9112 }
9113 }
Joe Malin32736f02011-01-19 16:14:20 -08009114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 /**
Joe Malin32736f02011-01-19 16:14:20 -08009116 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009117 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009118 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009119 * @return true if scrollbar fading is enabled
9120 */
9121 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009122 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009123 }
Joe Malin32736f02011-01-19 16:14:20 -08009124
Mike Cleron52f0a642009-09-28 18:21:37 -07009125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009126 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9127 * inset. When inset, they add to the padding of the view. And the scrollbars
9128 * can be drawn inside the padding area or on the edge of the view. For example,
9129 * if a view has a background drawable and you want to draw the scrollbars
9130 * inside the padding specified by the drawable, you can use
9131 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9132 * appear at the edge of the view, ignoring the padding, then you can use
9133 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9134 * @param style the style of the scrollbars. Should be one of
9135 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9136 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9137 * @see #SCROLLBARS_INSIDE_OVERLAY
9138 * @see #SCROLLBARS_INSIDE_INSET
9139 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9140 * @see #SCROLLBARS_OUTSIDE_INSET
9141 */
9142 public void setScrollBarStyle(int style) {
9143 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9144 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009145 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009146 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009147 }
9148 }
9149
9150 /**
9151 * <p>Returns the current scrollbar style.</p>
9152 * @return the current scrollbar style
9153 * @see #SCROLLBARS_INSIDE_OVERLAY
9154 * @see #SCROLLBARS_INSIDE_INSET
9155 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9156 * @see #SCROLLBARS_OUTSIDE_INSET
9157 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009158 @ViewDebug.ExportedProperty(mapping = {
9159 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9160 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9161 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9162 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9163 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 public int getScrollBarStyle() {
9165 return mViewFlags & SCROLLBARS_STYLE_MASK;
9166 }
9167
9168 /**
9169 * <p>Compute the horizontal range that the horizontal scrollbar
9170 * represents.</p>
9171 *
9172 * <p>The range is expressed in arbitrary units that must be the same as the
9173 * units used by {@link #computeHorizontalScrollExtent()} and
9174 * {@link #computeHorizontalScrollOffset()}.</p>
9175 *
9176 * <p>The default range is the drawing width of this view.</p>
9177 *
9178 * @return the total horizontal range represented by the horizontal
9179 * scrollbar
9180 *
9181 * @see #computeHorizontalScrollExtent()
9182 * @see #computeHorizontalScrollOffset()
9183 * @see android.widget.ScrollBarDrawable
9184 */
9185 protected int computeHorizontalScrollRange() {
9186 return getWidth();
9187 }
9188
9189 /**
9190 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9191 * within the horizontal range. This value is used to compute the position
9192 * of the thumb within the scrollbar's track.</p>
9193 *
9194 * <p>The range is expressed in arbitrary units that must be the same as the
9195 * units used by {@link #computeHorizontalScrollRange()} and
9196 * {@link #computeHorizontalScrollExtent()}.</p>
9197 *
9198 * <p>The default offset is the scroll offset of this view.</p>
9199 *
9200 * @return the horizontal offset of the scrollbar's thumb
9201 *
9202 * @see #computeHorizontalScrollRange()
9203 * @see #computeHorizontalScrollExtent()
9204 * @see android.widget.ScrollBarDrawable
9205 */
9206 protected int computeHorizontalScrollOffset() {
9207 return mScrollX;
9208 }
9209
9210 /**
9211 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9212 * within the horizontal range. This value is used to compute the length
9213 * of the thumb within the scrollbar's track.</p>
9214 *
9215 * <p>The range is expressed in arbitrary units that must be the same as the
9216 * units used by {@link #computeHorizontalScrollRange()} and
9217 * {@link #computeHorizontalScrollOffset()}.</p>
9218 *
9219 * <p>The default extent is the drawing width of this view.</p>
9220 *
9221 * @return the horizontal extent of the scrollbar's thumb
9222 *
9223 * @see #computeHorizontalScrollRange()
9224 * @see #computeHorizontalScrollOffset()
9225 * @see android.widget.ScrollBarDrawable
9226 */
9227 protected int computeHorizontalScrollExtent() {
9228 return getWidth();
9229 }
9230
9231 /**
9232 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9233 *
9234 * <p>The range is expressed in arbitrary units that must be the same as the
9235 * units used by {@link #computeVerticalScrollExtent()} and
9236 * {@link #computeVerticalScrollOffset()}.</p>
9237 *
9238 * @return the total vertical range represented by the vertical scrollbar
9239 *
9240 * <p>The default range is the drawing height of this view.</p>
9241 *
9242 * @see #computeVerticalScrollExtent()
9243 * @see #computeVerticalScrollOffset()
9244 * @see android.widget.ScrollBarDrawable
9245 */
9246 protected int computeVerticalScrollRange() {
9247 return getHeight();
9248 }
9249
9250 /**
9251 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9252 * within the horizontal range. This value is used to compute the position
9253 * of the thumb within the scrollbar's track.</p>
9254 *
9255 * <p>The range is expressed in arbitrary units that must be the same as the
9256 * units used by {@link #computeVerticalScrollRange()} and
9257 * {@link #computeVerticalScrollExtent()}.</p>
9258 *
9259 * <p>The default offset is the scroll offset of this view.</p>
9260 *
9261 * @return the vertical offset of the scrollbar's thumb
9262 *
9263 * @see #computeVerticalScrollRange()
9264 * @see #computeVerticalScrollExtent()
9265 * @see android.widget.ScrollBarDrawable
9266 */
9267 protected int computeVerticalScrollOffset() {
9268 return mScrollY;
9269 }
9270
9271 /**
9272 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9273 * within the vertical range. This value is used to compute the length
9274 * of the thumb within the scrollbar's track.</p>
9275 *
9276 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009277 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009278 * {@link #computeVerticalScrollOffset()}.</p>
9279 *
9280 * <p>The default extent is the drawing height of this view.</p>
9281 *
9282 * @return the vertical extent of the scrollbar's thumb
9283 *
9284 * @see #computeVerticalScrollRange()
9285 * @see #computeVerticalScrollOffset()
9286 * @see android.widget.ScrollBarDrawable
9287 */
9288 protected int computeVerticalScrollExtent() {
9289 return getHeight();
9290 }
9291
9292 /**
Adam Powell69159442011-06-13 17:53:06 -07009293 * Check if this view can be scrolled horizontally in a certain direction.
9294 *
9295 * @param direction Negative to check scrolling left, positive to check scrolling right.
9296 * @return true if this view can be scrolled in the specified direction, false otherwise.
9297 */
9298 public boolean canScrollHorizontally(int direction) {
9299 final int offset = computeHorizontalScrollOffset();
9300 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9301 if (range == 0) return false;
9302 if (direction < 0) {
9303 return offset > 0;
9304 } else {
9305 return offset < range - 1;
9306 }
9307 }
9308
9309 /**
9310 * Check if this view can be scrolled vertically in a certain direction.
9311 *
9312 * @param direction Negative to check scrolling up, positive to check scrolling down.
9313 * @return true if this view can be scrolled in the specified direction, false otherwise.
9314 */
9315 public boolean canScrollVertically(int direction) {
9316 final int offset = computeVerticalScrollOffset();
9317 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9318 if (range == 0) return false;
9319 if (direction < 0) {
9320 return offset > 0;
9321 } else {
9322 return offset < range - 1;
9323 }
9324 }
9325
9326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9328 * scrollbars are painted only if they have been awakened first.</p>
9329 *
9330 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009331 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009332 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009334 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 // scrollbars are drawn only when the animation is running
9336 final ScrollabilityCache cache = mScrollCache;
9337 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009338
Mike Cleronf116bf82009-09-27 19:14:12 -07009339 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009340
Mike Cleronf116bf82009-09-27 19:14:12 -07009341 if (state == ScrollabilityCache.OFF) {
9342 return;
9343 }
Joe Malin32736f02011-01-19 16:14:20 -08009344
Mike Cleronf116bf82009-09-27 19:14:12 -07009345 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009346
Mike Cleronf116bf82009-09-27 19:14:12 -07009347 if (state == ScrollabilityCache.FADING) {
9348 // We're fading -- get our fade interpolation
9349 if (cache.interpolatorValues == null) {
9350 cache.interpolatorValues = new float[1];
9351 }
Joe Malin32736f02011-01-19 16:14:20 -08009352
Mike Cleronf116bf82009-09-27 19:14:12 -07009353 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009354
Mike Cleronf116bf82009-09-27 19:14:12 -07009355 // Stops the animation if we're done
9356 if (cache.scrollBarInterpolator.timeToValues(values) ==
9357 Interpolator.Result.FREEZE_END) {
9358 cache.state = ScrollabilityCache.OFF;
9359 } else {
9360 cache.scrollBar.setAlpha(Math.round(values[0]));
9361 }
Joe Malin32736f02011-01-19 16:14:20 -08009362
9363 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009364 // drawing. We only want this when we're fading so that
9365 // we prevent excessive redraws
9366 invalidate = true;
9367 } else {
9368 // We're just on -- but we may have been fading before so
9369 // reset alpha
9370 cache.scrollBar.setAlpha(255);
9371 }
9372
Joe Malin32736f02011-01-19 16:14:20 -08009373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009374 final int viewFlags = mViewFlags;
9375
9376 final boolean drawHorizontalScrollBar =
9377 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9378 final boolean drawVerticalScrollBar =
9379 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9380 && !isVerticalScrollBarHidden();
9381
9382 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9383 final int width = mRight - mLeft;
9384 final int height = mBottom - mTop;
9385
9386 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387
Mike Reede8853fc2009-09-04 14:01:48 -04009388 final int scrollX = mScrollX;
9389 final int scrollY = mScrollY;
9390 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9391
Mike Cleronf116bf82009-09-27 19:14:12 -07009392 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009395 int size = scrollBar.getSize(false);
9396 if (size <= 0) {
9397 size = cache.scrollBarSize;
9398 }
9399
Mike Cleronf116bf82009-09-27 19:14:12 -07009400 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009401 computeHorizontalScrollOffset(),
9402 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009403 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009404 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009405 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009406 left = scrollX + (mPaddingLeft & inside);
9407 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9408 bottom = top + size;
9409 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9410 if (invalidate) {
9411 invalidate(left, top, right, bottom);
9412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413 }
9414
9415 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009416 int size = scrollBar.getSize(true);
9417 if (size <= 0) {
9418 size = cache.scrollBarSize;
9419 }
9420
Mike Reede8853fc2009-09-04 14:01:48 -04009421 scrollBar.setParameters(computeVerticalScrollRange(),
9422 computeVerticalScrollOffset(),
9423 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009424 switch (mVerticalScrollbarPosition) {
9425 default:
9426 case SCROLLBAR_POSITION_DEFAULT:
9427 case SCROLLBAR_POSITION_RIGHT:
9428 left = scrollX + width - size - (mUserPaddingRight & inside);
9429 break;
9430 case SCROLLBAR_POSITION_LEFT:
9431 left = scrollX + (mUserPaddingLeft & inside);
9432 break;
9433 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009434 top = scrollY + (mPaddingTop & inside);
9435 right = left + size;
9436 bottom = scrollY + height - (mUserPaddingBottom & inside);
9437 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9438 if (invalidate) {
9439 invalidate(left, top, right, bottom);
9440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009441 }
9442 }
9443 }
9444 }
Romain Guy8506ab42009-06-11 17:35:47 -07009445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009447 * 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 -08009448 * FastScroller is visible.
9449 * @return whether to temporarily hide the vertical scrollbar
9450 * @hide
9451 */
9452 protected boolean isVerticalScrollBarHidden() {
9453 return false;
9454 }
9455
9456 /**
9457 * <p>Draw the horizontal scrollbar if
9458 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9459 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009460 * @param canvas the canvas on which to draw the scrollbar
9461 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 *
9463 * @see #isHorizontalScrollBarEnabled()
9464 * @see #computeHorizontalScrollRange()
9465 * @see #computeHorizontalScrollExtent()
9466 * @see #computeHorizontalScrollOffset()
9467 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009468 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009469 */
Romain Guy8fb95422010-08-17 18:38:51 -07009470 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9471 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009472 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009473 scrollBar.draw(canvas);
9474 }
Mike Reede8853fc2009-09-04 14:01:48 -04009475
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009477 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9478 * returns true.</p>
9479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009480 * @param canvas the canvas on which to draw the scrollbar
9481 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 *
9483 * @see #isVerticalScrollBarEnabled()
9484 * @see #computeVerticalScrollRange()
9485 * @see #computeVerticalScrollExtent()
9486 * @see #computeVerticalScrollOffset()
9487 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009488 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 */
Romain Guy8fb95422010-08-17 18:38:51 -07009490 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9491 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009492 scrollBar.setBounds(l, t, r, b);
9493 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 }
9495
9496 /**
9497 * Implement this to do your drawing.
9498 *
9499 * @param canvas the canvas on which the background will be drawn
9500 */
9501 protected void onDraw(Canvas canvas) {
9502 }
9503
9504 /*
9505 * Caller is responsible for calling requestLayout if necessary.
9506 * (This allows addViewInLayout to not request a new layout.)
9507 */
9508 void assignParent(ViewParent parent) {
9509 if (mParent == null) {
9510 mParent = parent;
9511 } else if (parent == null) {
9512 mParent = null;
9513 } else {
9514 throw new RuntimeException("view " + this + " being added, but"
9515 + " it already has a parent");
9516 }
9517 }
9518
9519 /**
9520 * This is called when the view is attached to a window. At this point it
9521 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009522 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9523 * however it may be called any time before the first onDraw -- including
9524 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009525 *
9526 * @see #onDetachedFromWindow()
9527 */
9528 protected void onAttachedToWindow() {
9529 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9530 mParent.requestTransparentRegion(this);
9531 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009532 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9533 initialAwakenScrollBars();
9534 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9535 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009536 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009537 // Order is important here: LayoutDirection MUST be resolved before Padding
9538 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009539 resolveLayoutDirectionIfNeeded();
9540 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009541 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009542 if (isFocused()) {
9543 InputMethodManager imm = InputMethodManager.peekInstance();
9544 imm.focusIn(this);
9545 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009546 }
Cibu Johny86666632010-02-22 13:01:02 -08009547
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009548 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009549 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9550 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009551 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009552 private void resolveLayoutDirectionIfNeeded() {
9553 // Do not resolve if it is not needed
9554 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9555
9556 // Clear any previous layout direction resolution
9557 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9558
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009559 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9560 // TextDirectionHeuristic
9561 resetResolvedTextDirection();
9562
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009563 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009564 switch (getLayoutDirection()) {
9565 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009566 // We cannot do the resolution if there is no parent
9567 if (mParent == null) return;
9568
Cibu Johny86666632010-02-22 13:01:02 -08009569 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009570 if (mParent instanceof ViewGroup) {
9571 ViewGroup viewGroup = ((ViewGroup) mParent);
9572
9573 // Check if the parent view group can resolve
9574 if (! viewGroup.canResolveLayoutDirection()) {
9575 return;
9576 }
9577 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9578 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9579 }
Cibu Johny86666632010-02-22 13:01:02 -08009580 }
9581 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009582 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009583 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009584 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009585 case LAYOUT_DIRECTION_LOCALE:
9586 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009587 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009588 }
9589 break;
9590 default:
9591 // Nothing to do, LTR by default
9592 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009593
9594 // Set to resolved
9595 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9596 }
9597
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009598 /**
9599 * @hide
9600 */
9601 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009602 // If the user specified the absolute padding (either with android:padding or
9603 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9604 // use the default padding or the padding from the background drawable
9605 // (stored at this point in mPadding*)
9606 switch (getResolvedLayoutDirection()) {
9607 case LAYOUT_DIRECTION_RTL:
9608 // Start user padding override Right user padding. Otherwise, if Right user
9609 // padding is not defined, use the default Right padding. If Right user padding
9610 // is defined, just use it.
9611 if (mUserPaddingStart >= 0) {
9612 mUserPaddingRight = mUserPaddingStart;
9613 } else if (mUserPaddingRight < 0) {
9614 mUserPaddingRight = mPaddingRight;
9615 }
9616 if (mUserPaddingEnd >= 0) {
9617 mUserPaddingLeft = mUserPaddingEnd;
9618 } else if (mUserPaddingLeft < 0) {
9619 mUserPaddingLeft = mPaddingLeft;
9620 }
9621 break;
9622 case LAYOUT_DIRECTION_LTR:
9623 default:
9624 // Start user padding override Left user padding. Otherwise, if Left user
9625 // padding is not defined, use the default left padding. If Left user padding
9626 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009627 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009628 mUserPaddingLeft = mUserPaddingStart;
9629 } else if (mUserPaddingLeft < 0) {
9630 mUserPaddingLeft = mPaddingLeft;
9631 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009632 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009633 mUserPaddingRight = mUserPaddingEnd;
9634 } else if (mUserPaddingRight < 0) {
9635 mUserPaddingRight = mPaddingRight;
9636 }
9637 }
9638
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009639 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9640
9641 recomputePadding();
9642 }
9643
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009644 /**
9645 * Return true if layout direction resolution can be done
9646 *
9647 * @hide
9648 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009649 protected boolean canResolveLayoutDirection() {
9650 switch (getLayoutDirection()) {
9651 case LAYOUT_DIRECTION_INHERIT:
9652 return (mParent != null);
9653 default:
9654 return true;
9655 }
9656 }
9657
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009658 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009659 * Reset the resolved layout direction.
9660 *
9661 * Subclasses need to override this method to clear cached information that depends on the
9662 * resolved layout direction, or to inform child views that inherit their layout direction.
9663 * Overrides must also call the superclass implementation at the start of their implementation.
9664 *
9665 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009666 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009667 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009668 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009669 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009670 }
9671
9672 /**
9673 * Check if a Locale is corresponding to a RTL script.
9674 *
9675 * @param locale Locale to check
9676 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009677 *
9678 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009679 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009680 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009681 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9682 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 }
9684
9685 /**
9686 * This is called when the view is detached from a window. At this point it
9687 * no longer has a surface for drawing.
9688 *
9689 * @see #onAttachedToWindow()
9690 */
9691 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009692 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009693
Romain Guya440b002010-02-24 15:57:54 -08009694 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009695 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009696 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009697 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009700
Romain Guy6d7475d2011-07-27 16:28:21 -07009701 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009702
Chet Haasedaf98e92011-01-10 14:10:36 -08009703 if (mDisplayList != null) {
9704 mDisplayList.invalidate();
9705 }
9706
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009707 if (mAttachInfo != null) {
9708 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009709 }
9710
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009711 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009712
9713 resetResolvedLayoutDirection();
9714 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 }
9716
9717 /**
9718 * @return The number of times this view has been attached to a window
9719 */
9720 protected int getWindowAttachCount() {
9721 return mWindowAttachCount;
9722 }
9723
9724 /**
9725 * Retrieve a unique token identifying the window this view is attached to.
9726 * @return Return the window's token for use in
9727 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9728 */
9729 public IBinder getWindowToken() {
9730 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9731 }
9732
9733 /**
9734 * Retrieve a unique token identifying the top-level "real" window of
9735 * the window that this view is attached to. That is, this is like
9736 * {@link #getWindowToken}, except if the window this view in is a panel
9737 * window (attached to another containing window), then the token of
9738 * the containing window is returned instead.
9739 *
9740 * @return Returns the associated window token, either
9741 * {@link #getWindowToken()} or the containing window's token.
9742 */
9743 public IBinder getApplicationWindowToken() {
9744 AttachInfo ai = mAttachInfo;
9745 if (ai != null) {
9746 IBinder appWindowToken = ai.mPanelParentWindowToken;
9747 if (appWindowToken == null) {
9748 appWindowToken = ai.mWindowToken;
9749 }
9750 return appWindowToken;
9751 }
9752 return null;
9753 }
9754
9755 /**
9756 * Retrieve private session object this view hierarchy is using to
9757 * communicate with the window manager.
9758 * @return the session object to communicate with the window manager
9759 */
9760 /*package*/ IWindowSession getWindowSession() {
9761 return mAttachInfo != null ? mAttachInfo.mSession : null;
9762 }
9763
9764 /**
9765 * @param info the {@link android.view.View.AttachInfo} to associated with
9766 * this view
9767 */
9768 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9769 //System.out.println("Attached! " + this);
9770 mAttachInfo = info;
9771 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009772 // We will need to evaluate the drawable state at least once.
9773 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009774 if (mFloatingTreeObserver != null) {
9775 info.mTreeObserver.merge(mFloatingTreeObserver);
9776 mFloatingTreeObserver = null;
9777 }
9778 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9779 mAttachInfo.mScrollContainers.add(this);
9780 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9781 }
9782 performCollectViewAttributes(visibility);
9783 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009784
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009785 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009786 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009787 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009788 if (listeners != null && listeners.size() > 0) {
9789 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9790 // perform the dispatching. The iterator is a safe guard against listeners that
9791 // could mutate the list by calling the various add/remove methods. This prevents
9792 // the array from being modified while we iterate it.
9793 for (OnAttachStateChangeListener listener : listeners) {
9794 listener.onViewAttachedToWindow(this);
9795 }
9796 }
9797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009798 int vis = info.mWindowVisibility;
9799 if (vis != GONE) {
9800 onWindowVisibilityChanged(vis);
9801 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009802 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9803 // If nobody has evaluated the drawable state yet, then do it now.
9804 refreshDrawableState();
9805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 }
9807
9808 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 AttachInfo info = mAttachInfo;
9810 if (info != null) {
9811 int vis = info.mWindowVisibility;
9812 if (vis != GONE) {
9813 onWindowVisibilityChanged(GONE);
9814 }
9815 }
9816
9817 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009818
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009819 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009820 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009821 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009822 if (listeners != null && listeners.size() > 0) {
9823 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9824 // perform the dispatching. The iterator is a safe guard against listeners that
9825 // could mutate the list by calling the various add/remove methods. This prevents
9826 // the array from being modified while we iterate it.
9827 for (OnAttachStateChangeListener listener : listeners) {
9828 listener.onViewDetachedFromWindow(this);
9829 }
9830 }
9831
Romain Guy01d5edc2011-01-28 11:28:53 -08009832 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 mAttachInfo.mScrollContainers.remove(this);
9834 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9835 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 mAttachInfo = null;
9838 }
9839
9840 /**
9841 * Store this view hierarchy's frozen state into the given container.
9842 *
9843 * @param container The SparseArray in which to save the view's state.
9844 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009845 * @see #restoreHierarchyState(android.util.SparseArray)
9846 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9847 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009848 */
9849 public void saveHierarchyState(SparseArray<Parcelable> container) {
9850 dispatchSaveInstanceState(container);
9851 }
9852
9853 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009854 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9855 * this view and its children. May be overridden to modify how freezing happens to a
9856 * 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 -08009857 *
9858 * @param container The SparseArray in which to save the view's state.
9859 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009860 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9861 * @see #saveHierarchyState(android.util.SparseArray)
9862 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009863 */
9864 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9865 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9866 mPrivateFlags &= ~SAVE_STATE_CALLED;
9867 Parcelable state = onSaveInstanceState();
9868 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9869 throw new IllegalStateException(
9870 "Derived class did not call super.onSaveInstanceState()");
9871 }
9872 if (state != null) {
9873 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9874 // + ": " + state);
9875 container.put(mID, state);
9876 }
9877 }
9878 }
9879
9880 /**
9881 * Hook allowing a view to generate a representation of its internal state
9882 * that can later be used to create a new instance with that same state.
9883 * This state should only contain information that is not persistent or can
9884 * not be reconstructed later. For example, you will never store your
9885 * current position on screen because that will be computed again when a
9886 * new instance of the view is placed in its view hierarchy.
9887 * <p>
9888 * Some examples of things you may store here: the current cursor position
9889 * in a text view (but usually not the text itself since that is stored in a
9890 * content provider or other persistent storage), the currently selected
9891 * item in a list view.
9892 *
9893 * @return Returns a Parcelable object containing the view's current dynamic
9894 * state, or null if there is nothing interesting to save. The
9895 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009896 * @see #onRestoreInstanceState(android.os.Parcelable)
9897 * @see #saveHierarchyState(android.util.SparseArray)
9898 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899 * @see #setSaveEnabled(boolean)
9900 */
9901 protected Parcelable onSaveInstanceState() {
9902 mPrivateFlags |= SAVE_STATE_CALLED;
9903 return BaseSavedState.EMPTY_STATE;
9904 }
9905
9906 /**
9907 * Restore this view hierarchy's frozen state from the given container.
9908 *
9909 * @param container The SparseArray which holds previously frozen states.
9910 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009911 * @see #saveHierarchyState(android.util.SparseArray)
9912 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9913 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009914 */
9915 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9916 dispatchRestoreInstanceState(container);
9917 }
9918
9919 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009920 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9921 * state for this view and its children. May be overridden to modify how restoring
9922 * happens to a view's children; for example, some views may want to not store state
9923 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 *
9925 * @param container The SparseArray which holds previously saved state.
9926 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009927 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9928 * @see #restoreHierarchyState(android.util.SparseArray)
9929 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 */
9931 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9932 if (mID != NO_ID) {
9933 Parcelable state = container.get(mID);
9934 if (state != null) {
9935 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9936 // + ": " + state);
9937 mPrivateFlags &= ~SAVE_STATE_CALLED;
9938 onRestoreInstanceState(state);
9939 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9940 throw new IllegalStateException(
9941 "Derived class did not call super.onRestoreInstanceState()");
9942 }
9943 }
9944 }
9945 }
9946
9947 /**
9948 * Hook allowing a view to re-apply a representation of its internal state that had previously
9949 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9950 * null state.
9951 *
9952 * @param state The frozen state that had previously been returned by
9953 * {@link #onSaveInstanceState}.
9954 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009955 * @see #onSaveInstanceState()
9956 * @see #restoreHierarchyState(android.util.SparseArray)
9957 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 */
9959 protected void onRestoreInstanceState(Parcelable state) {
9960 mPrivateFlags |= SAVE_STATE_CALLED;
9961 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009962 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9963 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009964 + "when two views of different type have the same id in the same hierarchy. "
9965 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009966 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 }
9968 }
9969
9970 /**
9971 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9972 *
9973 * @return the drawing start time in milliseconds
9974 */
9975 public long getDrawingTime() {
9976 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9977 }
9978
9979 /**
9980 * <p>Enables or disables the duplication of the parent's state into this view. When
9981 * duplication is enabled, this view gets its drawable state from its parent rather
9982 * than from its own internal properties.</p>
9983 *
9984 * <p>Note: in the current implementation, setting this property to true after the
9985 * view was added to a ViewGroup might have no effect at all. This property should
9986 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9987 *
9988 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9989 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009990 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009991 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9992 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 *
9994 * @param enabled True to enable duplication of the parent's drawable state, false
9995 * to disable it.
9996 *
9997 * @see #getDrawableState()
9998 * @see #isDuplicateParentStateEnabled()
9999 */
10000 public void setDuplicateParentStateEnabled(boolean enabled) {
10001 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10002 }
10003
10004 /**
10005 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10006 *
10007 * @return True if this view's drawable state is duplicated from the parent,
10008 * false otherwise
10009 *
10010 * @see #getDrawableState()
10011 * @see #setDuplicateParentStateEnabled(boolean)
10012 */
10013 public boolean isDuplicateParentStateEnabled() {
10014 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10015 }
10016
10017 /**
Romain Guy171c5922011-01-06 10:04:23 -080010018 * <p>Specifies the type of layer backing this view. The layer can be
10019 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10020 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010021 *
Romain Guy171c5922011-01-06 10:04:23 -080010022 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10023 * instance that controls how the layer is composed on screen. The following
10024 * properties of the paint are taken into account when composing the layer:</p>
10025 * <ul>
10026 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10027 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10028 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10029 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010030 *
Romain Guy171c5922011-01-06 10:04:23 -080010031 * <p>If this view has an alpha value set to < 1.0 by calling
10032 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10033 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10034 * equivalent to setting a hardware layer on this view and providing a paint with
10035 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010036 *
Romain Guy171c5922011-01-06 10:04:23 -080010037 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10038 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10039 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010040 *
Romain Guy171c5922011-01-06 10:04:23 -080010041 * @param layerType The ype of layer to use with this view, must be one of
10042 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10043 * {@link #LAYER_TYPE_HARDWARE}
10044 * @param paint The paint used to compose the layer. This argument is optional
10045 * and can be null. It is ignored when the layer type is
10046 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010047 *
10048 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010049 * @see #LAYER_TYPE_NONE
10050 * @see #LAYER_TYPE_SOFTWARE
10051 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010052 * @see #setAlpha(float)
10053 *
Romain Guy171c5922011-01-06 10:04:23 -080010054 * @attr ref android.R.styleable#View_layerType
10055 */
10056 public void setLayerType(int layerType, Paint paint) {
10057 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010058 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010059 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10060 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010061
Romain Guyd6cd5722011-01-17 14:42:41 -080010062 if (layerType == mLayerType) {
10063 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10064 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010065 invalidateParentCaches();
10066 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010067 }
10068 return;
10069 }
Romain Guy171c5922011-01-06 10:04:23 -080010070
10071 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010072 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010073 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010074 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -070010075 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010076 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010077 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010078 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010079 default:
10080 break;
Romain Guy171c5922011-01-06 10:04:23 -080010081 }
10082
10083 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010084 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10085 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10086 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010087
Romain Guy0fd89bf2011-01-26 15:41:30 -080010088 invalidateParentCaches();
10089 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010090 }
10091
10092 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010093 * Indicates whether this view has a static layer. A view with layer type
10094 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10095 * dynamic.
10096 */
10097 boolean hasStaticLayer() {
10098 return mLayerType == LAYER_TYPE_NONE;
10099 }
10100
10101 /**
Romain Guy171c5922011-01-06 10:04:23 -080010102 * Indicates what type of layer is currently associated with this view. By default
10103 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10104 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10105 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010106 *
Romain Guy171c5922011-01-06 10:04:23 -080010107 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10108 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010109 *
10110 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010111 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010112 * @see #LAYER_TYPE_NONE
10113 * @see #LAYER_TYPE_SOFTWARE
10114 * @see #LAYER_TYPE_HARDWARE
10115 */
10116 public int getLayerType() {
10117 return mLayerType;
10118 }
Joe Malin32736f02011-01-19 16:14:20 -080010119
Romain Guy6c319ca2011-01-11 14:29:25 -080010120 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010121 * Forces this view's layer to be created and this view to be rendered
10122 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10123 * invoking this method will have no effect.
10124 *
10125 * This method can for instance be used to render a view into its layer before
10126 * starting an animation. If this view is complex, rendering into the layer
10127 * before starting the animation will avoid skipping frames.
10128 *
10129 * @throws IllegalStateException If this view is not attached to a window
10130 *
10131 * @see #setLayerType(int, android.graphics.Paint)
10132 */
10133 public void buildLayer() {
10134 if (mLayerType == LAYER_TYPE_NONE) return;
10135
10136 if (mAttachInfo == null) {
10137 throw new IllegalStateException("This view must be attached to a window first");
10138 }
10139
10140 switch (mLayerType) {
10141 case LAYER_TYPE_HARDWARE:
10142 getHardwareLayer();
10143 break;
10144 case LAYER_TYPE_SOFTWARE:
10145 buildDrawingCache(true);
10146 break;
10147 }
10148 }
10149
10150 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010151 * <p>Returns a hardware layer that can be used to draw this view again
10152 * without executing its draw method.</p>
10153 *
10154 * @return A HardwareLayer ready to render, or null if an error occurred.
10155 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010156 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010157 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10158 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010159 return null;
10160 }
10161
10162 final int width = mRight - mLeft;
10163 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010164
Romain Guy6c319ca2011-01-11 14:29:25 -080010165 if (width == 0 || height == 0) {
10166 return null;
10167 }
10168
10169 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10170 if (mHardwareLayer == null) {
10171 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10172 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010173 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010174 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10175 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010176 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010177 }
10178
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010179 // The layer is not valid if the underlying GPU resources cannot be allocated
10180 if (!mHardwareLayer.isValid()) {
10181 return null;
10182 }
10183
Romain Guy59a12ca2011-06-09 17:48:21 -070010184 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010185 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010186
10187 // Make sure all the GPU resources have been properly allocated
10188 if (canvas == null) {
10189 mHardwareLayer.end(currentCanvas);
10190 return null;
10191 }
10192
Romain Guy5e7f7662011-01-24 22:35:56 -080010193 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010194 try {
10195 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010196 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010197 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010198
Chet Haase88172fe2011-03-07 17:36:33 -080010199 final int restoreCount = canvas.save();
10200
Romain Guy6c319ca2011-01-11 14:29:25 -080010201 computeScroll();
10202 canvas.translate(-mScrollX, -mScrollY);
10203
Romain Guy6c319ca2011-01-11 14:29:25 -080010204 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010205
Romain Guy6c319ca2011-01-11 14:29:25 -080010206 // Fast path for layouts with no backgrounds
10207 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10208 mPrivateFlags &= ~DIRTY_MASK;
10209 dispatchDraw(canvas);
10210 } else {
10211 draw(canvas);
10212 }
Joe Malin32736f02011-01-19 16:14:20 -080010213
Chet Haase88172fe2011-03-07 17:36:33 -080010214 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010215 } finally {
10216 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010217 mHardwareLayer.end(currentCanvas);
10218 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010219 }
10220 }
10221
10222 return mHardwareLayer;
10223 }
Romain Guy171c5922011-01-06 10:04:23 -080010224
Romain Guy589b0bb2011-10-10 13:57:47 -070010225 /**
10226 * Destroys this View's hardware layer if possible.
10227 *
10228 * @return True if the layer was destroyed, false otherwise.
10229 *
10230 * @see #setLayerType(int, android.graphics.Paint)
10231 * @see #LAYER_TYPE_HARDWARE
10232 */
Romain Guy65b345f2011-07-27 18:51:50 -070010233 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010234 if (mHardwareLayer != null) {
10235 mHardwareLayer.destroy();
10236 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010237 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010238 }
Romain Guy65b345f2011-07-27 18:51:50 -070010239 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010240 }
10241
Romain Guy171c5922011-01-06 10:04:23 -080010242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10244 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10245 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10246 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10247 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10248 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010249 *
Romain Guy171c5922011-01-06 10:04:23 -080010250 * <p>Enabling the drawing cache is similar to
10251 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010252 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10253 * drawing cache has no effect on rendering because the system uses a different mechanism
10254 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10255 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10256 * for information on how to enable software and hardware layers.</p>
10257 *
10258 * <p>This API can be used to manually generate
10259 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10260 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 *
10262 * @param enabled true to enable the drawing cache, false otherwise
10263 *
10264 * @see #isDrawingCacheEnabled()
10265 * @see #getDrawingCache()
10266 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010267 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010268 */
10269 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010270 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10272 }
10273
10274 /**
10275 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10276 *
10277 * @return true if the drawing cache is enabled
10278 *
10279 * @see #setDrawingCacheEnabled(boolean)
10280 * @see #getDrawingCache()
10281 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010282 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 public boolean isDrawingCacheEnabled() {
10284 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10285 }
10286
10287 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010288 * Debugging utility which recursively outputs the dirty state of a view and its
10289 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010290 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010291 * @hide
10292 */
Romain Guy676b1732011-02-14 14:45:33 -080010293 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010294 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10295 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10296 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10297 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10298 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10299 if (clear) {
10300 mPrivateFlags &= clearMask;
10301 }
10302 if (this instanceof ViewGroup) {
10303 ViewGroup parent = (ViewGroup) this;
10304 final int count = parent.getChildCount();
10305 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010306 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010307 child.outputDirtyFlags(indent + " ", clear, clearMask);
10308 }
10309 }
10310 }
10311
10312 /**
10313 * This method is used by ViewGroup to cause its children to restore or recreate their
10314 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10315 * to recreate its own display list, which would happen if it went through the normal
10316 * draw/dispatchDraw mechanisms.
10317 *
10318 * @hide
10319 */
10320 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010321
10322 /**
10323 * A view that is not attached or hardware accelerated cannot create a display list.
10324 * This method checks these conditions and returns the appropriate result.
10325 *
10326 * @return true if view has the ability to create a display list, false otherwise.
10327 *
10328 * @hide
10329 */
10330 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010331 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010332 }
Joe Malin32736f02011-01-19 16:14:20 -080010333
Chet Haasedaf98e92011-01-10 14:10:36 -080010334 /**
Romain Guyb051e892010-09-28 19:09:36 -070010335 * <p>Returns a display list that can be used to draw this view again
10336 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010337 *
Romain Guyb051e892010-09-28 19:09:36 -070010338 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010339 *
10340 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010341 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010342 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010343 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010344 return null;
10345 }
10346
Chet Haasedaf98e92011-01-10 14:10:36 -080010347 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10348 mDisplayList == null || !mDisplayList.isValid() ||
10349 mRecreateDisplayList)) {
10350 // Don't need to recreate the display list, just need to tell our
10351 // children to restore/recreate theirs
10352 if (mDisplayList != null && mDisplayList.isValid() &&
10353 !mRecreateDisplayList) {
10354 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10355 mPrivateFlags &= ~DIRTY_MASK;
10356 dispatchGetDisplayList();
10357
10358 return mDisplayList;
10359 }
10360
10361 // If we got here, we're recreating it. Mark it as such to ensure that
10362 // we copy in child display lists into ours in drawChild()
10363 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010364 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010365 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010366 // If we're creating a new display list, make sure our parent gets invalidated
10367 // since they will need to recreate their display list to account for this
10368 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010369 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010370 }
Romain Guyb051e892010-09-28 19:09:36 -070010371
10372 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010373 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010374 try {
10375 int width = mRight - mLeft;
10376 int height = mBottom - mTop;
10377
10378 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010379 // The dirty rect should always be null for a display list
10380 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010381
Chet Haasedaf98e92011-01-10 14:10:36 -080010382 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010383
10384 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010385 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010386 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010387 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010388
Romain Guyb051e892010-09-28 19:09:36 -070010389 // Fast path for layouts with no backgrounds
10390 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010391 dispatchDraw(canvas);
10392 } else {
10393 draw(canvas);
10394 }
Romain Guyb051e892010-09-28 19:09:36 -070010395 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010396 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010397 canvas.onPostDraw();
10398
10399 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010400 }
Chet Haase77785f92011-01-25 23:22:09 -080010401 } else {
10402 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10403 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010404 }
10405
10406 return mDisplayList;
10407 }
10408
10409 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010410 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010411 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010412 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010413 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010414 * @see #getDrawingCache(boolean)
10415 */
10416 public Bitmap getDrawingCache() {
10417 return getDrawingCache(false);
10418 }
10419
10420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10422 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10423 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10424 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10425 * request the drawing cache by calling this method and draw it on screen if the
10426 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010427 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010428 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10429 * this method will create a bitmap of the same size as this view. Because this bitmap
10430 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10431 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10432 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10433 * size than the view. This implies that your application must be able to handle this
10434 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010435 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010436 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10437 * the current density of the screen when the application is in compatibility
10438 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010440 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 * @see #setDrawingCacheEnabled(boolean)
10443 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010444 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 * @see #destroyDrawingCache()
10446 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010447 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10449 return null;
10450 }
10451 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010452 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 }
Romain Guy02890fd2010-08-06 17:58:44 -070010454 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 }
10456
10457 /**
10458 * <p>Frees the resources used by the drawing cache. If you call
10459 * {@link #buildDrawingCache()} manually without calling
10460 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10461 * should cleanup the cache with this method afterwards.</p>
10462 *
10463 * @see #setDrawingCacheEnabled(boolean)
10464 * @see #buildDrawingCache()
10465 * @see #getDrawingCache()
10466 */
10467 public void destroyDrawingCache() {
10468 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010469 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 mDrawingCache = null;
10471 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010472 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010473 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010474 mUnscaledDrawingCache = null;
10475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 }
10477
10478 /**
10479 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010480 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 * view will always be drawn on top of a solid color.
10482 *
10483 * @param color The background color to use for the drawing cache's bitmap
10484 *
10485 * @see #setDrawingCacheEnabled(boolean)
10486 * @see #buildDrawingCache()
10487 * @see #getDrawingCache()
10488 */
10489 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010490 if (color != mDrawingCacheBackgroundColor) {
10491 mDrawingCacheBackgroundColor = color;
10492 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 }
10495
10496 /**
10497 * @see #setDrawingCacheBackgroundColor(int)
10498 *
10499 * @return The background color to used for the drawing cache's bitmap
10500 */
10501 public int getDrawingCacheBackgroundColor() {
10502 return mDrawingCacheBackgroundColor;
10503 }
10504
10505 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010506 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010507 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010508 * @see #buildDrawingCache(boolean)
10509 */
10510 public void buildDrawingCache() {
10511 buildDrawingCache(false);
10512 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010513
Romain Guyfbd8f692009-06-26 14:51:58 -070010514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10516 *
10517 * <p>If you call {@link #buildDrawingCache()} manually without calling
10518 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10519 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010520 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010521 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10522 * this method will create a bitmap of the same size as this view. Because this bitmap
10523 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10524 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10525 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10526 * size than the view. This implies that your application must be able to handle this
10527 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010528 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010529 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10530 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010531 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010532 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 *
10534 * @see #getDrawingCache()
10535 * @see #destroyDrawingCache()
10536 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010537 public void buildDrawingCache(boolean autoScale) {
10538 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010539 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010540 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541
10542 if (ViewDebug.TRACE_HIERARCHY) {
10543 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545
Romain Guy8506ab42009-06-11 17:35:47 -070010546 int width = mRight - mLeft;
10547 int height = mBottom - mTop;
10548
10549 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010550 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010551
Romain Guye1123222009-06-29 14:24:56 -070010552 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010553 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10554 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556
10557 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010558 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010559 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560
10561 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010562 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010563 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10565 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010566 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 return;
10568 }
10569
10570 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010571 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572
10573 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 Bitmap.Config quality;
10575 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010576 // Never pick ARGB_4444 because it looks awful
10577 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10579 case DRAWING_CACHE_QUALITY_AUTO:
10580 quality = Bitmap.Config.ARGB_8888;
10581 break;
10582 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010583 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584 break;
10585 case DRAWING_CACHE_QUALITY_HIGH:
10586 quality = Bitmap.Config.ARGB_8888;
10587 break;
10588 default:
10589 quality = Bitmap.Config.ARGB_8888;
10590 break;
10591 }
10592 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010593 // Optimization for translucent windows
10594 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010595 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 }
10597
10598 // Try to cleanup memory
10599 if (bitmap != null) bitmap.recycle();
10600
10601 try {
10602 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010603 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010604 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010605 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010606 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010607 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010608 }
Adam Powell26153a32010-11-08 15:22:27 -080010609 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 } catch (OutOfMemoryError e) {
10611 // If there is not enough memory to create the bitmap cache, just
10612 // ignore the issue as bitmap caches are not required to draw the
10613 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010614 if (autoScale) {
10615 mDrawingCache = null;
10616 } else {
10617 mUnscaledDrawingCache = null;
10618 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010619 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 return;
10621 }
10622
10623 clear = drawingCacheBackgroundColor != 0;
10624 }
10625
10626 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 if (attachInfo != null) {
10628 canvas = attachInfo.mCanvas;
10629 if (canvas == null) {
10630 canvas = new Canvas();
10631 }
10632 canvas.setBitmap(bitmap);
10633 // Temporarily clobber the cached Canvas in case one of our children
10634 // is also using a drawing cache. Without this, the children would
10635 // steal the canvas by attaching their own bitmap to it and bad, bad
10636 // thing would happen (invisible views, corrupted drawings, etc.)
10637 attachInfo.mCanvas = null;
10638 } else {
10639 // This case should hopefully never or seldom happen
10640 canvas = new Canvas(bitmap);
10641 }
10642
10643 if (clear) {
10644 bitmap.eraseColor(drawingCacheBackgroundColor);
10645 }
10646
10647 computeScroll();
10648 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010649
Romain Guye1123222009-06-29 14:24:56 -070010650 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010651 final float scale = attachInfo.mApplicationScale;
10652 canvas.scale(scale, scale);
10653 }
Joe Malin32736f02011-01-19 16:14:20 -080010654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 canvas.translate(-mScrollX, -mScrollY);
10656
Romain Guy5bcdff42009-05-14 21:27:18 -070010657 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010658 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10659 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010660 mPrivateFlags |= DRAWING_CACHE_VALID;
10661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662
10663 // Fast path for layouts with no backgrounds
10664 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10665 if (ViewDebug.TRACE_HIERARCHY) {
10666 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10667 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010668 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 dispatchDraw(canvas);
10670 } else {
10671 draw(canvas);
10672 }
10673
10674 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010675 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676
10677 if (attachInfo != null) {
10678 // Restore the cached Canvas for our siblings
10679 attachInfo.mCanvas = canvas;
10680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 }
10682 }
10683
10684 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010685 * Create a snapshot of the view into a bitmap. We should probably make
10686 * some form of this public, but should think about the API.
10687 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010688 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010689 int width = mRight - mLeft;
10690 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010691
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010692 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010693 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010694 width = (int) ((width * scale) + 0.5f);
10695 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010696
Romain Guy8c11e312009-09-14 15:15:30 -070010697 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010698 if (bitmap == null) {
10699 throw new OutOfMemoryError();
10700 }
10701
Romain Guyc529d8d2011-09-06 15:01:39 -070010702 Resources resources = getResources();
10703 if (resources != null) {
10704 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10705 }
Joe Malin32736f02011-01-19 16:14:20 -080010706
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010707 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010708 if (attachInfo != null) {
10709 canvas = attachInfo.mCanvas;
10710 if (canvas == null) {
10711 canvas = new Canvas();
10712 }
10713 canvas.setBitmap(bitmap);
10714 // Temporarily clobber the cached Canvas in case one of our children
10715 // is also using a drawing cache. Without this, the children would
10716 // steal the canvas by attaching their own bitmap to it and bad, bad
10717 // things would happen (invisible views, corrupted drawings, etc.)
10718 attachInfo.mCanvas = null;
10719 } else {
10720 // This case should hopefully never or seldom happen
10721 canvas = new Canvas(bitmap);
10722 }
10723
Romain Guy5bcdff42009-05-14 21:27:18 -070010724 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010725 bitmap.eraseColor(backgroundColor);
10726 }
10727
10728 computeScroll();
10729 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010730 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010731 canvas.translate(-mScrollX, -mScrollY);
10732
Romain Guy5bcdff42009-05-14 21:27:18 -070010733 // Temporarily remove the dirty mask
10734 int flags = mPrivateFlags;
10735 mPrivateFlags &= ~DIRTY_MASK;
10736
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010737 // Fast path for layouts with no backgrounds
10738 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10739 dispatchDraw(canvas);
10740 } else {
10741 draw(canvas);
10742 }
10743
Romain Guy5bcdff42009-05-14 21:27:18 -070010744 mPrivateFlags = flags;
10745
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010746 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010747 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010748
10749 if (attachInfo != null) {
10750 // Restore the cached Canvas for our siblings
10751 attachInfo.mCanvas = canvas;
10752 }
Romain Guy8506ab42009-06-11 17:35:47 -070010753
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010754 return bitmap;
10755 }
10756
10757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 * Indicates whether this View is currently in edit mode. A View is usually
10759 * in edit mode when displayed within a developer tool. For instance, if
10760 * this View is being drawn by a visual user interface builder, this method
10761 * should return true.
10762 *
10763 * Subclasses should check the return value of this method to provide
10764 * different behaviors if their normal behavior might interfere with the
10765 * host environment. For instance: the class spawns a thread in its
10766 * constructor, the drawing code relies on device-specific features, etc.
10767 *
10768 * This method is usually checked in the drawing code of custom widgets.
10769 *
10770 * @return True if this View is in edit mode, false otherwise.
10771 */
10772 public boolean isInEditMode() {
10773 return false;
10774 }
10775
10776 /**
10777 * If the View draws content inside its padding and enables fading edges,
10778 * it needs to support padding offsets. Padding offsets are added to the
10779 * fading edges to extend the length of the fade so that it covers pixels
10780 * drawn inside the padding.
10781 *
10782 * Subclasses of this class should override this method if they need
10783 * to draw content inside the padding.
10784 *
10785 * @return True if padding offset must be applied, false otherwise.
10786 *
10787 * @see #getLeftPaddingOffset()
10788 * @see #getRightPaddingOffset()
10789 * @see #getTopPaddingOffset()
10790 * @see #getBottomPaddingOffset()
10791 *
10792 * @since CURRENT
10793 */
10794 protected boolean isPaddingOffsetRequired() {
10795 return false;
10796 }
10797
10798 /**
10799 * Amount by which to extend the left fading region. Called only when
10800 * {@link #isPaddingOffsetRequired()} returns true.
10801 *
10802 * @return The left padding offset in pixels.
10803 *
10804 * @see #isPaddingOffsetRequired()
10805 *
10806 * @since CURRENT
10807 */
10808 protected int getLeftPaddingOffset() {
10809 return 0;
10810 }
10811
10812 /**
10813 * Amount by which to extend the right fading region. Called only when
10814 * {@link #isPaddingOffsetRequired()} returns true.
10815 *
10816 * @return The right padding offset in pixels.
10817 *
10818 * @see #isPaddingOffsetRequired()
10819 *
10820 * @since CURRENT
10821 */
10822 protected int getRightPaddingOffset() {
10823 return 0;
10824 }
10825
10826 /**
10827 * Amount by which to extend the top fading region. Called only when
10828 * {@link #isPaddingOffsetRequired()} returns true.
10829 *
10830 * @return The top padding offset in pixels.
10831 *
10832 * @see #isPaddingOffsetRequired()
10833 *
10834 * @since CURRENT
10835 */
10836 protected int getTopPaddingOffset() {
10837 return 0;
10838 }
10839
10840 /**
10841 * Amount by which to extend the bottom fading region. Called only when
10842 * {@link #isPaddingOffsetRequired()} returns true.
10843 *
10844 * @return The bottom padding offset in pixels.
10845 *
10846 * @see #isPaddingOffsetRequired()
10847 *
10848 * @since CURRENT
10849 */
10850 protected int getBottomPaddingOffset() {
10851 return 0;
10852 }
10853
10854 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010855 * @hide
10856 * @param offsetRequired
10857 */
10858 protected int getFadeTop(boolean offsetRequired) {
10859 int top = mPaddingTop;
10860 if (offsetRequired) top += getTopPaddingOffset();
10861 return top;
10862 }
10863
10864 /**
10865 * @hide
10866 * @param offsetRequired
10867 */
10868 protected int getFadeHeight(boolean offsetRequired) {
10869 int padding = mPaddingTop;
10870 if (offsetRequired) padding += getTopPaddingOffset();
10871 return mBottom - mTop - mPaddingBottom - padding;
10872 }
10873
10874 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010875 * <p>Indicates whether this view is attached to an hardware accelerated
10876 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010877 *
Romain Guy2bffd262010-09-12 17:40:02 -070010878 * <p>Even if this method returns true, it does not mean that every call
10879 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10880 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10881 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10882 * window is hardware accelerated,
10883 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10884 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010885 *
Romain Guy2bffd262010-09-12 17:40:02 -070010886 * @return True if the view is attached to a window and the window is
10887 * hardware accelerated; false in any other case.
10888 */
10889 public boolean isHardwareAccelerated() {
10890 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10891 }
Joe Malin32736f02011-01-19 16:14:20 -080010892
Romain Guy2bffd262010-09-12 17:40:02 -070010893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 * Manually render this view (and all of its children) to the given Canvas.
10895 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010896 * called. When implementing a view, implement
10897 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10898 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 *
10900 * @param canvas The Canvas to which the View is rendered.
10901 */
10902 public void draw(Canvas canvas) {
10903 if (ViewDebug.TRACE_HIERARCHY) {
10904 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10905 }
10906
Romain Guy5bcdff42009-05-14 21:27:18 -070010907 final int privateFlags = mPrivateFlags;
10908 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10909 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10910 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 /*
10913 * Draw traversal performs several drawing steps which must be executed
10914 * in the appropriate order:
10915 *
10916 * 1. Draw the background
10917 * 2. If necessary, save the canvas' layers to prepare for fading
10918 * 3. Draw view's content
10919 * 4. Draw children
10920 * 5. If necessary, draw the fading edges and restore layers
10921 * 6. Draw decorations (scrollbars for instance)
10922 */
10923
10924 // Step 1, draw the background, if needed
10925 int saveCount;
10926
Romain Guy24443ea2009-05-11 11:56:30 -070010927 if (!dirtyOpaque) {
10928 final Drawable background = mBGDrawable;
10929 if (background != null) {
10930 final int scrollX = mScrollX;
10931 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932
Romain Guy24443ea2009-05-11 11:56:30 -070010933 if (mBackgroundSizeChanged) {
10934 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10935 mBackgroundSizeChanged = false;
10936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010937
Romain Guy24443ea2009-05-11 11:56:30 -070010938 if ((scrollX | scrollY) == 0) {
10939 background.draw(canvas);
10940 } else {
10941 canvas.translate(scrollX, scrollY);
10942 background.draw(canvas);
10943 canvas.translate(-scrollX, -scrollY);
10944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010945 }
10946 }
10947
10948 // skip step 2 & 5 if possible (common case)
10949 final int viewFlags = mViewFlags;
10950 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10951 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10952 if (!verticalEdges && !horizontalEdges) {
10953 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010954 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955
10956 // Step 4, draw the children
10957 dispatchDraw(canvas);
10958
10959 // Step 6, draw decorations (scrollbars)
10960 onDrawScrollBars(canvas);
10961
10962 // we're done...
10963 return;
10964 }
10965
10966 /*
10967 * Here we do the full fledged routine...
10968 * (this is an uncommon case where speed matters less,
10969 * this is why we repeat some of the tests that have been
10970 * done above)
10971 */
10972
10973 boolean drawTop = false;
10974 boolean drawBottom = false;
10975 boolean drawLeft = false;
10976 boolean drawRight = false;
10977
10978 float topFadeStrength = 0.0f;
10979 float bottomFadeStrength = 0.0f;
10980 float leftFadeStrength = 0.0f;
10981 float rightFadeStrength = 0.0f;
10982
10983 // Step 2, save the canvas' layers
10984 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985
10986 final boolean offsetRequired = isPaddingOffsetRequired();
10987 if (offsetRequired) {
10988 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 }
10990
10991 int left = mScrollX + paddingLeft;
10992 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010993 int top = mScrollY + getFadeTop(offsetRequired);
10994 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995
10996 if (offsetRequired) {
10997 right += getRightPaddingOffset();
10998 bottom += getBottomPaddingOffset();
10999 }
11000
11001 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011002 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11003 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011004
11005 // clip the fade length if top and bottom fades overlap
11006 // overlapping fades produce odd-looking artifacts
11007 if (verticalEdges && (top + length > bottom - length)) {
11008 length = (bottom - top) / 2;
11009 }
11010
11011 // also clip horizontal fades if necessary
11012 if (horizontalEdges && (left + length > right - length)) {
11013 length = (right - left) / 2;
11014 }
11015
11016 if (verticalEdges) {
11017 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011018 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011019 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011020 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011021 }
11022
11023 if (horizontalEdges) {
11024 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011025 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011027 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 }
11029
11030 saveCount = canvas.getSaveCount();
11031
11032 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011033 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011034 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11035
11036 if (drawTop) {
11037 canvas.saveLayer(left, top, right, top + length, null, flags);
11038 }
11039
11040 if (drawBottom) {
11041 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11042 }
11043
11044 if (drawLeft) {
11045 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11046 }
11047
11048 if (drawRight) {
11049 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11050 }
11051 } else {
11052 scrollabilityCache.setFadeColor(solidColor);
11053 }
11054
11055 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011056 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057
11058 // Step 4, draw the children
11059 dispatchDraw(canvas);
11060
11061 // Step 5, draw the fade effect and restore layers
11062 final Paint p = scrollabilityCache.paint;
11063 final Matrix matrix = scrollabilityCache.matrix;
11064 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011065
11066 if (drawTop) {
11067 matrix.setScale(1, fadeHeight * topFadeStrength);
11068 matrix.postTranslate(left, top);
11069 fade.setLocalMatrix(matrix);
11070 canvas.drawRect(left, top, right, top + length, p);
11071 }
11072
11073 if (drawBottom) {
11074 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11075 matrix.postRotate(180);
11076 matrix.postTranslate(left, bottom);
11077 fade.setLocalMatrix(matrix);
11078 canvas.drawRect(left, bottom - length, right, bottom, p);
11079 }
11080
11081 if (drawLeft) {
11082 matrix.setScale(1, fadeHeight * leftFadeStrength);
11083 matrix.postRotate(-90);
11084 matrix.postTranslate(left, top);
11085 fade.setLocalMatrix(matrix);
11086 canvas.drawRect(left, top, left + length, bottom, p);
11087 }
11088
11089 if (drawRight) {
11090 matrix.setScale(1, fadeHeight * rightFadeStrength);
11091 matrix.postRotate(90);
11092 matrix.postTranslate(right, top);
11093 fade.setLocalMatrix(matrix);
11094 canvas.drawRect(right - length, top, right, bottom, p);
11095 }
11096
11097 canvas.restoreToCount(saveCount);
11098
11099 // Step 6, draw decorations (scrollbars)
11100 onDrawScrollBars(canvas);
11101 }
11102
11103 /**
11104 * Override this if your view is known to always be drawn on top of a solid color background,
11105 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11106 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11107 * should be set to 0xFF.
11108 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011109 * @see #setVerticalFadingEdgeEnabled(boolean)
11110 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 *
11112 * @return The known solid color background for this view, or 0 if the color may vary
11113 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011114 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 public int getSolidColor() {
11116 return 0;
11117 }
11118
11119 /**
11120 * Build a human readable string representation of the specified view flags.
11121 *
11122 * @param flags the view flags to convert to a string
11123 * @return a String representing the supplied flags
11124 */
11125 private static String printFlags(int flags) {
11126 String output = "";
11127 int numFlags = 0;
11128 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11129 output += "TAKES_FOCUS";
11130 numFlags++;
11131 }
11132
11133 switch (flags & VISIBILITY_MASK) {
11134 case INVISIBLE:
11135 if (numFlags > 0) {
11136 output += " ";
11137 }
11138 output += "INVISIBLE";
11139 // USELESS HERE numFlags++;
11140 break;
11141 case GONE:
11142 if (numFlags > 0) {
11143 output += " ";
11144 }
11145 output += "GONE";
11146 // USELESS HERE numFlags++;
11147 break;
11148 default:
11149 break;
11150 }
11151 return output;
11152 }
11153
11154 /**
11155 * Build a human readable string representation of the specified private
11156 * view flags.
11157 *
11158 * @param privateFlags the private view flags to convert to a string
11159 * @return a String representing the supplied flags
11160 */
11161 private static String printPrivateFlags(int privateFlags) {
11162 String output = "";
11163 int numFlags = 0;
11164
11165 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11166 output += "WANTS_FOCUS";
11167 numFlags++;
11168 }
11169
11170 if ((privateFlags & FOCUSED) == FOCUSED) {
11171 if (numFlags > 0) {
11172 output += " ";
11173 }
11174 output += "FOCUSED";
11175 numFlags++;
11176 }
11177
11178 if ((privateFlags & SELECTED) == SELECTED) {
11179 if (numFlags > 0) {
11180 output += " ";
11181 }
11182 output += "SELECTED";
11183 numFlags++;
11184 }
11185
11186 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11187 if (numFlags > 0) {
11188 output += " ";
11189 }
11190 output += "IS_ROOT_NAMESPACE";
11191 numFlags++;
11192 }
11193
11194 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11195 if (numFlags > 0) {
11196 output += " ";
11197 }
11198 output += "HAS_BOUNDS";
11199 numFlags++;
11200 }
11201
11202 if ((privateFlags & DRAWN) == DRAWN) {
11203 if (numFlags > 0) {
11204 output += " ";
11205 }
11206 output += "DRAWN";
11207 // USELESS HERE numFlags++;
11208 }
11209 return output;
11210 }
11211
11212 /**
11213 * <p>Indicates whether or not this view's layout will be requested during
11214 * the next hierarchy layout pass.</p>
11215 *
11216 * @return true if the layout will be forced during next layout pass
11217 */
11218 public boolean isLayoutRequested() {
11219 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11220 }
11221
11222 /**
11223 * Assign a size and position to a view and all of its
11224 * descendants
11225 *
11226 * <p>This is the second phase of the layout mechanism.
11227 * (The first is measuring). In this phase, each parent calls
11228 * layout on all of its children to position them.
11229 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011230 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011231 *
Chet Haase9c087442011-01-12 16:20:16 -080011232 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 * Derived classes with children should override
11234 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011235 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011236 *
11237 * @param l Left position, relative to parent
11238 * @param t Top position, relative to parent
11239 * @param r Right position, relative to parent
11240 * @param b Bottom position, relative to parent
11241 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011242 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011243 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011244 int oldL = mLeft;
11245 int oldT = mTop;
11246 int oldB = mBottom;
11247 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248 boolean changed = setFrame(l, t, r, b);
11249 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11250 if (ViewDebug.TRACE_HIERARCHY) {
11251 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11252 }
11253
11254 onLayout(changed, l, t, r, b);
11255 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011256
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011257 ListenerInfo li = mListenerInfo;
11258 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011259 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011260 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011261 int numListeners = listenersCopy.size();
11262 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011263 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011264 }
11265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011266 }
11267 mPrivateFlags &= ~FORCE_LAYOUT;
11268 }
11269
11270 /**
11271 * Called from layout when this view should
11272 * assign a size and position to each of its children.
11273 *
11274 * Derived classes with children should override
11275 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011276 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 * @param changed This is a new size or position for this view
11278 * @param left Left position, relative to parent
11279 * @param top Top position, relative to parent
11280 * @param right Right position, relative to parent
11281 * @param bottom Bottom position, relative to parent
11282 */
11283 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11284 }
11285
11286 /**
11287 * Assign a size and position to this view.
11288 *
11289 * This is called from layout.
11290 *
11291 * @param left Left position, relative to parent
11292 * @param top Top position, relative to parent
11293 * @param right Right position, relative to parent
11294 * @param bottom Bottom position, relative to parent
11295 * @return true if the new size and position are different than the
11296 * previous ones
11297 * {@hide}
11298 */
11299 protected boolean setFrame(int left, int top, int right, int bottom) {
11300 boolean changed = false;
11301
11302 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011303 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011304 + right + "," + bottom + ")");
11305 }
11306
11307 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11308 changed = true;
11309
11310 // Remember our drawn bit
11311 int drawn = mPrivateFlags & DRAWN;
11312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 int oldWidth = mRight - mLeft;
11314 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011315 int newWidth = right - left;
11316 int newHeight = bottom - top;
11317 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11318
11319 // Invalidate our old position
11320 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011321
11322 mLeft = left;
11323 mTop = top;
11324 mRight = right;
11325 mBottom = bottom;
11326
11327 mPrivateFlags |= HAS_BOUNDS;
11328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011329
Chet Haase75755e22011-07-18 17:48:25 -070011330 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011331 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11332 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011333 if (mTransformationInfo != null) {
11334 mTransformationInfo.mMatrixDirty = true;
11335 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11338 }
11339
11340 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11341 // If we are visible, force the DRAWN bit to on so that
11342 // this invalidate will go through (at least to our parent).
11343 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011344 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 // the DRAWN bit.
11346 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011347 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011348 // parent display list may need to be recreated based on a change in the bounds
11349 // of any child
11350 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011351 }
11352
11353 // Reset drawn bit to original value (invalidate turns it off)
11354 mPrivateFlags |= drawn;
11355
11356 mBackgroundSizeChanged = true;
11357 }
11358 return changed;
11359 }
11360
11361 /**
11362 * Finalize inflating a view from XML. This is called as the last phase
11363 * of inflation, after all child views have been added.
11364 *
11365 * <p>Even if the subclass overrides onFinishInflate, they should always be
11366 * sure to call the super method, so that we get called.
11367 */
11368 protected void onFinishInflate() {
11369 }
11370
11371 /**
11372 * Returns the resources associated with this view.
11373 *
11374 * @return Resources object.
11375 */
11376 public Resources getResources() {
11377 return mResources;
11378 }
11379
11380 /**
11381 * Invalidates the specified Drawable.
11382 *
11383 * @param drawable the drawable to invalidate
11384 */
11385 public void invalidateDrawable(Drawable drawable) {
11386 if (verifyDrawable(drawable)) {
11387 final Rect dirty = drawable.getBounds();
11388 final int scrollX = mScrollX;
11389 final int scrollY = mScrollY;
11390
11391 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11392 dirty.right + scrollX, dirty.bottom + scrollY);
11393 }
11394 }
11395
11396 /**
11397 * Schedules an action on a drawable to occur at a specified time.
11398 *
11399 * @param who the recipient of the action
11400 * @param what the action to run on the drawable
11401 * @param when the time at which the action must occur. Uses the
11402 * {@link SystemClock#uptimeMillis} timebase.
11403 */
11404 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011405 if (verifyDrawable(who) && what != null) {
11406 if (mAttachInfo != null) {
11407 mAttachInfo.mHandler.postAtTime(what, who, when);
11408 } else {
11409 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011411 }
11412 }
11413
11414 /**
11415 * Cancels a scheduled action on a drawable.
11416 *
11417 * @param who the recipient of the action
11418 * @param what the action to cancel
11419 */
11420 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011421 if (verifyDrawable(who) && what != null) {
11422 if (mAttachInfo != null) {
11423 mAttachInfo.mHandler.removeCallbacks(what, who);
11424 } else {
11425 ViewRootImpl.getRunQueue().removeCallbacks(what);
11426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011427 }
11428 }
11429
11430 /**
11431 * Unschedule any events associated with the given Drawable. This can be
11432 * used when selecting a new Drawable into a view, so that the previous
11433 * one is completely unscheduled.
11434 *
11435 * @param who The Drawable to unschedule.
11436 *
11437 * @see #drawableStateChanged
11438 */
11439 public void unscheduleDrawable(Drawable who) {
11440 if (mAttachInfo != null) {
11441 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11442 }
11443 }
11444
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011445 /**
11446 * Return the layout direction of a given Drawable.
11447 *
11448 * @param who the Drawable to query
11449 *
11450 * @hide
11451 */
11452 public int getResolvedLayoutDirection(Drawable who) {
11453 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011454 }
11455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011456 /**
11457 * If your view subclass is displaying its own Drawable objects, it should
11458 * override this function and return true for any Drawable it is
11459 * displaying. This allows animations for those drawables to be
11460 * scheduled.
11461 *
11462 * <p>Be sure to call through to the super class when overriding this
11463 * function.
11464 *
11465 * @param who The Drawable to verify. Return true if it is one you are
11466 * displaying, else return the result of calling through to the
11467 * super class.
11468 *
11469 * @return boolean If true than the Drawable is being displayed in the
11470 * view; else false and it is not allowed to animate.
11471 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011472 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11473 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011474 */
11475 protected boolean verifyDrawable(Drawable who) {
11476 return who == mBGDrawable;
11477 }
11478
11479 /**
11480 * This function is called whenever the state of the view changes in such
11481 * a way that it impacts the state of drawables being shown.
11482 *
11483 * <p>Be sure to call through to the superclass when overriding this
11484 * function.
11485 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011486 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011487 */
11488 protected void drawableStateChanged() {
11489 Drawable d = mBGDrawable;
11490 if (d != null && d.isStateful()) {
11491 d.setState(getDrawableState());
11492 }
11493 }
11494
11495 /**
11496 * Call this to force a view to update its drawable state. This will cause
11497 * drawableStateChanged to be called on this view. Views that are interested
11498 * in the new state should call getDrawableState.
11499 *
11500 * @see #drawableStateChanged
11501 * @see #getDrawableState
11502 */
11503 public void refreshDrawableState() {
11504 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11505 drawableStateChanged();
11506
11507 ViewParent parent = mParent;
11508 if (parent != null) {
11509 parent.childDrawableStateChanged(this);
11510 }
11511 }
11512
11513 /**
11514 * Return an array of resource IDs of the drawable states representing the
11515 * current state of the view.
11516 *
11517 * @return The current drawable state
11518 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011519 * @see Drawable#setState(int[])
11520 * @see #drawableStateChanged()
11521 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011522 */
11523 public final int[] getDrawableState() {
11524 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11525 return mDrawableState;
11526 } else {
11527 mDrawableState = onCreateDrawableState(0);
11528 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11529 return mDrawableState;
11530 }
11531 }
11532
11533 /**
11534 * Generate the new {@link android.graphics.drawable.Drawable} state for
11535 * this view. This is called by the view
11536 * system when the cached Drawable state is determined to be invalid. To
11537 * retrieve the current state, you should use {@link #getDrawableState}.
11538 *
11539 * @param extraSpace if non-zero, this is the number of extra entries you
11540 * would like in the returned array in which you can place your own
11541 * states.
11542 *
11543 * @return Returns an array holding the current {@link Drawable} state of
11544 * the view.
11545 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011546 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 */
11548 protected int[] onCreateDrawableState(int extraSpace) {
11549 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11550 mParent instanceof View) {
11551 return ((View) mParent).onCreateDrawableState(extraSpace);
11552 }
11553
11554 int[] drawableState;
11555
11556 int privateFlags = mPrivateFlags;
11557
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011558 int viewStateIndex = 0;
11559 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11560 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11561 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011562 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011563 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11564 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011565 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11566 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011567 // This is set if HW acceleration is requested, even if the current
11568 // process doesn't allow it. This is just to allow app preview
11569 // windows to better match their app.
11570 viewStateIndex |= VIEW_STATE_ACCELERATED;
11571 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011572 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011573
Christopher Tate3d4bf172011-03-28 16:16:46 -070011574 final int privateFlags2 = mPrivateFlags2;
11575 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11576 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 drawableState = VIEW_STATE_SETS[viewStateIndex];
11579
11580 //noinspection ConstantIfStatement
11581 if (false) {
11582 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11583 Log.i("View", toString()
11584 + " pressed=" + ((privateFlags & PRESSED) != 0)
11585 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11586 + " fo=" + hasFocus()
11587 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011588 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589 + ": " + Arrays.toString(drawableState));
11590 }
11591
11592 if (extraSpace == 0) {
11593 return drawableState;
11594 }
11595
11596 final int[] fullState;
11597 if (drawableState != null) {
11598 fullState = new int[drawableState.length + extraSpace];
11599 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11600 } else {
11601 fullState = new int[extraSpace];
11602 }
11603
11604 return fullState;
11605 }
11606
11607 /**
11608 * Merge your own state values in <var>additionalState</var> into the base
11609 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011610 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011611 *
11612 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011613 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 * own additional state values.
11615 *
11616 * @param additionalState The additional state values you would like
11617 * added to <var>baseState</var>; this array is not modified.
11618 *
11619 * @return As a convenience, the <var>baseState</var> array you originally
11620 * passed into the function is returned.
11621 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011622 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011623 */
11624 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11625 final int N = baseState.length;
11626 int i = N - 1;
11627 while (i >= 0 && baseState[i] == 0) {
11628 i--;
11629 }
11630 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11631 return baseState;
11632 }
11633
11634 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011635 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11636 * on all Drawable objects associated with this view.
11637 */
11638 public void jumpDrawablesToCurrentState() {
11639 if (mBGDrawable != null) {
11640 mBGDrawable.jumpToCurrentState();
11641 }
11642 }
11643
11644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011645 * Sets the background color for this view.
11646 * @param color the color of the background
11647 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011648 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011650 if (mBGDrawable instanceof ColorDrawable) {
11651 ((ColorDrawable) mBGDrawable).setColor(color);
11652 } else {
11653 setBackgroundDrawable(new ColorDrawable(color));
11654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011655 }
11656
11657 /**
11658 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011659 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011660 * @param resid The identifier of the resource.
11661 * @attr ref android.R.styleable#View_background
11662 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011663 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011664 public void setBackgroundResource(int resid) {
11665 if (resid != 0 && resid == mBackgroundResource) {
11666 return;
11667 }
11668
11669 Drawable d= null;
11670 if (resid != 0) {
11671 d = mResources.getDrawable(resid);
11672 }
11673 setBackgroundDrawable(d);
11674
11675 mBackgroundResource = resid;
11676 }
11677
11678 /**
11679 * Set the background to a given Drawable, or remove the background. If the
11680 * background has padding, this View's padding is set to the background's
11681 * padding. However, when a background is removed, this View's padding isn't
11682 * touched. If setting the padding is desired, please use
11683 * {@link #setPadding(int, int, int, int)}.
11684 *
11685 * @param d The Drawable to use as the background, or null to remove the
11686 * background
11687 */
11688 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011689 if (d == mBGDrawable) {
11690 return;
11691 }
11692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 boolean requestLayout = false;
11694
11695 mBackgroundResource = 0;
11696
11697 /*
11698 * Regardless of whether we're setting a new background or not, we want
11699 * to clear the previous drawable.
11700 */
11701 if (mBGDrawable != null) {
11702 mBGDrawable.setCallback(null);
11703 unscheduleDrawable(mBGDrawable);
11704 }
11705
11706 if (d != null) {
11707 Rect padding = sThreadLocal.get();
11708 if (padding == null) {
11709 padding = new Rect();
11710 sThreadLocal.set(padding);
11711 }
11712 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011713 switch (d.getResolvedLayoutDirectionSelf()) {
11714 case LAYOUT_DIRECTION_RTL:
11715 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11716 break;
11717 case LAYOUT_DIRECTION_LTR:
11718 default:
11719 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721 }
11722
11723 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11724 // if it has a different minimum size, we should layout again
11725 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11726 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11727 requestLayout = true;
11728 }
11729
11730 d.setCallback(this);
11731 if (d.isStateful()) {
11732 d.setState(getDrawableState());
11733 }
11734 d.setVisible(getVisibility() == VISIBLE, false);
11735 mBGDrawable = d;
11736
11737 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11738 mPrivateFlags &= ~SKIP_DRAW;
11739 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11740 requestLayout = true;
11741 }
11742 } else {
11743 /* Remove the background */
11744 mBGDrawable = null;
11745
11746 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11747 /*
11748 * This view ONLY drew the background before and we're removing
11749 * the background, so now it won't draw anything
11750 * (hence we SKIP_DRAW)
11751 */
11752 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11753 mPrivateFlags |= SKIP_DRAW;
11754 }
11755
11756 /*
11757 * When the background is set, we try to apply its padding to this
11758 * View. When the background is removed, we don't touch this View's
11759 * padding. This is noted in the Javadocs. Hence, we don't need to
11760 * requestLayout(), the invalidate() below is sufficient.
11761 */
11762
11763 // The old background's minimum size could have affected this
11764 // View's layout, so let's requestLayout
11765 requestLayout = true;
11766 }
11767
Romain Guy8f1344f52009-05-15 16:03:59 -070011768 computeOpaqueFlags();
11769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011770 if (requestLayout) {
11771 requestLayout();
11772 }
11773
11774 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011775 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011776 }
11777
11778 /**
11779 * Gets the background drawable
11780 * @return The drawable used as the background for this view, if any.
11781 */
11782 public Drawable getBackground() {
11783 return mBGDrawable;
11784 }
11785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011786 /**
11787 * Sets the padding. The view may add on the space required to display
11788 * the scrollbars, depending on the style and visibility of the scrollbars.
11789 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11790 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11791 * from the values set in this call.
11792 *
11793 * @attr ref android.R.styleable#View_padding
11794 * @attr ref android.R.styleable#View_paddingBottom
11795 * @attr ref android.R.styleable#View_paddingLeft
11796 * @attr ref android.R.styleable#View_paddingRight
11797 * @attr ref android.R.styleable#View_paddingTop
11798 * @param left the left padding in pixels
11799 * @param top the top padding in pixels
11800 * @param right the right padding in pixels
11801 * @param bottom the bottom padding in pixels
11802 */
11803 public void setPadding(int left, int top, int right, int bottom) {
11804 boolean changed = false;
11805
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011806 mUserPaddingRelative = false;
11807
Adam Powell20232d02010-12-08 21:08:53 -080011808 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011809 mUserPaddingRight = right;
11810 mUserPaddingBottom = bottom;
11811
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011812 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011813
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011814 // Common case is there are no scroll bars.
11815 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011816 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011817 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011818 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011819 switch (mVerticalScrollbarPosition) {
11820 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011821 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11822 left += offset;
11823 } else {
11824 right += offset;
11825 }
11826 break;
Adam Powell20232d02010-12-08 21:08:53 -080011827 case SCROLLBAR_POSITION_RIGHT:
11828 right += offset;
11829 break;
11830 case SCROLLBAR_POSITION_LEFT:
11831 left += offset;
11832 break;
11833 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011834 }
Adam Powell20232d02010-12-08 21:08:53 -080011835 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011836 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11837 ? 0 : getHorizontalScrollbarHeight();
11838 }
11839 }
Romain Guy8506ab42009-06-11 17:35:47 -070011840
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011841 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011842 changed = true;
11843 mPaddingLeft = left;
11844 }
11845 if (mPaddingTop != top) {
11846 changed = true;
11847 mPaddingTop = top;
11848 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011849 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011851 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011853 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011855 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 }
11857
11858 if (changed) {
11859 requestLayout();
11860 }
11861 }
11862
11863 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011864 * Sets the relative padding. The view may add on the space required to display
11865 * the scrollbars, depending on the style and visibility of the scrollbars.
11866 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11867 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11868 * from the values set in this call.
11869 *
11870 * @attr ref android.R.styleable#View_padding
11871 * @attr ref android.R.styleable#View_paddingBottom
11872 * @attr ref android.R.styleable#View_paddingStart
11873 * @attr ref android.R.styleable#View_paddingEnd
11874 * @attr ref android.R.styleable#View_paddingTop
11875 * @param start the start padding in pixels
11876 * @param top the top padding in pixels
11877 * @param end the end padding in pixels
11878 * @param bottom the bottom padding in pixels
11879 *
11880 * @hide
11881 */
11882 public void setPaddingRelative(int start, int top, int end, int bottom) {
11883 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011884
11885 mUserPaddingStart = start;
11886 mUserPaddingEnd = end;
11887
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011888 switch(getResolvedLayoutDirection()) {
11889 case LAYOUT_DIRECTION_RTL:
11890 setPadding(end, top, start, bottom);
11891 break;
11892 case LAYOUT_DIRECTION_LTR:
11893 default:
11894 setPadding(start, top, end, bottom);
11895 }
11896 }
11897
11898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011899 * Returns the top padding of this view.
11900 *
11901 * @return the top padding in pixels
11902 */
11903 public int getPaddingTop() {
11904 return mPaddingTop;
11905 }
11906
11907 /**
11908 * Returns the bottom padding of this view. If there are inset and enabled
11909 * scrollbars, this value may include the space required to display the
11910 * scrollbars as well.
11911 *
11912 * @return the bottom padding in pixels
11913 */
11914 public int getPaddingBottom() {
11915 return mPaddingBottom;
11916 }
11917
11918 /**
11919 * Returns the left padding of this view. If there are inset and enabled
11920 * scrollbars, this value may include the space required to display the
11921 * scrollbars as well.
11922 *
11923 * @return the left padding in pixels
11924 */
11925 public int getPaddingLeft() {
11926 return mPaddingLeft;
11927 }
11928
11929 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011930 * Returns the start padding of this view. If there are inset and enabled
11931 * scrollbars, this value may include the space required to display the
11932 * scrollbars as well.
11933 *
11934 * @return the start padding in pixels
11935 *
11936 * @hide
11937 */
11938 public int getPaddingStart() {
11939 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11940 mPaddingRight : mPaddingLeft;
11941 }
11942
11943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 * Returns the right padding of this view. If there are inset and enabled
11945 * scrollbars, this value may include the space required to display the
11946 * scrollbars as well.
11947 *
11948 * @return the right padding in pixels
11949 */
11950 public int getPaddingRight() {
11951 return mPaddingRight;
11952 }
11953
11954 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011955 * Returns the end padding of this view. If there are inset and enabled
11956 * scrollbars, this value may include the space required to display the
11957 * scrollbars as well.
11958 *
11959 * @return the end padding in pixels
11960 *
11961 * @hide
11962 */
11963 public int getPaddingEnd() {
11964 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11965 mPaddingLeft : mPaddingRight;
11966 }
11967
11968 /**
11969 * Return if the padding as been set thru relative values
11970 * {@link #setPaddingRelative(int, int, int, int)} or thru
11971 * @attr ref android.R.styleable#View_paddingStart or
11972 * @attr ref android.R.styleable#View_paddingEnd
11973 *
11974 * @return true if the padding is relative or false if it is not.
11975 *
11976 * @hide
11977 */
11978 public boolean isPaddingRelative() {
11979 return mUserPaddingRelative;
11980 }
11981
11982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983 * Changes the selection state of this view. A view can be selected or not.
11984 * Note that selection is not the same as focus. Views are typically
11985 * selected in the context of an AdapterView like ListView or GridView;
11986 * the selected view is the view that is highlighted.
11987 *
11988 * @param selected true if the view must be selected, false otherwise
11989 */
11990 public void setSelected(boolean selected) {
11991 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11992 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011993 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011994 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 refreshDrawableState();
11996 dispatchSetSelected(selected);
11997 }
11998 }
11999
12000 /**
12001 * Dispatch setSelected to all of this View's children.
12002 *
12003 * @see #setSelected(boolean)
12004 *
12005 * @param selected The new selected state
12006 */
12007 protected void dispatchSetSelected(boolean selected) {
12008 }
12009
12010 /**
12011 * Indicates the selection state of this view.
12012 *
12013 * @return true if the view is selected, false otherwise
12014 */
12015 @ViewDebug.ExportedProperty
12016 public boolean isSelected() {
12017 return (mPrivateFlags & SELECTED) != 0;
12018 }
12019
12020 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012021 * Changes the activated state of this view. A view can be activated or not.
12022 * Note that activation is not the same as selection. Selection is
12023 * a transient property, representing the view (hierarchy) the user is
12024 * currently interacting with. Activation is a longer-term state that the
12025 * user can move views in and out of. For example, in a list view with
12026 * single or multiple selection enabled, the views in the current selection
12027 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12028 * here.) The activated state is propagated down to children of the view it
12029 * is set on.
12030 *
12031 * @param activated true if the view must be activated, false otherwise
12032 */
12033 public void setActivated(boolean activated) {
12034 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12035 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012036 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012037 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012038 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012039 }
12040 }
12041
12042 /**
12043 * Dispatch setActivated to all of this View's children.
12044 *
12045 * @see #setActivated(boolean)
12046 *
12047 * @param activated The new activated state
12048 */
12049 protected void dispatchSetActivated(boolean activated) {
12050 }
12051
12052 /**
12053 * Indicates the activation state of this view.
12054 *
12055 * @return true if the view is activated, false otherwise
12056 */
12057 @ViewDebug.ExportedProperty
12058 public boolean isActivated() {
12059 return (mPrivateFlags & ACTIVATED) != 0;
12060 }
12061
12062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012063 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12064 * observer can be used to get notifications when global events, like
12065 * layout, happen.
12066 *
12067 * The returned ViewTreeObserver observer is not guaranteed to remain
12068 * valid for the lifetime of this View. If the caller of this method keeps
12069 * a long-lived reference to ViewTreeObserver, it should always check for
12070 * the return value of {@link ViewTreeObserver#isAlive()}.
12071 *
12072 * @return The ViewTreeObserver for this view's hierarchy.
12073 */
12074 public ViewTreeObserver getViewTreeObserver() {
12075 if (mAttachInfo != null) {
12076 return mAttachInfo.mTreeObserver;
12077 }
12078 if (mFloatingTreeObserver == null) {
12079 mFloatingTreeObserver = new ViewTreeObserver();
12080 }
12081 return mFloatingTreeObserver;
12082 }
12083
12084 /**
12085 * <p>Finds the topmost view in the current view hierarchy.</p>
12086 *
12087 * @return the topmost view containing this view
12088 */
12089 public View getRootView() {
12090 if (mAttachInfo != null) {
12091 final View v = mAttachInfo.mRootView;
12092 if (v != null) {
12093 return v;
12094 }
12095 }
Romain Guy8506ab42009-06-11 17:35:47 -070012096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012097 View parent = this;
12098
12099 while (parent.mParent != null && parent.mParent instanceof View) {
12100 parent = (View) parent.mParent;
12101 }
12102
12103 return parent;
12104 }
12105
12106 /**
12107 * <p>Computes the coordinates of this view on the screen. The argument
12108 * must be an array of two integers. After the method returns, the array
12109 * contains the x and y location in that order.</p>
12110 *
12111 * @param location an array of two integers in which to hold the coordinates
12112 */
12113 public void getLocationOnScreen(int[] location) {
12114 getLocationInWindow(location);
12115
12116 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012117 if (info != null) {
12118 location[0] += info.mWindowLeft;
12119 location[1] += info.mWindowTop;
12120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012121 }
12122
12123 /**
12124 * <p>Computes the coordinates of this view in its window. The argument
12125 * must be an array of two integers. After the method returns, the array
12126 * contains the x and y location in that order.</p>
12127 *
12128 * @param location an array of two integers in which to hold the coordinates
12129 */
12130 public void getLocationInWindow(int[] location) {
12131 if (location == null || location.length < 2) {
12132 throw new IllegalArgumentException("location must be an array of "
12133 + "two integers");
12134 }
12135
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012136 location[0] = mLeft;
12137 location[1] = mTop;
12138 if (mTransformationInfo != null) {
12139 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12140 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012142
12143 ViewParent viewParent = mParent;
12144 while (viewParent instanceof View) {
12145 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012146 location[0] += view.mLeft - view.mScrollX;
12147 location[1] += view.mTop - view.mScrollY;
12148 if (view.mTransformationInfo != null) {
12149 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12150 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012152 viewParent = view.mParent;
12153 }
Romain Guy8506ab42009-06-11 17:35:47 -070012154
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012155 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012157 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012158 location[1] -= vr.mCurScrollY;
12159 }
12160 }
12161
12162 /**
12163 * {@hide}
12164 * @param id the id of the view to be found
12165 * @return the view of the specified id, null if cannot be found
12166 */
12167 protected View findViewTraversal(int id) {
12168 if (id == mID) {
12169 return this;
12170 }
12171 return null;
12172 }
12173
12174 /**
12175 * {@hide}
12176 * @param tag the tag of the view to be found
12177 * @return the view of specified tag, null if cannot be found
12178 */
12179 protected View findViewWithTagTraversal(Object tag) {
12180 if (tag != null && tag.equals(mTag)) {
12181 return this;
12182 }
12183 return null;
12184 }
12185
12186 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012187 * {@hide}
12188 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012189 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012190 * @return The first view that matches the predicate or null.
12191 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012192 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012193 if (predicate.apply(this)) {
12194 return this;
12195 }
12196 return null;
12197 }
12198
12199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200 * Look for a child view with the given id. If this view has the given
12201 * id, return this view.
12202 *
12203 * @param id The id to search for.
12204 * @return The view that has the given id in the hierarchy or null
12205 */
12206 public final View findViewById(int id) {
12207 if (id < 0) {
12208 return null;
12209 }
12210 return findViewTraversal(id);
12211 }
12212
12213 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012214 * Finds a view by its unuque and stable accessibility id.
12215 *
12216 * @param accessibilityId The searched accessibility id.
12217 * @return The found view.
12218 */
12219 final View findViewByAccessibilityId(int accessibilityId) {
12220 if (accessibilityId < 0) {
12221 return null;
12222 }
12223 return findViewByAccessibilityIdTraversal(accessibilityId);
12224 }
12225
12226 /**
12227 * Performs the traversal to find a view by its unuque and stable accessibility id.
12228 *
12229 * <strong>Note:</strong>This method does not stop at the root namespace
12230 * boundary since the user can touch the screen at an arbitrary location
12231 * potentially crossing the root namespace bounday which will send an
12232 * accessibility event to accessibility services and they should be able
12233 * to obtain the event source. Also accessibility ids are guaranteed to be
12234 * unique in the window.
12235 *
12236 * @param accessibilityId The accessibility id.
12237 * @return The found view.
12238 */
12239 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12240 if (getAccessibilityViewId() == accessibilityId) {
12241 return this;
12242 }
12243 return null;
12244 }
12245
12246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012247 * Look for a child view with the given tag. If this view has the given
12248 * tag, return this view.
12249 *
12250 * @param tag The tag to search for, using "tag.equals(getTag())".
12251 * @return The View that has the given tag in the hierarchy or null
12252 */
12253 public final View findViewWithTag(Object tag) {
12254 if (tag == null) {
12255 return null;
12256 }
12257 return findViewWithTagTraversal(tag);
12258 }
12259
12260 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012261 * {@hide}
12262 * Look for a child view that matches the specified predicate.
12263 * If this view matches the predicate, return this view.
12264 *
12265 * @param predicate The predicate to evaluate.
12266 * @return The first view that matches the predicate or null.
12267 */
12268 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012269 return findViewByPredicateTraversal(predicate, null);
12270 }
12271
12272 /**
12273 * {@hide}
12274 * Look for a child view that matches the specified predicate,
12275 * starting with the specified view and its descendents and then
12276 * recusively searching the ancestors and siblings of that view
12277 * until this view is reached.
12278 *
12279 * This method is useful in cases where the predicate does not match
12280 * a single unique view (perhaps multiple views use the same id)
12281 * and we are trying to find the view that is "closest" in scope to the
12282 * starting view.
12283 *
12284 * @param start The view to start from.
12285 * @param predicate The predicate to evaluate.
12286 * @return The first view that matches the predicate or null.
12287 */
12288 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12289 View childToSkip = null;
12290 for (;;) {
12291 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12292 if (view != null || start == this) {
12293 return view;
12294 }
12295
12296 ViewParent parent = start.getParent();
12297 if (parent == null || !(parent instanceof View)) {
12298 return null;
12299 }
12300
12301 childToSkip = start;
12302 start = (View) parent;
12303 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012304 }
12305
12306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307 * Sets the identifier for this view. The identifier does not have to be
12308 * unique in this view's hierarchy. The identifier should be a positive
12309 * number.
12310 *
12311 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012312 * @see #getId()
12313 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 *
12315 * @param id a number used to identify the view
12316 *
12317 * @attr ref android.R.styleable#View_id
12318 */
12319 public void setId(int id) {
12320 mID = id;
12321 }
12322
12323 /**
12324 * {@hide}
12325 *
12326 * @param isRoot true if the view belongs to the root namespace, false
12327 * otherwise
12328 */
12329 public void setIsRootNamespace(boolean isRoot) {
12330 if (isRoot) {
12331 mPrivateFlags |= IS_ROOT_NAMESPACE;
12332 } else {
12333 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12334 }
12335 }
12336
12337 /**
12338 * {@hide}
12339 *
12340 * @return true if the view belongs to the root namespace, false otherwise
12341 */
12342 public boolean isRootNamespace() {
12343 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12344 }
12345
12346 /**
12347 * Returns this view's identifier.
12348 *
12349 * @return a positive integer used to identify the view or {@link #NO_ID}
12350 * if the view has no ID
12351 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012352 * @see #setId(int)
12353 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012354 * @attr ref android.R.styleable#View_id
12355 */
12356 @ViewDebug.CapturedViewProperty
12357 public int getId() {
12358 return mID;
12359 }
12360
12361 /**
12362 * Returns this view's tag.
12363 *
12364 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012365 *
12366 * @see #setTag(Object)
12367 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012368 */
12369 @ViewDebug.ExportedProperty
12370 public Object getTag() {
12371 return mTag;
12372 }
12373
12374 /**
12375 * Sets the tag associated with this view. A tag can be used to mark
12376 * a view in its hierarchy and does not have to be unique within the
12377 * hierarchy. Tags can also be used to store data within a view without
12378 * resorting to another data structure.
12379 *
12380 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012381 *
12382 * @see #getTag()
12383 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012384 */
12385 public void setTag(final Object tag) {
12386 mTag = tag;
12387 }
12388
12389 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012390 * Returns the tag associated with this view and the specified key.
12391 *
12392 * @param key The key identifying the tag
12393 *
12394 * @return the Object stored in this view as a tag
12395 *
12396 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012397 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012398 */
12399 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012400 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012401 return null;
12402 }
12403
12404 /**
12405 * Sets a tag associated with this view and a key. A tag can be used
12406 * to mark a view in its hierarchy and does not have to be unique within
12407 * the hierarchy. Tags can also be used to store data within a view
12408 * without resorting to another data structure.
12409 *
12410 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012411 * application to ensure it is unique (see the <a
12412 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12413 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012414 * the Android framework or not associated with any package will cause
12415 * an {@link IllegalArgumentException} to be thrown.
12416 *
12417 * @param key The key identifying the tag
12418 * @param tag An Object to tag the view with
12419 *
12420 * @throws IllegalArgumentException If they specified key is not valid
12421 *
12422 * @see #setTag(Object)
12423 * @see #getTag(int)
12424 */
12425 public void setTag(int key, final Object tag) {
12426 // If the package id is 0x00 or 0x01, it's either an undefined package
12427 // or a framework id
12428 if ((key >>> 24) < 2) {
12429 throw new IllegalArgumentException("The key must be an application-specific "
12430 + "resource id.");
12431 }
12432
Adam Powell2b2f6d62011-09-23 11:15:39 -070012433 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012434 }
12435
12436 /**
12437 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12438 * framework id.
12439 *
12440 * @hide
12441 */
12442 public void setTagInternal(int key, Object tag) {
12443 if ((key >>> 24) != 0x1) {
12444 throw new IllegalArgumentException("The key must be a framework-specific "
12445 + "resource id.");
12446 }
12447
Adam Powell2b2f6d62011-09-23 11:15:39 -070012448 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012449 }
12450
Adam Powell2b2f6d62011-09-23 11:15:39 -070012451 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012452 if (mKeyedTags == null) {
12453 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012454 }
12455
Adam Powell7db82ac2011-09-22 19:44:04 -070012456 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012457 }
12458
12459 /**
Romain Guy13922e02009-05-12 17:56:14 -070012460 * @param consistency The type of consistency. See ViewDebug for more information.
12461 *
12462 * @hide
12463 */
12464 protected boolean dispatchConsistencyCheck(int consistency) {
12465 return onConsistencyCheck(consistency);
12466 }
12467
12468 /**
12469 * Method that subclasses should implement to check their consistency. The type of
12470 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012471 *
Romain Guy13922e02009-05-12 17:56:14 -070012472 * @param consistency The type of consistency. See ViewDebug for more information.
12473 *
12474 * @throws IllegalStateException if the view is in an inconsistent state.
12475 *
12476 * @hide
12477 */
12478 protected boolean onConsistencyCheck(int consistency) {
12479 boolean result = true;
12480
12481 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12482 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12483
12484 if (checkLayout) {
12485 if (getParent() == null) {
12486 result = false;
12487 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12488 "View " + this + " does not have a parent.");
12489 }
12490
12491 if (mAttachInfo == null) {
12492 result = false;
12493 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12494 "View " + this + " is not attached to a window.");
12495 }
12496 }
12497
12498 if (checkDrawing) {
12499 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12500 // from their draw() method
12501
12502 if ((mPrivateFlags & DRAWN) != DRAWN &&
12503 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12504 result = false;
12505 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12506 "View " + this + " was invalidated but its drawing cache is valid.");
12507 }
12508 }
12509
12510 return result;
12511 }
12512
12513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 * Prints information about this view in the log output, with the tag
12515 * {@link #VIEW_LOG_TAG}.
12516 *
12517 * @hide
12518 */
12519 public void debug() {
12520 debug(0);
12521 }
12522
12523 /**
12524 * Prints information about this view in the log output, with the tag
12525 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12526 * indentation defined by the <code>depth</code>.
12527 *
12528 * @param depth the indentation level
12529 *
12530 * @hide
12531 */
12532 protected void debug(int depth) {
12533 String output = debugIndent(depth - 1);
12534
12535 output += "+ " + this;
12536 int id = getId();
12537 if (id != -1) {
12538 output += " (id=" + id + ")";
12539 }
12540 Object tag = getTag();
12541 if (tag != null) {
12542 output += " (tag=" + tag + ")";
12543 }
12544 Log.d(VIEW_LOG_TAG, output);
12545
12546 if ((mPrivateFlags & FOCUSED) != 0) {
12547 output = debugIndent(depth) + " FOCUSED";
12548 Log.d(VIEW_LOG_TAG, output);
12549 }
12550
12551 output = debugIndent(depth);
12552 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12553 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12554 + "} ";
12555 Log.d(VIEW_LOG_TAG, output);
12556
12557 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12558 || mPaddingBottom != 0) {
12559 output = debugIndent(depth);
12560 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12561 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12562 Log.d(VIEW_LOG_TAG, output);
12563 }
12564
12565 output = debugIndent(depth);
12566 output += "mMeasureWidth=" + mMeasuredWidth +
12567 " mMeasureHeight=" + mMeasuredHeight;
12568 Log.d(VIEW_LOG_TAG, output);
12569
12570 output = debugIndent(depth);
12571 if (mLayoutParams == null) {
12572 output += "BAD! no layout params";
12573 } else {
12574 output = mLayoutParams.debug(output);
12575 }
12576 Log.d(VIEW_LOG_TAG, output);
12577
12578 output = debugIndent(depth);
12579 output += "flags={";
12580 output += View.printFlags(mViewFlags);
12581 output += "}";
12582 Log.d(VIEW_LOG_TAG, output);
12583
12584 output = debugIndent(depth);
12585 output += "privateFlags={";
12586 output += View.printPrivateFlags(mPrivateFlags);
12587 output += "}";
12588 Log.d(VIEW_LOG_TAG, output);
12589 }
12590
12591 /**
12592 * Creates an string of whitespaces used for indentation.
12593 *
12594 * @param depth the indentation level
12595 * @return a String containing (depth * 2 + 3) * 2 white spaces
12596 *
12597 * @hide
12598 */
12599 protected static String debugIndent(int depth) {
12600 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12601 for (int i = 0; i < (depth * 2) + 3; i++) {
12602 spaces.append(' ').append(' ');
12603 }
12604 return spaces.toString();
12605 }
12606
12607 /**
12608 * <p>Return the offset of the widget's text baseline from the widget's top
12609 * boundary. If this widget does not support baseline alignment, this
12610 * method returns -1. </p>
12611 *
12612 * @return the offset of the baseline within the widget's bounds or -1
12613 * if baseline alignment is not supported
12614 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012615 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012616 public int getBaseline() {
12617 return -1;
12618 }
12619
12620 /**
12621 * Call this when something has changed which has invalidated the
12622 * layout of this view. This will schedule a layout pass of the view
12623 * tree.
12624 */
12625 public void requestLayout() {
12626 if (ViewDebug.TRACE_HIERARCHY) {
12627 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12628 }
12629
12630 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012631 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012632
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012633 if (mParent != null) {
12634 if (mLayoutParams != null) {
12635 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12636 }
12637 if (!mParent.isLayoutRequested()) {
12638 mParent.requestLayout();
12639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 }
12641 }
12642
12643 /**
12644 * Forces this view to be laid out during the next layout pass.
12645 * This method does not call requestLayout() or forceLayout()
12646 * on the parent.
12647 */
12648 public void forceLayout() {
12649 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012650 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012651 }
12652
12653 /**
12654 * <p>
12655 * This is called to find out how big a view should be. The parent
12656 * supplies constraint information in the width and height parameters.
12657 * </p>
12658 *
12659 * <p>
12660 * The actual mesurement work of a view is performed in
12661 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12662 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12663 * </p>
12664 *
12665 *
12666 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12667 * parent
12668 * @param heightMeasureSpec Vertical space requirements as imposed by the
12669 * parent
12670 *
12671 * @see #onMeasure(int, int)
12672 */
12673 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12674 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12675 widthMeasureSpec != mOldWidthMeasureSpec ||
12676 heightMeasureSpec != mOldHeightMeasureSpec) {
12677
12678 // first clears the measured dimension flag
12679 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12680
12681 if (ViewDebug.TRACE_HIERARCHY) {
12682 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12683 }
12684
12685 // measure ourselves, this should set the measured dimension flag back
12686 onMeasure(widthMeasureSpec, heightMeasureSpec);
12687
12688 // flag not set, setMeasuredDimension() was not invoked, we raise
12689 // an exception to warn the developer
12690 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12691 throw new IllegalStateException("onMeasure() did not set the"
12692 + " measured dimension by calling"
12693 + " setMeasuredDimension()");
12694 }
12695
12696 mPrivateFlags |= LAYOUT_REQUIRED;
12697 }
12698
12699 mOldWidthMeasureSpec = widthMeasureSpec;
12700 mOldHeightMeasureSpec = heightMeasureSpec;
12701 }
12702
12703 /**
12704 * <p>
12705 * Measure the view and its content to determine the measured width and the
12706 * measured height. This method is invoked by {@link #measure(int, int)} and
12707 * should be overriden by subclasses to provide accurate and efficient
12708 * measurement of their contents.
12709 * </p>
12710 *
12711 * <p>
12712 * <strong>CONTRACT:</strong> When overriding this method, you
12713 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12714 * measured width and height of this view. Failure to do so will trigger an
12715 * <code>IllegalStateException</code>, thrown by
12716 * {@link #measure(int, int)}. Calling the superclass'
12717 * {@link #onMeasure(int, int)} is a valid use.
12718 * </p>
12719 *
12720 * <p>
12721 * The base class implementation of measure defaults to the background size,
12722 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12723 * override {@link #onMeasure(int, int)} to provide better measurements of
12724 * their content.
12725 * </p>
12726 *
12727 * <p>
12728 * If this method is overridden, it is the subclass's responsibility to make
12729 * sure the measured height and width are at least the view's minimum height
12730 * and width ({@link #getSuggestedMinimumHeight()} and
12731 * {@link #getSuggestedMinimumWidth()}).
12732 * </p>
12733 *
12734 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12735 * The requirements are encoded with
12736 * {@link android.view.View.MeasureSpec}.
12737 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12738 * The requirements are encoded with
12739 * {@link android.view.View.MeasureSpec}.
12740 *
12741 * @see #getMeasuredWidth()
12742 * @see #getMeasuredHeight()
12743 * @see #setMeasuredDimension(int, int)
12744 * @see #getSuggestedMinimumHeight()
12745 * @see #getSuggestedMinimumWidth()
12746 * @see android.view.View.MeasureSpec#getMode(int)
12747 * @see android.view.View.MeasureSpec#getSize(int)
12748 */
12749 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12750 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12751 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12752 }
12753
12754 /**
12755 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12756 * measured width and measured height. Failing to do so will trigger an
12757 * exception at measurement time.</p>
12758 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012759 * @param measuredWidth The measured width of this view. May be a complex
12760 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12761 * {@link #MEASURED_STATE_TOO_SMALL}.
12762 * @param measuredHeight The measured height of this view. May be a complex
12763 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12764 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012765 */
12766 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12767 mMeasuredWidth = measuredWidth;
12768 mMeasuredHeight = measuredHeight;
12769
12770 mPrivateFlags |= MEASURED_DIMENSION_SET;
12771 }
12772
12773 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012774 * Merge two states as returned by {@link #getMeasuredState()}.
12775 * @param curState The current state as returned from a view or the result
12776 * of combining multiple views.
12777 * @param newState The new view state to combine.
12778 * @return Returns a new integer reflecting the combination of the two
12779 * states.
12780 */
12781 public static int combineMeasuredStates(int curState, int newState) {
12782 return curState | newState;
12783 }
12784
12785 /**
12786 * Version of {@link #resolveSizeAndState(int, int, int)}
12787 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12788 */
12789 public static int resolveSize(int size, int measureSpec) {
12790 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12791 }
12792
12793 /**
12794 * Utility to reconcile a desired size and state, with constraints imposed
12795 * by a MeasureSpec. Will take the desired size, unless a different size
12796 * is imposed by the constraints. The returned value is a compound integer,
12797 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12798 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12799 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012800 *
12801 * @param size How big the view wants to be
12802 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012803 * @return Size information bit mask as defined by
12804 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012806 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012807 int result = size;
12808 int specMode = MeasureSpec.getMode(measureSpec);
12809 int specSize = MeasureSpec.getSize(measureSpec);
12810 switch (specMode) {
12811 case MeasureSpec.UNSPECIFIED:
12812 result = size;
12813 break;
12814 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012815 if (specSize < size) {
12816 result = specSize | MEASURED_STATE_TOO_SMALL;
12817 } else {
12818 result = size;
12819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012820 break;
12821 case MeasureSpec.EXACTLY:
12822 result = specSize;
12823 break;
12824 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012825 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012826 }
12827
12828 /**
12829 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012830 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012831 * by the MeasureSpec.
12832 *
12833 * @param size Default size for this view
12834 * @param measureSpec Constraints imposed by the parent
12835 * @return The size this view should be.
12836 */
12837 public static int getDefaultSize(int size, int measureSpec) {
12838 int result = size;
12839 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012840 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012841
12842 switch (specMode) {
12843 case MeasureSpec.UNSPECIFIED:
12844 result = size;
12845 break;
12846 case MeasureSpec.AT_MOST:
12847 case MeasureSpec.EXACTLY:
12848 result = specSize;
12849 break;
12850 }
12851 return result;
12852 }
12853
12854 /**
12855 * Returns the suggested minimum height that the view should use. This
12856 * returns the maximum of the view's minimum height
12857 * and the background's minimum height
12858 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12859 * <p>
12860 * When being used in {@link #onMeasure(int, int)}, the caller should still
12861 * ensure the returned height is within the requirements of the parent.
12862 *
12863 * @return The suggested minimum height of the view.
12864 */
12865 protected int getSuggestedMinimumHeight() {
12866 int suggestedMinHeight = mMinHeight;
12867
12868 if (mBGDrawable != null) {
12869 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12870 if (suggestedMinHeight < bgMinHeight) {
12871 suggestedMinHeight = bgMinHeight;
12872 }
12873 }
12874
12875 return suggestedMinHeight;
12876 }
12877
12878 /**
12879 * Returns the suggested minimum width that the view should use. This
12880 * returns the maximum of the view's minimum width)
12881 * and the background's minimum width
12882 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12883 * <p>
12884 * When being used in {@link #onMeasure(int, int)}, the caller should still
12885 * ensure the returned width is within the requirements of the parent.
12886 *
12887 * @return The suggested minimum width of the view.
12888 */
12889 protected int getSuggestedMinimumWidth() {
12890 int suggestedMinWidth = mMinWidth;
12891
12892 if (mBGDrawable != null) {
12893 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12894 if (suggestedMinWidth < bgMinWidth) {
12895 suggestedMinWidth = bgMinWidth;
12896 }
12897 }
12898
12899 return suggestedMinWidth;
12900 }
12901
12902 /**
12903 * Sets the minimum height of the view. It is not guaranteed the view will
12904 * be able to achieve this minimum height (for example, if its parent layout
12905 * constrains it with less available height).
12906 *
12907 * @param minHeight The minimum height the view will try to be.
12908 */
12909 public void setMinimumHeight(int minHeight) {
12910 mMinHeight = minHeight;
12911 }
12912
12913 /**
12914 * Sets the minimum width of the view. It is not guaranteed the view will
12915 * be able to achieve this minimum width (for example, if its parent layout
12916 * constrains it with less available width).
12917 *
12918 * @param minWidth The minimum width the view will try to be.
12919 */
12920 public void setMinimumWidth(int minWidth) {
12921 mMinWidth = minWidth;
12922 }
12923
12924 /**
12925 * Get the animation currently associated with this view.
12926 *
12927 * @return The animation that is currently playing or
12928 * scheduled to play for this view.
12929 */
12930 public Animation getAnimation() {
12931 return mCurrentAnimation;
12932 }
12933
12934 /**
12935 * Start the specified animation now.
12936 *
12937 * @param animation the animation to start now
12938 */
12939 public void startAnimation(Animation animation) {
12940 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12941 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012942 invalidateParentCaches();
12943 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012944 }
12945
12946 /**
12947 * Cancels any animations for this view.
12948 */
12949 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012950 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012951 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012953 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012954 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012955 }
12956
12957 /**
12958 * Sets the next animation to play for this view.
12959 * If you want the animation to play immediately, use
12960 * startAnimation. This method provides allows fine-grained
12961 * control over the start time and invalidation, but you
12962 * must make sure that 1) the animation has a start time set, and
12963 * 2) the view will be invalidated when the animation is supposed to
12964 * start.
12965 *
12966 * @param animation The next animation, or null.
12967 */
12968 public void setAnimation(Animation animation) {
12969 mCurrentAnimation = animation;
12970 if (animation != null) {
12971 animation.reset();
12972 }
12973 }
12974
12975 /**
12976 * Invoked by a parent ViewGroup to notify the start of the animation
12977 * currently associated with this view. If you override this method,
12978 * always call super.onAnimationStart();
12979 *
12980 * @see #setAnimation(android.view.animation.Animation)
12981 * @see #getAnimation()
12982 */
12983 protected void onAnimationStart() {
12984 mPrivateFlags |= ANIMATION_STARTED;
12985 }
12986
12987 /**
12988 * Invoked by a parent ViewGroup to notify the end of the animation
12989 * currently associated with this view. If you override this method,
12990 * always call super.onAnimationEnd();
12991 *
12992 * @see #setAnimation(android.view.animation.Animation)
12993 * @see #getAnimation()
12994 */
12995 protected void onAnimationEnd() {
12996 mPrivateFlags &= ~ANIMATION_STARTED;
12997 }
12998
12999 /**
13000 * Invoked if there is a Transform that involves alpha. Subclass that can
13001 * draw themselves with the specified alpha should return true, and then
13002 * respect that alpha when their onDraw() is called. If this returns false
13003 * then the view may be redirected to draw into an offscreen buffer to
13004 * fulfill the request, which will look fine, but may be slower than if the
13005 * subclass handles it internally. The default implementation returns false.
13006 *
13007 * @param alpha The alpha (0..255) to apply to the view's drawing
13008 * @return true if the view can draw with the specified alpha.
13009 */
13010 protected boolean onSetAlpha(int alpha) {
13011 return false;
13012 }
13013
13014 /**
13015 * This is used by the RootView to perform an optimization when
13016 * the view hierarchy contains one or several SurfaceView.
13017 * SurfaceView is always considered transparent, but its children are not,
13018 * therefore all View objects remove themselves from the global transparent
13019 * region (passed as a parameter to this function).
13020 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013021 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013022 *
13023 * @return Returns true if the effective visibility of the view at this
13024 * point is opaque, regardless of the transparent region; returns false
13025 * if it is possible for underlying windows to be seen behind the view.
13026 *
13027 * {@hide}
13028 */
13029 public boolean gatherTransparentRegion(Region region) {
13030 final AttachInfo attachInfo = mAttachInfo;
13031 if (region != null && attachInfo != null) {
13032 final int pflags = mPrivateFlags;
13033 if ((pflags & SKIP_DRAW) == 0) {
13034 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13035 // remove it from the transparent region.
13036 final int[] location = attachInfo.mTransparentLocation;
13037 getLocationInWindow(location);
13038 region.op(location[0], location[1], location[0] + mRight - mLeft,
13039 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13040 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13041 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13042 // exists, so we remove the background drawable's non-transparent
13043 // parts from this transparent region.
13044 applyDrawableToTransparentRegion(mBGDrawable, region);
13045 }
13046 }
13047 return true;
13048 }
13049
13050 /**
13051 * Play a sound effect for this view.
13052 *
13053 * <p>The framework will play sound effects for some built in actions, such as
13054 * clicking, but you may wish to play these effects in your widget,
13055 * for instance, for internal navigation.
13056 *
13057 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13058 * {@link #isSoundEffectsEnabled()} is true.
13059 *
13060 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13061 */
13062 public void playSoundEffect(int soundConstant) {
13063 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13064 return;
13065 }
13066 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13067 }
13068
13069 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013070 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013071 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013072 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013073 *
13074 * <p>The framework will provide haptic feedback for some built in actions,
13075 * such as long presses, but you may wish to provide feedback for your
13076 * own widget.
13077 *
13078 * <p>The feedback will only be performed if
13079 * {@link #isHapticFeedbackEnabled()} is true.
13080 *
13081 * @param feedbackConstant One of the constants defined in
13082 * {@link HapticFeedbackConstants}
13083 */
13084 public boolean performHapticFeedback(int feedbackConstant) {
13085 return performHapticFeedback(feedbackConstant, 0);
13086 }
13087
13088 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013089 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013090 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013091 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013092 *
13093 * @param feedbackConstant One of the constants defined in
13094 * {@link HapticFeedbackConstants}
13095 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13096 */
13097 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13098 if (mAttachInfo == null) {
13099 return false;
13100 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013101 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013102 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013103 && !isHapticFeedbackEnabled()) {
13104 return false;
13105 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013106 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13107 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013108 }
13109
13110 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013111 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013112 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13113 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013114 */
13115 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013116 if (visibility != mSystemUiVisibility) {
13117 mSystemUiVisibility = visibility;
13118 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13119 mParent.recomputeViewAttributes(this);
13120 }
Joe Onorato664644d2011-01-23 17:53:23 -080013121 }
13122 }
13123
13124 /**
13125 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013126 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13127 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013128 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013129 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013130 return mSystemUiVisibility;
13131 }
13132
Scott Mainec6331b2011-05-24 16:55:56 -070013133 /**
13134 * Set a listener to receive callbacks when the visibility of the system bar changes.
13135 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13136 */
Joe Onorato664644d2011-01-23 17:53:23 -080013137 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013138 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013139 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13140 mParent.recomputeViewAttributes(this);
13141 }
13142 }
13143
13144 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013145 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13146 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013147 */
13148 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013149 ListenerInfo li = mListenerInfo;
13150 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13151 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013152 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013153 }
13154 }
13155
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013156 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13157 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13158 if (val != mSystemUiVisibility) {
13159 setSystemUiVisibility(val);
13160 }
13161 }
13162
Joe Onorato664644d2011-01-23 17:53:23 -080013163 /**
Joe Malin32736f02011-01-19 16:14:20 -080013164 * Creates an image that the system displays during the drag and drop
13165 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13166 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13167 * appearance as the given View. The default also positions the center of the drag shadow
13168 * directly under the touch point. If no View is provided (the constructor with no parameters
13169 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13170 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13171 * default is an invisible drag shadow.
13172 * <p>
13173 * You are not required to use the View you provide to the constructor as the basis of the
13174 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13175 * anything you want as the drag shadow.
13176 * </p>
13177 * <p>
13178 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13179 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13180 * size and position of the drag shadow. It uses this data to construct a
13181 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13182 * so that your application can draw the shadow image in the Canvas.
13183 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013184 *
13185 * <div class="special reference">
13186 * <h3>Developer Guides</h3>
13187 * <p>For a guide to implementing drag and drop features, read the
13188 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13189 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013190 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013191 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013192 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013193
13194 /**
Joe Malin32736f02011-01-19 16:14:20 -080013195 * Constructs a shadow image builder based on a View. By default, the resulting drag
13196 * shadow will have the same appearance and dimensions as the View, with the touch point
13197 * over the center of the View.
13198 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013199 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013200 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013201 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013202 }
13203
Christopher Tate17ed60c2011-01-18 12:50:26 -080013204 /**
13205 * Construct a shadow builder object with no associated View. This
13206 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13207 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13208 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013209 * reference to any View object. If they are not overridden, then the result is an
13210 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013211 */
13212 public DragShadowBuilder() {
13213 mView = new WeakReference<View>(null);
13214 }
13215
13216 /**
13217 * Returns the View object that had been passed to the
13218 * {@link #View.DragShadowBuilder(View)}
13219 * constructor. If that View parameter was {@code null} or if the
13220 * {@link #View.DragShadowBuilder()}
13221 * constructor was used to instantiate the builder object, this method will return
13222 * null.
13223 *
13224 * @return The View object associate with this builder object.
13225 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013226 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013227 final public View getView() {
13228 return mView.get();
13229 }
13230
Christopher Tate2c095f32010-10-04 14:13:40 -070013231 /**
Joe Malin32736f02011-01-19 16:14:20 -080013232 * Provides the metrics for the shadow image. These include the dimensions of
13233 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013234 * be centered under the touch location while dragging.
13235 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013236 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013237 * same as the dimensions of the View itself and centers the shadow under
13238 * the touch point.
13239 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013240 *
Joe Malin32736f02011-01-19 16:14:20 -080013241 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13242 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13243 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13244 * image.
13245 *
13246 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13247 * shadow image that should be underneath the touch point during the drag and drop
13248 * operation. Your application must set {@link android.graphics.Point#x} to the
13249 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013250 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013251 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013252 final View view = mView.get();
13253 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013254 shadowSize.set(view.getWidth(), view.getHeight());
13255 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013256 } else {
13257 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13258 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013259 }
13260
13261 /**
Joe Malin32736f02011-01-19 16:14:20 -080013262 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13263 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013264 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013265 *
Joe Malin32736f02011-01-19 16:14:20 -080013266 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013267 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013268 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013269 final View view = mView.get();
13270 if (view != null) {
13271 view.draw(canvas);
13272 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013273 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013274 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013275 }
13276 }
13277
13278 /**
Joe Malin32736f02011-01-19 16:14:20 -080013279 * Starts a drag and drop operation. When your application calls this method, it passes a
13280 * {@link android.view.View.DragShadowBuilder} object to the system. The
13281 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13282 * to get metrics for the drag shadow, and then calls the object's
13283 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13284 * <p>
13285 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13286 * drag events to all the View objects in your application that are currently visible. It does
13287 * this either by calling the View object's drag listener (an implementation of
13288 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13289 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13290 * Both are passed a {@link android.view.DragEvent} object that has a
13291 * {@link android.view.DragEvent#getAction()} value of
13292 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13293 * </p>
13294 * <p>
13295 * Your application can invoke startDrag() on any attached View object. The View object does not
13296 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13297 * be related to the View the user selected for dragging.
13298 * </p>
13299 * @param data A {@link android.content.ClipData} object pointing to the data to be
13300 * transferred by the drag and drop operation.
13301 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13302 * drag shadow.
13303 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13304 * drop operation. This Object is put into every DragEvent object sent by the system during the
13305 * current drag.
13306 * <p>
13307 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13308 * to the target Views. For example, it can contain flags that differentiate between a
13309 * a copy operation and a move operation.
13310 * </p>
13311 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13312 * so the parameter should be set to 0.
13313 * @return {@code true} if the method completes successfully, or
13314 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13315 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013316 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013317 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013318 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013319 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013320 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013321 }
13322 boolean okay = false;
13323
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013324 Point shadowSize = new Point();
13325 Point shadowTouchPoint = new Point();
13326 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013327
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013328 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13329 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13330 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013331 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013332
Chris Tatea32dcf72010-10-14 12:13:50 -070013333 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013334 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13335 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013336 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013337 Surface surface = new Surface();
13338 try {
13339 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013340 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013341 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013342 + " surface=" + surface);
13343 if (token != null) {
13344 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013345 try {
Chris Tate6b391282010-10-14 15:48:59 -070013346 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013347 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013348 } finally {
13349 surface.unlockCanvasAndPost(canvas);
13350 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013351
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013352 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013353
13354 // Cache the local state object for delivery with DragEvents
13355 root.setLocalDragState(myLocalState);
13356
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013357 // repurpose 'shadowSize' for the last touch point
13358 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013359
Christopher Tatea53146c2010-09-07 11:57:52 -070013360 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013361 shadowSize.x, shadowSize.y,
13362 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013363 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013364
13365 // Off and running! Release our local surface instance; the drag
13366 // shadow surface is now managed by the system process.
13367 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013368 }
13369 } catch (Exception e) {
13370 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13371 surface.destroy();
13372 }
13373
13374 return okay;
13375 }
13376
Christopher Tatea53146c2010-09-07 11:57:52 -070013377 /**
Joe Malin32736f02011-01-19 16:14:20 -080013378 * Handles drag events sent by the system following a call to
13379 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13380 *<p>
13381 * When the system calls this method, it passes a
13382 * {@link android.view.DragEvent} object. A call to
13383 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13384 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13385 * operation.
13386 * @param event The {@link android.view.DragEvent} sent by the system.
13387 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13388 * in DragEvent, indicating the type of drag event represented by this object.
13389 * @return {@code true} if the method was successful, otherwise {@code false}.
13390 * <p>
13391 * The method should return {@code true} in response to an action type of
13392 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13393 * operation.
13394 * </p>
13395 * <p>
13396 * The method should also return {@code true} in response to an action type of
13397 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13398 * {@code false} if it didn't.
13399 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013400 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013401 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013402 return false;
13403 }
13404
13405 /**
Joe Malin32736f02011-01-19 16:14:20 -080013406 * Detects if this View is enabled and has a drag event listener.
13407 * If both are true, then it calls the drag event listener with the
13408 * {@link android.view.DragEvent} it received. If the drag event listener returns
13409 * {@code true}, then dispatchDragEvent() returns {@code true}.
13410 * <p>
13411 * For all other cases, the method calls the
13412 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13413 * method and returns its result.
13414 * </p>
13415 * <p>
13416 * This ensures that a drag event is always consumed, even if the View does not have a drag
13417 * event listener. However, if the View has a listener and the listener returns true, then
13418 * onDragEvent() is not called.
13419 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013420 */
13421 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013422 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013423 ListenerInfo li = mListenerInfo;
13424 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13425 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013426 return true;
13427 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013428 return onDragEvent(event);
13429 }
13430
Christopher Tate3d4bf172011-03-28 16:16:46 -070013431 boolean canAcceptDrag() {
13432 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13433 }
13434
Christopher Tatea53146c2010-09-07 11:57:52 -070013435 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013436 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13437 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013438 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013439 */
13440 public void onCloseSystemDialogs(String reason) {
13441 }
Joe Malin32736f02011-01-19 16:14:20 -080013442
Dianne Hackbornffa42482009-09-23 22:20:11 -070013443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013445 * update a Region being computed for
13446 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013447 * that any non-transparent parts of the Drawable are removed from the
13448 * given transparent region.
13449 *
13450 * @param dr The Drawable whose transparency is to be applied to the region.
13451 * @param region A Region holding the current transparency information,
13452 * where any parts of the region that are set are considered to be
13453 * transparent. On return, this region will be modified to have the
13454 * transparency information reduced by the corresponding parts of the
13455 * Drawable that are not transparent.
13456 * {@hide}
13457 */
13458 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13459 if (DBG) {
13460 Log.i("View", "Getting transparent region for: " + this);
13461 }
13462 final Region r = dr.getTransparentRegion();
13463 final Rect db = dr.getBounds();
13464 final AttachInfo attachInfo = mAttachInfo;
13465 if (r != null && attachInfo != null) {
13466 final int w = getRight()-getLeft();
13467 final int h = getBottom()-getTop();
13468 if (db.left > 0) {
13469 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13470 r.op(0, 0, db.left, h, Region.Op.UNION);
13471 }
13472 if (db.right < w) {
13473 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13474 r.op(db.right, 0, w, h, Region.Op.UNION);
13475 }
13476 if (db.top > 0) {
13477 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13478 r.op(0, 0, w, db.top, Region.Op.UNION);
13479 }
13480 if (db.bottom < h) {
13481 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13482 r.op(0, db.bottom, w, h, Region.Op.UNION);
13483 }
13484 final int[] location = attachInfo.mTransparentLocation;
13485 getLocationInWindow(location);
13486 r.translate(location[0], location[1]);
13487 region.op(r, Region.Op.INTERSECT);
13488 } else {
13489 region.op(db, Region.Op.DIFFERENCE);
13490 }
13491 }
13492
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013493 private void checkForLongClick(int delayOffset) {
13494 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13495 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013496
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013497 if (mPendingCheckForLongPress == null) {
13498 mPendingCheckForLongPress = new CheckForLongPress();
13499 }
13500 mPendingCheckForLongPress.rememberWindowAttachCount();
13501 postDelayed(mPendingCheckForLongPress,
13502 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013504 }
13505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 /**
13507 * Inflate a view from an XML resource. This convenience method wraps the {@link
13508 * LayoutInflater} class, which provides a full range of options for view inflation.
13509 *
13510 * @param context The Context object for your activity or application.
13511 * @param resource The resource ID to inflate
13512 * @param root A view group that will be the parent. Used to properly inflate the
13513 * layout_* parameters.
13514 * @see LayoutInflater
13515 */
13516 public static View inflate(Context context, int resource, ViewGroup root) {
13517 LayoutInflater factory = LayoutInflater.from(context);
13518 return factory.inflate(resource, root);
13519 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013521 /**
Adam Powell637d3372010-08-25 14:37:03 -070013522 * Scroll the view with standard behavior for scrolling beyond the normal
13523 * content boundaries. Views that call this method should override
13524 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13525 * results of an over-scroll operation.
13526 *
13527 * Views can use this method to handle any touch or fling-based scrolling.
13528 *
13529 * @param deltaX Change in X in pixels
13530 * @param deltaY Change in Y in pixels
13531 * @param scrollX Current X scroll value in pixels before applying deltaX
13532 * @param scrollY Current Y scroll value in pixels before applying deltaY
13533 * @param scrollRangeX Maximum content scroll range along the X axis
13534 * @param scrollRangeY Maximum content scroll range along the Y axis
13535 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13536 * along the X axis.
13537 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13538 * along the Y axis.
13539 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13540 * @return true if scrolling was clamped to an over-scroll boundary along either
13541 * axis, false otherwise.
13542 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013543 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013544 protected boolean overScrollBy(int deltaX, int deltaY,
13545 int scrollX, int scrollY,
13546 int scrollRangeX, int scrollRangeY,
13547 int maxOverScrollX, int maxOverScrollY,
13548 boolean isTouchEvent) {
13549 final int overScrollMode = mOverScrollMode;
13550 final boolean canScrollHorizontal =
13551 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13552 final boolean canScrollVertical =
13553 computeVerticalScrollRange() > computeVerticalScrollExtent();
13554 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13555 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13556 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13557 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13558
13559 int newScrollX = scrollX + deltaX;
13560 if (!overScrollHorizontal) {
13561 maxOverScrollX = 0;
13562 }
13563
13564 int newScrollY = scrollY + deltaY;
13565 if (!overScrollVertical) {
13566 maxOverScrollY = 0;
13567 }
13568
13569 // Clamp values if at the limits and record
13570 final int left = -maxOverScrollX;
13571 final int right = maxOverScrollX + scrollRangeX;
13572 final int top = -maxOverScrollY;
13573 final int bottom = maxOverScrollY + scrollRangeY;
13574
13575 boolean clampedX = false;
13576 if (newScrollX > right) {
13577 newScrollX = right;
13578 clampedX = true;
13579 } else if (newScrollX < left) {
13580 newScrollX = left;
13581 clampedX = true;
13582 }
13583
13584 boolean clampedY = false;
13585 if (newScrollY > bottom) {
13586 newScrollY = bottom;
13587 clampedY = true;
13588 } else if (newScrollY < top) {
13589 newScrollY = top;
13590 clampedY = true;
13591 }
13592
13593 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13594
13595 return clampedX || clampedY;
13596 }
13597
13598 /**
13599 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13600 * respond to the results of an over-scroll operation.
13601 *
13602 * @param scrollX New X scroll value in pixels
13603 * @param scrollY New Y scroll value in pixels
13604 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13605 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13606 */
13607 protected void onOverScrolled(int scrollX, int scrollY,
13608 boolean clampedX, boolean clampedY) {
13609 // Intentionally empty.
13610 }
13611
13612 /**
13613 * Returns the over-scroll mode for this view. The result will be
13614 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13615 * (allow over-scrolling only if the view content is larger than the container),
13616 * or {@link #OVER_SCROLL_NEVER}.
13617 *
13618 * @return This view's over-scroll mode.
13619 */
13620 public int getOverScrollMode() {
13621 return mOverScrollMode;
13622 }
13623
13624 /**
13625 * Set the over-scroll mode for this view. Valid over-scroll modes are
13626 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13627 * (allow over-scrolling only if the view content is larger than the container),
13628 * or {@link #OVER_SCROLL_NEVER}.
13629 *
13630 * Setting the over-scroll mode of a view will have an effect only if the
13631 * view is capable of scrolling.
13632 *
13633 * @param overScrollMode The new over-scroll mode for this view.
13634 */
13635 public void setOverScrollMode(int overScrollMode) {
13636 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13637 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13638 overScrollMode != OVER_SCROLL_NEVER) {
13639 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13640 }
13641 mOverScrollMode = overScrollMode;
13642 }
13643
13644 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013645 * Gets a scale factor that determines the distance the view should scroll
13646 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13647 * @return The vertical scroll scale factor.
13648 * @hide
13649 */
13650 protected float getVerticalScrollFactor() {
13651 if (mVerticalScrollFactor == 0) {
13652 TypedValue outValue = new TypedValue();
13653 if (!mContext.getTheme().resolveAttribute(
13654 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13655 throw new IllegalStateException(
13656 "Expected theme to define listPreferredItemHeight.");
13657 }
13658 mVerticalScrollFactor = outValue.getDimension(
13659 mContext.getResources().getDisplayMetrics());
13660 }
13661 return mVerticalScrollFactor;
13662 }
13663
13664 /**
13665 * Gets a scale factor that determines the distance the view should scroll
13666 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13667 * @return The horizontal scroll scale factor.
13668 * @hide
13669 */
13670 protected float getHorizontalScrollFactor() {
13671 // TODO: Should use something else.
13672 return getVerticalScrollFactor();
13673 }
13674
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013675 /**
13676 * Return the value specifying the text direction or policy that was set with
13677 * {@link #setTextDirection(int)}.
13678 *
13679 * @return the defined text direction. It can be one of:
13680 *
13681 * {@link #TEXT_DIRECTION_INHERIT},
13682 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13683 * {@link #TEXT_DIRECTION_ANY_RTL},
13684 * {@link #TEXT_DIRECTION_LTR},
13685 * {@link #TEXT_DIRECTION_RTL},
13686 *
13687 * @hide
13688 */
13689 public int getTextDirection() {
13690 return mTextDirection;
13691 }
13692
13693 /**
13694 * Set the text direction.
13695 *
13696 * @param textDirection the direction to set. Should be one of:
13697 *
13698 * {@link #TEXT_DIRECTION_INHERIT},
13699 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13700 * {@link #TEXT_DIRECTION_ANY_RTL},
13701 * {@link #TEXT_DIRECTION_LTR},
13702 * {@link #TEXT_DIRECTION_RTL},
13703 *
13704 * @hide
13705 */
13706 public void setTextDirection(int textDirection) {
13707 if (textDirection != mTextDirection) {
13708 mTextDirection = textDirection;
13709 resetResolvedTextDirection();
13710 requestLayout();
13711 }
13712 }
13713
13714 /**
13715 * Return the resolved text direction.
13716 *
13717 * @return the resolved text direction. Return one of:
13718 *
Doug Feltcb3791202011-07-07 11:57:48 -070013719 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13720 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013721 * {@link #TEXT_DIRECTION_LTR},
13722 * {@link #TEXT_DIRECTION_RTL},
13723 *
13724 * @hide
13725 */
13726 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013727 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013728 resolveTextDirection();
13729 }
13730 return mResolvedTextDirection;
13731 }
13732
13733 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013734 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013735 *
13736 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013737 */
13738 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013739 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13740 mResolvedTextDirection = mTextDirection;
13741 return;
13742 }
13743 if (mParent != null && mParent instanceof ViewGroup) {
13744 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13745 return;
13746 }
13747 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013748 }
13749
13750 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013751 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013752 *
13753 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013754 */
13755 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013756 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013757 }
13758
Chet Haaseb39f0512011-05-24 14:36:40 -070013759 //
13760 // Properties
13761 //
13762 /**
13763 * A Property wrapper around the <code>alpha</code> functionality handled by the
13764 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13765 */
Romain Guy02ccac62011-06-24 13:20:23 -070013766 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013767 @Override
13768 public void setValue(View object, float value) {
13769 object.setAlpha(value);
13770 }
13771
13772 @Override
13773 public Float get(View object) {
13774 return object.getAlpha();
13775 }
13776 };
13777
13778 /**
13779 * A Property wrapper around the <code>translationX</code> functionality handled by the
13780 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13781 */
13782 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13783 @Override
13784 public void setValue(View object, float value) {
13785 object.setTranslationX(value);
13786 }
13787
13788 @Override
13789 public Float get(View object) {
13790 return object.getTranslationX();
13791 }
13792 };
13793
13794 /**
13795 * A Property wrapper around the <code>translationY</code> functionality handled by the
13796 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13797 */
13798 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13799 @Override
13800 public void setValue(View object, float value) {
13801 object.setTranslationY(value);
13802 }
13803
13804 @Override
13805 public Float get(View object) {
13806 return object.getTranslationY();
13807 }
13808 };
13809
13810 /**
13811 * A Property wrapper around the <code>x</code> functionality handled by the
13812 * {@link View#setX(float)} and {@link View#getX()} methods.
13813 */
13814 public static Property<View, Float> X = new FloatProperty<View>("x") {
13815 @Override
13816 public void setValue(View object, float value) {
13817 object.setX(value);
13818 }
13819
13820 @Override
13821 public Float get(View object) {
13822 return object.getX();
13823 }
13824 };
13825
13826 /**
13827 * A Property wrapper around the <code>y</code> functionality handled by the
13828 * {@link View#setY(float)} and {@link View#getY()} methods.
13829 */
13830 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13831 @Override
13832 public void setValue(View object, float value) {
13833 object.setY(value);
13834 }
13835
13836 @Override
13837 public Float get(View object) {
13838 return object.getY();
13839 }
13840 };
13841
13842 /**
13843 * A Property wrapper around the <code>rotation</code> functionality handled by the
13844 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13845 */
13846 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13847 @Override
13848 public void setValue(View object, float value) {
13849 object.setRotation(value);
13850 }
13851
13852 @Override
13853 public Float get(View object) {
13854 return object.getRotation();
13855 }
13856 };
13857
13858 /**
13859 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13860 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13861 */
13862 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13863 @Override
13864 public void setValue(View object, float value) {
13865 object.setRotationX(value);
13866 }
13867
13868 @Override
13869 public Float get(View object) {
13870 return object.getRotationX();
13871 }
13872 };
13873
13874 /**
13875 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13876 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13877 */
13878 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13879 @Override
13880 public void setValue(View object, float value) {
13881 object.setRotationY(value);
13882 }
13883
13884 @Override
13885 public Float get(View object) {
13886 return object.getRotationY();
13887 }
13888 };
13889
13890 /**
13891 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13892 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13893 */
13894 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13895 @Override
13896 public void setValue(View object, float value) {
13897 object.setScaleX(value);
13898 }
13899
13900 @Override
13901 public Float get(View object) {
13902 return object.getScaleX();
13903 }
13904 };
13905
13906 /**
13907 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13908 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13909 */
13910 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13911 @Override
13912 public void setValue(View object, float value) {
13913 object.setScaleY(value);
13914 }
13915
13916 @Override
13917 public Float get(View object) {
13918 return object.getScaleY();
13919 }
13920 };
13921
Jeff Brown33bbfd22011-02-24 20:55:35 -080013922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13924 * Each MeasureSpec represents a requirement for either the width or the height.
13925 * A MeasureSpec is comprised of a size and a mode. There are three possible
13926 * modes:
13927 * <dl>
13928 * <dt>UNSPECIFIED</dt>
13929 * <dd>
13930 * The parent has not imposed any constraint on the child. It can be whatever size
13931 * it wants.
13932 * </dd>
13933 *
13934 * <dt>EXACTLY</dt>
13935 * <dd>
13936 * The parent has determined an exact size for the child. The child is going to be
13937 * given those bounds regardless of how big it wants to be.
13938 * </dd>
13939 *
13940 * <dt>AT_MOST</dt>
13941 * <dd>
13942 * The child can be as large as it wants up to the specified size.
13943 * </dd>
13944 * </dl>
13945 *
13946 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13947 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13948 */
13949 public static class MeasureSpec {
13950 private static final int MODE_SHIFT = 30;
13951 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13952
13953 /**
13954 * Measure specification mode: The parent has not imposed any constraint
13955 * on the child. It can be whatever size it wants.
13956 */
13957 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13958
13959 /**
13960 * Measure specification mode: The parent has determined an exact size
13961 * for the child. The child is going to be given those bounds regardless
13962 * of how big it wants to be.
13963 */
13964 public static final int EXACTLY = 1 << MODE_SHIFT;
13965
13966 /**
13967 * Measure specification mode: The child can be as large as it wants up
13968 * to the specified size.
13969 */
13970 public static final int AT_MOST = 2 << MODE_SHIFT;
13971
13972 /**
13973 * Creates a measure specification based on the supplied size and mode.
13974 *
13975 * The mode must always be one of the following:
13976 * <ul>
13977 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13978 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13979 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13980 * </ul>
13981 *
13982 * @param size the size of the measure specification
13983 * @param mode the mode of the measure specification
13984 * @return the measure specification based on size and mode
13985 */
13986 public static int makeMeasureSpec(int size, int mode) {
13987 return size + mode;
13988 }
13989
13990 /**
13991 * Extracts the mode from the supplied measure specification.
13992 *
13993 * @param measureSpec the measure specification to extract the mode from
13994 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13995 * {@link android.view.View.MeasureSpec#AT_MOST} or
13996 * {@link android.view.View.MeasureSpec#EXACTLY}
13997 */
13998 public static int getMode(int measureSpec) {
13999 return (measureSpec & MODE_MASK);
14000 }
14001
14002 /**
14003 * Extracts the size from the supplied measure specification.
14004 *
14005 * @param measureSpec the measure specification to extract the size from
14006 * @return the size in pixels defined in the supplied measure specification
14007 */
14008 public static int getSize(int measureSpec) {
14009 return (measureSpec & ~MODE_MASK);
14010 }
14011
14012 /**
14013 * Returns a String representation of the specified measure
14014 * specification.
14015 *
14016 * @param measureSpec the measure specification to convert to a String
14017 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14018 */
14019 public static String toString(int measureSpec) {
14020 int mode = getMode(measureSpec);
14021 int size = getSize(measureSpec);
14022
14023 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14024
14025 if (mode == UNSPECIFIED)
14026 sb.append("UNSPECIFIED ");
14027 else if (mode == EXACTLY)
14028 sb.append("EXACTLY ");
14029 else if (mode == AT_MOST)
14030 sb.append("AT_MOST ");
14031 else
14032 sb.append(mode).append(" ");
14033
14034 sb.append(size);
14035 return sb.toString();
14036 }
14037 }
14038
14039 class CheckForLongPress implements Runnable {
14040
14041 private int mOriginalWindowAttachCount;
14042
14043 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014044 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014045 && mOriginalWindowAttachCount == mWindowAttachCount) {
14046 if (performLongClick()) {
14047 mHasPerformedLongPress = true;
14048 }
14049 }
14050 }
14051
14052 public void rememberWindowAttachCount() {
14053 mOriginalWindowAttachCount = mWindowAttachCount;
14054 }
14055 }
Joe Malin32736f02011-01-19 16:14:20 -080014056
Adam Powelle14579b2009-12-16 18:39:52 -080014057 private final class CheckForTap implements Runnable {
14058 public void run() {
14059 mPrivateFlags &= ~PREPRESSED;
14060 mPrivateFlags |= PRESSED;
14061 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014062 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014063 }
14064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014065
Adam Powella35d7682010-03-12 14:48:13 -080014066 private final class PerformClick implements Runnable {
14067 public void run() {
14068 performClick();
14069 }
14070 }
14071
Dianne Hackborn63042d62011-01-26 18:56:29 -080014072 /** @hide */
14073 public void hackTurnOffWindowResizeAnim(boolean off) {
14074 mAttachInfo.mTurnOffWindowResizeAnim = off;
14075 }
Joe Malin32736f02011-01-19 16:14:20 -080014076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014078 * This method returns a ViewPropertyAnimator object, which can be used to animate
14079 * specific properties on this View.
14080 *
14081 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14082 */
14083 public ViewPropertyAnimator animate() {
14084 if (mAnimator == null) {
14085 mAnimator = new ViewPropertyAnimator(this);
14086 }
14087 return mAnimator;
14088 }
14089
14090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091 * Interface definition for a callback to be invoked when a key event is
14092 * dispatched to this view. The callback will be invoked before the key
14093 * event is given to the view.
14094 */
14095 public interface OnKeyListener {
14096 /**
14097 * Called when a key is dispatched to a view. This allows listeners to
14098 * get a chance to respond before the target view.
14099 *
14100 * @param v The view the key has been dispatched to.
14101 * @param keyCode The code for the physical key that was pressed
14102 * @param event The KeyEvent object containing full information about
14103 * the event.
14104 * @return True if the listener has consumed the event, false otherwise.
14105 */
14106 boolean onKey(View v, int keyCode, KeyEvent event);
14107 }
14108
14109 /**
14110 * Interface definition for a callback to be invoked when a touch event is
14111 * dispatched to this view. The callback will be invoked before the touch
14112 * event is given to the view.
14113 */
14114 public interface OnTouchListener {
14115 /**
14116 * Called when a touch event is dispatched to a view. This allows listeners to
14117 * get a chance to respond before the target view.
14118 *
14119 * @param v The view the touch event has been dispatched to.
14120 * @param event The MotionEvent object containing full information about
14121 * the event.
14122 * @return True if the listener has consumed the event, false otherwise.
14123 */
14124 boolean onTouch(View v, MotionEvent event);
14125 }
14126
14127 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014128 * Interface definition for a callback to be invoked when a hover event is
14129 * dispatched to this view. The callback will be invoked before the hover
14130 * event is given to the view.
14131 */
14132 public interface OnHoverListener {
14133 /**
14134 * Called when a hover event is dispatched to a view. This allows listeners to
14135 * get a chance to respond before the target view.
14136 *
14137 * @param v The view the hover event has been dispatched to.
14138 * @param event The MotionEvent object containing full information about
14139 * the event.
14140 * @return True if the listener has consumed the event, false otherwise.
14141 */
14142 boolean onHover(View v, MotionEvent event);
14143 }
14144
14145 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014146 * Interface definition for a callback to be invoked when a generic motion event is
14147 * dispatched to this view. The callback will be invoked before the generic motion
14148 * event is given to the view.
14149 */
14150 public interface OnGenericMotionListener {
14151 /**
14152 * Called when a generic motion event is dispatched to a view. This allows listeners to
14153 * get a chance to respond before the target view.
14154 *
14155 * @param v The view the generic motion event has been dispatched to.
14156 * @param event The MotionEvent object containing full information about
14157 * the event.
14158 * @return True if the listener has consumed the event, false otherwise.
14159 */
14160 boolean onGenericMotion(View v, MotionEvent event);
14161 }
14162
14163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014164 * Interface definition for a callback to be invoked when a view has been clicked and held.
14165 */
14166 public interface OnLongClickListener {
14167 /**
14168 * Called when a view has been clicked and held.
14169 *
14170 * @param v The view that was clicked and held.
14171 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014172 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014173 */
14174 boolean onLongClick(View v);
14175 }
14176
14177 /**
Chris Tate32affef2010-10-18 15:29:21 -070014178 * Interface definition for a callback to be invoked when a drag is being dispatched
14179 * to this view. The callback will be invoked before the hosting view's own
14180 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14181 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014182 *
14183 * <div class="special reference">
14184 * <h3>Developer Guides</h3>
14185 * <p>For a guide to implementing drag and drop features, read the
14186 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14187 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014188 */
14189 public interface OnDragListener {
14190 /**
14191 * Called when a drag event is dispatched to a view. This allows listeners
14192 * to get a chance to override base View behavior.
14193 *
Joe Malin32736f02011-01-19 16:14:20 -080014194 * @param v The View that received the drag event.
14195 * @param event The {@link android.view.DragEvent} object for the drag event.
14196 * @return {@code true} if the drag event was handled successfully, or {@code false}
14197 * if the drag event was not handled. Note that {@code false} will trigger the View
14198 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014199 */
14200 boolean onDrag(View v, DragEvent event);
14201 }
14202
14203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014204 * Interface definition for a callback to be invoked when the focus state of
14205 * a view changed.
14206 */
14207 public interface OnFocusChangeListener {
14208 /**
14209 * Called when the focus state of a view has changed.
14210 *
14211 * @param v The view whose state has changed.
14212 * @param hasFocus The new focus state of v.
14213 */
14214 void onFocusChange(View v, boolean hasFocus);
14215 }
14216
14217 /**
14218 * Interface definition for a callback to be invoked when a view is clicked.
14219 */
14220 public interface OnClickListener {
14221 /**
14222 * Called when a view has been clicked.
14223 *
14224 * @param v The view that was clicked.
14225 */
14226 void onClick(View v);
14227 }
14228
14229 /**
14230 * Interface definition for a callback to be invoked when the context menu
14231 * for this view is being built.
14232 */
14233 public interface OnCreateContextMenuListener {
14234 /**
14235 * Called when the context menu for this view is being built. It is not
14236 * safe to hold onto the menu after this method returns.
14237 *
14238 * @param menu The context menu that is being built
14239 * @param v The view for which the context menu is being built
14240 * @param menuInfo Extra information about the item for which the
14241 * context menu should be shown. This information will vary
14242 * depending on the class of v.
14243 */
14244 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14245 }
14246
Joe Onorato664644d2011-01-23 17:53:23 -080014247 /**
14248 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014249 * visibility. This reports <strong>global</strong> changes to the system UI
14250 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014251 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014252 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014253 */
14254 public interface OnSystemUiVisibilityChangeListener {
14255 /**
14256 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014257 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014258 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014259 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014260 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14261 * <strong>global</strong> state of the UI visibility flags, not what your
14262 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014263 */
14264 public void onSystemUiVisibilityChange(int visibility);
14265 }
14266
Adam Powell4afd62b2011-02-18 15:02:18 -080014267 /**
14268 * Interface definition for a callback to be invoked when this view is attached
14269 * or detached from its window.
14270 */
14271 public interface OnAttachStateChangeListener {
14272 /**
14273 * Called when the view is attached to a window.
14274 * @param v The view that was attached
14275 */
14276 public void onViewAttachedToWindow(View v);
14277 /**
14278 * Called when the view is detached from a window.
14279 * @param v The view that was detached
14280 */
14281 public void onViewDetachedFromWindow(View v);
14282 }
14283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014284 private final class UnsetPressedState implements Runnable {
14285 public void run() {
14286 setPressed(false);
14287 }
14288 }
14289
14290 /**
14291 * Base class for derived classes that want to save and restore their own
14292 * state in {@link android.view.View#onSaveInstanceState()}.
14293 */
14294 public static class BaseSavedState extends AbsSavedState {
14295 /**
14296 * Constructor used when reading from a parcel. Reads the state of the superclass.
14297 *
14298 * @param source
14299 */
14300 public BaseSavedState(Parcel source) {
14301 super(source);
14302 }
14303
14304 /**
14305 * Constructor called by derived classes when creating their SavedState objects
14306 *
14307 * @param superState The state of the superclass of this view
14308 */
14309 public BaseSavedState(Parcelable superState) {
14310 super(superState);
14311 }
14312
14313 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14314 new Parcelable.Creator<BaseSavedState>() {
14315 public BaseSavedState createFromParcel(Parcel in) {
14316 return new BaseSavedState(in);
14317 }
14318
14319 public BaseSavedState[] newArray(int size) {
14320 return new BaseSavedState[size];
14321 }
14322 };
14323 }
14324
14325 /**
14326 * A set of information given to a view when it is attached to its parent
14327 * window.
14328 */
14329 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014330 interface Callbacks {
14331 void playSoundEffect(int effectId);
14332 boolean performHapticFeedback(int effectId, boolean always);
14333 }
14334
14335 /**
14336 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14337 * to a Handler. This class contains the target (View) to invalidate and
14338 * the coordinates of the dirty rectangle.
14339 *
14340 * For performance purposes, this class also implements a pool of up to
14341 * POOL_LIMIT objects that get reused. This reduces memory allocations
14342 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 */
Romain Guyd928d682009-03-31 17:52:16 -070014344 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014345 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014346 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14347 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014348 public InvalidateInfo newInstance() {
14349 return new InvalidateInfo();
14350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014351
Romain Guyd928d682009-03-31 17:52:16 -070014352 public void onAcquired(InvalidateInfo element) {
14353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014354
Romain Guyd928d682009-03-31 17:52:16 -070014355 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014356 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014357 }
14358 }, POOL_LIMIT)
14359 );
14360
14361 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014362 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014363
14364 View target;
14365
14366 int left;
14367 int top;
14368 int right;
14369 int bottom;
14370
Romain Guyd928d682009-03-31 17:52:16 -070014371 public void setNextPoolable(InvalidateInfo element) {
14372 mNext = element;
14373 }
14374
14375 public InvalidateInfo getNextPoolable() {
14376 return mNext;
14377 }
14378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014380 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014381 }
14382
14383 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014384 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014385 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014386
14387 public boolean isPooled() {
14388 return mIsPooled;
14389 }
14390
14391 public void setPooled(boolean isPooled) {
14392 mIsPooled = isPooled;
14393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014394 }
14395
14396 final IWindowSession mSession;
14397
14398 final IWindow mWindow;
14399
14400 final IBinder mWindowToken;
14401
14402 final Callbacks mRootCallbacks;
14403
Romain Guy59a12ca2011-06-09 17:48:21 -070014404 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014406 /**
14407 * The top view of the hierarchy.
14408 */
14409 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 IBinder mPanelParentWindowToken;
14412 Surface mSurface;
14413
Romain Guyb051e892010-09-28 19:09:36 -070014414 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014415 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014416 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014418 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014419 * Scale factor used by the compatibility mode
14420 */
14421 float mApplicationScale;
14422
14423 /**
14424 * Indicates whether the application is in compatibility mode
14425 */
14426 boolean mScalingRequired;
14427
14428 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014429 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014430 */
14431 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014432
Dianne Hackborn63042d62011-01-26 18:56:29 -080014433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014434 * Left position of this view's window
14435 */
14436 int mWindowLeft;
14437
14438 /**
14439 * Top position of this view's window
14440 */
14441 int mWindowTop;
14442
14443 /**
Adam Powell26153a32010-11-08 15:22:27 -080014444 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014445 */
Adam Powell26153a32010-11-08 15:22:27 -080014446 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014447
14448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014449 * For windows that are full-screen but using insets to layout inside
14450 * of the screen decorations, these are the current insets for the
14451 * content of the window.
14452 */
14453 final Rect mContentInsets = new Rect();
14454
14455 /**
14456 * For windows that are full-screen but using insets to layout inside
14457 * of the screen decorations, these are the current insets for the
14458 * actual visible parts of the window.
14459 */
14460 final Rect mVisibleInsets = new Rect();
14461
14462 /**
14463 * The internal insets given by this window. This value is
14464 * supplied by the client (through
14465 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14466 * be given to the window manager when changed to be used in laying
14467 * out windows behind it.
14468 */
14469 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14470 = new ViewTreeObserver.InternalInsetsInfo();
14471
14472 /**
14473 * All views in the window's hierarchy that serve as scroll containers,
14474 * used to determine if the window can be resized or must be panned
14475 * to adjust for a soft input area.
14476 */
14477 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14478
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014479 final KeyEvent.DispatcherState mKeyDispatchState
14480 = new KeyEvent.DispatcherState();
14481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014482 /**
14483 * Indicates whether the view's window currently has the focus.
14484 */
14485 boolean mHasWindowFocus;
14486
14487 /**
14488 * The current visibility of the window.
14489 */
14490 int mWindowVisibility;
14491
14492 /**
14493 * Indicates the time at which drawing started to occur.
14494 */
14495 long mDrawingTime;
14496
14497 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014498 * Indicates whether or not ignoring the DIRTY_MASK flags.
14499 */
14500 boolean mIgnoreDirtyState;
14501
14502 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014503 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14504 * to avoid clearing that flag prematurely.
14505 */
14506 boolean mSetIgnoreDirtyState = false;
14507
14508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014509 * Indicates whether the view's window is currently in touch mode.
14510 */
14511 boolean mInTouchMode;
14512
14513 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014514 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014515 * the next time it performs a traversal
14516 */
14517 boolean mRecomputeGlobalAttributes;
14518
14519 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014520 * Always report new attributes at next traversal.
14521 */
14522 boolean mForceReportNewAttributes;
14523
14524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014525 * Set during a traveral if any views want to keep the screen on.
14526 */
14527 boolean mKeepScreenOn;
14528
14529 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014530 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14531 */
14532 int mSystemUiVisibility;
14533
14534 /**
14535 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14536 * attached.
14537 */
14538 boolean mHasSystemUiListeners;
14539
14540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014541 * Set if the visibility of any views has changed.
14542 */
14543 boolean mViewVisibilityChanged;
14544
14545 /**
14546 * Set to true if a view has been scrolled.
14547 */
14548 boolean mViewScrollChanged;
14549
14550 /**
14551 * Global to the view hierarchy used as a temporary for dealing with
14552 * x/y points in the transparent region computations.
14553 */
14554 final int[] mTransparentLocation = new int[2];
14555
14556 /**
14557 * Global to the view hierarchy used as a temporary for dealing with
14558 * x/y points in the ViewGroup.invalidateChild implementation.
14559 */
14560 final int[] mInvalidateChildLocation = new int[2];
14561
Chet Haasec3aa3612010-06-17 08:50:37 -070014562
14563 /**
14564 * Global to the view hierarchy used as a temporary for dealing with
14565 * x/y location when view is transformed.
14566 */
14567 final float[] mTmpTransformLocation = new float[2];
14568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 /**
14570 * The view tree observer used to dispatch global events like
14571 * layout, pre-draw, touch mode change, etc.
14572 */
14573 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14574
14575 /**
14576 * A Canvas used by the view hierarchy to perform bitmap caching.
14577 */
14578 Canvas mCanvas;
14579
14580 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014581 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014582 * handler can be used to pump events in the UI events queue.
14583 */
14584 final Handler mHandler;
14585
14586 /**
14587 * Identifier for messages requesting the view to be invalidated.
14588 * Such messages should be sent to {@link #mHandler}.
14589 */
14590 static final int INVALIDATE_MSG = 0x1;
14591
14592 /**
14593 * Identifier for messages requesting the view to invalidate a region.
14594 * Such messages should be sent to {@link #mHandler}.
14595 */
14596 static final int INVALIDATE_RECT_MSG = 0x2;
14597
14598 /**
14599 * Temporary for use in computing invalidate rectangles while
14600 * calling up the hierarchy.
14601 */
14602 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014603
14604 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014605 * Temporary for use in computing hit areas with transformed views
14606 */
14607 final RectF mTmpTransformRect = new RectF();
14608
14609 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014610 * Temporary list for use in collecting focusable descendents of a view.
14611 */
14612 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014614 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014615 * The id of the window for accessibility purposes.
14616 */
14617 int mAccessibilityWindowId = View.NO_ID;
14618
14619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014620 * Creates a new set of attachment information with the specified
14621 * events handler and thread.
14622 *
14623 * @param handler the events handler the view must use
14624 */
14625 AttachInfo(IWindowSession session, IWindow window,
14626 Handler handler, Callbacks effectPlayer) {
14627 mSession = session;
14628 mWindow = window;
14629 mWindowToken = window.asBinder();
14630 mHandler = handler;
14631 mRootCallbacks = effectPlayer;
14632 }
14633 }
14634
14635 /**
14636 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14637 * is supported. This avoids keeping too many unused fields in most
14638 * instances of View.</p>
14639 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014640 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014641
Mike Cleronf116bf82009-09-27 19:14:12 -070014642 /**
14643 * Scrollbars are not visible
14644 */
14645 public static final int OFF = 0;
14646
14647 /**
14648 * Scrollbars are visible
14649 */
14650 public static final int ON = 1;
14651
14652 /**
14653 * Scrollbars are fading away
14654 */
14655 public static final int FADING = 2;
14656
14657 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014659 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014660 public int scrollBarDefaultDelayBeforeFade;
14661 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014662
14663 public int scrollBarSize;
14664 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014665 public float[] interpolatorValues;
14666 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014667
14668 public final Paint paint;
14669 public final Matrix matrix;
14670 public Shader shader;
14671
Mike Cleronf116bf82009-09-27 19:14:12 -070014672 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14673
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014674 private static final float[] OPAQUE = { 255 };
14675 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014676
Mike Cleronf116bf82009-09-27 19:14:12 -070014677 /**
14678 * When fading should start. This time moves into the future every time
14679 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14680 */
14681 public long fadeStartTime;
14682
14683
14684 /**
14685 * The current state of the scrollbars: ON, OFF, or FADING
14686 */
14687 public int state = OFF;
14688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014689 private int mLastColor;
14690
Mike Cleronf116bf82009-09-27 19:14:12 -070014691 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014692 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14693 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014694 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14695 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014696
14697 paint = new Paint();
14698 matrix = new Matrix();
14699 // use use a height of 1, and then wack the matrix each time we
14700 // actually use it.
14701 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014703 paint.setShader(shader);
14704 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014705 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014706 }
Romain Guy8506ab42009-06-11 17:35:47 -070014707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014708 public void setFadeColor(int color) {
14709 if (color != 0 && color != mLastColor) {
14710 mLastColor = color;
14711 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014712
Romain Guye55e1a72009-08-27 10:42:26 -070014713 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14714 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014716 paint.setShader(shader);
14717 // Restore the default transfer mode (src_over)
14718 paint.setXfermode(null);
14719 }
14720 }
Joe Malin32736f02011-01-19 16:14:20 -080014721
Mike Cleronf116bf82009-09-27 19:14:12 -070014722 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014723 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014724 if (now >= fadeStartTime) {
14725
14726 // the animation fades the scrollbars out by changing
14727 // the opacity (alpha) from fully opaque to fully
14728 // transparent
14729 int nextFrame = (int) now;
14730 int framesCount = 0;
14731
14732 Interpolator interpolator = scrollBarInterpolator;
14733
14734 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014735 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014736
14737 // End transparent
14738 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014739 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014740
14741 state = FADING;
14742
14743 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014744 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014745 }
14746 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014747 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014748
Svetoslav Ganova0156172011-06-26 17:55:44 -070014749 /**
14750 * Resuable callback for sending
14751 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14752 */
14753 private class SendViewScrolledAccessibilityEvent implements Runnable {
14754 public volatile boolean mIsPending;
14755
14756 public void run() {
14757 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14758 mIsPending = false;
14759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014760 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014761
14762 /**
14763 * <p>
14764 * This class represents a delegate that can be registered in a {@link View}
14765 * to enhance accessibility support via composition rather via inheritance.
14766 * It is specifically targeted to widget developers that extend basic View
14767 * classes i.e. classes in package android.view, that would like their
14768 * applications to be backwards compatible.
14769 * </p>
14770 * <p>
14771 * A scenario in which a developer would like to use an accessibility delegate
14772 * is overriding a method introduced in a later API version then the minimal API
14773 * version supported by the application. For example, the method
14774 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14775 * in API version 4 when the accessibility APIs were first introduced. If a
14776 * developer would like his application to run on API version 4 devices (assuming
14777 * all other APIs used by the application are version 4 or lower) and take advantage
14778 * of this method, instead of overriding the method which would break the application's
14779 * backwards compatibility, he can override the corresponding method in this
14780 * delegate and register the delegate in the target View if the API version of
14781 * the system is high enough i.e. the API version is same or higher to the API
14782 * version that introduced
14783 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14784 * </p>
14785 * <p>
14786 * Here is an example implementation:
14787 * </p>
14788 * <code><pre><p>
14789 * if (Build.VERSION.SDK_INT >= 14) {
14790 * // If the API version is equal of higher than the version in
14791 * // which onInitializeAccessibilityNodeInfo was introduced we
14792 * // register a delegate with a customized implementation.
14793 * View view = findViewById(R.id.view_id);
14794 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14795 * public void onInitializeAccessibilityNodeInfo(View host,
14796 * AccessibilityNodeInfo info) {
14797 * // Let the default implementation populate the info.
14798 * super.onInitializeAccessibilityNodeInfo(host, info);
14799 * // Set some other information.
14800 * info.setEnabled(host.isEnabled());
14801 * }
14802 * });
14803 * }
14804 * </code></pre></p>
14805 * <p>
14806 * This delegate contains methods that correspond to the accessibility methods
14807 * in View. If a delegate has been specified the implementation in View hands
14808 * off handling to the corresponding method in this delegate. The default
14809 * implementation the delegate methods behaves exactly as the corresponding
14810 * method in View for the case of no accessibility delegate been set. Hence,
14811 * to customize the behavior of a View method, clients can override only the
14812 * corresponding delegate method without altering the behavior of the rest
14813 * accessibility related methods of the host view.
14814 * </p>
14815 */
14816 public static class AccessibilityDelegate {
14817
14818 /**
14819 * Sends an accessibility event of the given type. If accessibility is not
14820 * enabled this method has no effect.
14821 * <p>
14822 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14823 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14824 * been set.
14825 * </p>
14826 *
14827 * @param host The View hosting the delegate.
14828 * @param eventType The type of the event to send.
14829 *
14830 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14831 */
14832 public void sendAccessibilityEvent(View host, int eventType) {
14833 host.sendAccessibilityEventInternal(eventType);
14834 }
14835
14836 /**
14837 * Sends an accessibility event. This method behaves exactly as
14838 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14839 * empty {@link AccessibilityEvent} and does not perform a check whether
14840 * accessibility is enabled.
14841 * <p>
14842 * The default implementation behaves as
14843 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14844 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14845 * the case of no accessibility delegate been set.
14846 * </p>
14847 *
14848 * @param host The View hosting the delegate.
14849 * @param event The event to send.
14850 *
14851 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14852 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14853 */
14854 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14855 host.sendAccessibilityEventUncheckedInternal(event);
14856 }
14857
14858 /**
14859 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14860 * to its children for adding their text content to the event.
14861 * <p>
14862 * The default implementation behaves as
14863 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14864 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14865 * the case of no accessibility delegate been set.
14866 * </p>
14867 *
14868 * @param host The View hosting the delegate.
14869 * @param event The event.
14870 * @return True if the event population was completed.
14871 *
14872 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14873 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14874 */
14875 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14876 return host.dispatchPopulateAccessibilityEventInternal(event);
14877 }
14878
14879 /**
14880 * Gives a chance to the host View to populate the accessibility event with its
14881 * text content.
14882 * <p>
14883 * The default implementation behaves as
14884 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14885 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14886 * the case of no accessibility delegate been set.
14887 * </p>
14888 *
14889 * @param host The View hosting the delegate.
14890 * @param event The accessibility event which to populate.
14891 *
14892 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14893 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14894 */
14895 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14896 host.onPopulateAccessibilityEventInternal(event);
14897 }
14898
14899 /**
14900 * Initializes an {@link AccessibilityEvent} with information about the
14901 * the host View which is the event source.
14902 * <p>
14903 * The default implementation behaves as
14904 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14905 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14906 * the case of no accessibility delegate been set.
14907 * </p>
14908 *
14909 * @param host The View hosting the delegate.
14910 * @param event The event to initialize.
14911 *
14912 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14913 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14914 */
14915 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14916 host.onInitializeAccessibilityEventInternal(event);
14917 }
14918
14919 /**
14920 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14921 * <p>
14922 * The default implementation behaves as
14923 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14924 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14925 * the case of no accessibility delegate been set.
14926 * </p>
14927 *
14928 * @param host The View hosting the delegate.
14929 * @param info The instance to initialize.
14930 *
14931 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14932 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14933 */
14934 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14935 host.onInitializeAccessibilityNodeInfoInternal(info);
14936 }
14937
14938 /**
14939 * Called when a child of the host View has requested sending an
14940 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14941 * to augment the event.
14942 * <p>
14943 * The default implementation behaves as
14944 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14945 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14946 * the case of no accessibility delegate been set.
14947 * </p>
14948 *
14949 * @param host The View hosting the delegate.
14950 * @param child The child which requests sending the event.
14951 * @param event The event to be sent.
14952 * @return True if the event should be sent
14953 *
14954 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14955 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14956 */
14957 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14958 AccessibilityEvent event) {
14959 return host.onRequestSendAccessibilityEventInternal(child, event);
14960 }
14961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014962}