blob: e0192aeda1a8fbdba9a61d38bf09aebb3f838459 [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
421 * intersects the the invalid region. Because the tree is traversed in-order,
422 * 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
1489 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT;
1490
1491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 * Temporary Rect currently for use in setBackground(). This will probably
1493 * be extended in the future to hold our own class with more than just
1494 * a Rect. :)
1495 */
1496 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001497
1498 /**
1499 * Map used to store views' tags.
1500 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001501 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001504 * The next available accessiiblity id.
1505 */
1506 private static int sNextAccessibilityViewId;
1507
1508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * The animation currently associated with this view.
1510 * @hide
1511 */
1512 protected Animation mCurrentAnimation = null;
1513
1514 /**
1515 * Width as measured during measure pass.
1516 * {@hide}
1517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001518 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001519 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
1521 /**
1522 * Height as measured during measure pass.
1523 * {@hide}
1524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001525 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001526 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001529 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1530 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1531 * its display list. This flag, used only when hw accelerated, allows us to clear the
1532 * flag while retaining this information until it's needed (at getDisplayList() time and
1533 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1534 *
1535 * {@hide}
1536 */
1537 boolean mRecreateDisplayList = false;
1538
1539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * The view's identifier.
1541 * {@hide}
1542 *
1543 * @see #setId(int)
1544 * @see #getId()
1545 */
1546 @ViewDebug.ExportedProperty(resolveId = true)
1547 int mID = NO_ID;
1548
1549 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001550 * The stable ID of this view for accessibility porposes.
1551 */
1552 int mAccessibilityViewId = NO_ID;
1553
1554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 * The view's tag.
1556 * {@hide}
1557 *
1558 * @see #setTag(Object)
1559 * @see #getTag()
1560 */
1561 protected Object mTag;
1562
1563 // for mPrivateFlags:
1564 /** {@hide} */
1565 static final int WANTS_FOCUS = 0x00000001;
1566 /** {@hide} */
1567 static final int FOCUSED = 0x00000002;
1568 /** {@hide} */
1569 static final int SELECTED = 0x00000004;
1570 /** {@hide} */
1571 static final int IS_ROOT_NAMESPACE = 0x00000008;
1572 /** {@hide} */
1573 static final int HAS_BOUNDS = 0x00000010;
1574 /** {@hide} */
1575 static final int DRAWN = 0x00000020;
1576 /**
1577 * When this flag is set, this view is running an animation on behalf of its
1578 * children and should therefore not cancel invalidate requests, even if they
1579 * lie outside of this view's bounds.
1580 *
1581 * {@hide}
1582 */
1583 static final int DRAW_ANIMATION = 0x00000040;
1584 /** {@hide} */
1585 static final int SKIP_DRAW = 0x00000080;
1586 /** {@hide} */
1587 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1588 /** {@hide} */
1589 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1590 /** {@hide} */
1591 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1592 /** {@hide} */
1593 static final int MEASURED_DIMENSION_SET = 0x00000800;
1594 /** {@hide} */
1595 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001596 /** {@hide} */
1597 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
1599 private static final int PRESSED = 0x00004000;
1600
1601 /** {@hide} */
1602 static final int DRAWING_CACHE_VALID = 0x00008000;
1603 /**
1604 * Flag used to indicate that this view should be drawn once more (and only once
1605 * more) after its animation has completed.
1606 * {@hide}
1607 */
1608 static final int ANIMATION_STARTED = 0x00010000;
1609
1610 private static final int SAVE_STATE_CALLED = 0x00020000;
1611
1612 /**
1613 * Indicates that the View returned true when onSetAlpha() was called and that
1614 * the alpha must be restored.
1615 * {@hide}
1616 */
1617 static final int ALPHA_SET = 0x00040000;
1618
1619 /**
1620 * Set by {@link #setScrollContainer(boolean)}.
1621 */
1622 static final int SCROLL_CONTAINER = 0x00080000;
1623
1624 /**
1625 * Set by {@link #setScrollContainer(boolean)}.
1626 */
1627 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1628
1629 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001630 * View flag indicating whether this view was invalidated (fully or partially.)
1631 *
1632 * @hide
1633 */
1634 static final int DIRTY = 0x00200000;
1635
1636 /**
1637 * View flag indicating whether this view was invalidated by an opaque
1638 * invalidate request.
1639 *
1640 * @hide
1641 */
1642 static final int DIRTY_OPAQUE = 0x00400000;
1643
1644 /**
1645 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1646 *
1647 * @hide
1648 */
1649 static final int DIRTY_MASK = 0x00600000;
1650
1651 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001652 * Indicates whether the background is opaque.
1653 *
1654 * @hide
1655 */
1656 static final int OPAQUE_BACKGROUND = 0x00800000;
1657
1658 /**
1659 * Indicates whether the scrollbars are opaque.
1660 *
1661 * @hide
1662 */
1663 static final int OPAQUE_SCROLLBARS = 0x01000000;
1664
1665 /**
1666 * Indicates whether the view is opaque.
1667 *
1668 * @hide
1669 */
1670 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001671
Adam Powelle14579b2009-12-16 18:39:52 -08001672 /**
1673 * Indicates a prepressed state;
1674 * the short time between ACTION_DOWN and recognizing
1675 * a 'real' press. Prepressed is used to recognize quick taps
1676 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001677 *
Adam Powelle14579b2009-12-16 18:39:52 -08001678 * @hide
1679 */
1680 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001681
Adam Powellc9fbaab2010-02-16 17:16:19 -08001682 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001683 * Indicates whether the view is temporarily detached.
1684 *
1685 * @hide
1686 */
1687 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001688
Adam Powell8568c3a2010-04-19 14:26:11 -07001689 /**
1690 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001691 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001692 * @hide
1693 */
1694 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001695
1696 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001697 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1698 * @hide
1699 */
1700 private static final int HOVERED = 0x10000000;
1701
1702 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001703 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1704 * for transform operations
1705 *
1706 * @hide
1707 */
Adam Powellf37df072010-09-17 16:22:49 -07001708 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001709
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001711 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001712
Chet Haasefd2b0022010-08-06 13:08:56 -07001713 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001714 * Indicates that this view was specifically invalidated, not just dirtied because some
1715 * child view was invalidated. The flag is used to determine when we need to recreate
1716 * a view's display list (as opposed to just returning a reference to its existing
1717 * display list).
1718 *
1719 * @hide
1720 */
1721 static final int INVALIDATED = 0x80000000;
1722
Christopher Tate3d4bf172011-03-28 16:16:46 -07001723 /* Masks for mPrivateFlags2 */
1724
1725 /**
1726 * Indicates that this view has reported that it can accept the current drag's content.
1727 * Cleared when the drag operation concludes.
1728 * @hide
1729 */
1730 static final int DRAG_CAN_ACCEPT = 0x00000001;
1731
1732 /**
1733 * Indicates that this view is currently directly under the drag location in a
1734 * drag-and-drop operation involving content that it can accept. Cleared when
1735 * the drag exits the view, or when the drag operation concludes.
1736 * @hide
1737 */
1738 static final int DRAG_HOVERED = 0x00000002;
1739
Cibu Johny86666632010-02-22 13:01:02 -08001740 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001741 * Indicates whether the view layout direction has been resolved and drawn to the
1742 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001743 *
1744 * @hide
1745 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001746 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1747
1748 /**
1749 * Indicates whether the view layout direction has been resolved.
1750 *
1751 * @hide
1752 */
1753 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1754
Cibu Johny86666632010-02-22 13:01:02 -08001755
Christopher Tate3d4bf172011-03-28 16:16:46 -07001756 /* End of masks for mPrivateFlags2 */
1757
1758 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1759
Chet Haasedaf98e92011-01-10 14:10:36 -08001760 /**
Adam Powell637d3372010-08-25 14:37:03 -07001761 * Always allow a user to over-scroll this view, provided it is a
1762 * view that can scroll.
1763 *
1764 * @see #getOverScrollMode()
1765 * @see #setOverScrollMode(int)
1766 */
1767 public static final int OVER_SCROLL_ALWAYS = 0;
1768
1769 /**
1770 * Allow a user to over-scroll this view only if the content is large
1771 * enough to meaningfully scroll, provided it is a view that can scroll.
1772 *
1773 * @see #getOverScrollMode()
1774 * @see #setOverScrollMode(int)
1775 */
1776 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1777
1778 /**
1779 * Never allow a user to over-scroll this view.
1780 *
1781 * @see #getOverScrollMode()
1782 * @see #setOverScrollMode(int)
1783 */
1784 public static final int OVER_SCROLL_NEVER = 2;
1785
1786 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001787 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001788 *
Joe Malin32736f02011-01-19 16:14:20 -08001789 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001790 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001791 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001792
1793 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001794 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1795 *
1796 * This is for use in games, book readers, video players, or any other "immersive" application
1797 * where the usual system chrome is deemed too distracting.
1798 *
1799 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001800 *
Joe Malin32736f02011-01-19 16:14:20 -08001801 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001802 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001803 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1804
1805 /**
1806 * View has requested that the system navigation be temporarily hidden.
1807 *
1808 * This is an even less obtrusive state than that called for by
1809 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1810 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1811 * those to disappear. This is useful (in conjunction with the
1812 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1813 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1814 * window flags) for displaying content using every last pixel on the display.
1815 *
1816 * There is a limitation: because navigation controls are so important, the least user
1817 * interaction will cause them to reappear immediately.
1818 *
1819 * @see #setSystemUiVisibility(int)
1820 */
1821 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1822
1823 /**
1824 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1825 */
1826 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1827
1828 /**
1829 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1830 */
1831 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001832
1833 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001834 * @hide
1835 *
1836 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1837 * out of the public fields to keep the undefined bits out of the developer's way.
1838 *
1839 * Flag to make the status bar not expandable. Unless you also
1840 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1841 */
1842 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1843
1844 /**
1845 * @hide
1846 *
1847 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1848 * out of the public fields to keep the undefined bits out of the developer's way.
1849 *
1850 * Flag to hide notification icons and scrolling ticker text.
1851 */
1852 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1853
1854 /**
1855 * @hide
1856 *
1857 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1858 * out of the public fields to keep the undefined bits out of the developer's way.
1859 *
1860 * Flag to disable incoming notification alerts. This will not block
1861 * icons, but it will block sound, vibrating and other visual or aural notifications.
1862 */
1863 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
1871 * Flag to hide only the scrolling ticker. Note that
1872 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1873 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1874 */
1875 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1876
1877 /**
1878 * @hide
1879 *
1880 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1881 * out of the public fields to keep the undefined bits out of the developer's way.
1882 *
1883 * Flag to hide the center system info area.
1884 */
1885 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1886
1887 /**
1888 * @hide
1889 *
1890 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1891 * out of the public fields to keep the undefined bits out of the developer's way.
1892 *
1893 * Flag to hide only the navigation buttons. Don't use this
1894 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001895 *
1896 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001897 */
1898 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1899
1900 /**
1901 * @hide
1902 *
1903 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1904 * out of the public fields to keep the undefined bits out of the developer's way.
1905 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001906 * Flag to hide only the back button. Don't use this
1907 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1908 */
1909 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1910
1911 /**
1912 * @hide
1913 *
1914 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1915 * out of the public fields to keep the undefined bits out of the developer's way.
1916 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001917 * Flag to hide only the clock. You might use this if your activity has
1918 * its own clock making the status bar's clock redundant.
1919 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001920 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1921
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001922 /**
1923 * @hide
1924 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001925 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001926
1927 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001928 * Find views that render the specified text.
1929 *
1930 * @see #findViewsWithText(ArrayList, CharSequence, int)
1931 */
1932 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1933
1934 /**
1935 * Find find views that contain the specified content description.
1936 *
1937 * @see #findViewsWithText(ArrayList, CharSequence, int)
1938 */
1939 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1940
1941 /**
Adam Powell637d3372010-08-25 14:37:03 -07001942 * Controls the over-scroll mode for this view.
1943 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1944 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1945 * and {@link #OVER_SCROLL_NEVER}.
1946 */
1947 private int mOverScrollMode;
1948
1949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 * The parent this view is attached to.
1951 * {@hide}
1952 *
1953 * @see #getParent()
1954 */
1955 protected ViewParent mParent;
1956
1957 /**
1958 * {@hide}
1959 */
1960 AttachInfo mAttachInfo;
1961
1962 /**
1963 * {@hide}
1964 */
Romain Guy809a7f62009-05-14 15:44:42 -07001965 @ViewDebug.ExportedProperty(flagMapping = {
1966 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1967 name = "FORCE_LAYOUT"),
1968 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1969 name = "LAYOUT_REQUIRED"),
1970 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001971 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001972 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1973 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1974 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1975 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1976 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001978 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979
1980 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001981 * This view's request for the visibility of the status bar.
1982 * @hide
1983 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001984 @ViewDebug.ExportedProperty(flagMapping = {
1985 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1986 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1987 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1988 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1989 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1990 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1991 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1992 equals = SYSTEM_UI_FLAG_VISIBLE,
1993 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1994 })
Joe Onorato664644d2011-01-23 17:53:23 -08001995 int mSystemUiVisibility;
1996
1997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 * Count of how many windows this view has been attached to.
1999 */
2000 int mWindowAttachCount;
2001
2002 /**
2003 * The layout parameters associated with this view and used by the parent
2004 * {@link android.view.ViewGroup} to determine how this view should be
2005 * laid out.
2006 * {@hide}
2007 */
2008 protected ViewGroup.LayoutParams mLayoutParams;
2009
2010 /**
2011 * The view flags hold various views states.
2012 * {@hide}
2013 */
2014 @ViewDebug.ExportedProperty
2015 int mViewFlags;
2016
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002017 static class TransformationInfo {
2018 /**
2019 * The transform matrix for the View. This transform is calculated internally
2020 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2021 * is used by default. Do *not* use this variable directly; instead call
2022 * getMatrix(), which will automatically recalculate the matrix if necessary
2023 * to get the correct matrix based on the latest rotation and scale properties.
2024 */
2025 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002026
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002027 /**
2028 * The transform matrix for the View. This transform is calculated internally
2029 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2030 * is used by default. Do *not* use this variable directly; instead call
2031 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2032 * to get the correct matrix based on the latest rotation and scale properties.
2033 */
2034 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002035
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002036 /**
2037 * An internal variable that tracks whether we need to recalculate the
2038 * transform matrix, based on whether the rotation or scaleX/Y properties
2039 * have changed since the matrix was last calculated.
2040 */
2041 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002042
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002043 /**
2044 * An internal variable that tracks whether we need to recalculate the
2045 * transform matrix, based on whether the rotation or scaleX/Y properties
2046 * have changed since the matrix was last calculated.
2047 */
2048 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002049
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002050 /**
2051 * A variable that tracks whether we need to recalculate the
2052 * transform matrix, based on whether the rotation or scaleX/Y properties
2053 * have changed since the matrix was last calculated. This variable
2054 * is only valid after a call to updateMatrix() or to a function that
2055 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2056 */
2057 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002058
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002059 /**
2060 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2061 */
2062 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002063
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002064 /**
2065 * This matrix is used when computing the matrix for 3D rotations.
2066 */
2067 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002068
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002069 /**
2070 * These prev values are used to recalculate a centered pivot point when necessary. The
2071 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2072 * set), so thes values are only used then as well.
2073 */
2074 private int mPrevWidth = -1;
2075 private int mPrevHeight = -1;
2076
2077 /**
2078 * The degrees rotation around the vertical axis through the pivot point.
2079 */
2080 @ViewDebug.ExportedProperty
2081 float mRotationY = 0f;
2082
2083 /**
2084 * The degrees rotation around the horizontal axis through the pivot point.
2085 */
2086 @ViewDebug.ExportedProperty
2087 float mRotationX = 0f;
2088
2089 /**
2090 * The degrees rotation around the pivot point.
2091 */
2092 @ViewDebug.ExportedProperty
2093 float mRotation = 0f;
2094
2095 /**
2096 * The amount of translation of the object away from its left property (post-layout).
2097 */
2098 @ViewDebug.ExportedProperty
2099 float mTranslationX = 0f;
2100
2101 /**
2102 * The amount of translation of the object away from its top property (post-layout).
2103 */
2104 @ViewDebug.ExportedProperty
2105 float mTranslationY = 0f;
2106
2107 /**
2108 * The amount of scale in the x direction around the pivot point. A
2109 * value of 1 means no scaling is applied.
2110 */
2111 @ViewDebug.ExportedProperty
2112 float mScaleX = 1f;
2113
2114 /**
2115 * The amount of scale in the y direction around the pivot point. A
2116 * value of 1 means no scaling is applied.
2117 */
2118 @ViewDebug.ExportedProperty
2119 float mScaleY = 1f;
2120
2121 /**
2122 * The amount of scale in the x direction around the pivot point. A
2123 * value of 1 means no scaling is applied.
2124 */
2125 @ViewDebug.ExportedProperty
2126 float mPivotX = 0f;
2127
2128 /**
2129 * The amount of scale in the y direction around the pivot point. A
2130 * value of 1 means no scaling is applied.
2131 */
2132 @ViewDebug.ExportedProperty
2133 float mPivotY = 0f;
2134
2135 /**
2136 * The opacity of the View. This is a value from 0 to 1, where 0 means
2137 * completely transparent and 1 means completely opaque.
2138 */
2139 @ViewDebug.ExportedProperty
2140 float mAlpha = 1f;
2141 }
2142
2143 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002144
Joe Malin32736f02011-01-19 16:14:20 -08002145 private boolean mLastIsOpaque;
2146
Chet Haasefd2b0022010-08-06 13:08:56 -07002147 /**
2148 * Convenience value to check for float values that are close enough to zero to be considered
2149 * zero.
2150 */
Romain Guy2542d192010-08-18 11:47:12 -07002151 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002152
2153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 * The distance in pixels from the left edge of this view's parent
2155 * to the left edge of this view.
2156 * {@hide}
2157 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002158 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 protected int mLeft;
2160 /**
2161 * The distance in pixels from the left edge of this view's parent
2162 * to the right edge of this view.
2163 * {@hide}
2164 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002165 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 protected int mRight;
2167 /**
2168 * The distance in pixels from the top edge of this view's parent
2169 * to the top edge of this view.
2170 * {@hide}
2171 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002172 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 protected int mTop;
2174 /**
2175 * The distance in pixels from the top edge of this view's parent
2176 * to the bottom edge of this view.
2177 * {@hide}
2178 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002179 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 protected int mBottom;
2181
2182 /**
2183 * The offset, in pixels, by which the content of this view is scrolled
2184 * horizontally.
2185 * {@hide}
2186 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002187 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 protected int mScrollX;
2189 /**
2190 * The offset, in pixels, by which the content of this view is scrolled
2191 * vertically.
2192 * {@hide}
2193 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002194 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 protected int mScrollY;
2196
2197 /**
2198 * The left padding in pixels, that is the distance in pixels between the
2199 * left edge of this view and the left edge of its content.
2200 * {@hide}
2201 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002202 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 protected int mPaddingLeft;
2204 /**
2205 * The right padding in pixels, that is the distance in pixels between the
2206 * right edge of this view and the right edge of its content.
2207 * {@hide}
2208 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002209 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 protected int mPaddingRight;
2211 /**
2212 * The top padding in pixels, that is the distance in pixels between the
2213 * top edge of this view and the top edge of its content.
2214 * {@hide}
2215 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002216 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 protected int mPaddingTop;
2218 /**
2219 * The bottom padding in pixels, that is the distance in pixels between the
2220 * bottom edge of this view and the bottom edge of its content.
2221 * {@hide}
2222 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002223 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 protected int mPaddingBottom;
2225
2226 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002227 * Briefly describes the view and is primarily used for accessibility support.
2228 */
2229 private CharSequence mContentDescription;
2230
2231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002233 *
2234 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002236 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002237 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238
2239 /**
2240 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002241 *
2242 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002244 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002245 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002247 /**
Adam Powell20232d02010-12-08 21:08:53 -08002248 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002249 *
2250 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002251 */
2252 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002253 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002254
2255 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002256 * Cache if the user padding is relative.
2257 *
2258 */
2259 @ViewDebug.ExportedProperty(category = "padding")
2260 boolean mUserPaddingRelative;
2261
2262 /**
2263 * Cache the paddingStart set by the user to append to the scrollbar's size.
2264 *
2265 */
2266 @ViewDebug.ExportedProperty(category = "padding")
2267 int mUserPaddingStart;
2268
2269 /**
2270 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2271 *
2272 */
2273 @ViewDebug.ExportedProperty(category = "padding")
2274 int mUserPaddingEnd;
2275
2276 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002277 * @hide
2278 */
2279 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2280 /**
2281 * @hide
2282 */
2283 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 private Drawable mBGDrawable;
2286
2287 private int mBackgroundResource;
2288 private boolean mBackgroundSizeChanged;
2289
2290 /**
2291 * Listener used to dispatch focus change events.
2292 * This field should be made private, so it is hidden from the SDK.
2293 * {@hide}
2294 */
2295 protected OnFocusChangeListener mOnFocusChangeListener;
2296
2297 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002298 * Listeners for layout change events.
2299 */
2300 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2301
2302 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002303 * Listeners for attach events.
2304 */
2305 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2306
2307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 * Listener used to dispatch click events.
2309 * This field should be made private, so it is hidden from the SDK.
2310 * {@hide}
2311 */
2312 protected OnClickListener mOnClickListener;
2313
2314 /**
2315 * Listener used to dispatch long click events.
2316 * This field should be made private, so it is hidden from the SDK.
2317 * {@hide}
2318 */
2319 protected OnLongClickListener mOnLongClickListener;
2320
2321 /**
2322 * Listener used to build the context menu.
2323 * This field should be made private, so it is hidden from the SDK.
2324 * {@hide}
2325 */
2326 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2327
2328 private OnKeyListener mOnKeyListener;
2329
2330 private OnTouchListener mOnTouchListener;
2331
Jeff Brown10b62902011-06-20 16:40:37 -07002332 private OnHoverListener mOnHoverListener;
2333
Jeff Brown33bbfd22011-02-24 20:55:35 -08002334 private OnGenericMotionListener mOnGenericMotionListener;
2335
Chris Tate32affef2010-10-18 15:29:21 -07002336 private OnDragListener mOnDragListener;
2337
Joe Onorato664644d2011-01-23 17:53:23 -08002338 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 /**
2341 * The application environment this view lives in.
2342 * This field should be made private, so it is hidden from the SDK.
2343 * {@hide}
2344 */
2345 protected Context mContext;
2346
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002347 private final Resources mResources;
2348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 private ScrollabilityCache mScrollCache;
2350
2351 private int[] mDrawableState = null;
2352
Romain Guy0211a0a2011-02-14 16:34:59 -08002353 /**
2354 * Set to true when drawing cache is enabled and cannot be created.
2355 *
2356 * @hide
2357 */
2358 public boolean mCachingFailed;
2359
Romain Guy02890fd2010-08-06 17:58:44 -07002360 private Bitmap mDrawingCache;
2361 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002362 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002363 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364
2365 /**
2366 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2367 * the user may specify which view to go to next.
2368 */
2369 private int mNextFocusLeftId = View.NO_ID;
2370
2371 /**
2372 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2373 * the user may specify which view to go to next.
2374 */
2375 private int mNextFocusRightId = View.NO_ID;
2376
2377 /**
2378 * When this view has focus and the next focus is {@link #FOCUS_UP},
2379 * the user may specify which view to go to next.
2380 */
2381 private int mNextFocusUpId = View.NO_ID;
2382
2383 /**
2384 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2385 * the user may specify which view to go to next.
2386 */
2387 private int mNextFocusDownId = View.NO_ID;
2388
Jeff Brown4e6319b2010-12-13 10:36:51 -08002389 /**
2390 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2391 * the user may specify which view to go to next.
2392 */
2393 int mNextFocusForwardId = View.NO_ID;
2394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002396 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002397 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002398 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 private UnsetPressedState mUnsetPressedState;
2401
2402 /**
2403 * Whether the long press's action has been invoked. The tap's action is invoked on the
2404 * up event while a long press is invoked as soon as the long press duration is reached, so
2405 * a long press could be performed before the tap is checked, in which case the tap's action
2406 * should not be invoked.
2407 */
2408 private boolean mHasPerformedLongPress;
2409
2410 /**
2411 * The minimum height of the view. We'll try our best to have the height
2412 * of this view to at least this amount.
2413 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002414 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 private int mMinHeight;
2416
2417 /**
2418 * The minimum width of the view. We'll try our best to have the width
2419 * of this view to at least this amount.
2420 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002421 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 private int mMinWidth;
2423
2424 /**
2425 * The delegate to handle touch events that are physically in this view
2426 * but should be handled by another view.
2427 */
2428 private TouchDelegate mTouchDelegate = null;
2429
2430 /**
2431 * Solid color to use as a background when creating the drawing cache. Enables
2432 * the cache to use 16 bit bitmaps instead of 32 bit.
2433 */
2434 private int mDrawingCacheBackgroundColor = 0;
2435
2436 /**
2437 * Special tree observer used when mAttachInfo is null.
2438 */
2439 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002440
Adam Powelle14579b2009-12-16 18:39:52 -08002441 /**
2442 * Cache the touch slop from the context that created the view.
2443 */
2444 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002447 * Object that handles automatic animation of view properties.
2448 */
2449 private ViewPropertyAnimator mAnimator = null;
2450
2451 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002452 * Flag indicating that a drag can cross window boundaries. When
2453 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2454 * with this flag set, all visible applications will be able to participate
2455 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002456 *
2457 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002458 */
2459 public static final int DRAG_FLAG_GLOBAL = 1;
2460
2461 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002462 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2463 */
2464 private float mVerticalScrollFactor;
2465
2466 /**
Adam Powell20232d02010-12-08 21:08:53 -08002467 * Position of the vertical scroll bar.
2468 */
2469 private int mVerticalScrollbarPosition;
2470
2471 /**
2472 * Position the scroll bar at the default position as determined by the system.
2473 */
2474 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2475
2476 /**
2477 * Position the scroll bar along the left edge.
2478 */
2479 public static final int SCROLLBAR_POSITION_LEFT = 1;
2480
2481 /**
2482 * Position the scroll bar along the right edge.
2483 */
2484 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2485
2486 /**
Romain Guy171c5922011-01-06 10:04:23 -08002487 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002488 *
2489 * @see #getLayerType()
2490 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002491 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002492 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002493 */
2494 public static final int LAYER_TYPE_NONE = 0;
2495
2496 /**
2497 * <p>Indicates that the view has a software layer. A software layer is backed
2498 * by a bitmap and causes the view to be rendered using Android's software
2499 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002500 *
Romain Guy171c5922011-01-06 10:04:23 -08002501 * <p>Software layers have various usages:</p>
2502 * <p>When the application is not using hardware acceleration, a software layer
2503 * is useful to apply a specific color filter and/or blending mode and/or
2504 * translucency to a view and all its children.</p>
2505 * <p>When the application is using hardware acceleration, a software layer
2506 * is useful to render drawing primitives not supported by the hardware
2507 * accelerated pipeline. It can also be used to cache a complex view tree
2508 * into a texture and reduce the complexity of drawing operations. For instance,
2509 * when animating a complex view tree with a translation, a software layer can
2510 * be used to render the view tree only once.</p>
2511 * <p>Software layers should be avoided when the affected view tree updates
2512 * often. Every update will require to re-render the software layer, which can
2513 * potentially be slow (particularly when hardware acceleration is turned on
2514 * since the layer will have to be uploaded into a hardware texture after every
2515 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002516 *
2517 * @see #getLayerType()
2518 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002519 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002520 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002521 */
2522 public static final int LAYER_TYPE_SOFTWARE = 1;
2523
2524 /**
2525 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2526 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2527 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2528 * rendering pipeline, but only if hardware acceleration is turned on for the
2529 * view hierarchy. When hardware acceleration is turned off, hardware layers
2530 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002531 *
Romain Guy171c5922011-01-06 10:04:23 -08002532 * <p>A hardware layer is useful to apply a specific color filter and/or
2533 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002534 * <p>A hardware layer can be used to cache a complex view tree into a
2535 * texture and reduce the complexity of drawing operations. For instance,
2536 * when animating a complex view tree with a translation, a hardware layer can
2537 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002538 * <p>A hardware layer can also be used to increase the rendering quality when
2539 * rotation transformations are applied on a view. It can also be used to
2540 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002541 *
2542 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002543 * @see #setLayerType(int, android.graphics.Paint)
2544 * @see #LAYER_TYPE_NONE
2545 * @see #LAYER_TYPE_SOFTWARE
2546 */
2547 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002548
Romain Guy3aaff3a2011-01-12 14:18:47 -08002549 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2550 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2551 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2552 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2553 })
Romain Guy171c5922011-01-06 10:04:23 -08002554 int mLayerType = LAYER_TYPE_NONE;
2555 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002556 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002557
2558 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002559 * Set to true when the view is sending hover accessibility events because it
2560 * is the innermost hovered view.
2561 */
2562 private boolean mSendingHoverAccessibilityEvents;
2563
2564 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002565 * Delegate for injecting accessiblity functionality.
2566 */
2567 AccessibilityDelegate mAccessibilityDelegate;
2568
2569 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002570 * Text direction is inherited thru {@link ViewGroup}
2571 * @hide
2572 */
2573 public static final int TEXT_DIRECTION_INHERIT = 0;
2574
2575 /**
2576 * Text direction is using "first strong algorithm". The first strong directional character
2577 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002578 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002579 *
2580 * @hide
2581 */
2582 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2583
2584 /**
2585 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2586 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002587 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002588 *
2589 * @hide
2590 */
2591 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2592
2593 /**
2594 * Text direction is forced to LTR.
2595 *
2596 * @hide
2597 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002598 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002599
2600 /**
2601 * Text direction is forced to RTL.
2602 *
2603 * @hide
2604 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002605 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002606
2607 /**
2608 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002609 *
2610 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002611 */
2612 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2613
2614 /**
2615 * The text direction that has been defined by {@link #setTextDirection(int)}.
2616 *
2617 * {@hide}
2618 */
2619 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002620 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2621 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2622 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2623 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2624 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2625 })
Doug Feltcb3791202011-07-07 11:57:48 -07002626 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002627
2628 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002629 * The resolved text direction. This needs resolution if the value is
2630 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2631 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2632 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002633 *
2634 * {@hide}
2635 */
2636 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002637 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2638 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2639 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002640 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2641 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2642 })
Doug Feltcb3791202011-07-07 11:57:48 -07002643 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002644
2645 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002646 * Consistency verifier for debugging purposes.
2647 * @hide
2648 */
2649 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2650 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2651 new InputEventConsistencyVerifier(this, 0) : null;
2652
2653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * Simple constructor to use when creating a view from code.
2655 *
2656 * @param context The Context the view is running in, through which it can
2657 * access the current theme, resources, etc.
2658 */
2659 public View(Context context) {
2660 mContext = context;
2661 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002662 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002663 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002664 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002665 mUserPaddingStart = -1;
2666 mUserPaddingEnd = -1;
2667 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 }
2669
2670 /**
2671 * Constructor that is called when inflating a view from XML. This is called
2672 * when a view is being constructed from an XML file, supplying attributes
2673 * that were specified in the XML file. This version uses a default style of
2674 * 0, so the only attribute values applied are those in the Context's Theme
2675 * and the given AttributeSet.
2676 *
2677 * <p>
2678 * The method onFinishInflate() will be called after all children have been
2679 * added.
2680 *
2681 * @param context The Context the view is running in, through which it can
2682 * access the current theme, resources, etc.
2683 * @param attrs The attributes of the XML tag that is inflating the view.
2684 * @see #View(Context, AttributeSet, int)
2685 */
2686 public View(Context context, AttributeSet attrs) {
2687 this(context, attrs, 0);
2688 }
2689
2690 /**
2691 * Perform inflation from XML and apply a class-specific base style. This
2692 * constructor of View allows subclasses to use their own base style when
2693 * they are inflating. For example, a Button class's constructor would call
2694 * this version of the super class constructor and supply
2695 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2696 * the theme's button style to modify all of the base view attributes (in
2697 * particular its background) as well as the Button class's attributes.
2698 *
2699 * @param context The Context the view is running in, through which it can
2700 * access the current theme, resources, etc.
2701 * @param attrs The attributes of the XML tag that is inflating the view.
2702 * @param defStyle The default style to apply to this view. If 0, no style
2703 * will be applied (beyond what is included in the theme). This may
2704 * either be an attribute resource, whose value will be retrieved
2705 * from the current theme, or an explicit style resource.
2706 * @see #View(Context, AttributeSet)
2707 */
2708 public View(Context context, AttributeSet attrs, int defStyle) {
2709 this(context);
2710
2711 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2712 defStyle, 0);
2713
2714 Drawable background = null;
2715
2716 int leftPadding = -1;
2717 int topPadding = -1;
2718 int rightPadding = -1;
2719 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002720 int startPadding = -1;
2721 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722
2723 int padding = -1;
2724
2725 int viewFlagValues = 0;
2726 int viewFlagMasks = 0;
2727
2728 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 int x = 0;
2731 int y = 0;
2732
Chet Haase73066682010-11-29 15:55:32 -08002733 float tx = 0;
2734 float ty = 0;
2735 float rotation = 0;
2736 float rotationX = 0;
2737 float rotationY = 0;
2738 float sx = 1f;
2739 float sy = 1f;
2740 boolean transformSet = false;
2741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2743
Adam Powell637d3372010-08-25 14:37:03 -07002744 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 final int N = a.getIndexCount();
2746 for (int i = 0; i < N; i++) {
2747 int attr = a.getIndex(i);
2748 switch (attr) {
2749 case com.android.internal.R.styleable.View_background:
2750 background = a.getDrawable(attr);
2751 break;
2752 case com.android.internal.R.styleable.View_padding:
2753 padding = a.getDimensionPixelSize(attr, -1);
2754 break;
2755 case com.android.internal.R.styleable.View_paddingLeft:
2756 leftPadding = a.getDimensionPixelSize(attr, -1);
2757 break;
2758 case com.android.internal.R.styleable.View_paddingTop:
2759 topPadding = a.getDimensionPixelSize(attr, -1);
2760 break;
2761 case com.android.internal.R.styleable.View_paddingRight:
2762 rightPadding = a.getDimensionPixelSize(attr, -1);
2763 break;
2764 case com.android.internal.R.styleable.View_paddingBottom:
2765 bottomPadding = a.getDimensionPixelSize(attr, -1);
2766 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002767 case com.android.internal.R.styleable.View_paddingStart:
2768 startPadding = a.getDimensionPixelSize(attr, -1);
2769 break;
2770 case com.android.internal.R.styleable.View_paddingEnd:
2771 endPadding = a.getDimensionPixelSize(attr, -1);
2772 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 case com.android.internal.R.styleable.View_scrollX:
2774 x = a.getDimensionPixelOffset(attr, 0);
2775 break;
2776 case com.android.internal.R.styleable.View_scrollY:
2777 y = a.getDimensionPixelOffset(attr, 0);
2778 break;
Chet Haase73066682010-11-29 15:55:32 -08002779 case com.android.internal.R.styleable.View_alpha:
2780 setAlpha(a.getFloat(attr, 1f));
2781 break;
2782 case com.android.internal.R.styleable.View_transformPivotX:
2783 setPivotX(a.getDimensionPixelOffset(attr, 0));
2784 break;
2785 case com.android.internal.R.styleable.View_transformPivotY:
2786 setPivotY(a.getDimensionPixelOffset(attr, 0));
2787 break;
2788 case com.android.internal.R.styleable.View_translationX:
2789 tx = a.getDimensionPixelOffset(attr, 0);
2790 transformSet = true;
2791 break;
2792 case com.android.internal.R.styleable.View_translationY:
2793 ty = a.getDimensionPixelOffset(attr, 0);
2794 transformSet = true;
2795 break;
2796 case com.android.internal.R.styleable.View_rotation:
2797 rotation = a.getFloat(attr, 0);
2798 transformSet = true;
2799 break;
2800 case com.android.internal.R.styleable.View_rotationX:
2801 rotationX = a.getFloat(attr, 0);
2802 transformSet = true;
2803 break;
2804 case com.android.internal.R.styleable.View_rotationY:
2805 rotationY = a.getFloat(attr, 0);
2806 transformSet = true;
2807 break;
2808 case com.android.internal.R.styleable.View_scaleX:
2809 sx = a.getFloat(attr, 1f);
2810 transformSet = true;
2811 break;
2812 case com.android.internal.R.styleable.View_scaleY:
2813 sy = a.getFloat(attr, 1f);
2814 transformSet = true;
2815 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 case com.android.internal.R.styleable.View_id:
2817 mID = a.getResourceId(attr, NO_ID);
2818 break;
2819 case com.android.internal.R.styleable.View_tag:
2820 mTag = a.getText(attr);
2821 break;
2822 case com.android.internal.R.styleable.View_fitsSystemWindows:
2823 if (a.getBoolean(attr, false)) {
2824 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2825 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2826 }
2827 break;
2828 case com.android.internal.R.styleable.View_focusable:
2829 if (a.getBoolean(attr, false)) {
2830 viewFlagValues |= FOCUSABLE;
2831 viewFlagMasks |= FOCUSABLE_MASK;
2832 }
2833 break;
2834 case com.android.internal.R.styleable.View_focusableInTouchMode:
2835 if (a.getBoolean(attr, false)) {
2836 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2837 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2838 }
2839 break;
2840 case com.android.internal.R.styleable.View_clickable:
2841 if (a.getBoolean(attr, false)) {
2842 viewFlagValues |= CLICKABLE;
2843 viewFlagMasks |= CLICKABLE;
2844 }
2845 break;
2846 case com.android.internal.R.styleable.View_longClickable:
2847 if (a.getBoolean(attr, false)) {
2848 viewFlagValues |= LONG_CLICKABLE;
2849 viewFlagMasks |= LONG_CLICKABLE;
2850 }
2851 break;
2852 case com.android.internal.R.styleable.View_saveEnabled:
2853 if (!a.getBoolean(attr, true)) {
2854 viewFlagValues |= SAVE_DISABLED;
2855 viewFlagMasks |= SAVE_DISABLED_MASK;
2856 }
2857 break;
2858 case com.android.internal.R.styleable.View_duplicateParentState:
2859 if (a.getBoolean(attr, false)) {
2860 viewFlagValues |= DUPLICATE_PARENT_STATE;
2861 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2862 }
2863 break;
2864 case com.android.internal.R.styleable.View_visibility:
2865 final int visibility = a.getInt(attr, 0);
2866 if (visibility != 0) {
2867 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2868 viewFlagMasks |= VISIBILITY_MASK;
2869 }
2870 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002871 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002872 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002873 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002874 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002875 final int layoutDirection = a.getInt(attr, -1);
2876 if (layoutDirection != -1) {
2877 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002878 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002879 // Set to default (LAYOUT_DIRECTION_INHERIT)
2880 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002881 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002882 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002883 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 case com.android.internal.R.styleable.View_drawingCacheQuality:
2885 final int cacheQuality = a.getInt(attr, 0);
2886 if (cacheQuality != 0) {
2887 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2888 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2889 }
2890 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002891 case com.android.internal.R.styleable.View_contentDescription:
2892 mContentDescription = a.getString(attr);
2893 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2895 if (!a.getBoolean(attr, true)) {
2896 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2897 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2898 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002899 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2901 if (!a.getBoolean(attr, true)) {
2902 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2903 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2904 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002905 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 case R.styleable.View_scrollbars:
2907 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2908 if (scrollbars != SCROLLBARS_NONE) {
2909 viewFlagValues |= scrollbars;
2910 viewFlagMasks |= SCROLLBARS_MASK;
2911 initializeScrollbars(a);
2912 }
2913 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002914 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002916 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2917 // Ignore the attribute starting with ICS
2918 break;
2919 }
2920 // With builds < ICS, fall through and apply fading edges
2921 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2923 if (fadingEdge != FADING_EDGE_NONE) {
2924 viewFlagValues |= fadingEdge;
2925 viewFlagMasks |= FADING_EDGE_MASK;
2926 initializeFadingEdge(a);
2927 }
2928 break;
2929 case R.styleable.View_scrollbarStyle:
2930 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2931 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2932 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2933 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2934 }
2935 break;
2936 case R.styleable.View_isScrollContainer:
2937 setScrollContainer = true;
2938 if (a.getBoolean(attr, false)) {
2939 setScrollContainer(true);
2940 }
2941 break;
2942 case com.android.internal.R.styleable.View_keepScreenOn:
2943 if (a.getBoolean(attr, false)) {
2944 viewFlagValues |= KEEP_SCREEN_ON;
2945 viewFlagMasks |= KEEP_SCREEN_ON;
2946 }
2947 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002948 case R.styleable.View_filterTouchesWhenObscured:
2949 if (a.getBoolean(attr, false)) {
2950 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2951 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2952 }
2953 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 case R.styleable.View_nextFocusLeft:
2955 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2956 break;
2957 case R.styleable.View_nextFocusRight:
2958 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2959 break;
2960 case R.styleable.View_nextFocusUp:
2961 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2962 break;
2963 case R.styleable.View_nextFocusDown:
2964 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2965 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002966 case R.styleable.View_nextFocusForward:
2967 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2968 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 case R.styleable.View_minWidth:
2970 mMinWidth = a.getDimensionPixelSize(attr, 0);
2971 break;
2972 case R.styleable.View_minHeight:
2973 mMinHeight = a.getDimensionPixelSize(attr, 0);
2974 break;
Romain Guy9a817362009-05-01 10:57:14 -07002975 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002976 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002977 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002978 + "be used within a restricted context");
2979 }
2980
Romain Guy9a817362009-05-01 10:57:14 -07002981 final String handlerName = a.getString(attr);
2982 if (handlerName != null) {
2983 setOnClickListener(new OnClickListener() {
2984 private Method mHandler;
2985
2986 public void onClick(View v) {
2987 if (mHandler == null) {
2988 try {
2989 mHandler = getContext().getClass().getMethod(handlerName,
2990 View.class);
2991 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002992 int id = getId();
2993 String idText = id == NO_ID ? "" : " with id '"
2994 + getContext().getResources().getResourceEntryName(
2995 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002996 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002997 handlerName + "(View) in the activity "
2998 + getContext().getClass() + " for onClick handler"
2999 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003000 }
3001 }
3002
3003 try {
3004 mHandler.invoke(getContext(), View.this);
3005 } catch (IllegalAccessException e) {
3006 throw new IllegalStateException("Could not execute non "
3007 + "public method of the activity", e);
3008 } catch (InvocationTargetException e) {
3009 throw new IllegalStateException("Could not execute "
3010 + "method of the activity", e);
3011 }
3012 }
3013 });
3014 }
3015 break;
Adam Powell637d3372010-08-25 14:37:03 -07003016 case R.styleable.View_overScrollMode:
3017 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3018 break;
Adam Powell20232d02010-12-08 21:08:53 -08003019 case R.styleable.View_verticalScrollbarPosition:
3020 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3021 break;
Romain Guy171c5922011-01-06 10:04:23 -08003022 case R.styleable.View_layerType:
3023 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3024 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003025 case R.styleable.View_textDirection:
3026 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3027 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
3029 }
3030
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003031 a.recycle();
3032
Adam Powell637d3372010-08-25 14:37:03 -07003033 setOverScrollMode(overScrollMode);
3034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 if (background != null) {
3036 setBackgroundDrawable(background);
3037 }
3038
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003039 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3040
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003041 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3042 // layout direction). Those cached values will be used later during padding resolution.
3043 mUserPaddingStart = startPadding;
3044 mUserPaddingEnd = endPadding;
3045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 if (padding >= 0) {
3047 leftPadding = padding;
3048 topPadding = padding;
3049 rightPadding = padding;
3050 bottomPadding = padding;
3051 }
3052
3053 // If the user specified the padding (either with android:padding or
3054 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3055 // use the default padding or the padding from the background drawable
3056 // (stored at this point in mPadding*)
3057 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3058 topPadding >= 0 ? topPadding : mPaddingTop,
3059 rightPadding >= 0 ? rightPadding : mPaddingRight,
3060 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3061
3062 if (viewFlagMasks != 0) {
3063 setFlags(viewFlagValues, viewFlagMasks);
3064 }
3065
3066 // Needs to be called after mViewFlags is set
3067 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3068 recomputePadding();
3069 }
3070
3071 if (x != 0 || y != 0) {
3072 scrollTo(x, y);
3073 }
3074
Chet Haase73066682010-11-29 15:55:32 -08003075 if (transformSet) {
3076 setTranslationX(tx);
3077 setTranslationY(ty);
3078 setRotation(rotation);
3079 setRotationX(rotationX);
3080 setRotationY(rotationY);
3081 setScaleX(sx);
3082 setScaleY(sy);
3083 }
3084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3086 setScrollContainer(true);
3087 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003088
3089 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 }
3091
3092 /**
3093 * Non-public constructor for use in testing
3094 */
3095 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003096 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 }
3098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 /**
3100 * <p>
3101 * Initializes the fading edges from a given set of styled attributes. This
3102 * method should be called by subclasses that need fading edges and when an
3103 * instance of these subclasses is created programmatically rather than
3104 * being inflated from XML. This method is automatically called when the XML
3105 * is inflated.
3106 * </p>
3107 *
3108 * @param a the styled attributes set to initialize the fading edges from
3109 */
3110 protected void initializeFadingEdge(TypedArray a) {
3111 initScrollCache();
3112
3113 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3114 R.styleable.View_fadingEdgeLength,
3115 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3116 }
3117
3118 /**
3119 * Returns the size of the vertical faded edges used to indicate that more
3120 * content in this view is visible.
3121 *
3122 * @return The size in pixels of the vertical faded edge or 0 if vertical
3123 * faded edges are not enabled for this view.
3124 * @attr ref android.R.styleable#View_fadingEdgeLength
3125 */
3126 public int getVerticalFadingEdgeLength() {
3127 if (isVerticalFadingEdgeEnabled()) {
3128 ScrollabilityCache cache = mScrollCache;
3129 if (cache != null) {
3130 return cache.fadingEdgeLength;
3131 }
3132 }
3133 return 0;
3134 }
3135
3136 /**
3137 * Set the size of the faded edge used to indicate that more content in this
3138 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003139 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3140 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3141 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 *
3143 * @param length The size in pixels of the faded edge used to indicate that more
3144 * content in this view is visible.
3145 */
3146 public void setFadingEdgeLength(int length) {
3147 initScrollCache();
3148 mScrollCache.fadingEdgeLength = length;
3149 }
3150
3151 /**
3152 * Returns the size of the horizontal faded edges used to indicate that more
3153 * content in this view is visible.
3154 *
3155 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3156 * faded edges are not enabled for this view.
3157 * @attr ref android.R.styleable#View_fadingEdgeLength
3158 */
3159 public int getHorizontalFadingEdgeLength() {
3160 if (isHorizontalFadingEdgeEnabled()) {
3161 ScrollabilityCache cache = mScrollCache;
3162 if (cache != null) {
3163 return cache.fadingEdgeLength;
3164 }
3165 }
3166 return 0;
3167 }
3168
3169 /**
3170 * Returns the width of the vertical scrollbar.
3171 *
3172 * @return The width in pixels of the vertical scrollbar or 0 if there
3173 * is no vertical scrollbar.
3174 */
3175 public int getVerticalScrollbarWidth() {
3176 ScrollabilityCache cache = mScrollCache;
3177 if (cache != null) {
3178 ScrollBarDrawable scrollBar = cache.scrollBar;
3179 if (scrollBar != null) {
3180 int size = scrollBar.getSize(true);
3181 if (size <= 0) {
3182 size = cache.scrollBarSize;
3183 }
3184 return size;
3185 }
3186 return 0;
3187 }
3188 return 0;
3189 }
3190
3191 /**
3192 * Returns the height of the horizontal scrollbar.
3193 *
3194 * @return The height in pixels of the horizontal scrollbar or 0 if
3195 * there is no horizontal scrollbar.
3196 */
3197 protected int getHorizontalScrollbarHeight() {
3198 ScrollabilityCache cache = mScrollCache;
3199 if (cache != null) {
3200 ScrollBarDrawable scrollBar = cache.scrollBar;
3201 if (scrollBar != null) {
3202 int size = scrollBar.getSize(false);
3203 if (size <= 0) {
3204 size = cache.scrollBarSize;
3205 }
3206 return size;
3207 }
3208 return 0;
3209 }
3210 return 0;
3211 }
3212
3213 /**
3214 * <p>
3215 * Initializes the scrollbars from a given set of styled attributes. This
3216 * method should be called by subclasses that need scrollbars and when an
3217 * instance of these subclasses is created programmatically rather than
3218 * being inflated from XML. This method is automatically called when the XML
3219 * is inflated.
3220 * </p>
3221 *
3222 * @param a the styled attributes set to initialize the scrollbars from
3223 */
3224 protected void initializeScrollbars(TypedArray a) {
3225 initScrollCache();
3226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003228
Mike Cleronf116bf82009-09-27 19:14:12 -07003229 if (scrollabilityCache.scrollBar == null) {
3230 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3231 }
Joe Malin32736f02011-01-19 16:14:20 -08003232
Romain Guy8bda2482010-03-02 11:42:11 -08003233 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234
Mike Cleronf116bf82009-09-27 19:14:12 -07003235 if (!fadeScrollbars) {
3236 scrollabilityCache.state = ScrollabilityCache.ON;
3237 }
3238 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003239
3240
Mike Cleronf116bf82009-09-27 19:14:12 -07003241 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3242 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3243 .getScrollBarFadeDuration());
3244 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3245 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3246 ViewConfiguration.getScrollDefaultDelay());
3247
Joe Malin32736f02011-01-19 16:14:20 -08003248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3250 com.android.internal.R.styleable.View_scrollbarSize,
3251 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3252
3253 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3254 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3255
3256 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3257 if (thumb != null) {
3258 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3259 }
3260
3261 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3262 false);
3263 if (alwaysDraw) {
3264 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3265 }
3266
3267 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3268 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3269
3270 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3271 if (thumb != null) {
3272 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3273 }
3274
3275 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3276 false);
3277 if (alwaysDraw) {
3278 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3279 }
3280
3281 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003282 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 }
3284
3285 /**
3286 * <p>
3287 * Initalizes the scrollability cache if necessary.
3288 * </p>
3289 */
3290 private void initScrollCache() {
3291 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003292 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 }
3294 }
3295
3296 /**
Adam Powell20232d02010-12-08 21:08:53 -08003297 * Set the position of the vertical scroll bar. Should be one of
3298 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3299 * {@link #SCROLLBAR_POSITION_RIGHT}.
3300 *
3301 * @param position Where the vertical scroll bar should be positioned.
3302 */
3303 public void setVerticalScrollbarPosition(int position) {
3304 if (mVerticalScrollbarPosition != position) {
3305 mVerticalScrollbarPosition = position;
3306 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003307 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003308 }
3309 }
3310
3311 /**
3312 * @return The position where the vertical scroll bar will show, if applicable.
3313 * @see #setVerticalScrollbarPosition(int)
3314 */
3315 public int getVerticalScrollbarPosition() {
3316 return mVerticalScrollbarPosition;
3317 }
3318
3319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 * Register a callback to be invoked when focus of this view changed.
3321 *
3322 * @param l The callback that will run.
3323 */
3324 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3325 mOnFocusChangeListener = l;
3326 }
3327
3328 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003329 * Add a listener that will be called when the bounds of the view change due to
3330 * layout processing.
3331 *
3332 * @param listener The listener that will be called when layout bounds change.
3333 */
3334 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3335 if (mOnLayoutChangeListeners == null) {
3336 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3337 }
3338 mOnLayoutChangeListeners.add(listener);
3339 }
3340
3341 /**
3342 * Remove a listener for layout changes.
3343 *
3344 * @param listener The listener for layout bounds change.
3345 */
3346 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3347 if (mOnLayoutChangeListeners == null) {
3348 return;
3349 }
3350 mOnLayoutChangeListeners.remove(listener);
3351 }
3352
3353 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003354 * Add a listener for attach state changes.
3355 *
3356 * This listener will be called whenever this view is attached or detached
3357 * from a window. Remove the listener using
3358 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3359 *
3360 * @param listener Listener to attach
3361 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3362 */
3363 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3364 if (mOnAttachStateChangeListeners == null) {
3365 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3366 }
3367 mOnAttachStateChangeListeners.add(listener);
3368 }
3369
3370 /**
3371 * Remove a listener for attach state changes. The listener will receive no further
3372 * notification of window attach/detach events.
3373 *
3374 * @param listener Listener to remove
3375 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3376 */
3377 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3378 if (mOnAttachStateChangeListeners == null) {
3379 return;
3380 }
3381 mOnAttachStateChangeListeners.remove(listener);
3382 }
3383
3384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 * Returns the focus-change callback registered for this view.
3386 *
3387 * @return The callback, or null if one is not registered.
3388 */
3389 public OnFocusChangeListener getOnFocusChangeListener() {
3390 return mOnFocusChangeListener;
3391 }
3392
3393 /**
3394 * Register a callback to be invoked when this view is clicked. If this view is not
3395 * clickable, it becomes clickable.
3396 *
3397 * @param l The callback that will run
3398 *
3399 * @see #setClickable(boolean)
3400 */
3401 public void setOnClickListener(OnClickListener l) {
3402 if (!isClickable()) {
3403 setClickable(true);
3404 }
3405 mOnClickListener = l;
3406 }
3407
3408 /**
3409 * Register a callback to be invoked when this view is clicked and held. If this view is not
3410 * long clickable, it becomes long clickable.
3411 *
3412 * @param l The callback that will run
3413 *
3414 * @see #setLongClickable(boolean)
3415 */
3416 public void setOnLongClickListener(OnLongClickListener l) {
3417 if (!isLongClickable()) {
3418 setLongClickable(true);
3419 }
3420 mOnLongClickListener = l;
3421 }
3422
3423 /**
3424 * Register a callback to be invoked when the context menu for this view is
3425 * being built. If this view is not long clickable, it becomes long clickable.
3426 *
3427 * @param l The callback that will run
3428 *
3429 */
3430 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3431 if (!isLongClickable()) {
3432 setLongClickable(true);
3433 }
3434 mOnCreateContextMenuListener = l;
3435 }
3436
3437 /**
3438 * Call this view's OnClickListener, if it is defined.
3439 *
3440 * @return True there was an assigned OnClickListener that was called, false
3441 * otherwise is returned.
3442 */
3443 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003444 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 if (mOnClickListener != null) {
3447 playSoundEffect(SoundEffectConstants.CLICK);
3448 mOnClickListener.onClick(this);
3449 return true;
3450 }
3451
3452 return false;
3453 }
3454
3455 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003456 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3457 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003459 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 */
3461 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003462 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 boolean handled = false;
3465 if (mOnLongClickListener != null) {
3466 handled = mOnLongClickListener.onLongClick(View.this);
3467 }
3468 if (!handled) {
3469 handled = showContextMenu();
3470 }
3471 if (handled) {
3472 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3473 }
3474 return handled;
3475 }
3476
3477 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003478 * Performs button-related actions during a touch down event.
3479 *
3480 * @param event The event.
3481 * @return True if the down was consumed.
3482 *
3483 * @hide
3484 */
3485 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3486 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3487 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3488 return true;
3489 }
3490 }
3491 return false;
3492 }
3493
3494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 * Bring up the context menu for this view.
3496 *
3497 * @return Whether a context menu was displayed.
3498 */
3499 public boolean showContextMenu() {
3500 return getParent().showContextMenuForChild(this);
3501 }
3502
3503 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003504 * Bring up the context menu for this view, referring to the item under the specified point.
3505 *
3506 * @param x The referenced x coordinate.
3507 * @param y The referenced y coordinate.
3508 * @param metaState The keyboard modifiers that were pressed.
3509 * @return Whether a context menu was displayed.
3510 *
3511 * @hide
3512 */
3513 public boolean showContextMenu(float x, float y, int metaState) {
3514 return showContextMenu();
3515 }
3516
3517 /**
Adam Powell6e346362010-07-23 10:18:23 -07003518 * Start an action mode.
3519 *
3520 * @param callback Callback that will control the lifecycle of the action mode
3521 * @return The new action mode if it is started, null otherwise
3522 *
3523 * @see ActionMode
3524 */
3525 public ActionMode startActionMode(ActionMode.Callback callback) {
3526 return getParent().startActionModeForChild(this, callback);
3527 }
3528
3529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 * Register a callback to be invoked when a key is pressed in this view.
3531 * @param l the key listener to attach to this view
3532 */
3533 public void setOnKeyListener(OnKeyListener l) {
3534 mOnKeyListener = l;
3535 }
3536
3537 /**
3538 * Register a callback to be invoked when a touch event is sent to this view.
3539 * @param l the touch listener to attach to this view
3540 */
3541 public void setOnTouchListener(OnTouchListener l) {
3542 mOnTouchListener = l;
3543 }
3544
3545 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003546 * Register a callback to be invoked when a generic motion event is sent to this view.
3547 * @param l the generic motion listener to attach to this view
3548 */
3549 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3550 mOnGenericMotionListener = l;
3551 }
3552
3553 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003554 * Register a callback to be invoked when a hover event is sent to this view.
3555 * @param l the hover listener to attach to this view
3556 */
3557 public void setOnHoverListener(OnHoverListener l) {
3558 mOnHoverListener = l;
3559 }
3560
3561 /**
Joe Malin32736f02011-01-19 16:14:20 -08003562 * Register a drag event listener callback object for this View. The parameter is
3563 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3564 * View, the system calls the
3565 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3566 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003567 */
3568 public void setOnDragListener(OnDragListener l) {
3569 mOnDragListener = l;
3570 }
3571
3572 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003573 * Give this view focus. This will cause
3574 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 *
3576 * Note: this does not check whether this {@link View} should get focus, it just
3577 * gives it focus no matter what. It should only be called internally by framework
3578 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3579 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003580 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3581 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 * focus moved when requestFocus() is called. It may not always
3583 * apply, in which case use the default View.FOCUS_DOWN.
3584 * @param previouslyFocusedRect The rectangle of the view that had focus
3585 * prior in this View's coordinate system.
3586 */
3587 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3588 if (DBG) {
3589 System.out.println(this + " requestFocus()");
3590 }
3591
3592 if ((mPrivateFlags & FOCUSED) == 0) {
3593 mPrivateFlags |= FOCUSED;
3594
3595 if (mParent != null) {
3596 mParent.requestChildFocus(this, this);
3597 }
3598
3599 onFocusChanged(true, direction, previouslyFocusedRect);
3600 refreshDrawableState();
3601 }
3602 }
3603
3604 /**
3605 * Request that a rectangle of this view be visible on the screen,
3606 * scrolling if necessary just enough.
3607 *
3608 * <p>A View should call this if it maintains some notion of which part
3609 * of its content is interesting. For example, a text editing view
3610 * should call this when its cursor moves.
3611 *
3612 * @param rectangle The rectangle.
3613 * @return Whether any parent scrolled.
3614 */
3615 public boolean requestRectangleOnScreen(Rect rectangle) {
3616 return requestRectangleOnScreen(rectangle, false);
3617 }
3618
3619 /**
3620 * Request that a rectangle of this view be visible on the screen,
3621 * scrolling if necessary just enough.
3622 *
3623 * <p>A View should call this if it maintains some notion of which part
3624 * of its content is interesting. For example, a text editing view
3625 * should call this when its cursor moves.
3626 *
3627 * <p>When <code>immediate</code> is set to true, scrolling will not be
3628 * animated.
3629 *
3630 * @param rectangle The rectangle.
3631 * @param immediate True to forbid animated scrolling, false otherwise
3632 * @return Whether any parent scrolled.
3633 */
3634 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3635 View child = this;
3636 ViewParent parent = mParent;
3637 boolean scrolled = false;
3638 while (parent != null) {
3639 scrolled |= parent.requestChildRectangleOnScreen(child,
3640 rectangle, immediate);
3641
3642 // offset rect so next call has the rectangle in the
3643 // coordinate system of its direct child.
3644 rectangle.offset(child.getLeft(), child.getTop());
3645 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3646
3647 if (!(parent instanceof View)) {
3648 break;
3649 }
Romain Guy8506ab42009-06-11 17:35:47 -07003650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 child = (View) parent;
3652 parent = child.getParent();
3653 }
3654 return scrolled;
3655 }
3656
3657 /**
3658 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003659 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 */
3661 public void clearFocus() {
3662 if (DBG) {
3663 System.out.println(this + " clearFocus()");
3664 }
3665
3666 if ((mPrivateFlags & FOCUSED) != 0) {
3667 mPrivateFlags &= ~FOCUSED;
3668
3669 if (mParent != null) {
3670 mParent.clearChildFocus(this);
3671 }
3672
3673 onFocusChanged(false, 0, null);
3674 refreshDrawableState();
3675 }
3676 }
3677
3678 /**
3679 * Called to clear the focus of a view that is about to be removed.
3680 * Doesn't call clearChildFocus, which prevents this view from taking
3681 * focus again before it has been removed from the parent
3682 */
3683 void clearFocusForRemoval() {
3684 if ((mPrivateFlags & FOCUSED) != 0) {
3685 mPrivateFlags &= ~FOCUSED;
3686
3687 onFocusChanged(false, 0, null);
3688 refreshDrawableState();
3689 }
3690 }
3691
3692 /**
3693 * Called internally by the view system when a new view is getting focus.
3694 * This is what clears the old focus.
3695 */
3696 void unFocus() {
3697 if (DBG) {
3698 System.out.println(this + " unFocus()");
3699 }
3700
3701 if ((mPrivateFlags & FOCUSED) != 0) {
3702 mPrivateFlags &= ~FOCUSED;
3703
3704 onFocusChanged(false, 0, null);
3705 refreshDrawableState();
3706 }
3707 }
3708
3709 /**
3710 * Returns true if this view has focus iteself, or is the ancestor of the
3711 * view that has focus.
3712 *
3713 * @return True if this view has or contains focus, false otherwise.
3714 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003715 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 public boolean hasFocus() {
3717 return (mPrivateFlags & FOCUSED) != 0;
3718 }
3719
3720 /**
3721 * Returns true if this view is focusable or if it contains a reachable View
3722 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3723 * is a View whose parents do not block descendants focus.
3724 *
3725 * Only {@link #VISIBLE} views are considered focusable.
3726 *
3727 * @return True if the view is focusable or if the view contains a focusable
3728 * View, false otherwise.
3729 *
3730 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3731 */
3732 public boolean hasFocusable() {
3733 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3734 }
3735
3736 /**
3737 * Called by the view system when the focus state of this view changes.
3738 * When the focus change event is caused by directional navigation, direction
3739 * and previouslyFocusedRect provide insight into where the focus is coming from.
3740 * When overriding, be sure to call up through to the super class so that
3741 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003742 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 * @param gainFocus True if the View has focus; false otherwise.
3744 * @param direction The direction focus has moved when requestFocus()
3745 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003746 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3747 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3748 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3750 * system, of the previously focused view. If applicable, this will be
3751 * passed in as finer grained information about where the focus is coming
3752 * from (in addition to direction). Will be <code>null</code> otherwise.
3753 */
3754 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003755 if (gainFocus) {
3756 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3757 }
3758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 InputMethodManager imm = InputMethodManager.peekInstance();
3760 if (!gainFocus) {
3761 if (isPressed()) {
3762 setPressed(false);
3763 }
3764 if (imm != null && mAttachInfo != null
3765 && mAttachInfo.mHasWindowFocus) {
3766 imm.focusOut(this);
3767 }
Romain Guya2431d02009-04-30 16:30:00 -07003768 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 } else if (imm != null && mAttachInfo != null
3770 && mAttachInfo.mHasWindowFocus) {
3771 imm.focusIn(this);
3772 }
Romain Guy8506ab42009-06-11 17:35:47 -07003773
Romain Guy0fd89bf2011-01-26 15:41:30 -08003774 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 if (mOnFocusChangeListener != null) {
3776 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3777 }
Joe Malin32736f02011-01-19 16:14:20 -08003778
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003779 if (mAttachInfo != null) {
3780 mAttachInfo.mKeyDispatchState.reset(this);
3781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 }
3783
3784 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003785 * Sends an accessibility event of the given type. If accessiiblity is
3786 * not enabled this method has no effect. The default implementation calls
3787 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3788 * to populate information about the event source (this View), then calls
3789 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3790 * populate the text content of the event source including its descendants,
3791 * and last calls
3792 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3793 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003794 * <p>
3795 * If an {@link AccessibilityDelegate} has been specified via calling
3796 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3797 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3798 * responsible for handling this call.
3799 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003800 *
3801 * @param eventType The type of the event to send.
3802 *
3803 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3804 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3805 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003806 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003807 */
3808 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003809 if (mAccessibilityDelegate != null) {
3810 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3811 } else {
3812 sendAccessibilityEventInternal(eventType);
3813 }
3814 }
3815
3816 /**
3817 * @see #sendAccessibilityEvent(int)
3818 *
3819 * Note: Called from the default {@link AccessibilityDelegate}.
3820 */
3821 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003822 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3823 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3824 }
3825 }
3826
3827 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003828 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3829 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003830 * perform a check whether accessibility is enabled.
3831 * <p>
3832 * If an {@link AccessibilityDelegate} has been specified via calling
3833 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3834 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3835 * is responsible for handling this call.
3836 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003837 *
3838 * @param event The event to send.
3839 *
3840 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003841 */
3842 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003843 if (mAccessibilityDelegate != null) {
3844 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3845 } else {
3846 sendAccessibilityEventUncheckedInternal(event);
3847 }
3848 }
3849
3850 /**
3851 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3852 *
3853 * Note: Called from the default {@link AccessibilityDelegate}.
3854 */
3855 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003856 if (!isShown()) {
3857 return;
3858 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003859 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003860 // Only a subset of accessibility events populates text content.
3861 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3862 dispatchPopulateAccessibilityEvent(event);
3863 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003864 // In the beginning we called #isShown(), so we know that getParent() is not null.
3865 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003866 }
3867
3868 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003869 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3870 * to its children for adding their text content to the event. Note that the
3871 * event text is populated in a separate dispatch path since we add to the
3872 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003873 * A typical implementation will call
3874 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3875 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3876 * on each child. Override this method if custom population of the event text
3877 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003878 * <p>
3879 * If an {@link AccessibilityDelegate} has been specified via calling
3880 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3881 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3882 * is responsible for handling this call.
3883 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003884 * <p>
3885 * <em>Note:</em> Accessibility events of certain types are not dispatched for
3886 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
3887 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003888 *
3889 * @param event The event.
3890 *
3891 * @return True if the event population was completed.
3892 */
3893 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003894 if (mAccessibilityDelegate != null) {
3895 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3896 } else {
3897 return dispatchPopulateAccessibilityEventInternal(event);
3898 }
3899 }
3900
3901 /**
3902 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3903 *
3904 * Note: Called from the default {@link AccessibilityDelegate}.
3905 */
3906 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003907 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003908 return false;
3909 }
3910
3911 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003912 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003913 * giving a chance to this View to populate the accessibility event with its
3914 * text content. While the implementation is free to modify other event
3915 * attributes this should be performed in
3916 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3917 * <p>
3918 * Example: Adding formatted date string to an accessibility event in addition
3919 * to the text added by the super implementation.
3920 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003921 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3922 * super.onPopulateAccessibilityEvent(event);
3923 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3924 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3925 * mCurrentDate.getTimeInMillis(), flags);
3926 * event.getText().add(selectedDateUtterance);
3927 * }
3928 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 * <p>
3930 * If an {@link AccessibilityDelegate} has been specified via calling
3931 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3932 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3933 * is responsible for handling this call.
3934 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003935 *
3936 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003937 *
3938 * @see #sendAccessibilityEvent(int)
3939 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003940 */
3941 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003942 if (mAccessibilityDelegate != null) {
3943 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3944 } else {
3945 onPopulateAccessibilityEventInternal(event);
3946 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003947 }
3948
3949 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003950 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3951 *
3952 * Note: Called from the default {@link AccessibilityDelegate}.
3953 */
3954 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3955
3956 }
3957
3958 /**
3959 * Initializes an {@link AccessibilityEvent} with information about
3960 * this View which is the event source. In other words, the source of
3961 * an accessibility event is the view whose state change triggered firing
3962 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003963 * <p>
3964 * Example: Setting the password property of an event in addition
3965 * to properties set by the super implementation.
3966 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003967 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3968 * super.onInitializeAccessibilityEvent(event);
3969 * event.setPassword(true);
3970 * }
3971 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003972 * <p>
3973 * If an {@link AccessibilityDelegate} has been specified via calling
3974 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3975 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
3976 * is responsible for handling this call.
3977 * </p>
3978 *
3979 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003980 *
3981 * @see #sendAccessibilityEvent(int)
3982 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3983 */
3984 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003985 if (mAccessibilityDelegate != null) {
3986 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
3987 } else {
3988 onInitializeAccessibilityEventInternal(event);
3989 }
3990 }
3991
3992 /**
3993 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3994 *
3995 * Note: Called from the default {@link AccessibilityDelegate}.
3996 */
3997 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003998 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003999 event.setClassName(getClass().getName());
4000 event.setPackageName(getContext().getPackageName());
4001 event.setEnabled(isEnabled());
4002 event.setContentDescription(mContentDescription);
4003
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004004 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4005 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4006 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4007 FOCUSABLES_ALL);
4008 event.setItemCount(focusablesTempList.size());
4009 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4010 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004011 }
4012 }
4013
4014 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004015 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4016 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4017 * This method is responsible for obtaining an accessibility node info from a
4018 * pool of reusable instances and calling
4019 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4020 * initialize the former.
4021 * <p>
4022 * Note: The client is responsible for recycling the obtained instance by calling
4023 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4024 * </p>
4025 * @return A populated {@link AccessibilityNodeInfo}.
4026 *
4027 * @see AccessibilityNodeInfo
4028 */
4029 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4030 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4031 onInitializeAccessibilityNodeInfo(info);
4032 return info;
4033 }
4034
4035 /**
4036 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4037 * The base implementation sets:
4038 * <ul>
4039 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004040 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4041 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004042 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4043 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4044 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4045 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4046 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4047 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4048 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4049 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4050 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4051 * </ul>
4052 * <p>
4053 * Subclasses should override this method, call the super implementation,
4054 * and set additional attributes.
4055 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004056 * <p>
4057 * If an {@link AccessibilityDelegate} has been specified via calling
4058 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4059 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4060 * is responsible for handling this call.
4061 * </p>
4062 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004063 * @param info The instance to initialize.
4064 */
4065 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004066 if (mAccessibilityDelegate != null) {
4067 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4068 } else {
4069 onInitializeAccessibilityNodeInfoInternal(info);
4070 }
4071 }
4072
4073 /**
4074 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4075 *
4076 * Note: Called from the default {@link AccessibilityDelegate}.
4077 */
4078 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004079 Rect bounds = mAttachInfo.mTmpInvalRect;
4080 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004081 info.setBoundsInParent(bounds);
4082
4083 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4084 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004085 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004086 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4087 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004088
4089 ViewParent parent = getParent();
4090 if (parent instanceof View) {
4091 View parentView = (View) parent;
4092 info.setParent(parentView);
4093 }
4094
4095 info.setPackageName(mContext.getPackageName());
4096 info.setClassName(getClass().getName());
4097 info.setContentDescription(getContentDescription());
4098
4099 info.setEnabled(isEnabled());
4100 info.setClickable(isClickable());
4101 info.setFocusable(isFocusable());
4102 info.setFocused(isFocused());
4103 info.setSelected(isSelected());
4104 info.setLongClickable(isLongClickable());
4105
4106 // TODO: These make sense only if we are in an AdapterView but all
4107 // views can be selected. Maybe from accessiiblity perspective
4108 // we should report as selectable view in an AdapterView.
4109 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4110 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4111
4112 if (isFocusable()) {
4113 if (isFocused()) {
4114 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4115 } else {
4116 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4117 }
4118 }
4119 }
4120
4121 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004122 * Sets a delegate for implementing accessibility support via compositon as
4123 * opposed to inheritance. The delegate's primary use is for implementing
4124 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4125 *
4126 * @param delegate The delegate instance.
4127 *
4128 * @see AccessibilityDelegate
4129 */
4130 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4131 mAccessibilityDelegate = delegate;
4132 }
4133
4134 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004135 * Gets the unique identifier of this view on the screen for accessibility purposes.
4136 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4137 *
4138 * @return The view accessibility id.
4139 *
4140 * @hide
4141 */
4142 public int getAccessibilityViewId() {
4143 if (mAccessibilityViewId == NO_ID) {
4144 mAccessibilityViewId = sNextAccessibilityViewId++;
4145 }
4146 return mAccessibilityViewId;
4147 }
4148
4149 /**
4150 * Gets the unique identifier of the window in which this View reseides.
4151 *
4152 * @return The window accessibility id.
4153 *
4154 * @hide
4155 */
4156 public int getAccessibilityWindowId() {
4157 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4158 }
4159
4160 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004161 * Gets the {@link View} description. It briefly describes the view and is
4162 * primarily used for accessibility support. Set this property to enable
4163 * better accessibility support for your application. This is especially
4164 * true for views that do not have textual representation (For example,
4165 * ImageButton).
4166 *
4167 * @return The content descriptiopn.
4168 *
4169 * @attr ref android.R.styleable#View_contentDescription
4170 */
4171 public CharSequence getContentDescription() {
4172 return mContentDescription;
4173 }
4174
4175 /**
4176 * Sets the {@link View} description. It briefly describes the view and is
4177 * primarily used for accessibility support. Set this property to enable
4178 * better accessibility support for your application. This is especially
4179 * true for views that do not have textual representation (For example,
4180 * ImageButton).
4181 *
4182 * @param contentDescription The content description.
4183 *
4184 * @attr ref android.R.styleable#View_contentDescription
4185 */
4186 public void setContentDescription(CharSequence contentDescription) {
4187 mContentDescription = contentDescription;
4188 }
4189
4190 /**
Romain Guya2431d02009-04-30 16:30:00 -07004191 * Invoked whenever this view loses focus, either by losing window focus or by losing
4192 * focus within its window. This method can be used to clear any state tied to the
4193 * focus. For instance, if a button is held pressed with the trackball and the window
4194 * loses focus, this method can be used to cancel the press.
4195 *
4196 * Subclasses of View overriding this method should always call super.onFocusLost().
4197 *
4198 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004199 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004200 *
4201 * @hide pending API council approval
4202 */
4203 protected void onFocusLost() {
4204 resetPressedState();
4205 }
4206
4207 private void resetPressedState() {
4208 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4209 return;
4210 }
4211
4212 if (isPressed()) {
4213 setPressed(false);
4214
4215 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004216 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004217 }
4218 }
4219 }
4220
4221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 * Returns true if this view has focus
4223 *
4224 * @return True if this view has focus, false otherwise.
4225 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004226 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 public boolean isFocused() {
4228 return (mPrivateFlags & FOCUSED) != 0;
4229 }
4230
4231 /**
4232 * Find the view in the hierarchy rooted at this view that currently has
4233 * focus.
4234 *
4235 * @return The view that currently has focus, or null if no focused view can
4236 * be found.
4237 */
4238 public View findFocus() {
4239 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4240 }
4241
4242 /**
4243 * Change whether this view is one of the set of scrollable containers in
4244 * its window. This will be used to determine whether the window can
4245 * resize or must pan when a soft input area is open -- scrollable
4246 * containers allow the window to use resize mode since the container
4247 * will appropriately shrink.
4248 */
4249 public void setScrollContainer(boolean isScrollContainer) {
4250 if (isScrollContainer) {
4251 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4252 mAttachInfo.mScrollContainers.add(this);
4253 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4254 }
4255 mPrivateFlags |= SCROLL_CONTAINER;
4256 } else {
4257 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4258 mAttachInfo.mScrollContainers.remove(this);
4259 }
4260 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4261 }
4262 }
4263
4264 /**
4265 * Returns the quality of the drawing cache.
4266 *
4267 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4268 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4269 *
4270 * @see #setDrawingCacheQuality(int)
4271 * @see #setDrawingCacheEnabled(boolean)
4272 * @see #isDrawingCacheEnabled()
4273 *
4274 * @attr ref android.R.styleable#View_drawingCacheQuality
4275 */
4276 public int getDrawingCacheQuality() {
4277 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4278 }
4279
4280 /**
4281 * Set the drawing cache quality of this view. This value is used only when the
4282 * drawing cache is enabled
4283 *
4284 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4285 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4286 *
4287 * @see #getDrawingCacheQuality()
4288 * @see #setDrawingCacheEnabled(boolean)
4289 * @see #isDrawingCacheEnabled()
4290 *
4291 * @attr ref android.R.styleable#View_drawingCacheQuality
4292 */
4293 public void setDrawingCacheQuality(int quality) {
4294 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4295 }
4296
4297 /**
4298 * Returns whether the screen should remain on, corresponding to the current
4299 * value of {@link #KEEP_SCREEN_ON}.
4300 *
4301 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4302 *
4303 * @see #setKeepScreenOn(boolean)
4304 *
4305 * @attr ref android.R.styleable#View_keepScreenOn
4306 */
4307 public boolean getKeepScreenOn() {
4308 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4309 }
4310
4311 /**
4312 * Controls whether the screen should remain on, modifying the
4313 * value of {@link #KEEP_SCREEN_ON}.
4314 *
4315 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4316 *
4317 * @see #getKeepScreenOn()
4318 *
4319 * @attr ref android.R.styleable#View_keepScreenOn
4320 */
4321 public void setKeepScreenOn(boolean keepScreenOn) {
4322 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4323 }
4324
4325 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004326 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4327 * @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 -08004328 *
4329 * @attr ref android.R.styleable#View_nextFocusLeft
4330 */
4331 public int getNextFocusLeftId() {
4332 return mNextFocusLeftId;
4333 }
4334
4335 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004336 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4337 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4338 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 *
4340 * @attr ref android.R.styleable#View_nextFocusLeft
4341 */
4342 public void setNextFocusLeftId(int nextFocusLeftId) {
4343 mNextFocusLeftId = nextFocusLeftId;
4344 }
4345
4346 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004347 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4348 * @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 -08004349 *
4350 * @attr ref android.R.styleable#View_nextFocusRight
4351 */
4352 public int getNextFocusRightId() {
4353 return mNextFocusRightId;
4354 }
4355
4356 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004357 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4358 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4359 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 *
4361 * @attr ref android.R.styleable#View_nextFocusRight
4362 */
4363 public void setNextFocusRightId(int nextFocusRightId) {
4364 mNextFocusRightId = nextFocusRightId;
4365 }
4366
4367 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004368 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4369 * @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 -08004370 *
4371 * @attr ref android.R.styleable#View_nextFocusUp
4372 */
4373 public int getNextFocusUpId() {
4374 return mNextFocusUpId;
4375 }
4376
4377 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004378 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4379 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4380 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 *
4382 * @attr ref android.R.styleable#View_nextFocusUp
4383 */
4384 public void setNextFocusUpId(int nextFocusUpId) {
4385 mNextFocusUpId = nextFocusUpId;
4386 }
4387
4388 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004389 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4390 * @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 -08004391 *
4392 * @attr ref android.R.styleable#View_nextFocusDown
4393 */
4394 public int getNextFocusDownId() {
4395 return mNextFocusDownId;
4396 }
4397
4398 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004399 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4400 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4401 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 *
4403 * @attr ref android.R.styleable#View_nextFocusDown
4404 */
4405 public void setNextFocusDownId(int nextFocusDownId) {
4406 mNextFocusDownId = nextFocusDownId;
4407 }
4408
4409 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004410 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4411 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4412 *
4413 * @attr ref android.R.styleable#View_nextFocusForward
4414 */
4415 public int getNextFocusForwardId() {
4416 return mNextFocusForwardId;
4417 }
4418
4419 /**
4420 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4421 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4422 * decide automatically.
4423 *
4424 * @attr ref android.R.styleable#View_nextFocusForward
4425 */
4426 public void setNextFocusForwardId(int nextFocusForwardId) {
4427 mNextFocusForwardId = nextFocusForwardId;
4428 }
4429
4430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 * Returns the visibility of this view and all of its ancestors
4432 *
4433 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4434 */
4435 public boolean isShown() {
4436 View current = this;
4437 //noinspection ConstantConditions
4438 do {
4439 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4440 return false;
4441 }
4442 ViewParent parent = current.mParent;
4443 if (parent == null) {
4444 return false; // We are not attached to the view root
4445 }
4446 if (!(parent instanceof View)) {
4447 return true;
4448 }
4449 current = (View) parent;
4450 } while (current != null);
4451
4452 return false;
4453 }
4454
4455 /**
4456 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4457 * is set
4458 *
4459 * @param insets Insets for system windows
4460 *
4461 * @return True if this view applied the insets, false otherwise
4462 */
4463 protected boolean fitSystemWindows(Rect insets) {
4464 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4465 mPaddingLeft = insets.left;
4466 mPaddingTop = insets.top;
4467 mPaddingRight = insets.right;
4468 mPaddingBottom = insets.bottom;
4469 requestLayout();
4470 return true;
4471 }
4472 return false;
4473 }
4474
4475 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004476 * Set whether or not this view should account for system screen decorations
4477 * such as the status bar and inset its content. This allows this view to be
4478 * positioned in absolute screen coordinates and remain visible to the user.
4479 *
4480 * <p>This should only be used by top-level window decor views.
4481 *
4482 * @param fitSystemWindows true to inset content for system screen decorations, false for
4483 * default behavior.
4484 *
4485 * @attr ref android.R.styleable#View_fitsSystemWindows
4486 */
4487 public void setFitsSystemWindows(boolean fitSystemWindows) {
4488 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4489 }
4490
4491 /**
4492 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4493 * will account for system screen decorations such as the status bar and inset its
4494 * content. This allows the view to be positioned in absolute screen coordinates
4495 * and remain visible to the user.
4496 *
4497 * @return true if this view will adjust its content bounds for system screen decorations.
4498 *
4499 * @attr ref android.R.styleable#View_fitsSystemWindows
4500 */
4501 public boolean fitsSystemWindows() {
4502 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4503 }
4504
4505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 * Returns the visibility status for this view.
4507 *
4508 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4509 * @attr ref android.R.styleable#View_visibility
4510 */
4511 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004512 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4513 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4514 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 })
4516 public int getVisibility() {
4517 return mViewFlags & VISIBILITY_MASK;
4518 }
4519
4520 /**
4521 * Set the enabled state of this view.
4522 *
4523 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4524 * @attr ref android.R.styleable#View_visibility
4525 */
4526 @RemotableViewMethod
4527 public void setVisibility(int visibility) {
4528 setFlags(visibility, VISIBILITY_MASK);
4529 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4530 }
4531
4532 /**
4533 * Returns the enabled status for this view. The interpretation of the
4534 * enabled state varies by subclass.
4535 *
4536 * @return True if this view is enabled, false otherwise.
4537 */
4538 @ViewDebug.ExportedProperty
4539 public boolean isEnabled() {
4540 return (mViewFlags & ENABLED_MASK) == ENABLED;
4541 }
4542
4543 /**
4544 * Set the enabled state of this view. The interpretation of the enabled
4545 * state varies by subclass.
4546 *
4547 * @param enabled True if this view is enabled, false otherwise.
4548 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004549 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004551 if (enabled == isEnabled()) return;
4552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4554
4555 /*
4556 * The View most likely has to change its appearance, so refresh
4557 * the drawable state.
4558 */
4559 refreshDrawableState();
4560
4561 // Invalidate too, since the default behavior for views is to be
4562 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004563 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 }
4565
4566 /**
4567 * Set whether this view can receive the focus.
4568 *
4569 * Setting this to false will also ensure that this view is not focusable
4570 * in touch mode.
4571 *
4572 * @param focusable If true, this view can receive the focus.
4573 *
4574 * @see #setFocusableInTouchMode(boolean)
4575 * @attr ref android.R.styleable#View_focusable
4576 */
4577 public void setFocusable(boolean focusable) {
4578 if (!focusable) {
4579 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4580 }
4581 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4582 }
4583
4584 /**
4585 * Set whether this view can receive focus while in touch mode.
4586 *
4587 * Setting this to true will also ensure that this view is focusable.
4588 *
4589 * @param focusableInTouchMode If true, this view can receive the focus while
4590 * in touch mode.
4591 *
4592 * @see #setFocusable(boolean)
4593 * @attr ref android.R.styleable#View_focusableInTouchMode
4594 */
4595 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4596 // Focusable in touch mode should always be set before the focusable flag
4597 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4598 // which, in touch mode, will not successfully request focus on this view
4599 // because the focusable in touch mode flag is not set
4600 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4601 if (focusableInTouchMode) {
4602 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4603 }
4604 }
4605
4606 /**
4607 * Set whether this view should have sound effects enabled for events such as
4608 * clicking and touching.
4609 *
4610 * <p>You may wish to disable sound effects for a view if you already play sounds,
4611 * for instance, a dial key that plays dtmf tones.
4612 *
4613 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4614 * @see #isSoundEffectsEnabled()
4615 * @see #playSoundEffect(int)
4616 * @attr ref android.R.styleable#View_soundEffectsEnabled
4617 */
4618 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4619 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4620 }
4621
4622 /**
4623 * @return whether this view should have sound effects enabled for events such as
4624 * clicking and touching.
4625 *
4626 * @see #setSoundEffectsEnabled(boolean)
4627 * @see #playSoundEffect(int)
4628 * @attr ref android.R.styleable#View_soundEffectsEnabled
4629 */
4630 @ViewDebug.ExportedProperty
4631 public boolean isSoundEffectsEnabled() {
4632 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4633 }
4634
4635 /**
4636 * Set whether this view should have haptic feedback for events such as
4637 * long presses.
4638 *
4639 * <p>You may wish to disable haptic feedback if your view already controls
4640 * its own haptic feedback.
4641 *
4642 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4643 * @see #isHapticFeedbackEnabled()
4644 * @see #performHapticFeedback(int)
4645 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4646 */
4647 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4648 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4649 }
4650
4651 /**
4652 * @return whether this view should have haptic feedback enabled for events
4653 * long presses.
4654 *
4655 * @see #setHapticFeedbackEnabled(boolean)
4656 * @see #performHapticFeedback(int)
4657 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4658 */
4659 @ViewDebug.ExportedProperty
4660 public boolean isHapticFeedbackEnabled() {
4661 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4662 }
4663
4664 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004665 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004666 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004667 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4668 * {@link #LAYOUT_DIRECTION_RTL},
4669 * {@link #LAYOUT_DIRECTION_INHERIT} or
4670 * {@link #LAYOUT_DIRECTION_LOCALE}.
4671 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004672 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004673 * @hide
4674 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004675 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004676 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4677 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4678 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4679 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004680 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004681 public int getLayoutDirection() {
4682 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004683 }
4684
4685 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004686 * Set the layout direction for this view. This will propagate a reset of layout direction
4687 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004688 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004689 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4690 * {@link #LAYOUT_DIRECTION_RTL},
4691 * {@link #LAYOUT_DIRECTION_INHERIT} or
4692 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004693 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004694 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004695 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004696 * @hide
4697 */
4698 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004699 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004700 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004701 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004702 // Setting the flag will also request a layout.
4703 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4704 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004705 }
4706
4707 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004708 * Returns the resolved layout direction for this view.
4709 *
4710 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4711 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4712 *
4713 * @hide
4714 */
4715 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4716 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4717 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4718 })
4719 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004720 resolveLayoutDirectionIfNeeded();
4721 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004722 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4723 }
4724
4725 /**
4726 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4727 * layout attribute and/or the inherited value from the parent.</p>
4728 *
4729 * @return true if the layout is right-to-left.
4730 *
4731 * @hide
4732 */
4733 @ViewDebug.ExportedProperty(category = "layout")
4734 public boolean isLayoutRtl() {
4735 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4736 }
4737
4738 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 * If this view doesn't do any drawing on its own, set this flag to
4740 * allow further optimizations. By default, this flag is not set on
4741 * View, but could be set on some View subclasses such as ViewGroup.
4742 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004743 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4744 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 *
4746 * @param willNotDraw whether or not this View draw on its own
4747 */
4748 public void setWillNotDraw(boolean willNotDraw) {
4749 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4750 }
4751
4752 /**
4753 * Returns whether or not this View draws on its own.
4754 *
4755 * @return true if this view has nothing to draw, false otherwise
4756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004757 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 public boolean willNotDraw() {
4759 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4760 }
4761
4762 /**
4763 * When a View's drawing cache is enabled, drawing is redirected to an
4764 * offscreen bitmap. Some views, like an ImageView, must be able to
4765 * bypass this mechanism if they already draw a single bitmap, to avoid
4766 * unnecessary usage of the memory.
4767 *
4768 * @param willNotCacheDrawing true if this view does not cache its
4769 * drawing, false otherwise
4770 */
4771 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4772 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4773 }
4774
4775 /**
4776 * Returns whether or not this View can cache its drawing or not.
4777 *
4778 * @return true if this view does not cache its drawing, false otherwise
4779 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004780 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 public boolean willNotCacheDrawing() {
4782 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4783 }
4784
4785 /**
4786 * Indicates whether this view reacts to click events or not.
4787 *
4788 * @return true if the view is clickable, false otherwise
4789 *
4790 * @see #setClickable(boolean)
4791 * @attr ref android.R.styleable#View_clickable
4792 */
4793 @ViewDebug.ExportedProperty
4794 public boolean isClickable() {
4795 return (mViewFlags & CLICKABLE) == CLICKABLE;
4796 }
4797
4798 /**
4799 * Enables or disables click events for this view. When a view
4800 * is clickable it will change its state to "pressed" on every click.
4801 * Subclasses should set the view clickable to visually react to
4802 * user's clicks.
4803 *
4804 * @param clickable true to make the view clickable, false otherwise
4805 *
4806 * @see #isClickable()
4807 * @attr ref android.R.styleable#View_clickable
4808 */
4809 public void setClickable(boolean clickable) {
4810 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4811 }
4812
4813 /**
4814 * Indicates whether this view reacts to long click events or not.
4815 *
4816 * @return true if the view is long clickable, false otherwise
4817 *
4818 * @see #setLongClickable(boolean)
4819 * @attr ref android.R.styleable#View_longClickable
4820 */
4821 public boolean isLongClickable() {
4822 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4823 }
4824
4825 /**
4826 * Enables or disables long click events for this view. When a view is long
4827 * clickable it reacts to the user holding down the button for a longer
4828 * duration than a tap. This event can either launch the listener or a
4829 * context menu.
4830 *
4831 * @param longClickable true to make the view long clickable, false otherwise
4832 * @see #isLongClickable()
4833 * @attr ref android.R.styleable#View_longClickable
4834 */
4835 public void setLongClickable(boolean longClickable) {
4836 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4837 }
4838
4839 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004840 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 *
4842 * @see #isClickable()
4843 * @see #setClickable(boolean)
4844 *
4845 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4846 * the View's internal state from a previously set "pressed" state.
4847 */
4848 public void setPressed(boolean pressed) {
4849 if (pressed) {
4850 mPrivateFlags |= PRESSED;
4851 } else {
4852 mPrivateFlags &= ~PRESSED;
4853 }
4854 refreshDrawableState();
4855 dispatchSetPressed(pressed);
4856 }
4857
4858 /**
4859 * Dispatch setPressed to all of this View's children.
4860 *
4861 * @see #setPressed(boolean)
4862 *
4863 * @param pressed The new pressed state
4864 */
4865 protected void dispatchSetPressed(boolean pressed) {
4866 }
4867
4868 /**
4869 * Indicates whether the view is currently in pressed state. Unless
4870 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4871 * the pressed state.
4872 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004873 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 * @see #isClickable()
4875 * @see #setClickable(boolean)
4876 *
4877 * @return true if the view is currently pressed, false otherwise
4878 */
4879 public boolean isPressed() {
4880 return (mPrivateFlags & PRESSED) == PRESSED;
4881 }
4882
4883 /**
4884 * Indicates whether this view will save its state (that is,
4885 * whether its {@link #onSaveInstanceState} method will be called).
4886 *
4887 * @return Returns true if the view state saving is enabled, else false.
4888 *
4889 * @see #setSaveEnabled(boolean)
4890 * @attr ref android.R.styleable#View_saveEnabled
4891 */
4892 public boolean isSaveEnabled() {
4893 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4894 }
4895
4896 /**
4897 * Controls whether the saving of this view's state is
4898 * enabled (that is, whether its {@link #onSaveInstanceState} method
4899 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004900 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 * for its state to be saved. This flag can only disable the
4902 * saving of this view; any child views may still have their state saved.
4903 *
4904 * @param enabled Set to false to <em>disable</em> state saving, or true
4905 * (the default) to allow it.
4906 *
4907 * @see #isSaveEnabled()
4908 * @see #setId(int)
4909 * @see #onSaveInstanceState()
4910 * @attr ref android.R.styleable#View_saveEnabled
4911 */
4912 public void setSaveEnabled(boolean enabled) {
4913 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4914 }
4915
Jeff Brown85a31762010-09-01 17:01:00 -07004916 /**
4917 * Gets whether the framework should discard touches when the view's
4918 * window is obscured by another visible window.
4919 * Refer to the {@link View} security documentation for more details.
4920 *
4921 * @return True if touch filtering is enabled.
4922 *
4923 * @see #setFilterTouchesWhenObscured(boolean)
4924 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4925 */
4926 @ViewDebug.ExportedProperty
4927 public boolean getFilterTouchesWhenObscured() {
4928 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4929 }
4930
4931 /**
4932 * Sets whether the framework should discard touches when the view's
4933 * window is obscured by another visible window.
4934 * Refer to the {@link View} security documentation for more details.
4935 *
4936 * @param enabled True if touch filtering should be enabled.
4937 *
4938 * @see #getFilterTouchesWhenObscured
4939 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4940 */
4941 public void setFilterTouchesWhenObscured(boolean enabled) {
4942 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4943 FILTER_TOUCHES_WHEN_OBSCURED);
4944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945
4946 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004947 * Indicates whether the entire hierarchy under this view will save its
4948 * state when a state saving traversal occurs from its parent. The default
4949 * is true; if false, these views will not be saved unless
4950 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4951 *
4952 * @return Returns true if the view state saving from parent is enabled, else false.
4953 *
4954 * @see #setSaveFromParentEnabled(boolean)
4955 */
4956 public boolean isSaveFromParentEnabled() {
4957 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4958 }
4959
4960 /**
4961 * Controls whether the entire hierarchy under this view will save its
4962 * state when a state saving traversal occurs from its parent. The default
4963 * is true; if false, these views will not be saved unless
4964 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4965 *
4966 * @param enabled Set to false to <em>disable</em> state saving, or true
4967 * (the default) to allow it.
4968 *
4969 * @see #isSaveFromParentEnabled()
4970 * @see #setId(int)
4971 * @see #onSaveInstanceState()
4972 */
4973 public void setSaveFromParentEnabled(boolean enabled) {
4974 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4975 }
4976
4977
4978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 * Returns whether this View is able to take focus.
4980 *
4981 * @return True if this view can take focus, or false otherwise.
4982 * @attr ref android.R.styleable#View_focusable
4983 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004984 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 public final boolean isFocusable() {
4986 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4987 }
4988
4989 /**
4990 * When a view is focusable, it may not want to take focus when in touch mode.
4991 * For example, a button would like focus when the user is navigating via a D-pad
4992 * so that the user can click on it, but once the user starts touching the screen,
4993 * the button shouldn't take focus
4994 * @return Whether the view is focusable in touch mode.
4995 * @attr ref android.R.styleable#View_focusableInTouchMode
4996 */
4997 @ViewDebug.ExportedProperty
4998 public final boolean isFocusableInTouchMode() {
4999 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5000 }
5001
5002 /**
5003 * Find the nearest view in the specified direction that can take focus.
5004 * This does not actually give focus to that view.
5005 *
5006 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5007 *
5008 * @return The nearest focusable in the specified direction, or null if none
5009 * can be found.
5010 */
5011 public View focusSearch(int direction) {
5012 if (mParent != null) {
5013 return mParent.focusSearch(this, direction);
5014 } else {
5015 return null;
5016 }
5017 }
5018
5019 /**
5020 * This method is the last chance for the focused view and its ancestors to
5021 * respond to an arrow key. This is called when the focused view did not
5022 * consume the key internally, nor could the view system find a new view in
5023 * the requested direction to give focus to.
5024 *
5025 * @param focused The currently focused view.
5026 * @param direction The direction focus wants to move. One of FOCUS_UP,
5027 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5028 * @return True if the this view consumed this unhandled move.
5029 */
5030 public boolean dispatchUnhandledMove(View focused, int direction) {
5031 return false;
5032 }
5033
5034 /**
5035 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005036 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005038 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5039 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 * @return The user specified next view, or null if there is none.
5041 */
5042 View findUserSetNextFocus(View root, int direction) {
5043 switch (direction) {
5044 case FOCUS_LEFT:
5045 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005046 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 case FOCUS_RIGHT:
5048 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005049 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 case FOCUS_UP:
5051 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005052 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005053 case FOCUS_DOWN:
5054 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005055 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005056 case FOCUS_FORWARD:
5057 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005058 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005059 case FOCUS_BACKWARD: {
5060 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005061 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005062 @Override
5063 public boolean apply(View t) {
5064 return t.mNextFocusForwardId == id;
5065 }
5066 });
5067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 }
5069 return null;
5070 }
5071
Jeff Brown4dfbec22011-08-15 14:55:37 -07005072 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5073 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5074 @Override
5075 public boolean apply(View t) {
5076 return t.mID == childViewId;
5077 }
5078 });
5079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 if (result == null) {
5081 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5082 + "by user for id " + childViewId);
5083 }
5084 return result;
5085 }
5086
5087 /**
5088 * Find and return all focusable views that are descendants of this view,
5089 * possibly including this view if it is focusable itself.
5090 *
5091 * @param direction The direction of the focus
5092 * @return A list of focusable views
5093 */
5094 public ArrayList<View> getFocusables(int direction) {
5095 ArrayList<View> result = new ArrayList<View>(24);
5096 addFocusables(result, direction);
5097 return result;
5098 }
5099
5100 /**
5101 * Add any focusable views that are descendants of this view (possibly
5102 * including this view if it is focusable itself) to views. If we are in touch mode,
5103 * only add views that are also focusable in touch mode.
5104 *
5105 * @param views Focusable views found so far
5106 * @param direction The direction of the focus
5107 */
5108 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005109 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111
svetoslavganov75986cf2009-05-14 22:28:01 -07005112 /**
5113 * Adds any focusable views that are descendants of this view (possibly
5114 * including this view if it is focusable itself) to views. This method
5115 * adds all focusable views regardless if we are in touch mode or
5116 * only views focusable in touch mode if we are in touch mode depending on
5117 * the focusable mode paramater.
5118 *
5119 * @param views Focusable views found so far or null if all we are interested is
5120 * the number of focusables.
5121 * @param direction The direction of the focus.
5122 * @param focusableMode The type of focusables to be added.
5123 *
5124 * @see #FOCUSABLES_ALL
5125 * @see #FOCUSABLES_TOUCH_MODE
5126 */
5127 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5128 if (!isFocusable()) {
5129 return;
5130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131
svetoslavganov75986cf2009-05-14 22:28:01 -07005132 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5133 isInTouchMode() && !isFocusableInTouchMode()) {
5134 return;
5135 }
5136
5137 if (views != null) {
5138 views.add(this);
5139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 }
5141
5142 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005143 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005144 * The search is performed by either the text that the View renders or the content
5145 * description that describes the view for accessibility purposes and the view does
5146 * not render or both. Clients can specify how the search is to be performed via
5147 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5148 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005149 *
5150 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005151 * @param searched The text to match against.
5152 *
5153 * @see #FIND_VIEWS_WITH_TEXT
5154 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5155 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005156 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005157 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5158 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5159 && !TextUtils.isEmpty(mContentDescription)) {
5160 String searchedLowerCase = searched.toString().toLowerCase();
5161 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5162 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5163 outViews.add(this);
5164 }
5165 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005166 }
5167
5168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * Find and return all touchable views that are descendants of this view,
5170 * possibly including this view if it is touchable itself.
5171 *
5172 * @return A list of touchable views
5173 */
5174 public ArrayList<View> getTouchables() {
5175 ArrayList<View> result = new ArrayList<View>();
5176 addTouchables(result);
5177 return result;
5178 }
5179
5180 /**
5181 * Add any touchable views that are descendants of this view (possibly
5182 * including this view if it is touchable itself) to views.
5183 *
5184 * @param views Touchable views found so far
5185 */
5186 public void addTouchables(ArrayList<View> views) {
5187 final int viewFlags = mViewFlags;
5188
5189 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5190 && (viewFlags & ENABLED_MASK) == ENABLED) {
5191 views.add(this);
5192 }
5193 }
5194
5195 /**
5196 * Call this to try to give focus to a specific view or to one of its
5197 * descendants.
5198 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005199 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5200 * false), or if it is focusable and it is not focusable in touch mode
5201 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005203 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 * have focus, and you want your parent to look for the next one.
5205 *
5206 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5207 * {@link #FOCUS_DOWN} and <code>null</code>.
5208 *
5209 * @return Whether this view or one of its descendants actually took focus.
5210 */
5211 public final boolean requestFocus() {
5212 return requestFocus(View.FOCUS_DOWN);
5213 }
5214
5215
5216 /**
5217 * Call this to try to give focus to a specific view or to one of its
5218 * descendants and give it a hint about what direction focus is heading.
5219 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005220 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5221 * false), or if it is focusable and it is not focusable in touch mode
5222 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005224 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 * have focus, and you want your parent to look for the next one.
5226 *
5227 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5228 * <code>null</code> set for the previously focused rectangle.
5229 *
5230 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5231 * @return Whether this view or one of its descendants actually took focus.
5232 */
5233 public final boolean requestFocus(int direction) {
5234 return requestFocus(direction, null);
5235 }
5236
5237 /**
5238 * Call this to try to give focus to a specific view or to one of its descendants
5239 * and give it hints about the direction and a specific rectangle that the focus
5240 * is coming from. The rectangle can help give larger views a finer grained hint
5241 * about where focus is coming from, and therefore, where to show selection, or
5242 * forward focus change internally.
5243 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005244 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5245 * false), or if it is focusable and it is not focusable in touch mode
5246 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 *
5248 * A View will not take focus if it is not visible.
5249 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005250 * A View will not take focus if one of its parents has
5251 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5252 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005254 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 * have focus, and you want your parent to look for the next one.
5256 *
5257 * You may wish to override this method if your custom {@link View} has an internal
5258 * {@link View} that it wishes to forward the request to.
5259 *
5260 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5261 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5262 * to give a finer grained hint about where focus is coming from. May be null
5263 * if there is no hint.
5264 * @return Whether this view or one of its descendants actually took focus.
5265 */
5266 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5267 // need to be focusable
5268 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5269 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5270 return false;
5271 }
5272
5273 // need to be focusable in touch mode if in touch mode
5274 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005275 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5276 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 }
5278
5279 // need to not have any parents blocking us
5280 if (hasAncestorThatBlocksDescendantFocus()) {
5281 return false;
5282 }
5283
5284 handleFocusGainInternal(direction, previouslyFocusedRect);
5285 return true;
5286 }
5287
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005288 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005289 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005290 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005291 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005292 }
5293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 /**
5295 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5296 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5297 * touch mode to request focus when they are touched.
5298 *
5299 * @return Whether this view or one of its descendants actually took focus.
5300 *
5301 * @see #isInTouchMode()
5302 *
5303 */
5304 public final boolean requestFocusFromTouch() {
5305 // Leave touch mode if we need to
5306 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005307 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005308 if (viewRoot != null) {
5309 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 }
5311 }
5312 return requestFocus(View.FOCUS_DOWN);
5313 }
5314
5315 /**
5316 * @return Whether any ancestor of this view blocks descendant focus.
5317 */
5318 private boolean hasAncestorThatBlocksDescendantFocus() {
5319 ViewParent ancestor = mParent;
5320 while (ancestor instanceof ViewGroup) {
5321 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5322 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5323 return true;
5324 } else {
5325 ancestor = vgAncestor.getParent();
5326 }
5327 }
5328 return false;
5329 }
5330
5331 /**
Romain Guya440b002010-02-24 15:57:54 -08005332 * @hide
5333 */
5334 public void dispatchStartTemporaryDetach() {
5335 onStartTemporaryDetach();
5336 }
5337
5338 /**
5339 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5341 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005342 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 */
5344 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005345 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005346 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005347 }
5348
5349 /**
5350 * @hide
5351 */
5352 public void dispatchFinishTemporaryDetach() {
5353 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 }
Romain Guy8506ab42009-06-11 17:35:47 -07005355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 /**
5357 * Called after {@link #onStartTemporaryDetach} when the container is done
5358 * changing the view.
5359 */
5360 public void onFinishTemporaryDetach() {
5361 }
Romain Guy8506ab42009-06-11 17:35:47 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005364 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5365 * for this view's window. Returns null if the view is not currently attached
5366 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005367 * just use the standard high-level event callbacks like
5368 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005369 */
5370 public KeyEvent.DispatcherState getKeyDispatcherState() {
5371 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5372 }
Joe Malin32736f02011-01-19 16:14:20 -08005373
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 * Dispatch a key event before it is processed by any input method
5376 * associated with the view hierarchy. This can be used to intercept
5377 * key events in special situations before the IME consumes them; a
5378 * typical example would be handling the BACK key to update the application's
5379 * UI instead of allowing the IME to see it and close itself.
5380 *
5381 * @param event The key event to be dispatched.
5382 * @return True if the event was handled, false otherwise.
5383 */
5384 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5385 return onKeyPreIme(event.getKeyCode(), event);
5386 }
5387
5388 /**
5389 * Dispatch a key event to the next view on the focus path. This path runs
5390 * from the top of the view tree down to the currently focused view. If this
5391 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5392 * the next node down the focus path. This method also fires any key
5393 * listeners.
5394 *
5395 * @param event The key event to be dispatched.
5396 * @return True if the event was handled, false otherwise.
5397 */
5398 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005399 if (mInputEventConsistencyVerifier != null) {
5400 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402
Jeff Brown21bc5c92011-02-28 18:27:14 -08005403 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005404 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5406 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5407 return true;
5408 }
5409
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005410 if (event.dispatch(this, mAttachInfo != null
5411 ? mAttachInfo.mKeyDispatchState : null, this)) {
5412 return true;
5413 }
5414
5415 if (mInputEventConsistencyVerifier != null) {
5416 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5417 }
5418 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 }
5420
5421 /**
5422 * Dispatches a key shortcut event.
5423 *
5424 * @param event The key event to be dispatched.
5425 * @return True if the event was handled by the view, false otherwise.
5426 */
5427 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5428 return onKeyShortcut(event.getKeyCode(), event);
5429 }
5430
5431 /**
5432 * Pass the touch screen motion event down to the target view, or this
5433 * view if it is the target.
5434 *
5435 * @param event The motion event to be dispatched.
5436 * @return True if the event was handled by the view, false otherwise.
5437 */
5438 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005439 if (mInputEventConsistencyVerifier != null) {
5440 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5441 }
5442
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005443 if (onFilterTouchEventForSecurity(event)) {
5444 //noinspection SimplifiableIfStatement
5445 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5446 mOnTouchListener.onTouch(this, event)) {
5447 return true;
5448 }
5449
5450 if (onTouchEvent(event)) {
5451 return true;
5452 }
Jeff Brown85a31762010-09-01 17:01:00 -07005453 }
5454
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005455 if (mInputEventConsistencyVerifier != null) {
5456 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005458 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 }
5460
5461 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005462 * Filter the touch event to apply security policies.
5463 *
5464 * @param event The motion event to be filtered.
5465 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005466 *
Jeff Brown85a31762010-09-01 17:01:00 -07005467 * @see #getFilterTouchesWhenObscured
5468 */
5469 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005470 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005471 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5472 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5473 // Window is obscured, drop this touch.
5474 return false;
5475 }
5476 return true;
5477 }
5478
5479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 * Pass a trackball motion event down to the focused view.
5481 *
5482 * @param event The motion event to be dispatched.
5483 * @return True if the event was handled by the view, false otherwise.
5484 */
5485 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005486 if (mInputEventConsistencyVerifier != null) {
5487 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5488 }
5489
Romain Guy02ccac62011-06-24 13:20:23 -07005490 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 }
5492
5493 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005494 * Dispatch a generic motion event.
5495 * <p>
5496 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5497 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005498 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005499 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005500 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005501 *
5502 * @param event The motion event to be dispatched.
5503 * @return True if the event was handled by the view, false otherwise.
5504 */
5505 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005506 if (mInputEventConsistencyVerifier != null) {
5507 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5508 }
5509
Jeff Browna032cc02011-03-07 16:56:21 -08005510 final int source = event.getSource();
5511 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5512 final int action = event.getAction();
5513 if (action == MotionEvent.ACTION_HOVER_ENTER
5514 || action == MotionEvent.ACTION_HOVER_MOVE
5515 || action == MotionEvent.ACTION_HOVER_EXIT) {
5516 if (dispatchHoverEvent(event)) {
5517 return true;
5518 }
5519 } else if (dispatchGenericPointerEvent(event)) {
5520 return true;
5521 }
5522 } else if (dispatchGenericFocusedEvent(event)) {
5523 return true;
5524 }
5525
Jeff Brown10b62902011-06-20 16:40:37 -07005526 if (dispatchGenericMotionEventInternal(event)) {
5527 return true;
5528 }
5529
5530 if (mInputEventConsistencyVerifier != null) {
5531 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5532 }
5533 return false;
5534 }
5535
5536 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005537 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005538 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5539 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5540 return true;
5541 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005542
5543 if (onGenericMotionEvent(event)) {
5544 return true;
5545 }
5546
5547 if (mInputEventConsistencyVerifier != null) {
5548 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5549 }
5550 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005551 }
5552
5553 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005554 * Dispatch a hover event.
5555 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005556 * Do not call this method directly.
5557 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005558 * </p>
5559 *
5560 * @param event The motion event to be dispatched.
5561 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005562 */
5563 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005564 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005565 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5566 && mOnHoverListener.onHover(this, event)) {
5567 return true;
5568 }
5569
Jeff Browna032cc02011-03-07 16:56:21 -08005570 return onHoverEvent(event);
5571 }
5572
5573 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005574 * Returns true if the view has a child to which it has recently sent
5575 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5576 * it does not have a hovered child, then it must be the innermost hovered view.
5577 * @hide
5578 */
5579 protected boolean hasHoveredChild() {
5580 return false;
5581 }
5582
5583 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005584 * Dispatch a generic motion event to the view under the first pointer.
5585 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005586 * Do not call this method directly.
5587 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005588 * </p>
5589 *
5590 * @param event The motion event to be dispatched.
5591 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005592 */
5593 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5594 return false;
5595 }
5596
5597 /**
5598 * Dispatch a generic motion event to the currently focused view.
5599 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005600 * Do not call this method directly.
5601 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005602 * </p>
5603 *
5604 * @param event The motion event to be dispatched.
5605 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005606 */
5607 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5608 return false;
5609 }
5610
5611 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005612 * Dispatch a pointer event.
5613 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005614 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5615 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5616 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005617 * and should not be expected to handle other pointing device features.
5618 * </p>
5619 *
5620 * @param event The motion event to be dispatched.
5621 * @return True if the event was handled by the view, false otherwise.
5622 * @hide
5623 */
5624 public final boolean dispatchPointerEvent(MotionEvent event) {
5625 if (event.isTouchEvent()) {
5626 return dispatchTouchEvent(event);
5627 } else {
5628 return dispatchGenericMotionEvent(event);
5629 }
5630 }
5631
5632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 * Called when the window containing this view gains or loses window focus.
5634 * ViewGroups should override to route to their children.
5635 *
5636 * @param hasFocus True if the window containing this view now has focus,
5637 * false otherwise.
5638 */
5639 public void dispatchWindowFocusChanged(boolean hasFocus) {
5640 onWindowFocusChanged(hasFocus);
5641 }
5642
5643 /**
5644 * Called when the window containing this view gains or loses focus. Note
5645 * that this is separate from view focus: to receive key events, both
5646 * your view and its window must have focus. If a window is displayed
5647 * on top of yours that takes input focus, then your own window will lose
5648 * focus but the view focus will remain unchanged.
5649 *
5650 * @param hasWindowFocus True if the window containing this view now has
5651 * focus, false otherwise.
5652 */
5653 public void onWindowFocusChanged(boolean hasWindowFocus) {
5654 InputMethodManager imm = InputMethodManager.peekInstance();
5655 if (!hasWindowFocus) {
5656 if (isPressed()) {
5657 setPressed(false);
5658 }
5659 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5660 imm.focusOut(this);
5661 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005662 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005663 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005664 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5666 imm.focusIn(this);
5667 }
5668 refreshDrawableState();
5669 }
5670
5671 /**
5672 * Returns true if this view is in a window that currently has window focus.
5673 * Note that this is not the same as the view itself having focus.
5674 *
5675 * @return True if this view is in a window that currently has window focus.
5676 */
5677 public boolean hasWindowFocus() {
5678 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5679 }
5680
5681 /**
Adam Powell326d8082009-12-09 15:10:07 -08005682 * Dispatch a view visibility change down the view hierarchy.
5683 * ViewGroups should override to route to their children.
5684 * @param changedView The view whose visibility changed. Could be 'this' or
5685 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005686 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5687 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005688 */
5689 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5690 onVisibilityChanged(changedView, visibility);
5691 }
5692
5693 /**
5694 * Called when the visibility of the view or an ancestor of the view is changed.
5695 * @param changedView The view whose visibility changed. Could be 'this' or
5696 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005697 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5698 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005699 */
5700 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005701 if (visibility == VISIBLE) {
5702 if (mAttachInfo != null) {
5703 initialAwakenScrollBars();
5704 } else {
5705 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5706 }
5707 }
Adam Powell326d8082009-12-09 15:10:07 -08005708 }
5709
5710 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005711 * Dispatch a hint about whether this view is displayed. For instance, when
5712 * a View moves out of the screen, it might receives a display hint indicating
5713 * the view is not displayed. Applications should not <em>rely</em> on this hint
5714 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005715 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005716 * @param hint A hint about whether or not this view is displayed:
5717 * {@link #VISIBLE} or {@link #INVISIBLE}.
5718 */
5719 public void dispatchDisplayHint(int hint) {
5720 onDisplayHint(hint);
5721 }
5722
5723 /**
5724 * Gives this view a hint about whether is displayed or not. For instance, when
5725 * a View moves out of the screen, it might receives a display hint indicating
5726 * the view is not displayed. Applications should not <em>rely</em> on this hint
5727 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005728 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005729 * @param hint A hint about whether or not this view is displayed:
5730 * {@link #VISIBLE} or {@link #INVISIBLE}.
5731 */
5732 protected void onDisplayHint(int hint) {
5733 }
5734
5735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 * Dispatch a window visibility change down the view hierarchy.
5737 * ViewGroups should override to route to their children.
5738 *
5739 * @param visibility The new visibility of the window.
5740 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005741 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 */
5743 public void dispatchWindowVisibilityChanged(int visibility) {
5744 onWindowVisibilityChanged(visibility);
5745 }
5746
5747 /**
5748 * Called when the window containing has change its visibility
5749 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5750 * that this tells you whether or not your window is being made visible
5751 * to the window manager; this does <em>not</em> tell you whether or not
5752 * your window is obscured by other windows on the screen, even if it
5753 * is itself visible.
5754 *
5755 * @param visibility The new visibility of the window.
5756 */
5757 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005758 if (visibility == VISIBLE) {
5759 initialAwakenScrollBars();
5760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 }
5762
5763 /**
5764 * Returns the current visibility of the window this view is attached to
5765 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5766 *
5767 * @return Returns the current visibility of the view's window.
5768 */
5769 public int getWindowVisibility() {
5770 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5771 }
5772
5773 /**
5774 * Retrieve the overall visible display size in which the window this view is
5775 * attached to has been positioned in. This takes into account screen
5776 * decorations above the window, for both cases where the window itself
5777 * is being position inside of them or the window is being placed under
5778 * then and covered insets are used for the window to position its content
5779 * inside. In effect, this tells you the available area where content can
5780 * be placed and remain visible to users.
5781 *
5782 * <p>This function requires an IPC back to the window manager to retrieve
5783 * the requested information, so should not be used in performance critical
5784 * code like drawing.
5785 *
5786 * @param outRect Filled in with the visible display frame. If the view
5787 * is not attached to a window, this is simply the raw display size.
5788 */
5789 public void getWindowVisibleDisplayFrame(Rect outRect) {
5790 if (mAttachInfo != null) {
5791 try {
5792 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5793 } catch (RemoteException e) {
5794 return;
5795 }
5796 // XXX This is really broken, and probably all needs to be done
5797 // in the window manager, and we need to know more about whether
5798 // we want the area behind or in front of the IME.
5799 final Rect insets = mAttachInfo.mVisibleInsets;
5800 outRect.left += insets.left;
5801 outRect.top += insets.top;
5802 outRect.right -= insets.right;
5803 outRect.bottom -= insets.bottom;
5804 return;
5805 }
5806 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005807 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 }
5809
5810 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005811 * Dispatch a notification about a resource configuration change down
5812 * the view hierarchy.
5813 * ViewGroups should override to route to their children.
5814 *
5815 * @param newConfig The new resource configuration.
5816 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005817 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005818 */
5819 public void dispatchConfigurationChanged(Configuration newConfig) {
5820 onConfigurationChanged(newConfig);
5821 }
5822
5823 /**
5824 * Called when the current configuration of the resources being used
5825 * by the application have changed. You can use this to decide when
5826 * to reload resources that can changed based on orientation and other
5827 * configuration characterstics. You only need to use this if you are
5828 * not relying on the normal {@link android.app.Activity} mechanism of
5829 * recreating the activity instance upon a configuration change.
5830 *
5831 * @param newConfig The new resource configuration.
5832 */
5833 protected void onConfigurationChanged(Configuration newConfig) {
5834 }
5835
5836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 * Private function to aggregate all per-view attributes in to the view
5838 * root.
5839 */
5840 void dispatchCollectViewAttributes(int visibility) {
5841 performCollectViewAttributes(visibility);
5842 }
5843
5844 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005845 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005846 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5847 mAttachInfo.mKeepScreenOn = true;
5848 }
5849 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5850 if (mOnSystemUiVisibilityChangeListener != null) {
5851 mAttachInfo.mHasSystemUiListeners = true;
5852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 }
5854 }
5855
5856 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005857 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005859 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5860 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 ai.mRecomputeGlobalAttributes = true;
5862 }
5863 }
5864 }
5865
5866 /**
5867 * Returns whether the device is currently in touch mode. Touch mode is entered
5868 * once the user begins interacting with the device by touch, and affects various
5869 * things like whether focus is always visible to the user.
5870 *
5871 * @return Whether the device is in touch mode.
5872 */
5873 @ViewDebug.ExportedProperty
5874 public boolean isInTouchMode() {
5875 if (mAttachInfo != null) {
5876 return mAttachInfo.mInTouchMode;
5877 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005878 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 }
5880 }
5881
5882 /**
5883 * Returns the context the view is running in, through which it can
5884 * access the current theme, resources, etc.
5885 *
5886 * @return The view's Context.
5887 */
5888 @ViewDebug.CapturedViewProperty
5889 public final Context getContext() {
5890 return mContext;
5891 }
5892
5893 /**
5894 * Handle a key event before it is processed by any input method
5895 * associated with the view hierarchy. This can be used to intercept
5896 * key events in special situations before the IME consumes them; a
5897 * typical example would be handling the BACK key to update the application's
5898 * UI instead of allowing the IME to see it and close itself.
5899 *
5900 * @param keyCode The value in event.getKeyCode().
5901 * @param event Description of the key event.
5902 * @return If you handled the event, return true. If you want to allow the
5903 * event to be handled by the next receiver, return false.
5904 */
5905 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5906 return false;
5907 }
5908
5909 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005910 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5911 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5913 * is released, if the view is enabled and clickable.
5914 *
5915 * @param keyCode A key code that represents the button pressed, from
5916 * {@link android.view.KeyEvent}.
5917 * @param event The KeyEvent object that defines the button action.
5918 */
5919 public boolean onKeyDown(int keyCode, KeyEvent event) {
5920 boolean result = false;
5921
5922 switch (keyCode) {
5923 case KeyEvent.KEYCODE_DPAD_CENTER:
5924 case KeyEvent.KEYCODE_ENTER: {
5925 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5926 return true;
5927 }
5928 // Long clickable items don't necessarily have to be clickable
5929 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5930 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5931 (event.getRepeatCount() == 0)) {
5932 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005933 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 return true;
5935 }
5936 break;
5937 }
5938 }
5939 return result;
5940 }
5941
5942 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005943 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5944 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5945 * the event).
5946 */
5947 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5948 return false;
5949 }
5950
5951 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005952 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5953 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5955 * {@link KeyEvent#KEYCODE_ENTER} is released.
5956 *
5957 * @param keyCode A key code that represents the button pressed, from
5958 * {@link android.view.KeyEvent}.
5959 * @param event The KeyEvent object that defines the button action.
5960 */
5961 public boolean onKeyUp(int keyCode, KeyEvent event) {
5962 boolean result = false;
5963
5964 switch (keyCode) {
5965 case KeyEvent.KEYCODE_DPAD_CENTER:
5966 case KeyEvent.KEYCODE_ENTER: {
5967 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5968 return true;
5969 }
5970 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5971 setPressed(false);
5972
5973 if (!mHasPerformedLongPress) {
5974 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005975 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976
5977 result = performClick();
5978 }
5979 }
5980 break;
5981 }
5982 }
5983 return result;
5984 }
5985
5986 /**
5987 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5988 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5989 * the event).
5990 *
5991 * @param keyCode A key code that represents the button pressed, from
5992 * {@link android.view.KeyEvent}.
5993 * @param repeatCount The number of times the action was made.
5994 * @param event The KeyEvent object that defines the button action.
5995 */
5996 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5997 return false;
5998 }
5999
6000 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006001 * Called on the focused view when a key shortcut event is not handled.
6002 * Override this method to implement local key shortcuts for the View.
6003 * Key shortcuts can also be implemented by setting the
6004 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 *
6006 * @param keyCode The value in event.getKeyCode().
6007 * @param event Description of the key event.
6008 * @return If you handled the event, return true. If you want to allow the
6009 * event to be handled by the next receiver, return false.
6010 */
6011 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6012 return false;
6013 }
6014
6015 /**
6016 * Check whether the called view is a text editor, in which case it
6017 * would make sense to automatically display a soft input window for
6018 * it. Subclasses should override this if they implement
6019 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006020 * a call on that method would return a non-null InputConnection, and
6021 * they are really a first-class editor that the user would normally
6022 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006023 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006024 * <p>The default implementation always returns false. This does
6025 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6026 * will not be called or the user can not otherwise perform edits on your
6027 * view; it is just a hint to the system that this is not the primary
6028 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 * @return Returns true if this view is a text editor, else false.
6031 */
6032 public boolean onCheckIsTextEditor() {
6033 return false;
6034 }
Romain Guy8506ab42009-06-11 17:35:47 -07006035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 /**
6037 * Create a new InputConnection for an InputMethod to interact
6038 * with the view. The default implementation returns null, since it doesn't
6039 * support input methods. You can override this to implement such support.
6040 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 * <p>When implementing this, you probably also want to implement
6043 * {@link #onCheckIsTextEditor()} to indicate you will return a
6044 * non-null InputConnection.
6045 *
6046 * @param outAttrs Fill in with attribute information about the connection.
6047 */
6048 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6049 return null;
6050 }
6051
6052 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006053 * Called by the {@link android.view.inputmethod.InputMethodManager}
6054 * when a view who is not the current
6055 * input connection target is trying to make a call on the manager. The
6056 * default implementation returns false; you can override this to return
6057 * true for certain views if you are performing InputConnection proxying
6058 * to them.
6059 * @param view The View that is making the InputMethodManager call.
6060 * @return Return true to allow the call, false to reject.
6061 */
6062 public boolean checkInputConnectionProxy(View view) {
6063 return false;
6064 }
Romain Guy8506ab42009-06-11 17:35:47 -07006065
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 * Show the context menu for this view. It is not safe to hold on to the
6068 * menu after returning from this method.
6069 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006070 * You should normally not overload this method. Overload
6071 * {@link #onCreateContextMenu(ContextMenu)} or define an
6072 * {@link OnCreateContextMenuListener} to add items to the context menu.
6073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 * @param menu The context menu to populate
6075 */
6076 public void createContextMenu(ContextMenu menu) {
6077 ContextMenuInfo menuInfo = getContextMenuInfo();
6078
6079 // Sets the current menu info so all items added to menu will have
6080 // my extra info set.
6081 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6082
6083 onCreateContextMenu(menu);
6084 if (mOnCreateContextMenuListener != null) {
6085 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6086 }
6087
6088 // Clear the extra information so subsequent items that aren't mine don't
6089 // have my extra info.
6090 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6091
6092 if (mParent != null) {
6093 mParent.createContextMenu(menu);
6094 }
6095 }
6096
6097 /**
6098 * Views should implement this if they have extra information to associate
6099 * with the context menu. The return result is supplied as a parameter to
6100 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6101 * callback.
6102 *
6103 * @return Extra information about the item for which the context menu
6104 * should be shown. This information will vary across different
6105 * subclasses of View.
6106 */
6107 protected ContextMenuInfo getContextMenuInfo() {
6108 return null;
6109 }
6110
6111 /**
6112 * Views should implement this if the view itself is going to add items to
6113 * the context menu.
6114 *
6115 * @param menu the context menu to populate
6116 */
6117 protected void onCreateContextMenu(ContextMenu menu) {
6118 }
6119
6120 /**
6121 * Implement this method to handle trackball motion events. The
6122 * <em>relative</em> movement of the trackball since the last event
6123 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6124 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6125 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6126 * they will often be fractional values, representing the more fine-grained
6127 * movement information available from a trackball).
6128 *
6129 * @param event The motion event.
6130 * @return True if the event was handled, false otherwise.
6131 */
6132 public boolean onTrackballEvent(MotionEvent event) {
6133 return false;
6134 }
6135
6136 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006137 * Implement this method to handle generic motion events.
6138 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006139 * Generic motion events describe joystick movements, mouse hovers, track pad
6140 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006141 * {@link MotionEvent#getSource() source} of the motion event specifies
6142 * the class of input that was received. Implementations of this method
6143 * must examine the bits in the source before processing the event.
6144 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006145 * </p><p>
6146 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6147 * are delivered to the view under the pointer. All other generic motion events are
6148 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006149 * </p>
6150 * <code>
6151 * public boolean onGenericMotionEvent(MotionEvent event) {
6152 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006153 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6154 * // process the joystick movement...
6155 * return true;
6156 * }
6157 * }
6158 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6159 * switch (event.getAction()) {
6160 * case MotionEvent.ACTION_HOVER_MOVE:
6161 * // process the mouse hover movement...
6162 * return true;
6163 * case MotionEvent.ACTION_SCROLL:
6164 * // process the scroll wheel movement...
6165 * return true;
6166 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006167 * }
6168 * return super.onGenericMotionEvent(event);
6169 * }
6170 * </code>
6171 *
6172 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006173 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006174 */
6175 public boolean onGenericMotionEvent(MotionEvent event) {
6176 return false;
6177 }
6178
6179 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006180 * Implement this method to handle hover events.
6181 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006182 * This method is called whenever a pointer is hovering into, over, or out of the
6183 * bounds of a view and the view is not currently being touched.
6184 * Hover events are represented as pointer events with action
6185 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6186 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6187 * </p>
6188 * <ul>
6189 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6190 * when the pointer enters the bounds of the view.</li>
6191 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6192 * when the pointer has already entered the bounds of the view and has moved.</li>
6193 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6194 * when the pointer has exited the bounds of the view or when the pointer is
6195 * about to go down due to a button click, tap, or similar user action that
6196 * causes the view to be touched.</li>
6197 * </ul>
6198 * <p>
6199 * The view should implement this method to return true to indicate that it is
6200 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006201 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006202 * The default implementation calls {@link #setHovered} to update the hovered state
6203 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006204 * is enabled and is clickable. The default implementation also sends hover
6205 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006206 * </p>
6207 *
6208 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006209 * @return True if the view handled the hover event.
6210 *
6211 * @see #isHovered
6212 * @see #setHovered
6213 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006214 */
6215 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006216 // The root view may receive hover (or touch) events that are outside the bounds of
6217 // the window. This code ensures that we only send accessibility events for
6218 // hovers that are actually within the bounds of the root view.
6219 final int action = event.getAction();
6220 if (!mSendingHoverAccessibilityEvents) {
6221 if ((action == MotionEvent.ACTION_HOVER_ENTER
6222 || action == MotionEvent.ACTION_HOVER_MOVE)
6223 && !hasHoveredChild()
6224 && pointInView(event.getX(), event.getY())) {
6225 mSendingHoverAccessibilityEvents = true;
6226 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6227 }
6228 } else {
6229 if (action == MotionEvent.ACTION_HOVER_EXIT
6230 || (action == MotionEvent.ACTION_HOVER_MOVE
6231 && !pointInView(event.getX(), event.getY()))) {
6232 mSendingHoverAccessibilityEvents = false;
6233 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6234 }
Jeff Browna1b24182011-07-28 13:38:24 -07006235 }
6236
Jeff Brown87b7f802011-06-21 18:35:45 -07006237 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006238 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006239 case MotionEvent.ACTION_HOVER_ENTER:
6240 setHovered(true);
6241 break;
6242 case MotionEvent.ACTION_HOVER_EXIT:
6243 setHovered(false);
6244 break;
6245 }
Jeff Browna1b24182011-07-28 13:38:24 -07006246
6247 // Dispatch the event to onGenericMotionEvent before returning true.
6248 // This is to provide compatibility with existing applications that
6249 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6250 // break because of the new default handling for hoverable views
6251 // in onHoverEvent.
6252 // Note that onGenericMotionEvent will be called by default when
6253 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6254 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006255 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006256 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006257 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006258 }
6259
6260 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006261 * Returns true if the view should handle {@link #onHoverEvent}
6262 * by calling {@link #setHovered} to change its hovered state.
6263 *
6264 * @return True if the view is hoverable.
6265 */
6266 private boolean isHoverable() {
6267 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006268 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006269 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6270 return false;
6271 }
6272
6273 return (viewFlags & CLICKABLE) == CLICKABLE
6274 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6275 }
6276
6277 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006278 * Returns true if the view is currently hovered.
6279 *
6280 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006281 *
6282 * @see #setHovered
6283 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006284 */
Jeff Brown10b62902011-06-20 16:40:37 -07006285 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006286 public boolean isHovered() {
6287 return (mPrivateFlags & HOVERED) != 0;
6288 }
6289
6290 /**
6291 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006292 * <p>
6293 * Calling this method also changes the drawable state of the view. This
6294 * enables the view to react to hover by using different drawable resources
6295 * to change its appearance.
6296 * </p><p>
6297 * The {@link #onHoverChanged} method is called when the hovered state changes.
6298 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006299 *
6300 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006301 *
6302 * @see #isHovered
6303 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006304 */
6305 public void setHovered(boolean hovered) {
6306 if (hovered) {
6307 if ((mPrivateFlags & HOVERED) == 0) {
6308 mPrivateFlags |= HOVERED;
6309 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006310 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006311 }
6312 } else {
6313 if ((mPrivateFlags & HOVERED) != 0) {
6314 mPrivateFlags &= ~HOVERED;
6315 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006316 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006317 }
6318 }
6319 }
6320
6321 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006322 * Implement this method to handle hover state changes.
6323 * <p>
6324 * This method is called whenever the hover state changes as a result of a
6325 * call to {@link #setHovered}.
6326 * </p>
6327 *
6328 * @param hovered The current hover state, as returned by {@link #isHovered}.
6329 *
6330 * @see #isHovered
6331 * @see #setHovered
6332 */
6333 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006334 }
6335
6336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 * Implement this method to handle touch screen motion events.
6338 *
6339 * @param event The motion event.
6340 * @return True if the event was handled, false otherwise.
6341 */
6342 public boolean onTouchEvent(MotionEvent event) {
6343 final int viewFlags = mViewFlags;
6344
6345 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006346 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6347 mPrivateFlags &= ~PRESSED;
6348 refreshDrawableState();
6349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 // A disabled view that is clickable still consumes the touch
6351 // events, it just doesn't respond to them.
6352 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6353 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6354 }
6355
6356 if (mTouchDelegate != null) {
6357 if (mTouchDelegate.onTouchEvent(event)) {
6358 return true;
6359 }
6360 }
6361
6362 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6363 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6364 switch (event.getAction()) {
6365 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006366 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6367 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 // take focus if we don't have it already and we should in
6369 // touch mode.
6370 boolean focusTaken = false;
6371 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6372 focusTaken = requestFocus();
6373 }
6374
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006375 if (prepressed) {
6376 // The button is being released before we actually
6377 // showed it as pressed. Make it show the pressed
6378 // state now (before scheduling the click) to ensure
6379 // the user sees it.
6380 mPrivateFlags |= PRESSED;
6381 refreshDrawableState();
6382 }
Joe Malin32736f02011-01-19 16:14:20 -08006383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 if (!mHasPerformedLongPress) {
6385 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006386 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387
6388 // Only perform take click actions if we were in the pressed state
6389 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006390 // Use a Runnable and post this rather than calling
6391 // performClick directly. This lets other visual state
6392 // of the view update before click actions start.
6393 if (mPerformClick == null) {
6394 mPerformClick = new PerformClick();
6395 }
6396 if (!post(mPerformClick)) {
6397 performClick();
6398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 }
6400 }
6401
6402 if (mUnsetPressedState == null) {
6403 mUnsetPressedState = new UnsetPressedState();
6404 }
6405
Adam Powelle14579b2009-12-16 18:39:52 -08006406 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006407 postDelayed(mUnsetPressedState,
6408 ViewConfiguration.getPressedStateDuration());
6409 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 // If the post failed, unpress right now
6411 mUnsetPressedState.run();
6412 }
Adam Powelle14579b2009-12-16 18:39:52 -08006413 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 }
6415 break;
6416
6417 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006418 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006419
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006420 if (performButtonActionOnTouchDown(event)) {
6421 break;
6422 }
6423
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006424 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006425 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006426
6427 // For views inside a scrolling container, delay the pressed feedback for
6428 // a short period in case this is a scroll.
6429 if (isInScrollingContainer) {
6430 mPrivateFlags |= PREPRESSED;
6431 if (mPendingCheckForTap == null) {
6432 mPendingCheckForTap = new CheckForTap();
6433 }
6434 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6435 } else {
6436 // Not inside a scrolling container, so show the feedback right away
6437 mPrivateFlags |= PRESSED;
6438 refreshDrawableState();
6439 checkForLongClick(0);
6440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006441 break;
6442
6443 case MotionEvent.ACTION_CANCEL:
6444 mPrivateFlags &= ~PRESSED;
6445 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006446 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 break;
6448
6449 case MotionEvent.ACTION_MOVE:
6450 final int x = (int) event.getX();
6451 final int y = (int) event.getY();
6452
6453 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006454 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006456 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006458 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006459 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460
6461 // Need to switch from pressed to not pressed
6462 mPrivateFlags &= ~PRESSED;
6463 refreshDrawableState();
6464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 }
6466 break;
6467 }
6468 return true;
6469 }
6470
6471 return false;
6472 }
6473
6474 /**
Adam Powell10298662011-08-14 18:26:30 -07006475 * @hide
6476 */
6477 public boolean isInScrollingContainer() {
6478 ViewParent p = getParent();
6479 while (p != null && p instanceof ViewGroup) {
6480 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6481 return true;
6482 }
6483 p = p.getParent();
6484 }
6485 return false;
6486 }
6487
6488 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006489 * Remove the longpress detection timer.
6490 */
6491 private void removeLongPressCallback() {
6492 if (mPendingCheckForLongPress != null) {
6493 removeCallbacks(mPendingCheckForLongPress);
6494 }
6495 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006496
6497 /**
6498 * Remove the pending click action
6499 */
6500 private void removePerformClickCallback() {
6501 if (mPerformClick != null) {
6502 removeCallbacks(mPerformClick);
6503 }
6504 }
6505
Adam Powelle14579b2009-12-16 18:39:52 -08006506 /**
Romain Guya440b002010-02-24 15:57:54 -08006507 * Remove the prepress detection timer.
6508 */
6509 private void removeUnsetPressCallback() {
6510 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6511 setPressed(false);
6512 removeCallbacks(mUnsetPressedState);
6513 }
6514 }
6515
6516 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006517 * Remove the tap detection timer.
6518 */
6519 private void removeTapCallback() {
6520 if (mPendingCheckForTap != null) {
6521 mPrivateFlags &= ~PREPRESSED;
6522 removeCallbacks(mPendingCheckForTap);
6523 }
6524 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006525
6526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 * Cancels a pending long press. Your subclass can use this if you
6528 * want the context menu to come up if the user presses and holds
6529 * at the same place, but you don't want it to come up if they press
6530 * and then move around enough to cause scrolling.
6531 */
6532 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006533 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006534
6535 /*
6536 * The prepressed state handled by the tap callback is a display
6537 * construct, but the tap callback will post a long press callback
6538 * less its own timeout. Remove it here.
6539 */
6540 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 }
6542
6543 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006544 * Remove the pending callback for sending a
6545 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6546 */
6547 private void removeSendViewScrolledAccessibilityEventCallback() {
6548 if (mSendViewScrolledAccessibilityEvent != null) {
6549 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6550 }
6551 }
6552
6553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 * Sets the TouchDelegate for this View.
6555 */
6556 public void setTouchDelegate(TouchDelegate delegate) {
6557 mTouchDelegate = delegate;
6558 }
6559
6560 /**
6561 * Gets the TouchDelegate for this View.
6562 */
6563 public TouchDelegate getTouchDelegate() {
6564 return mTouchDelegate;
6565 }
6566
6567 /**
6568 * Set flags controlling behavior of this view.
6569 *
6570 * @param flags Constant indicating the value which should be set
6571 * @param mask Constant indicating the bit range that should be changed
6572 */
6573 void setFlags(int flags, int mask) {
6574 int old = mViewFlags;
6575 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6576
6577 int changed = mViewFlags ^ old;
6578 if (changed == 0) {
6579 return;
6580 }
6581 int privateFlags = mPrivateFlags;
6582
6583 /* Check if the FOCUSABLE bit has changed */
6584 if (((changed & FOCUSABLE_MASK) != 0) &&
6585 ((privateFlags & HAS_BOUNDS) !=0)) {
6586 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6587 && ((privateFlags & FOCUSED) != 0)) {
6588 /* Give up focus if we are no longer focusable */
6589 clearFocus();
6590 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6591 && ((privateFlags & FOCUSED) == 0)) {
6592 /*
6593 * Tell the view system that we are now available to take focus
6594 * if no one else already has it.
6595 */
6596 if (mParent != null) mParent.focusableViewAvailable(this);
6597 }
6598 }
6599
6600 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6601 if ((changed & VISIBILITY_MASK) != 0) {
6602 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006603 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006604 * it was not visible. Marking it drawn ensures that the invalidation will
6605 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006607 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006608 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609
6610 needGlobalAttributesUpdate(true);
6611
6612 // a view becoming visible is worth notifying the parent
6613 // about in case nothing has focus. even if this specific view
6614 // isn't focusable, it may contain something that is, so let
6615 // the root view try to give this focus if nothing else does.
6616 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6617 mParent.focusableViewAvailable(this);
6618 }
6619 }
6620 }
6621
6622 /* Check if the GONE bit has changed */
6623 if ((changed & GONE) != 0) {
6624 needGlobalAttributesUpdate(false);
6625 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626
Romain Guyecd80ee2009-12-03 17:13:02 -08006627 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6628 if (hasFocus()) clearFocus();
6629 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006630 if (mParent instanceof View) {
6631 // GONE views noop invalidation, so invalidate the parent
6632 ((View) mParent).invalidate(true);
6633 }
6634 // Mark the view drawn to ensure that it gets invalidated properly the next
6635 // time it is visible and gets invalidated
6636 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 }
6638 if (mAttachInfo != null) {
6639 mAttachInfo.mViewVisibilityChanged = true;
6640 }
6641 }
6642
6643 /* Check if the VISIBLE bit has changed */
6644 if ((changed & INVISIBLE) != 0) {
6645 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006646 /*
6647 * If this view is becoming invisible, set the DRAWN flag so that
6648 * the next invalidate() will not be skipped.
6649 */
6650 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651
6652 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6653 // root view becoming invisible shouldn't clear focus
6654 if (getRootView() != this) {
6655 clearFocus();
6656 }
6657 }
6658 if (mAttachInfo != null) {
6659 mAttachInfo.mViewVisibilityChanged = true;
6660 }
6661 }
6662
Adam Powell326d8082009-12-09 15:10:07 -08006663 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006664 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006665 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6666 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006667 } else if (mParent != null) {
6668 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006669 }
Adam Powell326d8082009-12-09 15:10:07 -08006670 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6671 }
6672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6674 destroyDrawingCache();
6675 }
6676
6677 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6678 destroyDrawingCache();
6679 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006680 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 }
6682
6683 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6684 destroyDrawingCache();
6685 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6686 }
6687
6688 if ((changed & DRAW_MASK) != 0) {
6689 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6690 if (mBGDrawable != null) {
6691 mPrivateFlags &= ~SKIP_DRAW;
6692 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6693 } else {
6694 mPrivateFlags |= SKIP_DRAW;
6695 }
6696 } else {
6697 mPrivateFlags &= ~SKIP_DRAW;
6698 }
6699 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006700 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006701 }
6702
6703 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006704 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 mParent.recomputeViewAttributes(this);
6706 }
6707 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006708
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006709 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006710 requestLayout();
6711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 }
6713
6714 /**
6715 * Change the view's z order in the tree, so it's on top of other sibling
6716 * views
6717 */
6718 public void bringToFront() {
6719 if (mParent != null) {
6720 mParent.bringChildToFront(this);
6721 }
6722 }
6723
6724 /**
6725 * This is called in response to an internal scroll in this view (i.e., the
6726 * view scrolled its own contents). This is typically as a result of
6727 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6728 * called.
6729 *
6730 * @param l Current horizontal scroll origin.
6731 * @param t Current vertical scroll origin.
6732 * @param oldl Previous horizontal scroll origin.
6733 * @param oldt Previous vertical scroll origin.
6734 */
6735 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006736 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6737 postSendViewScrolledAccessibilityEventCallback();
6738 }
6739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006740 mBackgroundSizeChanged = true;
6741
6742 final AttachInfo ai = mAttachInfo;
6743 if (ai != null) {
6744 ai.mViewScrollChanged = true;
6745 }
6746 }
6747
6748 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006749 * Interface definition for a callback to be invoked when the layout bounds of a view
6750 * changes due to layout processing.
6751 */
6752 public interface OnLayoutChangeListener {
6753 /**
6754 * Called when the focus state of a view has changed.
6755 *
6756 * @param v The view whose state has changed.
6757 * @param left The new value of the view's left property.
6758 * @param top The new value of the view's top property.
6759 * @param right The new value of the view's right property.
6760 * @param bottom The new value of the view's bottom property.
6761 * @param oldLeft The previous value of the view's left property.
6762 * @param oldTop The previous value of the view's top property.
6763 * @param oldRight The previous value of the view's right property.
6764 * @param oldBottom The previous value of the view's bottom property.
6765 */
6766 void onLayoutChange(View v, int left, int top, int right, int bottom,
6767 int oldLeft, int oldTop, int oldRight, int oldBottom);
6768 }
6769
6770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 * This is called during layout when the size of this view has changed. If
6772 * you were just added to the view hierarchy, you're called with the old
6773 * values of 0.
6774 *
6775 * @param w Current width of this view.
6776 * @param h Current height of this view.
6777 * @param oldw Old width of this view.
6778 * @param oldh Old height of this view.
6779 */
6780 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6781 }
6782
6783 /**
6784 * Called by draw to draw the child views. This may be overridden
6785 * by derived classes to gain control just before its children are drawn
6786 * (but after its own view has been drawn).
6787 * @param canvas the canvas on which to draw the view
6788 */
6789 protected void dispatchDraw(Canvas canvas) {
6790 }
6791
6792 /**
6793 * Gets the parent of this view. Note that the parent is a
6794 * ViewParent and not necessarily a View.
6795 *
6796 * @return Parent of this view.
6797 */
6798 public final ViewParent getParent() {
6799 return mParent;
6800 }
6801
6802 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006803 * Set the horizontal scrolled position of your view. This will cause a call to
6804 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6805 * invalidated.
6806 * @param value the x position to scroll to
6807 */
6808 public void setScrollX(int value) {
6809 scrollTo(value, mScrollY);
6810 }
6811
6812 /**
6813 * Set the vertical scrolled position of your view. This will cause a call to
6814 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6815 * invalidated.
6816 * @param value the y position to scroll to
6817 */
6818 public void setScrollY(int value) {
6819 scrollTo(mScrollX, value);
6820 }
6821
6822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 * Return the scrolled left position of this view. This is the left edge of
6824 * the displayed part of your view. You do not need to draw any pixels
6825 * farther left, since those are outside of the frame of your view on
6826 * screen.
6827 *
6828 * @return The left edge of the displayed part of your view, in pixels.
6829 */
6830 public final int getScrollX() {
6831 return mScrollX;
6832 }
6833
6834 /**
6835 * Return the scrolled top position of this view. This is the top edge of
6836 * the displayed part of your view. You do not need to draw any pixels above
6837 * it, since those are outside of the frame of your view on screen.
6838 *
6839 * @return The top edge of the displayed part of your view, in pixels.
6840 */
6841 public final int getScrollY() {
6842 return mScrollY;
6843 }
6844
6845 /**
6846 * Return the width of the your view.
6847 *
6848 * @return The width of your view, in pixels.
6849 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006850 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 public final int getWidth() {
6852 return mRight - mLeft;
6853 }
6854
6855 /**
6856 * Return the height of your view.
6857 *
6858 * @return The height of your view, in pixels.
6859 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006860 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 public final int getHeight() {
6862 return mBottom - mTop;
6863 }
6864
6865 /**
6866 * Return the visible drawing bounds of your view. Fills in the output
6867 * rectangle with the values from getScrollX(), getScrollY(),
6868 * getWidth(), and getHeight().
6869 *
6870 * @param outRect The (scrolled) drawing bounds of the view.
6871 */
6872 public void getDrawingRect(Rect outRect) {
6873 outRect.left = mScrollX;
6874 outRect.top = mScrollY;
6875 outRect.right = mScrollX + (mRight - mLeft);
6876 outRect.bottom = mScrollY + (mBottom - mTop);
6877 }
6878
6879 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006880 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6881 * raw width component (that is the result is masked by
6882 * {@link #MEASURED_SIZE_MASK}).
6883 *
6884 * @return The raw measured width of this view.
6885 */
6886 public final int getMeasuredWidth() {
6887 return mMeasuredWidth & MEASURED_SIZE_MASK;
6888 }
6889
6890 /**
6891 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006892 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006893 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 * This should be used during measurement and layout calculations only. Use
6895 * {@link #getWidth()} to see how wide a view is after layout.
6896 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006897 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006899 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 return mMeasuredWidth;
6901 }
6902
6903 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006904 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6905 * raw width component (that is the result is masked by
6906 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006908 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 */
6910 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006911 return mMeasuredHeight & MEASURED_SIZE_MASK;
6912 }
6913
6914 /**
6915 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006916 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006917 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6918 * This should be used during measurement and layout calculations only. Use
6919 * {@link #getHeight()} to see how wide a view is after layout.
6920 *
6921 * @return The measured width of this view as a bit mask.
6922 */
6923 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 return mMeasuredHeight;
6925 }
6926
6927 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006928 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6929 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6930 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6931 * and the height component is at the shifted bits
6932 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6933 */
6934 public final int getMeasuredState() {
6935 return (mMeasuredWidth&MEASURED_STATE_MASK)
6936 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6937 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6938 }
6939
6940 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006941 * The transform matrix of this view, which is calculated based on the current
6942 * roation, scale, and pivot properties.
6943 *
6944 * @see #getRotation()
6945 * @see #getScaleX()
6946 * @see #getScaleY()
6947 * @see #getPivotX()
6948 * @see #getPivotY()
6949 * @return The current transform matrix for the view
6950 */
6951 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006952 if (mTransformationInfo != null) {
6953 updateMatrix();
6954 return mTransformationInfo.mMatrix;
6955 }
6956 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006957 }
6958
6959 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006960 * Utility function to determine if the value is far enough away from zero to be
6961 * considered non-zero.
6962 * @param value A floating point value to check for zero-ness
6963 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6964 */
6965 private static boolean nonzero(float value) {
6966 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6967 }
6968
6969 /**
Jeff Brown86671742010-09-30 20:00:15 -07006970 * Returns true if the transform matrix is the identity matrix.
6971 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006972 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006973 * @return True if the transform matrix is the identity matrix, false otherwise.
6974 */
Jeff Brown86671742010-09-30 20:00:15 -07006975 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006976 if (mTransformationInfo != null) {
6977 updateMatrix();
6978 return mTransformationInfo.mMatrixIsIdentity;
6979 }
6980 return true;
6981 }
6982
6983 void ensureTransformationInfo() {
6984 if (mTransformationInfo == null) {
6985 mTransformationInfo = new TransformationInfo();
6986 }
Jeff Brown86671742010-09-30 20:00:15 -07006987 }
6988
6989 /**
6990 * Recomputes the transform matrix if necessary.
6991 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006992 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006993 final TransformationInfo info = mTransformationInfo;
6994 if (info == null) {
6995 return;
6996 }
6997 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006998 // transform-related properties have changed since the last time someone
6999 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007000
7001 // Figure out if we need to update the pivot point
7002 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007003 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7004 info.mPrevWidth = mRight - mLeft;
7005 info.mPrevHeight = mBottom - mTop;
7006 info.mPivotX = info.mPrevWidth / 2f;
7007 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007008 }
7009 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007010 info.mMatrix.reset();
7011 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7012 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7013 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7014 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007015 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007016 if (info.mCamera == null) {
7017 info.mCamera = new Camera();
7018 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007019 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007020 info.mCamera.save();
7021 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7022 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7023 info.mCamera.getMatrix(info.matrix3D);
7024 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7025 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7026 info.mPivotY + info.mTranslationY);
7027 info.mMatrix.postConcat(info.matrix3D);
7028 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007029 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007030 info.mMatrixDirty = false;
7031 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7032 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007033 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007034 }
7035
7036 /**
7037 * Utility method to retrieve the inverse of the current mMatrix property.
7038 * We cache the matrix to avoid recalculating it when transform properties
7039 * have not changed.
7040 *
7041 * @return The inverse of the current matrix of this view.
7042 */
Jeff Brown86671742010-09-30 20:00:15 -07007043 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007044 final TransformationInfo info = mTransformationInfo;
7045 if (info != null) {
7046 updateMatrix();
7047 if (info.mInverseMatrixDirty) {
7048 if (info.mInverseMatrix == null) {
7049 info.mInverseMatrix = new Matrix();
7050 }
7051 info.mMatrix.invert(info.mInverseMatrix);
7052 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007053 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007054 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007055 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007056 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007057 }
7058
7059 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007060 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7061 * views are drawn) from the camera to this view. The camera's distance
7062 * affects 3D transformations, for instance rotations around the X and Y
7063 * axis. If the rotationX or rotationY properties are changed and this view is
7064 * large (more than half the size of the screen), it is recommended to always
7065 * use a camera distance that's greater than the height (X axis rotation) or
7066 * the width (Y axis rotation) of this view.</p>
7067 *
7068 * <p>The distance of the camera from the view plane can have an affect on the
7069 * perspective distortion of the view when it is rotated around the x or y axis.
7070 * For example, a large distance will result in a large viewing angle, and there
7071 * will not be much perspective distortion of the view as it rotates. A short
7072 * distance may cause much more perspective distortion upon rotation, and can
7073 * also result in some drawing artifacts if the rotated view ends up partially
7074 * behind the camera (which is why the recommendation is to use a distance at
7075 * least as far as the size of the view, if the view is to be rotated.)</p>
7076 *
7077 * <p>The distance is expressed in "depth pixels." The default distance depends
7078 * on the screen density. For instance, on a medium density display, the
7079 * default distance is 1280. On a high density display, the default distance
7080 * is 1920.</p>
7081 *
7082 * <p>If you want to specify a distance that leads to visually consistent
7083 * results across various densities, use the following formula:</p>
7084 * <pre>
7085 * float scale = context.getResources().getDisplayMetrics().density;
7086 * view.setCameraDistance(distance * scale);
7087 * </pre>
7088 *
7089 * <p>The density scale factor of a high density display is 1.5,
7090 * and 1920 = 1280 * 1.5.</p>
7091 *
7092 * @param distance The distance in "depth pixels", if negative the opposite
7093 * value is used
7094 *
7095 * @see #setRotationX(float)
7096 * @see #setRotationY(float)
7097 */
7098 public void setCameraDistance(float distance) {
7099 invalidateParentCaches();
7100 invalidate(false);
7101
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007102 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007103 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007104 final TransformationInfo info = mTransformationInfo;
7105 if (info.mCamera == null) {
7106 info.mCamera = new Camera();
7107 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007108 }
7109
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007110 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7111 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007112
7113 invalidate(false);
7114 }
7115
7116 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007117 * The degrees that the view is rotated around the pivot point.
7118 *
Romain Guya5364ee2011-02-24 14:46:04 -08007119 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007120 * @see #getPivotX()
7121 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007122 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007123 * @return The degrees of rotation.
7124 */
7125 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007126 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007127 }
7128
7129 /**
Chet Haase897247b2010-09-09 14:54:47 -07007130 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7131 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007132 *
7133 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007134 *
7135 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007136 * @see #getPivotX()
7137 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007138 * @see #setRotationX(float)
7139 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007140 *
7141 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007142 */
7143 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007144 ensureTransformationInfo();
7145 final TransformationInfo info = mTransformationInfo;
7146 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007147 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007148 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007149 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007150 info.mRotation = rotation;
7151 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007153 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007154 }
7155 }
7156
7157 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007158 * The degrees that the view is rotated around the vertical axis through the pivot point.
7159 *
7160 * @see #getPivotX()
7161 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007162 * @see #setRotationY(float)
7163 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007164 * @return The degrees of Y rotation.
7165 */
7166 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007167 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007168 }
7169
7170 /**
Chet Haase897247b2010-09-09 14:54:47 -07007171 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7172 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7173 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007174 *
7175 * When rotating large views, it is recommended to adjust the camera distance
7176 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007177 *
7178 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007179 *
7180 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007181 * @see #getPivotX()
7182 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007183 * @see #setRotation(float)
7184 * @see #setRotationX(float)
7185 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007186 *
7187 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007188 */
7189 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007190 ensureTransformationInfo();
7191 final TransformationInfo info = mTransformationInfo;
7192 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007193 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007194 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007195 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007196 info.mRotationY = rotationY;
7197 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007198 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007199 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007200 }
7201 }
7202
7203 /**
7204 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7205 *
7206 * @see #getPivotX()
7207 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007208 * @see #setRotationX(float)
7209 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007210 * @return The degrees of X rotation.
7211 */
7212 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007213 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007214 }
7215
7216 /**
Chet Haase897247b2010-09-09 14:54:47 -07007217 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7218 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7219 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007220 *
7221 * When rotating large views, it is recommended to adjust the camera distance
7222 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007223 *
7224 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007225 *
7226 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007227 * @see #getPivotX()
7228 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007229 * @see #setRotation(float)
7230 * @see #setRotationY(float)
7231 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007232 *
7233 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007234 */
7235 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007236 ensureTransformationInfo();
7237 final TransformationInfo info = mTransformationInfo;
7238 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007239 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007240 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007241 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007242 info.mRotationX = rotationX;
7243 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007244 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007245 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007246 }
7247 }
7248
7249 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007250 * The amount that the view is scaled in x around the pivot point, as a proportion of
7251 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7252 *
Joe Onorato93162322010-09-16 15:42:01 -04007253 * <p>By default, this is 1.0f.
7254 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007255 * @see #getPivotX()
7256 * @see #getPivotY()
7257 * @return The scaling factor.
7258 */
7259 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007260 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007261 }
7262
7263 /**
7264 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7265 * the view's unscaled width. A value of 1 means that no scaling is applied.
7266 *
7267 * @param scaleX The scaling factor.
7268 * @see #getPivotX()
7269 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007270 *
7271 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007272 */
7273 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007274 ensureTransformationInfo();
7275 final TransformationInfo info = mTransformationInfo;
7276 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007277 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007278 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007279 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007280 info.mScaleX = scaleX;
7281 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007282 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007283 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007284 }
7285 }
7286
7287 /**
7288 * The amount that the view is scaled in y around the pivot point, as a proportion of
7289 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7290 *
Joe Onorato93162322010-09-16 15:42:01 -04007291 * <p>By default, this is 1.0f.
7292 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007293 * @see #getPivotX()
7294 * @see #getPivotY()
7295 * @return The scaling factor.
7296 */
7297 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007298 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007299 }
7300
7301 /**
7302 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7303 * the view's unscaled width. A value of 1 means that no scaling is applied.
7304 *
7305 * @param scaleY The scaling factor.
7306 * @see #getPivotX()
7307 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007308 *
7309 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007310 */
7311 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007312 ensureTransformationInfo();
7313 final TransformationInfo info = mTransformationInfo;
7314 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007315 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007316 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007317 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007318 info.mScaleY = scaleY;
7319 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007320 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007321 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007322 }
7323 }
7324
7325 /**
7326 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7327 * and {@link #setScaleX(float) scaled}.
7328 *
7329 * @see #getRotation()
7330 * @see #getScaleX()
7331 * @see #getScaleY()
7332 * @see #getPivotY()
7333 * @return The x location of the pivot point.
7334 */
7335 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007336 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007337 }
7338
7339 /**
7340 * Sets the x location of the point around which the view is
7341 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007342 * By default, the pivot point is centered on the object.
7343 * Setting this property disables this behavior and causes the view to use only the
7344 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007345 *
7346 * @param pivotX The x location of the pivot point.
7347 * @see #getRotation()
7348 * @see #getScaleX()
7349 * @see #getScaleY()
7350 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007351 *
7352 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007353 */
7354 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007355 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007356 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007357 final TransformationInfo info = mTransformationInfo;
7358 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007359 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007360 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007361 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007362 info.mPivotX = pivotX;
7363 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007364 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007365 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007366 }
7367 }
7368
7369 /**
7370 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7371 * and {@link #setScaleY(float) scaled}.
7372 *
7373 * @see #getRotation()
7374 * @see #getScaleX()
7375 * @see #getScaleY()
7376 * @see #getPivotY()
7377 * @return The y location of the pivot point.
7378 */
7379 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007380 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007381 }
7382
7383 /**
7384 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7386 * Setting this property disables this behavior and causes the view to use only the
7387 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007388 *
7389 * @param pivotY The y location of the pivot point.
7390 * @see #getRotation()
7391 * @see #getScaleX()
7392 * @see #getScaleY()
7393 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007394 *
7395 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007396 */
7397 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007398 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007399 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007400 final TransformationInfo info = mTransformationInfo;
7401 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007402 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007404 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 info.mPivotY = pivotY;
7406 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007407 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007408 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007409 }
7410 }
7411
7412 /**
7413 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7414 * completely transparent and 1 means the view is completely opaque.
7415 *
Joe Onorato93162322010-09-16 15:42:01 -04007416 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007417 * @return The opacity of the view.
7418 */
7419 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007420 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007421 }
7422
7423 /**
Romain Guy171c5922011-01-06 10:04:23 -08007424 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7425 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007426 *
Romain Guy171c5922011-01-06 10:04:23 -08007427 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7428 * responsible for applying the opacity itself. Otherwise, calling this method is
7429 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007430 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007431 *
7432 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007433 *
Joe Malin32736f02011-01-19 16:14:20 -08007434 * @see #setLayerType(int, android.graphics.Paint)
7435 *
Chet Haase73066682010-11-29 15:55:32 -08007436 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007437 */
7438 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007439 ensureTransformationInfo();
7440 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007441 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007442 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007443 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007444 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007445 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007446 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007447 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007448 invalidate(false);
7449 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007450 }
7451
7452 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007453 * Faster version of setAlpha() which performs the same steps except there are
7454 * no calls to invalidate(). The caller of this function should perform proper invalidation
7455 * on the parent and this object. The return value indicates whether the subclass handles
7456 * alpha (the return value for onSetAlpha()).
7457 *
7458 * @param alpha The new value for the alpha property
7459 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7460 */
7461 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007462 ensureTransformationInfo();
7463 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007464 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7465 if (subclassHandlesAlpha) {
7466 mPrivateFlags |= ALPHA_SET;
7467 } else {
7468 mPrivateFlags &= ~ALPHA_SET;
7469 }
7470 return subclassHandlesAlpha;
7471 }
7472
7473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 * Top position of this view relative to its parent.
7475 *
7476 * @return The top of this view, in pixels.
7477 */
7478 @ViewDebug.CapturedViewProperty
7479 public final int getTop() {
7480 return mTop;
7481 }
7482
7483 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007484 * Sets the top position of this view relative to its parent. This method is meant to be called
7485 * by the layout system and should not generally be called otherwise, because the property
7486 * may be changed at any time by the layout.
7487 *
7488 * @param top The top of this view, in pixels.
7489 */
7490 public final void setTop(int top) {
7491 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007492 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007493 final boolean matrixIsIdentity = mTransformationInfo == null
7494 || mTransformationInfo.mMatrixIsIdentity;
7495 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007496 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007497 int minTop;
7498 int yLoc;
7499 if (top < mTop) {
7500 minTop = top;
7501 yLoc = top - mTop;
7502 } else {
7503 minTop = mTop;
7504 yLoc = 0;
7505 }
Chet Haasee9140a72011-02-16 16:23:29 -08007506 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007507 }
7508 } else {
7509 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007510 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007511 }
7512
Chet Haaseed032702010-10-01 14:05:54 -07007513 int width = mRight - mLeft;
7514 int oldHeight = mBottom - mTop;
7515
Chet Haase21cd1382010-09-01 17:42:29 -07007516 mTop = top;
7517
Chet Haaseed032702010-10-01 14:05:54 -07007518 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7519
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007520 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007521 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7522 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007523 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007524 }
Chet Haase21cd1382010-09-01 17:42:29 -07007525 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007526 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007527 }
Chet Haase55dbb652010-12-21 20:15:08 -08007528 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007529 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007530 }
7531 }
7532
7533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 * Bottom position of this view relative to its parent.
7535 *
7536 * @return The bottom of this view, in pixels.
7537 */
7538 @ViewDebug.CapturedViewProperty
7539 public final int getBottom() {
7540 return mBottom;
7541 }
7542
7543 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007544 * True if this view has changed since the last time being drawn.
7545 *
7546 * @return The dirty state of this view.
7547 */
7548 public boolean isDirty() {
7549 return (mPrivateFlags & DIRTY_MASK) != 0;
7550 }
7551
7552 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007553 * Sets the bottom position of this view relative to its parent. This method is meant to be
7554 * called by the layout system and should not generally be called otherwise, because the
7555 * property may be changed at any time by the layout.
7556 *
7557 * @param bottom The bottom of this view, in pixels.
7558 */
7559 public final void setBottom(int bottom) {
7560 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007561 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007562 final boolean matrixIsIdentity = mTransformationInfo == null
7563 || mTransformationInfo.mMatrixIsIdentity;
7564 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007565 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007566 int maxBottom;
7567 if (bottom < mBottom) {
7568 maxBottom = mBottom;
7569 } else {
7570 maxBottom = bottom;
7571 }
Chet Haasee9140a72011-02-16 16:23:29 -08007572 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007573 }
7574 } else {
7575 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007576 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007577 }
7578
Chet Haaseed032702010-10-01 14:05:54 -07007579 int width = mRight - mLeft;
7580 int oldHeight = mBottom - mTop;
7581
Chet Haase21cd1382010-09-01 17:42:29 -07007582 mBottom = bottom;
7583
Chet Haaseed032702010-10-01 14:05:54 -07007584 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007586 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007587 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7588 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007589 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007590 }
Chet Haase21cd1382010-09-01 17:42:29 -07007591 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007592 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007593 }
Chet Haase55dbb652010-12-21 20:15:08 -08007594 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007595 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007596 }
7597 }
7598
7599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 * Left position of this view relative to its parent.
7601 *
7602 * @return The left edge of this view, in pixels.
7603 */
7604 @ViewDebug.CapturedViewProperty
7605 public final int getLeft() {
7606 return mLeft;
7607 }
7608
7609 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007610 * Sets the left position of this view relative to its parent. This method is meant to be called
7611 * by the layout system and should not generally be called otherwise, because the property
7612 * may be changed at any time by the layout.
7613 *
7614 * @param left The bottom of this view, in pixels.
7615 */
7616 public final void setLeft(int left) {
7617 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007618 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007619 final boolean matrixIsIdentity = mTransformationInfo == null
7620 || mTransformationInfo.mMatrixIsIdentity;
7621 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007622 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007623 int minLeft;
7624 int xLoc;
7625 if (left < mLeft) {
7626 minLeft = left;
7627 xLoc = left - mLeft;
7628 } else {
7629 minLeft = mLeft;
7630 xLoc = 0;
7631 }
Chet Haasee9140a72011-02-16 16:23:29 -08007632 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007633 }
7634 } else {
7635 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007636 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007637 }
7638
Chet Haaseed032702010-10-01 14:05:54 -07007639 int oldWidth = mRight - mLeft;
7640 int height = mBottom - mTop;
7641
Chet Haase21cd1382010-09-01 17:42:29 -07007642 mLeft = left;
7643
Chet Haaseed032702010-10-01 14:05:54 -07007644 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7645
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007646 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007647 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7648 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007649 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007650 }
Chet Haase21cd1382010-09-01 17:42:29 -07007651 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007652 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007653 }
Chet Haase55dbb652010-12-21 20:15:08 -08007654 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007655 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007656 }
7657 }
7658
7659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 * Right position of this view relative to its parent.
7661 *
7662 * @return The right edge of this view, in pixels.
7663 */
7664 @ViewDebug.CapturedViewProperty
7665 public final int getRight() {
7666 return mRight;
7667 }
7668
7669 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007670 * Sets the right position of this view relative to its parent. This method is meant to be called
7671 * by the layout system and should not generally be called otherwise, because the property
7672 * may be changed at any time by the layout.
7673 *
7674 * @param right The bottom of this view, in pixels.
7675 */
7676 public final void setRight(int right) {
7677 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007678 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007679 final boolean matrixIsIdentity = mTransformationInfo == null
7680 || mTransformationInfo.mMatrixIsIdentity;
7681 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007682 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007683 int maxRight;
7684 if (right < mRight) {
7685 maxRight = mRight;
7686 } else {
7687 maxRight = right;
7688 }
Chet Haasee9140a72011-02-16 16:23:29 -08007689 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007690 }
7691 } else {
7692 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007693 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007694 }
7695
Chet Haaseed032702010-10-01 14:05:54 -07007696 int oldWidth = mRight - mLeft;
7697 int height = mBottom - mTop;
7698
Chet Haase21cd1382010-09-01 17:42:29 -07007699 mRight = right;
7700
Chet Haaseed032702010-10-01 14:05:54 -07007701 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7702
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007703 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007704 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7705 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007706 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007707 }
Chet Haase21cd1382010-09-01 17:42:29 -07007708 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007709 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007710 }
Chet Haase55dbb652010-12-21 20:15:08 -08007711 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007712 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007713 }
7714 }
7715
7716 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007717 * The visual x position of this view, in pixels. This is equivalent to the
7718 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007719 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007720 *
Chet Haasedf030d22010-07-30 17:22:38 -07007721 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007722 */
Chet Haasedf030d22010-07-30 17:22:38 -07007723 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007724 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007725 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007726
Chet Haasedf030d22010-07-30 17:22:38 -07007727 /**
7728 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7729 * {@link #setTranslationX(float) translationX} property to be the difference between
7730 * the x value passed in and the current {@link #getLeft() left} property.
7731 *
7732 * @param x The visual x position of this view, in pixels.
7733 */
7734 public void setX(float x) {
7735 setTranslationX(x - mLeft);
7736 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007737
Chet Haasedf030d22010-07-30 17:22:38 -07007738 /**
7739 * The visual y position of this view, in pixels. This is equivalent to the
7740 * {@link #setTranslationY(float) translationY} property plus the current
7741 * {@link #getTop() top} property.
7742 *
7743 * @return The visual y position of this view, in pixels.
7744 */
7745 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007746 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007747 }
7748
7749 /**
7750 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7751 * {@link #setTranslationY(float) translationY} property to be the difference between
7752 * the y value passed in and the current {@link #getTop() top} property.
7753 *
7754 * @param y The visual y position of this view, in pixels.
7755 */
7756 public void setY(float y) {
7757 setTranslationY(y - mTop);
7758 }
7759
7760
7761 /**
7762 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7763 * This position is post-layout, in addition to wherever the object's
7764 * layout placed it.
7765 *
7766 * @return The horizontal position of this view relative to its left position, in pixels.
7767 */
7768 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007769 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007770 }
7771
7772 /**
7773 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7774 * This effectively positions the object post-layout, in addition to wherever the object's
7775 * layout placed it.
7776 *
7777 * @param translationX The horizontal position of this view relative to its left position,
7778 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007779 *
7780 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007781 */
7782 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007783 ensureTransformationInfo();
7784 final TransformationInfo info = mTransformationInfo;
7785 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007786 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007787 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007788 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007789 info.mTranslationX = translationX;
7790 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007791 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007792 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007793 }
7794 }
7795
7796 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007797 * The horizontal location of this view relative to its {@link #getTop() top} position.
7798 * This position is post-layout, in addition to wherever the object's
7799 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007800 *
Chet Haasedf030d22010-07-30 17:22:38 -07007801 * @return The vertical position of this view relative to its top position,
7802 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007803 */
Chet Haasedf030d22010-07-30 17:22:38 -07007804 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007805 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007806 }
7807
7808 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007809 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7810 * This effectively positions the object post-layout, in addition to wherever the object's
7811 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007812 *
Chet Haasedf030d22010-07-30 17:22:38 -07007813 * @param translationY The vertical position of this view relative to its top position,
7814 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007815 *
7816 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007817 */
Chet Haasedf030d22010-07-30 17:22:38 -07007818 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007819 ensureTransformationInfo();
7820 final TransformationInfo info = mTransformationInfo;
7821 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007822 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007823 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007824 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007825 info.mTranslationY = translationY;
7826 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007827 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007828 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007829 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007830 }
7831
7832 /**
Romain Guyda489792011-02-03 01:05:15 -08007833 * @hide
7834 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007835 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007836 ensureTransformationInfo();
7837 final TransformationInfo info = mTransformationInfo;
7838 info.mTranslationX = x;
7839 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007840 }
7841
7842 /**
7843 * @hide
7844 */
7845 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007846 ensureTransformationInfo();
7847 final TransformationInfo info = mTransformationInfo;
7848 info.mTranslationY = y;
7849 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007850 }
7851
7852 /**
7853 * @hide
7854 */
Romain Guyda489792011-02-03 01:05:15 -08007855 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007856 ensureTransformationInfo();
7857 final TransformationInfo info = mTransformationInfo;
7858 info.mTranslationX = x - mLeft;
7859 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007860 }
7861
7862 /**
7863 * @hide
7864 */
7865 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007866 ensureTransformationInfo();
7867 final TransformationInfo info = mTransformationInfo;
7868 info.mTranslationY = y - mTop;
7869 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007870 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007871
Romain Guyda489792011-02-03 01:05:15 -08007872 /**
7873 * @hide
7874 */
7875 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007876 ensureTransformationInfo();
7877 final TransformationInfo info = mTransformationInfo;
7878 info.mScaleX = x;
7879 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007880 }
7881
7882 /**
7883 * @hide
7884 */
7885 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007886 ensureTransformationInfo();
7887 final TransformationInfo info = mTransformationInfo;
7888 info.mScaleY = y;
7889 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007890 }
7891
7892 /**
7893 * @hide
7894 */
7895 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007896 ensureTransformationInfo();
7897 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007898 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007899
Romain Guyda489792011-02-03 01:05:15 -08007900 /**
7901 * @hide
7902 */
7903 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007904 ensureTransformationInfo();
7905 final TransformationInfo info = mTransformationInfo;
7906 info.mRotationY = y;
7907 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007908 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007909
Romain Guyda489792011-02-03 01:05:15 -08007910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007911 * Hit rectangle in parent's coordinates
7912 *
7913 * @param outRect The hit rectangle of the view.
7914 */
7915 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007916 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007917 final TransformationInfo info = mTransformationInfo;
7918 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007919 outRect.set(mLeft, mTop, mRight, mBottom);
7920 } else {
7921 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007922 tmpRect.set(-info.mPivotX, -info.mPivotY,
7923 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7924 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007925 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7926 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007927 }
7928 }
7929
7930 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007931 * Determines whether the given point, in local coordinates is inside the view.
7932 */
7933 /*package*/ final boolean pointInView(float localX, float localY) {
7934 return localX >= 0 && localX < (mRight - mLeft)
7935 && localY >= 0 && localY < (mBottom - mTop);
7936 }
7937
7938 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007939 * Utility method to determine whether the given point, in local coordinates,
7940 * is inside the view, where the area of the view is expanded by the slop factor.
7941 * This method is called while processing touch-move events to determine if the event
7942 * is still within the view.
7943 */
7944 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007945 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007946 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 }
7948
7949 /**
7950 * When a view has focus and the user navigates away from it, the next view is searched for
7951 * starting from the rectangle filled in by this method.
7952 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007953 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7954 * of the view. However, if your view maintains some idea of internal selection,
7955 * such as a cursor, or a selected row or column, you should override this method and
7956 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 *
7958 * @param r The rectangle to fill in, in this view's coordinates.
7959 */
7960 public void getFocusedRect(Rect r) {
7961 getDrawingRect(r);
7962 }
7963
7964 /**
7965 * If some part of this view is not clipped by any of its parents, then
7966 * return that area in r in global (root) coordinates. To convert r to local
7967 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7968 * -globalOffset.y)) If the view is completely clipped or translated out,
7969 * return false.
7970 *
7971 * @param r If true is returned, r holds the global coordinates of the
7972 * visible portion of this view.
7973 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7974 * between this view and its root. globalOffet may be null.
7975 * @return true if r is non-empty (i.e. part of the view is visible at the
7976 * root level.
7977 */
7978 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7979 int width = mRight - mLeft;
7980 int height = mBottom - mTop;
7981 if (width > 0 && height > 0) {
7982 r.set(0, 0, width, height);
7983 if (globalOffset != null) {
7984 globalOffset.set(-mScrollX, -mScrollY);
7985 }
7986 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7987 }
7988 return false;
7989 }
7990
7991 public final boolean getGlobalVisibleRect(Rect r) {
7992 return getGlobalVisibleRect(r, null);
7993 }
7994
7995 public final boolean getLocalVisibleRect(Rect r) {
7996 Point offset = new Point();
7997 if (getGlobalVisibleRect(r, offset)) {
7998 r.offset(-offset.x, -offset.y); // make r local
7999 return true;
8000 }
8001 return false;
8002 }
8003
8004 /**
8005 * Offset this view's vertical location by the specified number of pixels.
8006 *
8007 * @param offset the number of pixels to offset the view by
8008 */
8009 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008010 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008011 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008012 final boolean matrixIsIdentity = mTransformationInfo == null
8013 || mTransformationInfo.mMatrixIsIdentity;
8014 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008015 final ViewParent p = mParent;
8016 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008018 int minTop;
8019 int maxBottom;
8020 int yLoc;
8021 if (offset < 0) {
8022 minTop = mTop + offset;
8023 maxBottom = mBottom;
8024 yLoc = offset;
8025 } else {
8026 minTop = mTop;
8027 maxBottom = mBottom + offset;
8028 yLoc = 0;
8029 }
8030 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8031 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 }
8033 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008034 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008035 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008036
Chet Haasec3aa3612010-06-17 08:50:37 -07008037 mTop += offset;
8038 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008039
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008040 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008041 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008042 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008043 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008044 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 }
8047
8048 /**
8049 * Offset this view's horizontal location by the specified amount of pixels.
8050 *
8051 * @param offset the numer of pixels to offset the view by
8052 */
8053 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008054 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008055 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008056 final boolean matrixIsIdentity = mTransformationInfo == null
8057 || mTransformationInfo.mMatrixIsIdentity;
8058 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008059 final ViewParent p = mParent;
8060 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008061 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008062 int minLeft;
8063 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008064 if (offset < 0) {
8065 minLeft = mLeft + offset;
8066 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008067 } else {
8068 minLeft = mLeft;
8069 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008070 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008071 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008072 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008073 }
8074 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008075 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008076 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008077
Chet Haasec3aa3612010-06-17 08:50:37 -07008078 mLeft += offset;
8079 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008080
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008081 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008082 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008083 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008084 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008085 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 }
8088
8089 /**
8090 * Get the LayoutParams associated with this view. All views should have
8091 * layout parameters. These supply parameters to the <i>parent</i> of this
8092 * view specifying how it should be arranged. There are many subclasses of
8093 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8094 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008095 *
8096 * This method may return null if this View is not attached to a parent
8097 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8098 * was not invoked successfully. When a View is attached to a parent
8099 * ViewGroup, this method must not return null.
8100 *
8101 * @return The LayoutParams associated with this view, or null if no
8102 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008104 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 public ViewGroup.LayoutParams getLayoutParams() {
8106 return mLayoutParams;
8107 }
8108
8109 /**
8110 * Set the layout parameters associated with this view. These supply
8111 * parameters to the <i>parent</i> of this view specifying how it should be
8112 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8113 * correspond to the different subclasses of ViewGroup that are responsible
8114 * for arranging their children.
8115 *
Romain Guy01c174b2011-02-22 11:51:06 -08008116 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 */
8118 public void setLayoutParams(ViewGroup.LayoutParams params) {
8119 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008120 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122 mLayoutParams = params;
8123 requestLayout();
8124 }
8125
8126 /**
8127 * Set the scrolled position of your view. This will cause a call to
8128 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8129 * invalidated.
8130 * @param x the x position to scroll to
8131 * @param y the y position to scroll to
8132 */
8133 public void scrollTo(int x, int y) {
8134 if (mScrollX != x || mScrollY != y) {
8135 int oldX = mScrollX;
8136 int oldY = mScrollY;
8137 mScrollX = x;
8138 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008139 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008141 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008142 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 }
8145 }
8146
8147 /**
8148 * Move the scrolled position of your view. This will cause a call to
8149 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8150 * invalidated.
8151 * @param x the amount of pixels to scroll by horizontally
8152 * @param y the amount of pixels to scroll by vertically
8153 */
8154 public void scrollBy(int x, int y) {
8155 scrollTo(mScrollX + x, mScrollY + y);
8156 }
8157
8158 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008159 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8160 * animation to fade the scrollbars out after a default delay. If a subclass
8161 * provides animated scrolling, the start delay should equal the duration
8162 * of the scrolling animation.</p>
8163 *
8164 * <p>The animation starts only if at least one of the scrollbars is
8165 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8166 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8167 * this method returns true, and false otherwise. If the animation is
8168 * started, this method calls {@link #invalidate()}; in that case the
8169 * caller should not call {@link #invalidate()}.</p>
8170 *
8171 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008172 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008173 *
8174 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8175 * and {@link #scrollTo(int, int)}.</p>
8176 *
8177 * @return true if the animation is played, false otherwise
8178 *
8179 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008180 * @see #scrollBy(int, int)
8181 * @see #scrollTo(int, int)
8182 * @see #isHorizontalScrollBarEnabled()
8183 * @see #isVerticalScrollBarEnabled()
8184 * @see #setHorizontalScrollBarEnabled(boolean)
8185 * @see #setVerticalScrollBarEnabled(boolean)
8186 */
8187 protected boolean awakenScrollBars() {
8188 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008189 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008190 }
8191
8192 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008193 * Trigger the scrollbars to draw.
8194 * This method differs from awakenScrollBars() only in its default duration.
8195 * initialAwakenScrollBars() will show the scroll bars for longer than
8196 * usual to give the user more of a chance to notice them.
8197 *
8198 * @return true if the animation is played, false otherwise.
8199 */
8200 private boolean initialAwakenScrollBars() {
8201 return mScrollCache != null &&
8202 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8203 }
8204
8205 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008206 * <p>
8207 * Trigger the scrollbars to draw. When invoked this method starts an
8208 * animation to fade the scrollbars out after a fixed delay. If a subclass
8209 * provides animated scrolling, the start delay should equal the duration of
8210 * the scrolling animation.
8211 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008212 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008213 * <p>
8214 * The animation starts only if at least one of the scrollbars is enabled,
8215 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8216 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8217 * this method returns true, and false otherwise. If the animation is
8218 * started, this method calls {@link #invalidate()}; in that case the caller
8219 * should not call {@link #invalidate()}.
8220 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008221 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008222 * <p>
8223 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008224 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008225 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008226 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008227 * @param startDelay the delay, in milliseconds, after which the animation
8228 * should start; when the delay is 0, the animation starts
8229 * immediately
8230 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008231 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008232 * @see #scrollBy(int, int)
8233 * @see #scrollTo(int, int)
8234 * @see #isHorizontalScrollBarEnabled()
8235 * @see #isVerticalScrollBarEnabled()
8236 * @see #setHorizontalScrollBarEnabled(boolean)
8237 * @see #setVerticalScrollBarEnabled(boolean)
8238 */
8239 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008240 return awakenScrollBars(startDelay, true);
8241 }
Joe Malin32736f02011-01-19 16:14:20 -08008242
Mike Cleron290947b2009-09-29 18:34:32 -07008243 /**
8244 * <p>
8245 * Trigger the scrollbars to draw. When invoked this method starts an
8246 * animation to fade the scrollbars out after a fixed delay. If a subclass
8247 * provides animated scrolling, the start delay should equal the duration of
8248 * the scrolling animation.
8249 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008250 *
Mike Cleron290947b2009-09-29 18:34:32 -07008251 * <p>
8252 * The animation starts only if at least one of the scrollbars is enabled,
8253 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8254 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8255 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008256 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008257 * is set to true; in that case the caller
8258 * should not call {@link #invalidate()}.
8259 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008260 *
Mike Cleron290947b2009-09-29 18:34:32 -07008261 * <p>
8262 * This method should be invoked everytime a subclass directly updates the
8263 * scroll parameters.
8264 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008265 *
Mike Cleron290947b2009-09-29 18:34:32 -07008266 * @param startDelay the delay, in milliseconds, after which the animation
8267 * should start; when the delay is 0, the animation starts
8268 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008269 *
Mike Cleron290947b2009-09-29 18:34:32 -07008270 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008271 *
Mike Cleron290947b2009-09-29 18:34:32 -07008272 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008273 *
Mike Cleron290947b2009-09-29 18:34:32 -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(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008282 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008283
Mike Cleronf116bf82009-09-27 19:14:12 -07008284 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8285 return false;
8286 }
8287
8288 if (scrollCache.scrollBar == null) {
8289 scrollCache.scrollBar = new ScrollBarDrawable();
8290 }
8291
8292 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8293
Mike Cleron290947b2009-09-29 18:34:32 -07008294 if (invalidate) {
8295 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008296 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008297 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008298
8299 if (scrollCache.state == ScrollabilityCache.OFF) {
8300 // FIXME: this is copied from WindowManagerService.
8301 // We should get this value from the system when it
8302 // is possible to do so.
8303 final int KEY_REPEAT_FIRST_DELAY = 750;
8304 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8305 }
8306
8307 // Tell mScrollCache when we should start fading. This may
8308 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008309 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008310 scrollCache.fadeStartTime = fadeStartTime;
8311 scrollCache.state = ScrollabilityCache.ON;
8312
8313 // Schedule our fader to run, unscheduling any old ones first
8314 if (mAttachInfo != null) {
8315 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8316 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8317 }
8318
8319 return true;
8320 }
8321
8322 return false;
8323 }
8324
8325 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008326 * Do not invalidate views which are not visible and which are not running an animation. They
8327 * will not get drawn and they should not set dirty flags as if they will be drawn
8328 */
8329 private boolean skipInvalidate() {
8330 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8331 (!(mParent instanceof ViewGroup) ||
8332 !((ViewGroup) mParent).isViewTransitioning(this));
8333 }
8334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008336 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8337 * in the future. This must be called from a UI thread. To call from a non-UI
8338 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 *
8340 * WARNING: This method is destructive to dirty.
8341 * @param dirty the rectangle representing the bounds of the dirty region
8342 */
8343 public void invalidate(Rect dirty) {
8344 if (ViewDebug.TRACE_HIERARCHY) {
8345 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8346 }
8347
Chet Haaseaceafe62011-08-26 15:44:33 -07008348 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008349 return;
8350 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008351 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008352 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8353 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008355 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008356 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 final ViewParent p = mParent;
8358 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008359 //noinspection PointlessBooleanExpression,ConstantConditions
8360 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8361 if (p != null && ai != null && ai.mHardwareAccelerated) {
8362 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008363 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008364 p.invalidateChild(this, null);
8365 return;
8366 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 if (p != null && ai != null) {
8369 final int scrollX = mScrollX;
8370 final int scrollY = mScrollY;
8371 final Rect r = ai.mTmpInvalRect;
8372 r.set(dirty.left - scrollX, dirty.top - scrollY,
8373 dirty.right - scrollX, dirty.bottom - scrollY);
8374 mParent.invalidateChild(this, r);
8375 }
8376 }
8377 }
8378
8379 /**
8380 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8381 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008382 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8383 * will be called at some point in the future. This must be called from
8384 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 * @param l the left position of the dirty region
8386 * @param t the top position of the dirty region
8387 * @param r the right position of the dirty region
8388 * @param b the bottom position of the dirty region
8389 */
8390 public void invalidate(int l, int t, int r, int b) {
8391 if (ViewDebug.TRACE_HIERARCHY) {
8392 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8393 }
8394
Chet Haaseaceafe62011-08-26 15:44:33 -07008395 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008396 return;
8397 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008398 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008399 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8400 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008402 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008403 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404 final ViewParent p = mParent;
8405 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008406 //noinspection PointlessBooleanExpression,ConstantConditions
8407 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8408 if (p != null && ai != null && ai.mHardwareAccelerated) {
8409 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008410 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008411 p.invalidateChild(this, null);
8412 return;
8413 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008415 if (p != null && ai != null && l < r && t < b) {
8416 final int scrollX = mScrollX;
8417 final int scrollY = mScrollY;
8418 final Rect tmpr = ai.mTmpInvalRect;
8419 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8420 p.invalidateChild(this, tmpr);
8421 }
8422 }
8423 }
8424
8425 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008426 * Invalidate the whole view. If the view is visible,
8427 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8428 * the future. This must be called from a UI thread. To call from a non-UI thread,
8429 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 */
8431 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008432 invalidate(true);
8433 }
Joe Malin32736f02011-01-19 16:14:20 -08008434
Chet Haaseed032702010-10-01 14:05:54 -07008435 /**
8436 * This is where the invalidate() work actually happens. A full invalidate()
8437 * causes the drawing cache to be invalidated, but this function can be called with
8438 * invalidateCache set to false to skip that invalidation step for cases that do not
8439 * need it (for example, a component that remains at the same dimensions with the same
8440 * content).
8441 *
8442 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8443 * well. This is usually true for a full invalidate, but may be set to false if the
8444 * View's contents or dimensions have not changed.
8445 */
Romain Guy849d0a32011-02-01 17:20:48 -08008446 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 if (ViewDebug.TRACE_HIERARCHY) {
8448 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8449 }
8450
Chet Haaseaceafe62011-08-26 15:44:33 -07008451 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008452 return;
8453 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008454 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008455 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008456 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8457 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008458 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008459 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008460 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008461 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008462 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008465 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008466 //noinspection PointlessBooleanExpression,ConstantConditions
8467 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8468 if (p != null && ai != null && ai.mHardwareAccelerated) {
8469 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008470 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008471 p.invalidateChild(this, null);
8472 return;
8473 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008474 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 if (p != null && ai != null) {
8477 final Rect r = ai.mTmpInvalRect;
8478 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8479 // Don't call invalidate -- we don't want to internally scroll
8480 // our own bounds
8481 p.invalidateChild(this, r);
8482 }
8483 }
8484 }
8485
8486 /**
Romain Guyda489792011-02-03 01:05:15 -08008487 * @hide
8488 */
8489 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008490 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008491 return;
8492 }
Romain Guyda489792011-02-03 01:05:15 -08008493 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8494 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8495 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8496 if (mParent instanceof View) {
8497 ((View) mParent).mPrivateFlags |= INVALIDATED;
8498 }
8499 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008500 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008501 mPrivateFlags |= INVALIDATED;
8502 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008503 if (mParent != null && mAttachInfo != null) {
8504 if (mAttachInfo.mHardwareAccelerated) {
8505 mParent.invalidateChild(this, null);
8506 } else {
8507 final Rect r = mAttachInfo.mTmpInvalRect;
8508 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8509 // Don't call invalidate -- we don't want to internally scroll
8510 // our own bounds
8511 mParent.invalidateChild(this, r);
8512 }
Romain Guyda489792011-02-03 01:05:15 -08008513 }
8514 }
8515 }
8516
8517 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008518 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008519 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8520 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008521 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8522 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008523 *
8524 * @hide
8525 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008526 protected void invalidateParentCaches() {
8527 if (mParent instanceof View) {
8528 ((View) mParent).mPrivateFlags |= INVALIDATED;
8529 }
8530 }
Joe Malin32736f02011-01-19 16:14:20 -08008531
Romain Guy0fd89bf2011-01-26 15:41:30 -08008532 /**
8533 * Used to indicate that the parent of this view should be invalidated. This functionality
8534 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8535 * which is necessary when various parent-managed properties of the view change, such as
8536 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8537 * an invalidation event to the parent.
8538 *
8539 * @hide
8540 */
8541 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008542 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008543 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008544 }
8545 }
8546
8547 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008548 * Indicates whether this View is opaque. An opaque View guarantees that it will
8549 * draw all the pixels overlapping its bounds using a fully opaque color.
8550 *
8551 * Subclasses of View should override this method whenever possible to indicate
8552 * whether an instance is opaque. Opaque Views are treated in a special way by
8553 * the View hierarchy, possibly allowing it to perform optimizations during
8554 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008555 *
Romain Guy24443ea2009-05-11 11:56:30 -07008556 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008557 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008558 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008559 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008560 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008561 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8562 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008563 }
8564
Adam Powell20232d02010-12-08 21:08:53 -08008565 /**
8566 * @hide
8567 */
8568 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008569 // Opaque if:
8570 // - Has a background
8571 // - Background is opaque
8572 // - Doesn't have scrollbars or scrollbars are inside overlay
8573
8574 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8575 mPrivateFlags |= OPAQUE_BACKGROUND;
8576 } else {
8577 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8578 }
8579
8580 final int flags = mViewFlags;
8581 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8582 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8583 mPrivateFlags |= OPAQUE_SCROLLBARS;
8584 } else {
8585 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8586 }
8587 }
8588
8589 /**
8590 * @hide
8591 */
8592 protected boolean hasOpaqueScrollbars() {
8593 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008594 }
8595
8596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 * @return A handler associated with the thread running the View. This
8598 * handler can be used to pump events in the UI events queue.
8599 */
8600 public Handler getHandler() {
8601 if (mAttachInfo != null) {
8602 return mAttachInfo.mHandler;
8603 }
8604 return null;
8605 }
8606
8607 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008608 * <p>Causes the Runnable to be added to the message queue.
8609 * The runnable will be run on the user interface thread.</p>
8610 *
8611 * <p>This method can be invoked from outside of the UI thread
8612 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 *
8614 * @param action The Runnable that will be executed.
8615 *
8616 * @return Returns true if the Runnable was successfully placed in to the
8617 * message queue. Returns false on failure, usually because the
8618 * looper processing the message queue is exiting.
8619 */
8620 public boolean post(Runnable action) {
8621 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008622 AttachInfo attachInfo = mAttachInfo;
8623 if (attachInfo != null) {
8624 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 } else {
8626 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008627 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 return true;
8629 }
8630
8631 return handler.post(action);
8632 }
8633
8634 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008635 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008637 * The runnable will be run on the user interface thread.</p>
8638 *
8639 * <p>This method can be invoked from outside of the UI thread
8640 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008641 *
8642 * @param action The Runnable that will be executed.
8643 * @param delayMillis The delay (in milliseconds) until the Runnable
8644 * will be executed.
8645 *
8646 * @return true if the Runnable was successfully placed in to the
8647 * message queue. Returns false on failure, usually because the
8648 * looper processing the message queue is exiting. Note that a
8649 * result of true does not mean the Runnable will be processed --
8650 * if the looper is quit before the delivery time of the message
8651 * occurs then the message will be dropped.
8652 */
8653 public boolean postDelayed(Runnable action, long delayMillis) {
8654 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008655 AttachInfo attachInfo = mAttachInfo;
8656 if (attachInfo != null) {
8657 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 } else {
8659 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008660 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 return true;
8662 }
8663
8664 return handler.postDelayed(action, delayMillis);
8665 }
8666
8667 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008668 * <p>Removes the specified Runnable from the message queue.</p>
8669 *
8670 * <p>This method can be invoked from outside of the UI thread
8671 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 *
8673 * @param action The Runnable to remove from the message handling queue
8674 *
8675 * @return true if this view could ask the Handler to remove the Runnable,
8676 * false otherwise. When the returned value is true, the Runnable
8677 * may or may not have been actually removed from the message queue
8678 * (for instance, if the Runnable was not in the queue already.)
8679 */
8680 public boolean removeCallbacks(Runnable action) {
8681 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008682 AttachInfo attachInfo = mAttachInfo;
8683 if (attachInfo != null) {
8684 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 } else {
8686 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008687 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 return true;
8689 }
8690
8691 handler.removeCallbacks(action);
8692 return true;
8693 }
8694
8695 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008696 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8697 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 *
Romain Guye63a4f32011-08-11 11:33:31 -07008699 * <p>This method can be invoked from outside of the UI thread
8700 * only when this View is attached to a window.</p>
8701 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 * @see #invalidate()
8703 */
8704 public void postInvalidate() {
8705 postInvalidateDelayed(0);
8706 }
8707
8708 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008709 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8710 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8711 *
8712 * <p>This method can be invoked from outside of the UI thread
8713 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 *
8715 * @param left The left coordinate of the rectangle to invalidate.
8716 * @param top The top coordinate of the rectangle to invalidate.
8717 * @param right The right coordinate of the rectangle to invalidate.
8718 * @param bottom The bottom coordinate of the rectangle to invalidate.
8719 *
8720 * @see #invalidate(int, int, int, int)
8721 * @see #invalidate(Rect)
8722 */
8723 public void postInvalidate(int left, int top, int right, int bottom) {
8724 postInvalidateDelayed(0, left, top, right, bottom);
8725 }
8726
8727 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008728 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8729 * loop. Waits for the specified amount of time.</p>
8730 *
8731 * <p>This method can be invoked from outside of the UI thread
8732 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 *
8734 * @param delayMilliseconds the duration in milliseconds to delay the
8735 * invalidation by
8736 */
8737 public void postInvalidateDelayed(long delayMilliseconds) {
8738 // We try only with the AttachInfo because there's no point in invalidating
8739 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008740 AttachInfo attachInfo = mAttachInfo;
8741 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008742 Message msg = Message.obtain();
8743 msg.what = AttachInfo.INVALIDATE_MSG;
8744 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008745 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 }
8747 }
8748
8749 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008750 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8751 * through the event loop. Waits for the specified amount of time.</p>
8752 *
8753 * <p>This method can be invoked from outside of the UI thread
8754 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 *
8756 * @param delayMilliseconds the duration in milliseconds to delay the
8757 * invalidation by
8758 * @param left The left coordinate of the rectangle to invalidate.
8759 * @param top The top coordinate of the rectangle to invalidate.
8760 * @param right The right coordinate of the rectangle to invalidate.
8761 * @param bottom The bottom coordinate of the rectangle to invalidate.
8762 */
8763 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8764 int right, int bottom) {
8765
8766 // We try only with the AttachInfo because there's no point in invalidating
8767 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008768 AttachInfo attachInfo = mAttachInfo;
8769 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8771 info.target = this;
8772 info.left = left;
8773 info.top = top;
8774 info.right = right;
8775 info.bottom = bottom;
8776
8777 final Message msg = Message.obtain();
8778 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8779 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008780 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 }
8782 }
8783
8784 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008785 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8786 * This event is sent at most once every
8787 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8788 */
8789 private void postSendViewScrolledAccessibilityEventCallback() {
8790 if (mSendViewScrolledAccessibilityEvent == null) {
8791 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8792 }
8793 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8794 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8795 postDelayed(mSendViewScrolledAccessibilityEvent,
8796 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8797 }
8798 }
8799
8800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 * Called by a parent to request that a child update its values for mScrollX
8802 * and mScrollY if necessary. This will typically be done if the child is
8803 * animating a scroll using a {@link android.widget.Scroller Scroller}
8804 * object.
8805 */
8806 public void computeScroll() {
8807 }
8808
8809 /**
8810 * <p>Indicate whether the horizontal edges are faded when the view is
8811 * scrolled horizontally.</p>
8812 *
8813 * @return true if the horizontal edges should are faded on scroll, false
8814 * otherwise
8815 *
8816 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008817 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 */
8819 public boolean isHorizontalFadingEdgeEnabled() {
8820 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8821 }
8822
8823 /**
8824 * <p>Define whether the horizontal edges should be faded when this view
8825 * is scrolled horizontally.</p>
8826 *
8827 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8828 * be faded when the view is scrolled
8829 * horizontally
8830 *
8831 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008832 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 */
8834 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8835 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8836 if (horizontalFadingEdgeEnabled) {
8837 initScrollCache();
8838 }
8839
8840 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8841 }
8842 }
8843
8844 /**
8845 * <p>Indicate whether the vertical edges are faded when the view is
8846 * scrolled horizontally.</p>
8847 *
8848 * @return true if the vertical edges should are faded on scroll, false
8849 * otherwise
8850 *
8851 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008852 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 */
8854 public boolean isVerticalFadingEdgeEnabled() {
8855 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8856 }
8857
8858 /**
8859 * <p>Define whether the vertical edges should be faded when this view
8860 * is scrolled vertically.</p>
8861 *
8862 * @param verticalFadingEdgeEnabled true if the vertical edges should
8863 * be faded when the view is scrolled
8864 * vertically
8865 *
8866 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008867 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008868 */
8869 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8870 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8871 if (verticalFadingEdgeEnabled) {
8872 initScrollCache();
8873 }
8874
8875 mViewFlags ^= FADING_EDGE_VERTICAL;
8876 }
8877 }
8878
8879 /**
8880 * Returns the strength, or intensity, of the top faded edge. The strength is
8881 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8882 * returns 0.0 or 1.0 but no value in between.
8883 *
8884 * Subclasses should override this method to provide a smoother fade transition
8885 * when scrolling occurs.
8886 *
8887 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8888 */
8889 protected float getTopFadingEdgeStrength() {
8890 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8891 }
8892
8893 /**
8894 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8895 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8896 * returns 0.0 or 1.0 but no value in between.
8897 *
8898 * Subclasses should override this method to provide a smoother fade transition
8899 * when scrolling occurs.
8900 *
8901 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8902 */
8903 protected float getBottomFadingEdgeStrength() {
8904 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8905 computeVerticalScrollRange() ? 1.0f : 0.0f;
8906 }
8907
8908 /**
8909 * Returns the strength, or intensity, of the left faded edge. The strength is
8910 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8911 * returns 0.0 or 1.0 but no value in between.
8912 *
8913 * Subclasses should override this method to provide a smoother fade transition
8914 * when scrolling occurs.
8915 *
8916 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8917 */
8918 protected float getLeftFadingEdgeStrength() {
8919 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8920 }
8921
8922 /**
8923 * Returns the strength, or intensity, of the right faded edge. The strength is
8924 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8925 * returns 0.0 or 1.0 but no value in between.
8926 *
8927 * Subclasses should override this method to provide a smoother fade transition
8928 * when scrolling occurs.
8929 *
8930 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8931 */
8932 protected float getRightFadingEdgeStrength() {
8933 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8934 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8935 }
8936
8937 /**
8938 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8939 * scrollbar is not drawn by default.</p>
8940 *
8941 * @return true if the horizontal scrollbar should be painted, false
8942 * otherwise
8943 *
8944 * @see #setHorizontalScrollBarEnabled(boolean)
8945 */
8946 public boolean isHorizontalScrollBarEnabled() {
8947 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8948 }
8949
8950 /**
8951 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8952 * scrollbar is not drawn by default.</p>
8953 *
8954 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8955 * be painted
8956 *
8957 * @see #isHorizontalScrollBarEnabled()
8958 */
8959 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8960 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8961 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008962 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008963 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 }
8965 }
8966
8967 /**
8968 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8969 * scrollbar is not drawn by default.</p>
8970 *
8971 * @return true if the vertical scrollbar should be painted, false
8972 * otherwise
8973 *
8974 * @see #setVerticalScrollBarEnabled(boolean)
8975 */
8976 public boolean isVerticalScrollBarEnabled() {
8977 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8978 }
8979
8980 /**
8981 * <p>Define whether the vertical scrollbar should be drawn or not. The
8982 * scrollbar is not drawn by default.</p>
8983 *
8984 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8985 * be painted
8986 *
8987 * @see #isVerticalScrollBarEnabled()
8988 */
8989 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8990 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8991 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008992 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008993 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 }
8995 }
8996
Adam Powell20232d02010-12-08 21:08:53 -08008997 /**
8998 * @hide
8999 */
9000 protected void recomputePadding() {
9001 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 }
Joe Malin32736f02011-01-19 16:14:20 -08009003
Mike Cleronfe81d382009-09-28 14:22:16 -07009004 /**
9005 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009006 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009007 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009008 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009009 */
9010 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9011 initScrollCache();
9012 final ScrollabilityCache scrollabilityCache = mScrollCache;
9013 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009014 if (fadeScrollbars) {
9015 scrollabilityCache.state = ScrollabilityCache.OFF;
9016 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009017 scrollabilityCache.state = ScrollabilityCache.ON;
9018 }
9019 }
Joe Malin32736f02011-01-19 16:14:20 -08009020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 /**
Joe Malin32736f02011-01-19 16:14:20 -08009022 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009023 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009024 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009025 * @return true if scrollbar fading is enabled
9026 */
9027 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009028 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009029 }
Joe Malin32736f02011-01-19 16:14:20 -08009030
Mike Cleron52f0a642009-09-28 18:21:37 -07009031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9033 * inset. When inset, they add to the padding of the view. And the scrollbars
9034 * can be drawn inside the padding area or on the edge of the view. For example,
9035 * if a view has a background drawable and you want to draw the scrollbars
9036 * inside the padding specified by the drawable, you can use
9037 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9038 * appear at the edge of the view, ignoring the padding, then you can use
9039 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9040 * @param style the style of the scrollbars. Should be one of
9041 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9042 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9043 * @see #SCROLLBARS_INSIDE_OVERLAY
9044 * @see #SCROLLBARS_INSIDE_INSET
9045 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9046 * @see #SCROLLBARS_OUTSIDE_INSET
9047 */
9048 public void setScrollBarStyle(int style) {
9049 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9050 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009051 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009052 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 }
9054 }
9055
9056 /**
9057 * <p>Returns the current scrollbar style.</p>
9058 * @return the current scrollbar style
9059 * @see #SCROLLBARS_INSIDE_OVERLAY
9060 * @see #SCROLLBARS_INSIDE_INSET
9061 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9062 * @see #SCROLLBARS_OUTSIDE_INSET
9063 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009064 @ViewDebug.ExportedProperty(mapping = {
9065 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9066 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9067 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9068 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9069 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009070 public int getScrollBarStyle() {
9071 return mViewFlags & SCROLLBARS_STYLE_MASK;
9072 }
9073
9074 /**
9075 * <p>Compute the horizontal range that the horizontal scrollbar
9076 * represents.</p>
9077 *
9078 * <p>The range is expressed in arbitrary units that must be the same as the
9079 * units used by {@link #computeHorizontalScrollExtent()} and
9080 * {@link #computeHorizontalScrollOffset()}.</p>
9081 *
9082 * <p>The default range is the drawing width of this view.</p>
9083 *
9084 * @return the total horizontal range represented by the horizontal
9085 * scrollbar
9086 *
9087 * @see #computeHorizontalScrollExtent()
9088 * @see #computeHorizontalScrollOffset()
9089 * @see android.widget.ScrollBarDrawable
9090 */
9091 protected int computeHorizontalScrollRange() {
9092 return getWidth();
9093 }
9094
9095 /**
9096 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9097 * within the horizontal range. This value is used to compute the position
9098 * of the thumb within the scrollbar's track.</p>
9099 *
9100 * <p>The range is expressed in arbitrary units that must be the same as the
9101 * units used by {@link #computeHorizontalScrollRange()} and
9102 * {@link #computeHorizontalScrollExtent()}.</p>
9103 *
9104 * <p>The default offset is the scroll offset of this view.</p>
9105 *
9106 * @return the horizontal offset of the scrollbar's thumb
9107 *
9108 * @see #computeHorizontalScrollRange()
9109 * @see #computeHorizontalScrollExtent()
9110 * @see android.widget.ScrollBarDrawable
9111 */
9112 protected int computeHorizontalScrollOffset() {
9113 return mScrollX;
9114 }
9115
9116 /**
9117 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9118 * within the horizontal range. This value is used to compute the length
9119 * of the thumb within the scrollbar's track.</p>
9120 *
9121 * <p>The range is expressed in arbitrary units that must be the same as the
9122 * units used by {@link #computeHorizontalScrollRange()} and
9123 * {@link #computeHorizontalScrollOffset()}.</p>
9124 *
9125 * <p>The default extent is the drawing width of this view.</p>
9126 *
9127 * @return the horizontal extent of the scrollbar's thumb
9128 *
9129 * @see #computeHorizontalScrollRange()
9130 * @see #computeHorizontalScrollOffset()
9131 * @see android.widget.ScrollBarDrawable
9132 */
9133 protected int computeHorizontalScrollExtent() {
9134 return getWidth();
9135 }
9136
9137 /**
9138 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9139 *
9140 * <p>The range is expressed in arbitrary units that must be the same as the
9141 * units used by {@link #computeVerticalScrollExtent()} and
9142 * {@link #computeVerticalScrollOffset()}.</p>
9143 *
9144 * @return the total vertical range represented by the vertical scrollbar
9145 *
9146 * <p>The default range is the drawing height of this view.</p>
9147 *
9148 * @see #computeVerticalScrollExtent()
9149 * @see #computeVerticalScrollOffset()
9150 * @see android.widget.ScrollBarDrawable
9151 */
9152 protected int computeVerticalScrollRange() {
9153 return getHeight();
9154 }
9155
9156 /**
9157 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9158 * within the horizontal range. This value is used to compute the position
9159 * of the thumb within the scrollbar's track.</p>
9160 *
9161 * <p>The range is expressed in arbitrary units that must be the same as the
9162 * units used by {@link #computeVerticalScrollRange()} and
9163 * {@link #computeVerticalScrollExtent()}.</p>
9164 *
9165 * <p>The default offset is the scroll offset of this view.</p>
9166 *
9167 * @return the vertical offset of the scrollbar's thumb
9168 *
9169 * @see #computeVerticalScrollRange()
9170 * @see #computeVerticalScrollExtent()
9171 * @see android.widget.ScrollBarDrawable
9172 */
9173 protected int computeVerticalScrollOffset() {
9174 return mScrollY;
9175 }
9176
9177 /**
9178 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9179 * within the vertical range. This value is used to compute the length
9180 * of the thumb within the scrollbar's track.</p>
9181 *
9182 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009183 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 * {@link #computeVerticalScrollOffset()}.</p>
9185 *
9186 * <p>The default extent is the drawing height of this view.</p>
9187 *
9188 * @return the vertical extent of the scrollbar's thumb
9189 *
9190 * @see #computeVerticalScrollRange()
9191 * @see #computeVerticalScrollOffset()
9192 * @see android.widget.ScrollBarDrawable
9193 */
9194 protected int computeVerticalScrollExtent() {
9195 return getHeight();
9196 }
9197
9198 /**
Adam Powell69159442011-06-13 17:53:06 -07009199 * Check if this view can be scrolled horizontally in a certain direction.
9200 *
9201 * @param direction Negative to check scrolling left, positive to check scrolling right.
9202 * @return true if this view can be scrolled in the specified direction, false otherwise.
9203 */
9204 public boolean canScrollHorizontally(int direction) {
9205 final int offset = computeHorizontalScrollOffset();
9206 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9207 if (range == 0) return false;
9208 if (direction < 0) {
9209 return offset > 0;
9210 } else {
9211 return offset < range - 1;
9212 }
9213 }
9214
9215 /**
9216 * Check if this view can be scrolled vertically in a certain direction.
9217 *
9218 * @param direction Negative to check scrolling up, positive to check scrolling down.
9219 * @return true if this view can be scrolled in the specified direction, false otherwise.
9220 */
9221 public boolean canScrollVertically(int direction) {
9222 final int offset = computeVerticalScrollOffset();
9223 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9224 if (range == 0) return false;
9225 if (direction < 0) {
9226 return offset > 0;
9227 } else {
9228 return offset < range - 1;
9229 }
9230 }
9231
9232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9234 * scrollbars are painted only if they have been awakened first.</p>
9235 *
9236 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009237 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009238 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009240 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 // scrollbars are drawn only when the animation is running
9242 final ScrollabilityCache cache = mScrollCache;
9243 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009244
Mike Cleronf116bf82009-09-27 19:14:12 -07009245 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009246
Mike Cleronf116bf82009-09-27 19:14:12 -07009247 if (state == ScrollabilityCache.OFF) {
9248 return;
9249 }
Joe Malin32736f02011-01-19 16:14:20 -08009250
Mike Cleronf116bf82009-09-27 19:14:12 -07009251 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009252
Mike Cleronf116bf82009-09-27 19:14:12 -07009253 if (state == ScrollabilityCache.FADING) {
9254 // We're fading -- get our fade interpolation
9255 if (cache.interpolatorValues == null) {
9256 cache.interpolatorValues = new float[1];
9257 }
Joe Malin32736f02011-01-19 16:14:20 -08009258
Mike Cleronf116bf82009-09-27 19:14:12 -07009259 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009260
Mike Cleronf116bf82009-09-27 19:14:12 -07009261 // Stops the animation if we're done
9262 if (cache.scrollBarInterpolator.timeToValues(values) ==
9263 Interpolator.Result.FREEZE_END) {
9264 cache.state = ScrollabilityCache.OFF;
9265 } else {
9266 cache.scrollBar.setAlpha(Math.round(values[0]));
9267 }
Joe Malin32736f02011-01-19 16:14:20 -08009268
9269 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009270 // drawing. We only want this when we're fading so that
9271 // we prevent excessive redraws
9272 invalidate = true;
9273 } else {
9274 // We're just on -- but we may have been fading before so
9275 // reset alpha
9276 cache.scrollBar.setAlpha(255);
9277 }
9278
Joe Malin32736f02011-01-19 16:14:20 -08009279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 final int viewFlags = mViewFlags;
9281
9282 final boolean drawHorizontalScrollBar =
9283 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9284 final boolean drawVerticalScrollBar =
9285 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9286 && !isVerticalScrollBarHidden();
9287
9288 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9289 final int width = mRight - mLeft;
9290 final int height = mBottom - mTop;
9291
9292 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293
Mike Reede8853fc2009-09-04 14:01:48 -04009294 final int scrollX = mScrollX;
9295 final int scrollY = mScrollY;
9296 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9297
Mike Cleronf116bf82009-09-27 19:14:12 -07009298 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009301 int size = scrollBar.getSize(false);
9302 if (size <= 0) {
9303 size = cache.scrollBarSize;
9304 }
9305
Mike Cleronf116bf82009-09-27 19:14:12 -07009306 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009307 computeHorizontalScrollOffset(),
9308 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009309 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009310 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009311 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009312 left = scrollX + (mPaddingLeft & inside);
9313 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9314 bottom = top + size;
9315 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9316 if (invalidate) {
9317 invalidate(left, top, right, bottom);
9318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 }
9320
9321 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009322 int size = scrollBar.getSize(true);
9323 if (size <= 0) {
9324 size = cache.scrollBarSize;
9325 }
9326
Mike Reede8853fc2009-09-04 14:01:48 -04009327 scrollBar.setParameters(computeVerticalScrollRange(),
9328 computeVerticalScrollOffset(),
9329 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009330 switch (mVerticalScrollbarPosition) {
9331 default:
9332 case SCROLLBAR_POSITION_DEFAULT:
9333 case SCROLLBAR_POSITION_RIGHT:
9334 left = scrollX + width - size - (mUserPaddingRight & inside);
9335 break;
9336 case SCROLLBAR_POSITION_LEFT:
9337 left = scrollX + (mUserPaddingLeft & inside);
9338 break;
9339 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009340 top = scrollY + (mPaddingTop & inside);
9341 right = left + size;
9342 bottom = scrollY + height - (mUserPaddingBottom & inside);
9343 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9344 if (invalidate) {
9345 invalidate(left, top, right, bottom);
9346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 }
9348 }
9349 }
9350 }
Romain Guy8506ab42009-06-11 17:35:47 -07009351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009353 * 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 -08009354 * FastScroller is visible.
9355 * @return whether to temporarily hide the vertical scrollbar
9356 * @hide
9357 */
9358 protected boolean isVerticalScrollBarHidden() {
9359 return false;
9360 }
9361
9362 /**
9363 * <p>Draw the horizontal scrollbar if
9364 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 * @param canvas the canvas on which to draw the scrollbar
9367 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 *
9369 * @see #isHorizontalScrollBarEnabled()
9370 * @see #computeHorizontalScrollRange()
9371 * @see #computeHorizontalScrollExtent()
9372 * @see #computeHorizontalScrollOffset()
9373 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009374 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009375 */
Romain Guy8fb95422010-08-17 18:38:51 -07009376 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9377 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009378 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009379 scrollBar.draw(canvas);
9380 }
Mike Reede8853fc2009-09-04 14:01:48 -04009381
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9384 * returns true.</p>
9385 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 * @param canvas the canvas on which to draw the scrollbar
9387 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 *
9389 * @see #isVerticalScrollBarEnabled()
9390 * @see #computeVerticalScrollRange()
9391 * @see #computeVerticalScrollExtent()
9392 * @see #computeVerticalScrollOffset()
9393 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009394 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 */
Romain Guy8fb95422010-08-17 18:38:51 -07009396 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9397 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009398 scrollBar.setBounds(l, t, r, b);
9399 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009400 }
9401
9402 /**
9403 * Implement this to do your drawing.
9404 *
9405 * @param canvas the canvas on which the background will be drawn
9406 */
9407 protected void onDraw(Canvas canvas) {
9408 }
9409
9410 /*
9411 * Caller is responsible for calling requestLayout if necessary.
9412 * (This allows addViewInLayout to not request a new layout.)
9413 */
9414 void assignParent(ViewParent parent) {
9415 if (mParent == null) {
9416 mParent = parent;
9417 } else if (parent == null) {
9418 mParent = null;
9419 } else {
9420 throw new RuntimeException("view " + this + " being added, but"
9421 + " it already has a parent");
9422 }
9423 }
9424
9425 /**
9426 * This is called when the view is attached to a window. At this point it
9427 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009428 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9429 * however it may be called any time before the first onDraw -- including
9430 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 *
9432 * @see #onDetachedFromWindow()
9433 */
9434 protected void onAttachedToWindow() {
9435 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9436 mParent.requestTransparentRegion(this);
9437 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009438 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9439 initialAwakenScrollBars();
9440 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9441 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009442 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009443 // Order is important here: LayoutDirection MUST be resolved before Padding
9444 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009445 resolveLayoutDirectionIfNeeded();
9446 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009447 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009448 if (isFocused()) {
9449 InputMethodManager imm = InputMethodManager.peekInstance();
9450 imm.focusIn(this);
9451 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009452 }
Cibu Johny86666632010-02-22 13:01:02 -08009453
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009454 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009455 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9456 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009457 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009458 private void resolveLayoutDirectionIfNeeded() {
9459 // Do not resolve if it is not needed
9460 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9461
9462 // Clear any previous layout direction resolution
9463 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9464
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009465 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9466 // TextDirectionHeuristic
9467 resetResolvedTextDirection();
9468
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009469 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009470 switch (getLayoutDirection()) {
9471 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009472 // We cannot do the resolution if there is no parent
9473 if (mParent == null) return;
9474
Cibu Johny86666632010-02-22 13:01:02 -08009475 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009476 if (mParent instanceof ViewGroup) {
9477 ViewGroup viewGroup = ((ViewGroup) mParent);
9478
9479 // Check if the parent view group can resolve
9480 if (! viewGroup.canResolveLayoutDirection()) {
9481 return;
9482 }
9483 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9484 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9485 }
Cibu Johny86666632010-02-22 13:01:02 -08009486 }
9487 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009488 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009489 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009490 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009491 case LAYOUT_DIRECTION_LOCALE:
9492 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009493 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009494 }
9495 break;
9496 default:
9497 // Nothing to do, LTR by default
9498 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009499
9500 // Set to resolved
9501 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9502 }
9503
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009504 /**
9505 * @hide
9506 */
9507 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009508 // If the user specified the absolute padding (either with android:padding or
9509 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9510 // use the default padding or the padding from the background drawable
9511 // (stored at this point in mPadding*)
9512 switch (getResolvedLayoutDirection()) {
9513 case LAYOUT_DIRECTION_RTL:
9514 // Start user padding override Right user padding. Otherwise, if Right user
9515 // padding is not defined, use the default Right padding. If Right user padding
9516 // is defined, just use it.
9517 if (mUserPaddingStart >= 0) {
9518 mUserPaddingRight = mUserPaddingStart;
9519 } else if (mUserPaddingRight < 0) {
9520 mUserPaddingRight = mPaddingRight;
9521 }
9522 if (mUserPaddingEnd >= 0) {
9523 mUserPaddingLeft = mUserPaddingEnd;
9524 } else if (mUserPaddingLeft < 0) {
9525 mUserPaddingLeft = mPaddingLeft;
9526 }
9527 break;
9528 case LAYOUT_DIRECTION_LTR:
9529 default:
9530 // Start user padding override Left user padding. Otherwise, if Left user
9531 // padding is not defined, use the default left padding. If Left user padding
9532 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009533 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009534 mUserPaddingLeft = mUserPaddingStart;
9535 } else if (mUserPaddingLeft < 0) {
9536 mUserPaddingLeft = mPaddingLeft;
9537 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009538 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009539 mUserPaddingRight = mUserPaddingEnd;
9540 } else if (mUserPaddingRight < 0) {
9541 mUserPaddingRight = mPaddingRight;
9542 }
9543 }
9544
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009545 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9546
9547 recomputePadding();
9548 }
9549
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009550 /**
9551 * Return true if layout direction resolution can be done
9552 *
9553 * @hide
9554 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009555 protected boolean canResolveLayoutDirection() {
9556 switch (getLayoutDirection()) {
9557 case LAYOUT_DIRECTION_INHERIT:
9558 return (mParent != null);
9559 default:
9560 return true;
9561 }
9562 }
9563
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009564 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009565 * Reset the resolved layout direction.
9566 *
9567 * Subclasses need to override this method to clear cached information that depends on the
9568 * resolved layout direction, or to inform child views that inherit their layout direction.
9569 * Overrides must also call the superclass implementation at the start of their implementation.
9570 *
9571 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009572 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009573 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009574 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009575 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009576 }
9577
9578 /**
9579 * Check if a Locale is corresponding to a RTL script.
9580 *
9581 * @param locale Locale to check
9582 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009583 *
9584 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009585 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009586 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009587 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9588 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 }
9590
9591 /**
9592 * This is called when the view is detached from a window. At this point it
9593 * no longer has a surface for drawing.
9594 *
9595 * @see #onAttachedToWindow()
9596 */
9597 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009598 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009599
Romain Guya440b002010-02-24 15:57:54 -08009600 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009601 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009602 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009603 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009606
Romain Guy6d7475d2011-07-27 16:28:21 -07009607 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009608
Chet Haasedaf98e92011-01-10 14:10:36 -08009609 if (mDisplayList != null) {
9610 mDisplayList.invalidate();
9611 }
9612
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009613 if (mAttachInfo != null) {
9614 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009615 }
9616
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009617 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009618
9619 resetResolvedLayoutDirection();
9620 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 }
9622
9623 /**
9624 * @return The number of times this view has been attached to a window
9625 */
9626 protected int getWindowAttachCount() {
9627 return mWindowAttachCount;
9628 }
9629
9630 /**
9631 * Retrieve a unique token identifying the window this view is attached to.
9632 * @return Return the window's token for use in
9633 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9634 */
9635 public IBinder getWindowToken() {
9636 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9637 }
9638
9639 /**
9640 * Retrieve a unique token identifying the top-level "real" window of
9641 * the window that this view is attached to. That is, this is like
9642 * {@link #getWindowToken}, except if the window this view in is a panel
9643 * window (attached to another containing window), then the token of
9644 * the containing window is returned instead.
9645 *
9646 * @return Returns the associated window token, either
9647 * {@link #getWindowToken()} or the containing window's token.
9648 */
9649 public IBinder getApplicationWindowToken() {
9650 AttachInfo ai = mAttachInfo;
9651 if (ai != null) {
9652 IBinder appWindowToken = ai.mPanelParentWindowToken;
9653 if (appWindowToken == null) {
9654 appWindowToken = ai.mWindowToken;
9655 }
9656 return appWindowToken;
9657 }
9658 return null;
9659 }
9660
9661 /**
9662 * Retrieve private session object this view hierarchy is using to
9663 * communicate with the window manager.
9664 * @return the session object to communicate with the window manager
9665 */
9666 /*package*/ IWindowSession getWindowSession() {
9667 return mAttachInfo != null ? mAttachInfo.mSession : null;
9668 }
9669
9670 /**
9671 * @param info the {@link android.view.View.AttachInfo} to associated with
9672 * this view
9673 */
9674 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9675 //System.out.println("Attached! " + this);
9676 mAttachInfo = info;
9677 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009678 // We will need to evaluate the drawable state at least once.
9679 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 if (mFloatingTreeObserver != null) {
9681 info.mTreeObserver.merge(mFloatingTreeObserver);
9682 mFloatingTreeObserver = null;
9683 }
9684 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9685 mAttachInfo.mScrollContainers.add(this);
9686 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9687 }
9688 performCollectViewAttributes(visibility);
9689 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009690
9691 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9692 mOnAttachStateChangeListeners;
9693 if (listeners != null && listeners.size() > 0) {
9694 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9695 // perform the dispatching. The iterator is a safe guard against listeners that
9696 // could mutate the list by calling the various add/remove methods. This prevents
9697 // the array from being modified while we iterate it.
9698 for (OnAttachStateChangeListener listener : listeners) {
9699 listener.onViewAttachedToWindow(this);
9700 }
9701 }
9702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 int vis = info.mWindowVisibility;
9704 if (vis != GONE) {
9705 onWindowVisibilityChanged(vis);
9706 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009707 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9708 // If nobody has evaluated the drawable state yet, then do it now.
9709 refreshDrawableState();
9710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 }
9712
9713 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 AttachInfo info = mAttachInfo;
9715 if (info != null) {
9716 int vis = info.mWindowVisibility;
9717 if (vis != GONE) {
9718 onWindowVisibilityChanged(GONE);
9719 }
9720 }
9721
9722 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009723
Adam Powell4afd62b2011-02-18 15:02:18 -08009724 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9725 mOnAttachStateChangeListeners;
9726 if (listeners != null && listeners.size() > 0) {
9727 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9728 // perform the dispatching. The iterator is a safe guard against listeners that
9729 // could mutate the list by calling the various add/remove methods. This prevents
9730 // the array from being modified while we iterate it.
9731 for (OnAttachStateChangeListener listener : listeners) {
9732 listener.onViewDetachedFromWindow(this);
9733 }
9734 }
9735
Romain Guy01d5edc2011-01-28 11:28:53 -08009736 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009737 mAttachInfo.mScrollContainers.remove(this);
9738 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9739 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009741 mAttachInfo = null;
9742 }
9743
9744 /**
9745 * Store this view hierarchy's frozen state into the given container.
9746 *
9747 * @param container The SparseArray in which to save the view's state.
9748 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009749 * @see #restoreHierarchyState(android.util.SparseArray)
9750 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9751 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 */
9753 public void saveHierarchyState(SparseArray<Parcelable> container) {
9754 dispatchSaveInstanceState(container);
9755 }
9756
9757 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009758 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9759 * this view and its children. May be overridden to modify how freezing happens to a
9760 * 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 -08009761 *
9762 * @param container The SparseArray in which to save the view's state.
9763 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009764 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9765 * @see #saveHierarchyState(android.util.SparseArray)
9766 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 */
9768 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9769 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9770 mPrivateFlags &= ~SAVE_STATE_CALLED;
9771 Parcelable state = onSaveInstanceState();
9772 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9773 throw new IllegalStateException(
9774 "Derived class did not call super.onSaveInstanceState()");
9775 }
9776 if (state != null) {
9777 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9778 // + ": " + state);
9779 container.put(mID, state);
9780 }
9781 }
9782 }
9783
9784 /**
9785 * Hook allowing a view to generate a representation of its internal state
9786 * that can later be used to create a new instance with that same state.
9787 * This state should only contain information that is not persistent or can
9788 * not be reconstructed later. For example, you will never store your
9789 * current position on screen because that will be computed again when a
9790 * new instance of the view is placed in its view hierarchy.
9791 * <p>
9792 * Some examples of things you may store here: the current cursor position
9793 * in a text view (but usually not the text itself since that is stored in a
9794 * content provider or other persistent storage), the currently selected
9795 * item in a list view.
9796 *
9797 * @return Returns a Parcelable object containing the view's current dynamic
9798 * state, or null if there is nothing interesting to save. The
9799 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009800 * @see #onRestoreInstanceState(android.os.Parcelable)
9801 * @see #saveHierarchyState(android.util.SparseArray)
9802 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 * @see #setSaveEnabled(boolean)
9804 */
9805 protected Parcelable onSaveInstanceState() {
9806 mPrivateFlags |= SAVE_STATE_CALLED;
9807 return BaseSavedState.EMPTY_STATE;
9808 }
9809
9810 /**
9811 * Restore this view hierarchy's frozen state from the given container.
9812 *
9813 * @param container The SparseArray which holds previously frozen states.
9814 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009815 * @see #saveHierarchyState(android.util.SparseArray)
9816 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9817 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 */
9819 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9820 dispatchRestoreInstanceState(container);
9821 }
9822
9823 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009824 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9825 * state for this view and its children. May be overridden to modify how restoring
9826 * happens to a view's children; for example, some views may want to not store state
9827 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828 *
9829 * @param container The SparseArray which holds previously saved state.
9830 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009831 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9832 * @see #restoreHierarchyState(android.util.SparseArray)
9833 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 */
9835 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9836 if (mID != NO_ID) {
9837 Parcelable state = container.get(mID);
9838 if (state != null) {
9839 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9840 // + ": " + state);
9841 mPrivateFlags &= ~SAVE_STATE_CALLED;
9842 onRestoreInstanceState(state);
9843 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9844 throw new IllegalStateException(
9845 "Derived class did not call super.onRestoreInstanceState()");
9846 }
9847 }
9848 }
9849 }
9850
9851 /**
9852 * Hook allowing a view to re-apply a representation of its internal state that had previously
9853 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9854 * null state.
9855 *
9856 * @param state The frozen state that had previously been returned by
9857 * {@link #onSaveInstanceState}.
9858 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009859 * @see #onSaveInstanceState()
9860 * @see #restoreHierarchyState(android.util.SparseArray)
9861 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 */
9863 protected void onRestoreInstanceState(Parcelable state) {
9864 mPrivateFlags |= SAVE_STATE_CALLED;
9865 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009866 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9867 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009868 + "when two views of different type have the same id in the same hierarchy. "
9869 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009870 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009871 }
9872 }
9873
9874 /**
9875 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9876 *
9877 * @return the drawing start time in milliseconds
9878 */
9879 public long getDrawingTime() {
9880 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9881 }
9882
9883 /**
9884 * <p>Enables or disables the duplication of the parent's state into this view. When
9885 * duplication is enabled, this view gets its drawable state from its parent rather
9886 * than from its own internal properties.</p>
9887 *
9888 * <p>Note: in the current implementation, setting this property to true after the
9889 * view was added to a ViewGroup might have no effect at all. This property should
9890 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9891 *
9892 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9893 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009894 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009895 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9896 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 *
9898 * @param enabled True to enable duplication of the parent's drawable state, false
9899 * to disable it.
9900 *
9901 * @see #getDrawableState()
9902 * @see #isDuplicateParentStateEnabled()
9903 */
9904 public void setDuplicateParentStateEnabled(boolean enabled) {
9905 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9906 }
9907
9908 /**
9909 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9910 *
9911 * @return True if this view's drawable state is duplicated from the parent,
9912 * false otherwise
9913 *
9914 * @see #getDrawableState()
9915 * @see #setDuplicateParentStateEnabled(boolean)
9916 */
9917 public boolean isDuplicateParentStateEnabled() {
9918 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9919 }
9920
9921 /**
Romain Guy171c5922011-01-06 10:04:23 -08009922 * <p>Specifies the type of layer backing this view. The layer can be
9923 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9924 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009925 *
Romain Guy171c5922011-01-06 10:04:23 -08009926 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9927 * instance that controls how the layer is composed on screen. The following
9928 * properties of the paint are taken into account when composing the layer:</p>
9929 * <ul>
9930 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9931 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9932 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9933 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009934 *
Romain Guy171c5922011-01-06 10:04:23 -08009935 * <p>If this view has an alpha value set to < 1.0 by calling
9936 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9937 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9938 * equivalent to setting a hardware layer on this view and providing a paint with
9939 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009940 *
Romain Guy171c5922011-01-06 10:04:23 -08009941 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9942 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9943 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009944 *
Romain Guy171c5922011-01-06 10:04:23 -08009945 * @param layerType The ype of layer to use with this view, must be one of
9946 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9947 * {@link #LAYER_TYPE_HARDWARE}
9948 * @param paint The paint used to compose the layer. This argument is optional
9949 * and can be null. It is ignored when the layer type is
9950 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009951 *
9952 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009953 * @see #LAYER_TYPE_NONE
9954 * @see #LAYER_TYPE_SOFTWARE
9955 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009956 * @see #setAlpha(float)
9957 *
Romain Guy171c5922011-01-06 10:04:23 -08009958 * @attr ref android.R.styleable#View_layerType
9959 */
9960 public void setLayerType(int layerType, Paint paint) {
9961 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009962 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009963 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9964 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009965
Romain Guyd6cd5722011-01-17 14:42:41 -08009966 if (layerType == mLayerType) {
9967 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9968 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009969 invalidateParentCaches();
9970 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009971 }
9972 return;
9973 }
Romain Guy171c5922011-01-06 10:04:23 -08009974
9975 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009976 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009977 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009978 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009979 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009980 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009981 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009982 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009983 default:
9984 break;
Romain Guy171c5922011-01-06 10:04:23 -08009985 }
9986
9987 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009988 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9989 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9990 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009991
Romain Guy0fd89bf2011-01-26 15:41:30 -08009992 invalidateParentCaches();
9993 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009994 }
9995
9996 /**
Romain Guy59c7f802011-09-29 17:21:45 -07009997 * Indicates whether this view has a static layer. A view with layer type
9998 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
9999 * dynamic.
10000 */
10001 boolean hasStaticLayer() {
10002 return mLayerType == LAYER_TYPE_NONE;
10003 }
10004
10005 /**
Romain Guy171c5922011-01-06 10:04:23 -080010006 * Indicates what type of layer is currently associated with this view. By default
10007 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10008 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10009 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010010 *
Romain Guy171c5922011-01-06 10:04:23 -080010011 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10012 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010013 *
10014 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010015 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010016 * @see #LAYER_TYPE_NONE
10017 * @see #LAYER_TYPE_SOFTWARE
10018 * @see #LAYER_TYPE_HARDWARE
10019 */
10020 public int getLayerType() {
10021 return mLayerType;
10022 }
Joe Malin32736f02011-01-19 16:14:20 -080010023
Romain Guy6c319ca2011-01-11 14:29:25 -080010024 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010025 * Forces this view's layer to be created and this view to be rendered
10026 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10027 * invoking this method will have no effect.
10028 *
10029 * This method can for instance be used to render a view into its layer before
10030 * starting an animation. If this view is complex, rendering into the layer
10031 * before starting the animation will avoid skipping frames.
10032 *
10033 * @throws IllegalStateException If this view is not attached to a window
10034 *
10035 * @see #setLayerType(int, android.graphics.Paint)
10036 */
10037 public void buildLayer() {
10038 if (mLayerType == LAYER_TYPE_NONE) return;
10039
10040 if (mAttachInfo == null) {
10041 throw new IllegalStateException("This view must be attached to a window first");
10042 }
10043
10044 switch (mLayerType) {
10045 case LAYER_TYPE_HARDWARE:
10046 getHardwareLayer();
10047 break;
10048 case LAYER_TYPE_SOFTWARE:
10049 buildDrawingCache(true);
10050 break;
10051 }
10052 }
10053
10054 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010055 * <p>Returns a hardware layer that can be used to draw this view again
10056 * without executing its draw method.</p>
10057 *
10058 * @return A HardwareLayer ready to render, or null if an error occurred.
10059 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010060 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010061 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10062 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010063 return null;
10064 }
10065
10066 final int width = mRight - mLeft;
10067 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010068
Romain Guy6c319ca2011-01-11 14:29:25 -080010069 if (width == 0 || height == 0) {
10070 return null;
10071 }
10072
10073 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10074 if (mHardwareLayer == null) {
10075 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10076 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010077 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010078 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10079 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010080 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010081 }
10082
Romain Guy59a12ca2011-06-09 17:48:21 -070010083 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010084 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10085 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010086 try {
10087 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010088 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010089 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010090
Chet Haase88172fe2011-03-07 17:36:33 -080010091 final int restoreCount = canvas.save();
10092
Romain Guy6c319ca2011-01-11 14:29:25 -080010093 computeScroll();
10094 canvas.translate(-mScrollX, -mScrollY);
10095
Romain Guy6c319ca2011-01-11 14:29:25 -080010096 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010097
Romain Guy6c319ca2011-01-11 14:29:25 -080010098 // Fast path for layouts with no backgrounds
10099 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10100 mPrivateFlags &= ~DIRTY_MASK;
10101 dispatchDraw(canvas);
10102 } else {
10103 draw(canvas);
10104 }
Joe Malin32736f02011-01-19 16:14:20 -080010105
Chet Haase88172fe2011-03-07 17:36:33 -080010106 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010107 } finally {
10108 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010109 mHardwareLayer.end(currentCanvas);
10110 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010111 }
10112 }
10113
10114 return mHardwareLayer;
10115 }
Romain Guy171c5922011-01-06 10:04:23 -080010116
Romain Guy65b345f2011-07-27 18:51:50 -070010117 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010118 if (mHardwareLayer != null) {
10119 mHardwareLayer.destroy();
10120 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010121 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010122 }
Romain Guy65b345f2011-07-27 18:51:50 -070010123 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010124 }
10125
Romain Guy171c5922011-01-06 10:04:23 -080010126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010127 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10128 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10129 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10130 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10131 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10132 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010133 *
Romain Guy171c5922011-01-06 10:04:23 -080010134 * <p>Enabling the drawing cache is similar to
10135 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010136 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10137 * drawing cache has no effect on rendering because the system uses a different mechanism
10138 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10139 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10140 * for information on how to enable software and hardware layers.</p>
10141 *
10142 * <p>This API can be used to manually generate
10143 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10144 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010145 *
10146 * @param enabled true to enable the drawing cache, false otherwise
10147 *
10148 * @see #isDrawingCacheEnabled()
10149 * @see #getDrawingCache()
10150 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010151 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 */
10153 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010154 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10156 }
10157
10158 /**
10159 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10160 *
10161 * @return true if the drawing cache is enabled
10162 *
10163 * @see #setDrawingCacheEnabled(boolean)
10164 * @see #getDrawingCache()
10165 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010166 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 public boolean isDrawingCacheEnabled() {
10168 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10169 }
10170
10171 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010172 * Debugging utility which recursively outputs the dirty state of a view and its
10173 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010174 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010175 * @hide
10176 */
Romain Guy676b1732011-02-14 14:45:33 -080010177 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010178 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10179 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10180 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10181 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10182 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10183 if (clear) {
10184 mPrivateFlags &= clearMask;
10185 }
10186 if (this instanceof ViewGroup) {
10187 ViewGroup parent = (ViewGroup) this;
10188 final int count = parent.getChildCount();
10189 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010190 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010191 child.outputDirtyFlags(indent + " ", clear, clearMask);
10192 }
10193 }
10194 }
10195
10196 /**
10197 * This method is used by ViewGroup to cause its children to restore or recreate their
10198 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10199 * to recreate its own display list, which would happen if it went through the normal
10200 * draw/dispatchDraw mechanisms.
10201 *
10202 * @hide
10203 */
10204 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010205
10206 /**
10207 * A view that is not attached or hardware accelerated cannot create a display list.
10208 * This method checks these conditions and returns the appropriate result.
10209 *
10210 * @return true if view has the ability to create a display list, false otherwise.
10211 *
10212 * @hide
10213 */
10214 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010215 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010216 }
Joe Malin32736f02011-01-19 16:14:20 -080010217
Chet Haasedaf98e92011-01-10 14:10:36 -080010218 /**
Romain Guyb051e892010-09-28 19:09:36 -070010219 * <p>Returns a display list that can be used to draw this view again
10220 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010221 *
Romain Guyb051e892010-09-28 19:09:36 -070010222 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010223 *
10224 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010225 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010226 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010227 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010228 return null;
10229 }
10230
Chet Haasedaf98e92011-01-10 14:10:36 -080010231 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10232 mDisplayList == null || !mDisplayList.isValid() ||
10233 mRecreateDisplayList)) {
10234 // Don't need to recreate the display list, just need to tell our
10235 // children to restore/recreate theirs
10236 if (mDisplayList != null && mDisplayList.isValid() &&
10237 !mRecreateDisplayList) {
10238 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10239 mPrivateFlags &= ~DIRTY_MASK;
10240 dispatchGetDisplayList();
10241
10242 return mDisplayList;
10243 }
10244
10245 // If we got here, we're recreating it. Mark it as such to ensure that
10246 // we copy in child display lists into ours in drawChild()
10247 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010248 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010249 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010250 // If we're creating a new display list, make sure our parent gets invalidated
10251 // since they will need to recreate their display list to account for this
10252 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010253 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010254 }
Romain Guyb051e892010-09-28 19:09:36 -070010255
10256 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010257 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010258 try {
10259 int width = mRight - mLeft;
10260 int height = mBottom - mTop;
10261
10262 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010263 // The dirty rect should always be null for a display list
10264 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010265
Chet Haasedaf98e92011-01-10 14:10:36 -080010266 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010267
10268 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010269 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010270 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010271 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010272
Romain Guyb051e892010-09-28 19:09:36 -070010273 // Fast path for layouts with no backgrounds
10274 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010275 dispatchDraw(canvas);
10276 } else {
10277 draw(canvas);
10278 }
Romain Guyb051e892010-09-28 19:09:36 -070010279 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010280 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010281 canvas.onPostDraw();
10282
10283 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010284 }
Chet Haase77785f92011-01-25 23:22:09 -080010285 } else {
10286 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10287 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010288 }
10289
10290 return mDisplayList;
10291 }
10292
10293 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010294 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010295 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010296 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010297 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010298 * @see #getDrawingCache(boolean)
10299 */
10300 public Bitmap getDrawingCache() {
10301 return getDrawingCache(false);
10302 }
10303
10304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10306 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10307 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10308 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10309 * request the drawing cache by calling this method and draw it on screen if the
10310 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010311 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010312 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10313 * this method will create a bitmap of the same size as this view. Because this bitmap
10314 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10315 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10316 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10317 * size than the view. This implies that your application must be able to handle this
10318 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010319 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010320 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10321 * the current density of the screen when the application is in compatibility
10322 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010324 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010325 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326 * @see #setDrawingCacheEnabled(boolean)
10327 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010328 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 * @see #destroyDrawingCache()
10330 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010331 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10333 return null;
10334 }
10335 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010336 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 }
Romain Guy02890fd2010-08-06 17:58:44 -070010338 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 }
10340
10341 /**
10342 * <p>Frees the resources used by the drawing cache. If you call
10343 * {@link #buildDrawingCache()} manually without calling
10344 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10345 * should cleanup the cache with this method afterwards.</p>
10346 *
10347 * @see #setDrawingCacheEnabled(boolean)
10348 * @see #buildDrawingCache()
10349 * @see #getDrawingCache()
10350 */
10351 public void destroyDrawingCache() {
10352 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010353 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 mDrawingCache = null;
10355 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010356 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010357 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010358 mUnscaledDrawingCache = null;
10359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 }
10361
10362 /**
10363 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010364 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010365 * view will always be drawn on top of a solid color.
10366 *
10367 * @param color The background color to use for the drawing cache's bitmap
10368 *
10369 * @see #setDrawingCacheEnabled(boolean)
10370 * @see #buildDrawingCache()
10371 * @see #getDrawingCache()
10372 */
10373 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010374 if (color != mDrawingCacheBackgroundColor) {
10375 mDrawingCacheBackgroundColor = color;
10376 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 }
10379
10380 /**
10381 * @see #setDrawingCacheBackgroundColor(int)
10382 *
10383 * @return The background color to used for the drawing cache's bitmap
10384 */
10385 public int getDrawingCacheBackgroundColor() {
10386 return mDrawingCacheBackgroundColor;
10387 }
10388
10389 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010390 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010391 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010392 * @see #buildDrawingCache(boolean)
10393 */
10394 public void buildDrawingCache() {
10395 buildDrawingCache(false);
10396 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010397
Romain Guyfbd8f692009-06-26 14:51:58 -070010398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10400 *
10401 * <p>If you call {@link #buildDrawingCache()} manually without calling
10402 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10403 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010404 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010405 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10406 * this method will create a bitmap of the same size as this view. Because this bitmap
10407 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10408 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10409 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10410 * size than the view. This implies that your application must be able to handle this
10411 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010412 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010413 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10414 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010415 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010416 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 *
10418 * @see #getDrawingCache()
10419 * @see #destroyDrawingCache()
10420 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010421 public void buildDrawingCache(boolean autoScale) {
10422 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010423 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010424 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425
10426 if (ViewDebug.TRACE_HIERARCHY) {
10427 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429
Romain Guy8506ab42009-06-11 17:35:47 -070010430 int width = mRight - mLeft;
10431 int height = mBottom - mTop;
10432
10433 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010434 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010435
Romain Guye1123222009-06-29 14:24:56 -070010436 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010437 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10438 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440
10441 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010442 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010443 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444
10445 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010446 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010447 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10449 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010450 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 return;
10452 }
10453
10454 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010455 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456
10457 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 Bitmap.Config quality;
10459 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010460 // Never pick ARGB_4444 because it looks awful
10461 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10463 case DRAWING_CACHE_QUALITY_AUTO:
10464 quality = Bitmap.Config.ARGB_8888;
10465 break;
10466 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010467 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468 break;
10469 case DRAWING_CACHE_QUALITY_HIGH:
10470 quality = Bitmap.Config.ARGB_8888;
10471 break;
10472 default:
10473 quality = Bitmap.Config.ARGB_8888;
10474 break;
10475 }
10476 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010477 // Optimization for translucent windows
10478 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010479 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 }
10481
10482 // Try to cleanup memory
10483 if (bitmap != null) bitmap.recycle();
10484
10485 try {
10486 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010487 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010488 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010489 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010490 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010491 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010492 }
Adam Powell26153a32010-11-08 15:22:27 -080010493 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 } catch (OutOfMemoryError e) {
10495 // If there is not enough memory to create the bitmap cache, just
10496 // ignore the issue as bitmap caches are not required to draw the
10497 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010498 if (autoScale) {
10499 mDrawingCache = null;
10500 } else {
10501 mUnscaledDrawingCache = null;
10502 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010503 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 return;
10505 }
10506
10507 clear = drawingCacheBackgroundColor != 0;
10508 }
10509
10510 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 if (attachInfo != null) {
10512 canvas = attachInfo.mCanvas;
10513 if (canvas == null) {
10514 canvas = new Canvas();
10515 }
10516 canvas.setBitmap(bitmap);
10517 // Temporarily clobber the cached Canvas in case one of our children
10518 // is also using a drawing cache. Without this, the children would
10519 // steal the canvas by attaching their own bitmap to it and bad, bad
10520 // thing would happen (invisible views, corrupted drawings, etc.)
10521 attachInfo.mCanvas = null;
10522 } else {
10523 // This case should hopefully never or seldom happen
10524 canvas = new Canvas(bitmap);
10525 }
10526
10527 if (clear) {
10528 bitmap.eraseColor(drawingCacheBackgroundColor);
10529 }
10530
10531 computeScroll();
10532 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010533
Romain Guye1123222009-06-29 14:24:56 -070010534 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010535 final float scale = attachInfo.mApplicationScale;
10536 canvas.scale(scale, scale);
10537 }
Joe Malin32736f02011-01-19 16:14:20 -080010538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 canvas.translate(-mScrollX, -mScrollY);
10540
Romain Guy5bcdff42009-05-14 21:27:18 -070010541 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010542 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10543 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010544 mPrivateFlags |= DRAWING_CACHE_VALID;
10545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546
10547 // Fast path for layouts with no backgrounds
10548 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10549 if (ViewDebug.TRACE_HIERARCHY) {
10550 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10551 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010552 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 dispatchDraw(canvas);
10554 } else {
10555 draw(canvas);
10556 }
10557
10558 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010559 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560
10561 if (attachInfo != null) {
10562 // Restore the cached Canvas for our siblings
10563 attachInfo.mCanvas = canvas;
10564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 }
10566 }
10567
10568 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010569 * Create a snapshot of the view into a bitmap. We should probably make
10570 * some form of this public, but should think about the API.
10571 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010572 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010573 int width = mRight - mLeft;
10574 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010575
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010576 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010577 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010578 width = (int) ((width * scale) + 0.5f);
10579 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010580
Romain Guy8c11e312009-09-14 15:15:30 -070010581 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010582 if (bitmap == null) {
10583 throw new OutOfMemoryError();
10584 }
10585
Romain Guyc529d8d2011-09-06 15:01:39 -070010586 Resources resources = getResources();
10587 if (resources != null) {
10588 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10589 }
Joe Malin32736f02011-01-19 16:14:20 -080010590
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010591 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010592 if (attachInfo != null) {
10593 canvas = attachInfo.mCanvas;
10594 if (canvas == null) {
10595 canvas = new Canvas();
10596 }
10597 canvas.setBitmap(bitmap);
10598 // Temporarily clobber the cached Canvas in case one of our children
10599 // is also using a drawing cache. Without this, the children would
10600 // steal the canvas by attaching their own bitmap to it and bad, bad
10601 // things would happen (invisible views, corrupted drawings, etc.)
10602 attachInfo.mCanvas = null;
10603 } else {
10604 // This case should hopefully never or seldom happen
10605 canvas = new Canvas(bitmap);
10606 }
10607
Romain Guy5bcdff42009-05-14 21:27:18 -070010608 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010609 bitmap.eraseColor(backgroundColor);
10610 }
10611
10612 computeScroll();
10613 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010614 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010615 canvas.translate(-mScrollX, -mScrollY);
10616
Romain Guy5bcdff42009-05-14 21:27:18 -070010617 // Temporarily remove the dirty mask
10618 int flags = mPrivateFlags;
10619 mPrivateFlags &= ~DIRTY_MASK;
10620
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010621 // Fast path for layouts with no backgrounds
10622 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10623 dispatchDraw(canvas);
10624 } else {
10625 draw(canvas);
10626 }
10627
Romain Guy5bcdff42009-05-14 21:27:18 -070010628 mPrivateFlags = flags;
10629
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010630 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010631 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010632
10633 if (attachInfo != null) {
10634 // Restore the cached Canvas for our siblings
10635 attachInfo.mCanvas = canvas;
10636 }
Romain Guy8506ab42009-06-11 17:35:47 -070010637
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010638 return bitmap;
10639 }
10640
10641 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 * Indicates whether this View is currently in edit mode. A View is usually
10643 * in edit mode when displayed within a developer tool. For instance, if
10644 * this View is being drawn by a visual user interface builder, this method
10645 * should return true.
10646 *
10647 * Subclasses should check the return value of this method to provide
10648 * different behaviors if their normal behavior might interfere with the
10649 * host environment. For instance: the class spawns a thread in its
10650 * constructor, the drawing code relies on device-specific features, etc.
10651 *
10652 * This method is usually checked in the drawing code of custom widgets.
10653 *
10654 * @return True if this View is in edit mode, false otherwise.
10655 */
10656 public boolean isInEditMode() {
10657 return false;
10658 }
10659
10660 /**
10661 * If the View draws content inside its padding and enables fading edges,
10662 * it needs to support padding offsets. Padding offsets are added to the
10663 * fading edges to extend the length of the fade so that it covers pixels
10664 * drawn inside the padding.
10665 *
10666 * Subclasses of this class should override this method if they need
10667 * to draw content inside the padding.
10668 *
10669 * @return True if padding offset must be applied, false otherwise.
10670 *
10671 * @see #getLeftPaddingOffset()
10672 * @see #getRightPaddingOffset()
10673 * @see #getTopPaddingOffset()
10674 * @see #getBottomPaddingOffset()
10675 *
10676 * @since CURRENT
10677 */
10678 protected boolean isPaddingOffsetRequired() {
10679 return false;
10680 }
10681
10682 /**
10683 * Amount by which to extend the left fading region. Called only when
10684 * {@link #isPaddingOffsetRequired()} returns true.
10685 *
10686 * @return The left padding offset in pixels.
10687 *
10688 * @see #isPaddingOffsetRequired()
10689 *
10690 * @since CURRENT
10691 */
10692 protected int getLeftPaddingOffset() {
10693 return 0;
10694 }
10695
10696 /**
10697 * Amount by which to extend the right fading region. Called only when
10698 * {@link #isPaddingOffsetRequired()} returns true.
10699 *
10700 * @return The right padding offset in pixels.
10701 *
10702 * @see #isPaddingOffsetRequired()
10703 *
10704 * @since CURRENT
10705 */
10706 protected int getRightPaddingOffset() {
10707 return 0;
10708 }
10709
10710 /**
10711 * Amount by which to extend the top fading region. Called only when
10712 * {@link #isPaddingOffsetRequired()} returns true.
10713 *
10714 * @return The top padding offset in pixels.
10715 *
10716 * @see #isPaddingOffsetRequired()
10717 *
10718 * @since CURRENT
10719 */
10720 protected int getTopPaddingOffset() {
10721 return 0;
10722 }
10723
10724 /**
10725 * Amount by which to extend the bottom fading region. Called only when
10726 * {@link #isPaddingOffsetRequired()} returns true.
10727 *
10728 * @return The bottom padding offset in pixels.
10729 *
10730 * @see #isPaddingOffsetRequired()
10731 *
10732 * @since CURRENT
10733 */
10734 protected int getBottomPaddingOffset() {
10735 return 0;
10736 }
10737
10738 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010739 * @hide
10740 * @param offsetRequired
10741 */
10742 protected int getFadeTop(boolean offsetRequired) {
10743 int top = mPaddingTop;
10744 if (offsetRequired) top += getTopPaddingOffset();
10745 return top;
10746 }
10747
10748 /**
10749 * @hide
10750 * @param offsetRequired
10751 */
10752 protected int getFadeHeight(boolean offsetRequired) {
10753 int padding = mPaddingTop;
10754 if (offsetRequired) padding += getTopPaddingOffset();
10755 return mBottom - mTop - mPaddingBottom - padding;
10756 }
10757
10758 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010759 * <p>Indicates whether this view is attached to an hardware accelerated
10760 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010761 *
Romain Guy2bffd262010-09-12 17:40:02 -070010762 * <p>Even if this method returns true, it does not mean that every call
10763 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10764 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10765 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10766 * window is hardware accelerated,
10767 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10768 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010769 *
Romain Guy2bffd262010-09-12 17:40:02 -070010770 * @return True if the view is attached to a window and the window is
10771 * hardware accelerated; false in any other case.
10772 */
10773 public boolean isHardwareAccelerated() {
10774 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10775 }
Joe Malin32736f02011-01-19 16:14:20 -080010776
Romain Guy2bffd262010-09-12 17:40:02 -070010777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 * Manually render this view (and all of its children) to the given Canvas.
10779 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010780 * called. When implementing a view, implement
10781 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10782 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 *
10784 * @param canvas The Canvas to which the View is rendered.
10785 */
10786 public void draw(Canvas canvas) {
10787 if (ViewDebug.TRACE_HIERARCHY) {
10788 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10789 }
10790
Romain Guy5bcdff42009-05-14 21:27:18 -070010791 final int privateFlags = mPrivateFlags;
10792 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10793 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10794 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010796 /*
10797 * Draw traversal performs several drawing steps which must be executed
10798 * in the appropriate order:
10799 *
10800 * 1. Draw the background
10801 * 2. If necessary, save the canvas' layers to prepare for fading
10802 * 3. Draw view's content
10803 * 4. Draw children
10804 * 5. If necessary, draw the fading edges and restore layers
10805 * 6. Draw decorations (scrollbars for instance)
10806 */
10807
10808 // Step 1, draw the background, if needed
10809 int saveCount;
10810
Romain Guy24443ea2009-05-11 11:56:30 -070010811 if (!dirtyOpaque) {
10812 final Drawable background = mBGDrawable;
10813 if (background != null) {
10814 final int scrollX = mScrollX;
10815 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010816
Romain Guy24443ea2009-05-11 11:56:30 -070010817 if (mBackgroundSizeChanged) {
10818 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10819 mBackgroundSizeChanged = false;
10820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010821
Romain Guy24443ea2009-05-11 11:56:30 -070010822 if ((scrollX | scrollY) == 0) {
10823 background.draw(canvas);
10824 } else {
10825 canvas.translate(scrollX, scrollY);
10826 background.draw(canvas);
10827 canvas.translate(-scrollX, -scrollY);
10828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 }
10830 }
10831
10832 // skip step 2 & 5 if possible (common case)
10833 final int viewFlags = mViewFlags;
10834 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10835 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10836 if (!verticalEdges && !horizontalEdges) {
10837 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010838 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839
10840 // Step 4, draw the children
10841 dispatchDraw(canvas);
10842
10843 // Step 6, draw decorations (scrollbars)
10844 onDrawScrollBars(canvas);
10845
10846 // we're done...
10847 return;
10848 }
10849
10850 /*
10851 * Here we do the full fledged routine...
10852 * (this is an uncommon case where speed matters less,
10853 * this is why we repeat some of the tests that have been
10854 * done above)
10855 */
10856
10857 boolean drawTop = false;
10858 boolean drawBottom = false;
10859 boolean drawLeft = false;
10860 boolean drawRight = false;
10861
10862 float topFadeStrength = 0.0f;
10863 float bottomFadeStrength = 0.0f;
10864 float leftFadeStrength = 0.0f;
10865 float rightFadeStrength = 0.0f;
10866
10867 // Step 2, save the canvas' layers
10868 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869
10870 final boolean offsetRequired = isPaddingOffsetRequired();
10871 if (offsetRequired) {
10872 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 }
10874
10875 int left = mScrollX + paddingLeft;
10876 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010877 int top = mScrollY + getFadeTop(offsetRequired);
10878 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010879
10880 if (offsetRequired) {
10881 right += getRightPaddingOffset();
10882 bottom += getBottomPaddingOffset();
10883 }
10884
10885 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010886 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10887 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010888
10889 // clip the fade length if top and bottom fades overlap
10890 // overlapping fades produce odd-looking artifacts
10891 if (verticalEdges && (top + length > bottom - length)) {
10892 length = (bottom - top) / 2;
10893 }
10894
10895 // also clip horizontal fades if necessary
10896 if (horizontalEdges && (left + length > right - length)) {
10897 length = (right - left) / 2;
10898 }
10899
10900 if (verticalEdges) {
10901 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010902 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010903 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010904 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905 }
10906
10907 if (horizontalEdges) {
10908 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010909 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010911 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 }
10913
10914 saveCount = canvas.getSaveCount();
10915
10916 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010917 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010918 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10919
10920 if (drawTop) {
10921 canvas.saveLayer(left, top, right, top + length, null, flags);
10922 }
10923
10924 if (drawBottom) {
10925 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10926 }
10927
10928 if (drawLeft) {
10929 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10930 }
10931
10932 if (drawRight) {
10933 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10934 }
10935 } else {
10936 scrollabilityCache.setFadeColor(solidColor);
10937 }
10938
10939 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010940 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941
10942 // Step 4, draw the children
10943 dispatchDraw(canvas);
10944
10945 // Step 5, draw the fade effect and restore layers
10946 final Paint p = scrollabilityCache.paint;
10947 final Matrix matrix = scrollabilityCache.matrix;
10948 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949
10950 if (drawTop) {
10951 matrix.setScale(1, fadeHeight * topFadeStrength);
10952 matrix.postTranslate(left, top);
10953 fade.setLocalMatrix(matrix);
10954 canvas.drawRect(left, top, right, top + length, p);
10955 }
10956
10957 if (drawBottom) {
10958 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10959 matrix.postRotate(180);
10960 matrix.postTranslate(left, bottom);
10961 fade.setLocalMatrix(matrix);
10962 canvas.drawRect(left, bottom - length, right, bottom, p);
10963 }
10964
10965 if (drawLeft) {
10966 matrix.setScale(1, fadeHeight * leftFadeStrength);
10967 matrix.postRotate(-90);
10968 matrix.postTranslate(left, top);
10969 fade.setLocalMatrix(matrix);
10970 canvas.drawRect(left, top, left + length, bottom, p);
10971 }
10972
10973 if (drawRight) {
10974 matrix.setScale(1, fadeHeight * rightFadeStrength);
10975 matrix.postRotate(90);
10976 matrix.postTranslate(right, top);
10977 fade.setLocalMatrix(matrix);
10978 canvas.drawRect(right - length, top, right, bottom, p);
10979 }
10980
10981 canvas.restoreToCount(saveCount);
10982
10983 // Step 6, draw decorations (scrollbars)
10984 onDrawScrollBars(canvas);
10985 }
10986
10987 /**
10988 * Override this if your view is known to always be drawn on top of a solid color background,
10989 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10990 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10991 * should be set to 0xFF.
10992 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010993 * @see #setVerticalFadingEdgeEnabled(boolean)
10994 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995 *
10996 * @return The known solid color background for this view, or 0 if the color may vary
10997 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010998 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 public int getSolidColor() {
11000 return 0;
11001 }
11002
11003 /**
11004 * Build a human readable string representation of the specified view flags.
11005 *
11006 * @param flags the view flags to convert to a string
11007 * @return a String representing the supplied flags
11008 */
11009 private static String printFlags(int flags) {
11010 String output = "";
11011 int numFlags = 0;
11012 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11013 output += "TAKES_FOCUS";
11014 numFlags++;
11015 }
11016
11017 switch (flags & VISIBILITY_MASK) {
11018 case INVISIBLE:
11019 if (numFlags > 0) {
11020 output += " ";
11021 }
11022 output += "INVISIBLE";
11023 // USELESS HERE numFlags++;
11024 break;
11025 case GONE:
11026 if (numFlags > 0) {
11027 output += " ";
11028 }
11029 output += "GONE";
11030 // USELESS HERE numFlags++;
11031 break;
11032 default:
11033 break;
11034 }
11035 return output;
11036 }
11037
11038 /**
11039 * Build a human readable string representation of the specified private
11040 * view flags.
11041 *
11042 * @param privateFlags the private view flags to convert to a string
11043 * @return a String representing the supplied flags
11044 */
11045 private static String printPrivateFlags(int privateFlags) {
11046 String output = "";
11047 int numFlags = 0;
11048
11049 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11050 output += "WANTS_FOCUS";
11051 numFlags++;
11052 }
11053
11054 if ((privateFlags & FOCUSED) == FOCUSED) {
11055 if (numFlags > 0) {
11056 output += " ";
11057 }
11058 output += "FOCUSED";
11059 numFlags++;
11060 }
11061
11062 if ((privateFlags & SELECTED) == SELECTED) {
11063 if (numFlags > 0) {
11064 output += " ";
11065 }
11066 output += "SELECTED";
11067 numFlags++;
11068 }
11069
11070 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11071 if (numFlags > 0) {
11072 output += " ";
11073 }
11074 output += "IS_ROOT_NAMESPACE";
11075 numFlags++;
11076 }
11077
11078 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11079 if (numFlags > 0) {
11080 output += " ";
11081 }
11082 output += "HAS_BOUNDS";
11083 numFlags++;
11084 }
11085
11086 if ((privateFlags & DRAWN) == DRAWN) {
11087 if (numFlags > 0) {
11088 output += " ";
11089 }
11090 output += "DRAWN";
11091 // USELESS HERE numFlags++;
11092 }
11093 return output;
11094 }
11095
11096 /**
11097 * <p>Indicates whether or not this view's layout will be requested during
11098 * the next hierarchy layout pass.</p>
11099 *
11100 * @return true if the layout will be forced during next layout pass
11101 */
11102 public boolean isLayoutRequested() {
11103 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11104 }
11105
11106 /**
11107 * Assign a size and position to a view and all of its
11108 * descendants
11109 *
11110 * <p>This is the second phase of the layout mechanism.
11111 * (The first is measuring). In this phase, each parent calls
11112 * layout on all of its children to position them.
11113 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011114 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 *
Chet Haase9c087442011-01-12 16:20:16 -080011116 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011117 * Derived classes with children should override
11118 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011119 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 *
11121 * @param l Left position, relative to parent
11122 * @param t Top position, relative to parent
11123 * @param r Right position, relative to parent
11124 * @param b Bottom position, relative to parent
11125 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011126 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011127 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011128 int oldL = mLeft;
11129 int oldT = mTop;
11130 int oldB = mBottom;
11131 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 boolean changed = setFrame(l, t, r, b);
11133 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11134 if (ViewDebug.TRACE_HIERARCHY) {
11135 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11136 }
11137
11138 onLayout(changed, l, t, r, b);
11139 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011140
11141 if (mOnLayoutChangeListeners != null) {
11142 ArrayList<OnLayoutChangeListener> listenersCopy =
11143 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11144 int numListeners = listenersCopy.size();
11145 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011146 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011147 }
11148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 }
11150 mPrivateFlags &= ~FORCE_LAYOUT;
11151 }
11152
11153 /**
11154 * Called from layout when this view should
11155 * assign a size and position to each of its children.
11156 *
11157 * Derived classes with children should override
11158 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011159 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 * @param changed This is a new size or position for this view
11161 * @param left Left position, relative to parent
11162 * @param top Top position, relative to parent
11163 * @param right Right position, relative to parent
11164 * @param bottom Bottom position, relative to parent
11165 */
11166 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11167 }
11168
11169 /**
11170 * Assign a size and position to this view.
11171 *
11172 * This is called from layout.
11173 *
11174 * @param left Left position, relative to parent
11175 * @param top Top position, relative to parent
11176 * @param right Right position, relative to parent
11177 * @param bottom Bottom position, relative to parent
11178 * @return true if the new size and position are different than the
11179 * previous ones
11180 * {@hide}
11181 */
11182 protected boolean setFrame(int left, int top, int right, int bottom) {
11183 boolean changed = false;
11184
11185 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011186 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 + right + "," + bottom + ")");
11188 }
11189
11190 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11191 changed = true;
11192
11193 // Remember our drawn bit
11194 int drawn = mPrivateFlags & DRAWN;
11195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011196 int oldWidth = mRight - mLeft;
11197 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011198 int newWidth = right - left;
11199 int newHeight = bottom - top;
11200 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11201
11202 // Invalidate our old position
11203 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011204
11205 mLeft = left;
11206 mTop = top;
11207 mRight = right;
11208 mBottom = bottom;
11209
11210 mPrivateFlags |= HAS_BOUNDS;
11211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011212
Chet Haase75755e22011-07-18 17:48:25 -070011213 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011214 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11215 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011216 if (mTransformationInfo != null) {
11217 mTransformationInfo.mMatrixDirty = true;
11218 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011220 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11221 }
11222
11223 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11224 // If we are visible, force the DRAWN bit to on so that
11225 // this invalidate will go through (at least to our parent).
11226 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011227 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011228 // the DRAWN bit.
11229 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011230 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011231 // parent display list may need to be recreated based on a change in the bounds
11232 // of any child
11233 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 }
11235
11236 // Reset drawn bit to original value (invalidate turns it off)
11237 mPrivateFlags |= drawn;
11238
11239 mBackgroundSizeChanged = true;
11240 }
11241 return changed;
11242 }
11243
11244 /**
11245 * Finalize inflating a view from XML. This is called as the last phase
11246 * of inflation, after all child views have been added.
11247 *
11248 * <p>Even if the subclass overrides onFinishInflate, they should always be
11249 * sure to call the super method, so that we get called.
11250 */
11251 protected void onFinishInflate() {
11252 }
11253
11254 /**
11255 * Returns the resources associated with this view.
11256 *
11257 * @return Resources object.
11258 */
11259 public Resources getResources() {
11260 return mResources;
11261 }
11262
11263 /**
11264 * Invalidates the specified Drawable.
11265 *
11266 * @param drawable the drawable to invalidate
11267 */
11268 public void invalidateDrawable(Drawable drawable) {
11269 if (verifyDrawable(drawable)) {
11270 final Rect dirty = drawable.getBounds();
11271 final int scrollX = mScrollX;
11272 final int scrollY = mScrollY;
11273
11274 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11275 dirty.right + scrollX, dirty.bottom + scrollY);
11276 }
11277 }
11278
11279 /**
11280 * Schedules an action on a drawable to occur at a specified time.
11281 *
11282 * @param who the recipient of the action
11283 * @param what the action to run on the drawable
11284 * @param when the time at which the action must occur. Uses the
11285 * {@link SystemClock#uptimeMillis} timebase.
11286 */
11287 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11288 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11289 mAttachInfo.mHandler.postAtTime(what, who, when);
11290 }
11291 }
11292
11293 /**
11294 * Cancels a scheduled action on a drawable.
11295 *
11296 * @param who the recipient of the action
11297 * @param what the action to cancel
11298 */
11299 public void unscheduleDrawable(Drawable who, Runnable what) {
11300 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11301 mAttachInfo.mHandler.removeCallbacks(what, who);
11302 }
11303 }
11304
11305 /**
11306 * Unschedule any events associated with the given Drawable. This can be
11307 * used when selecting a new Drawable into a view, so that the previous
11308 * one is completely unscheduled.
11309 *
11310 * @param who The Drawable to unschedule.
11311 *
11312 * @see #drawableStateChanged
11313 */
11314 public void unscheduleDrawable(Drawable who) {
11315 if (mAttachInfo != null) {
11316 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11317 }
11318 }
11319
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011320 /**
11321 * Return the layout direction of a given Drawable.
11322 *
11323 * @param who the Drawable to query
11324 *
11325 * @hide
11326 */
11327 public int getResolvedLayoutDirection(Drawable who) {
11328 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011329 }
11330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 /**
11332 * If your view subclass is displaying its own Drawable objects, it should
11333 * override this function and return true for any Drawable it is
11334 * displaying. This allows animations for those drawables to be
11335 * scheduled.
11336 *
11337 * <p>Be sure to call through to the super class when overriding this
11338 * function.
11339 *
11340 * @param who The Drawable to verify. Return true if it is one you are
11341 * displaying, else return the result of calling through to the
11342 * super class.
11343 *
11344 * @return boolean If true than the Drawable is being displayed in the
11345 * view; else false and it is not allowed to animate.
11346 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011347 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11348 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349 */
11350 protected boolean verifyDrawable(Drawable who) {
11351 return who == mBGDrawable;
11352 }
11353
11354 /**
11355 * This function is called whenever the state of the view changes in such
11356 * a way that it impacts the state of drawables being shown.
11357 *
11358 * <p>Be sure to call through to the superclass when overriding this
11359 * function.
11360 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011361 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011362 */
11363 protected void drawableStateChanged() {
11364 Drawable d = mBGDrawable;
11365 if (d != null && d.isStateful()) {
11366 d.setState(getDrawableState());
11367 }
11368 }
11369
11370 /**
11371 * Call this to force a view to update its drawable state. This will cause
11372 * drawableStateChanged to be called on this view. Views that are interested
11373 * in the new state should call getDrawableState.
11374 *
11375 * @see #drawableStateChanged
11376 * @see #getDrawableState
11377 */
11378 public void refreshDrawableState() {
11379 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11380 drawableStateChanged();
11381
11382 ViewParent parent = mParent;
11383 if (parent != null) {
11384 parent.childDrawableStateChanged(this);
11385 }
11386 }
11387
11388 /**
11389 * Return an array of resource IDs of the drawable states representing the
11390 * current state of the view.
11391 *
11392 * @return The current drawable state
11393 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011394 * @see Drawable#setState(int[])
11395 * @see #drawableStateChanged()
11396 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397 */
11398 public final int[] getDrawableState() {
11399 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11400 return mDrawableState;
11401 } else {
11402 mDrawableState = onCreateDrawableState(0);
11403 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11404 return mDrawableState;
11405 }
11406 }
11407
11408 /**
11409 * Generate the new {@link android.graphics.drawable.Drawable} state for
11410 * this view. This is called by the view
11411 * system when the cached Drawable state is determined to be invalid. To
11412 * retrieve the current state, you should use {@link #getDrawableState}.
11413 *
11414 * @param extraSpace if non-zero, this is the number of extra entries you
11415 * would like in the returned array in which you can place your own
11416 * states.
11417 *
11418 * @return Returns an array holding the current {@link Drawable} state of
11419 * the view.
11420 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011421 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011422 */
11423 protected int[] onCreateDrawableState(int extraSpace) {
11424 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11425 mParent instanceof View) {
11426 return ((View) mParent).onCreateDrawableState(extraSpace);
11427 }
11428
11429 int[] drawableState;
11430
11431 int privateFlags = mPrivateFlags;
11432
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011433 int viewStateIndex = 0;
11434 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11435 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11436 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011437 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011438 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11439 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011440 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11441 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011442 // This is set if HW acceleration is requested, even if the current
11443 // process doesn't allow it. This is just to allow app preview
11444 // windows to better match their app.
11445 viewStateIndex |= VIEW_STATE_ACCELERATED;
11446 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011447 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448
Christopher Tate3d4bf172011-03-28 16:16:46 -070011449 final int privateFlags2 = mPrivateFlags2;
11450 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11451 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011453 drawableState = VIEW_STATE_SETS[viewStateIndex];
11454
11455 //noinspection ConstantIfStatement
11456 if (false) {
11457 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11458 Log.i("View", toString()
11459 + " pressed=" + ((privateFlags & PRESSED) != 0)
11460 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11461 + " fo=" + hasFocus()
11462 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011463 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 + ": " + Arrays.toString(drawableState));
11465 }
11466
11467 if (extraSpace == 0) {
11468 return drawableState;
11469 }
11470
11471 final int[] fullState;
11472 if (drawableState != null) {
11473 fullState = new int[drawableState.length + extraSpace];
11474 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11475 } else {
11476 fullState = new int[extraSpace];
11477 }
11478
11479 return fullState;
11480 }
11481
11482 /**
11483 * Merge your own state values in <var>additionalState</var> into the base
11484 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011485 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 *
11487 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011488 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 * own additional state values.
11490 *
11491 * @param additionalState The additional state values you would like
11492 * added to <var>baseState</var>; this array is not modified.
11493 *
11494 * @return As a convenience, the <var>baseState</var> array you originally
11495 * passed into the function is returned.
11496 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011497 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 */
11499 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11500 final int N = baseState.length;
11501 int i = N - 1;
11502 while (i >= 0 && baseState[i] == 0) {
11503 i--;
11504 }
11505 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11506 return baseState;
11507 }
11508
11509 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011510 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11511 * on all Drawable objects associated with this view.
11512 */
11513 public void jumpDrawablesToCurrentState() {
11514 if (mBGDrawable != null) {
11515 mBGDrawable.jumpToCurrentState();
11516 }
11517 }
11518
11519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011520 * Sets the background color for this view.
11521 * @param color the color of the background
11522 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011523 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011524 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011525 if (mBGDrawable instanceof ColorDrawable) {
11526 ((ColorDrawable) mBGDrawable).setColor(color);
11527 } else {
11528 setBackgroundDrawable(new ColorDrawable(color));
11529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011530 }
11531
11532 /**
11533 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011534 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011535 * @param resid The identifier of the resource.
11536 * @attr ref android.R.styleable#View_background
11537 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011538 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011539 public void setBackgroundResource(int resid) {
11540 if (resid != 0 && resid == mBackgroundResource) {
11541 return;
11542 }
11543
11544 Drawable d= null;
11545 if (resid != 0) {
11546 d = mResources.getDrawable(resid);
11547 }
11548 setBackgroundDrawable(d);
11549
11550 mBackgroundResource = resid;
11551 }
11552
11553 /**
11554 * Set the background to a given Drawable, or remove the background. If the
11555 * background has padding, this View's padding is set to the background's
11556 * padding. However, when a background is removed, this View's padding isn't
11557 * touched. If setting the padding is desired, please use
11558 * {@link #setPadding(int, int, int, int)}.
11559 *
11560 * @param d The Drawable to use as the background, or null to remove the
11561 * background
11562 */
11563 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011564 if (d == mBGDrawable) {
11565 return;
11566 }
11567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011568 boolean requestLayout = false;
11569
11570 mBackgroundResource = 0;
11571
11572 /*
11573 * Regardless of whether we're setting a new background or not, we want
11574 * to clear the previous drawable.
11575 */
11576 if (mBGDrawable != null) {
11577 mBGDrawable.setCallback(null);
11578 unscheduleDrawable(mBGDrawable);
11579 }
11580
11581 if (d != null) {
11582 Rect padding = sThreadLocal.get();
11583 if (padding == null) {
11584 padding = new Rect();
11585 sThreadLocal.set(padding);
11586 }
11587 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011588 switch (d.getResolvedLayoutDirectionSelf()) {
11589 case LAYOUT_DIRECTION_RTL:
11590 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11591 break;
11592 case LAYOUT_DIRECTION_LTR:
11593 default:
11594 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011596 }
11597
11598 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11599 // if it has a different minimum size, we should layout again
11600 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11601 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11602 requestLayout = true;
11603 }
11604
11605 d.setCallback(this);
11606 if (d.isStateful()) {
11607 d.setState(getDrawableState());
11608 }
11609 d.setVisible(getVisibility() == VISIBLE, false);
11610 mBGDrawable = d;
11611
11612 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11613 mPrivateFlags &= ~SKIP_DRAW;
11614 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11615 requestLayout = true;
11616 }
11617 } else {
11618 /* Remove the background */
11619 mBGDrawable = null;
11620
11621 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11622 /*
11623 * This view ONLY drew the background before and we're removing
11624 * the background, so now it won't draw anything
11625 * (hence we SKIP_DRAW)
11626 */
11627 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11628 mPrivateFlags |= SKIP_DRAW;
11629 }
11630
11631 /*
11632 * When the background is set, we try to apply its padding to this
11633 * View. When the background is removed, we don't touch this View's
11634 * padding. This is noted in the Javadocs. Hence, we don't need to
11635 * requestLayout(), the invalidate() below is sufficient.
11636 */
11637
11638 // The old background's minimum size could have affected this
11639 // View's layout, so let's requestLayout
11640 requestLayout = true;
11641 }
11642
Romain Guy8f1344f52009-05-15 16:03:59 -070011643 computeOpaqueFlags();
11644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011645 if (requestLayout) {
11646 requestLayout();
11647 }
11648
11649 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011650 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011651 }
11652
11653 /**
11654 * Gets the background drawable
11655 * @return The drawable used as the background for this view, if any.
11656 */
11657 public Drawable getBackground() {
11658 return mBGDrawable;
11659 }
11660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011661 /**
11662 * Sets the padding. The view may add on the space required to display
11663 * the scrollbars, depending on the style and visibility of the scrollbars.
11664 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11665 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11666 * from the values set in this call.
11667 *
11668 * @attr ref android.R.styleable#View_padding
11669 * @attr ref android.R.styleable#View_paddingBottom
11670 * @attr ref android.R.styleable#View_paddingLeft
11671 * @attr ref android.R.styleable#View_paddingRight
11672 * @attr ref android.R.styleable#View_paddingTop
11673 * @param left the left padding in pixels
11674 * @param top the top padding in pixels
11675 * @param right the right padding in pixels
11676 * @param bottom the bottom padding in pixels
11677 */
11678 public void setPadding(int left, int top, int right, int bottom) {
11679 boolean changed = false;
11680
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011681 mUserPaddingRelative = false;
11682
Adam Powell20232d02010-12-08 21:08:53 -080011683 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 mUserPaddingRight = right;
11685 mUserPaddingBottom = bottom;
11686
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011687 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011688
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011689 // Common case is there are no scroll bars.
11690 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011691 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011692 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011693 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011694 switch (mVerticalScrollbarPosition) {
11695 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011696 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11697 left += offset;
11698 } else {
11699 right += offset;
11700 }
11701 break;
Adam Powell20232d02010-12-08 21:08:53 -080011702 case SCROLLBAR_POSITION_RIGHT:
11703 right += offset;
11704 break;
11705 case SCROLLBAR_POSITION_LEFT:
11706 left += offset;
11707 break;
11708 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011709 }
Adam Powell20232d02010-12-08 21:08:53 -080011710 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011711 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11712 ? 0 : getHorizontalScrollbarHeight();
11713 }
11714 }
Romain Guy8506ab42009-06-11 17:35:47 -070011715
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011716 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011717 changed = true;
11718 mPaddingLeft = left;
11719 }
11720 if (mPaddingTop != top) {
11721 changed = true;
11722 mPaddingTop = top;
11723 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011724 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011726 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011727 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011728 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011729 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011730 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011731 }
11732
11733 if (changed) {
11734 requestLayout();
11735 }
11736 }
11737
11738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011739 * Sets the relative padding. The view may add on the space required to display
11740 * the scrollbars, depending on the style and visibility of the scrollbars.
11741 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11742 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11743 * from the values set in this call.
11744 *
11745 * @attr ref android.R.styleable#View_padding
11746 * @attr ref android.R.styleable#View_paddingBottom
11747 * @attr ref android.R.styleable#View_paddingStart
11748 * @attr ref android.R.styleable#View_paddingEnd
11749 * @attr ref android.R.styleable#View_paddingTop
11750 * @param start the start padding in pixels
11751 * @param top the top padding in pixels
11752 * @param end the end padding in pixels
11753 * @param bottom the bottom padding in pixels
11754 *
11755 * @hide
11756 */
11757 public void setPaddingRelative(int start, int top, int end, int bottom) {
11758 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011759
11760 mUserPaddingStart = start;
11761 mUserPaddingEnd = end;
11762
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011763 switch(getResolvedLayoutDirection()) {
11764 case LAYOUT_DIRECTION_RTL:
11765 setPadding(end, top, start, bottom);
11766 break;
11767 case LAYOUT_DIRECTION_LTR:
11768 default:
11769 setPadding(start, top, end, bottom);
11770 }
11771 }
11772
11773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011774 * Returns the top padding of this view.
11775 *
11776 * @return the top padding in pixels
11777 */
11778 public int getPaddingTop() {
11779 return mPaddingTop;
11780 }
11781
11782 /**
11783 * Returns the bottom padding of this view. If there are inset and enabled
11784 * scrollbars, this value may include the space required to display the
11785 * scrollbars as well.
11786 *
11787 * @return the bottom padding in pixels
11788 */
11789 public int getPaddingBottom() {
11790 return mPaddingBottom;
11791 }
11792
11793 /**
11794 * Returns the left padding of this view. If there are inset and enabled
11795 * scrollbars, this value may include the space required to display the
11796 * scrollbars as well.
11797 *
11798 * @return the left padding in pixels
11799 */
11800 public int getPaddingLeft() {
11801 return mPaddingLeft;
11802 }
11803
11804 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011805 * Returns the start padding of this view. If there are inset and enabled
11806 * scrollbars, this value may include the space required to display the
11807 * scrollbars as well.
11808 *
11809 * @return the start padding in pixels
11810 *
11811 * @hide
11812 */
11813 public int getPaddingStart() {
11814 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11815 mPaddingRight : mPaddingLeft;
11816 }
11817
11818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011819 * Returns the right padding of this view. If there are inset and enabled
11820 * scrollbars, this value may include the space required to display the
11821 * scrollbars as well.
11822 *
11823 * @return the right padding in pixels
11824 */
11825 public int getPaddingRight() {
11826 return mPaddingRight;
11827 }
11828
11829 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011830 * Returns the end padding of this view. If there are inset and enabled
11831 * scrollbars, this value may include the space required to display the
11832 * scrollbars as well.
11833 *
11834 * @return the end padding in pixels
11835 *
11836 * @hide
11837 */
11838 public int getPaddingEnd() {
11839 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11840 mPaddingLeft : mPaddingRight;
11841 }
11842
11843 /**
11844 * Return if the padding as been set thru relative values
11845 * {@link #setPaddingRelative(int, int, int, int)} or thru
11846 * @attr ref android.R.styleable#View_paddingStart or
11847 * @attr ref android.R.styleable#View_paddingEnd
11848 *
11849 * @return true if the padding is relative or false if it is not.
11850 *
11851 * @hide
11852 */
11853 public boolean isPaddingRelative() {
11854 return mUserPaddingRelative;
11855 }
11856
11857 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 * Changes the selection state of this view. A view can be selected or not.
11859 * Note that selection is not the same as focus. Views are typically
11860 * selected in the context of an AdapterView like ListView or GridView;
11861 * the selected view is the view that is highlighted.
11862 *
11863 * @param selected true if the view must be selected, false otherwise
11864 */
11865 public void setSelected(boolean selected) {
11866 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11867 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011868 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011869 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 refreshDrawableState();
11871 dispatchSetSelected(selected);
11872 }
11873 }
11874
11875 /**
11876 * Dispatch setSelected to all of this View's children.
11877 *
11878 * @see #setSelected(boolean)
11879 *
11880 * @param selected The new selected state
11881 */
11882 protected void dispatchSetSelected(boolean selected) {
11883 }
11884
11885 /**
11886 * Indicates the selection state of this view.
11887 *
11888 * @return true if the view is selected, false otherwise
11889 */
11890 @ViewDebug.ExportedProperty
11891 public boolean isSelected() {
11892 return (mPrivateFlags & SELECTED) != 0;
11893 }
11894
11895 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011896 * Changes the activated state of this view. A view can be activated or not.
11897 * Note that activation is not the same as selection. Selection is
11898 * a transient property, representing the view (hierarchy) the user is
11899 * currently interacting with. Activation is a longer-term state that the
11900 * user can move views in and out of. For example, in a list view with
11901 * single or multiple selection enabled, the views in the current selection
11902 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11903 * here.) The activated state is propagated down to children of the view it
11904 * is set on.
11905 *
11906 * @param activated true if the view must be activated, false otherwise
11907 */
11908 public void setActivated(boolean activated) {
11909 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11910 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011911 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011912 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011913 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011914 }
11915 }
11916
11917 /**
11918 * Dispatch setActivated to all of this View's children.
11919 *
11920 * @see #setActivated(boolean)
11921 *
11922 * @param activated The new activated state
11923 */
11924 protected void dispatchSetActivated(boolean activated) {
11925 }
11926
11927 /**
11928 * Indicates the activation state of this view.
11929 *
11930 * @return true if the view is activated, false otherwise
11931 */
11932 @ViewDebug.ExportedProperty
11933 public boolean isActivated() {
11934 return (mPrivateFlags & ACTIVATED) != 0;
11935 }
11936
11937 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011938 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11939 * observer can be used to get notifications when global events, like
11940 * layout, happen.
11941 *
11942 * The returned ViewTreeObserver observer is not guaranteed to remain
11943 * valid for the lifetime of this View. If the caller of this method keeps
11944 * a long-lived reference to ViewTreeObserver, it should always check for
11945 * the return value of {@link ViewTreeObserver#isAlive()}.
11946 *
11947 * @return The ViewTreeObserver for this view's hierarchy.
11948 */
11949 public ViewTreeObserver getViewTreeObserver() {
11950 if (mAttachInfo != null) {
11951 return mAttachInfo.mTreeObserver;
11952 }
11953 if (mFloatingTreeObserver == null) {
11954 mFloatingTreeObserver = new ViewTreeObserver();
11955 }
11956 return mFloatingTreeObserver;
11957 }
11958
11959 /**
11960 * <p>Finds the topmost view in the current view hierarchy.</p>
11961 *
11962 * @return the topmost view containing this view
11963 */
11964 public View getRootView() {
11965 if (mAttachInfo != null) {
11966 final View v = mAttachInfo.mRootView;
11967 if (v != null) {
11968 return v;
11969 }
11970 }
Romain Guy8506ab42009-06-11 17:35:47 -070011971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011972 View parent = this;
11973
11974 while (parent.mParent != null && parent.mParent instanceof View) {
11975 parent = (View) parent.mParent;
11976 }
11977
11978 return parent;
11979 }
11980
11981 /**
11982 * <p>Computes the coordinates of this view on the screen. The argument
11983 * must be an array of two integers. After the method returns, the array
11984 * contains the x and y location in that order.</p>
11985 *
11986 * @param location an array of two integers in which to hold the coordinates
11987 */
11988 public void getLocationOnScreen(int[] location) {
11989 getLocationInWindow(location);
11990
11991 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011992 if (info != null) {
11993 location[0] += info.mWindowLeft;
11994 location[1] += info.mWindowTop;
11995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 }
11997
11998 /**
11999 * <p>Computes the coordinates of this view in its window. The argument
12000 * must be an array of two integers. After the method returns, the array
12001 * contains the x and y location in that order.</p>
12002 *
12003 * @param location an array of two integers in which to hold the coordinates
12004 */
12005 public void getLocationInWindow(int[] location) {
12006 if (location == null || location.length < 2) {
12007 throw new IllegalArgumentException("location must be an array of "
12008 + "two integers");
12009 }
12010
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012011 location[0] = mLeft;
12012 location[1] = mTop;
12013 if (mTransformationInfo != null) {
12014 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12015 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012017
12018 ViewParent viewParent = mParent;
12019 while (viewParent instanceof View) {
12020 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012021 location[0] += view.mLeft - view.mScrollX;
12022 location[1] += view.mTop - view.mScrollY;
12023 if (view.mTransformationInfo != null) {
12024 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12025 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012027 viewParent = view.mParent;
12028 }
Romain Guy8506ab42009-06-11 17:35:47 -070012029
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012030 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012032 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012033 location[1] -= vr.mCurScrollY;
12034 }
12035 }
12036
12037 /**
12038 * {@hide}
12039 * @param id the id of the view to be found
12040 * @return the view of the specified id, null if cannot be found
12041 */
12042 protected View findViewTraversal(int id) {
12043 if (id == mID) {
12044 return this;
12045 }
12046 return null;
12047 }
12048
12049 /**
12050 * {@hide}
12051 * @param tag the tag of the view to be found
12052 * @return the view of specified tag, null if cannot be found
12053 */
12054 protected View findViewWithTagTraversal(Object tag) {
12055 if (tag != null && tag.equals(mTag)) {
12056 return this;
12057 }
12058 return null;
12059 }
12060
12061 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012062 * {@hide}
12063 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012064 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012065 * @return The first view that matches the predicate or null.
12066 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012067 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012068 if (predicate.apply(this)) {
12069 return this;
12070 }
12071 return null;
12072 }
12073
12074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075 * Look for a child view with the given id. If this view has the given
12076 * id, return this view.
12077 *
12078 * @param id The id to search for.
12079 * @return The view that has the given id in the hierarchy or null
12080 */
12081 public final View findViewById(int id) {
12082 if (id < 0) {
12083 return null;
12084 }
12085 return findViewTraversal(id);
12086 }
12087
12088 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012089 * Finds a view by its unuque and stable accessibility id.
12090 *
12091 * @param accessibilityId The searched accessibility id.
12092 * @return The found view.
12093 */
12094 final View findViewByAccessibilityId(int accessibilityId) {
12095 if (accessibilityId < 0) {
12096 return null;
12097 }
12098 return findViewByAccessibilityIdTraversal(accessibilityId);
12099 }
12100
12101 /**
12102 * Performs the traversal to find a view by its unuque and stable accessibility id.
12103 *
12104 * <strong>Note:</strong>This method does not stop at the root namespace
12105 * boundary since the user can touch the screen at an arbitrary location
12106 * potentially crossing the root namespace bounday which will send an
12107 * accessibility event to accessibility services and they should be able
12108 * to obtain the event source. Also accessibility ids are guaranteed to be
12109 * unique in the window.
12110 *
12111 * @param accessibilityId The accessibility id.
12112 * @return The found view.
12113 */
12114 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12115 if (getAccessibilityViewId() == accessibilityId) {
12116 return this;
12117 }
12118 return null;
12119 }
12120
12121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 * Look for a child view with the given tag. If this view has the given
12123 * tag, return this view.
12124 *
12125 * @param tag The tag to search for, using "tag.equals(getTag())".
12126 * @return The View that has the given tag in the hierarchy or null
12127 */
12128 public final View findViewWithTag(Object tag) {
12129 if (tag == null) {
12130 return null;
12131 }
12132 return findViewWithTagTraversal(tag);
12133 }
12134
12135 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012136 * {@hide}
12137 * Look for a child view that matches the specified predicate.
12138 * If this view matches the predicate, return this view.
12139 *
12140 * @param predicate The predicate to evaluate.
12141 * @return The first view that matches the predicate or null.
12142 */
12143 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012144 return findViewByPredicateTraversal(predicate, null);
12145 }
12146
12147 /**
12148 * {@hide}
12149 * Look for a child view that matches the specified predicate,
12150 * starting with the specified view and its descendents and then
12151 * recusively searching the ancestors and siblings of that view
12152 * until this view is reached.
12153 *
12154 * This method is useful in cases where the predicate does not match
12155 * a single unique view (perhaps multiple views use the same id)
12156 * and we are trying to find the view that is "closest" in scope to the
12157 * starting view.
12158 *
12159 * @param start The view to start from.
12160 * @param predicate The predicate to evaluate.
12161 * @return The first view that matches the predicate or null.
12162 */
12163 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12164 View childToSkip = null;
12165 for (;;) {
12166 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12167 if (view != null || start == this) {
12168 return view;
12169 }
12170
12171 ViewParent parent = start.getParent();
12172 if (parent == null || !(parent instanceof View)) {
12173 return null;
12174 }
12175
12176 childToSkip = start;
12177 start = (View) parent;
12178 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012179 }
12180
12181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012182 * Sets the identifier for this view. The identifier does not have to be
12183 * unique in this view's hierarchy. The identifier should be a positive
12184 * number.
12185 *
12186 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012187 * @see #getId()
12188 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012189 *
12190 * @param id a number used to identify the view
12191 *
12192 * @attr ref android.R.styleable#View_id
12193 */
12194 public void setId(int id) {
12195 mID = id;
12196 }
12197
12198 /**
12199 * {@hide}
12200 *
12201 * @param isRoot true if the view belongs to the root namespace, false
12202 * otherwise
12203 */
12204 public void setIsRootNamespace(boolean isRoot) {
12205 if (isRoot) {
12206 mPrivateFlags |= IS_ROOT_NAMESPACE;
12207 } else {
12208 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12209 }
12210 }
12211
12212 /**
12213 * {@hide}
12214 *
12215 * @return true if the view belongs to the root namespace, false otherwise
12216 */
12217 public boolean isRootNamespace() {
12218 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12219 }
12220
12221 /**
12222 * Returns this view's identifier.
12223 *
12224 * @return a positive integer used to identify the view or {@link #NO_ID}
12225 * if the view has no ID
12226 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012227 * @see #setId(int)
12228 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012229 * @attr ref android.R.styleable#View_id
12230 */
12231 @ViewDebug.CapturedViewProperty
12232 public int getId() {
12233 return mID;
12234 }
12235
12236 /**
12237 * Returns this view's tag.
12238 *
12239 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012240 *
12241 * @see #setTag(Object)
12242 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012243 */
12244 @ViewDebug.ExportedProperty
12245 public Object getTag() {
12246 return mTag;
12247 }
12248
12249 /**
12250 * Sets the tag associated with this view. A tag can be used to mark
12251 * a view in its hierarchy and does not have to be unique within the
12252 * hierarchy. Tags can also be used to store data within a view without
12253 * resorting to another data structure.
12254 *
12255 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012256 *
12257 * @see #getTag()
12258 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012259 */
12260 public void setTag(final Object tag) {
12261 mTag = tag;
12262 }
12263
12264 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012265 * Returns the tag associated with this view and the specified key.
12266 *
12267 * @param key The key identifying the tag
12268 *
12269 * @return the Object stored in this view as a tag
12270 *
12271 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012272 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012273 */
12274 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012275 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012276 return null;
12277 }
12278
12279 /**
12280 * Sets a tag associated with this view and a key. A tag can be used
12281 * to mark a view in its hierarchy and does not have to be unique within
12282 * the hierarchy. Tags can also be used to store data within a view
12283 * without resorting to another data structure.
12284 *
12285 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012286 * application to ensure it is unique (see the <a
12287 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12288 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012289 * the Android framework or not associated with any package will cause
12290 * an {@link IllegalArgumentException} to be thrown.
12291 *
12292 * @param key The key identifying the tag
12293 * @param tag An Object to tag the view with
12294 *
12295 * @throws IllegalArgumentException If they specified key is not valid
12296 *
12297 * @see #setTag(Object)
12298 * @see #getTag(int)
12299 */
12300 public void setTag(int key, final Object tag) {
12301 // If the package id is 0x00 or 0x01, it's either an undefined package
12302 // or a framework id
12303 if ((key >>> 24) < 2) {
12304 throw new IllegalArgumentException("The key must be an application-specific "
12305 + "resource id.");
12306 }
12307
Adam Powell2b2f6d62011-09-23 11:15:39 -070012308 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012309 }
12310
12311 /**
12312 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12313 * framework id.
12314 *
12315 * @hide
12316 */
12317 public void setTagInternal(int key, Object tag) {
12318 if ((key >>> 24) != 0x1) {
12319 throw new IllegalArgumentException("The key must be a framework-specific "
12320 + "resource id.");
12321 }
12322
Adam Powell2b2f6d62011-09-23 11:15:39 -070012323 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012324 }
12325
Adam Powell2b2f6d62011-09-23 11:15:39 -070012326 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012327 if (mKeyedTags == null) {
12328 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012329 }
12330
Adam Powell7db82ac2011-09-22 19:44:04 -070012331 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012332 }
12333
12334 /**
Romain Guy13922e02009-05-12 17:56:14 -070012335 * @param consistency The type of consistency. See ViewDebug for more information.
12336 *
12337 * @hide
12338 */
12339 protected boolean dispatchConsistencyCheck(int consistency) {
12340 return onConsistencyCheck(consistency);
12341 }
12342
12343 /**
12344 * Method that subclasses should implement to check their consistency. The type of
12345 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012346 *
Romain Guy13922e02009-05-12 17:56:14 -070012347 * @param consistency The type of consistency. See ViewDebug for more information.
12348 *
12349 * @throws IllegalStateException if the view is in an inconsistent state.
12350 *
12351 * @hide
12352 */
12353 protected boolean onConsistencyCheck(int consistency) {
12354 boolean result = true;
12355
12356 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12357 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12358
12359 if (checkLayout) {
12360 if (getParent() == null) {
12361 result = false;
12362 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12363 "View " + this + " does not have a parent.");
12364 }
12365
12366 if (mAttachInfo == null) {
12367 result = false;
12368 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12369 "View " + this + " is not attached to a window.");
12370 }
12371 }
12372
12373 if (checkDrawing) {
12374 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12375 // from their draw() method
12376
12377 if ((mPrivateFlags & DRAWN) != DRAWN &&
12378 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12379 result = false;
12380 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12381 "View " + this + " was invalidated but its drawing cache is valid.");
12382 }
12383 }
12384
12385 return result;
12386 }
12387
12388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 * Prints information about this view in the log output, with the tag
12390 * {@link #VIEW_LOG_TAG}.
12391 *
12392 * @hide
12393 */
12394 public void debug() {
12395 debug(0);
12396 }
12397
12398 /**
12399 * Prints information about this view in the log output, with the tag
12400 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12401 * indentation defined by the <code>depth</code>.
12402 *
12403 * @param depth the indentation level
12404 *
12405 * @hide
12406 */
12407 protected void debug(int depth) {
12408 String output = debugIndent(depth - 1);
12409
12410 output += "+ " + this;
12411 int id = getId();
12412 if (id != -1) {
12413 output += " (id=" + id + ")";
12414 }
12415 Object tag = getTag();
12416 if (tag != null) {
12417 output += " (tag=" + tag + ")";
12418 }
12419 Log.d(VIEW_LOG_TAG, output);
12420
12421 if ((mPrivateFlags & FOCUSED) != 0) {
12422 output = debugIndent(depth) + " FOCUSED";
12423 Log.d(VIEW_LOG_TAG, output);
12424 }
12425
12426 output = debugIndent(depth);
12427 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12428 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12429 + "} ";
12430 Log.d(VIEW_LOG_TAG, output);
12431
12432 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12433 || mPaddingBottom != 0) {
12434 output = debugIndent(depth);
12435 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12436 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12437 Log.d(VIEW_LOG_TAG, output);
12438 }
12439
12440 output = debugIndent(depth);
12441 output += "mMeasureWidth=" + mMeasuredWidth +
12442 " mMeasureHeight=" + mMeasuredHeight;
12443 Log.d(VIEW_LOG_TAG, output);
12444
12445 output = debugIndent(depth);
12446 if (mLayoutParams == null) {
12447 output += "BAD! no layout params";
12448 } else {
12449 output = mLayoutParams.debug(output);
12450 }
12451 Log.d(VIEW_LOG_TAG, output);
12452
12453 output = debugIndent(depth);
12454 output += "flags={";
12455 output += View.printFlags(mViewFlags);
12456 output += "}";
12457 Log.d(VIEW_LOG_TAG, output);
12458
12459 output = debugIndent(depth);
12460 output += "privateFlags={";
12461 output += View.printPrivateFlags(mPrivateFlags);
12462 output += "}";
12463 Log.d(VIEW_LOG_TAG, output);
12464 }
12465
12466 /**
12467 * Creates an string of whitespaces used for indentation.
12468 *
12469 * @param depth the indentation level
12470 * @return a String containing (depth * 2 + 3) * 2 white spaces
12471 *
12472 * @hide
12473 */
12474 protected static String debugIndent(int depth) {
12475 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12476 for (int i = 0; i < (depth * 2) + 3; i++) {
12477 spaces.append(' ').append(' ');
12478 }
12479 return spaces.toString();
12480 }
12481
12482 /**
12483 * <p>Return the offset of the widget's text baseline from the widget's top
12484 * boundary. If this widget does not support baseline alignment, this
12485 * method returns -1. </p>
12486 *
12487 * @return the offset of the baseline within the widget's bounds or -1
12488 * if baseline alignment is not supported
12489 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012490 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012491 public int getBaseline() {
12492 return -1;
12493 }
12494
12495 /**
12496 * Call this when something has changed which has invalidated the
12497 * layout of this view. This will schedule a layout pass of the view
12498 * tree.
12499 */
12500 public void requestLayout() {
12501 if (ViewDebug.TRACE_HIERARCHY) {
12502 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12503 }
12504
12505 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012506 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012508 if (mParent != null) {
12509 if (mLayoutParams != null) {
12510 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12511 }
12512 if (!mParent.isLayoutRequested()) {
12513 mParent.requestLayout();
12514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012515 }
12516 }
12517
12518 /**
12519 * Forces this view to be laid out during the next layout pass.
12520 * This method does not call requestLayout() or forceLayout()
12521 * on the parent.
12522 */
12523 public void forceLayout() {
12524 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012525 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526 }
12527
12528 /**
12529 * <p>
12530 * This is called to find out how big a view should be. The parent
12531 * supplies constraint information in the width and height parameters.
12532 * </p>
12533 *
12534 * <p>
12535 * The actual mesurement work of a view is performed in
12536 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12537 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12538 * </p>
12539 *
12540 *
12541 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12542 * parent
12543 * @param heightMeasureSpec Vertical space requirements as imposed by the
12544 * parent
12545 *
12546 * @see #onMeasure(int, int)
12547 */
12548 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12549 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12550 widthMeasureSpec != mOldWidthMeasureSpec ||
12551 heightMeasureSpec != mOldHeightMeasureSpec) {
12552
12553 // first clears the measured dimension flag
12554 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12555
12556 if (ViewDebug.TRACE_HIERARCHY) {
12557 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12558 }
12559
12560 // measure ourselves, this should set the measured dimension flag back
12561 onMeasure(widthMeasureSpec, heightMeasureSpec);
12562
12563 // flag not set, setMeasuredDimension() was not invoked, we raise
12564 // an exception to warn the developer
12565 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12566 throw new IllegalStateException("onMeasure() did not set the"
12567 + " measured dimension by calling"
12568 + " setMeasuredDimension()");
12569 }
12570
12571 mPrivateFlags |= LAYOUT_REQUIRED;
12572 }
12573
12574 mOldWidthMeasureSpec = widthMeasureSpec;
12575 mOldHeightMeasureSpec = heightMeasureSpec;
12576 }
12577
12578 /**
12579 * <p>
12580 * Measure the view and its content to determine the measured width and the
12581 * measured height. This method is invoked by {@link #measure(int, int)} and
12582 * should be overriden by subclasses to provide accurate and efficient
12583 * measurement of their contents.
12584 * </p>
12585 *
12586 * <p>
12587 * <strong>CONTRACT:</strong> When overriding this method, you
12588 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12589 * measured width and height of this view. Failure to do so will trigger an
12590 * <code>IllegalStateException</code>, thrown by
12591 * {@link #measure(int, int)}. Calling the superclass'
12592 * {@link #onMeasure(int, int)} is a valid use.
12593 * </p>
12594 *
12595 * <p>
12596 * The base class implementation of measure defaults to the background size,
12597 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12598 * override {@link #onMeasure(int, int)} to provide better measurements of
12599 * their content.
12600 * </p>
12601 *
12602 * <p>
12603 * If this method is overridden, it is the subclass's responsibility to make
12604 * sure the measured height and width are at least the view's minimum height
12605 * and width ({@link #getSuggestedMinimumHeight()} and
12606 * {@link #getSuggestedMinimumWidth()}).
12607 * </p>
12608 *
12609 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12610 * The requirements are encoded with
12611 * {@link android.view.View.MeasureSpec}.
12612 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12613 * The requirements are encoded with
12614 * {@link android.view.View.MeasureSpec}.
12615 *
12616 * @see #getMeasuredWidth()
12617 * @see #getMeasuredHeight()
12618 * @see #setMeasuredDimension(int, int)
12619 * @see #getSuggestedMinimumHeight()
12620 * @see #getSuggestedMinimumWidth()
12621 * @see android.view.View.MeasureSpec#getMode(int)
12622 * @see android.view.View.MeasureSpec#getSize(int)
12623 */
12624 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12625 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12626 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12627 }
12628
12629 /**
12630 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12631 * measured width and measured height. Failing to do so will trigger an
12632 * exception at measurement time.</p>
12633 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012634 * @param measuredWidth The measured width of this view. May be a complex
12635 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12636 * {@link #MEASURED_STATE_TOO_SMALL}.
12637 * @param measuredHeight The measured height of this view. May be a complex
12638 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12639 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 */
12641 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12642 mMeasuredWidth = measuredWidth;
12643 mMeasuredHeight = measuredHeight;
12644
12645 mPrivateFlags |= MEASURED_DIMENSION_SET;
12646 }
12647
12648 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012649 * Merge two states as returned by {@link #getMeasuredState()}.
12650 * @param curState The current state as returned from a view or the result
12651 * of combining multiple views.
12652 * @param newState The new view state to combine.
12653 * @return Returns a new integer reflecting the combination of the two
12654 * states.
12655 */
12656 public static int combineMeasuredStates(int curState, int newState) {
12657 return curState | newState;
12658 }
12659
12660 /**
12661 * Version of {@link #resolveSizeAndState(int, int, int)}
12662 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12663 */
12664 public static int resolveSize(int size, int measureSpec) {
12665 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12666 }
12667
12668 /**
12669 * Utility to reconcile a desired size and state, with constraints imposed
12670 * by a MeasureSpec. Will take the desired size, unless a different size
12671 * is imposed by the constraints. The returned value is a compound integer,
12672 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12673 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12674 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675 *
12676 * @param size How big the view wants to be
12677 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012678 * @return Size information bit mask as defined by
12679 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012680 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012681 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012682 int result = size;
12683 int specMode = MeasureSpec.getMode(measureSpec);
12684 int specSize = MeasureSpec.getSize(measureSpec);
12685 switch (specMode) {
12686 case MeasureSpec.UNSPECIFIED:
12687 result = size;
12688 break;
12689 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012690 if (specSize < size) {
12691 result = specSize | MEASURED_STATE_TOO_SMALL;
12692 } else {
12693 result = size;
12694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012695 break;
12696 case MeasureSpec.EXACTLY:
12697 result = specSize;
12698 break;
12699 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012700 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012701 }
12702
12703 /**
12704 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012705 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012706 * by the MeasureSpec.
12707 *
12708 * @param size Default size for this view
12709 * @param measureSpec Constraints imposed by the parent
12710 * @return The size this view should be.
12711 */
12712 public static int getDefaultSize(int size, int measureSpec) {
12713 int result = size;
12714 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012715 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012716
12717 switch (specMode) {
12718 case MeasureSpec.UNSPECIFIED:
12719 result = size;
12720 break;
12721 case MeasureSpec.AT_MOST:
12722 case MeasureSpec.EXACTLY:
12723 result = specSize;
12724 break;
12725 }
12726 return result;
12727 }
12728
12729 /**
12730 * Returns the suggested minimum height that the view should use. This
12731 * returns the maximum of the view's minimum height
12732 * and the background's minimum height
12733 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12734 * <p>
12735 * When being used in {@link #onMeasure(int, int)}, the caller should still
12736 * ensure the returned height is within the requirements of the parent.
12737 *
12738 * @return The suggested minimum height of the view.
12739 */
12740 protected int getSuggestedMinimumHeight() {
12741 int suggestedMinHeight = mMinHeight;
12742
12743 if (mBGDrawable != null) {
12744 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12745 if (suggestedMinHeight < bgMinHeight) {
12746 suggestedMinHeight = bgMinHeight;
12747 }
12748 }
12749
12750 return suggestedMinHeight;
12751 }
12752
12753 /**
12754 * Returns the suggested minimum width that the view should use. This
12755 * returns the maximum of the view's minimum width)
12756 * and the background's minimum width
12757 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12758 * <p>
12759 * When being used in {@link #onMeasure(int, int)}, the caller should still
12760 * ensure the returned width is within the requirements of the parent.
12761 *
12762 * @return The suggested minimum width of the view.
12763 */
12764 protected int getSuggestedMinimumWidth() {
12765 int suggestedMinWidth = mMinWidth;
12766
12767 if (mBGDrawable != null) {
12768 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12769 if (suggestedMinWidth < bgMinWidth) {
12770 suggestedMinWidth = bgMinWidth;
12771 }
12772 }
12773
12774 return suggestedMinWidth;
12775 }
12776
12777 /**
12778 * Sets the minimum height of the view. It is not guaranteed the view will
12779 * be able to achieve this minimum height (for example, if its parent layout
12780 * constrains it with less available height).
12781 *
12782 * @param minHeight The minimum height the view will try to be.
12783 */
12784 public void setMinimumHeight(int minHeight) {
12785 mMinHeight = minHeight;
12786 }
12787
12788 /**
12789 * Sets the minimum width of the view. It is not guaranteed the view will
12790 * be able to achieve this minimum width (for example, if its parent layout
12791 * constrains it with less available width).
12792 *
12793 * @param minWidth The minimum width the view will try to be.
12794 */
12795 public void setMinimumWidth(int minWidth) {
12796 mMinWidth = minWidth;
12797 }
12798
12799 /**
12800 * Get the animation currently associated with this view.
12801 *
12802 * @return The animation that is currently playing or
12803 * scheduled to play for this view.
12804 */
12805 public Animation getAnimation() {
12806 return mCurrentAnimation;
12807 }
12808
12809 /**
12810 * Start the specified animation now.
12811 *
12812 * @param animation the animation to start now
12813 */
12814 public void startAnimation(Animation animation) {
12815 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12816 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012817 invalidateParentCaches();
12818 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012819 }
12820
12821 /**
12822 * Cancels any animations for this view.
12823 */
12824 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012825 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012826 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012829 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012830 }
12831
12832 /**
12833 * Sets the next animation to play for this view.
12834 * If you want the animation to play immediately, use
12835 * startAnimation. This method provides allows fine-grained
12836 * control over the start time and invalidation, but you
12837 * must make sure that 1) the animation has a start time set, and
12838 * 2) the view will be invalidated when the animation is supposed to
12839 * start.
12840 *
12841 * @param animation The next animation, or null.
12842 */
12843 public void setAnimation(Animation animation) {
12844 mCurrentAnimation = animation;
12845 if (animation != null) {
12846 animation.reset();
12847 }
12848 }
12849
12850 /**
12851 * Invoked by a parent ViewGroup to notify the start of the animation
12852 * currently associated with this view. If you override this method,
12853 * always call super.onAnimationStart();
12854 *
12855 * @see #setAnimation(android.view.animation.Animation)
12856 * @see #getAnimation()
12857 */
12858 protected void onAnimationStart() {
12859 mPrivateFlags |= ANIMATION_STARTED;
12860 }
12861
12862 /**
12863 * Invoked by a parent ViewGroup to notify the end of the animation
12864 * currently associated with this view. If you override this method,
12865 * always call super.onAnimationEnd();
12866 *
12867 * @see #setAnimation(android.view.animation.Animation)
12868 * @see #getAnimation()
12869 */
12870 protected void onAnimationEnd() {
12871 mPrivateFlags &= ~ANIMATION_STARTED;
12872 }
12873
12874 /**
12875 * Invoked if there is a Transform that involves alpha. Subclass that can
12876 * draw themselves with the specified alpha should return true, and then
12877 * respect that alpha when their onDraw() is called. If this returns false
12878 * then the view may be redirected to draw into an offscreen buffer to
12879 * fulfill the request, which will look fine, but may be slower than if the
12880 * subclass handles it internally. The default implementation returns false.
12881 *
12882 * @param alpha The alpha (0..255) to apply to the view's drawing
12883 * @return true if the view can draw with the specified alpha.
12884 */
12885 protected boolean onSetAlpha(int alpha) {
12886 return false;
12887 }
12888
12889 /**
12890 * This is used by the RootView to perform an optimization when
12891 * the view hierarchy contains one or several SurfaceView.
12892 * SurfaceView is always considered transparent, but its children are not,
12893 * therefore all View objects remove themselves from the global transparent
12894 * region (passed as a parameter to this function).
12895 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012896 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012897 *
12898 * @return Returns true if the effective visibility of the view at this
12899 * point is opaque, regardless of the transparent region; returns false
12900 * if it is possible for underlying windows to be seen behind the view.
12901 *
12902 * {@hide}
12903 */
12904 public boolean gatherTransparentRegion(Region region) {
12905 final AttachInfo attachInfo = mAttachInfo;
12906 if (region != null && attachInfo != null) {
12907 final int pflags = mPrivateFlags;
12908 if ((pflags & SKIP_DRAW) == 0) {
12909 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12910 // remove it from the transparent region.
12911 final int[] location = attachInfo.mTransparentLocation;
12912 getLocationInWindow(location);
12913 region.op(location[0], location[1], location[0] + mRight - mLeft,
12914 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12915 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12916 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12917 // exists, so we remove the background drawable's non-transparent
12918 // parts from this transparent region.
12919 applyDrawableToTransparentRegion(mBGDrawable, region);
12920 }
12921 }
12922 return true;
12923 }
12924
12925 /**
12926 * Play a sound effect for this view.
12927 *
12928 * <p>The framework will play sound effects for some built in actions, such as
12929 * clicking, but you may wish to play these effects in your widget,
12930 * for instance, for internal navigation.
12931 *
12932 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12933 * {@link #isSoundEffectsEnabled()} is true.
12934 *
12935 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12936 */
12937 public void playSoundEffect(int soundConstant) {
12938 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12939 return;
12940 }
12941 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12942 }
12943
12944 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012945 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012946 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012947 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012948 *
12949 * <p>The framework will provide haptic feedback for some built in actions,
12950 * such as long presses, but you may wish to provide feedback for your
12951 * own widget.
12952 *
12953 * <p>The feedback will only be performed if
12954 * {@link #isHapticFeedbackEnabled()} is true.
12955 *
12956 * @param feedbackConstant One of the constants defined in
12957 * {@link HapticFeedbackConstants}
12958 */
12959 public boolean performHapticFeedback(int feedbackConstant) {
12960 return performHapticFeedback(feedbackConstant, 0);
12961 }
12962
12963 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012964 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012965 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012966 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012967 *
12968 * @param feedbackConstant One of the constants defined in
12969 * {@link HapticFeedbackConstants}
12970 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12971 */
12972 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12973 if (mAttachInfo == null) {
12974 return false;
12975 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012976 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012977 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012978 && !isHapticFeedbackEnabled()) {
12979 return false;
12980 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012981 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12982 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012983 }
12984
12985 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012986 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012987 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12988 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012989 */
12990 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040012991 if (visibility != mSystemUiVisibility) {
12992 mSystemUiVisibility = visibility;
12993 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12994 mParent.recomputeViewAttributes(this);
12995 }
Joe Onorato664644d2011-01-23 17:53:23 -080012996 }
12997 }
12998
12999 /**
13000 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013001 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13002 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013003 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013004 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013005 return mSystemUiVisibility;
13006 }
13007
Scott Mainec6331b2011-05-24 16:55:56 -070013008 /**
13009 * Set a listener to receive callbacks when the visibility of the system bar changes.
13010 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13011 */
Joe Onorato664644d2011-01-23 17:53:23 -080013012 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
13013 mOnSystemUiVisibilityChangeListener = l;
13014 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13015 mParent.recomputeViewAttributes(this);
13016 }
13017 }
13018
13019 /**
13020 */
13021 public void dispatchSystemUiVisibilityChanged(int visibility) {
13022 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080013023 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013024 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013025 }
13026 }
13027
13028 /**
Joe Malin32736f02011-01-19 16:14:20 -080013029 * Creates an image that the system displays during the drag and drop
13030 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13031 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13032 * appearance as the given View. The default also positions the center of the drag shadow
13033 * directly under the touch point. If no View is provided (the constructor with no parameters
13034 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13035 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13036 * default is an invisible drag shadow.
13037 * <p>
13038 * You are not required to use the View you provide to the constructor as the basis of the
13039 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13040 * anything you want as the drag shadow.
13041 * </p>
13042 * <p>
13043 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13044 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13045 * size and position of the drag shadow. It uses this data to construct a
13046 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13047 * so that your application can draw the shadow image in the Canvas.
13048 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013049 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013050 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013051 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013052
13053 /**
Joe Malin32736f02011-01-19 16:14:20 -080013054 * Constructs a shadow image builder based on a View. By default, the resulting drag
13055 * shadow will have the same appearance and dimensions as the View, with the touch point
13056 * over the center of the View.
13057 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013058 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013059 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013060 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013061 }
13062
Christopher Tate17ed60c2011-01-18 12:50:26 -080013063 /**
13064 * Construct a shadow builder object with no associated View. This
13065 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13066 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13067 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013068 * reference to any View object. If they are not overridden, then the result is an
13069 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013070 */
13071 public DragShadowBuilder() {
13072 mView = new WeakReference<View>(null);
13073 }
13074
13075 /**
13076 * Returns the View object that had been passed to the
13077 * {@link #View.DragShadowBuilder(View)}
13078 * constructor. If that View parameter was {@code null} or if the
13079 * {@link #View.DragShadowBuilder()}
13080 * constructor was used to instantiate the builder object, this method will return
13081 * null.
13082 *
13083 * @return The View object associate with this builder object.
13084 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013085 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013086 final public View getView() {
13087 return mView.get();
13088 }
13089
Christopher Tate2c095f32010-10-04 14:13:40 -070013090 /**
Joe Malin32736f02011-01-19 16:14:20 -080013091 * Provides the metrics for the shadow image. These include the dimensions of
13092 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013093 * be centered under the touch location while dragging.
13094 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013095 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013096 * same as the dimensions of the View itself and centers the shadow under
13097 * the touch point.
13098 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013099 *
Joe Malin32736f02011-01-19 16:14:20 -080013100 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13101 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13102 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13103 * image.
13104 *
13105 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13106 * shadow image that should be underneath the touch point during the drag and drop
13107 * operation. Your application must set {@link android.graphics.Point#x} to the
13108 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013109 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013110 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013111 final View view = mView.get();
13112 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013113 shadowSize.set(view.getWidth(), view.getHeight());
13114 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013115 } else {
13116 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13117 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013118 }
13119
13120 /**
Joe Malin32736f02011-01-19 16:14:20 -080013121 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13122 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013123 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013124 *
Joe Malin32736f02011-01-19 16:14:20 -080013125 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013126 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013127 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013128 final View view = mView.get();
13129 if (view != null) {
13130 view.draw(canvas);
13131 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013132 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013133 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013134 }
13135 }
13136
13137 /**
Joe Malin32736f02011-01-19 16:14:20 -080013138 * Starts a drag and drop operation. When your application calls this method, it passes a
13139 * {@link android.view.View.DragShadowBuilder} object to the system. The
13140 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13141 * to get metrics for the drag shadow, and then calls the object's
13142 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13143 * <p>
13144 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13145 * drag events to all the View objects in your application that are currently visible. It does
13146 * this either by calling the View object's drag listener (an implementation of
13147 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13148 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13149 * Both are passed a {@link android.view.DragEvent} object that has a
13150 * {@link android.view.DragEvent#getAction()} value of
13151 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13152 * </p>
13153 * <p>
13154 * Your application can invoke startDrag() on any attached View object. The View object does not
13155 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13156 * be related to the View the user selected for dragging.
13157 * </p>
13158 * @param data A {@link android.content.ClipData} object pointing to the data to be
13159 * transferred by the drag and drop operation.
13160 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13161 * drag shadow.
13162 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13163 * drop operation. This Object is put into every DragEvent object sent by the system during the
13164 * current drag.
13165 * <p>
13166 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13167 * to the target Views. For example, it can contain flags that differentiate between a
13168 * a copy operation and a move operation.
13169 * </p>
13170 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13171 * so the parameter should be set to 0.
13172 * @return {@code true} if the method completes successfully, or
13173 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13174 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013175 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013176 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013177 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013178 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013179 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013180 }
13181 boolean okay = false;
13182
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013183 Point shadowSize = new Point();
13184 Point shadowTouchPoint = new Point();
13185 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013186
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013187 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13188 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13189 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013190 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013191
Chris Tatea32dcf72010-10-14 12:13:50 -070013192 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013193 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13194 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013195 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013196 Surface surface = new Surface();
13197 try {
13198 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013199 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013200 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013201 + " surface=" + surface);
13202 if (token != null) {
13203 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013204 try {
Chris Tate6b391282010-10-14 15:48:59 -070013205 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013206 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013207 } finally {
13208 surface.unlockCanvasAndPost(canvas);
13209 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013210
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013211 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013212
13213 // Cache the local state object for delivery with DragEvents
13214 root.setLocalDragState(myLocalState);
13215
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013216 // repurpose 'shadowSize' for the last touch point
13217 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013218
Christopher Tatea53146c2010-09-07 11:57:52 -070013219 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013220 shadowSize.x, shadowSize.y,
13221 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013222 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013223
13224 // Off and running! Release our local surface instance; the drag
13225 // shadow surface is now managed by the system process.
13226 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013227 }
13228 } catch (Exception e) {
13229 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13230 surface.destroy();
13231 }
13232
13233 return okay;
13234 }
13235
Christopher Tatea53146c2010-09-07 11:57:52 -070013236 /**
Joe Malin32736f02011-01-19 16:14:20 -080013237 * Handles drag events sent by the system following a call to
13238 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13239 *<p>
13240 * When the system calls this method, it passes a
13241 * {@link android.view.DragEvent} object. A call to
13242 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13243 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13244 * operation.
13245 * @param event The {@link android.view.DragEvent} sent by the system.
13246 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13247 * in DragEvent, indicating the type of drag event represented by this object.
13248 * @return {@code true} if the method was successful, otherwise {@code false}.
13249 * <p>
13250 * The method should return {@code true} in response to an action type of
13251 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13252 * operation.
13253 * </p>
13254 * <p>
13255 * The method should also return {@code true} in response to an action type of
13256 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13257 * {@code false} if it didn't.
13258 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013259 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013260 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013261 return false;
13262 }
13263
13264 /**
Joe Malin32736f02011-01-19 16:14:20 -080013265 * Detects if this View is enabled and has a drag event listener.
13266 * If both are true, then it calls the drag event listener with the
13267 * {@link android.view.DragEvent} it received. If the drag event listener returns
13268 * {@code true}, then dispatchDragEvent() returns {@code true}.
13269 * <p>
13270 * For all other cases, the method calls the
13271 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13272 * method and returns its result.
13273 * </p>
13274 * <p>
13275 * This ensures that a drag event is always consumed, even if the View does not have a drag
13276 * event listener. However, if the View has a listener and the listener returns true, then
13277 * onDragEvent() is not called.
13278 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013279 */
13280 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013281 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013282 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13283 && mOnDragListener.onDrag(this, event)) {
13284 return true;
13285 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013286 return onDragEvent(event);
13287 }
13288
Christopher Tate3d4bf172011-03-28 16:16:46 -070013289 boolean canAcceptDrag() {
13290 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13291 }
13292
Christopher Tatea53146c2010-09-07 11:57:52 -070013293 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013294 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13295 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013296 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013297 */
13298 public void onCloseSystemDialogs(String reason) {
13299 }
Joe Malin32736f02011-01-19 16:14:20 -080013300
Dianne Hackbornffa42482009-09-23 22:20:11 -070013301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013302 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013303 * update a Region being computed for
13304 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013305 * that any non-transparent parts of the Drawable are removed from the
13306 * given transparent region.
13307 *
13308 * @param dr The Drawable whose transparency is to be applied to the region.
13309 * @param region A Region holding the current transparency information,
13310 * where any parts of the region that are set are considered to be
13311 * transparent. On return, this region will be modified to have the
13312 * transparency information reduced by the corresponding parts of the
13313 * Drawable that are not transparent.
13314 * {@hide}
13315 */
13316 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13317 if (DBG) {
13318 Log.i("View", "Getting transparent region for: " + this);
13319 }
13320 final Region r = dr.getTransparentRegion();
13321 final Rect db = dr.getBounds();
13322 final AttachInfo attachInfo = mAttachInfo;
13323 if (r != null && attachInfo != null) {
13324 final int w = getRight()-getLeft();
13325 final int h = getBottom()-getTop();
13326 if (db.left > 0) {
13327 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13328 r.op(0, 0, db.left, h, Region.Op.UNION);
13329 }
13330 if (db.right < w) {
13331 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13332 r.op(db.right, 0, w, h, Region.Op.UNION);
13333 }
13334 if (db.top > 0) {
13335 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13336 r.op(0, 0, w, db.top, Region.Op.UNION);
13337 }
13338 if (db.bottom < h) {
13339 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13340 r.op(0, db.bottom, w, h, Region.Op.UNION);
13341 }
13342 final int[] location = attachInfo.mTransparentLocation;
13343 getLocationInWindow(location);
13344 r.translate(location[0], location[1]);
13345 region.op(r, Region.Op.INTERSECT);
13346 } else {
13347 region.op(db, Region.Op.DIFFERENCE);
13348 }
13349 }
13350
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013351 private void checkForLongClick(int delayOffset) {
13352 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13353 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013354
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013355 if (mPendingCheckForLongPress == null) {
13356 mPendingCheckForLongPress = new CheckForLongPress();
13357 }
13358 mPendingCheckForLongPress.rememberWindowAttachCount();
13359 postDelayed(mPendingCheckForLongPress,
13360 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013362 }
13363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013364 /**
13365 * Inflate a view from an XML resource. This convenience method wraps the {@link
13366 * LayoutInflater} class, which provides a full range of options for view inflation.
13367 *
13368 * @param context The Context object for your activity or application.
13369 * @param resource The resource ID to inflate
13370 * @param root A view group that will be the parent. Used to properly inflate the
13371 * layout_* parameters.
13372 * @see LayoutInflater
13373 */
13374 public static View inflate(Context context, int resource, ViewGroup root) {
13375 LayoutInflater factory = LayoutInflater.from(context);
13376 return factory.inflate(resource, root);
13377 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013379 /**
Adam Powell637d3372010-08-25 14:37:03 -070013380 * Scroll the view with standard behavior for scrolling beyond the normal
13381 * content boundaries. Views that call this method should override
13382 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13383 * results of an over-scroll operation.
13384 *
13385 * Views can use this method to handle any touch or fling-based scrolling.
13386 *
13387 * @param deltaX Change in X in pixels
13388 * @param deltaY Change in Y in pixels
13389 * @param scrollX Current X scroll value in pixels before applying deltaX
13390 * @param scrollY Current Y scroll value in pixels before applying deltaY
13391 * @param scrollRangeX Maximum content scroll range along the X axis
13392 * @param scrollRangeY Maximum content scroll range along the Y axis
13393 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13394 * along the X axis.
13395 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13396 * along the Y axis.
13397 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13398 * @return true if scrolling was clamped to an over-scroll boundary along either
13399 * axis, false otherwise.
13400 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013401 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013402 protected boolean overScrollBy(int deltaX, int deltaY,
13403 int scrollX, int scrollY,
13404 int scrollRangeX, int scrollRangeY,
13405 int maxOverScrollX, int maxOverScrollY,
13406 boolean isTouchEvent) {
13407 final int overScrollMode = mOverScrollMode;
13408 final boolean canScrollHorizontal =
13409 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13410 final boolean canScrollVertical =
13411 computeVerticalScrollRange() > computeVerticalScrollExtent();
13412 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13413 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13414 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13415 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13416
13417 int newScrollX = scrollX + deltaX;
13418 if (!overScrollHorizontal) {
13419 maxOverScrollX = 0;
13420 }
13421
13422 int newScrollY = scrollY + deltaY;
13423 if (!overScrollVertical) {
13424 maxOverScrollY = 0;
13425 }
13426
13427 // Clamp values if at the limits and record
13428 final int left = -maxOverScrollX;
13429 final int right = maxOverScrollX + scrollRangeX;
13430 final int top = -maxOverScrollY;
13431 final int bottom = maxOverScrollY + scrollRangeY;
13432
13433 boolean clampedX = false;
13434 if (newScrollX > right) {
13435 newScrollX = right;
13436 clampedX = true;
13437 } else if (newScrollX < left) {
13438 newScrollX = left;
13439 clampedX = true;
13440 }
13441
13442 boolean clampedY = false;
13443 if (newScrollY > bottom) {
13444 newScrollY = bottom;
13445 clampedY = true;
13446 } else if (newScrollY < top) {
13447 newScrollY = top;
13448 clampedY = true;
13449 }
13450
13451 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13452
13453 return clampedX || clampedY;
13454 }
13455
13456 /**
13457 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13458 * respond to the results of an over-scroll operation.
13459 *
13460 * @param scrollX New X scroll value in pixels
13461 * @param scrollY New Y scroll value in pixels
13462 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13463 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13464 */
13465 protected void onOverScrolled(int scrollX, int scrollY,
13466 boolean clampedX, boolean clampedY) {
13467 // Intentionally empty.
13468 }
13469
13470 /**
13471 * Returns the over-scroll mode for this view. The result will be
13472 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13473 * (allow over-scrolling only if the view content is larger than the container),
13474 * or {@link #OVER_SCROLL_NEVER}.
13475 *
13476 * @return This view's over-scroll mode.
13477 */
13478 public int getOverScrollMode() {
13479 return mOverScrollMode;
13480 }
13481
13482 /**
13483 * Set the over-scroll mode for this view. Valid over-scroll modes are
13484 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13485 * (allow over-scrolling only if the view content is larger than the container),
13486 * or {@link #OVER_SCROLL_NEVER}.
13487 *
13488 * Setting the over-scroll mode of a view will have an effect only if the
13489 * view is capable of scrolling.
13490 *
13491 * @param overScrollMode The new over-scroll mode for this view.
13492 */
13493 public void setOverScrollMode(int overScrollMode) {
13494 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13495 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13496 overScrollMode != OVER_SCROLL_NEVER) {
13497 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13498 }
13499 mOverScrollMode = overScrollMode;
13500 }
13501
13502 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013503 * Gets a scale factor that determines the distance the view should scroll
13504 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13505 * @return The vertical scroll scale factor.
13506 * @hide
13507 */
13508 protected float getVerticalScrollFactor() {
13509 if (mVerticalScrollFactor == 0) {
13510 TypedValue outValue = new TypedValue();
13511 if (!mContext.getTheme().resolveAttribute(
13512 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13513 throw new IllegalStateException(
13514 "Expected theme to define listPreferredItemHeight.");
13515 }
13516 mVerticalScrollFactor = outValue.getDimension(
13517 mContext.getResources().getDisplayMetrics());
13518 }
13519 return mVerticalScrollFactor;
13520 }
13521
13522 /**
13523 * Gets a scale factor that determines the distance the view should scroll
13524 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13525 * @return The horizontal scroll scale factor.
13526 * @hide
13527 */
13528 protected float getHorizontalScrollFactor() {
13529 // TODO: Should use something else.
13530 return getVerticalScrollFactor();
13531 }
13532
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013533 /**
13534 * Return the value specifying the text direction or policy that was set with
13535 * {@link #setTextDirection(int)}.
13536 *
13537 * @return the defined text direction. It can be one of:
13538 *
13539 * {@link #TEXT_DIRECTION_INHERIT},
13540 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13541 * {@link #TEXT_DIRECTION_ANY_RTL},
13542 * {@link #TEXT_DIRECTION_LTR},
13543 * {@link #TEXT_DIRECTION_RTL},
13544 *
13545 * @hide
13546 */
13547 public int getTextDirection() {
13548 return mTextDirection;
13549 }
13550
13551 /**
13552 * Set the text direction.
13553 *
13554 * @param textDirection the direction to set. Should be one of:
13555 *
13556 * {@link #TEXT_DIRECTION_INHERIT},
13557 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13558 * {@link #TEXT_DIRECTION_ANY_RTL},
13559 * {@link #TEXT_DIRECTION_LTR},
13560 * {@link #TEXT_DIRECTION_RTL},
13561 *
13562 * @hide
13563 */
13564 public void setTextDirection(int textDirection) {
13565 if (textDirection != mTextDirection) {
13566 mTextDirection = textDirection;
13567 resetResolvedTextDirection();
13568 requestLayout();
13569 }
13570 }
13571
13572 /**
13573 * Return the resolved text direction.
13574 *
13575 * @return the resolved text direction. Return one of:
13576 *
Doug Feltcb3791202011-07-07 11:57:48 -070013577 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13578 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013579 * {@link #TEXT_DIRECTION_LTR},
13580 * {@link #TEXT_DIRECTION_RTL},
13581 *
13582 * @hide
13583 */
13584 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013585 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013586 resolveTextDirection();
13587 }
13588 return mResolvedTextDirection;
13589 }
13590
13591 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013592 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013593 *
13594 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013595 */
13596 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013597 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13598 mResolvedTextDirection = mTextDirection;
13599 return;
13600 }
13601 if (mParent != null && mParent instanceof ViewGroup) {
13602 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13603 return;
13604 }
13605 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013606 }
13607
13608 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013609 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013610 *
13611 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013612 */
13613 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013614 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013615 }
13616
Chet Haaseb39f0512011-05-24 14:36:40 -070013617 //
13618 // Properties
13619 //
13620 /**
13621 * A Property wrapper around the <code>alpha</code> functionality handled by the
13622 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13623 */
Romain Guy02ccac62011-06-24 13:20:23 -070013624 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013625 @Override
13626 public void setValue(View object, float value) {
13627 object.setAlpha(value);
13628 }
13629
13630 @Override
13631 public Float get(View object) {
13632 return object.getAlpha();
13633 }
13634 };
13635
13636 /**
13637 * A Property wrapper around the <code>translationX</code> functionality handled by the
13638 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13639 */
13640 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13641 @Override
13642 public void setValue(View object, float value) {
13643 object.setTranslationX(value);
13644 }
13645
13646 @Override
13647 public Float get(View object) {
13648 return object.getTranslationX();
13649 }
13650 };
13651
13652 /**
13653 * A Property wrapper around the <code>translationY</code> functionality handled by the
13654 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13655 */
13656 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13657 @Override
13658 public void setValue(View object, float value) {
13659 object.setTranslationY(value);
13660 }
13661
13662 @Override
13663 public Float get(View object) {
13664 return object.getTranslationY();
13665 }
13666 };
13667
13668 /**
13669 * A Property wrapper around the <code>x</code> functionality handled by the
13670 * {@link View#setX(float)} and {@link View#getX()} methods.
13671 */
13672 public static Property<View, Float> X = new FloatProperty<View>("x") {
13673 @Override
13674 public void setValue(View object, float value) {
13675 object.setX(value);
13676 }
13677
13678 @Override
13679 public Float get(View object) {
13680 return object.getX();
13681 }
13682 };
13683
13684 /**
13685 * A Property wrapper around the <code>y</code> functionality handled by the
13686 * {@link View#setY(float)} and {@link View#getY()} methods.
13687 */
13688 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13689 @Override
13690 public void setValue(View object, float value) {
13691 object.setY(value);
13692 }
13693
13694 @Override
13695 public Float get(View object) {
13696 return object.getY();
13697 }
13698 };
13699
13700 /**
13701 * A Property wrapper around the <code>rotation</code> functionality handled by the
13702 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13703 */
13704 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13705 @Override
13706 public void setValue(View object, float value) {
13707 object.setRotation(value);
13708 }
13709
13710 @Override
13711 public Float get(View object) {
13712 return object.getRotation();
13713 }
13714 };
13715
13716 /**
13717 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13718 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13719 */
13720 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13721 @Override
13722 public void setValue(View object, float value) {
13723 object.setRotationX(value);
13724 }
13725
13726 @Override
13727 public Float get(View object) {
13728 return object.getRotationX();
13729 }
13730 };
13731
13732 /**
13733 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13734 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13735 */
13736 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13737 @Override
13738 public void setValue(View object, float value) {
13739 object.setRotationY(value);
13740 }
13741
13742 @Override
13743 public Float get(View object) {
13744 return object.getRotationY();
13745 }
13746 };
13747
13748 /**
13749 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13750 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13751 */
13752 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13753 @Override
13754 public void setValue(View object, float value) {
13755 object.setScaleX(value);
13756 }
13757
13758 @Override
13759 public Float get(View object) {
13760 return object.getScaleX();
13761 }
13762 };
13763
13764 /**
13765 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13766 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13767 */
13768 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13769 @Override
13770 public void setValue(View object, float value) {
13771 object.setScaleY(value);
13772 }
13773
13774 @Override
13775 public Float get(View object) {
13776 return object.getScaleY();
13777 }
13778 };
13779
Jeff Brown33bbfd22011-02-24 20:55:35 -080013780 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013781 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13782 * Each MeasureSpec represents a requirement for either the width or the height.
13783 * A MeasureSpec is comprised of a size and a mode. There are three possible
13784 * modes:
13785 * <dl>
13786 * <dt>UNSPECIFIED</dt>
13787 * <dd>
13788 * The parent has not imposed any constraint on the child. It can be whatever size
13789 * it wants.
13790 * </dd>
13791 *
13792 * <dt>EXACTLY</dt>
13793 * <dd>
13794 * The parent has determined an exact size for the child. The child is going to be
13795 * given those bounds regardless of how big it wants to be.
13796 * </dd>
13797 *
13798 * <dt>AT_MOST</dt>
13799 * <dd>
13800 * The child can be as large as it wants up to the specified size.
13801 * </dd>
13802 * </dl>
13803 *
13804 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13805 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13806 */
13807 public static class MeasureSpec {
13808 private static final int MODE_SHIFT = 30;
13809 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13810
13811 /**
13812 * Measure specification mode: The parent has not imposed any constraint
13813 * on the child. It can be whatever size it wants.
13814 */
13815 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13816
13817 /**
13818 * Measure specification mode: The parent has determined an exact size
13819 * for the child. The child is going to be given those bounds regardless
13820 * of how big it wants to be.
13821 */
13822 public static final int EXACTLY = 1 << MODE_SHIFT;
13823
13824 /**
13825 * Measure specification mode: The child can be as large as it wants up
13826 * to the specified size.
13827 */
13828 public static final int AT_MOST = 2 << MODE_SHIFT;
13829
13830 /**
13831 * Creates a measure specification based on the supplied size and mode.
13832 *
13833 * The mode must always be one of the following:
13834 * <ul>
13835 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13836 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13837 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13838 * </ul>
13839 *
13840 * @param size the size of the measure specification
13841 * @param mode the mode of the measure specification
13842 * @return the measure specification based on size and mode
13843 */
13844 public static int makeMeasureSpec(int size, int mode) {
13845 return size + mode;
13846 }
13847
13848 /**
13849 * Extracts the mode from the supplied measure specification.
13850 *
13851 * @param measureSpec the measure specification to extract the mode from
13852 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13853 * {@link android.view.View.MeasureSpec#AT_MOST} or
13854 * {@link android.view.View.MeasureSpec#EXACTLY}
13855 */
13856 public static int getMode(int measureSpec) {
13857 return (measureSpec & MODE_MASK);
13858 }
13859
13860 /**
13861 * Extracts the size from the supplied measure specification.
13862 *
13863 * @param measureSpec the measure specification to extract the size from
13864 * @return the size in pixels defined in the supplied measure specification
13865 */
13866 public static int getSize(int measureSpec) {
13867 return (measureSpec & ~MODE_MASK);
13868 }
13869
13870 /**
13871 * Returns a String representation of the specified measure
13872 * specification.
13873 *
13874 * @param measureSpec the measure specification to convert to a String
13875 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13876 */
13877 public static String toString(int measureSpec) {
13878 int mode = getMode(measureSpec);
13879 int size = getSize(measureSpec);
13880
13881 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13882
13883 if (mode == UNSPECIFIED)
13884 sb.append("UNSPECIFIED ");
13885 else if (mode == EXACTLY)
13886 sb.append("EXACTLY ");
13887 else if (mode == AT_MOST)
13888 sb.append("AT_MOST ");
13889 else
13890 sb.append(mode).append(" ");
13891
13892 sb.append(size);
13893 return sb.toString();
13894 }
13895 }
13896
13897 class CheckForLongPress implements Runnable {
13898
13899 private int mOriginalWindowAttachCount;
13900
13901 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013902 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 && mOriginalWindowAttachCount == mWindowAttachCount) {
13904 if (performLongClick()) {
13905 mHasPerformedLongPress = true;
13906 }
13907 }
13908 }
13909
13910 public void rememberWindowAttachCount() {
13911 mOriginalWindowAttachCount = mWindowAttachCount;
13912 }
13913 }
Joe Malin32736f02011-01-19 16:14:20 -080013914
Adam Powelle14579b2009-12-16 18:39:52 -080013915 private final class CheckForTap implements Runnable {
13916 public void run() {
13917 mPrivateFlags &= ~PREPRESSED;
13918 mPrivateFlags |= PRESSED;
13919 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013920 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013921 }
13922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923
Adam Powella35d7682010-03-12 14:48:13 -080013924 private final class PerformClick implements Runnable {
13925 public void run() {
13926 performClick();
13927 }
13928 }
13929
Dianne Hackborn63042d62011-01-26 18:56:29 -080013930 /** @hide */
13931 public void hackTurnOffWindowResizeAnim(boolean off) {
13932 mAttachInfo.mTurnOffWindowResizeAnim = off;
13933 }
Joe Malin32736f02011-01-19 16:14:20 -080013934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013936 * This method returns a ViewPropertyAnimator object, which can be used to animate
13937 * specific properties on this View.
13938 *
13939 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13940 */
13941 public ViewPropertyAnimator animate() {
13942 if (mAnimator == null) {
13943 mAnimator = new ViewPropertyAnimator(this);
13944 }
13945 return mAnimator;
13946 }
13947
13948 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013949 * Interface definition for a callback to be invoked when a key event is
13950 * dispatched to this view. The callback will be invoked before the key
13951 * event is given to the view.
13952 */
13953 public interface OnKeyListener {
13954 /**
13955 * Called when a key is dispatched to a view. This allows listeners to
13956 * get a chance to respond before the target view.
13957 *
13958 * @param v The view the key has been dispatched to.
13959 * @param keyCode The code for the physical key that was pressed
13960 * @param event The KeyEvent object containing full information about
13961 * the event.
13962 * @return True if the listener has consumed the event, false otherwise.
13963 */
13964 boolean onKey(View v, int keyCode, KeyEvent event);
13965 }
13966
13967 /**
13968 * Interface definition for a callback to be invoked when a touch event is
13969 * dispatched to this view. The callback will be invoked before the touch
13970 * event is given to the view.
13971 */
13972 public interface OnTouchListener {
13973 /**
13974 * Called when a touch event is dispatched to a view. This allows listeners to
13975 * get a chance to respond before the target view.
13976 *
13977 * @param v The view the touch event has been dispatched to.
13978 * @param event The MotionEvent object containing full information about
13979 * the event.
13980 * @return True if the listener has consumed the event, false otherwise.
13981 */
13982 boolean onTouch(View v, MotionEvent event);
13983 }
13984
13985 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013986 * Interface definition for a callback to be invoked when a hover event is
13987 * dispatched to this view. The callback will be invoked before the hover
13988 * event is given to the view.
13989 */
13990 public interface OnHoverListener {
13991 /**
13992 * Called when a hover event is dispatched to a view. This allows listeners to
13993 * get a chance to respond before the target view.
13994 *
13995 * @param v The view the hover event has been dispatched to.
13996 * @param event The MotionEvent object containing full information about
13997 * the event.
13998 * @return True if the listener has consumed the event, false otherwise.
13999 */
14000 boolean onHover(View v, MotionEvent event);
14001 }
14002
14003 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014004 * Interface definition for a callback to be invoked when a generic motion event is
14005 * dispatched to this view. The callback will be invoked before the generic motion
14006 * event is given to the view.
14007 */
14008 public interface OnGenericMotionListener {
14009 /**
14010 * Called when a generic motion event is dispatched to a view. This allows listeners to
14011 * get a chance to respond before the target view.
14012 *
14013 * @param v The view the generic motion event has been dispatched to.
14014 * @param event The MotionEvent object containing full information about
14015 * the event.
14016 * @return True if the listener has consumed the event, false otherwise.
14017 */
14018 boolean onGenericMotion(View v, MotionEvent event);
14019 }
14020
14021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014022 * Interface definition for a callback to be invoked when a view has been clicked and held.
14023 */
14024 public interface OnLongClickListener {
14025 /**
14026 * Called when a view has been clicked and held.
14027 *
14028 * @param v The view that was clicked and held.
14029 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014030 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 */
14032 boolean onLongClick(View v);
14033 }
14034
14035 /**
Chris Tate32affef2010-10-18 15:29:21 -070014036 * Interface definition for a callback to be invoked when a drag is being dispatched
14037 * to this view. The callback will be invoked before the hosting view's own
14038 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14039 * onDrag(event) behavior, it should return 'false' from this callback.
14040 */
14041 public interface OnDragListener {
14042 /**
14043 * Called when a drag event is dispatched to a view. This allows listeners
14044 * to get a chance to override base View behavior.
14045 *
Joe Malin32736f02011-01-19 16:14:20 -080014046 * @param v The View that received the drag event.
14047 * @param event The {@link android.view.DragEvent} object for the drag event.
14048 * @return {@code true} if the drag event was handled successfully, or {@code false}
14049 * if the drag event was not handled. Note that {@code false} will trigger the View
14050 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014051 */
14052 boolean onDrag(View v, DragEvent event);
14053 }
14054
14055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014056 * Interface definition for a callback to be invoked when the focus state of
14057 * a view changed.
14058 */
14059 public interface OnFocusChangeListener {
14060 /**
14061 * Called when the focus state of a view has changed.
14062 *
14063 * @param v The view whose state has changed.
14064 * @param hasFocus The new focus state of v.
14065 */
14066 void onFocusChange(View v, boolean hasFocus);
14067 }
14068
14069 /**
14070 * Interface definition for a callback to be invoked when a view is clicked.
14071 */
14072 public interface OnClickListener {
14073 /**
14074 * Called when a view has been clicked.
14075 *
14076 * @param v The view that was clicked.
14077 */
14078 void onClick(View v);
14079 }
14080
14081 /**
14082 * Interface definition for a callback to be invoked when the context menu
14083 * for this view is being built.
14084 */
14085 public interface OnCreateContextMenuListener {
14086 /**
14087 * Called when the context menu for this view is being built. It is not
14088 * safe to hold onto the menu after this method returns.
14089 *
14090 * @param menu The context menu that is being built
14091 * @param v The view for which the context menu is being built
14092 * @param menuInfo Extra information about the item for which the
14093 * context menu should be shown. This information will vary
14094 * depending on the class of v.
14095 */
14096 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14097 }
14098
Joe Onorato664644d2011-01-23 17:53:23 -080014099 /**
14100 * Interface definition for a callback to be invoked when the status bar changes
14101 * visibility.
14102 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014103 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014104 */
14105 public interface OnSystemUiVisibilityChangeListener {
14106 /**
14107 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014108 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014109 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014110 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14111 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014112 */
14113 public void onSystemUiVisibilityChange(int visibility);
14114 }
14115
Adam Powell4afd62b2011-02-18 15:02:18 -080014116 /**
14117 * Interface definition for a callback to be invoked when this view is attached
14118 * or detached from its window.
14119 */
14120 public interface OnAttachStateChangeListener {
14121 /**
14122 * Called when the view is attached to a window.
14123 * @param v The view that was attached
14124 */
14125 public void onViewAttachedToWindow(View v);
14126 /**
14127 * Called when the view is detached from a window.
14128 * @param v The view that was detached
14129 */
14130 public void onViewDetachedFromWindow(View v);
14131 }
14132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014133 private final class UnsetPressedState implements Runnable {
14134 public void run() {
14135 setPressed(false);
14136 }
14137 }
14138
14139 /**
14140 * Base class for derived classes that want to save and restore their own
14141 * state in {@link android.view.View#onSaveInstanceState()}.
14142 */
14143 public static class BaseSavedState extends AbsSavedState {
14144 /**
14145 * Constructor used when reading from a parcel. Reads the state of the superclass.
14146 *
14147 * @param source
14148 */
14149 public BaseSavedState(Parcel source) {
14150 super(source);
14151 }
14152
14153 /**
14154 * Constructor called by derived classes when creating their SavedState objects
14155 *
14156 * @param superState The state of the superclass of this view
14157 */
14158 public BaseSavedState(Parcelable superState) {
14159 super(superState);
14160 }
14161
14162 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14163 new Parcelable.Creator<BaseSavedState>() {
14164 public BaseSavedState createFromParcel(Parcel in) {
14165 return new BaseSavedState(in);
14166 }
14167
14168 public BaseSavedState[] newArray(int size) {
14169 return new BaseSavedState[size];
14170 }
14171 };
14172 }
14173
14174 /**
14175 * A set of information given to a view when it is attached to its parent
14176 * window.
14177 */
14178 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179 interface Callbacks {
14180 void playSoundEffect(int effectId);
14181 boolean performHapticFeedback(int effectId, boolean always);
14182 }
14183
14184 /**
14185 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14186 * to a Handler. This class contains the target (View) to invalidate and
14187 * the coordinates of the dirty rectangle.
14188 *
14189 * For performance purposes, this class also implements a pool of up to
14190 * POOL_LIMIT objects that get reused. This reduces memory allocations
14191 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014192 */
Romain Guyd928d682009-03-31 17:52:16 -070014193 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014194 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014195 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14196 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014197 public InvalidateInfo newInstance() {
14198 return new InvalidateInfo();
14199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014200
Romain Guyd928d682009-03-31 17:52:16 -070014201 public void onAcquired(InvalidateInfo element) {
14202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014203
Romain Guyd928d682009-03-31 17:52:16 -070014204 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014205 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014206 }
14207 }, POOL_LIMIT)
14208 );
14209
14210 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014211 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212
14213 View target;
14214
14215 int left;
14216 int top;
14217 int right;
14218 int bottom;
14219
Romain Guyd928d682009-03-31 17:52:16 -070014220 public void setNextPoolable(InvalidateInfo element) {
14221 mNext = element;
14222 }
14223
14224 public InvalidateInfo getNextPoolable() {
14225 return mNext;
14226 }
14227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014228 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014229 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014230 }
14231
14232 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014233 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014234 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014235
14236 public boolean isPooled() {
14237 return mIsPooled;
14238 }
14239
14240 public void setPooled(boolean isPooled) {
14241 mIsPooled = isPooled;
14242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 }
14244
14245 final IWindowSession mSession;
14246
14247 final IWindow mWindow;
14248
14249 final IBinder mWindowToken;
14250
14251 final Callbacks mRootCallbacks;
14252
Romain Guy59a12ca2011-06-09 17:48:21 -070014253 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 /**
14256 * The top view of the hierarchy.
14257 */
14258 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014260 IBinder mPanelParentWindowToken;
14261 Surface mSurface;
14262
Romain Guyb051e892010-09-28 19:09:36 -070014263 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014264 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014265 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014267 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014268 * Scale factor used by the compatibility mode
14269 */
14270 float mApplicationScale;
14271
14272 /**
14273 * Indicates whether the application is in compatibility mode
14274 */
14275 boolean mScalingRequired;
14276
14277 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014278 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014279 */
14280 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014281
Dianne Hackborn63042d62011-01-26 18:56:29 -080014282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014283 * Left position of this view's window
14284 */
14285 int mWindowLeft;
14286
14287 /**
14288 * Top position of this view's window
14289 */
14290 int mWindowTop;
14291
14292 /**
Adam Powell26153a32010-11-08 15:22:27 -080014293 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014294 */
Adam Powell26153a32010-11-08 15:22:27 -080014295 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014296
14297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 * For windows that are full-screen but using insets to layout inside
14299 * of the screen decorations, these are the current insets for the
14300 * content of the window.
14301 */
14302 final Rect mContentInsets = new Rect();
14303
14304 /**
14305 * For windows that are full-screen but using insets to layout inside
14306 * of the screen decorations, these are the current insets for the
14307 * actual visible parts of the window.
14308 */
14309 final Rect mVisibleInsets = new Rect();
14310
14311 /**
14312 * The internal insets given by this window. This value is
14313 * supplied by the client (through
14314 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14315 * be given to the window manager when changed to be used in laying
14316 * out windows behind it.
14317 */
14318 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14319 = new ViewTreeObserver.InternalInsetsInfo();
14320
14321 /**
14322 * All views in the window's hierarchy that serve as scroll containers,
14323 * used to determine if the window can be resized or must be panned
14324 * to adjust for a soft input area.
14325 */
14326 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14327
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014328 final KeyEvent.DispatcherState mKeyDispatchState
14329 = new KeyEvent.DispatcherState();
14330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014331 /**
14332 * Indicates whether the view's window currently has the focus.
14333 */
14334 boolean mHasWindowFocus;
14335
14336 /**
14337 * The current visibility of the window.
14338 */
14339 int mWindowVisibility;
14340
14341 /**
14342 * Indicates the time at which drawing started to occur.
14343 */
14344 long mDrawingTime;
14345
14346 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014347 * Indicates whether or not ignoring the DIRTY_MASK flags.
14348 */
14349 boolean mIgnoreDirtyState;
14350
14351 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014352 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14353 * to avoid clearing that flag prematurely.
14354 */
14355 boolean mSetIgnoreDirtyState = false;
14356
14357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014358 * Indicates whether the view's window is currently in touch mode.
14359 */
14360 boolean mInTouchMode;
14361
14362 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014363 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014364 * the next time it performs a traversal
14365 */
14366 boolean mRecomputeGlobalAttributes;
14367
14368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014369 * Set during a traveral if any views want to keep the screen on.
14370 */
14371 boolean mKeepScreenOn;
14372
14373 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014374 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14375 */
14376 int mSystemUiVisibility;
14377
14378 /**
14379 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14380 * attached.
14381 */
14382 boolean mHasSystemUiListeners;
14383
14384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014385 * Set if the visibility of any views has changed.
14386 */
14387 boolean mViewVisibilityChanged;
14388
14389 /**
14390 * Set to true if a view has been scrolled.
14391 */
14392 boolean mViewScrollChanged;
14393
14394 /**
14395 * Global to the view hierarchy used as a temporary for dealing with
14396 * x/y points in the transparent region computations.
14397 */
14398 final int[] mTransparentLocation = new int[2];
14399
14400 /**
14401 * Global to the view hierarchy used as a temporary for dealing with
14402 * x/y points in the ViewGroup.invalidateChild implementation.
14403 */
14404 final int[] mInvalidateChildLocation = new int[2];
14405
Chet Haasec3aa3612010-06-17 08:50:37 -070014406
14407 /**
14408 * Global to the view hierarchy used as a temporary for dealing with
14409 * x/y location when view is transformed.
14410 */
14411 final float[] mTmpTransformLocation = new float[2];
14412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014413 /**
14414 * The view tree observer used to dispatch global events like
14415 * layout, pre-draw, touch mode change, etc.
14416 */
14417 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14418
14419 /**
14420 * A Canvas used by the view hierarchy to perform bitmap caching.
14421 */
14422 Canvas mCanvas;
14423
14424 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014425 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 * handler can be used to pump events in the UI events queue.
14427 */
14428 final Handler mHandler;
14429
14430 /**
14431 * Identifier for messages requesting the view to be invalidated.
14432 * Such messages should be sent to {@link #mHandler}.
14433 */
14434 static final int INVALIDATE_MSG = 0x1;
14435
14436 /**
14437 * Identifier for messages requesting the view to invalidate a region.
14438 * Such messages should be sent to {@link #mHandler}.
14439 */
14440 static final int INVALIDATE_RECT_MSG = 0x2;
14441
14442 /**
14443 * Temporary for use in computing invalidate rectangles while
14444 * calling up the hierarchy.
14445 */
14446 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014447
14448 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014449 * Temporary for use in computing hit areas with transformed views
14450 */
14451 final RectF mTmpTransformRect = new RectF();
14452
14453 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014454 * Temporary list for use in collecting focusable descendents of a view.
14455 */
14456 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014458 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014459 * The id of the window for accessibility purposes.
14460 */
14461 int mAccessibilityWindowId = View.NO_ID;
14462
14463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014464 * Creates a new set of attachment information with the specified
14465 * events handler and thread.
14466 *
14467 * @param handler the events handler the view must use
14468 */
14469 AttachInfo(IWindowSession session, IWindow window,
14470 Handler handler, Callbacks effectPlayer) {
14471 mSession = session;
14472 mWindow = window;
14473 mWindowToken = window.asBinder();
14474 mHandler = handler;
14475 mRootCallbacks = effectPlayer;
14476 }
14477 }
14478
14479 /**
14480 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14481 * is supported. This avoids keeping too many unused fields in most
14482 * instances of View.</p>
14483 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014484 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014485
Mike Cleronf116bf82009-09-27 19:14:12 -070014486 /**
14487 * Scrollbars are not visible
14488 */
14489 public static final int OFF = 0;
14490
14491 /**
14492 * Scrollbars are visible
14493 */
14494 public static final int ON = 1;
14495
14496 /**
14497 * Scrollbars are fading away
14498 */
14499 public static final int FADING = 2;
14500
14501 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014503 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014504 public int scrollBarDefaultDelayBeforeFade;
14505 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506
14507 public int scrollBarSize;
14508 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014509 public float[] interpolatorValues;
14510 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014511
14512 public final Paint paint;
14513 public final Matrix matrix;
14514 public Shader shader;
14515
Mike Cleronf116bf82009-09-27 19:14:12 -070014516 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14517
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014518 private static final float[] OPAQUE = { 255 };
14519 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014520
Mike Cleronf116bf82009-09-27 19:14:12 -070014521 /**
14522 * When fading should start. This time moves into the future every time
14523 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14524 */
14525 public long fadeStartTime;
14526
14527
14528 /**
14529 * The current state of the scrollbars: ON, OFF, or FADING
14530 */
14531 public int state = OFF;
14532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 private int mLastColor;
14534
Mike Cleronf116bf82009-09-27 19:14:12 -070014535 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014536 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14537 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014538 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14539 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014540
14541 paint = new Paint();
14542 matrix = new Matrix();
14543 // use use a height of 1, and then wack the matrix each time we
14544 // actually use it.
14545 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014547 paint.setShader(shader);
14548 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014549 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014550 }
Romain Guy8506ab42009-06-11 17:35:47 -070014551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 public void setFadeColor(int color) {
14553 if (color != 0 && color != mLastColor) {
14554 mLastColor = color;
14555 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014556
Romain Guye55e1a72009-08-27 10:42:26 -070014557 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14558 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014560 paint.setShader(shader);
14561 // Restore the default transfer mode (src_over)
14562 paint.setXfermode(null);
14563 }
14564 }
Joe Malin32736f02011-01-19 16:14:20 -080014565
Mike Cleronf116bf82009-09-27 19:14:12 -070014566 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014567 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014568 if (now >= fadeStartTime) {
14569
14570 // the animation fades the scrollbars out by changing
14571 // the opacity (alpha) from fully opaque to fully
14572 // transparent
14573 int nextFrame = (int) now;
14574 int framesCount = 0;
14575
14576 Interpolator interpolator = scrollBarInterpolator;
14577
14578 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014579 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014580
14581 // End transparent
14582 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014583 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014584
14585 state = FADING;
14586
14587 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014588 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014589 }
14590 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014591 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014592
Svetoslav Ganova0156172011-06-26 17:55:44 -070014593 /**
14594 * Resuable callback for sending
14595 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14596 */
14597 private class SendViewScrolledAccessibilityEvent implements Runnable {
14598 public volatile boolean mIsPending;
14599
14600 public void run() {
14601 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14602 mIsPending = false;
14603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014604 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014605
14606 /**
14607 * <p>
14608 * This class represents a delegate that can be registered in a {@link View}
14609 * to enhance accessibility support via composition rather via inheritance.
14610 * It is specifically targeted to widget developers that extend basic View
14611 * classes i.e. classes in package android.view, that would like their
14612 * applications to be backwards compatible.
14613 * </p>
14614 * <p>
14615 * A scenario in which a developer would like to use an accessibility delegate
14616 * is overriding a method introduced in a later API version then the minimal API
14617 * version supported by the application. For example, the method
14618 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14619 * in API version 4 when the accessibility APIs were first introduced. If a
14620 * developer would like his application to run on API version 4 devices (assuming
14621 * all other APIs used by the application are version 4 or lower) and take advantage
14622 * of this method, instead of overriding the method which would break the application's
14623 * backwards compatibility, he can override the corresponding method in this
14624 * delegate and register the delegate in the target View if the API version of
14625 * the system is high enough i.e. the API version is same or higher to the API
14626 * version that introduced
14627 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14628 * </p>
14629 * <p>
14630 * Here is an example implementation:
14631 * </p>
14632 * <code><pre><p>
14633 * if (Build.VERSION.SDK_INT >= 14) {
14634 * // If the API version is equal of higher than the version in
14635 * // which onInitializeAccessibilityNodeInfo was introduced we
14636 * // register a delegate with a customized implementation.
14637 * View view = findViewById(R.id.view_id);
14638 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14639 * public void onInitializeAccessibilityNodeInfo(View host,
14640 * AccessibilityNodeInfo info) {
14641 * // Let the default implementation populate the info.
14642 * super.onInitializeAccessibilityNodeInfo(host, info);
14643 * // Set some other information.
14644 * info.setEnabled(host.isEnabled());
14645 * }
14646 * });
14647 * }
14648 * </code></pre></p>
14649 * <p>
14650 * This delegate contains methods that correspond to the accessibility methods
14651 * in View. If a delegate has been specified the implementation in View hands
14652 * off handling to the corresponding method in this delegate. The default
14653 * implementation the delegate methods behaves exactly as the corresponding
14654 * method in View for the case of no accessibility delegate been set. Hence,
14655 * to customize the behavior of a View method, clients can override only the
14656 * corresponding delegate method without altering the behavior of the rest
14657 * accessibility related methods of the host view.
14658 * </p>
14659 */
14660 public static class AccessibilityDelegate {
14661
14662 /**
14663 * Sends an accessibility event of the given type. If accessibility is not
14664 * enabled this method has no effect.
14665 * <p>
14666 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14667 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14668 * been set.
14669 * </p>
14670 *
14671 * @param host The View hosting the delegate.
14672 * @param eventType The type of the event to send.
14673 *
14674 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14675 */
14676 public void sendAccessibilityEvent(View host, int eventType) {
14677 host.sendAccessibilityEventInternal(eventType);
14678 }
14679
14680 /**
14681 * Sends an accessibility event. This method behaves exactly as
14682 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14683 * empty {@link AccessibilityEvent} and does not perform a check whether
14684 * accessibility is enabled.
14685 * <p>
14686 * The default implementation behaves as
14687 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14688 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14689 * the case of no accessibility delegate been set.
14690 * </p>
14691 *
14692 * @param host The View hosting the delegate.
14693 * @param event The event to send.
14694 *
14695 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14696 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14697 */
14698 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14699 host.sendAccessibilityEventUncheckedInternal(event);
14700 }
14701
14702 /**
14703 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14704 * to its children for adding their text content to the event.
14705 * <p>
14706 * The default implementation behaves as
14707 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14708 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14709 * the case of no accessibility delegate been set.
14710 * </p>
14711 *
14712 * @param host The View hosting the delegate.
14713 * @param event The event.
14714 * @return True if the event population was completed.
14715 *
14716 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14717 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14718 */
14719 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14720 return host.dispatchPopulateAccessibilityEventInternal(event);
14721 }
14722
14723 /**
14724 * Gives a chance to the host View to populate the accessibility event with its
14725 * text content.
14726 * <p>
14727 * The default implementation behaves as
14728 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14729 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14730 * the case of no accessibility delegate been set.
14731 * </p>
14732 *
14733 * @param host The View hosting the delegate.
14734 * @param event The accessibility event which to populate.
14735 *
14736 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14737 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14738 */
14739 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14740 host.onPopulateAccessibilityEventInternal(event);
14741 }
14742
14743 /**
14744 * Initializes an {@link AccessibilityEvent} with information about the
14745 * the host View which is the event source.
14746 * <p>
14747 * The default implementation behaves as
14748 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14749 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14750 * the case of no accessibility delegate been set.
14751 * </p>
14752 *
14753 * @param host The View hosting the delegate.
14754 * @param event The event to initialize.
14755 *
14756 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14757 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14758 */
14759 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14760 host.onInitializeAccessibilityEventInternal(event);
14761 }
14762
14763 /**
14764 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14765 * <p>
14766 * The default implementation behaves as
14767 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14768 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14769 * the case of no accessibility delegate been set.
14770 * </p>
14771 *
14772 * @param host The View hosting the delegate.
14773 * @param info The instance to initialize.
14774 *
14775 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14776 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14777 */
14778 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14779 host.onInitializeAccessibilityNodeInfoInternal(info);
14780 }
14781
14782 /**
14783 * Called when a child of the host View has requested sending an
14784 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14785 * to augment the event.
14786 * <p>
14787 * The default implementation behaves as
14788 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14789 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14790 * the case of no accessibility delegate been set.
14791 * </p>
14792 *
14793 * @param host The View hosting the delegate.
14794 * @param child The child which requests sending the event.
14795 * @param event The event to be sent.
14796 * @return True if the event should be sent
14797 *
14798 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14799 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14800 */
14801 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14802 AccessibilityEvent event) {
14803 return host.onRequestSendAccessibilityEventInternal(child, event);
14804 }
14805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806}