blob: 632ab4acb25e7702625174e17a1dc30ec82758df [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 }
Chet Haase1a76dcd2011-10-06 11:16:40 -07003338 if (!mOnLayoutChangeListeners.contains(listener)) {
3339 mOnLayoutChangeListeners.add(listener);
3340 }
Chet Haase21cd1382010-09-01 17:42:29 -07003341 }
3342
3343 /**
3344 * Remove a listener for layout changes.
3345 *
3346 * @param listener The listener for layout bounds change.
3347 */
3348 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3349 if (mOnLayoutChangeListeners == null) {
3350 return;
3351 }
3352 mOnLayoutChangeListeners.remove(listener);
3353 }
3354
3355 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003356 * Add a listener for attach state changes.
3357 *
3358 * This listener will be called whenever this view is attached or detached
3359 * from a window. Remove the listener using
3360 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3361 *
3362 * @param listener Listener to attach
3363 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3364 */
3365 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3366 if (mOnAttachStateChangeListeners == null) {
3367 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3368 }
3369 mOnAttachStateChangeListeners.add(listener);
3370 }
3371
3372 /**
3373 * Remove a listener for attach state changes. The listener will receive no further
3374 * notification of window attach/detach events.
3375 *
3376 * @param listener Listener to remove
3377 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3378 */
3379 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3380 if (mOnAttachStateChangeListeners == null) {
3381 return;
3382 }
3383 mOnAttachStateChangeListeners.remove(listener);
3384 }
3385
3386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 * Returns the focus-change callback registered for this view.
3388 *
3389 * @return The callback, or null if one is not registered.
3390 */
3391 public OnFocusChangeListener getOnFocusChangeListener() {
3392 return mOnFocusChangeListener;
3393 }
3394
3395 /**
3396 * Register a callback to be invoked when this view is clicked. If this view is not
3397 * clickable, it becomes clickable.
3398 *
3399 * @param l The callback that will run
3400 *
3401 * @see #setClickable(boolean)
3402 */
3403 public void setOnClickListener(OnClickListener l) {
3404 if (!isClickable()) {
3405 setClickable(true);
3406 }
3407 mOnClickListener = l;
3408 }
3409
3410 /**
3411 * Register a callback to be invoked when this view is clicked and held. If this view is not
3412 * long clickable, it becomes long clickable.
3413 *
3414 * @param l The callback that will run
3415 *
3416 * @see #setLongClickable(boolean)
3417 */
3418 public void setOnLongClickListener(OnLongClickListener l) {
3419 if (!isLongClickable()) {
3420 setLongClickable(true);
3421 }
3422 mOnLongClickListener = l;
3423 }
3424
3425 /**
3426 * Register a callback to be invoked when the context menu for this view is
3427 * being built. If this view is not long clickable, it becomes long clickable.
3428 *
3429 * @param l The callback that will run
3430 *
3431 */
3432 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3433 if (!isLongClickable()) {
3434 setLongClickable(true);
3435 }
3436 mOnCreateContextMenuListener = l;
3437 }
3438
3439 /**
3440 * Call this view's OnClickListener, if it is defined.
3441 *
3442 * @return True there was an assigned OnClickListener that was called, false
3443 * otherwise is returned.
3444 */
3445 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003446 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 if (mOnClickListener != null) {
3449 playSoundEffect(SoundEffectConstants.CLICK);
3450 mOnClickListener.onClick(this);
3451 return true;
3452 }
3453
3454 return false;
3455 }
3456
3457 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003458 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3459 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003461 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 */
3463 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003464 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 boolean handled = false;
3467 if (mOnLongClickListener != null) {
3468 handled = mOnLongClickListener.onLongClick(View.this);
3469 }
3470 if (!handled) {
3471 handled = showContextMenu();
3472 }
3473 if (handled) {
3474 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3475 }
3476 return handled;
3477 }
3478
3479 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003480 * Performs button-related actions during a touch down event.
3481 *
3482 * @param event The event.
3483 * @return True if the down was consumed.
3484 *
3485 * @hide
3486 */
3487 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3488 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3489 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3490 return true;
3491 }
3492 }
3493 return false;
3494 }
3495
3496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 * Bring up the context menu for this view.
3498 *
3499 * @return Whether a context menu was displayed.
3500 */
3501 public boolean showContextMenu() {
3502 return getParent().showContextMenuForChild(this);
3503 }
3504
3505 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003506 * Bring up the context menu for this view, referring to the item under the specified point.
3507 *
3508 * @param x The referenced x coordinate.
3509 * @param y The referenced y coordinate.
3510 * @param metaState The keyboard modifiers that were pressed.
3511 * @return Whether a context menu was displayed.
3512 *
3513 * @hide
3514 */
3515 public boolean showContextMenu(float x, float y, int metaState) {
3516 return showContextMenu();
3517 }
3518
3519 /**
Adam Powell6e346362010-07-23 10:18:23 -07003520 * Start an action mode.
3521 *
3522 * @param callback Callback that will control the lifecycle of the action mode
3523 * @return The new action mode if it is started, null otherwise
3524 *
3525 * @see ActionMode
3526 */
3527 public ActionMode startActionMode(ActionMode.Callback callback) {
3528 return getParent().startActionModeForChild(this, callback);
3529 }
3530
3531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 * Register a callback to be invoked when a key is pressed in this view.
3533 * @param l the key listener to attach to this view
3534 */
3535 public void setOnKeyListener(OnKeyListener l) {
3536 mOnKeyListener = l;
3537 }
3538
3539 /**
3540 * Register a callback to be invoked when a touch event is sent to this view.
3541 * @param l the touch listener to attach to this view
3542 */
3543 public void setOnTouchListener(OnTouchListener l) {
3544 mOnTouchListener = l;
3545 }
3546
3547 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003548 * Register a callback to be invoked when a generic motion event is sent to this view.
3549 * @param l the generic motion listener to attach to this view
3550 */
3551 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3552 mOnGenericMotionListener = l;
3553 }
3554
3555 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003556 * Register a callback to be invoked when a hover event is sent to this view.
3557 * @param l the hover listener to attach to this view
3558 */
3559 public void setOnHoverListener(OnHoverListener l) {
3560 mOnHoverListener = l;
3561 }
3562
3563 /**
Joe Malin32736f02011-01-19 16:14:20 -08003564 * Register a drag event listener callback object for this View. The parameter is
3565 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3566 * View, the system calls the
3567 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3568 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003569 */
3570 public void setOnDragListener(OnDragListener l) {
3571 mOnDragListener = l;
3572 }
3573
3574 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003575 * Give this view focus. This will cause
3576 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 *
3578 * Note: this does not check whether this {@link View} should get focus, it just
3579 * gives it focus no matter what. It should only be called internally by framework
3580 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3581 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003582 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3583 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 * focus moved when requestFocus() is called. It may not always
3585 * apply, in which case use the default View.FOCUS_DOWN.
3586 * @param previouslyFocusedRect The rectangle of the view that had focus
3587 * prior in this View's coordinate system.
3588 */
3589 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3590 if (DBG) {
3591 System.out.println(this + " requestFocus()");
3592 }
3593
3594 if ((mPrivateFlags & FOCUSED) == 0) {
3595 mPrivateFlags |= FOCUSED;
3596
3597 if (mParent != null) {
3598 mParent.requestChildFocus(this, this);
3599 }
3600
3601 onFocusChanged(true, direction, previouslyFocusedRect);
3602 refreshDrawableState();
3603 }
3604 }
3605
3606 /**
3607 * Request that a rectangle of this view be visible on the screen,
3608 * scrolling if necessary just enough.
3609 *
3610 * <p>A View should call this if it maintains some notion of which part
3611 * of its content is interesting. For example, a text editing view
3612 * should call this when its cursor moves.
3613 *
3614 * @param rectangle The rectangle.
3615 * @return Whether any parent scrolled.
3616 */
3617 public boolean requestRectangleOnScreen(Rect rectangle) {
3618 return requestRectangleOnScreen(rectangle, false);
3619 }
3620
3621 /**
3622 * Request that a rectangle of this view be visible on the screen,
3623 * scrolling if necessary just enough.
3624 *
3625 * <p>A View should call this if it maintains some notion of which part
3626 * of its content is interesting. For example, a text editing view
3627 * should call this when its cursor moves.
3628 *
3629 * <p>When <code>immediate</code> is set to true, scrolling will not be
3630 * animated.
3631 *
3632 * @param rectangle The rectangle.
3633 * @param immediate True to forbid animated scrolling, false otherwise
3634 * @return Whether any parent scrolled.
3635 */
3636 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3637 View child = this;
3638 ViewParent parent = mParent;
3639 boolean scrolled = false;
3640 while (parent != null) {
3641 scrolled |= parent.requestChildRectangleOnScreen(child,
3642 rectangle, immediate);
3643
3644 // offset rect so next call has the rectangle in the
3645 // coordinate system of its direct child.
3646 rectangle.offset(child.getLeft(), child.getTop());
3647 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3648
3649 if (!(parent instanceof View)) {
3650 break;
3651 }
Romain Guy8506ab42009-06-11 17:35:47 -07003652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 child = (View) parent;
3654 parent = child.getParent();
3655 }
3656 return scrolled;
3657 }
3658
3659 /**
3660 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003661 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 */
3663 public void clearFocus() {
3664 if (DBG) {
3665 System.out.println(this + " clearFocus()");
3666 }
3667
3668 if ((mPrivateFlags & FOCUSED) != 0) {
3669 mPrivateFlags &= ~FOCUSED;
3670
3671 if (mParent != null) {
3672 mParent.clearChildFocus(this);
3673 }
3674
3675 onFocusChanged(false, 0, null);
3676 refreshDrawableState();
3677 }
3678 }
3679
3680 /**
3681 * Called to clear the focus of a view that is about to be removed.
3682 * Doesn't call clearChildFocus, which prevents this view from taking
3683 * focus again before it has been removed from the parent
3684 */
3685 void clearFocusForRemoval() {
3686 if ((mPrivateFlags & FOCUSED) != 0) {
3687 mPrivateFlags &= ~FOCUSED;
3688
3689 onFocusChanged(false, 0, null);
3690 refreshDrawableState();
3691 }
3692 }
3693
3694 /**
3695 * Called internally by the view system when a new view is getting focus.
3696 * This is what clears the old focus.
3697 */
3698 void unFocus() {
3699 if (DBG) {
3700 System.out.println(this + " unFocus()");
3701 }
3702
3703 if ((mPrivateFlags & FOCUSED) != 0) {
3704 mPrivateFlags &= ~FOCUSED;
3705
3706 onFocusChanged(false, 0, null);
3707 refreshDrawableState();
3708 }
3709 }
3710
3711 /**
3712 * Returns true if this view has focus iteself, or is the ancestor of the
3713 * view that has focus.
3714 *
3715 * @return True if this view has or contains focus, false otherwise.
3716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003717 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 public boolean hasFocus() {
3719 return (mPrivateFlags & FOCUSED) != 0;
3720 }
3721
3722 /**
3723 * Returns true if this view is focusable or if it contains a reachable View
3724 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3725 * is a View whose parents do not block descendants focus.
3726 *
3727 * Only {@link #VISIBLE} views are considered focusable.
3728 *
3729 * @return True if the view is focusable or if the view contains a focusable
3730 * View, false otherwise.
3731 *
3732 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3733 */
3734 public boolean hasFocusable() {
3735 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3736 }
3737
3738 /**
3739 * Called by the view system when the focus state of this view changes.
3740 * When the focus change event is caused by directional navigation, direction
3741 * and previouslyFocusedRect provide insight into where the focus is coming from.
3742 * When overriding, be sure to call up through to the super class so that
3743 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 * @param gainFocus True if the View has focus; false otherwise.
3746 * @param direction The direction focus has moved when requestFocus()
3747 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003748 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3749 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3750 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3752 * system, of the previously focused view. If applicable, this will be
3753 * passed in as finer grained information about where the focus is coming
3754 * from (in addition to direction). Will be <code>null</code> otherwise.
3755 */
3756 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003757 if (gainFocus) {
3758 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3759 }
3760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 InputMethodManager imm = InputMethodManager.peekInstance();
3762 if (!gainFocus) {
3763 if (isPressed()) {
3764 setPressed(false);
3765 }
3766 if (imm != null && mAttachInfo != null
3767 && mAttachInfo.mHasWindowFocus) {
3768 imm.focusOut(this);
3769 }
Romain Guya2431d02009-04-30 16:30:00 -07003770 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 } else if (imm != null && mAttachInfo != null
3772 && mAttachInfo.mHasWindowFocus) {
3773 imm.focusIn(this);
3774 }
Romain Guy8506ab42009-06-11 17:35:47 -07003775
Romain Guy0fd89bf2011-01-26 15:41:30 -08003776 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 if (mOnFocusChangeListener != null) {
3778 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3779 }
Joe Malin32736f02011-01-19 16:14:20 -08003780
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003781 if (mAttachInfo != null) {
3782 mAttachInfo.mKeyDispatchState.reset(this);
3783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 }
3785
3786 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003787 * Sends an accessibility event of the given type. If accessiiblity is
3788 * not enabled this method has no effect. The default implementation calls
3789 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3790 * to populate information about the event source (this View), then calls
3791 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3792 * populate the text content of the event source including its descendants,
3793 * and last calls
3794 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3795 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003796 * <p>
3797 * If an {@link AccessibilityDelegate} has been specified via calling
3798 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3799 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3800 * responsible for handling this call.
3801 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003802 *
3803 * @param eventType The type of the event to send.
3804 *
3805 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3806 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3807 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003808 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003809 */
3810 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003811 if (mAccessibilityDelegate != null) {
3812 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3813 } else {
3814 sendAccessibilityEventInternal(eventType);
3815 }
3816 }
3817
3818 /**
3819 * @see #sendAccessibilityEvent(int)
3820 *
3821 * Note: Called from the default {@link AccessibilityDelegate}.
3822 */
3823 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003824 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3825 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3826 }
3827 }
3828
3829 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003830 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3831 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003832 * perform a check whether accessibility is enabled.
3833 * <p>
3834 * If an {@link AccessibilityDelegate} has been specified via calling
3835 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3836 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3837 * is responsible for handling this call.
3838 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003839 *
3840 * @param event The event to send.
3841 *
3842 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003843 */
3844 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003845 if (mAccessibilityDelegate != null) {
3846 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3847 } else {
3848 sendAccessibilityEventUncheckedInternal(event);
3849 }
3850 }
3851
3852 /**
3853 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3854 *
3855 * Note: Called from the default {@link AccessibilityDelegate}.
3856 */
3857 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003858 if (!isShown()) {
3859 return;
3860 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003861 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003862 // Only a subset of accessibility events populates text content.
3863 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3864 dispatchPopulateAccessibilityEvent(event);
3865 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003866 // In the beginning we called #isShown(), so we know that getParent() is not null.
3867 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003868 }
3869
3870 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003871 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3872 * to its children for adding their text content to the event. Note that the
3873 * event text is populated in a separate dispatch path since we add to the
3874 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003875 * A typical implementation will call
3876 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3877 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3878 * on each child. Override this method if custom population of the event text
3879 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003880 * <p>
3881 * If an {@link AccessibilityDelegate} has been specified via calling
3882 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3883 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3884 * is responsible for handling this call.
3885 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003886 * <p>
3887 * <em>Note:</em> Accessibility events of certain types are not dispatched for
3888 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
3889 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003890 *
3891 * @param event The event.
3892 *
3893 * @return True if the event population was completed.
3894 */
3895 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003896 if (mAccessibilityDelegate != null) {
3897 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3898 } else {
3899 return dispatchPopulateAccessibilityEventInternal(event);
3900 }
3901 }
3902
3903 /**
3904 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3905 *
3906 * Note: Called from the default {@link AccessibilityDelegate}.
3907 */
3908 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003909 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003910 return false;
3911 }
3912
3913 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003914 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003915 * giving a chance to this View to populate the accessibility event with its
3916 * text content. While the implementation is free to modify other event
3917 * attributes this should be performed in
3918 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3919 * <p>
3920 * Example: Adding formatted date string to an accessibility event in addition
3921 * to the text added by the super implementation.
3922 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003923 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3924 * super.onPopulateAccessibilityEvent(event);
3925 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3926 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3927 * mCurrentDate.getTimeInMillis(), flags);
3928 * event.getText().add(selectedDateUtterance);
3929 * }
3930 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003931 * <p>
3932 * If an {@link AccessibilityDelegate} has been specified via calling
3933 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3934 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3935 * is responsible for handling this call.
3936 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003937 *
3938 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003939 *
3940 * @see #sendAccessibilityEvent(int)
3941 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003942 */
3943 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003944 if (mAccessibilityDelegate != null) {
3945 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3946 } else {
3947 onPopulateAccessibilityEventInternal(event);
3948 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003949 }
3950
3951 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003952 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3953 *
3954 * Note: Called from the default {@link AccessibilityDelegate}.
3955 */
3956 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3957
3958 }
3959
3960 /**
3961 * Initializes an {@link AccessibilityEvent} with information about
3962 * this View which is the event source. In other words, the source of
3963 * an accessibility event is the view whose state change triggered firing
3964 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003965 * <p>
3966 * Example: Setting the password property of an event in addition
3967 * to properties set by the super implementation.
3968 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003969 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3970 * super.onInitializeAccessibilityEvent(event);
3971 * event.setPassword(true);
3972 * }
3973 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003974 * <p>
3975 * If an {@link AccessibilityDelegate} has been specified via calling
3976 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3977 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
3978 * is responsible for handling this call.
3979 * </p>
3980 *
3981 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003982 *
3983 * @see #sendAccessibilityEvent(int)
3984 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3985 */
3986 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003987 if (mAccessibilityDelegate != null) {
3988 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
3989 } else {
3990 onInitializeAccessibilityEventInternal(event);
3991 }
3992 }
3993
3994 /**
3995 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3996 *
3997 * Note: Called from the default {@link AccessibilityDelegate}.
3998 */
3999 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004000 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07004001 event.setClassName(getClass().getName());
4002 event.setPackageName(getContext().getPackageName());
4003 event.setEnabled(isEnabled());
4004 event.setContentDescription(mContentDescription);
4005
Svetoslav Ganova0156172011-06-26 17:55:44 -07004006 final int eventType = event.getEventType();
4007 switch (eventType) {
4008 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
4009 if (mAttachInfo != null) {
4010 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4011 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4012 FOCUSABLES_ALL);
4013 event.setItemCount(focusablesTempList.size());
4014 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4015 focusablesTempList.clear();
4016 }
4017 } break;
4018 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
4019 event.setScrollX(mScrollX);
4020 event.setScrollY(mScrollY);
4021 event.setItemCount(getHeight());
4022 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07004023 }
4024 }
4025
4026 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004027 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4028 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4029 * This method is responsible for obtaining an accessibility node info from a
4030 * pool of reusable instances and calling
4031 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4032 * initialize the former.
4033 * <p>
4034 * Note: The client is responsible for recycling the obtained instance by calling
4035 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4036 * </p>
4037 * @return A populated {@link AccessibilityNodeInfo}.
4038 *
4039 * @see AccessibilityNodeInfo
4040 */
4041 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4042 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4043 onInitializeAccessibilityNodeInfo(info);
4044 return info;
4045 }
4046
4047 /**
4048 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4049 * The base implementation sets:
4050 * <ul>
4051 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004052 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4053 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004054 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4055 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4056 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4057 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4058 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4059 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4060 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4061 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4062 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4063 * </ul>
4064 * <p>
4065 * Subclasses should override this method, call the super implementation,
4066 * and set additional attributes.
4067 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004068 * <p>
4069 * If an {@link AccessibilityDelegate} has been specified via calling
4070 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4071 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4072 * is responsible for handling this call.
4073 * </p>
4074 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004075 * @param info The instance to initialize.
4076 */
4077 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004078 if (mAccessibilityDelegate != null) {
4079 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4080 } else {
4081 onInitializeAccessibilityNodeInfoInternal(info);
4082 }
4083 }
4084
4085 /**
4086 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4087 *
4088 * Note: Called from the default {@link AccessibilityDelegate}.
4089 */
4090 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004091 Rect bounds = mAttachInfo.mTmpInvalRect;
4092 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004093 info.setBoundsInParent(bounds);
4094
4095 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4096 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004097 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004098 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4099 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004100
4101 ViewParent parent = getParent();
4102 if (parent instanceof View) {
4103 View parentView = (View) parent;
4104 info.setParent(parentView);
4105 }
4106
4107 info.setPackageName(mContext.getPackageName());
4108 info.setClassName(getClass().getName());
4109 info.setContentDescription(getContentDescription());
4110
4111 info.setEnabled(isEnabled());
4112 info.setClickable(isClickable());
4113 info.setFocusable(isFocusable());
4114 info.setFocused(isFocused());
4115 info.setSelected(isSelected());
4116 info.setLongClickable(isLongClickable());
4117
4118 // TODO: These make sense only if we are in an AdapterView but all
4119 // views can be selected. Maybe from accessiiblity perspective
4120 // we should report as selectable view in an AdapterView.
4121 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4122 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4123
4124 if (isFocusable()) {
4125 if (isFocused()) {
4126 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4127 } else {
4128 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4129 }
4130 }
4131 }
4132
4133 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004134 * Sets a delegate for implementing accessibility support via compositon as
4135 * opposed to inheritance. The delegate's primary use is for implementing
4136 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4137 *
4138 * @param delegate The delegate instance.
4139 *
4140 * @see AccessibilityDelegate
4141 */
4142 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4143 mAccessibilityDelegate = delegate;
4144 }
4145
4146 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004147 * Gets the unique identifier of this view on the screen for accessibility purposes.
4148 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4149 *
4150 * @return The view accessibility id.
4151 *
4152 * @hide
4153 */
4154 public int getAccessibilityViewId() {
4155 if (mAccessibilityViewId == NO_ID) {
4156 mAccessibilityViewId = sNextAccessibilityViewId++;
4157 }
4158 return mAccessibilityViewId;
4159 }
4160
4161 /**
4162 * Gets the unique identifier of the window in which this View reseides.
4163 *
4164 * @return The window accessibility id.
4165 *
4166 * @hide
4167 */
4168 public int getAccessibilityWindowId() {
4169 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4170 }
4171
4172 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004173 * Gets the {@link View} description. It briefly describes the view and is
4174 * primarily used for accessibility support. Set this property to enable
4175 * better accessibility support for your application. This is especially
4176 * true for views that do not have textual representation (For example,
4177 * ImageButton).
4178 *
4179 * @return The content descriptiopn.
4180 *
4181 * @attr ref android.R.styleable#View_contentDescription
4182 */
4183 public CharSequence getContentDescription() {
4184 return mContentDescription;
4185 }
4186
4187 /**
4188 * Sets the {@link View} description. It briefly describes the view and is
4189 * primarily used for accessibility support. Set this property to enable
4190 * better accessibility support for your application. This is especially
4191 * true for views that do not have textual representation (For example,
4192 * ImageButton).
4193 *
4194 * @param contentDescription The content description.
4195 *
4196 * @attr ref android.R.styleable#View_contentDescription
4197 */
4198 public void setContentDescription(CharSequence contentDescription) {
4199 mContentDescription = contentDescription;
4200 }
4201
4202 /**
Romain Guya2431d02009-04-30 16:30:00 -07004203 * Invoked whenever this view loses focus, either by losing window focus or by losing
4204 * focus within its window. This method can be used to clear any state tied to the
4205 * focus. For instance, if a button is held pressed with the trackball and the window
4206 * loses focus, this method can be used to cancel the press.
4207 *
4208 * Subclasses of View overriding this method should always call super.onFocusLost().
4209 *
4210 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004211 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004212 *
4213 * @hide pending API council approval
4214 */
4215 protected void onFocusLost() {
4216 resetPressedState();
4217 }
4218
4219 private void resetPressedState() {
4220 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4221 return;
4222 }
4223
4224 if (isPressed()) {
4225 setPressed(false);
4226
4227 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004228 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004229 }
4230 }
4231 }
4232
4233 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 * Returns true if this view has focus
4235 *
4236 * @return True if this view has focus, false otherwise.
4237 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004238 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 public boolean isFocused() {
4240 return (mPrivateFlags & FOCUSED) != 0;
4241 }
4242
4243 /**
4244 * Find the view in the hierarchy rooted at this view that currently has
4245 * focus.
4246 *
4247 * @return The view that currently has focus, or null if no focused view can
4248 * be found.
4249 */
4250 public View findFocus() {
4251 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4252 }
4253
4254 /**
4255 * Change whether this view is one of the set of scrollable containers in
4256 * its window. This will be used to determine whether the window can
4257 * resize or must pan when a soft input area is open -- scrollable
4258 * containers allow the window to use resize mode since the container
4259 * will appropriately shrink.
4260 */
4261 public void setScrollContainer(boolean isScrollContainer) {
4262 if (isScrollContainer) {
4263 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4264 mAttachInfo.mScrollContainers.add(this);
4265 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4266 }
4267 mPrivateFlags |= SCROLL_CONTAINER;
4268 } else {
4269 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4270 mAttachInfo.mScrollContainers.remove(this);
4271 }
4272 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4273 }
4274 }
4275
4276 /**
4277 * Returns the quality of the drawing cache.
4278 *
4279 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4280 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4281 *
4282 * @see #setDrawingCacheQuality(int)
4283 * @see #setDrawingCacheEnabled(boolean)
4284 * @see #isDrawingCacheEnabled()
4285 *
4286 * @attr ref android.R.styleable#View_drawingCacheQuality
4287 */
4288 public int getDrawingCacheQuality() {
4289 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4290 }
4291
4292 /**
4293 * Set the drawing cache quality of this view. This value is used only when the
4294 * drawing cache is enabled
4295 *
4296 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4297 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4298 *
4299 * @see #getDrawingCacheQuality()
4300 * @see #setDrawingCacheEnabled(boolean)
4301 * @see #isDrawingCacheEnabled()
4302 *
4303 * @attr ref android.R.styleable#View_drawingCacheQuality
4304 */
4305 public void setDrawingCacheQuality(int quality) {
4306 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4307 }
4308
4309 /**
4310 * Returns whether the screen should remain on, corresponding to the current
4311 * value of {@link #KEEP_SCREEN_ON}.
4312 *
4313 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4314 *
4315 * @see #setKeepScreenOn(boolean)
4316 *
4317 * @attr ref android.R.styleable#View_keepScreenOn
4318 */
4319 public boolean getKeepScreenOn() {
4320 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4321 }
4322
4323 /**
4324 * Controls whether the screen should remain on, modifying the
4325 * value of {@link #KEEP_SCREEN_ON}.
4326 *
4327 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4328 *
4329 * @see #getKeepScreenOn()
4330 *
4331 * @attr ref android.R.styleable#View_keepScreenOn
4332 */
4333 public void setKeepScreenOn(boolean keepScreenOn) {
4334 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4335 }
4336
4337 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004338 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4339 * @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 -08004340 *
4341 * @attr ref android.R.styleable#View_nextFocusLeft
4342 */
4343 public int getNextFocusLeftId() {
4344 return mNextFocusLeftId;
4345 }
4346
4347 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004348 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4349 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4350 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 *
4352 * @attr ref android.R.styleable#View_nextFocusLeft
4353 */
4354 public void setNextFocusLeftId(int nextFocusLeftId) {
4355 mNextFocusLeftId = nextFocusLeftId;
4356 }
4357
4358 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004359 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4360 * @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 -08004361 *
4362 * @attr ref android.R.styleable#View_nextFocusRight
4363 */
4364 public int getNextFocusRightId() {
4365 return mNextFocusRightId;
4366 }
4367
4368 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004369 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4370 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4371 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 *
4373 * @attr ref android.R.styleable#View_nextFocusRight
4374 */
4375 public void setNextFocusRightId(int nextFocusRightId) {
4376 mNextFocusRightId = nextFocusRightId;
4377 }
4378
4379 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004380 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4381 * @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 -08004382 *
4383 * @attr ref android.R.styleable#View_nextFocusUp
4384 */
4385 public int getNextFocusUpId() {
4386 return mNextFocusUpId;
4387 }
4388
4389 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004390 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4391 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4392 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 *
4394 * @attr ref android.R.styleable#View_nextFocusUp
4395 */
4396 public void setNextFocusUpId(int nextFocusUpId) {
4397 mNextFocusUpId = nextFocusUpId;
4398 }
4399
4400 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004401 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4402 * @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 -08004403 *
4404 * @attr ref android.R.styleable#View_nextFocusDown
4405 */
4406 public int getNextFocusDownId() {
4407 return mNextFocusDownId;
4408 }
4409
4410 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004411 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4412 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4413 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 *
4415 * @attr ref android.R.styleable#View_nextFocusDown
4416 */
4417 public void setNextFocusDownId(int nextFocusDownId) {
4418 mNextFocusDownId = nextFocusDownId;
4419 }
4420
4421 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004422 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4423 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4424 *
4425 * @attr ref android.R.styleable#View_nextFocusForward
4426 */
4427 public int getNextFocusForwardId() {
4428 return mNextFocusForwardId;
4429 }
4430
4431 /**
4432 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4433 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4434 * decide automatically.
4435 *
4436 * @attr ref android.R.styleable#View_nextFocusForward
4437 */
4438 public void setNextFocusForwardId(int nextFocusForwardId) {
4439 mNextFocusForwardId = nextFocusForwardId;
4440 }
4441
4442 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 * Returns the visibility of this view and all of its ancestors
4444 *
4445 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4446 */
4447 public boolean isShown() {
4448 View current = this;
4449 //noinspection ConstantConditions
4450 do {
4451 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4452 return false;
4453 }
4454 ViewParent parent = current.mParent;
4455 if (parent == null) {
4456 return false; // We are not attached to the view root
4457 }
4458 if (!(parent instanceof View)) {
4459 return true;
4460 }
4461 current = (View) parent;
4462 } while (current != null);
4463
4464 return false;
4465 }
4466
4467 /**
4468 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4469 * is set
4470 *
4471 * @param insets Insets for system windows
4472 *
4473 * @return True if this view applied the insets, false otherwise
4474 */
4475 protected boolean fitSystemWindows(Rect insets) {
4476 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4477 mPaddingLeft = insets.left;
4478 mPaddingTop = insets.top;
4479 mPaddingRight = insets.right;
4480 mPaddingBottom = insets.bottom;
4481 requestLayout();
4482 return true;
4483 }
4484 return false;
4485 }
4486
4487 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004488 * Set whether or not this view should account for system screen decorations
4489 * such as the status bar and inset its content. This allows this view to be
4490 * positioned in absolute screen coordinates and remain visible to the user.
4491 *
4492 * <p>This should only be used by top-level window decor views.
4493 *
4494 * @param fitSystemWindows true to inset content for system screen decorations, false for
4495 * default behavior.
4496 *
4497 * @attr ref android.R.styleable#View_fitsSystemWindows
4498 */
4499 public void setFitsSystemWindows(boolean fitSystemWindows) {
4500 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4501 }
4502
4503 /**
4504 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4505 * will account for system screen decorations such as the status bar and inset its
4506 * content. This allows the view to be positioned in absolute screen coordinates
4507 * and remain visible to the user.
4508 *
4509 * @return true if this view will adjust its content bounds for system screen decorations.
4510 *
4511 * @attr ref android.R.styleable#View_fitsSystemWindows
4512 */
4513 public boolean fitsSystemWindows() {
4514 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4515 }
4516
4517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 * Returns the visibility status for this view.
4519 *
4520 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4521 * @attr ref android.R.styleable#View_visibility
4522 */
4523 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004524 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4525 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4526 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 })
4528 public int getVisibility() {
4529 return mViewFlags & VISIBILITY_MASK;
4530 }
4531
4532 /**
4533 * Set the enabled state of this view.
4534 *
4535 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4536 * @attr ref android.R.styleable#View_visibility
4537 */
4538 @RemotableViewMethod
4539 public void setVisibility(int visibility) {
4540 setFlags(visibility, VISIBILITY_MASK);
4541 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4542 }
4543
4544 /**
4545 * Returns the enabled status for this view. The interpretation of the
4546 * enabled state varies by subclass.
4547 *
4548 * @return True if this view is enabled, false otherwise.
4549 */
4550 @ViewDebug.ExportedProperty
4551 public boolean isEnabled() {
4552 return (mViewFlags & ENABLED_MASK) == ENABLED;
4553 }
4554
4555 /**
4556 * Set the enabled state of this view. The interpretation of the enabled
4557 * state varies by subclass.
4558 *
4559 * @param enabled True if this view is enabled, false otherwise.
4560 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004561 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004563 if (enabled == isEnabled()) return;
4564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4566
4567 /*
4568 * The View most likely has to change its appearance, so refresh
4569 * the drawable state.
4570 */
4571 refreshDrawableState();
4572
4573 // Invalidate too, since the default behavior for views is to be
4574 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004575 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 }
4577
4578 /**
4579 * Set whether this view can receive the focus.
4580 *
4581 * Setting this to false will also ensure that this view is not focusable
4582 * in touch mode.
4583 *
4584 * @param focusable If true, this view can receive the focus.
4585 *
4586 * @see #setFocusableInTouchMode(boolean)
4587 * @attr ref android.R.styleable#View_focusable
4588 */
4589 public void setFocusable(boolean focusable) {
4590 if (!focusable) {
4591 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4592 }
4593 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4594 }
4595
4596 /**
4597 * Set whether this view can receive focus while in touch mode.
4598 *
4599 * Setting this to true will also ensure that this view is focusable.
4600 *
4601 * @param focusableInTouchMode If true, this view can receive the focus while
4602 * in touch mode.
4603 *
4604 * @see #setFocusable(boolean)
4605 * @attr ref android.R.styleable#View_focusableInTouchMode
4606 */
4607 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4608 // Focusable in touch mode should always be set before the focusable flag
4609 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4610 // which, in touch mode, will not successfully request focus on this view
4611 // because the focusable in touch mode flag is not set
4612 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4613 if (focusableInTouchMode) {
4614 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4615 }
4616 }
4617
4618 /**
4619 * Set whether this view should have sound effects enabled for events such as
4620 * clicking and touching.
4621 *
4622 * <p>You may wish to disable sound effects for a view if you already play sounds,
4623 * for instance, a dial key that plays dtmf tones.
4624 *
4625 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4626 * @see #isSoundEffectsEnabled()
4627 * @see #playSoundEffect(int)
4628 * @attr ref android.R.styleable#View_soundEffectsEnabled
4629 */
4630 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4631 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4632 }
4633
4634 /**
4635 * @return whether this view should have sound effects enabled for events such as
4636 * clicking and touching.
4637 *
4638 * @see #setSoundEffectsEnabled(boolean)
4639 * @see #playSoundEffect(int)
4640 * @attr ref android.R.styleable#View_soundEffectsEnabled
4641 */
4642 @ViewDebug.ExportedProperty
4643 public boolean isSoundEffectsEnabled() {
4644 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4645 }
4646
4647 /**
4648 * Set whether this view should have haptic feedback for events such as
4649 * long presses.
4650 *
4651 * <p>You may wish to disable haptic feedback if your view already controls
4652 * its own haptic feedback.
4653 *
4654 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4655 * @see #isHapticFeedbackEnabled()
4656 * @see #performHapticFeedback(int)
4657 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4658 */
4659 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4660 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4661 }
4662
4663 /**
4664 * @return whether this view should have haptic feedback enabled for events
4665 * long presses.
4666 *
4667 * @see #setHapticFeedbackEnabled(boolean)
4668 * @see #performHapticFeedback(int)
4669 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4670 */
4671 @ViewDebug.ExportedProperty
4672 public boolean isHapticFeedbackEnabled() {
4673 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4674 }
4675
4676 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004677 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004678 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004679 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4680 * {@link #LAYOUT_DIRECTION_RTL},
4681 * {@link #LAYOUT_DIRECTION_INHERIT} or
4682 * {@link #LAYOUT_DIRECTION_LOCALE}.
4683 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004684 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004685 * @hide
4686 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004687 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004688 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4689 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4690 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4691 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004692 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004693 public int getLayoutDirection() {
4694 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004695 }
4696
4697 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004698 * Set the layout direction for this view. This will propagate a reset of layout direction
4699 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004700 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004701 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4702 * {@link #LAYOUT_DIRECTION_RTL},
4703 * {@link #LAYOUT_DIRECTION_INHERIT} or
4704 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004705 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004706 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004707 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004708 * @hide
4709 */
4710 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004711 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004712 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004713 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004714 // Setting the flag will also request a layout.
4715 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4716 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004717 }
4718
4719 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004720 * Returns the resolved layout direction for this view.
4721 *
4722 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4723 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4724 *
4725 * @hide
4726 */
4727 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4728 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4729 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4730 })
4731 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004732 resolveLayoutDirectionIfNeeded();
4733 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004734 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4735 }
4736
4737 /**
4738 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4739 * layout attribute and/or the inherited value from the parent.</p>
4740 *
4741 * @return true if the layout is right-to-left.
4742 *
4743 * @hide
4744 */
4745 @ViewDebug.ExportedProperty(category = "layout")
4746 public boolean isLayoutRtl() {
4747 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4748 }
4749
4750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 * If this view doesn't do any drawing on its own, set this flag to
4752 * allow further optimizations. By default, this flag is not set on
4753 * View, but could be set on some View subclasses such as ViewGroup.
4754 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004755 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4756 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 *
4758 * @param willNotDraw whether or not this View draw on its own
4759 */
4760 public void setWillNotDraw(boolean willNotDraw) {
4761 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4762 }
4763
4764 /**
4765 * Returns whether or not this View draws on its own.
4766 *
4767 * @return true if this view has nothing to draw, false otherwise
4768 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004769 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 public boolean willNotDraw() {
4771 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4772 }
4773
4774 /**
4775 * When a View's drawing cache is enabled, drawing is redirected to an
4776 * offscreen bitmap. Some views, like an ImageView, must be able to
4777 * bypass this mechanism if they already draw a single bitmap, to avoid
4778 * unnecessary usage of the memory.
4779 *
4780 * @param willNotCacheDrawing true if this view does not cache its
4781 * drawing, false otherwise
4782 */
4783 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4784 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4785 }
4786
4787 /**
4788 * Returns whether or not this View can cache its drawing or not.
4789 *
4790 * @return true if this view does not cache its drawing, false otherwise
4791 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004792 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 public boolean willNotCacheDrawing() {
4794 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4795 }
4796
4797 /**
4798 * Indicates whether this view reacts to click events or not.
4799 *
4800 * @return true if the view is clickable, false otherwise
4801 *
4802 * @see #setClickable(boolean)
4803 * @attr ref android.R.styleable#View_clickable
4804 */
4805 @ViewDebug.ExportedProperty
4806 public boolean isClickable() {
4807 return (mViewFlags & CLICKABLE) == CLICKABLE;
4808 }
4809
4810 /**
4811 * Enables or disables click events for this view. When a view
4812 * is clickable it will change its state to "pressed" on every click.
4813 * Subclasses should set the view clickable to visually react to
4814 * user's clicks.
4815 *
4816 * @param clickable true to make the view clickable, false otherwise
4817 *
4818 * @see #isClickable()
4819 * @attr ref android.R.styleable#View_clickable
4820 */
4821 public void setClickable(boolean clickable) {
4822 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4823 }
4824
4825 /**
4826 * Indicates whether this view reacts to long click events or not.
4827 *
4828 * @return true if the view is long clickable, false otherwise
4829 *
4830 * @see #setLongClickable(boolean)
4831 * @attr ref android.R.styleable#View_longClickable
4832 */
4833 public boolean isLongClickable() {
4834 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4835 }
4836
4837 /**
4838 * Enables or disables long click events for this view. When a view is long
4839 * clickable it reacts to the user holding down the button for a longer
4840 * duration than a tap. This event can either launch the listener or a
4841 * context menu.
4842 *
4843 * @param longClickable true to make the view long clickable, false otherwise
4844 * @see #isLongClickable()
4845 * @attr ref android.R.styleable#View_longClickable
4846 */
4847 public void setLongClickable(boolean longClickable) {
4848 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4849 }
4850
4851 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004852 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 *
4854 * @see #isClickable()
4855 * @see #setClickable(boolean)
4856 *
4857 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4858 * the View's internal state from a previously set "pressed" state.
4859 */
4860 public void setPressed(boolean pressed) {
4861 if (pressed) {
4862 mPrivateFlags |= PRESSED;
4863 } else {
4864 mPrivateFlags &= ~PRESSED;
4865 }
4866 refreshDrawableState();
4867 dispatchSetPressed(pressed);
4868 }
4869
4870 /**
4871 * Dispatch setPressed to all of this View's children.
4872 *
4873 * @see #setPressed(boolean)
4874 *
4875 * @param pressed The new pressed state
4876 */
4877 protected void dispatchSetPressed(boolean pressed) {
4878 }
4879
4880 /**
4881 * Indicates whether the view is currently in pressed state. Unless
4882 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4883 * the pressed state.
4884 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004885 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 * @see #isClickable()
4887 * @see #setClickable(boolean)
4888 *
4889 * @return true if the view is currently pressed, false otherwise
4890 */
4891 public boolean isPressed() {
4892 return (mPrivateFlags & PRESSED) == PRESSED;
4893 }
4894
4895 /**
4896 * Indicates whether this view will save its state (that is,
4897 * whether its {@link #onSaveInstanceState} method will be called).
4898 *
4899 * @return Returns true if the view state saving is enabled, else false.
4900 *
4901 * @see #setSaveEnabled(boolean)
4902 * @attr ref android.R.styleable#View_saveEnabled
4903 */
4904 public boolean isSaveEnabled() {
4905 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4906 }
4907
4908 /**
4909 * Controls whether the saving of this view's state is
4910 * enabled (that is, whether its {@link #onSaveInstanceState} method
4911 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004912 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 * for its state to be saved. This flag can only disable the
4914 * saving of this view; any child views may still have their state saved.
4915 *
4916 * @param enabled Set to false to <em>disable</em> state saving, or true
4917 * (the default) to allow it.
4918 *
4919 * @see #isSaveEnabled()
4920 * @see #setId(int)
4921 * @see #onSaveInstanceState()
4922 * @attr ref android.R.styleable#View_saveEnabled
4923 */
4924 public void setSaveEnabled(boolean enabled) {
4925 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4926 }
4927
Jeff Brown85a31762010-09-01 17:01:00 -07004928 /**
4929 * Gets whether the framework should discard touches when the view's
4930 * window is obscured by another visible window.
4931 * Refer to the {@link View} security documentation for more details.
4932 *
4933 * @return True if touch filtering is enabled.
4934 *
4935 * @see #setFilterTouchesWhenObscured(boolean)
4936 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4937 */
4938 @ViewDebug.ExportedProperty
4939 public boolean getFilterTouchesWhenObscured() {
4940 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4941 }
4942
4943 /**
4944 * Sets whether the framework should discard touches when the view's
4945 * window is obscured by another visible window.
4946 * Refer to the {@link View} security documentation for more details.
4947 *
4948 * @param enabled True if touch filtering should be enabled.
4949 *
4950 * @see #getFilterTouchesWhenObscured
4951 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4952 */
4953 public void setFilterTouchesWhenObscured(boolean enabled) {
4954 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4955 FILTER_TOUCHES_WHEN_OBSCURED);
4956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957
4958 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004959 * Indicates whether the entire hierarchy under this view will save its
4960 * state when a state saving traversal occurs from its parent. The default
4961 * is true; if false, these views will not be saved unless
4962 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4963 *
4964 * @return Returns true if the view state saving from parent is enabled, else false.
4965 *
4966 * @see #setSaveFromParentEnabled(boolean)
4967 */
4968 public boolean isSaveFromParentEnabled() {
4969 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4970 }
4971
4972 /**
4973 * Controls whether the entire hierarchy under this view will save its
4974 * state when a state saving traversal occurs from its parent. The default
4975 * is true; if false, these views will not be saved unless
4976 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4977 *
4978 * @param enabled Set to false to <em>disable</em> state saving, or true
4979 * (the default) to allow it.
4980 *
4981 * @see #isSaveFromParentEnabled()
4982 * @see #setId(int)
4983 * @see #onSaveInstanceState()
4984 */
4985 public void setSaveFromParentEnabled(boolean enabled) {
4986 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4987 }
4988
4989
4990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 * Returns whether this View is able to take focus.
4992 *
4993 * @return True if this view can take focus, or false otherwise.
4994 * @attr ref android.R.styleable#View_focusable
4995 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004996 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 public final boolean isFocusable() {
4998 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4999 }
5000
5001 /**
5002 * When a view is focusable, it may not want to take focus when in touch mode.
5003 * For example, a button would like focus when the user is navigating via a D-pad
5004 * so that the user can click on it, but once the user starts touching the screen,
5005 * the button shouldn't take focus
5006 * @return Whether the view is focusable in touch mode.
5007 * @attr ref android.R.styleable#View_focusableInTouchMode
5008 */
5009 @ViewDebug.ExportedProperty
5010 public final boolean isFocusableInTouchMode() {
5011 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5012 }
5013
5014 /**
5015 * Find the nearest view in the specified direction that can take focus.
5016 * This does not actually give focus to that view.
5017 *
5018 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5019 *
5020 * @return The nearest focusable in the specified direction, or null if none
5021 * can be found.
5022 */
5023 public View focusSearch(int direction) {
5024 if (mParent != null) {
5025 return mParent.focusSearch(this, direction);
5026 } else {
5027 return null;
5028 }
5029 }
5030
5031 /**
5032 * This method is the last chance for the focused view and its ancestors to
5033 * respond to an arrow key. This is called when the focused view did not
5034 * consume the key internally, nor could the view system find a new view in
5035 * the requested direction to give focus to.
5036 *
5037 * @param focused The currently focused view.
5038 * @param direction The direction focus wants to move. One of FOCUS_UP,
5039 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5040 * @return True if the this view consumed this unhandled move.
5041 */
5042 public boolean dispatchUnhandledMove(View focused, int direction) {
5043 return false;
5044 }
5045
5046 /**
5047 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005048 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005049 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005050 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5051 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 * @return The user specified next view, or null if there is none.
5053 */
5054 View findUserSetNextFocus(View root, int direction) {
5055 switch (direction) {
5056 case FOCUS_LEFT:
5057 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005058 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 case FOCUS_RIGHT:
5060 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005061 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 case FOCUS_UP:
5063 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005064 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 case FOCUS_DOWN:
5066 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005067 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005068 case FOCUS_FORWARD:
5069 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005070 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005071 case FOCUS_BACKWARD: {
5072 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005073 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005074 @Override
5075 public boolean apply(View t) {
5076 return t.mNextFocusForwardId == id;
5077 }
5078 });
5079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 }
5081 return null;
5082 }
5083
Jeff Brown4dfbec22011-08-15 14:55:37 -07005084 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5085 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5086 @Override
5087 public boolean apply(View t) {
5088 return t.mID == childViewId;
5089 }
5090 });
5091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 if (result == null) {
5093 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5094 + "by user for id " + childViewId);
5095 }
5096 return result;
5097 }
5098
5099 /**
5100 * Find and return all focusable views that are descendants of this view,
5101 * possibly including this view if it is focusable itself.
5102 *
5103 * @param direction The direction of the focus
5104 * @return A list of focusable views
5105 */
5106 public ArrayList<View> getFocusables(int direction) {
5107 ArrayList<View> result = new ArrayList<View>(24);
5108 addFocusables(result, direction);
5109 return result;
5110 }
5111
5112 /**
5113 * Add any focusable views that are descendants of this view (possibly
5114 * including this view if it is focusable itself) to views. If we are in touch mode,
5115 * only add views that are also focusable in touch mode.
5116 *
5117 * @param views Focusable views found so far
5118 * @param direction The direction of the focus
5119 */
5120 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005121 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123
svetoslavganov75986cf2009-05-14 22:28:01 -07005124 /**
5125 * Adds any focusable views that are descendants of this view (possibly
5126 * including this view if it is focusable itself) to views. This method
5127 * adds all focusable views regardless if we are in touch mode or
5128 * only views focusable in touch mode if we are in touch mode depending on
5129 * the focusable mode paramater.
5130 *
5131 * @param views Focusable views found so far or null if all we are interested is
5132 * the number of focusables.
5133 * @param direction The direction of the focus.
5134 * @param focusableMode The type of focusables to be added.
5135 *
5136 * @see #FOCUSABLES_ALL
5137 * @see #FOCUSABLES_TOUCH_MODE
5138 */
5139 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5140 if (!isFocusable()) {
5141 return;
5142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143
svetoslavganov75986cf2009-05-14 22:28:01 -07005144 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5145 isInTouchMode() && !isFocusableInTouchMode()) {
5146 return;
5147 }
5148
5149 if (views != null) {
5150 views.add(this);
5151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 }
5153
5154 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005155 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005156 * The search is performed by either the text that the View renders or the content
5157 * description that describes the view for accessibility purposes and the view does
5158 * not render or both. Clients can specify how the search is to be performed via
5159 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5160 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005161 *
5162 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005163 * @param searched The text to match against.
5164 *
5165 * @see #FIND_VIEWS_WITH_TEXT
5166 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5167 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005168 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005169 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5170 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5171 && !TextUtils.isEmpty(mContentDescription)) {
5172 String searchedLowerCase = searched.toString().toLowerCase();
5173 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5174 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5175 outViews.add(this);
5176 }
5177 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005178 }
5179
5180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 * Find and return all touchable views that are descendants of this view,
5182 * possibly including this view if it is touchable itself.
5183 *
5184 * @return A list of touchable views
5185 */
5186 public ArrayList<View> getTouchables() {
5187 ArrayList<View> result = new ArrayList<View>();
5188 addTouchables(result);
5189 return result;
5190 }
5191
5192 /**
5193 * Add any touchable views that are descendants of this view (possibly
5194 * including this view if it is touchable itself) to views.
5195 *
5196 * @param views Touchable views found so far
5197 */
5198 public void addTouchables(ArrayList<View> views) {
5199 final int viewFlags = mViewFlags;
5200
5201 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5202 && (viewFlags & ENABLED_MASK) == ENABLED) {
5203 views.add(this);
5204 }
5205 }
5206
5207 /**
5208 * Call this to try to give focus to a specific view or to one of its
5209 * descendants.
5210 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005211 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5212 * false), or if it is focusable and it is not focusable in touch mode
5213 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005215 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 * have focus, and you want your parent to look for the next one.
5217 *
5218 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5219 * {@link #FOCUS_DOWN} and <code>null</code>.
5220 *
5221 * @return Whether this view or one of its descendants actually took focus.
5222 */
5223 public final boolean requestFocus() {
5224 return requestFocus(View.FOCUS_DOWN);
5225 }
5226
5227
5228 /**
5229 * Call this to try to give focus to a specific view or to one of its
5230 * descendants and give it a hint about what direction focus is heading.
5231 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005232 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5233 * false), or if it is focusable and it is not focusable in touch mode
5234 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005236 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 * have focus, and you want your parent to look for the next one.
5238 *
5239 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5240 * <code>null</code> set for the previously focused rectangle.
5241 *
5242 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5243 * @return Whether this view or one of its descendants actually took focus.
5244 */
5245 public final boolean requestFocus(int direction) {
5246 return requestFocus(direction, null);
5247 }
5248
5249 /**
5250 * Call this to try to give focus to a specific view or to one of its descendants
5251 * and give it hints about the direction and a specific rectangle that the focus
5252 * is coming from. The rectangle can help give larger views a finer grained hint
5253 * about where focus is coming from, and therefore, where to show selection, or
5254 * forward focus change internally.
5255 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005256 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5257 * false), or if it is focusable and it is not focusable in touch mode
5258 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 *
5260 * A View will not take focus if it is not visible.
5261 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005262 * A View will not take focus if one of its parents has
5263 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5264 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005265 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005266 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 * have focus, and you want your parent to look for the next one.
5268 *
5269 * You may wish to override this method if your custom {@link View} has an internal
5270 * {@link View} that it wishes to forward the request to.
5271 *
5272 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5273 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5274 * to give a finer grained hint about where focus is coming from. May be null
5275 * if there is no hint.
5276 * @return Whether this view or one of its descendants actually took focus.
5277 */
5278 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5279 // need to be focusable
5280 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5281 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5282 return false;
5283 }
5284
5285 // need to be focusable in touch mode if in touch mode
5286 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005287 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5288 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 }
5290
5291 // need to not have any parents blocking us
5292 if (hasAncestorThatBlocksDescendantFocus()) {
5293 return false;
5294 }
5295
5296 handleFocusGainInternal(direction, previouslyFocusedRect);
5297 return true;
5298 }
5299
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005300 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005301 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005302 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005303 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005304 }
5305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 /**
5307 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5308 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5309 * touch mode to request focus when they are touched.
5310 *
5311 * @return Whether this view or one of its descendants actually took focus.
5312 *
5313 * @see #isInTouchMode()
5314 *
5315 */
5316 public final boolean requestFocusFromTouch() {
5317 // Leave touch mode if we need to
5318 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005319 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005320 if (viewRoot != null) {
5321 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 }
5323 }
5324 return requestFocus(View.FOCUS_DOWN);
5325 }
5326
5327 /**
5328 * @return Whether any ancestor of this view blocks descendant focus.
5329 */
5330 private boolean hasAncestorThatBlocksDescendantFocus() {
5331 ViewParent ancestor = mParent;
5332 while (ancestor instanceof ViewGroup) {
5333 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5334 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5335 return true;
5336 } else {
5337 ancestor = vgAncestor.getParent();
5338 }
5339 }
5340 return false;
5341 }
5342
5343 /**
Romain Guya440b002010-02-24 15:57:54 -08005344 * @hide
5345 */
5346 public void dispatchStartTemporaryDetach() {
5347 onStartTemporaryDetach();
5348 }
5349
5350 /**
5351 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5353 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005354 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 */
5356 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005357 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005358 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005359 }
5360
5361 /**
5362 * @hide
5363 */
5364 public void dispatchFinishTemporaryDetach() {
5365 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 }
Romain Guy8506ab42009-06-11 17:35:47 -07005367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 /**
5369 * Called after {@link #onStartTemporaryDetach} when the container is done
5370 * changing the view.
5371 */
5372 public void onFinishTemporaryDetach() {
5373 }
Romain Guy8506ab42009-06-11 17:35:47 -07005374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005376 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5377 * for this view's window. Returns null if the view is not currently attached
5378 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005379 * just use the standard high-level event callbacks like
5380 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005381 */
5382 public KeyEvent.DispatcherState getKeyDispatcherState() {
5383 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5384 }
Joe Malin32736f02011-01-19 16:14:20 -08005385
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 * Dispatch a key event before it is processed by any input method
5388 * associated with the view hierarchy. This can be used to intercept
5389 * key events in special situations before the IME consumes them; a
5390 * typical example would be handling the BACK key to update the application's
5391 * UI instead of allowing the IME to see it and close itself.
5392 *
5393 * @param event The key event to be dispatched.
5394 * @return True if the event was handled, false otherwise.
5395 */
5396 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5397 return onKeyPreIme(event.getKeyCode(), event);
5398 }
5399
5400 /**
5401 * Dispatch a key event to the next view on the focus path. This path runs
5402 * from the top of the view tree down to the currently focused view. If this
5403 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5404 * the next node down the focus path. This method also fires any key
5405 * listeners.
5406 *
5407 * @param event The key event to be dispatched.
5408 * @return True if the event was handled, false otherwise.
5409 */
5410 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005411 if (mInputEventConsistencyVerifier != null) {
5412 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414
Jeff Brown21bc5c92011-02-28 18:27:14 -08005415 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005416 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5418 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5419 return true;
5420 }
5421
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005422 if (event.dispatch(this, mAttachInfo != null
5423 ? mAttachInfo.mKeyDispatchState : null, this)) {
5424 return true;
5425 }
5426
5427 if (mInputEventConsistencyVerifier != null) {
5428 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5429 }
5430 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
5432
5433 /**
5434 * Dispatches a key shortcut event.
5435 *
5436 * @param event The key event to be dispatched.
5437 * @return True if the event was handled by the view, false otherwise.
5438 */
5439 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5440 return onKeyShortcut(event.getKeyCode(), event);
5441 }
5442
5443 /**
5444 * Pass the touch screen motion event down to the target view, or this
5445 * view if it is the target.
5446 *
5447 * @param event The motion event to be dispatched.
5448 * @return True if the event was handled by the view, false otherwise.
5449 */
5450 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005451 if (mInputEventConsistencyVerifier != null) {
5452 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5453 }
5454
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005455 if (onFilterTouchEventForSecurity(event)) {
5456 //noinspection SimplifiableIfStatement
5457 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5458 mOnTouchListener.onTouch(this, event)) {
5459 return true;
5460 }
5461
5462 if (onTouchEvent(event)) {
5463 return true;
5464 }
Jeff Brown85a31762010-09-01 17:01:00 -07005465 }
5466
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005467 if (mInputEventConsistencyVerifier != null) {
5468 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005470 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 }
5472
5473 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005474 * Filter the touch event to apply security policies.
5475 *
5476 * @param event The motion event to be filtered.
5477 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005478 *
Jeff Brown85a31762010-09-01 17:01:00 -07005479 * @see #getFilterTouchesWhenObscured
5480 */
5481 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005482 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005483 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5484 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5485 // Window is obscured, drop this touch.
5486 return false;
5487 }
5488 return true;
5489 }
5490
5491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 * Pass a trackball motion event down to the focused view.
5493 *
5494 * @param event The motion event to be dispatched.
5495 * @return True if the event was handled by the view, false otherwise.
5496 */
5497 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005498 if (mInputEventConsistencyVerifier != null) {
5499 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5500 }
5501
Romain Guy02ccac62011-06-24 13:20:23 -07005502 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 }
5504
5505 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005506 * Dispatch a generic motion event.
5507 * <p>
5508 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5509 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005510 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005511 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005512 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005513 *
5514 * @param event The motion event to be dispatched.
5515 * @return True if the event was handled by the view, false otherwise.
5516 */
5517 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005518 if (mInputEventConsistencyVerifier != null) {
5519 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5520 }
5521
Jeff Browna032cc02011-03-07 16:56:21 -08005522 final int source = event.getSource();
5523 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5524 final int action = event.getAction();
5525 if (action == MotionEvent.ACTION_HOVER_ENTER
5526 || action == MotionEvent.ACTION_HOVER_MOVE
5527 || action == MotionEvent.ACTION_HOVER_EXIT) {
5528 if (dispatchHoverEvent(event)) {
5529 return true;
5530 }
5531 } else if (dispatchGenericPointerEvent(event)) {
5532 return true;
5533 }
5534 } else if (dispatchGenericFocusedEvent(event)) {
5535 return true;
5536 }
5537
Jeff Brown10b62902011-06-20 16:40:37 -07005538 if (dispatchGenericMotionEventInternal(event)) {
5539 return true;
5540 }
5541
5542 if (mInputEventConsistencyVerifier != null) {
5543 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5544 }
5545 return false;
5546 }
5547
5548 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005549 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005550 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5551 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5552 return true;
5553 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005554
5555 if (onGenericMotionEvent(event)) {
5556 return true;
5557 }
5558
5559 if (mInputEventConsistencyVerifier != null) {
5560 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5561 }
5562 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005563 }
5564
5565 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005566 * Dispatch a hover event.
5567 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005568 * Do not call this method directly.
5569 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005570 * </p>
5571 *
5572 * @param event The motion event to be dispatched.
5573 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005574 */
5575 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005576 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005577 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5578 && mOnHoverListener.onHover(this, event)) {
5579 return true;
5580 }
5581
Jeff Browna032cc02011-03-07 16:56:21 -08005582 return onHoverEvent(event);
5583 }
5584
5585 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005586 * Returns true if the view has a child to which it has recently sent
5587 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5588 * it does not have a hovered child, then it must be the innermost hovered view.
5589 * @hide
5590 */
5591 protected boolean hasHoveredChild() {
5592 return false;
5593 }
5594
5595 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005596 * Dispatch a generic motion event to the view under the first pointer.
5597 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005598 * Do not call this method directly.
5599 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005600 * </p>
5601 *
5602 * @param event The motion event to be dispatched.
5603 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005604 */
5605 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5606 return false;
5607 }
5608
5609 /**
5610 * Dispatch a generic motion event to the currently focused view.
5611 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005612 * Do not call this method directly.
5613 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005614 * </p>
5615 *
5616 * @param event The motion event to be dispatched.
5617 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005618 */
5619 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5620 return false;
5621 }
5622
5623 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005624 * Dispatch a pointer event.
5625 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005626 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5627 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5628 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005629 * and should not be expected to handle other pointing device features.
5630 * </p>
5631 *
5632 * @param event The motion event to be dispatched.
5633 * @return True if the event was handled by the view, false otherwise.
5634 * @hide
5635 */
5636 public final boolean dispatchPointerEvent(MotionEvent event) {
5637 if (event.isTouchEvent()) {
5638 return dispatchTouchEvent(event);
5639 } else {
5640 return dispatchGenericMotionEvent(event);
5641 }
5642 }
5643
5644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 * Called when the window containing this view gains or loses window focus.
5646 * ViewGroups should override to route to their children.
5647 *
5648 * @param hasFocus True if the window containing this view now has focus,
5649 * false otherwise.
5650 */
5651 public void dispatchWindowFocusChanged(boolean hasFocus) {
5652 onWindowFocusChanged(hasFocus);
5653 }
5654
5655 /**
5656 * Called when the window containing this view gains or loses focus. Note
5657 * that this is separate from view focus: to receive key events, both
5658 * your view and its window must have focus. If a window is displayed
5659 * on top of yours that takes input focus, then your own window will lose
5660 * focus but the view focus will remain unchanged.
5661 *
5662 * @param hasWindowFocus True if the window containing this view now has
5663 * focus, false otherwise.
5664 */
5665 public void onWindowFocusChanged(boolean hasWindowFocus) {
5666 InputMethodManager imm = InputMethodManager.peekInstance();
5667 if (!hasWindowFocus) {
5668 if (isPressed()) {
5669 setPressed(false);
5670 }
5671 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5672 imm.focusOut(this);
5673 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005674 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005675 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005676 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5678 imm.focusIn(this);
5679 }
5680 refreshDrawableState();
5681 }
5682
5683 /**
5684 * Returns true if this view is in a window that currently has window focus.
5685 * Note that this is not the same as the view itself having focus.
5686 *
5687 * @return True if this view is in a window that currently has window focus.
5688 */
5689 public boolean hasWindowFocus() {
5690 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5691 }
5692
5693 /**
Adam Powell326d8082009-12-09 15:10:07 -08005694 * Dispatch a view visibility change down the view hierarchy.
5695 * ViewGroups should override to route to their children.
5696 * @param changedView The view whose visibility changed. Could be 'this' or
5697 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005698 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5699 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005700 */
5701 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5702 onVisibilityChanged(changedView, visibility);
5703 }
5704
5705 /**
5706 * Called when the visibility of the view or an ancestor of the view is changed.
5707 * @param changedView The view whose visibility changed. Could be 'this' or
5708 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005709 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5710 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005711 */
5712 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005713 if (visibility == VISIBLE) {
5714 if (mAttachInfo != null) {
5715 initialAwakenScrollBars();
5716 } else {
5717 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5718 }
5719 }
Adam Powell326d8082009-12-09 15:10:07 -08005720 }
5721
5722 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005723 * Dispatch a hint about whether this view is displayed. For instance, when
5724 * a View moves out of the screen, it might receives a display hint indicating
5725 * the view is not displayed. Applications should not <em>rely</em> on this hint
5726 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005727 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005728 * @param hint A hint about whether or not this view is displayed:
5729 * {@link #VISIBLE} or {@link #INVISIBLE}.
5730 */
5731 public void dispatchDisplayHint(int hint) {
5732 onDisplayHint(hint);
5733 }
5734
5735 /**
5736 * Gives this view a hint about whether is displayed or not. For instance, when
5737 * a View moves out of the screen, it might receives a display hint indicating
5738 * the view is not displayed. Applications should not <em>rely</em> on this hint
5739 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005740 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005741 * @param hint A hint about whether or not this view is displayed:
5742 * {@link #VISIBLE} or {@link #INVISIBLE}.
5743 */
5744 protected void onDisplayHint(int hint) {
5745 }
5746
5747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 * Dispatch a window visibility change down the view hierarchy.
5749 * ViewGroups should override to route to their children.
5750 *
5751 * @param visibility The new visibility of the window.
5752 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005753 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 */
5755 public void dispatchWindowVisibilityChanged(int visibility) {
5756 onWindowVisibilityChanged(visibility);
5757 }
5758
5759 /**
5760 * Called when the window containing has change its visibility
5761 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5762 * that this tells you whether or not your window is being made visible
5763 * to the window manager; this does <em>not</em> tell you whether or not
5764 * your window is obscured by other windows on the screen, even if it
5765 * is itself visible.
5766 *
5767 * @param visibility The new visibility of the window.
5768 */
5769 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005770 if (visibility == VISIBLE) {
5771 initialAwakenScrollBars();
5772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 }
5774
5775 /**
5776 * Returns the current visibility of the window this view is attached to
5777 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5778 *
5779 * @return Returns the current visibility of the view's window.
5780 */
5781 public int getWindowVisibility() {
5782 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5783 }
5784
5785 /**
5786 * Retrieve the overall visible display size in which the window this view is
5787 * attached to has been positioned in. This takes into account screen
5788 * decorations above the window, for both cases where the window itself
5789 * is being position inside of them or the window is being placed under
5790 * then and covered insets are used for the window to position its content
5791 * inside. In effect, this tells you the available area where content can
5792 * be placed and remain visible to users.
5793 *
5794 * <p>This function requires an IPC back to the window manager to retrieve
5795 * the requested information, so should not be used in performance critical
5796 * code like drawing.
5797 *
5798 * @param outRect Filled in with the visible display frame. If the view
5799 * is not attached to a window, this is simply the raw display size.
5800 */
5801 public void getWindowVisibleDisplayFrame(Rect outRect) {
5802 if (mAttachInfo != null) {
5803 try {
5804 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5805 } catch (RemoteException e) {
5806 return;
5807 }
5808 // XXX This is really broken, and probably all needs to be done
5809 // in the window manager, and we need to know more about whether
5810 // we want the area behind or in front of the IME.
5811 final Rect insets = mAttachInfo.mVisibleInsets;
5812 outRect.left += insets.left;
5813 outRect.top += insets.top;
5814 outRect.right -= insets.right;
5815 outRect.bottom -= insets.bottom;
5816 return;
5817 }
5818 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005819 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 }
5821
5822 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005823 * Dispatch a notification about a resource configuration change down
5824 * the view hierarchy.
5825 * ViewGroups should override to route to their children.
5826 *
5827 * @param newConfig The new resource configuration.
5828 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005829 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005830 */
5831 public void dispatchConfigurationChanged(Configuration newConfig) {
5832 onConfigurationChanged(newConfig);
5833 }
5834
5835 /**
5836 * Called when the current configuration of the resources being used
5837 * by the application have changed. You can use this to decide when
5838 * to reload resources that can changed based on orientation and other
5839 * configuration characterstics. You only need to use this if you are
5840 * not relying on the normal {@link android.app.Activity} mechanism of
5841 * recreating the activity instance upon a configuration change.
5842 *
5843 * @param newConfig The new resource configuration.
5844 */
5845 protected void onConfigurationChanged(Configuration newConfig) {
5846 }
5847
5848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 * Private function to aggregate all per-view attributes in to the view
5850 * root.
5851 */
5852 void dispatchCollectViewAttributes(int visibility) {
5853 performCollectViewAttributes(visibility);
5854 }
5855
5856 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005857 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005858 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5859 mAttachInfo.mKeepScreenOn = true;
5860 }
5861 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5862 if (mOnSystemUiVisibilityChangeListener != null) {
5863 mAttachInfo.mHasSystemUiListeners = true;
5864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 }
5866 }
5867
5868 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005869 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005871 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5872 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 ai.mRecomputeGlobalAttributes = true;
5874 }
5875 }
5876 }
5877
5878 /**
5879 * Returns whether the device is currently in touch mode. Touch mode is entered
5880 * once the user begins interacting with the device by touch, and affects various
5881 * things like whether focus is always visible to the user.
5882 *
5883 * @return Whether the device is in touch mode.
5884 */
5885 @ViewDebug.ExportedProperty
5886 public boolean isInTouchMode() {
5887 if (mAttachInfo != null) {
5888 return mAttachInfo.mInTouchMode;
5889 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005890 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 }
5892 }
5893
5894 /**
5895 * Returns the context the view is running in, through which it can
5896 * access the current theme, resources, etc.
5897 *
5898 * @return The view's Context.
5899 */
5900 @ViewDebug.CapturedViewProperty
5901 public final Context getContext() {
5902 return mContext;
5903 }
5904
5905 /**
5906 * Handle a key event before it is processed by any input method
5907 * associated with the view hierarchy. This can be used to intercept
5908 * key events in special situations before the IME consumes them; a
5909 * typical example would be handling the BACK key to update the application's
5910 * UI instead of allowing the IME to see it and close itself.
5911 *
5912 * @param keyCode The value in event.getKeyCode().
5913 * @param event Description of the key event.
5914 * @return If you handled the event, return true. If you want to allow the
5915 * event to be handled by the next receiver, return false.
5916 */
5917 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5918 return false;
5919 }
5920
5921 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005922 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5923 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5925 * is released, if the view is enabled and clickable.
5926 *
5927 * @param keyCode A key code that represents the button pressed, from
5928 * {@link android.view.KeyEvent}.
5929 * @param event The KeyEvent object that defines the button action.
5930 */
5931 public boolean onKeyDown(int keyCode, KeyEvent event) {
5932 boolean result = false;
5933
5934 switch (keyCode) {
5935 case KeyEvent.KEYCODE_DPAD_CENTER:
5936 case KeyEvent.KEYCODE_ENTER: {
5937 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5938 return true;
5939 }
5940 // Long clickable items don't necessarily have to be clickable
5941 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5942 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5943 (event.getRepeatCount() == 0)) {
5944 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005945 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 return true;
5947 }
5948 break;
5949 }
5950 }
5951 return result;
5952 }
5953
5954 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005955 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5956 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5957 * the event).
5958 */
5959 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5960 return false;
5961 }
5962
5963 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005964 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5965 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5967 * {@link KeyEvent#KEYCODE_ENTER} is released.
5968 *
5969 * @param keyCode A key code that represents the button pressed, from
5970 * {@link android.view.KeyEvent}.
5971 * @param event The KeyEvent object that defines the button action.
5972 */
5973 public boolean onKeyUp(int keyCode, KeyEvent event) {
5974 boolean result = false;
5975
5976 switch (keyCode) {
5977 case KeyEvent.KEYCODE_DPAD_CENTER:
5978 case KeyEvent.KEYCODE_ENTER: {
5979 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5980 return true;
5981 }
5982 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5983 setPressed(false);
5984
5985 if (!mHasPerformedLongPress) {
5986 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005987 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988
5989 result = performClick();
5990 }
5991 }
5992 break;
5993 }
5994 }
5995 return result;
5996 }
5997
5998 /**
5999 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6000 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6001 * the event).
6002 *
6003 * @param keyCode A key code that represents the button pressed, from
6004 * {@link android.view.KeyEvent}.
6005 * @param repeatCount The number of times the action was made.
6006 * @param event The KeyEvent object that defines the button action.
6007 */
6008 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6009 return false;
6010 }
6011
6012 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006013 * Called on the focused view when a key shortcut event is not handled.
6014 * Override this method to implement local key shortcuts for the View.
6015 * Key shortcuts can also be implemented by setting the
6016 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 *
6018 * @param keyCode The value in event.getKeyCode().
6019 * @param event Description of the key event.
6020 * @return If you handled the event, return true. If you want to allow the
6021 * event to be handled by the next receiver, return false.
6022 */
6023 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6024 return false;
6025 }
6026
6027 /**
6028 * Check whether the called view is a text editor, in which case it
6029 * would make sense to automatically display a soft input window for
6030 * it. Subclasses should override this if they implement
6031 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006032 * a call on that method would return a non-null InputConnection, and
6033 * they are really a first-class editor that the user would normally
6034 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006035 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006036 * <p>The default implementation always returns false. This does
6037 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6038 * will not be called or the user can not otherwise perform edits on your
6039 * view; it is just a hint to the system that this is not the primary
6040 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 * @return Returns true if this view is a text editor, else false.
6043 */
6044 public boolean onCheckIsTextEditor() {
6045 return false;
6046 }
Romain Guy8506ab42009-06-11 17:35:47 -07006047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 /**
6049 * Create a new InputConnection for an InputMethod to interact
6050 * with the view. The default implementation returns null, since it doesn't
6051 * support input methods. You can override this to implement such support.
6052 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006053 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 * <p>When implementing this, you probably also want to implement
6055 * {@link #onCheckIsTextEditor()} to indicate you will return a
6056 * non-null InputConnection.
6057 *
6058 * @param outAttrs Fill in with attribute information about the connection.
6059 */
6060 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6061 return null;
6062 }
6063
6064 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006065 * Called by the {@link android.view.inputmethod.InputMethodManager}
6066 * when a view who is not the current
6067 * input connection target is trying to make a call on the manager. The
6068 * default implementation returns false; you can override this to return
6069 * true for certain views if you are performing InputConnection proxying
6070 * to them.
6071 * @param view The View that is making the InputMethodManager call.
6072 * @return Return true to allow the call, false to reject.
6073 */
6074 public boolean checkInputConnectionProxy(View view) {
6075 return false;
6076 }
Romain Guy8506ab42009-06-11 17:35:47 -07006077
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 * Show the context menu for this view. It is not safe to hold on to the
6080 * menu after returning from this method.
6081 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006082 * You should normally not overload this method. Overload
6083 * {@link #onCreateContextMenu(ContextMenu)} or define an
6084 * {@link OnCreateContextMenuListener} to add items to the context menu.
6085 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 * @param menu The context menu to populate
6087 */
6088 public void createContextMenu(ContextMenu menu) {
6089 ContextMenuInfo menuInfo = getContextMenuInfo();
6090
6091 // Sets the current menu info so all items added to menu will have
6092 // my extra info set.
6093 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6094
6095 onCreateContextMenu(menu);
6096 if (mOnCreateContextMenuListener != null) {
6097 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6098 }
6099
6100 // Clear the extra information so subsequent items that aren't mine don't
6101 // have my extra info.
6102 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6103
6104 if (mParent != null) {
6105 mParent.createContextMenu(menu);
6106 }
6107 }
6108
6109 /**
6110 * Views should implement this if they have extra information to associate
6111 * with the context menu. The return result is supplied as a parameter to
6112 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6113 * callback.
6114 *
6115 * @return Extra information about the item for which the context menu
6116 * should be shown. This information will vary across different
6117 * subclasses of View.
6118 */
6119 protected ContextMenuInfo getContextMenuInfo() {
6120 return null;
6121 }
6122
6123 /**
6124 * Views should implement this if the view itself is going to add items to
6125 * the context menu.
6126 *
6127 * @param menu the context menu to populate
6128 */
6129 protected void onCreateContextMenu(ContextMenu menu) {
6130 }
6131
6132 /**
6133 * Implement this method to handle trackball motion events. The
6134 * <em>relative</em> movement of the trackball since the last event
6135 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6136 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6137 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6138 * they will often be fractional values, representing the more fine-grained
6139 * movement information available from a trackball).
6140 *
6141 * @param event The motion event.
6142 * @return True if the event was handled, false otherwise.
6143 */
6144 public boolean onTrackballEvent(MotionEvent event) {
6145 return false;
6146 }
6147
6148 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006149 * Implement this method to handle generic motion events.
6150 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006151 * Generic motion events describe joystick movements, mouse hovers, track pad
6152 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006153 * {@link MotionEvent#getSource() source} of the motion event specifies
6154 * the class of input that was received. Implementations of this method
6155 * must examine the bits in the source before processing the event.
6156 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006157 * </p><p>
6158 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6159 * are delivered to the view under the pointer. All other generic motion events are
6160 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006161 * </p>
6162 * <code>
6163 * public boolean onGenericMotionEvent(MotionEvent event) {
6164 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006165 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6166 * // process the joystick movement...
6167 * return true;
6168 * }
6169 * }
6170 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6171 * switch (event.getAction()) {
6172 * case MotionEvent.ACTION_HOVER_MOVE:
6173 * // process the mouse hover movement...
6174 * return true;
6175 * case MotionEvent.ACTION_SCROLL:
6176 * // process the scroll wheel movement...
6177 * return true;
6178 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006179 * }
6180 * return super.onGenericMotionEvent(event);
6181 * }
6182 * </code>
6183 *
6184 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006185 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006186 */
6187 public boolean onGenericMotionEvent(MotionEvent event) {
6188 return false;
6189 }
6190
6191 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006192 * Implement this method to handle hover events.
6193 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006194 * This method is called whenever a pointer is hovering into, over, or out of the
6195 * bounds of a view and the view is not currently being touched.
6196 * Hover events are represented as pointer events with action
6197 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6198 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6199 * </p>
6200 * <ul>
6201 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6202 * when the pointer enters the bounds of the view.</li>
6203 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6204 * when the pointer has already entered the bounds of the view and has moved.</li>
6205 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6206 * when the pointer has exited the bounds of the view or when the pointer is
6207 * about to go down due to a button click, tap, or similar user action that
6208 * causes the view to be touched.</li>
6209 * </ul>
6210 * <p>
6211 * The view should implement this method to return true to indicate that it is
6212 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006213 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006214 * The default implementation calls {@link #setHovered} to update the hovered state
6215 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006216 * is enabled and is clickable. The default implementation also sends hover
6217 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006218 * </p>
6219 *
6220 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006221 * @return True if the view handled the hover event.
6222 *
6223 * @see #isHovered
6224 * @see #setHovered
6225 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006226 */
6227 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006228 // The root view may receive hover (or touch) events that are outside the bounds of
6229 // the window. This code ensures that we only send accessibility events for
6230 // hovers that are actually within the bounds of the root view.
6231 final int action = event.getAction();
6232 if (!mSendingHoverAccessibilityEvents) {
6233 if ((action == MotionEvent.ACTION_HOVER_ENTER
6234 || action == MotionEvent.ACTION_HOVER_MOVE)
6235 && !hasHoveredChild()
6236 && pointInView(event.getX(), event.getY())) {
6237 mSendingHoverAccessibilityEvents = true;
6238 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6239 }
6240 } else {
6241 if (action == MotionEvent.ACTION_HOVER_EXIT
6242 || (action == MotionEvent.ACTION_HOVER_MOVE
6243 && !pointInView(event.getX(), event.getY()))) {
6244 mSendingHoverAccessibilityEvents = false;
6245 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6246 }
Jeff Browna1b24182011-07-28 13:38:24 -07006247 }
6248
Jeff Brown87b7f802011-06-21 18:35:45 -07006249 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006250 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006251 case MotionEvent.ACTION_HOVER_ENTER:
6252 setHovered(true);
6253 break;
6254 case MotionEvent.ACTION_HOVER_EXIT:
6255 setHovered(false);
6256 break;
6257 }
Jeff Browna1b24182011-07-28 13:38:24 -07006258
6259 // Dispatch the event to onGenericMotionEvent before returning true.
6260 // This is to provide compatibility with existing applications that
6261 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6262 // break because of the new default handling for hoverable views
6263 // in onHoverEvent.
6264 // Note that onGenericMotionEvent will be called by default when
6265 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6266 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006267 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006268 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006269 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006270 }
6271
6272 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006273 * Returns true if the view should handle {@link #onHoverEvent}
6274 * by calling {@link #setHovered} to change its hovered state.
6275 *
6276 * @return True if the view is hoverable.
6277 */
6278 private boolean isHoverable() {
6279 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006280 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006281 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6282 return false;
6283 }
6284
6285 return (viewFlags & CLICKABLE) == CLICKABLE
6286 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6287 }
6288
6289 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006290 * Returns true if the view is currently hovered.
6291 *
6292 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006293 *
6294 * @see #setHovered
6295 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006296 */
Jeff Brown10b62902011-06-20 16:40:37 -07006297 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006298 public boolean isHovered() {
6299 return (mPrivateFlags & HOVERED) != 0;
6300 }
6301
6302 /**
6303 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006304 * <p>
6305 * Calling this method also changes the drawable state of the view. This
6306 * enables the view to react to hover by using different drawable resources
6307 * to change its appearance.
6308 * </p><p>
6309 * The {@link #onHoverChanged} method is called when the hovered state changes.
6310 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006311 *
6312 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006313 *
6314 * @see #isHovered
6315 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006316 */
6317 public void setHovered(boolean hovered) {
6318 if (hovered) {
6319 if ((mPrivateFlags & HOVERED) == 0) {
6320 mPrivateFlags |= HOVERED;
6321 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006322 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006323 }
6324 } else {
6325 if ((mPrivateFlags & HOVERED) != 0) {
6326 mPrivateFlags &= ~HOVERED;
6327 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006328 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006329 }
6330 }
6331 }
6332
6333 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006334 * Implement this method to handle hover state changes.
6335 * <p>
6336 * This method is called whenever the hover state changes as a result of a
6337 * call to {@link #setHovered}.
6338 * </p>
6339 *
6340 * @param hovered The current hover state, as returned by {@link #isHovered}.
6341 *
6342 * @see #isHovered
6343 * @see #setHovered
6344 */
6345 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006346 }
6347
6348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 * Implement this method to handle touch screen motion events.
6350 *
6351 * @param event The motion event.
6352 * @return True if the event was handled, false otherwise.
6353 */
6354 public boolean onTouchEvent(MotionEvent event) {
6355 final int viewFlags = mViewFlags;
6356
6357 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006358 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6359 mPrivateFlags &= ~PRESSED;
6360 refreshDrawableState();
6361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 // A disabled view that is clickable still consumes the touch
6363 // events, it just doesn't respond to them.
6364 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6365 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6366 }
6367
6368 if (mTouchDelegate != null) {
6369 if (mTouchDelegate.onTouchEvent(event)) {
6370 return true;
6371 }
6372 }
6373
6374 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6375 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6376 switch (event.getAction()) {
6377 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006378 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6379 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 // take focus if we don't have it already and we should in
6381 // touch mode.
6382 boolean focusTaken = false;
6383 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6384 focusTaken = requestFocus();
6385 }
6386
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006387 if (prepressed) {
6388 // The button is being released before we actually
6389 // showed it as pressed. Make it show the pressed
6390 // state now (before scheduling the click) to ensure
6391 // the user sees it.
6392 mPrivateFlags |= PRESSED;
6393 refreshDrawableState();
6394 }
Joe Malin32736f02011-01-19 16:14:20 -08006395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 if (!mHasPerformedLongPress) {
6397 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006398 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399
6400 // Only perform take click actions if we were in the pressed state
6401 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006402 // Use a Runnable and post this rather than calling
6403 // performClick directly. This lets other visual state
6404 // of the view update before click actions start.
6405 if (mPerformClick == null) {
6406 mPerformClick = new PerformClick();
6407 }
6408 if (!post(mPerformClick)) {
6409 performClick();
6410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006411 }
6412 }
6413
6414 if (mUnsetPressedState == null) {
6415 mUnsetPressedState = new UnsetPressedState();
6416 }
6417
Adam Powelle14579b2009-12-16 18:39:52 -08006418 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006419 postDelayed(mUnsetPressedState,
6420 ViewConfiguration.getPressedStateDuration());
6421 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 // If the post failed, unpress right now
6423 mUnsetPressedState.run();
6424 }
Adam Powelle14579b2009-12-16 18:39:52 -08006425 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 }
6427 break;
6428
6429 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006430 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006431
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006432 if (performButtonActionOnTouchDown(event)) {
6433 break;
6434 }
6435
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006436 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006437 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006438
6439 // For views inside a scrolling container, delay the pressed feedback for
6440 // a short period in case this is a scroll.
6441 if (isInScrollingContainer) {
6442 mPrivateFlags |= PREPRESSED;
6443 if (mPendingCheckForTap == null) {
6444 mPendingCheckForTap = new CheckForTap();
6445 }
6446 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6447 } else {
6448 // Not inside a scrolling container, so show the feedback right away
6449 mPrivateFlags |= PRESSED;
6450 refreshDrawableState();
6451 checkForLongClick(0);
6452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 break;
6454
6455 case MotionEvent.ACTION_CANCEL:
6456 mPrivateFlags &= ~PRESSED;
6457 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006458 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 break;
6460
6461 case MotionEvent.ACTION_MOVE:
6462 final int x = (int) event.getX();
6463 final int y = (int) event.getY();
6464
6465 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006466 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006468 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006470 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006471 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472
6473 // Need to switch from pressed to not pressed
6474 mPrivateFlags &= ~PRESSED;
6475 refreshDrawableState();
6476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 }
6478 break;
6479 }
6480 return true;
6481 }
6482
6483 return false;
6484 }
6485
6486 /**
Adam Powell10298662011-08-14 18:26:30 -07006487 * @hide
6488 */
6489 public boolean isInScrollingContainer() {
6490 ViewParent p = getParent();
6491 while (p != null && p instanceof ViewGroup) {
6492 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6493 return true;
6494 }
6495 p = p.getParent();
6496 }
6497 return false;
6498 }
6499
6500 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006501 * Remove the longpress detection timer.
6502 */
6503 private void removeLongPressCallback() {
6504 if (mPendingCheckForLongPress != null) {
6505 removeCallbacks(mPendingCheckForLongPress);
6506 }
6507 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006508
6509 /**
6510 * Remove the pending click action
6511 */
6512 private void removePerformClickCallback() {
6513 if (mPerformClick != null) {
6514 removeCallbacks(mPerformClick);
6515 }
6516 }
6517
Adam Powelle14579b2009-12-16 18:39:52 -08006518 /**
Romain Guya440b002010-02-24 15:57:54 -08006519 * Remove the prepress detection timer.
6520 */
6521 private void removeUnsetPressCallback() {
6522 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6523 setPressed(false);
6524 removeCallbacks(mUnsetPressedState);
6525 }
6526 }
6527
6528 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006529 * Remove the tap detection timer.
6530 */
6531 private void removeTapCallback() {
6532 if (mPendingCheckForTap != null) {
6533 mPrivateFlags &= ~PREPRESSED;
6534 removeCallbacks(mPendingCheckForTap);
6535 }
6536 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006537
6538 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 * Cancels a pending long press. Your subclass can use this if you
6540 * want the context menu to come up if the user presses and holds
6541 * at the same place, but you don't want it to come up if they press
6542 * and then move around enough to cause scrolling.
6543 */
6544 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006545 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006546
6547 /*
6548 * The prepressed state handled by the tap callback is a display
6549 * construct, but the tap callback will post a long press callback
6550 * less its own timeout. Remove it here.
6551 */
6552 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 }
6554
6555 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006556 * Remove the pending callback for sending a
6557 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6558 */
6559 private void removeSendViewScrolledAccessibilityEventCallback() {
6560 if (mSendViewScrolledAccessibilityEvent != null) {
6561 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6562 }
6563 }
6564
6565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 * Sets the TouchDelegate for this View.
6567 */
6568 public void setTouchDelegate(TouchDelegate delegate) {
6569 mTouchDelegate = delegate;
6570 }
6571
6572 /**
6573 * Gets the TouchDelegate for this View.
6574 */
6575 public TouchDelegate getTouchDelegate() {
6576 return mTouchDelegate;
6577 }
6578
6579 /**
6580 * Set flags controlling behavior of this view.
6581 *
6582 * @param flags Constant indicating the value which should be set
6583 * @param mask Constant indicating the bit range that should be changed
6584 */
6585 void setFlags(int flags, int mask) {
6586 int old = mViewFlags;
6587 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6588
6589 int changed = mViewFlags ^ old;
6590 if (changed == 0) {
6591 return;
6592 }
6593 int privateFlags = mPrivateFlags;
6594
6595 /* Check if the FOCUSABLE bit has changed */
6596 if (((changed & FOCUSABLE_MASK) != 0) &&
6597 ((privateFlags & HAS_BOUNDS) !=0)) {
6598 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6599 && ((privateFlags & FOCUSED) != 0)) {
6600 /* Give up focus if we are no longer focusable */
6601 clearFocus();
6602 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6603 && ((privateFlags & FOCUSED) == 0)) {
6604 /*
6605 * Tell the view system that we are now available to take focus
6606 * if no one else already has it.
6607 */
6608 if (mParent != null) mParent.focusableViewAvailable(this);
6609 }
6610 }
6611
6612 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6613 if ((changed & VISIBILITY_MASK) != 0) {
6614 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006615 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006616 * it was not visible. Marking it drawn ensures that the invalidation will
6617 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006619 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006620 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621
6622 needGlobalAttributesUpdate(true);
6623
6624 // a view becoming visible is worth notifying the parent
6625 // about in case nothing has focus. even if this specific view
6626 // isn't focusable, it may contain something that is, so let
6627 // the root view try to give this focus if nothing else does.
6628 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6629 mParent.focusableViewAvailable(this);
6630 }
6631 }
6632 }
6633
6634 /* Check if the GONE bit has changed */
6635 if ((changed & GONE) != 0) {
6636 needGlobalAttributesUpdate(false);
6637 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638
Romain Guyecd80ee2009-12-03 17:13:02 -08006639 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6640 if (hasFocus()) clearFocus();
6641 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006642 if (mParent instanceof View) {
6643 // GONE views noop invalidation, so invalidate the parent
6644 ((View) mParent).invalidate(true);
6645 }
6646 // Mark the view drawn to ensure that it gets invalidated properly the next
6647 // time it is visible and gets invalidated
6648 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 }
6650 if (mAttachInfo != null) {
6651 mAttachInfo.mViewVisibilityChanged = true;
6652 }
6653 }
6654
6655 /* Check if the VISIBLE bit has changed */
6656 if ((changed & INVISIBLE) != 0) {
6657 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006658 /*
6659 * If this view is becoming invisible, set the DRAWN flag so that
6660 * the next invalidate() will not be skipped.
6661 */
6662 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663
6664 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6665 // root view becoming invisible shouldn't clear focus
6666 if (getRootView() != this) {
6667 clearFocus();
6668 }
6669 }
6670 if (mAttachInfo != null) {
6671 mAttachInfo.mViewVisibilityChanged = true;
6672 }
6673 }
6674
Adam Powell326d8082009-12-09 15:10:07 -08006675 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006676 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006677 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6678 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006679 } else if (mParent != null) {
6680 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006681 }
Adam Powell326d8082009-12-09 15:10:07 -08006682 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6683 }
6684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6686 destroyDrawingCache();
6687 }
6688
6689 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6690 destroyDrawingCache();
6691 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006692 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006693 }
6694
6695 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6696 destroyDrawingCache();
6697 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6698 }
6699
6700 if ((changed & DRAW_MASK) != 0) {
6701 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6702 if (mBGDrawable != null) {
6703 mPrivateFlags &= ~SKIP_DRAW;
6704 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6705 } else {
6706 mPrivateFlags |= SKIP_DRAW;
6707 }
6708 } else {
6709 mPrivateFlags &= ~SKIP_DRAW;
6710 }
6711 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006712 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 }
6714
6715 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006716 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 mParent.recomputeViewAttributes(this);
6718 }
6719 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006720
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006721 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006722 requestLayout();
6723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006724 }
6725
6726 /**
6727 * Change the view's z order in the tree, so it's on top of other sibling
6728 * views
6729 */
6730 public void bringToFront() {
6731 if (mParent != null) {
6732 mParent.bringChildToFront(this);
6733 }
6734 }
6735
6736 /**
6737 * This is called in response to an internal scroll in this view (i.e., the
6738 * view scrolled its own contents). This is typically as a result of
6739 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6740 * called.
6741 *
6742 * @param l Current horizontal scroll origin.
6743 * @param t Current vertical scroll origin.
6744 * @param oldl Previous horizontal scroll origin.
6745 * @param oldt Previous vertical scroll origin.
6746 */
6747 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006748 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6749 postSendViewScrolledAccessibilityEventCallback();
6750 }
6751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 mBackgroundSizeChanged = true;
6753
6754 final AttachInfo ai = mAttachInfo;
6755 if (ai != null) {
6756 ai.mViewScrollChanged = true;
6757 }
6758 }
6759
6760 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006761 * Interface definition for a callback to be invoked when the layout bounds of a view
6762 * changes due to layout processing.
6763 */
6764 public interface OnLayoutChangeListener {
6765 /**
6766 * Called when the focus state of a view has changed.
6767 *
6768 * @param v The view whose state has changed.
6769 * @param left The new value of the view's left property.
6770 * @param top The new value of the view's top property.
6771 * @param right The new value of the view's right property.
6772 * @param bottom The new value of the view's bottom property.
6773 * @param oldLeft The previous value of the view's left property.
6774 * @param oldTop The previous value of the view's top property.
6775 * @param oldRight The previous value of the view's right property.
6776 * @param oldBottom The previous value of the view's bottom property.
6777 */
6778 void onLayoutChange(View v, int left, int top, int right, int bottom,
6779 int oldLeft, int oldTop, int oldRight, int oldBottom);
6780 }
6781
6782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 * This is called during layout when the size of this view has changed. If
6784 * you were just added to the view hierarchy, you're called with the old
6785 * values of 0.
6786 *
6787 * @param w Current width of this view.
6788 * @param h Current height of this view.
6789 * @param oldw Old width of this view.
6790 * @param oldh Old height of this view.
6791 */
6792 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6793 }
6794
6795 /**
6796 * Called by draw to draw the child views. This may be overridden
6797 * by derived classes to gain control just before its children are drawn
6798 * (but after its own view has been drawn).
6799 * @param canvas the canvas on which to draw the view
6800 */
6801 protected void dispatchDraw(Canvas canvas) {
6802 }
6803
6804 /**
6805 * Gets the parent of this view. Note that the parent is a
6806 * ViewParent and not necessarily a View.
6807 *
6808 * @return Parent of this view.
6809 */
6810 public final ViewParent getParent() {
6811 return mParent;
6812 }
6813
6814 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006815 * Set the horizontal scrolled position of your view. This will cause a call to
6816 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6817 * invalidated.
6818 * @param value the x position to scroll to
6819 */
6820 public void setScrollX(int value) {
6821 scrollTo(value, mScrollY);
6822 }
6823
6824 /**
6825 * Set the vertical scrolled position of your view. This will cause a call to
6826 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6827 * invalidated.
6828 * @param value the y position to scroll to
6829 */
6830 public void setScrollY(int value) {
6831 scrollTo(mScrollX, value);
6832 }
6833
6834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 * Return the scrolled left position of this view. This is the left edge of
6836 * the displayed part of your view. You do not need to draw any pixels
6837 * farther left, since those are outside of the frame of your view on
6838 * screen.
6839 *
6840 * @return The left edge of the displayed part of your view, in pixels.
6841 */
6842 public final int getScrollX() {
6843 return mScrollX;
6844 }
6845
6846 /**
6847 * Return the scrolled top position of this view. This is the top edge of
6848 * the displayed part of your view. You do not need to draw any pixels above
6849 * it, since those are outside of the frame of your view on screen.
6850 *
6851 * @return The top edge of the displayed part of your view, in pixels.
6852 */
6853 public final int getScrollY() {
6854 return mScrollY;
6855 }
6856
6857 /**
6858 * Return the width of the your view.
6859 *
6860 * @return The width of your view, in pixels.
6861 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006862 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 public final int getWidth() {
6864 return mRight - mLeft;
6865 }
6866
6867 /**
6868 * Return the height of your view.
6869 *
6870 * @return The height of your view, in pixels.
6871 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006872 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 public final int getHeight() {
6874 return mBottom - mTop;
6875 }
6876
6877 /**
6878 * Return the visible drawing bounds of your view. Fills in the output
6879 * rectangle with the values from getScrollX(), getScrollY(),
6880 * getWidth(), and getHeight().
6881 *
6882 * @param outRect The (scrolled) drawing bounds of the view.
6883 */
6884 public void getDrawingRect(Rect outRect) {
6885 outRect.left = mScrollX;
6886 outRect.top = mScrollY;
6887 outRect.right = mScrollX + (mRight - mLeft);
6888 outRect.bottom = mScrollY + (mBottom - mTop);
6889 }
6890
6891 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006892 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6893 * raw width component (that is the result is masked by
6894 * {@link #MEASURED_SIZE_MASK}).
6895 *
6896 * @return The raw measured width of this view.
6897 */
6898 public final int getMeasuredWidth() {
6899 return mMeasuredWidth & MEASURED_SIZE_MASK;
6900 }
6901
6902 /**
6903 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006904 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006905 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 * This should be used during measurement and layout calculations only. Use
6907 * {@link #getWidth()} to see how wide a view is after layout.
6908 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006909 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006911 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 return mMeasuredWidth;
6913 }
6914
6915 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006916 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6917 * raw width component (that is the result is masked by
6918 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006920 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 */
6922 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006923 return mMeasuredHeight & MEASURED_SIZE_MASK;
6924 }
6925
6926 /**
6927 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006928 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006929 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6930 * This should be used during measurement and layout calculations only. Use
6931 * {@link #getHeight()} to see how wide a view is after layout.
6932 *
6933 * @return The measured width of this view as a bit mask.
6934 */
6935 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 return mMeasuredHeight;
6937 }
6938
6939 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006940 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6941 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6942 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6943 * and the height component is at the shifted bits
6944 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6945 */
6946 public final int getMeasuredState() {
6947 return (mMeasuredWidth&MEASURED_STATE_MASK)
6948 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6949 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6950 }
6951
6952 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006953 * The transform matrix of this view, which is calculated based on the current
6954 * roation, scale, and pivot properties.
6955 *
6956 * @see #getRotation()
6957 * @see #getScaleX()
6958 * @see #getScaleY()
6959 * @see #getPivotX()
6960 * @see #getPivotY()
6961 * @return The current transform matrix for the view
6962 */
6963 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006964 if (mTransformationInfo != null) {
6965 updateMatrix();
6966 return mTransformationInfo.mMatrix;
6967 }
6968 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006969 }
6970
6971 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006972 * Utility function to determine if the value is far enough away from zero to be
6973 * considered non-zero.
6974 * @param value A floating point value to check for zero-ness
6975 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6976 */
6977 private static boolean nonzero(float value) {
6978 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6979 }
6980
6981 /**
Jeff Brown86671742010-09-30 20:00:15 -07006982 * Returns true if the transform matrix is the identity matrix.
6983 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006984 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006985 * @return True if the transform matrix is the identity matrix, false otherwise.
6986 */
Jeff Brown86671742010-09-30 20:00:15 -07006987 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006988 if (mTransformationInfo != null) {
6989 updateMatrix();
6990 return mTransformationInfo.mMatrixIsIdentity;
6991 }
6992 return true;
6993 }
6994
6995 void ensureTransformationInfo() {
6996 if (mTransformationInfo == null) {
6997 mTransformationInfo = new TransformationInfo();
6998 }
Jeff Brown86671742010-09-30 20:00:15 -07006999 }
7000
7001 /**
7002 * Recomputes the transform matrix if necessary.
7003 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007004 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007005 final TransformationInfo info = mTransformationInfo;
7006 if (info == null) {
7007 return;
7008 }
7009 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007010 // transform-related properties have changed since the last time someone
7011 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007012
7013 // Figure out if we need to update the pivot point
7014 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007015 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7016 info.mPrevWidth = mRight - mLeft;
7017 info.mPrevHeight = mBottom - mTop;
7018 info.mPivotX = info.mPrevWidth / 2f;
7019 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007020 }
7021 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007022 info.mMatrix.reset();
7023 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7024 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7025 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7026 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007027 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007028 if (info.mCamera == null) {
7029 info.mCamera = new Camera();
7030 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007031 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007032 info.mCamera.save();
7033 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7034 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7035 info.mCamera.getMatrix(info.matrix3D);
7036 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7037 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7038 info.mPivotY + info.mTranslationY);
7039 info.mMatrix.postConcat(info.matrix3D);
7040 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007041 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007042 info.mMatrixDirty = false;
7043 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7044 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007045 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007046 }
7047
7048 /**
7049 * Utility method to retrieve the inverse of the current mMatrix property.
7050 * We cache the matrix to avoid recalculating it when transform properties
7051 * have not changed.
7052 *
7053 * @return The inverse of the current matrix of this view.
7054 */
Jeff Brown86671742010-09-30 20:00:15 -07007055 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007056 final TransformationInfo info = mTransformationInfo;
7057 if (info != null) {
7058 updateMatrix();
7059 if (info.mInverseMatrixDirty) {
7060 if (info.mInverseMatrix == null) {
7061 info.mInverseMatrix = new Matrix();
7062 }
7063 info.mMatrix.invert(info.mInverseMatrix);
7064 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007065 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007066 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007067 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007068 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007069 }
7070
7071 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007072 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7073 * views are drawn) from the camera to this view. The camera's distance
7074 * affects 3D transformations, for instance rotations around the X and Y
7075 * axis. If the rotationX or rotationY properties are changed and this view is
7076 * large (more than half the size of the screen), it is recommended to always
7077 * use a camera distance that's greater than the height (X axis rotation) or
7078 * the width (Y axis rotation) of this view.</p>
7079 *
7080 * <p>The distance of the camera from the view plane can have an affect on the
7081 * perspective distortion of the view when it is rotated around the x or y axis.
7082 * For example, a large distance will result in a large viewing angle, and there
7083 * will not be much perspective distortion of the view as it rotates. A short
7084 * distance may cause much more perspective distortion upon rotation, and can
7085 * also result in some drawing artifacts if the rotated view ends up partially
7086 * behind the camera (which is why the recommendation is to use a distance at
7087 * least as far as the size of the view, if the view is to be rotated.)</p>
7088 *
7089 * <p>The distance is expressed in "depth pixels." The default distance depends
7090 * on the screen density. For instance, on a medium density display, the
7091 * default distance is 1280. On a high density display, the default distance
7092 * is 1920.</p>
7093 *
7094 * <p>If you want to specify a distance that leads to visually consistent
7095 * results across various densities, use the following formula:</p>
7096 * <pre>
7097 * float scale = context.getResources().getDisplayMetrics().density;
7098 * view.setCameraDistance(distance * scale);
7099 * </pre>
7100 *
7101 * <p>The density scale factor of a high density display is 1.5,
7102 * and 1920 = 1280 * 1.5.</p>
7103 *
7104 * @param distance The distance in "depth pixels", if negative the opposite
7105 * value is used
7106 *
7107 * @see #setRotationX(float)
7108 * @see #setRotationY(float)
7109 */
7110 public void setCameraDistance(float distance) {
7111 invalidateParentCaches();
7112 invalidate(false);
7113
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007114 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007115 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007116 final TransformationInfo info = mTransformationInfo;
7117 if (info.mCamera == null) {
7118 info.mCamera = new Camera();
7119 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007120 }
7121
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007122 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7123 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007124
7125 invalidate(false);
7126 }
7127
7128 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007129 * The degrees that the view is rotated around the pivot point.
7130 *
Romain Guya5364ee2011-02-24 14:46:04 -08007131 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007132 * @see #getPivotX()
7133 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007134 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007135 * @return The degrees of rotation.
7136 */
7137 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007138 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007139 }
7140
7141 /**
Chet Haase897247b2010-09-09 14:54:47 -07007142 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7143 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007144 *
7145 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007146 *
7147 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007148 * @see #getPivotX()
7149 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007150 * @see #setRotationX(float)
7151 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007152 *
7153 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007154 */
7155 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007156 ensureTransformationInfo();
7157 final TransformationInfo info = mTransformationInfo;
7158 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007159 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007160 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007161 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007162 info.mRotation = rotation;
7163 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007164 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007165 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007166 }
7167 }
7168
7169 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007170 * The degrees that the view is rotated around the vertical axis through the pivot point.
7171 *
7172 * @see #getPivotX()
7173 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007174 * @see #setRotationY(float)
7175 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007176 * @return The degrees of Y rotation.
7177 */
7178 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007179 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007180 }
7181
7182 /**
Chet Haase897247b2010-09-09 14:54:47 -07007183 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7184 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7185 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007186 *
7187 * When rotating large views, it is recommended to adjust the camera distance
7188 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007189 *
7190 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007191 *
7192 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007193 * @see #getPivotX()
7194 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007195 * @see #setRotation(float)
7196 * @see #setRotationX(float)
7197 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007198 *
7199 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007200 */
7201 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007202 ensureTransformationInfo();
7203 final TransformationInfo info = mTransformationInfo;
7204 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007205 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007206 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007207 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007208 info.mRotationY = rotationY;
7209 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007210 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007211 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007212 }
7213 }
7214
7215 /**
7216 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7217 *
7218 * @see #getPivotX()
7219 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007220 * @see #setRotationX(float)
7221 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007222 * @return The degrees of X rotation.
7223 */
7224 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007225 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007226 }
7227
7228 /**
Chet Haase897247b2010-09-09 14:54:47 -07007229 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7230 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7231 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007232 *
7233 * When rotating large views, it is recommended to adjust the camera distance
7234 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007235 *
7236 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007237 *
7238 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007239 * @see #getPivotX()
7240 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007241 * @see #setRotation(float)
7242 * @see #setRotationY(float)
7243 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007244 *
7245 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007246 */
7247 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007248 ensureTransformationInfo();
7249 final TransformationInfo info = mTransformationInfo;
7250 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007251 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007252 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007253 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007254 info.mRotationX = rotationX;
7255 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007256 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007257 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007258 }
7259 }
7260
7261 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007262 * The amount that the view is scaled in x around the pivot point, as a proportion of
7263 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7264 *
Joe Onorato93162322010-09-16 15:42:01 -04007265 * <p>By default, this is 1.0f.
7266 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007267 * @see #getPivotX()
7268 * @see #getPivotY()
7269 * @return The scaling factor.
7270 */
7271 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007272 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007273 }
7274
7275 /**
7276 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7277 * the view's unscaled width. A value of 1 means that no scaling is applied.
7278 *
7279 * @param scaleX The scaling factor.
7280 * @see #getPivotX()
7281 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007282 *
7283 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007284 */
7285 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007286 ensureTransformationInfo();
7287 final TransformationInfo info = mTransformationInfo;
7288 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007289 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007290 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007291 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007292 info.mScaleX = scaleX;
7293 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007294 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007295 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007296 }
7297 }
7298
7299 /**
7300 * The amount that the view is scaled in y around the pivot point, as a proportion of
7301 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7302 *
Joe Onorato93162322010-09-16 15:42:01 -04007303 * <p>By default, this is 1.0f.
7304 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007305 * @see #getPivotX()
7306 * @see #getPivotY()
7307 * @return The scaling factor.
7308 */
7309 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007310 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007311 }
7312
7313 /**
7314 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7315 * the view's unscaled width. A value of 1 means that no scaling is applied.
7316 *
7317 * @param scaleY The scaling factor.
7318 * @see #getPivotX()
7319 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007320 *
7321 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007322 */
7323 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007324 ensureTransformationInfo();
7325 final TransformationInfo info = mTransformationInfo;
7326 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007327 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007328 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007329 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007330 info.mScaleY = scaleY;
7331 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007332 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007333 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007334 }
7335 }
7336
7337 /**
7338 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7339 * and {@link #setScaleX(float) scaled}.
7340 *
7341 * @see #getRotation()
7342 * @see #getScaleX()
7343 * @see #getScaleY()
7344 * @see #getPivotY()
7345 * @return The x location of the pivot point.
7346 */
7347 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007348 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007349 }
7350
7351 /**
7352 * Sets the x location of the point around which the view is
7353 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007354 * By default, the pivot point is centered on the object.
7355 * Setting this property disables this behavior and causes the view to use only the
7356 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007357 *
7358 * @param pivotX The x location of the pivot point.
7359 * @see #getRotation()
7360 * @see #getScaleX()
7361 * @see #getScaleY()
7362 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007363 *
7364 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007365 */
7366 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007367 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007368 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007369 final TransformationInfo info = mTransformationInfo;
7370 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007371 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007372 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007373 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007374 info.mPivotX = pivotX;
7375 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007376 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007377 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007378 }
7379 }
7380
7381 /**
7382 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7383 * and {@link #setScaleY(float) scaled}.
7384 *
7385 * @see #getRotation()
7386 * @see #getScaleX()
7387 * @see #getScaleY()
7388 * @see #getPivotY()
7389 * @return The y location of the pivot point.
7390 */
7391 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007392 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007393 }
7394
7395 /**
7396 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007397 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7398 * Setting this property disables this behavior and causes the view to use only the
7399 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007400 *
7401 * @param pivotY The y location of the pivot point.
7402 * @see #getRotation()
7403 * @see #getScaleX()
7404 * @see #getScaleY()
7405 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007406 *
7407 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007408 */
7409 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007410 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007411 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007412 final TransformationInfo info = mTransformationInfo;
7413 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007414 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007415 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007416 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007417 info.mPivotY = pivotY;
7418 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007419 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007420 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007421 }
7422 }
7423
7424 /**
7425 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7426 * completely transparent and 1 means the view is completely opaque.
7427 *
Joe Onorato93162322010-09-16 15:42:01 -04007428 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007429 * @return The opacity of the view.
7430 */
7431 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007432 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007433 }
7434
7435 /**
Romain Guy171c5922011-01-06 10:04:23 -08007436 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7437 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007438 *
Romain Guy171c5922011-01-06 10:04:23 -08007439 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7440 * responsible for applying the opacity itself. Otherwise, calling this method is
7441 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007442 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007443 *
7444 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007445 *
Joe Malin32736f02011-01-19 16:14:20 -08007446 * @see #setLayerType(int, android.graphics.Paint)
7447 *
Chet Haase73066682010-11-29 15:55:32 -08007448 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007449 */
7450 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007451 ensureTransformationInfo();
7452 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007453 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007454 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007455 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007456 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007457 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007458 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007459 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007460 invalidate(false);
7461 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007462 }
7463
7464 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007465 * Faster version of setAlpha() which performs the same steps except there are
7466 * no calls to invalidate(). The caller of this function should perform proper invalidation
7467 * on the parent and this object. The return value indicates whether the subclass handles
7468 * alpha (the return value for onSetAlpha()).
7469 *
7470 * @param alpha The new value for the alpha property
7471 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7472 */
7473 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007474 ensureTransformationInfo();
7475 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007476 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7477 if (subclassHandlesAlpha) {
7478 mPrivateFlags |= ALPHA_SET;
7479 } else {
7480 mPrivateFlags &= ~ALPHA_SET;
7481 }
7482 return subclassHandlesAlpha;
7483 }
7484
7485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 * Top position of this view relative to its parent.
7487 *
7488 * @return The top of this view, in pixels.
7489 */
7490 @ViewDebug.CapturedViewProperty
7491 public final int getTop() {
7492 return mTop;
7493 }
7494
7495 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007496 * Sets the top position of this view relative to its parent. This method is meant to be called
7497 * by the layout system and should not generally be called otherwise, because the property
7498 * may be changed at any time by the layout.
7499 *
7500 * @param top The top of this view, in pixels.
7501 */
7502 public final void setTop(int top) {
7503 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007504 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007505 final boolean matrixIsIdentity = mTransformationInfo == null
7506 || mTransformationInfo.mMatrixIsIdentity;
7507 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007508 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007509 int minTop;
7510 int yLoc;
7511 if (top < mTop) {
7512 minTop = top;
7513 yLoc = top - mTop;
7514 } else {
7515 minTop = mTop;
7516 yLoc = 0;
7517 }
Chet Haasee9140a72011-02-16 16:23:29 -08007518 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007519 }
7520 } else {
7521 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007522 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007523 }
7524
Chet Haaseed032702010-10-01 14:05:54 -07007525 int width = mRight - mLeft;
7526 int oldHeight = mBottom - mTop;
7527
Chet Haase21cd1382010-09-01 17:42:29 -07007528 mTop = top;
7529
Chet Haaseed032702010-10-01 14:05:54 -07007530 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7531
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007532 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007533 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7534 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007535 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007536 }
Chet Haase21cd1382010-09-01 17:42:29 -07007537 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007538 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007539 }
Chet Haase55dbb652010-12-21 20:15:08 -08007540 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007541 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007542 }
7543 }
7544
7545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 * Bottom position of this view relative to its parent.
7547 *
7548 * @return The bottom of this view, in pixels.
7549 */
7550 @ViewDebug.CapturedViewProperty
7551 public final int getBottom() {
7552 return mBottom;
7553 }
7554
7555 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007556 * True if this view has changed since the last time being drawn.
7557 *
7558 * @return The dirty state of this view.
7559 */
7560 public boolean isDirty() {
7561 return (mPrivateFlags & DIRTY_MASK) != 0;
7562 }
7563
7564 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007565 * Sets the bottom position of this view relative to its parent. This method is meant to be
7566 * called by the layout system and should not generally be called otherwise, because the
7567 * property may be changed at any time by the layout.
7568 *
7569 * @param bottom The bottom of this view, in pixels.
7570 */
7571 public final void setBottom(int bottom) {
7572 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007573 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007574 final boolean matrixIsIdentity = mTransformationInfo == null
7575 || mTransformationInfo.mMatrixIsIdentity;
7576 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007577 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007578 int maxBottom;
7579 if (bottom < mBottom) {
7580 maxBottom = mBottom;
7581 } else {
7582 maxBottom = bottom;
7583 }
Chet Haasee9140a72011-02-16 16:23:29 -08007584 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007585 }
7586 } else {
7587 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007588 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007589 }
7590
Chet Haaseed032702010-10-01 14:05:54 -07007591 int width = mRight - mLeft;
7592 int oldHeight = mBottom - mTop;
7593
Chet Haase21cd1382010-09-01 17:42:29 -07007594 mBottom = bottom;
7595
Chet Haaseed032702010-10-01 14:05:54 -07007596 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7597
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007598 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007599 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7600 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007601 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007602 }
Chet Haase21cd1382010-09-01 17:42:29 -07007603 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007604 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007605 }
Chet Haase55dbb652010-12-21 20:15:08 -08007606 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007607 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007608 }
7609 }
7610
7611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 * Left position of this view relative to its parent.
7613 *
7614 * @return The left edge of this view, in pixels.
7615 */
7616 @ViewDebug.CapturedViewProperty
7617 public final int getLeft() {
7618 return mLeft;
7619 }
7620
7621 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007622 * Sets the left position of this view relative to its parent. This method is meant to be called
7623 * by the layout system and should not generally be called otherwise, because the property
7624 * may be changed at any time by the layout.
7625 *
7626 * @param left The bottom of this view, in pixels.
7627 */
7628 public final void setLeft(int left) {
7629 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007630 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007631 final boolean matrixIsIdentity = mTransformationInfo == null
7632 || mTransformationInfo.mMatrixIsIdentity;
7633 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007634 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007635 int minLeft;
7636 int xLoc;
7637 if (left < mLeft) {
7638 minLeft = left;
7639 xLoc = left - mLeft;
7640 } else {
7641 minLeft = mLeft;
7642 xLoc = 0;
7643 }
Chet Haasee9140a72011-02-16 16:23:29 -08007644 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007645 }
7646 } else {
7647 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007648 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007649 }
7650
Chet Haaseed032702010-10-01 14:05:54 -07007651 int oldWidth = mRight - mLeft;
7652 int height = mBottom - mTop;
7653
Chet Haase21cd1382010-09-01 17:42:29 -07007654 mLeft = left;
7655
Chet Haaseed032702010-10-01 14:05:54 -07007656 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7657
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007658 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007659 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7660 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007661 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007662 }
Chet Haase21cd1382010-09-01 17:42:29 -07007663 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007664 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007665 }
Chet Haase55dbb652010-12-21 20:15:08 -08007666 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007667 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007668 }
7669 }
7670
7671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 * Right position of this view relative to its parent.
7673 *
7674 * @return The right edge of this view, in pixels.
7675 */
7676 @ViewDebug.CapturedViewProperty
7677 public final int getRight() {
7678 return mRight;
7679 }
7680
7681 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007682 * Sets the right position of this view relative to its parent. This method is meant to be called
7683 * by the layout system and should not generally be called otherwise, because the property
7684 * may be changed at any time by the layout.
7685 *
7686 * @param right The bottom of this view, in pixels.
7687 */
7688 public final void setRight(int right) {
7689 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007690 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007691 final boolean matrixIsIdentity = mTransformationInfo == null
7692 || mTransformationInfo.mMatrixIsIdentity;
7693 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007694 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007695 int maxRight;
7696 if (right < mRight) {
7697 maxRight = mRight;
7698 } else {
7699 maxRight = right;
7700 }
Chet Haasee9140a72011-02-16 16:23:29 -08007701 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007702 }
7703 } else {
7704 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007705 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007706 }
7707
Chet Haaseed032702010-10-01 14:05:54 -07007708 int oldWidth = mRight - mLeft;
7709 int height = mBottom - mTop;
7710
Chet Haase21cd1382010-09-01 17:42:29 -07007711 mRight = right;
7712
Chet Haaseed032702010-10-01 14:05:54 -07007713 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7714
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007715 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007716 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7717 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007718 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007719 }
Chet Haase21cd1382010-09-01 17:42:29 -07007720 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007721 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007722 }
Chet Haase55dbb652010-12-21 20:15:08 -08007723 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007724 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007725 }
7726 }
7727
7728 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007729 * The visual x position of this view, in pixels. This is equivalent to the
7730 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007731 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007732 *
Chet Haasedf030d22010-07-30 17:22:38 -07007733 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007734 */
Chet Haasedf030d22010-07-30 17:22:38 -07007735 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007736 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007737 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007738
Chet Haasedf030d22010-07-30 17:22:38 -07007739 /**
7740 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7741 * {@link #setTranslationX(float) translationX} property to be the difference between
7742 * the x value passed in and the current {@link #getLeft() left} property.
7743 *
7744 * @param x The visual x position of this view, in pixels.
7745 */
7746 public void setX(float x) {
7747 setTranslationX(x - mLeft);
7748 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007749
Chet Haasedf030d22010-07-30 17:22:38 -07007750 /**
7751 * The visual y position of this view, in pixels. This is equivalent to the
7752 * {@link #setTranslationY(float) translationY} property plus the current
7753 * {@link #getTop() top} property.
7754 *
7755 * @return The visual y position of this view, in pixels.
7756 */
7757 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007758 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007759 }
7760
7761 /**
7762 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7763 * {@link #setTranslationY(float) translationY} property to be the difference between
7764 * the y value passed in and the current {@link #getTop() top} property.
7765 *
7766 * @param y The visual y position of this view, in pixels.
7767 */
7768 public void setY(float y) {
7769 setTranslationY(y - mTop);
7770 }
7771
7772
7773 /**
7774 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7775 * This position is post-layout, in addition to wherever the object's
7776 * layout placed it.
7777 *
7778 * @return The horizontal position of this view relative to its left position, in pixels.
7779 */
7780 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007781 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007782 }
7783
7784 /**
7785 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7786 * This effectively positions the object post-layout, in addition to wherever the object's
7787 * layout placed it.
7788 *
7789 * @param translationX The horizontal position of this view relative to its left position,
7790 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007791 *
7792 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007793 */
7794 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007795 ensureTransformationInfo();
7796 final TransformationInfo info = mTransformationInfo;
7797 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007798 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007799 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007800 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007801 info.mTranslationX = translationX;
7802 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007803 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007804 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007805 }
7806 }
7807
7808 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007809 * The horizontal location of this view relative to its {@link #getTop() top} position.
7810 * This position is 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 * @return The vertical position of this view relative to its top position,
7814 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007815 */
Chet Haasedf030d22010-07-30 17:22:38 -07007816 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007817 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007818 }
7819
7820 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007821 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7822 * This effectively positions the object post-layout, in addition to wherever the object's
7823 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007824 *
Chet Haasedf030d22010-07-30 17:22:38 -07007825 * @param translationY The vertical position of this view relative to its top position,
7826 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007827 *
7828 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007829 */
Chet Haasedf030d22010-07-30 17:22:38 -07007830 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007831 ensureTransformationInfo();
7832 final TransformationInfo info = mTransformationInfo;
7833 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007834 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007835 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007836 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007837 info.mTranslationY = translationY;
7838 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007839 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007840 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007841 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007842 }
7843
7844 /**
Romain Guyda489792011-02-03 01:05:15 -08007845 * @hide
7846 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007847 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007848 ensureTransformationInfo();
7849 final TransformationInfo info = mTransformationInfo;
7850 info.mTranslationX = x;
7851 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007852 }
7853
7854 /**
7855 * @hide
7856 */
7857 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007858 ensureTransformationInfo();
7859 final TransformationInfo info = mTransformationInfo;
7860 info.mTranslationY = y;
7861 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007862 }
7863
7864 /**
7865 * @hide
7866 */
Romain Guyda489792011-02-03 01:05:15 -08007867 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007868 ensureTransformationInfo();
7869 final TransformationInfo info = mTransformationInfo;
7870 info.mTranslationX = x - mLeft;
7871 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007872 }
7873
7874 /**
7875 * @hide
7876 */
7877 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007878 ensureTransformationInfo();
7879 final TransformationInfo info = mTransformationInfo;
7880 info.mTranslationY = y - mTop;
7881 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007882 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007883
Romain Guyda489792011-02-03 01:05:15 -08007884 /**
7885 * @hide
7886 */
7887 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007888 ensureTransformationInfo();
7889 final TransformationInfo info = mTransformationInfo;
7890 info.mScaleX = x;
7891 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007892 }
7893
7894 /**
7895 * @hide
7896 */
7897 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007898 ensureTransformationInfo();
7899 final TransformationInfo info = mTransformationInfo;
7900 info.mScaleY = y;
7901 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007902 }
7903
7904 /**
7905 * @hide
7906 */
7907 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007908 ensureTransformationInfo();
7909 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007910 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007911
Romain Guyda489792011-02-03 01:05:15 -08007912 /**
7913 * @hide
7914 */
7915 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007916 ensureTransformationInfo();
7917 final TransformationInfo info = mTransformationInfo;
7918 info.mRotationY = y;
7919 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007920 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007921
Romain Guyda489792011-02-03 01:05:15 -08007922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 * Hit rectangle in parent's coordinates
7924 *
7925 * @param outRect The hit rectangle of the view.
7926 */
7927 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007928 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007929 final TransformationInfo info = mTransformationInfo;
7930 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007931 outRect.set(mLeft, mTop, mRight, mBottom);
7932 } else {
7933 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007934 tmpRect.set(-info.mPivotX, -info.mPivotY,
7935 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7936 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007937 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7938 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007939 }
7940 }
7941
7942 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007943 * Determines whether the given point, in local coordinates is inside the view.
7944 */
7945 /*package*/ final boolean pointInView(float localX, float localY) {
7946 return localX >= 0 && localX < (mRight - mLeft)
7947 && localY >= 0 && localY < (mBottom - mTop);
7948 }
7949
7950 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007951 * Utility method to determine whether the given point, in local coordinates,
7952 * is inside the view, where the area of the view is expanded by the slop factor.
7953 * This method is called while processing touch-move events to determine if the event
7954 * is still within the view.
7955 */
7956 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007957 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007958 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 }
7960
7961 /**
7962 * When a view has focus and the user navigates away from it, the next view is searched for
7963 * starting from the rectangle filled in by this method.
7964 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007965 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7966 * of the view. However, if your view maintains some idea of internal selection,
7967 * such as a cursor, or a selected row or column, you should override this method and
7968 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 *
7970 * @param r The rectangle to fill in, in this view's coordinates.
7971 */
7972 public void getFocusedRect(Rect r) {
7973 getDrawingRect(r);
7974 }
7975
7976 /**
7977 * If some part of this view is not clipped by any of its parents, then
7978 * return that area in r in global (root) coordinates. To convert r to local
7979 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7980 * -globalOffset.y)) If the view is completely clipped or translated out,
7981 * return false.
7982 *
7983 * @param r If true is returned, r holds the global coordinates of the
7984 * visible portion of this view.
7985 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7986 * between this view and its root. globalOffet may be null.
7987 * @return true if r is non-empty (i.e. part of the view is visible at the
7988 * root level.
7989 */
7990 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7991 int width = mRight - mLeft;
7992 int height = mBottom - mTop;
7993 if (width > 0 && height > 0) {
7994 r.set(0, 0, width, height);
7995 if (globalOffset != null) {
7996 globalOffset.set(-mScrollX, -mScrollY);
7997 }
7998 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7999 }
8000 return false;
8001 }
8002
8003 public final boolean getGlobalVisibleRect(Rect r) {
8004 return getGlobalVisibleRect(r, null);
8005 }
8006
8007 public final boolean getLocalVisibleRect(Rect r) {
8008 Point offset = new Point();
8009 if (getGlobalVisibleRect(r, offset)) {
8010 r.offset(-offset.x, -offset.y); // make r local
8011 return true;
8012 }
8013 return false;
8014 }
8015
8016 /**
8017 * Offset this view's vertical location by the specified number of pixels.
8018 *
8019 * @param offset the number of pixels to offset the view by
8020 */
8021 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008022 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008023 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008024 final boolean matrixIsIdentity = mTransformationInfo == null
8025 || mTransformationInfo.mMatrixIsIdentity;
8026 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008027 final ViewParent p = mParent;
8028 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008029 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008030 int minTop;
8031 int maxBottom;
8032 int yLoc;
8033 if (offset < 0) {
8034 minTop = mTop + offset;
8035 maxBottom = mBottom;
8036 yLoc = offset;
8037 } else {
8038 minTop = mTop;
8039 maxBottom = mBottom + offset;
8040 yLoc = 0;
8041 }
8042 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8043 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008044 }
8045 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008046 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008047 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008048
Chet Haasec3aa3612010-06-17 08:50:37 -07008049 mTop += offset;
8050 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008051
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008052 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008053 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008054 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008055 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008056 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 }
8059
8060 /**
8061 * Offset this view's horizontal location by the specified amount of pixels.
8062 *
8063 * @param offset the numer of pixels to offset the view by
8064 */
8065 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008066 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008067 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008068 final boolean matrixIsIdentity = mTransformationInfo == null
8069 || mTransformationInfo.mMatrixIsIdentity;
8070 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008071 final ViewParent p = mParent;
8072 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008073 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008074 int minLeft;
8075 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008076 if (offset < 0) {
8077 minLeft = mLeft + offset;
8078 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008079 } else {
8080 minLeft = mLeft;
8081 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008082 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008083 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008084 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008085 }
8086 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008087 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008088 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008089
Chet Haasec3aa3612010-06-17 08:50:37 -07008090 mLeft += offset;
8091 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008092
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008093 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008094 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008095 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008096 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008097 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 }
8100
8101 /**
8102 * Get the LayoutParams associated with this view. All views should have
8103 * layout parameters. These supply parameters to the <i>parent</i> of this
8104 * view specifying how it should be arranged. There are many subclasses of
8105 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8106 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008107 *
8108 * This method may return null if this View is not attached to a parent
8109 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8110 * was not invoked successfully. When a View is attached to a parent
8111 * ViewGroup, this method must not return null.
8112 *
8113 * @return The LayoutParams associated with this view, or null if no
8114 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008116 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 public ViewGroup.LayoutParams getLayoutParams() {
8118 return mLayoutParams;
8119 }
8120
8121 /**
8122 * Set the layout parameters associated with this view. These supply
8123 * parameters to the <i>parent</i> of this view specifying how it should be
8124 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8125 * correspond to the different subclasses of ViewGroup that are responsible
8126 * for arranging their children.
8127 *
Romain Guy01c174b2011-02-22 11:51:06 -08008128 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 */
8130 public void setLayoutParams(ViewGroup.LayoutParams params) {
8131 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008132 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 }
8134 mLayoutParams = params;
8135 requestLayout();
8136 }
8137
8138 /**
8139 * Set the scrolled position of your view. This will cause a call to
8140 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8141 * invalidated.
8142 * @param x the x position to scroll to
8143 * @param y the y position to scroll to
8144 */
8145 public void scrollTo(int x, int y) {
8146 if (mScrollX != x || mScrollY != y) {
8147 int oldX = mScrollX;
8148 int oldY = mScrollY;
8149 mScrollX = x;
8150 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008151 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008153 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008154 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 }
8157 }
8158
8159 /**
8160 * Move the scrolled position of your view. This will cause a call to
8161 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8162 * invalidated.
8163 * @param x the amount of pixels to scroll by horizontally
8164 * @param y the amount of pixels to scroll by vertically
8165 */
8166 public void scrollBy(int x, int y) {
8167 scrollTo(mScrollX + x, mScrollY + y);
8168 }
8169
8170 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008171 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8172 * animation to fade the scrollbars out after a default delay. If a subclass
8173 * provides animated scrolling, the start delay should equal the duration
8174 * of the scrolling animation.</p>
8175 *
8176 * <p>The animation starts only if at least one of the scrollbars is
8177 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8178 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8179 * this method returns true, and false otherwise. If the animation is
8180 * started, this method calls {@link #invalidate()}; in that case the
8181 * caller should not call {@link #invalidate()}.</p>
8182 *
8183 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008184 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008185 *
8186 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8187 * and {@link #scrollTo(int, int)}.</p>
8188 *
8189 * @return true if the animation is played, false otherwise
8190 *
8191 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008192 * @see #scrollBy(int, int)
8193 * @see #scrollTo(int, int)
8194 * @see #isHorizontalScrollBarEnabled()
8195 * @see #isVerticalScrollBarEnabled()
8196 * @see #setHorizontalScrollBarEnabled(boolean)
8197 * @see #setVerticalScrollBarEnabled(boolean)
8198 */
8199 protected boolean awakenScrollBars() {
8200 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008201 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008202 }
8203
8204 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008205 * Trigger the scrollbars to draw.
8206 * This method differs from awakenScrollBars() only in its default duration.
8207 * initialAwakenScrollBars() will show the scroll bars for longer than
8208 * usual to give the user more of a chance to notice them.
8209 *
8210 * @return true if the animation is played, false otherwise.
8211 */
8212 private boolean initialAwakenScrollBars() {
8213 return mScrollCache != null &&
8214 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8215 }
8216
8217 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008218 * <p>
8219 * Trigger the scrollbars to draw. When invoked this method starts an
8220 * animation to fade the scrollbars out after a fixed delay. If a subclass
8221 * provides animated scrolling, the start delay should equal the duration of
8222 * the scrolling animation.
8223 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008224 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008225 * <p>
8226 * The animation starts only if at least one of the scrollbars is enabled,
8227 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8228 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8229 * this method returns true, and false otherwise. If the animation is
8230 * started, this method calls {@link #invalidate()}; in that case the caller
8231 * should not call {@link #invalidate()}.
8232 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008233 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008234 * <p>
8235 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008236 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008237 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008238 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008239 * @param startDelay the delay, in milliseconds, after which the animation
8240 * should start; when the delay is 0, the animation starts
8241 * immediately
8242 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008243 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008244 * @see #scrollBy(int, int)
8245 * @see #scrollTo(int, int)
8246 * @see #isHorizontalScrollBarEnabled()
8247 * @see #isVerticalScrollBarEnabled()
8248 * @see #setHorizontalScrollBarEnabled(boolean)
8249 * @see #setVerticalScrollBarEnabled(boolean)
8250 */
8251 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008252 return awakenScrollBars(startDelay, true);
8253 }
Joe Malin32736f02011-01-19 16:14:20 -08008254
Mike Cleron290947b2009-09-29 18:34:32 -07008255 /**
8256 * <p>
8257 * Trigger the scrollbars to draw. When invoked this method starts an
8258 * animation to fade the scrollbars out after a fixed delay. If a subclass
8259 * provides animated scrolling, the start delay should equal the duration of
8260 * the scrolling animation.
8261 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008262 *
Mike Cleron290947b2009-09-29 18:34:32 -07008263 * <p>
8264 * The animation starts only if at least one of the scrollbars is enabled,
8265 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8266 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8267 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008268 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008269 * is set to true; in that case the caller
8270 * should not call {@link #invalidate()}.
8271 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008272 *
Mike Cleron290947b2009-09-29 18:34:32 -07008273 * <p>
8274 * This method should be invoked everytime a subclass directly updates the
8275 * scroll parameters.
8276 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008277 *
Mike Cleron290947b2009-09-29 18:34:32 -07008278 * @param startDelay the delay, in milliseconds, after which the animation
8279 * should start; when the delay is 0, the animation starts
8280 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008281 *
Mike Cleron290947b2009-09-29 18:34:32 -07008282 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008283 *
Mike Cleron290947b2009-09-29 18:34:32 -07008284 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008285 *
Mike Cleron290947b2009-09-29 18:34:32 -07008286 * @see #scrollBy(int, int)
8287 * @see #scrollTo(int, int)
8288 * @see #isHorizontalScrollBarEnabled()
8289 * @see #isVerticalScrollBarEnabled()
8290 * @see #setHorizontalScrollBarEnabled(boolean)
8291 * @see #setVerticalScrollBarEnabled(boolean)
8292 */
8293 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008294 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008295
Mike Cleronf116bf82009-09-27 19:14:12 -07008296 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8297 return false;
8298 }
8299
8300 if (scrollCache.scrollBar == null) {
8301 scrollCache.scrollBar = new ScrollBarDrawable();
8302 }
8303
8304 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8305
Mike Cleron290947b2009-09-29 18:34:32 -07008306 if (invalidate) {
8307 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008308 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008309 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008310
8311 if (scrollCache.state == ScrollabilityCache.OFF) {
8312 // FIXME: this is copied from WindowManagerService.
8313 // We should get this value from the system when it
8314 // is possible to do so.
8315 final int KEY_REPEAT_FIRST_DELAY = 750;
8316 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8317 }
8318
8319 // Tell mScrollCache when we should start fading. This may
8320 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008321 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008322 scrollCache.fadeStartTime = fadeStartTime;
8323 scrollCache.state = ScrollabilityCache.ON;
8324
8325 // Schedule our fader to run, unscheduling any old ones first
8326 if (mAttachInfo != null) {
8327 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8328 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8329 }
8330
8331 return true;
8332 }
8333
8334 return false;
8335 }
8336
8337 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008338 * Do not invalidate views which are not visible and which are not running an animation. They
8339 * will not get drawn and they should not set dirty flags as if they will be drawn
8340 */
8341 private boolean skipInvalidate() {
8342 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8343 (!(mParent instanceof ViewGroup) ||
8344 !((ViewGroup) mParent).isViewTransitioning(this));
8345 }
8346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008348 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8349 * in the future. This must be called from a UI thread. To call from a non-UI
8350 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 *
8352 * WARNING: This method is destructive to dirty.
8353 * @param dirty the rectangle representing the bounds of the dirty region
8354 */
8355 public void invalidate(Rect dirty) {
8356 if (ViewDebug.TRACE_HIERARCHY) {
8357 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8358 }
8359
Chet Haaseaceafe62011-08-26 15:44:33 -07008360 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008361 return;
8362 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008363 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008364 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8365 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008367 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008368 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008369 final ViewParent p = mParent;
8370 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008371 //noinspection PointlessBooleanExpression,ConstantConditions
8372 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8373 if (p != null && ai != null && ai.mHardwareAccelerated) {
8374 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008375 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008376 p.invalidateChild(this, null);
8377 return;
8378 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 if (p != null && ai != null) {
8381 final int scrollX = mScrollX;
8382 final int scrollY = mScrollY;
8383 final Rect r = ai.mTmpInvalRect;
8384 r.set(dirty.left - scrollX, dirty.top - scrollY,
8385 dirty.right - scrollX, dirty.bottom - scrollY);
8386 mParent.invalidateChild(this, r);
8387 }
8388 }
8389 }
8390
8391 /**
8392 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8393 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008394 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8395 * will be called at some point in the future. This must be called from
8396 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008397 * @param l the left position of the dirty region
8398 * @param t the top position of the dirty region
8399 * @param r the right position of the dirty region
8400 * @param b the bottom position of the dirty region
8401 */
8402 public void invalidate(int l, int t, int r, int b) {
8403 if (ViewDebug.TRACE_HIERARCHY) {
8404 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8405 }
8406
Chet Haaseaceafe62011-08-26 15:44:33 -07008407 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008408 return;
8409 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008410 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008411 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8412 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008414 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008415 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008416 final ViewParent p = mParent;
8417 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008418 //noinspection PointlessBooleanExpression,ConstantConditions
8419 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8420 if (p != null && ai != null && ai.mHardwareAccelerated) {
8421 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008422 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008423 p.invalidateChild(this, null);
8424 return;
8425 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 if (p != null && ai != null && l < r && t < b) {
8428 final int scrollX = mScrollX;
8429 final int scrollY = mScrollY;
8430 final Rect tmpr = ai.mTmpInvalRect;
8431 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8432 p.invalidateChild(this, tmpr);
8433 }
8434 }
8435 }
8436
8437 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008438 * Invalidate the whole view. If the view is visible,
8439 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8440 * the future. This must be called from a UI thread. To call from a non-UI thread,
8441 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 */
8443 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008444 invalidate(true);
8445 }
Joe Malin32736f02011-01-19 16:14:20 -08008446
Chet Haaseed032702010-10-01 14:05:54 -07008447 /**
8448 * This is where the invalidate() work actually happens. A full invalidate()
8449 * causes the drawing cache to be invalidated, but this function can be called with
8450 * invalidateCache set to false to skip that invalidation step for cases that do not
8451 * need it (for example, a component that remains at the same dimensions with the same
8452 * content).
8453 *
8454 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8455 * well. This is usually true for a full invalidate, but may be set to false if the
8456 * View's contents or dimensions have not changed.
8457 */
Romain Guy849d0a32011-02-01 17:20:48 -08008458 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 if (ViewDebug.TRACE_HIERARCHY) {
8460 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8461 }
8462
Chet Haaseaceafe62011-08-26 15:44:33 -07008463 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008464 return;
8465 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008466 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008467 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008468 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8469 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008470 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008471 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008472 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008473 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008474 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008477 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008478 //noinspection PointlessBooleanExpression,ConstantConditions
8479 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8480 if (p != null && ai != null && ai.mHardwareAccelerated) {
8481 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008482 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008483 p.invalidateChild(this, null);
8484 return;
8485 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008486 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 if (p != null && ai != null) {
8489 final Rect r = ai.mTmpInvalRect;
8490 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8491 // Don't call invalidate -- we don't want to internally scroll
8492 // our own bounds
8493 p.invalidateChild(this, r);
8494 }
8495 }
8496 }
8497
8498 /**
Romain Guyda489792011-02-03 01:05:15 -08008499 * @hide
8500 */
8501 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008502 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008503 return;
8504 }
Romain Guyda489792011-02-03 01:05:15 -08008505 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8506 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8507 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8508 if (mParent instanceof View) {
8509 ((View) mParent).mPrivateFlags |= INVALIDATED;
8510 }
8511 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008512 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008513 mPrivateFlags |= INVALIDATED;
8514 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008515 if (mParent != null && mAttachInfo != null) {
8516 if (mAttachInfo.mHardwareAccelerated) {
8517 mParent.invalidateChild(this, null);
8518 } else {
8519 final Rect r = mAttachInfo.mTmpInvalRect;
8520 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8521 // Don't call invalidate -- we don't want to internally scroll
8522 // our own bounds
8523 mParent.invalidateChild(this, r);
8524 }
Romain Guyda489792011-02-03 01:05:15 -08008525 }
8526 }
8527 }
8528
8529 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008530 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008531 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8532 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008533 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8534 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008535 *
8536 * @hide
8537 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008538 protected void invalidateParentCaches() {
8539 if (mParent instanceof View) {
8540 ((View) mParent).mPrivateFlags |= INVALIDATED;
8541 }
8542 }
Joe Malin32736f02011-01-19 16:14:20 -08008543
Romain Guy0fd89bf2011-01-26 15:41:30 -08008544 /**
8545 * Used to indicate that the parent of this view should be invalidated. This functionality
8546 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8547 * which is necessary when various parent-managed properties of the view change, such as
8548 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8549 * an invalidation event to the parent.
8550 *
8551 * @hide
8552 */
8553 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008554 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008555 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008556 }
8557 }
8558
8559 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008560 * Indicates whether this View is opaque. An opaque View guarantees that it will
8561 * draw all the pixels overlapping its bounds using a fully opaque color.
8562 *
8563 * Subclasses of View should override this method whenever possible to indicate
8564 * whether an instance is opaque. Opaque Views are treated in a special way by
8565 * the View hierarchy, possibly allowing it to perform optimizations during
8566 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008567 *
Romain Guy24443ea2009-05-11 11:56:30 -07008568 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008569 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008570 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008571 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008572 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008573 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8574 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008575 }
8576
Adam Powell20232d02010-12-08 21:08:53 -08008577 /**
8578 * @hide
8579 */
8580 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008581 // Opaque if:
8582 // - Has a background
8583 // - Background is opaque
8584 // - Doesn't have scrollbars or scrollbars are inside overlay
8585
8586 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8587 mPrivateFlags |= OPAQUE_BACKGROUND;
8588 } else {
8589 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8590 }
8591
8592 final int flags = mViewFlags;
8593 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8594 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8595 mPrivateFlags |= OPAQUE_SCROLLBARS;
8596 } else {
8597 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8598 }
8599 }
8600
8601 /**
8602 * @hide
8603 */
8604 protected boolean hasOpaqueScrollbars() {
8605 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008606 }
8607
8608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 * @return A handler associated with the thread running the View. This
8610 * handler can be used to pump events in the UI events queue.
8611 */
8612 public Handler getHandler() {
8613 if (mAttachInfo != null) {
8614 return mAttachInfo.mHandler;
8615 }
8616 return null;
8617 }
8618
8619 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008620 * <p>Causes the Runnable to be added to the message queue.
8621 * The runnable will be run on the user interface thread.</p>
8622 *
8623 * <p>This method can be invoked from outside of the UI thread
8624 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 *
8626 * @param action The Runnable that will be executed.
8627 *
8628 * @return Returns true if the Runnable was successfully placed in to the
8629 * message queue. Returns false on failure, usually because the
8630 * looper processing the message queue is exiting.
8631 */
8632 public boolean post(Runnable action) {
8633 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008634 AttachInfo attachInfo = mAttachInfo;
8635 if (attachInfo != null) {
8636 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 } else {
8638 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008639 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 return true;
8641 }
8642
8643 return handler.post(action);
8644 }
8645
8646 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008647 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008649 * The runnable will be run on the user interface thread.</p>
8650 *
8651 * <p>This method can be invoked from outside of the UI thread
8652 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 *
8654 * @param action The Runnable that will be executed.
8655 * @param delayMillis The delay (in milliseconds) until the Runnable
8656 * will be executed.
8657 *
8658 * @return true if the Runnable was successfully placed in to the
8659 * message queue. Returns false on failure, usually because the
8660 * looper processing the message queue is exiting. Note that a
8661 * result of true does not mean the Runnable will be processed --
8662 * if the looper is quit before the delivery time of the message
8663 * occurs then the message will be dropped.
8664 */
8665 public boolean postDelayed(Runnable action, long delayMillis) {
8666 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008667 AttachInfo attachInfo = mAttachInfo;
8668 if (attachInfo != null) {
8669 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 } else {
8671 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008672 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 return true;
8674 }
8675
8676 return handler.postDelayed(action, delayMillis);
8677 }
8678
8679 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008680 * <p>Removes the specified Runnable from the message queue.</p>
8681 *
8682 * <p>This method can be invoked from outside of the UI thread
8683 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 *
8685 * @param action The Runnable to remove from the message handling queue
8686 *
8687 * @return true if this view could ask the Handler to remove the Runnable,
8688 * false otherwise. When the returned value is true, the Runnable
8689 * may or may not have been actually removed from the message queue
8690 * (for instance, if the Runnable was not in the queue already.)
8691 */
8692 public boolean removeCallbacks(Runnable action) {
8693 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008694 AttachInfo attachInfo = mAttachInfo;
8695 if (attachInfo != null) {
8696 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 } else {
8698 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008699 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008700 return true;
8701 }
8702
8703 handler.removeCallbacks(action);
8704 return true;
8705 }
8706
8707 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008708 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8709 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 *
Romain Guye63a4f32011-08-11 11:33:31 -07008711 * <p>This method can be invoked from outside of the UI thread
8712 * only when this View is attached to a window.</p>
8713 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 * @see #invalidate()
8715 */
8716 public void postInvalidate() {
8717 postInvalidateDelayed(0);
8718 }
8719
8720 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008721 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8722 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8723 *
8724 * <p>This method can be invoked from outside of the UI thread
8725 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 *
8727 * @param left The left coordinate of the rectangle to invalidate.
8728 * @param top The top coordinate of the rectangle to invalidate.
8729 * @param right The right coordinate of the rectangle to invalidate.
8730 * @param bottom The bottom coordinate of the rectangle to invalidate.
8731 *
8732 * @see #invalidate(int, int, int, int)
8733 * @see #invalidate(Rect)
8734 */
8735 public void postInvalidate(int left, int top, int right, int bottom) {
8736 postInvalidateDelayed(0, left, top, right, bottom);
8737 }
8738
8739 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008740 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8741 * loop. Waits for the specified amount of time.</p>
8742 *
8743 * <p>This method can be invoked from outside of the UI thread
8744 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 *
8746 * @param delayMilliseconds the duration in milliseconds to delay the
8747 * invalidation by
8748 */
8749 public void postInvalidateDelayed(long delayMilliseconds) {
8750 // We try only with the AttachInfo because there's no point in invalidating
8751 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008752 AttachInfo attachInfo = mAttachInfo;
8753 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 Message msg = Message.obtain();
8755 msg.what = AttachInfo.INVALIDATE_MSG;
8756 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008757 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 }
8759 }
8760
8761 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008762 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8763 * through the event loop. Waits for the specified amount of time.</p>
8764 *
8765 * <p>This method can be invoked from outside of the UI thread
8766 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008767 *
8768 * @param delayMilliseconds the duration in milliseconds to delay the
8769 * invalidation by
8770 * @param left The left coordinate of the rectangle to invalidate.
8771 * @param top The top coordinate of the rectangle to invalidate.
8772 * @param right The right coordinate of the rectangle to invalidate.
8773 * @param bottom The bottom coordinate of the rectangle to invalidate.
8774 */
8775 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8776 int right, int bottom) {
8777
8778 // We try only with the AttachInfo because there's no point in invalidating
8779 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008780 AttachInfo attachInfo = mAttachInfo;
8781 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8783 info.target = this;
8784 info.left = left;
8785 info.top = top;
8786 info.right = right;
8787 info.bottom = bottom;
8788
8789 final Message msg = Message.obtain();
8790 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8791 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008792 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 }
8794 }
8795
8796 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008797 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8798 * This event is sent at most once every
8799 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8800 */
8801 private void postSendViewScrolledAccessibilityEventCallback() {
8802 if (mSendViewScrolledAccessibilityEvent == null) {
8803 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8804 }
8805 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8806 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8807 postDelayed(mSendViewScrolledAccessibilityEvent,
8808 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8809 }
8810 }
8811
8812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 * Called by a parent to request that a child update its values for mScrollX
8814 * and mScrollY if necessary. This will typically be done if the child is
8815 * animating a scroll using a {@link android.widget.Scroller Scroller}
8816 * object.
8817 */
8818 public void computeScroll() {
8819 }
8820
8821 /**
8822 * <p>Indicate whether the horizontal edges are faded when the view is
8823 * scrolled horizontally.</p>
8824 *
8825 * @return true if the horizontal edges should are faded on scroll, false
8826 * otherwise
8827 *
8828 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008829 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830 */
8831 public boolean isHorizontalFadingEdgeEnabled() {
8832 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8833 }
8834
8835 /**
8836 * <p>Define whether the horizontal edges should be faded when this view
8837 * is scrolled horizontally.</p>
8838 *
8839 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8840 * be faded when the view is scrolled
8841 * horizontally
8842 *
8843 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008844 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008845 */
8846 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8847 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8848 if (horizontalFadingEdgeEnabled) {
8849 initScrollCache();
8850 }
8851
8852 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8853 }
8854 }
8855
8856 /**
8857 * <p>Indicate whether the vertical edges are faded when the view is
8858 * scrolled horizontally.</p>
8859 *
8860 * @return true if the vertical edges should are faded on scroll, false
8861 * otherwise
8862 *
8863 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008864 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 */
8866 public boolean isVerticalFadingEdgeEnabled() {
8867 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8868 }
8869
8870 /**
8871 * <p>Define whether the vertical edges should be faded when this view
8872 * is scrolled vertically.</p>
8873 *
8874 * @param verticalFadingEdgeEnabled true if the vertical edges should
8875 * be faded when the view is scrolled
8876 * vertically
8877 *
8878 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008879 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 */
8881 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8882 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8883 if (verticalFadingEdgeEnabled) {
8884 initScrollCache();
8885 }
8886
8887 mViewFlags ^= FADING_EDGE_VERTICAL;
8888 }
8889 }
8890
8891 /**
8892 * Returns the strength, or intensity, of the top faded edge. The strength is
8893 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8894 * returns 0.0 or 1.0 but no value in between.
8895 *
8896 * Subclasses should override this method to provide a smoother fade transition
8897 * when scrolling occurs.
8898 *
8899 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8900 */
8901 protected float getTopFadingEdgeStrength() {
8902 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8903 }
8904
8905 /**
8906 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8907 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8908 * returns 0.0 or 1.0 but no value in between.
8909 *
8910 * Subclasses should override this method to provide a smoother fade transition
8911 * when scrolling occurs.
8912 *
8913 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8914 */
8915 protected float getBottomFadingEdgeStrength() {
8916 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8917 computeVerticalScrollRange() ? 1.0f : 0.0f;
8918 }
8919
8920 /**
8921 * Returns the strength, or intensity, of the left faded edge. The strength is
8922 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8923 * returns 0.0 or 1.0 but no value in between.
8924 *
8925 * Subclasses should override this method to provide a smoother fade transition
8926 * when scrolling occurs.
8927 *
8928 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8929 */
8930 protected float getLeftFadingEdgeStrength() {
8931 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8932 }
8933
8934 /**
8935 * Returns the strength, or intensity, of the right faded edge. The strength is
8936 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8937 * returns 0.0 or 1.0 but no value in between.
8938 *
8939 * Subclasses should override this method to provide a smoother fade transition
8940 * when scrolling occurs.
8941 *
8942 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8943 */
8944 protected float getRightFadingEdgeStrength() {
8945 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8946 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8947 }
8948
8949 /**
8950 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8951 * scrollbar is not drawn by default.</p>
8952 *
8953 * @return true if the horizontal scrollbar should be painted, false
8954 * otherwise
8955 *
8956 * @see #setHorizontalScrollBarEnabled(boolean)
8957 */
8958 public boolean isHorizontalScrollBarEnabled() {
8959 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8960 }
8961
8962 /**
8963 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8964 * scrollbar is not drawn by default.</p>
8965 *
8966 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8967 * be painted
8968 *
8969 * @see #isHorizontalScrollBarEnabled()
8970 */
8971 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8972 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8973 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008974 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008975 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008976 }
8977 }
8978
8979 /**
8980 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8981 * scrollbar is not drawn by default.</p>
8982 *
8983 * @return true if the vertical scrollbar should be painted, false
8984 * otherwise
8985 *
8986 * @see #setVerticalScrollBarEnabled(boolean)
8987 */
8988 public boolean isVerticalScrollBarEnabled() {
8989 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8990 }
8991
8992 /**
8993 * <p>Define whether the vertical scrollbar should be drawn or not. The
8994 * scrollbar is not drawn by default.</p>
8995 *
8996 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8997 * be painted
8998 *
8999 * @see #isVerticalScrollBarEnabled()
9000 */
9001 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9002 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9003 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009004 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009005 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 }
9007 }
9008
Adam Powell20232d02010-12-08 21:08:53 -08009009 /**
9010 * @hide
9011 */
9012 protected void recomputePadding() {
9013 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009014 }
Joe Malin32736f02011-01-19 16:14:20 -08009015
Mike Cleronfe81d382009-09-28 14:22:16 -07009016 /**
9017 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009018 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009019 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009020 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009021 */
9022 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9023 initScrollCache();
9024 final ScrollabilityCache scrollabilityCache = mScrollCache;
9025 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009026 if (fadeScrollbars) {
9027 scrollabilityCache.state = ScrollabilityCache.OFF;
9028 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009029 scrollabilityCache.state = ScrollabilityCache.ON;
9030 }
9031 }
Joe Malin32736f02011-01-19 16:14:20 -08009032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009033 /**
Joe Malin32736f02011-01-19 16:14:20 -08009034 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009035 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009036 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009037 * @return true if scrollbar fading is enabled
9038 */
9039 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009040 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009041 }
Joe Malin32736f02011-01-19 16:14:20 -08009042
Mike Cleron52f0a642009-09-28 18:21:37 -07009043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9045 * inset. When inset, they add to the padding of the view. And the scrollbars
9046 * can be drawn inside the padding area or on the edge of the view. For example,
9047 * if a view has a background drawable and you want to draw the scrollbars
9048 * inside the padding specified by the drawable, you can use
9049 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9050 * appear at the edge of the view, ignoring the padding, then you can use
9051 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9052 * @param style the style of the scrollbars. Should be one of
9053 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9054 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9055 * @see #SCROLLBARS_INSIDE_OVERLAY
9056 * @see #SCROLLBARS_INSIDE_INSET
9057 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9058 * @see #SCROLLBARS_OUTSIDE_INSET
9059 */
9060 public void setScrollBarStyle(int style) {
9061 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9062 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009063 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009064 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009065 }
9066 }
9067
9068 /**
9069 * <p>Returns the current scrollbar style.</p>
9070 * @return the current scrollbar style
9071 * @see #SCROLLBARS_INSIDE_OVERLAY
9072 * @see #SCROLLBARS_INSIDE_INSET
9073 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9074 * @see #SCROLLBARS_OUTSIDE_INSET
9075 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009076 @ViewDebug.ExportedProperty(mapping = {
9077 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9078 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9079 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9080 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9081 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 public int getScrollBarStyle() {
9083 return mViewFlags & SCROLLBARS_STYLE_MASK;
9084 }
9085
9086 /**
9087 * <p>Compute the horizontal range that the horizontal scrollbar
9088 * represents.</p>
9089 *
9090 * <p>The range is expressed in arbitrary units that must be the same as the
9091 * units used by {@link #computeHorizontalScrollExtent()} and
9092 * {@link #computeHorizontalScrollOffset()}.</p>
9093 *
9094 * <p>The default range is the drawing width of this view.</p>
9095 *
9096 * @return the total horizontal range represented by the horizontal
9097 * scrollbar
9098 *
9099 * @see #computeHorizontalScrollExtent()
9100 * @see #computeHorizontalScrollOffset()
9101 * @see android.widget.ScrollBarDrawable
9102 */
9103 protected int computeHorizontalScrollRange() {
9104 return getWidth();
9105 }
9106
9107 /**
9108 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9109 * within the horizontal range. This value is used to compute the position
9110 * of the thumb within the scrollbar's track.</p>
9111 *
9112 * <p>The range is expressed in arbitrary units that must be the same as the
9113 * units used by {@link #computeHorizontalScrollRange()} and
9114 * {@link #computeHorizontalScrollExtent()}.</p>
9115 *
9116 * <p>The default offset is the scroll offset of this view.</p>
9117 *
9118 * @return the horizontal offset of the scrollbar's thumb
9119 *
9120 * @see #computeHorizontalScrollRange()
9121 * @see #computeHorizontalScrollExtent()
9122 * @see android.widget.ScrollBarDrawable
9123 */
9124 protected int computeHorizontalScrollOffset() {
9125 return mScrollX;
9126 }
9127
9128 /**
9129 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9130 * within the horizontal range. This value is used to compute the length
9131 * of the thumb within the scrollbar's track.</p>
9132 *
9133 * <p>The range is expressed in arbitrary units that must be the same as the
9134 * units used by {@link #computeHorizontalScrollRange()} and
9135 * {@link #computeHorizontalScrollOffset()}.</p>
9136 *
9137 * <p>The default extent is the drawing width of this view.</p>
9138 *
9139 * @return the horizontal extent of the scrollbar's thumb
9140 *
9141 * @see #computeHorizontalScrollRange()
9142 * @see #computeHorizontalScrollOffset()
9143 * @see android.widget.ScrollBarDrawable
9144 */
9145 protected int computeHorizontalScrollExtent() {
9146 return getWidth();
9147 }
9148
9149 /**
9150 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9151 *
9152 * <p>The range is expressed in arbitrary units that must be the same as the
9153 * units used by {@link #computeVerticalScrollExtent()} and
9154 * {@link #computeVerticalScrollOffset()}.</p>
9155 *
9156 * @return the total vertical range represented by the vertical scrollbar
9157 *
9158 * <p>The default range is the drawing height of this view.</p>
9159 *
9160 * @see #computeVerticalScrollExtent()
9161 * @see #computeVerticalScrollOffset()
9162 * @see android.widget.ScrollBarDrawable
9163 */
9164 protected int computeVerticalScrollRange() {
9165 return getHeight();
9166 }
9167
9168 /**
9169 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9170 * within the horizontal range. This value is used to compute the position
9171 * of the thumb within the scrollbar's track.</p>
9172 *
9173 * <p>The range is expressed in arbitrary units that must be the same as the
9174 * units used by {@link #computeVerticalScrollRange()} and
9175 * {@link #computeVerticalScrollExtent()}.</p>
9176 *
9177 * <p>The default offset is the scroll offset of this view.</p>
9178 *
9179 * @return the vertical offset of the scrollbar's thumb
9180 *
9181 * @see #computeVerticalScrollRange()
9182 * @see #computeVerticalScrollExtent()
9183 * @see android.widget.ScrollBarDrawable
9184 */
9185 protected int computeVerticalScrollOffset() {
9186 return mScrollY;
9187 }
9188
9189 /**
9190 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9191 * within the vertical range. This value is used to compute the length
9192 * of the thumb within the scrollbar's track.</p>
9193 *
9194 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009195 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 * {@link #computeVerticalScrollOffset()}.</p>
9197 *
9198 * <p>The default extent is the drawing height of this view.</p>
9199 *
9200 * @return the vertical extent of the scrollbar's thumb
9201 *
9202 * @see #computeVerticalScrollRange()
9203 * @see #computeVerticalScrollOffset()
9204 * @see android.widget.ScrollBarDrawable
9205 */
9206 protected int computeVerticalScrollExtent() {
9207 return getHeight();
9208 }
9209
9210 /**
Adam Powell69159442011-06-13 17:53:06 -07009211 * Check if this view can be scrolled horizontally in a certain direction.
9212 *
9213 * @param direction Negative to check scrolling left, positive to check scrolling right.
9214 * @return true if this view can be scrolled in the specified direction, false otherwise.
9215 */
9216 public boolean canScrollHorizontally(int direction) {
9217 final int offset = computeHorizontalScrollOffset();
9218 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9219 if (range == 0) return false;
9220 if (direction < 0) {
9221 return offset > 0;
9222 } else {
9223 return offset < range - 1;
9224 }
9225 }
9226
9227 /**
9228 * Check if this view can be scrolled vertically in a certain direction.
9229 *
9230 * @param direction Negative to check scrolling up, positive to check scrolling down.
9231 * @return true if this view can be scrolled in the specified direction, false otherwise.
9232 */
9233 public boolean canScrollVertically(int direction) {
9234 final int offset = computeVerticalScrollOffset();
9235 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9236 if (range == 0) return false;
9237 if (direction < 0) {
9238 return offset > 0;
9239 } else {
9240 return offset < range - 1;
9241 }
9242 }
9243
9244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9246 * scrollbars are painted only if they have been awakened first.</p>
9247 *
9248 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009249 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009250 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009252 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 // scrollbars are drawn only when the animation is running
9254 final ScrollabilityCache cache = mScrollCache;
9255 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009256
Mike Cleronf116bf82009-09-27 19:14:12 -07009257 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009258
Mike Cleronf116bf82009-09-27 19:14:12 -07009259 if (state == ScrollabilityCache.OFF) {
9260 return;
9261 }
Joe Malin32736f02011-01-19 16:14:20 -08009262
Mike Cleronf116bf82009-09-27 19:14:12 -07009263 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009264
Mike Cleronf116bf82009-09-27 19:14:12 -07009265 if (state == ScrollabilityCache.FADING) {
9266 // We're fading -- get our fade interpolation
9267 if (cache.interpolatorValues == null) {
9268 cache.interpolatorValues = new float[1];
9269 }
Joe Malin32736f02011-01-19 16:14:20 -08009270
Mike Cleronf116bf82009-09-27 19:14:12 -07009271 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009272
Mike Cleronf116bf82009-09-27 19:14:12 -07009273 // Stops the animation if we're done
9274 if (cache.scrollBarInterpolator.timeToValues(values) ==
9275 Interpolator.Result.FREEZE_END) {
9276 cache.state = ScrollabilityCache.OFF;
9277 } else {
9278 cache.scrollBar.setAlpha(Math.round(values[0]));
9279 }
Joe Malin32736f02011-01-19 16:14:20 -08009280
9281 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009282 // drawing. We only want this when we're fading so that
9283 // we prevent excessive redraws
9284 invalidate = true;
9285 } else {
9286 // We're just on -- but we may have been fading before so
9287 // reset alpha
9288 cache.scrollBar.setAlpha(255);
9289 }
9290
Joe Malin32736f02011-01-19 16:14:20 -08009291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009292 final int viewFlags = mViewFlags;
9293
9294 final boolean drawHorizontalScrollBar =
9295 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9296 final boolean drawVerticalScrollBar =
9297 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9298 && !isVerticalScrollBarHidden();
9299
9300 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9301 final int width = mRight - mLeft;
9302 final int height = mBottom - mTop;
9303
9304 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305
Mike Reede8853fc2009-09-04 14:01:48 -04009306 final int scrollX = mScrollX;
9307 final int scrollY = mScrollY;
9308 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9309
Mike Cleronf116bf82009-09-27 19:14:12 -07009310 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009313 int size = scrollBar.getSize(false);
9314 if (size <= 0) {
9315 size = cache.scrollBarSize;
9316 }
9317
Mike Cleronf116bf82009-09-27 19:14:12 -07009318 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009319 computeHorizontalScrollOffset(),
9320 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009321 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009322 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009323 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009324 left = scrollX + (mPaddingLeft & inside);
9325 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9326 bottom = top + size;
9327 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9328 if (invalidate) {
9329 invalidate(left, top, right, bottom);
9330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 }
9332
9333 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009334 int size = scrollBar.getSize(true);
9335 if (size <= 0) {
9336 size = cache.scrollBarSize;
9337 }
9338
Mike Reede8853fc2009-09-04 14:01:48 -04009339 scrollBar.setParameters(computeVerticalScrollRange(),
9340 computeVerticalScrollOffset(),
9341 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009342 switch (mVerticalScrollbarPosition) {
9343 default:
9344 case SCROLLBAR_POSITION_DEFAULT:
9345 case SCROLLBAR_POSITION_RIGHT:
9346 left = scrollX + width - size - (mUserPaddingRight & inside);
9347 break;
9348 case SCROLLBAR_POSITION_LEFT:
9349 left = scrollX + (mUserPaddingLeft & inside);
9350 break;
9351 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009352 top = scrollY + (mPaddingTop & inside);
9353 right = left + size;
9354 bottom = scrollY + height - (mUserPaddingBottom & inside);
9355 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9356 if (invalidate) {
9357 invalidate(left, top, right, bottom);
9358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 }
9360 }
9361 }
9362 }
Romain Guy8506ab42009-06-11 17:35:47 -07009363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009365 * 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 -08009366 * FastScroller is visible.
9367 * @return whether to temporarily hide the vertical scrollbar
9368 * @hide
9369 */
9370 protected boolean isVerticalScrollBarHidden() {
9371 return false;
9372 }
9373
9374 /**
9375 * <p>Draw the horizontal scrollbar if
9376 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9377 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009378 * @param canvas the canvas on which to draw the scrollbar
9379 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 *
9381 * @see #isHorizontalScrollBarEnabled()
9382 * @see #computeHorizontalScrollRange()
9383 * @see #computeHorizontalScrollExtent()
9384 * @see #computeHorizontalScrollOffset()
9385 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009386 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009387 */
Romain Guy8fb95422010-08-17 18:38:51 -07009388 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9389 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009390 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009391 scrollBar.draw(canvas);
9392 }
Mike Reede8853fc2009-09-04 14:01:48 -04009393
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9396 * returns true.</p>
9397 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 * @param canvas the canvas on which to draw the scrollbar
9399 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009400 *
9401 * @see #isVerticalScrollBarEnabled()
9402 * @see #computeVerticalScrollRange()
9403 * @see #computeVerticalScrollExtent()
9404 * @see #computeVerticalScrollOffset()
9405 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009406 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 */
Romain Guy8fb95422010-08-17 18:38:51 -07009408 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9409 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009410 scrollBar.setBounds(l, t, r, b);
9411 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009412 }
9413
9414 /**
9415 * Implement this to do your drawing.
9416 *
9417 * @param canvas the canvas on which the background will be drawn
9418 */
9419 protected void onDraw(Canvas canvas) {
9420 }
9421
9422 /*
9423 * Caller is responsible for calling requestLayout if necessary.
9424 * (This allows addViewInLayout to not request a new layout.)
9425 */
9426 void assignParent(ViewParent parent) {
9427 if (mParent == null) {
9428 mParent = parent;
9429 } else if (parent == null) {
9430 mParent = null;
9431 } else {
9432 throw new RuntimeException("view " + this + " being added, but"
9433 + " it already has a parent");
9434 }
9435 }
9436
9437 /**
9438 * This is called when the view is attached to a window. At this point it
9439 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009440 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9441 * however it may be called any time before the first onDraw -- including
9442 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009443 *
9444 * @see #onDetachedFromWindow()
9445 */
9446 protected void onAttachedToWindow() {
9447 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9448 mParent.requestTransparentRegion(this);
9449 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009450 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9451 initialAwakenScrollBars();
9452 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9453 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009454 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009455 // Order is important here: LayoutDirection MUST be resolved before Padding
9456 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009457 resolveLayoutDirectionIfNeeded();
9458 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009459 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009460 if (isFocused()) {
9461 InputMethodManager imm = InputMethodManager.peekInstance();
9462 imm.focusIn(this);
9463 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009464 }
Cibu Johny86666632010-02-22 13:01:02 -08009465
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009466 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009467 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9468 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009469 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009470 private void resolveLayoutDirectionIfNeeded() {
9471 // Do not resolve if it is not needed
9472 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9473
9474 // Clear any previous layout direction resolution
9475 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9476
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009477 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9478 // TextDirectionHeuristic
9479 resetResolvedTextDirection();
9480
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009481 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009482 switch (getLayoutDirection()) {
9483 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009484 // We cannot do the resolution if there is no parent
9485 if (mParent == null) return;
9486
Cibu Johny86666632010-02-22 13:01:02 -08009487 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009488 if (mParent instanceof ViewGroup) {
9489 ViewGroup viewGroup = ((ViewGroup) mParent);
9490
9491 // Check if the parent view group can resolve
9492 if (! viewGroup.canResolveLayoutDirection()) {
9493 return;
9494 }
9495 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9496 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9497 }
Cibu Johny86666632010-02-22 13:01:02 -08009498 }
9499 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009500 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009501 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009502 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009503 case LAYOUT_DIRECTION_LOCALE:
9504 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009505 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009506 }
9507 break;
9508 default:
9509 // Nothing to do, LTR by default
9510 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009511
9512 // Set to resolved
9513 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9514 }
9515
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009516 /**
9517 * @hide
9518 */
9519 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009520 // If the user specified the absolute padding (either with android:padding or
9521 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9522 // use the default padding or the padding from the background drawable
9523 // (stored at this point in mPadding*)
9524 switch (getResolvedLayoutDirection()) {
9525 case LAYOUT_DIRECTION_RTL:
9526 // Start user padding override Right user padding. Otherwise, if Right user
9527 // padding is not defined, use the default Right padding. If Right user padding
9528 // is defined, just use it.
9529 if (mUserPaddingStart >= 0) {
9530 mUserPaddingRight = mUserPaddingStart;
9531 } else if (mUserPaddingRight < 0) {
9532 mUserPaddingRight = mPaddingRight;
9533 }
9534 if (mUserPaddingEnd >= 0) {
9535 mUserPaddingLeft = mUserPaddingEnd;
9536 } else if (mUserPaddingLeft < 0) {
9537 mUserPaddingLeft = mPaddingLeft;
9538 }
9539 break;
9540 case LAYOUT_DIRECTION_LTR:
9541 default:
9542 // Start user padding override Left user padding. Otherwise, if Left user
9543 // padding is not defined, use the default left padding. If Left user padding
9544 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009545 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009546 mUserPaddingLeft = mUserPaddingStart;
9547 } else if (mUserPaddingLeft < 0) {
9548 mUserPaddingLeft = mPaddingLeft;
9549 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009550 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009551 mUserPaddingRight = mUserPaddingEnd;
9552 } else if (mUserPaddingRight < 0) {
9553 mUserPaddingRight = mPaddingRight;
9554 }
9555 }
9556
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009557 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9558
9559 recomputePadding();
9560 }
9561
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009562 /**
9563 * Return true if layout direction resolution can be done
9564 *
9565 * @hide
9566 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009567 protected boolean canResolveLayoutDirection() {
9568 switch (getLayoutDirection()) {
9569 case LAYOUT_DIRECTION_INHERIT:
9570 return (mParent != null);
9571 default:
9572 return true;
9573 }
9574 }
9575
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009576 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009577 * Reset the resolved layout direction.
9578 *
9579 * Subclasses need to override this method to clear cached information that depends on the
9580 * resolved layout direction, or to inform child views that inherit their layout direction.
9581 * Overrides must also call the superclass implementation at the start of their implementation.
9582 *
9583 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009584 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009585 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009586 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009587 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009588 }
9589
9590 /**
9591 * Check if a Locale is corresponding to a RTL script.
9592 *
9593 * @param locale Locale to check
9594 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009595 *
9596 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009597 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009598 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009599 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9600 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601 }
9602
9603 /**
9604 * This is called when the view is detached from a window. At this point it
9605 * no longer has a surface for drawing.
9606 *
9607 * @see #onAttachedToWindow()
9608 */
9609 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009610 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009611
Romain Guya440b002010-02-24 15:57:54 -08009612 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009613 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009614 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009615 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009618
Romain Guy6d7475d2011-07-27 16:28:21 -07009619 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009620
Chet Haasedaf98e92011-01-10 14:10:36 -08009621 if (mDisplayList != null) {
9622 mDisplayList.invalidate();
9623 }
9624
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009625 if (mAttachInfo != null) {
9626 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009627 }
9628
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009629 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009630
9631 resetResolvedLayoutDirection();
9632 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 }
9634
9635 /**
9636 * @return The number of times this view has been attached to a window
9637 */
9638 protected int getWindowAttachCount() {
9639 return mWindowAttachCount;
9640 }
9641
9642 /**
9643 * Retrieve a unique token identifying the window this view is attached to.
9644 * @return Return the window's token for use in
9645 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9646 */
9647 public IBinder getWindowToken() {
9648 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9649 }
9650
9651 /**
9652 * Retrieve a unique token identifying the top-level "real" window of
9653 * the window that this view is attached to. That is, this is like
9654 * {@link #getWindowToken}, except if the window this view in is a panel
9655 * window (attached to another containing window), then the token of
9656 * the containing window is returned instead.
9657 *
9658 * @return Returns the associated window token, either
9659 * {@link #getWindowToken()} or the containing window's token.
9660 */
9661 public IBinder getApplicationWindowToken() {
9662 AttachInfo ai = mAttachInfo;
9663 if (ai != null) {
9664 IBinder appWindowToken = ai.mPanelParentWindowToken;
9665 if (appWindowToken == null) {
9666 appWindowToken = ai.mWindowToken;
9667 }
9668 return appWindowToken;
9669 }
9670 return null;
9671 }
9672
9673 /**
9674 * Retrieve private session object this view hierarchy is using to
9675 * communicate with the window manager.
9676 * @return the session object to communicate with the window manager
9677 */
9678 /*package*/ IWindowSession getWindowSession() {
9679 return mAttachInfo != null ? mAttachInfo.mSession : null;
9680 }
9681
9682 /**
9683 * @param info the {@link android.view.View.AttachInfo} to associated with
9684 * this view
9685 */
9686 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9687 //System.out.println("Attached! " + this);
9688 mAttachInfo = info;
9689 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009690 // We will need to evaluate the drawable state at least once.
9691 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 if (mFloatingTreeObserver != null) {
9693 info.mTreeObserver.merge(mFloatingTreeObserver);
9694 mFloatingTreeObserver = null;
9695 }
9696 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9697 mAttachInfo.mScrollContainers.add(this);
9698 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9699 }
9700 performCollectViewAttributes(visibility);
9701 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009702
9703 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9704 mOnAttachStateChangeListeners;
9705 if (listeners != null && listeners.size() > 0) {
9706 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9707 // perform the dispatching. The iterator is a safe guard against listeners that
9708 // could mutate the list by calling the various add/remove methods. This prevents
9709 // the array from being modified while we iterate it.
9710 for (OnAttachStateChangeListener listener : listeners) {
9711 listener.onViewAttachedToWindow(this);
9712 }
9713 }
9714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 int vis = info.mWindowVisibility;
9716 if (vis != GONE) {
9717 onWindowVisibilityChanged(vis);
9718 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009719 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9720 // If nobody has evaluated the drawable state yet, then do it now.
9721 refreshDrawableState();
9722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 }
9724
9725 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009726 AttachInfo info = mAttachInfo;
9727 if (info != null) {
9728 int vis = info.mWindowVisibility;
9729 if (vis != GONE) {
9730 onWindowVisibilityChanged(GONE);
9731 }
9732 }
9733
9734 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009735
Adam Powell4afd62b2011-02-18 15:02:18 -08009736 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9737 mOnAttachStateChangeListeners;
9738 if (listeners != null && listeners.size() > 0) {
9739 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9740 // perform the dispatching. The iterator is a safe guard against listeners that
9741 // could mutate the list by calling the various add/remove methods. This prevents
9742 // the array from being modified while we iterate it.
9743 for (OnAttachStateChangeListener listener : listeners) {
9744 listener.onViewDetachedFromWindow(this);
9745 }
9746 }
9747
Romain Guy01d5edc2011-01-28 11:28:53 -08009748 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 mAttachInfo.mScrollContainers.remove(this);
9750 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9751 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753 mAttachInfo = null;
9754 }
9755
9756 /**
9757 * Store this view hierarchy's frozen state into the given container.
9758 *
9759 * @param container The SparseArray in which to save the view's state.
9760 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009761 * @see #restoreHierarchyState(android.util.SparseArray)
9762 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9763 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 */
9765 public void saveHierarchyState(SparseArray<Parcelable> container) {
9766 dispatchSaveInstanceState(container);
9767 }
9768
9769 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009770 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9771 * this view and its children. May be overridden to modify how freezing happens to a
9772 * 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 -08009773 *
9774 * @param container The SparseArray in which to save the view's state.
9775 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009776 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9777 * @see #saveHierarchyState(android.util.SparseArray)
9778 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 */
9780 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9781 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9782 mPrivateFlags &= ~SAVE_STATE_CALLED;
9783 Parcelable state = onSaveInstanceState();
9784 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9785 throw new IllegalStateException(
9786 "Derived class did not call super.onSaveInstanceState()");
9787 }
9788 if (state != null) {
9789 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9790 // + ": " + state);
9791 container.put(mID, state);
9792 }
9793 }
9794 }
9795
9796 /**
9797 * Hook allowing a view to generate a representation of its internal state
9798 * that can later be used to create a new instance with that same state.
9799 * This state should only contain information that is not persistent or can
9800 * not be reconstructed later. For example, you will never store your
9801 * current position on screen because that will be computed again when a
9802 * new instance of the view is placed in its view hierarchy.
9803 * <p>
9804 * Some examples of things you may store here: the current cursor position
9805 * in a text view (but usually not the text itself since that is stored in a
9806 * content provider or other persistent storage), the currently selected
9807 * item in a list view.
9808 *
9809 * @return Returns a Parcelable object containing the view's current dynamic
9810 * state, or null if there is nothing interesting to save. The
9811 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009812 * @see #onRestoreInstanceState(android.os.Parcelable)
9813 * @see #saveHierarchyState(android.util.SparseArray)
9814 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 * @see #setSaveEnabled(boolean)
9816 */
9817 protected Parcelable onSaveInstanceState() {
9818 mPrivateFlags |= SAVE_STATE_CALLED;
9819 return BaseSavedState.EMPTY_STATE;
9820 }
9821
9822 /**
9823 * Restore this view hierarchy's frozen state from the given container.
9824 *
9825 * @param container The SparseArray which holds previously frozen states.
9826 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009827 * @see #saveHierarchyState(android.util.SparseArray)
9828 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9829 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 */
9831 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9832 dispatchRestoreInstanceState(container);
9833 }
9834
9835 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009836 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9837 * state for this view and its children. May be overridden to modify how restoring
9838 * happens to a view's children; for example, some views may want to not store state
9839 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 *
9841 * @param container The SparseArray which holds previously saved state.
9842 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009843 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9844 * @see #restoreHierarchyState(android.util.SparseArray)
9845 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 */
9847 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9848 if (mID != NO_ID) {
9849 Parcelable state = container.get(mID);
9850 if (state != null) {
9851 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9852 // + ": " + state);
9853 mPrivateFlags &= ~SAVE_STATE_CALLED;
9854 onRestoreInstanceState(state);
9855 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9856 throw new IllegalStateException(
9857 "Derived class did not call super.onRestoreInstanceState()");
9858 }
9859 }
9860 }
9861 }
9862
9863 /**
9864 * Hook allowing a view to re-apply a representation of its internal state that had previously
9865 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9866 * null state.
9867 *
9868 * @param state The frozen state that had previously been returned by
9869 * {@link #onSaveInstanceState}.
9870 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009871 * @see #onSaveInstanceState()
9872 * @see #restoreHierarchyState(android.util.SparseArray)
9873 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 */
9875 protected void onRestoreInstanceState(Parcelable state) {
9876 mPrivateFlags |= SAVE_STATE_CALLED;
9877 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009878 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9879 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009880 + "when two views of different type have the same id in the same hierarchy. "
9881 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009882 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 }
9884 }
9885
9886 /**
9887 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9888 *
9889 * @return the drawing start time in milliseconds
9890 */
9891 public long getDrawingTime() {
9892 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9893 }
9894
9895 /**
9896 * <p>Enables or disables the duplication of the parent's state into this view. When
9897 * duplication is enabled, this view gets its drawable state from its parent rather
9898 * than from its own internal properties.</p>
9899 *
9900 * <p>Note: in the current implementation, setting this property to true after the
9901 * view was added to a ViewGroup might have no effect at all. This property should
9902 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9903 *
9904 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9905 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009906 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009907 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9908 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 *
9910 * @param enabled True to enable duplication of the parent's drawable state, false
9911 * to disable it.
9912 *
9913 * @see #getDrawableState()
9914 * @see #isDuplicateParentStateEnabled()
9915 */
9916 public void setDuplicateParentStateEnabled(boolean enabled) {
9917 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9918 }
9919
9920 /**
9921 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9922 *
9923 * @return True if this view's drawable state is duplicated from the parent,
9924 * false otherwise
9925 *
9926 * @see #getDrawableState()
9927 * @see #setDuplicateParentStateEnabled(boolean)
9928 */
9929 public boolean isDuplicateParentStateEnabled() {
9930 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9931 }
9932
9933 /**
Romain Guy171c5922011-01-06 10:04:23 -08009934 * <p>Specifies the type of layer backing this view. The layer can be
9935 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9936 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009937 *
Romain Guy171c5922011-01-06 10:04:23 -08009938 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9939 * instance that controls how the layer is composed on screen. The following
9940 * properties of the paint are taken into account when composing the layer:</p>
9941 * <ul>
9942 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9943 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9944 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9945 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009946 *
Romain Guy171c5922011-01-06 10:04:23 -08009947 * <p>If this view has an alpha value set to < 1.0 by calling
9948 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9949 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9950 * equivalent to setting a hardware layer on this view and providing a paint with
9951 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009952 *
Romain Guy171c5922011-01-06 10:04:23 -08009953 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9954 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9955 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009956 *
Romain Guy171c5922011-01-06 10:04:23 -08009957 * @param layerType The ype of layer to use with this view, must be one of
9958 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9959 * {@link #LAYER_TYPE_HARDWARE}
9960 * @param paint The paint used to compose the layer. This argument is optional
9961 * and can be null. It is ignored when the layer type is
9962 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009963 *
9964 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009965 * @see #LAYER_TYPE_NONE
9966 * @see #LAYER_TYPE_SOFTWARE
9967 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009968 * @see #setAlpha(float)
9969 *
Romain Guy171c5922011-01-06 10:04:23 -08009970 * @attr ref android.R.styleable#View_layerType
9971 */
9972 public void setLayerType(int layerType, Paint paint) {
9973 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009974 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009975 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9976 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009977
Romain Guyd6cd5722011-01-17 14:42:41 -08009978 if (layerType == mLayerType) {
9979 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9980 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009981 invalidateParentCaches();
9982 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009983 }
9984 return;
9985 }
Romain Guy171c5922011-01-06 10:04:23 -08009986
9987 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009988 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009989 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009990 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009991 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009992 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009993 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009994 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009995 default:
9996 break;
Romain Guy171c5922011-01-06 10:04:23 -08009997 }
9998
9999 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010000 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10001 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10002 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010003
Romain Guy0fd89bf2011-01-26 15:41:30 -080010004 invalidateParentCaches();
10005 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010006 }
10007
10008 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010009 * Indicates whether this view has a static layer. A view with layer type
10010 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10011 * dynamic.
10012 */
10013 boolean hasStaticLayer() {
10014 return mLayerType == LAYER_TYPE_NONE;
10015 }
10016
10017 /**
Romain Guy171c5922011-01-06 10:04:23 -080010018 * Indicates what type of layer is currently associated with this view. By default
10019 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10020 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10021 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010022 *
Romain Guy171c5922011-01-06 10:04:23 -080010023 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10024 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010025 *
10026 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010027 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010028 * @see #LAYER_TYPE_NONE
10029 * @see #LAYER_TYPE_SOFTWARE
10030 * @see #LAYER_TYPE_HARDWARE
10031 */
10032 public int getLayerType() {
10033 return mLayerType;
10034 }
Joe Malin32736f02011-01-19 16:14:20 -080010035
Romain Guy6c319ca2011-01-11 14:29:25 -080010036 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010037 * Forces this view's layer to be created and this view to be rendered
10038 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10039 * invoking this method will have no effect.
10040 *
10041 * This method can for instance be used to render a view into its layer before
10042 * starting an animation. If this view is complex, rendering into the layer
10043 * before starting the animation will avoid skipping frames.
10044 *
10045 * @throws IllegalStateException If this view is not attached to a window
10046 *
10047 * @see #setLayerType(int, android.graphics.Paint)
10048 */
10049 public void buildLayer() {
10050 if (mLayerType == LAYER_TYPE_NONE) return;
10051
10052 if (mAttachInfo == null) {
10053 throw new IllegalStateException("This view must be attached to a window first");
10054 }
10055
10056 switch (mLayerType) {
10057 case LAYER_TYPE_HARDWARE:
10058 getHardwareLayer();
10059 break;
10060 case LAYER_TYPE_SOFTWARE:
10061 buildDrawingCache(true);
10062 break;
10063 }
10064 }
10065
10066 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010067 * <p>Returns a hardware layer that can be used to draw this view again
10068 * without executing its draw method.</p>
10069 *
10070 * @return A HardwareLayer ready to render, or null if an error occurred.
10071 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010072 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010073 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10074 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010075 return null;
10076 }
10077
10078 final int width = mRight - mLeft;
10079 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010080
Romain Guy6c319ca2011-01-11 14:29:25 -080010081 if (width == 0 || height == 0) {
10082 return null;
10083 }
10084
10085 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10086 if (mHardwareLayer == null) {
10087 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10088 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010089 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010090 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10091 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010092 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010093 }
10094
Romain Guy59a12ca2011-06-09 17:48:21 -070010095 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010096 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10097 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010098 try {
10099 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010100 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010101 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010102
Chet Haase88172fe2011-03-07 17:36:33 -080010103 final int restoreCount = canvas.save();
10104
Romain Guy6c319ca2011-01-11 14:29:25 -080010105 computeScroll();
10106 canvas.translate(-mScrollX, -mScrollY);
10107
Romain Guy6c319ca2011-01-11 14:29:25 -080010108 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010109
Romain Guy6c319ca2011-01-11 14:29:25 -080010110 // Fast path for layouts with no backgrounds
10111 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10112 mPrivateFlags &= ~DIRTY_MASK;
10113 dispatchDraw(canvas);
10114 } else {
10115 draw(canvas);
10116 }
Joe Malin32736f02011-01-19 16:14:20 -080010117
Chet Haase88172fe2011-03-07 17:36:33 -080010118 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010119 } finally {
10120 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010121 mHardwareLayer.end(currentCanvas);
10122 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010123 }
10124 }
10125
10126 return mHardwareLayer;
10127 }
Romain Guy171c5922011-01-06 10:04:23 -080010128
Romain Guy65b345f2011-07-27 18:51:50 -070010129 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010130 if (mHardwareLayer != null) {
10131 mHardwareLayer.destroy();
10132 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010133 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010134 }
Romain Guy65b345f2011-07-27 18:51:50 -070010135 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010136 }
10137
Romain Guy171c5922011-01-06 10:04:23 -080010138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10140 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10141 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10142 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10143 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10144 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010145 *
Romain Guy171c5922011-01-06 10:04:23 -080010146 * <p>Enabling the drawing cache is similar to
10147 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010148 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10149 * drawing cache has no effect on rendering because the system uses a different mechanism
10150 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10151 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10152 * for information on how to enable software and hardware layers.</p>
10153 *
10154 * <p>This API can be used to manually generate
10155 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10156 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 *
10158 * @param enabled true to enable the drawing cache, false otherwise
10159 *
10160 * @see #isDrawingCacheEnabled()
10161 * @see #getDrawingCache()
10162 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010163 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 */
10165 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010166 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10168 }
10169
10170 /**
10171 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10172 *
10173 * @return true if the drawing cache is enabled
10174 *
10175 * @see #setDrawingCacheEnabled(boolean)
10176 * @see #getDrawingCache()
10177 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010178 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010179 public boolean isDrawingCacheEnabled() {
10180 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10181 }
10182
10183 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010184 * Debugging utility which recursively outputs the dirty state of a view and its
10185 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010186 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010187 * @hide
10188 */
Romain Guy676b1732011-02-14 14:45:33 -080010189 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010190 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10191 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10192 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10193 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10194 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10195 if (clear) {
10196 mPrivateFlags &= clearMask;
10197 }
10198 if (this instanceof ViewGroup) {
10199 ViewGroup parent = (ViewGroup) this;
10200 final int count = parent.getChildCount();
10201 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010202 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010203 child.outputDirtyFlags(indent + " ", clear, clearMask);
10204 }
10205 }
10206 }
10207
10208 /**
10209 * This method is used by ViewGroup to cause its children to restore or recreate their
10210 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10211 * to recreate its own display list, which would happen if it went through the normal
10212 * draw/dispatchDraw mechanisms.
10213 *
10214 * @hide
10215 */
10216 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010217
10218 /**
10219 * A view that is not attached or hardware accelerated cannot create a display list.
10220 * This method checks these conditions and returns the appropriate result.
10221 *
10222 * @return true if view has the ability to create a display list, false otherwise.
10223 *
10224 * @hide
10225 */
10226 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010227 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010228 }
Joe Malin32736f02011-01-19 16:14:20 -080010229
Chet Haasedaf98e92011-01-10 14:10:36 -080010230 /**
Romain Guyb051e892010-09-28 19:09:36 -070010231 * <p>Returns a display list that can be used to draw this view again
10232 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010233 *
Romain Guyb051e892010-09-28 19:09:36 -070010234 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010235 *
10236 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010237 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010238 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010239 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010240 return null;
10241 }
10242
Chet Haasedaf98e92011-01-10 14:10:36 -080010243 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10244 mDisplayList == null || !mDisplayList.isValid() ||
10245 mRecreateDisplayList)) {
10246 // Don't need to recreate the display list, just need to tell our
10247 // children to restore/recreate theirs
10248 if (mDisplayList != null && mDisplayList.isValid() &&
10249 !mRecreateDisplayList) {
10250 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10251 mPrivateFlags &= ~DIRTY_MASK;
10252 dispatchGetDisplayList();
10253
10254 return mDisplayList;
10255 }
10256
10257 // If we got here, we're recreating it. Mark it as such to ensure that
10258 // we copy in child display lists into ours in drawChild()
10259 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010260 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010261 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010262 // If we're creating a new display list, make sure our parent gets invalidated
10263 // since they will need to recreate their display list to account for this
10264 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010265 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010266 }
Romain Guyb051e892010-09-28 19:09:36 -070010267
10268 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010269 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010270 try {
10271 int width = mRight - mLeft;
10272 int height = mBottom - mTop;
10273
10274 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010275 // The dirty rect should always be null for a display list
10276 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010277
Chet Haasedaf98e92011-01-10 14:10:36 -080010278 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010279
10280 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010281 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010282 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010283 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010284
Romain Guyb051e892010-09-28 19:09:36 -070010285 // Fast path for layouts with no backgrounds
10286 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010287 dispatchDraw(canvas);
10288 } else {
10289 draw(canvas);
10290 }
Romain Guyb051e892010-09-28 19:09:36 -070010291 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010292 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010293 canvas.onPostDraw();
10294
10295 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010296 }
Chet Haase77785f92011-01-25 23:22:09 -080010297 } else {
10298 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10299 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010300 }
10301
10302 return mDisplayList;
10303 }
10304
10305 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010306 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010307 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010308 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010309 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010310 * @see #getDrawingCache(boolean)
10311 */
10312 public Bitmap getDrawingCache() {
10313 return getDrawingCache(false);
10314 }
10315
10316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10318 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10319 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10320 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10321 * request the drawing cache by calling this method and draw it on screen if the
10322 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010323 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010324 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10325 * this method will create a bitmap of the same size as this view. Because this bitmap
10326 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10327 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10328 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10329 * size than the view. This implies that your application must be able to handle this
10330 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010331 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010332 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10333 * the current density of the screen when the application is in compatibility
10334 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010336 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010337 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 * @see #setDrawingCacheEnabled(boolean)
10339 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010340 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 * @see #destroyDrawingCache()
10342 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010343 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10345 return null;
10346 }
10347 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010348 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 }
Romain Guy02890fd2010-08-06 17:58:44 -070010350 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 }
10352
10353 /**
10354 * <p>Frees the resources used by the drawing cache. If you call
10355 * {@link #buildDrawingCache()} manually without calling
10356 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10357 * should cleanup the cache with this method afterwards.</p>
10358 *
10359 * @see #setDrawingCacheEnabled(boolean)
10360 * @see #buildDrawingCache()
10361 * @see #getDrawingCache()
10362 */
10363 public void destroyDrawingCache() {
10364 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010365 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 mDrawingCache = null;
10367 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010368 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010369 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010370 mUnscaledDrawingCache = null;
10371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 }
10373
10374 /**
10375 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010376 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 * view will always be drawn on top of a solid color.
10378 *
10379 * @param color The background color to use for the drawing cache's bitmap
10380 *
10381 * @see #setDrawingCacheEnabled(boolean)
10382 * @see #buildDrawingCache()
10383 * @see #getDrawingCache()
10384 */
10385 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010386 if (color != mDrawingCacheBackgroundColor) {
10387 mDrawingCacheBackgroundColor = color;
10388 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 }
10391
10392 /**
10393 * @see #setDrawingCacheBackgroundColor(int)
10394 *
10395 * @return The background color to used for the drawing cache's bitmap
10396 */
10397 public int getDrawingCacheBackgroundColor() {
10398 return mDrawingCacheBackgroundColor;
10399 }
10400
10401 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010402 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010403 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010404 * @see #buildDrawingCache(boolean)
10405 */
10406 public void buildDrawingCache() {
10407 buildDrawingCache(false);
10408 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010409
Romain Guyfbd8f692009-06-26 14:51:58 -070010410 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10412 *
10413 * <p>If you call {@link #buildDrawingCache()} manually without calling
10414 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10415 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010416 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010417 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10418 * this method will create a bitmap of the same size as this view. Because this bitmap
10419 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10420 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10421 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10422 * size than the view. This implies that your application must be able to handle this
10423 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010424 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010425 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10426 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010427 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010428 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 *
10430 * @see #getDrawingCache()
10431 * @see #destroyDrawingCache()
10432 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010433 public void buildDrawingCache(boolean autoScale) {
10434 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010435 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010436 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437
10438 if (ViewDebug.TRACE_HIERARCHY) {
10439 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441
Romain Guy8506ab42009-06-11 17:35:47 -070010442 int width = mRight - mLeft;
10443 int height = mBottom - mTop;
10444
10445 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010446 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010447
Romain Guye1123222009-06-29 14:24:56 -070010448 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010449 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10450 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452
10453 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010454 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010455 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456
10457 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010458 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010459 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010460 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10461 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010462 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 return;
10464 }
10465
10466 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010467 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468
10469 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 Bitmap.Config quality;
10471 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010472 // Never pick ARGB_4444 because it looks awful
10473 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10475 case DRAWING_CACHE_QUALITY_AUTO:
10476 quality = Bitmap.Config.ARGB_8888;
10477 break;
10478 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010479 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 break;
10481 case DRAWING_CACHE_QUALITY_HIGH:
10482 quality = Bitmap.Config.ARGB_8888;
10483 break;
10484 default:
10485 quality = Bitmap.Config.ARGB_8888;
10486 break;
10487 }
10488 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010489 // Optimization for translucent windows
10490 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010491 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 }
10493
10494 // Try to cleanup memory
10495 if (bitmap != null) bitmap.recycle();
10496
10497 try {
10498 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010499 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010500 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010501 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010502 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010503 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010504 }
Adam Powell26153a32010-11-08 15:22:27 -080010505 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 } catch (OutOfMemoryError e) {
10507 // If there is not enough memory to create the bitmap cache, just
10508 // ignore the issue as bitmap caches are not required to draw the
10509 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010510 if (autoScale) {
10511 mDrawingCache = null;
10512 } else {
10513 mUnscaledDrawingCache = null;
10514 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010515 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 return;
10517 }
10518
10519 clear = drawingCacheBackgroundColor != 0;
10520 }
10521
10522 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 if (attachInfo != null) {
10524 canvas = attachInfo.mCanvas;
10525 if (canvas == null) {
10526 canvas = new Canvas();
10527 }
10528 canvas.setBitmap(bitmap);
10529 // Temporarily clobber the cached Canvas in case one of our children
10530 // is also using a drawing cache. Without this, the children would
10531 // steal the canvas by attaching their own bitmap to it and bad, bad
10532 // thing would happen (invisible views, corrupted drawings, etc.)
10533 attachInfo.mCanvas = null;
10534 } else {
10535 // This case should hopefully never or seldom happen
10536 canvas = new Canvas(bitmap);
10537 }
10538
10539 if (clear) {
10540 bitmap.eraseColor(drawingCacheBackgroundColor);
10541 }
10542
10543 computeScroll();
10544 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010545
Romain Guye1123222009-06-29 14:24:56 -070010546 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010547 final float scale = attachInfo.mApplicationScale;
10548 canvas.scale(scale, scale);
10549 }
Joe Malin32736f02011-01-19 16:14:20 -080010550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 canvas.translate(-mScrollX, -mScrollY);
10552
Romain Guy5bcdff42009-05-14 21:27:18 -070010553 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010554 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10555 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010556 mPrivateFlags |= DRAWING_CACHE_VALID;
10557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558
10559 // Fast path for layouts with no backgrounds
10560 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10561 if (ViewDebug.TRACE_HIERARCHY) {
10562 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10563 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010564 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 dispatchDraw(canvas);
10566 } else {
10567 draw(canvas);
10568 }
10569
10570 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010571 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572
10573 if (attachInfo != null) {
10574 // Restore the cached Canvas for our siblings
10575 attachInfo.mCanvas = canvas;
10576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577 }
10578 }
10579
10580 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010581 * Create a snapshot of the view into a bitmap. We should probably make
10582 * some form of this public, but should think about the API.
10583 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010584 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010585 int width = mRight - mLeft;
10586 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010587
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010588 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010589 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010590 width = (int) ((width * scale) + 0.5f);
10591 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010592
Romain Guy8c11e312009-09-14 15:15:30 -070010593 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010594 if (bitmap == null) {
10595 throw new OutOfMemoryError();
10596 }
10597
Romain Guyc529d8d2011-09-06 15:01:39 -070010598 Resources resources = getResources();
10599 if (resources != null) {
10600 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10601 }
Joe Malin32736f02011-01-19 16:14:20 -080010602
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010603 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010604 if (attachInfo != null) {
10605 canvas = attachInfo.mCanvas;
10606 if (canvas == null) {
10607 canvas = new Canvas();
10608 }
10609 canvas.setBitmap(bitmap);
10610 // Temporarily clobber the cached Canvas in case one of our children
10611 // is also using a drawing cache. Without this, the children would
10612 // steal the canvas by attaching their own bitmap to it and bad, bad
10613 // things would happen (invisible views, corrupted drawings, etc.)
10614 attachInfo.mCanvas = null;
10615 } else {
10616 // This case should hopefully never or seldom happen
10617 canvas = new Canvas(bitmap);
10618 }
10619
Romain Guy5bcdff42009-05-14 21:27:18 -070010620 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010621 bitmap.eraseColor(backgroundColor);
10622 }
10623
10624 computeScroll();
10625 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010626 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010627 canvas.translate(-mScrollX, -mScrollY);
10628
Romain Guy5bcdff42009-05-14 21:27:18 -070010629 // Temporarily remove the dirty mask
10630 int flags = mPrivateFlags;
10631 mPrivateFlags &= ~DIRTY_MASK;
10632
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010633 // Fast path for layouts with no backgrounds
10634 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10635 dispatchDraw(canvas);
10636 } else {
10637 draw(canvas);
10638 }
10639
Romain Guy5bcdff42009-05-14 21:27:18 -070010640 mPrivateFlags = flags;
10641
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010642 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010643 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010644
10645 if (attachInfo != null) {
10646 // Restore the cached Canvas for our siblings
10647 attachInfo.mCanvas = canvas;
10648 }
Romain Guy8506ab42009-06-11 17:35:47 -070010649
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010650 return bitmap;
10651 }
10652
10653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 * Indicates whether this View is currently in edit mode. A View is usually
10655 * in edit mode when displayed within a developer tool. For instance, if
10656 * this View is being drawn by a visual user interface builder, this method
10657 * should return true.
10658 *
10659 * Subclasses should check the return value of this method to provide
10660 * different behaviors if their normal behavior might interfere with the
10661 * host environment. For instance: the class spawns a thread in its
10662 * constructor, the drawing code relies on device-specific features, etc.
10663 *
10664 * This method is usually checked in the drawing code of custom widgets.
10665 *
10666 * @return True if this View is in edit mode, false otherwise.
10667 */
10668 public boolean isInEditMode() {
10669 return false;
10670 }
10671
10672 /**
10673 * If the View draws content inside its padding and enables fading edges,
10674 * it needs to support padding offsets. Padding offsets are added to the
10675 * fading edges to extend the length of the fade so that it covers pixels
10676 * drawn inside the padding.
10677 *
10678 * Subclasses of this class should override this method if they need
10679 * to draw content inside the padding.
10680 *
10681 * @return True if padding offset must be applied, false otherwise.
10682 *
10683 * @see #getLeftPaddingOffset()
10684 * @see #getRightPaddingOffset()
10685 * @see #getTopPaddingOffset()
10686 * @see #getBottomPaddingOffset()
10687 *
10688 * @since CURRENT
10689 */
10690 protected boolean isPaddingOffsetRequired() {
10691 return false;
10692 }
10693
10694 /**
10695 * Amount by which to extend the left fading region. Called only when
10696 * {@link #isPaddingOffsetRequired()} returns true.
10697 *
10698 * @return The left padding offset in pixels.
10699 *
10700 * @see #isPaddingOffsetRequired()
10701 *
10702 * @since CURRENT
10703 */
10704 protected int getLeftPaddingOffset() {
10705 return 0;
10706 }
10707
10708 /**
10709 * Amount by which to extend the right fading region. Called only when
10710 * {@link #isPaddingOffsetRequired()} returns true.
10711 *
10712 * @return The right padding offset in pixels.
10713 *
10714 * @see #isPaddingOffsetRequired()
10715 *
10716 * @since CURRENT
10717 */
10718 protected int getRightPaddingOffset() {
10719 return 0;
10720 }
10721
10722 /**
10723 * Amount by which to extend the top fading region. Called only when
10724 * {@link #isPaddingOffsetRequired()} returns true.
10725 *
10726 * @return The top padding offset in pixels.
10727 *
10728 * @see #isPaddingOffsetRequired()
10729 *
10730 * @since CURRENT
10731 */
10732 protected int getTopPaddingOffset() {
10733 return 0;
10734 }
10735
10736 /**
10737 * Amount by which to extend the bottom fading region. Called only when
10738 * {@link #isPaddingOffsetRequired()} returns true.
10739 *
10740 * @return The bottom padding offset in pixels.
10741 *
10742 * @see #isPaddingOffsetRequired()
10743 *
10744 * @since CURRENT
10745 */
10746 protected int getBottomPaddingOffset() {
10747 return 0;
10748 }
10749
10750 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010751 * @hide
10752 * @param offsetRequired
10753 */
10754 protected int getFadeTop(boolean offsetRequired) {
10755 int top = mPaddingTop;
10756 if (offsetRequired) top += getTopPaddingOffset();
10757 return top;
10758 }
10759
10760 /**
10761 * @hide
10762 * @param offsetRequired
10763 */
10764 protected int getFadeHeight(boolean offsetRequired) {
10765 int padding = mPaddingTop;
10766 if (offsetRequired) padding += getTopPaddingOffset();
10767 return mBottom - mTop - mPaddingBottom - padding;
10768 }
10769
10770 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010771 * <p>Indicates whether this view is attached to an hardware accelerated
10772 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010773 *
Romain Guy2bffd262010-09-12 17:40:02 -070010774 * <p>Even if this method returns true, it does not mean that every call
10775 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10776 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10777 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10778 * window is hardware accelerated,
10779 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10780 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010781 *
Romain Guy2bffd262010-09-12 17:40:02 -070010782 * @return True if the view is attached to a window and the window is
10783 * hardware accelerated; false in any other case.
10784 */
10785 public boolean isHardwareAccelerated() {
10786 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10787 }
Joe Malin32736f02011-01-19 16:14:20 -080010788
Romain Guy2bffd262010-09-12 17:40:02 -070010789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010790 * Manually render this view (and all of its children) to the given Canvas.
10791 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010792 * called. When implementing a view, implement
10793 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10794 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 *
10796 * @param canvas The Canvas to which the View is rendered.
10797 */
10798 public void draw(Canvas canvas) {
10799 if (ViewDebug.TRACE_HIERARCHY) {
10800 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10801 }
10802
Romain Guy5bcdff42009-05-14 21:27:18 -070010803 final int privateFlags = mPrivateFlags;
10804 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10805 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10806 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010808 /*
10809 * Draw traversal performs several drawing steps which must be executed
10810 * in the appropriate order:
10811 *
10812 * 1. Draw the background
10813 * 2. If necessary, save the canvas' layers to prepare for fading
10814 * 3. Draw view's content
10815 * 4. Draw children
10816 * 5. If necessary, draw the fading edges and restore layers
10817 * 6. Draw decorations (scrollbars for instance)
10818 */
10819
10820 // Step 1, draw the background, if needed
10821 int saveCount;
10822
Romain Guy24443ea2009-05-11 11:56:30 -070010823 if (!dirtyOpaque) {
10824 final Drawable background = mBGDrawable;
10825 if (background != null) {
10826 final int scrollX = mScrollX;
10827 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828
Romain Guy24443ea2009-05-11 11:56:30 -070010829 if (mBackgroundSizeChanged) {
10830 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10831 mBackgroundSizeChanged = false;
10832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010833
Romain Guy24443ea2009-05-11 11:56:30 -070010834 if ((scrollX | scrollY) == 0) {
10835 background.draw(canvas);
10836 } else {
10837 canvas.translate(scrollX, scrollY);
10838 background.draw(canvas);
10839 canvas.translate(-scrollX, -scrollY);
10840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010841 }
10842 }
10843
10844 // skip step 2 & 5 if possible (common case)
10845 final int viewFlags = mViewFlags;
10846 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10847 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10848 if (!verticalEdges && !horizontalEdges) {
10849 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010850 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851
10852 // Step 4, draw the children
10853 dispatchDraw(canvas);
10854
10855 // Step 6, draw decorations (scrollbars)
10856 onDrawScrollBars(canvas);
10857
10858 // we're done...
10859 return;
10860 }
10861
10862 /*
10863 * Here we do the full fledged routine...
10864 * (this is an uncommon case where speed matters less,
10865 * this is why we repeat some of the tests that have been
10866 * done above)
10867 */
10868
10869 boolean drawTop = false;
10870 boolean drawBottom = false;
10871 boolean drawLeft = false;
10872 boolean drawRight = false;
10873
10874 float topFadeStrength = 0.0f;
10875 float bottomFadeStrength = 0.0f;
10876 float leftFadeStrength = 0.0f;
10877 float rightFadeStrength = 0.0f;
10878
10879 // Step 2, save the canvas' layers
10880 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881
10882 final boolean offsetRequired = isPaddingOffsetRequired();
10883 if (offsetRequired) {
10884 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 }
10886
10887 int left = mScrollX + paddingLeft;
10888 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010889 int top = mScrollY + getFadeTop(offsetRequired);
10890 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010891
10892 if (offsetRequired) {
10893 right += getRightPaddingOffset();
10894 bottom += getBottomPaddingOffset();
10895 }
10896
10897 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010898 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10899 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900
10901 // clip the fade length if top and bottom fades overlap
10902 // overlapping fades produce odd-looking artifacts
10903 if (verticalEdges && (top + length > bottom - length)) {
10904 length = (bottom - top) / 2;
10905 }
10906
10907 // also clip horizontal fades if necessary
10908 if (horizontalEdges && (left + length > right - length)) {
10909 length = (right - left) / 2;
10910 }
10911
10912 if (verticalEdges) {
10913 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010914 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010916 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010917 }
10918
10919 if (horizontalEdges) {
10920 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010921 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010923 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 }
10925
10926 saveCount = canvas.getSaveCount();
10927
10928 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010929 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010930 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10931
10932 if (drawTop) {
10933 canvas.saveLayer(left, top, right, top + length, null, flags);
10934 }
10935
10936 if (drawBottom) {
10937 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10938 }
10939
10940 if (drawLeft) {
10941 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10942 }
10943
10944 if (drawRight) {
10945 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10946 }
10947 } else {
10948 scrollabilityCache.setFadeColor(solidColor);
10949 }
10950
10951 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010952 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953
10954 // Step 4, draw the children
10955 dispatchDraw(canvas);
10956
10957 // Step 5, draw the fade effect and restore layers
10958 final Paint p = scrollabilityCache.paint;
10959 final Matrix matrix = scrollabilityCache.matrix;
10960 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961
10962 if (drawTop) {
10963 matrix.setScale(1, fadeHeight * topFadeStrength);
10964 matrix.postTranslate(left, top);
10965 fade.setLocalMatrix(matrix);
10966 canvas.drawRect(left, top, right, top + length, p);
10967 }
10968
10969 if (drawBottom) {
10970 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10971 matrix.postRotate(180);
10972 matrix.postTranslate(left, bottom);
10973 fade.setLocalMatrix(matrix);
10974 canvas.drawRect(left, bottom - length, right, bottom, p);
10975 }
10976
10977 if (drawLeft) {
10978 matrix.setScale(1, fadeHeight * leftFadeStrength);
10979 matrix.postRotate(-90);
10980 matrix.postTranslate(left, top);
10981 fade.setLocalMatrix(matrix);
10982 canvas.drawRect(left, top, left + length, bottom, p);
10983 }
10984
10985 if (drawRight) {
10986 matrix.setScale(1, fadeHeight * rightFadeStrength);
10987 matrix.postRotate(90);
10988 matrix.postTranslate(right, top);
10989 fade.setLocalMatrix(matrix);
10990 canvas.drawRect(right - length, top, right, bottom, p);
10991 }
10992
10993 canvas.restoreToCount(saveCount);
10994
10995 // Step 6, draw decorations (scrollbars)
10996 onDrawScrollBars(canvas);
10997 }
10998
10999 /**
11000 * Override this if your view is known to always be drawn on top of a solid color background,
11001 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11002 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11003 * should be set to 0xFF.
11004 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011005 * @see #setVerticalFadingEdgeEnabled(boolean)
11006 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 *
11008 * @return The known solid color background for this view, or 0 if the color may vary
11009 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011010 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 public int getSolidColor() {
11012 return 0;
11013 }
11014
11015 /**
11016 * Build a human readable string representation of the specified view flags.
11017 *
11018 * @param flags the view flags to convert to a string
11019 * @return a String representing the supplied flags
11020 */
11021 private static String printFlags(int flags) {
11022 String output = "";
11023 int numFlags = 0;
11024 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11025 output += "TAKES_FOCUS";
11026 numFlags++;
11027 }
11028
11029 switch (flags & VISIBILITY_MASK) {
11030 case INVISIBLE:
11031 if (numFlags > 0) {
11032 output += " ";
11033 }
11034 output += "INVISIBLE";
11035 // USELESS HERE numFlags++;
11036 break;
11037 case GONE:
11038 if (numFlags > 0) {
11039 output += " ";
11040 }
11041 output += "GONE";
11042 // USELESS HERE numFlags++;
11043 break;
11044 default:
11045 break;
11046 }
11047 return output;
11048 }
11049
11050 /**
11051 * Build a human readable string representation of the specified private
11052 * view flags.
11053 *
11054 * @param privateFlags the private view flags to convert to a string
11055 * @return a String representing the supplied flags
11056 */
11057 private static String printPrivateFlags(int privateFlags) {
11058 String output = "";
11059 int numFlags = 0;
11060
11061 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11062 output += "WANTS_FOCUS";
11063 numFlags++;
11064 }
11065
11066 if ((privateFlags & FOCUSED) == FOCUSED) {
11067 if (numFlags > 0) {
11068 output += " ";
11069 }
11070 output += "FOCUSED";
11071 numFlags++;
11072 }
11073
11074 if ((privateFlags & SELECTED) == SELECTED) {
11075 if (numFlags > 0) {
11076 output += " ";
11077 }
11078 output += "SELECTED";
11079 numFlags++;
11080 }
11081
11082 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11083 if (numFlags > 0) {
11084 output += " ";
11085 }
11086 output += "IS_ROOT_NAMESPACE";
11087 numFlags++;
11088 }
11089
11090 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11091 if (numFlags > 0) {
11092 output += " ";
11093 }
11094 output += "HAS_BOUNDS";
11095 numFlags++;
11096 }
11097
11098 if ((privateFlags & DRAWN) == DRAWN) {
11099 if (numFlags > 0) {
11100 output += " ";
11101 }
11102 output += "DRAWN";
11103 // USELESS HERE numFlags++;
11104 }
11105 return output;
11106 }
11107
11108 /**
11109 * <p>Indicates whether or not this view's layout will be requested during
11110 * the next hierarchy layout pass.</p>
11111 *
11112 * @return true if the layout will be forced during next layout pass
11113 */
11114 public boolean isLayoutRequested() {
11115 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11116 }
11117
11118 /**
11119 * Assign a size and position to a view and all of its
11120 * descendants
11121 *
11122 * <p>This is the second phase of the layout mechanism.
11123 * (The first is measuring). In this phase, each parent calls
11124 * layout on all of its children to position them.
11125 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011126 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 *
Chet Haase9c087442011-01-12 16:20:16 -080011128 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 * Derived classes with children should override
11130 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011131 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 *
11133 * @param l Left position, relative to parent
11134 * @param t Top position, relative to parent
11135 * @param r Right position, relative to parent
11136 * @param b Bottom position, relative to parent
11137 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011138 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011139 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011140 int oldL = mLeft;
11141 int oldT = mTop;
11142 int oldB = mBottom;
11143 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 boolean changed = setFrame(l, t, r, b);
11145 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11146 if (ViewDebug.TRACE_HIERARCHY) {
11147 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11148 }
11149
11150 onLayout(changed, l, t, r, b);
11151 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011152
11153 if (mOnLayoutChangeListeners != null) {
11154 ArrayList<OnLayoutChangeListener> listenersCopy =
11155 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11156 int numListeners = listenersCopy.size();
11157 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011158 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011159 }
11160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 }
11162 mPrivateFlags &= ~FORCE_LAYOUT;
11163 }
11164
11165 /**
11166 * Called from layout when this view should
11167 * assign a size and position to each of its children.
11168 *
11169 * Derived classes with children should override
11170 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011171 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 * @param changed This is a new size or position for this view
11173 * @param left Left position, relative to parent
11174 * @param top Top position, relative to parent
11175 * @param right Right position, relative to parent
11176 * @param bottom Bottom position, relative to parent
11177 */
11178 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11179 }
11180
11181 /**
11182 * Assign a size and position to this view.
11183 *
11184 * This is called from layout.
11185 *
11186 * @param left Left position, relative to parent
11187 * @param top Top position, relative to parent
11188 * @param right Right position, relative to parent
11189 * @param bottom Bottom position, relative to parent
11190 * @return true if the new size and position are different than the
11191 * previous ones
11192 * {@hide}
11193 */
11194 protected boolean setFrame(int left, int top, int right, int bottom) {
11195 boolean changed = false;
11196
11197 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011198 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011199 + right + "," + bottom + ")");
11200 }
11201
11202 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11203 changed = true;
11204
11205 // Remember our drawn bit
11206 int drawn = mPrivateFlags & DRAWN;
11207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011208 int oldWidth = mRight - mLeft;
11209 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011210 int newWidth = right - left;
11211 int newHeight = bottom - top;
11212 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11213
11214 // Invalidate our old position
11215 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216
11217 mLeft = left;
11218 mTop = top;
11219 mRight = right;
11220 mBottom = bottom;
11221
11222 mPrivateFlags |= HAS_BOUNDS;
11223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011224
Chet Haase75755e22011-07-18 17:48:25 -070011225 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011226 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11227 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011228 if (mTransformationInfo != null) {
11229 mTransformationInfo.mMatrixDirty = true;
11230 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11233 }
11234
11235 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11236 // If we are visible, force the DRAWN bit to on so that
11237 // this invalidate will go through (at least to our parent).
11238 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011239 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011240 // the DRAWN bit.
11241 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011242 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011243 // parent display list may need to be recreated based on a change in the bounds
11244 // of any child
11245 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 }
11247
11248 // Reset drawn bit to original value (invalidate turns it off)
11249 mPrivateFlags |= drawn;
11250
11251 mBackgroundSizeChanged = true;
11252 }
11253 return changed;
11254 }
11255
11256 /**
11257 * Finalize inflating a view from XML. This is called as the last phase
11258 * of inflation, after all child views have been added.
11259 *
11260 * <p>Even if the subclass overrides onFinishInflate, they should always be
11261 * sure to call the super method, so that we get called.
11262 */
11263 protected void onFinishInflate() {
11264 }
11265
11266 /**
11267 * Returns the resources associated with this view.
11268 *
11269 * @return Resources object.
11270 */
11271 public Resources getResources() {
11272 return mResources;
11273 }
11274
11275 /**
11276 * Invalidates the specified Drawable.
11277 *
11278 * @param drawable the drawable to invalidate
11279 */
11280 public void invalidateDrawable(Drawable drawable) {
11281 if (verifyDrawable(drawable)) {
11282 final Rect dirty = drawable.getBounds();
11283 final int scrollX = mScrollX;
11284 final int scrollY = mScrollY;
11285
11286 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11287 dirty.right + scrollX, dirty.bottom + scrollY);
11288 }
11289 }
11290
11291 /**
11292 * Schedules an action on a drawable to occur at a specified time.
11293 *
11294 * @param who the recipient of the action
11295 * @param what the action to run on the drawable
11296 * @param when the time at which the action must occur. Uses the
11297 * {@link SystemClock#uptimeMillis} timebase.
11298 */
11299 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11300 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11301 mAttachInfo.mHandler.postAtTime(what, who, when);
11302 }
11303 }
11304
11305 /**
11306 * Cancels a scheduled action on a drawable.
11307 *
11308 * @param who the recipient of the action
11309 * @param what the action to cancel
11310 */
11311 public void unscheduleDrawable(Drawable who, Runnable what) {
11312 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11313 mAttachInfo.mHandler.removeCallbacks(what, who);
11314 }
11315 }
11316
11317 /**
11318 * Unschedule any events associated with the given Drawable. This can be
11319 * used when selecting a new Drawable into a view, so that the previous
11320 * one is completely unscheduled.
11321 *
11322 * @param who The Drawable to unschedule.
11323 *
11324 * @see #drawableStateChanged
11325 */
11326 public void unscheduleDrawable(Drawable who) {
11327 if (mAttachInfo != null) {
11328 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11329 }
11330 }
11331
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011332 /**
11333 * Return the layout direction of a given Drawable.
11334 *
11335 * @param who the Drawable to query
11336 *
11337 * @hide
11338 */
11339 public int getResolvedLayoutDirection(Drawable who) {
11340 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011341 }
11342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011343 /**
11344 * If your view subclass is displaying its own Drawable objects, it should
11345 * override this function and return true for any Drawable it is
11346 * displaying. This allows animations for those drawables to be
11347 * scheduled.
11348 *
11349 * <p>Be sure to call through to the super class when overriding this
11350 * function.
11351 *
11352 * @param who The Drawable to verify. Return true if it is one you are
11353 * displaying, else return the result of calling through to the
11354 * super class.
11355 *
11356 * @return boolean If true than the Drawable is being displayed in the
11357 * view; else false and it is not allowed to animate.
11358 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011359 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11360 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 */
11362 protected boolean verifyDrawable(Drawable who) {
11363 return who == mBGDrawable;
11364 }
11365
11366 /**
11367 * This function is called whenever the state of the view changes in such
11368 * a way that it impacts the state of drawables being shown.
11369 *
11370 * <p>Be sure to call through to the superclass when overriding this
11371 * function.
11372 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011373 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011374 */
11375 protected void drawableStateChanged() {
11376 Drawable d = mBGDrawable;
11377 if (d != null && d.isStateful()) {
11378 d.setState(getDrawableState());
11379 }
11380 }
11381
11382 /**
11383 * Call this to force a view to update its drawable state. This will cause
11384 * drawableStateChanged to be called on this view. Views that are interested
11385 * in the new state should call getDrawableState.
11386 *
11387 * @see #drawableStateChanged
11388 * @see #getDrawableState
11389 */
11390 public void refreshDrawableState() {
11391 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11392 drawableStateChanged();
11393
11394 ViewParent parent = mParent;
11395 if (parent != null) {
11396 parent.childDrawableStateChanged(this);
11397 }
11398 }
11399
11400 /**
11401 * Return an array of resource IDs of the drawable states representing the
11402 * current state of the view.
11403 *
11404 * @return The current drawable state
11405 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011406 * @see Drawable#setState(int[])
11407 * @see #drawableStateChanged()
11408 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409 */
11410 public final int[] getDrawableState() {
11411 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11412 return mDrawableState;
11413 } else {
11414 mDrawableState = onCreateDrawableState(0);
11415 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11416 return mDrawableState;
11417 }
11418 }
11419
11420 /**
11421 * Generate the new {@link android.graphics.drawable.Drawable} state for
11422 * this view. This is called by the view
11423 * system when the cached Drawable state is determined to be invalid. To
11424 * retrieve the current state, you should use {@link #getDrawableState}.
11425 *
11426 * @param extraSpace if non-zero, this is the number of extra entries you
11427 * would like in the returned array in which you can place your own
11428 * states.
11429 *
11430 * @return Returns an array holding the current {@link Drawable} state of
11431 * the view.
11432 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011433 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011434 */
11435 protected int[] onCreateDrawableState(int extraSpace) {
11436 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11437 mParent instanceof View) {
11438 return ((View) mParent).onCreateDrawableState(extraSpace);
11439 }
11440
11441 int[] drawableState;
11442
11443 int privateFlags = mPrivateFlags;
11444
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011445 int viewStateIndex = 0;
11446 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11447 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11448 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011449 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011450 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11451 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011452 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11453 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011454 // This is set if HW acceleration is requested, even if the current
11455 // process doesn't allow it. This is just to allow app preview
11456 // windows to better match their app.
11457 viewStateIndex |= VIEW_STATE_ACCELERATED;
11458 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011459 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011460
Christopher Tate3d4bf172011-03-28 16:16:46 -070011461 final int privateFlags2 = mPrivateFlags2;
11462 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11463 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011465 drawableState = VIEW_STATE_SETS[viewStateIndex];
11466
11467 //noinspection ConstantIfStatement
11468 if (false) {
11469 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11470 Log.i("View", toString()
11471 + " pressed=" + ((privateFlags & PRESSED) != 0)
11472 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11473 + " fo=" + hasFocus()
11474 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011475 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011476 + ": " + Arrays.toString(drawableState));
11477 }
11478
11479 if (extraSpace == 0) {
11480 return drawableState;
11481 }
11482
11483 final int[] fullState;
11484 if (drawableState != null) {
11485 fullState = new int[drawableState.length + extraSpace];
11486 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11487 } else {
11488 fullState = new int[extraSpace];
11489 }
11490
11491 return fullState;
11492 }
11493
11494 /**
11495 * Merge your own state values in <var>additionalState</var> into the base
11496 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011497 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 *
11499 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011500 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011501 * own additional state values.
11502 *
11503 * @param additionalState The additional state values you would like
11504 * added to <var>baseState</var>; this array is not modified.
11505 *
11506 * @return As a convenience, the <var>baseState</var> array you originally
11507 * passed into the function is returned.
11508 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011509 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011510 */
11511 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11512 final int N = baseState.length;
11513 int i = N - 1;
11514 while (i >= 0 && baseState[i] == 0) {
11515 i--;
11516 }
11517 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11518 return baseState;
11519 }
11520
11521 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011522 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11523 * on all Drawable objects associated with this view.
11524 */
11525 public void jumpDrawablesToCurrentState() {
11526 if (mBGDrawable != null) {
11527 mBGDrawable.jumpToCurrentState();
11528 }
11529 }
11530
11531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 * Sets the background color for this view.
11533 * @param color the color of the background
11534 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011535 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011537 if (mBGDrawable instanceof ColorDrawable) {
11538 ((ColorDrawable) mBGDrawable).setColor(color);
11539 } else {
11540 setBackgroundDrawable(new ColorDrawable(color));
11541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011542 }
11543
11544 /**
11545 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011546 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 * @param resid The identifier of the resource.
11548 * @attr ref android.R.styleable#View_background
11549 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011550 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011551 public void setBackgroundResource(int resid) {
11552 if (resid != 0 && resid == mBackgroundResource) {
11553 return;
11554 }
11555
11556 Drawable d= null;
11557 if (resid != 0) {
11558 d = mResources.getDrawable(resid);
11559 }
11560 setBackgroundDrawable(d);
11561
11562 mBackgroundResource = resid;
11563 }
11564
11565 /**
11566 * Set the background to a given Drawable, or remove the background. If the
11567 * background has padding, this View's padding is set to the background's
11568 * padding. However, when a background is removed, this View's padding isn't
11569 * touched. If setting the padding is desired, please use
11570 * {@link #setPadding(int, int, int, int)}.
11571 *
11572 * @param d The Drawable to use as the background, or null to remove the
11573 * background
11574 */
11575 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011576 if (d == mBGDrawable) {
11577 return;
11578 }
11579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 boolean requestLayout = false;
11581
11582 mBackgroundResource = 0;
11583
11584 /*
11585 * Regardless of whether we're setting a new background or not, we want
11586 * to clear the previous drawable.
11587 */
11588 if (mBGDrawable != null) {
11589 mBGDrawable.setCallback(null);
11590 unscheduleDrawable(mBGDrawable);
11591 }
11592
11593 if (d != null) {
11594 Rect padding = sThreadLocal.get();
11595 if (padding == null) {
11596 padding = new Rect();
11597 sThreadLocal.set(padding);
11598 }
11599 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011600 switch (d.getResolvedLayoutDirectionSelf()) {
11601 case LAYOUT_DIRECTION_RTL:
11602 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11603 break;
11604 case LAYOUT_DIRECTION_LTR:
11605 default:
11606 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011608 }
11609
11610 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11611 // if it has a different minimum size, we should layout again
11612 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11613 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11614 requestLayout = true;
11615 }
11616
11617 d.setCallback(this);
11618 if (d.isStateful()) {
11619 d.setState(getDrawableState());
11620 }
11621 d.setVisible(getVisibility() == VISIBLE, false);
11622 mBGDrawable = d;
11623
11624 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11625 mPrivateFlags &= ~SKIP_DRAW;
11626 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11627 requestLayout = true;
11628 }
11629 } else {
11630 /* Remove the background */
11631 mBGDrawable = null;
11632
11633 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11634 /*
11635 * This view ONLY drew the background before and we're removing
11636 * the background, so now it won't draw anything
11637 * (hence we SKIP_DRAW)
11638 */
11639 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11640 mPrivateFlags |= SKIP_DRAW;
11641 }
11642
11643 /*
11644 * When the background is set, we try to apply its padding to this
11645 * View. When the background is removed, we don't touch this View's
11646 * padding. This is noted in the Javadocs. Hence, we don't need to
11647 * requestLayout(), the invalidate() below is sufficient.
11648 */
11649
11650 // The old background's minimum size could have affected this
11651 // View's layout, so let's requestLayout
11652 requestLayout = true;
11653 }
11654
Romain Guy8f1344f52009-05-15 16:03:59 -070011655 computeOpaqueFlags();
11656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011657 if (requestLayout) {
11658 requestLayout();
11659 }
11660
11661 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011662 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011663 }
11664
11665 /**
11666 * Gets the background drawable
11667 * @return The drawable used as the background for this view, if any.
11668 */
11669 public Drawable getBackground() {
11670 return mBGDrawable;
11671 }
11672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011673 /**
11674 * Sets the padding. The view may add on the space required to display
11675 * the scrollbars, depending on the style and visibility of the scrollbars.
11676 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11677 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11678 * from the values set in this call.
11679 *
11680 * @attr ref android.R.styleable#View_padding
11681 * @attr ref android.R.styleable#View_paddingBottom
11682 * @attr ref android.R.styleable#View_paddingLeft
11683 * @attr ref android.R.styleable#View_paddingRight
11684 * @attr ref android.R.styleable#View_paddingTop
11685 * @param left the left padding in pixels
11686 * @param top the top padding in pixels
11687 * @param right the right padding in pixels
11688 * @param bottom the bottom padding in pixels
11689 */
11690 public void setPadding(int left, int top, int right, int bottom) {
11691 boolean changed = false;
11692
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011693 mUserPaddingRelative = false;
11694
Adam Powell20232d02010-12-08 21:08:53 -080011695 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011696 mUserPaddingRight = right;
11697 mUserPaddingBottom = bottom;
11698
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011699 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011700
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011701 // Common case is there are no scroll bars.
11702 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011703 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011704 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011705 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011706 switch (mVerticalScrollbarPosition) {
11707 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011708 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11709 left += offset;
11710 } else {
11711 right += offset;
11712 }
11713 break;
Adam Powell20232d02010-12-08 21:08:53 -080011714 case SCROLLBAR_POSITION_RIGHT:
11715 right += offset;
11716 break;
11717 case SCROLLBAR_POSITION_LEFT:
11718 left += offset;
11719 break;
11720 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011721 }
Adam Powell20232d02010-12-08 21:08:53 -080011722 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011723 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11724 ? 0 : getHorizontalScrollbarHeight();
11725 }
11726 }
Romain Guy8506ab42009-06-11 17:35:47 -070011727
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011728 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011729 changed = true;
11730 mPaddingLeft = left;
11731 }
11732 if (mPaddingTop != top) {
11733 changed = true;
11734 mPaddingTop = top;
11735 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011736 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011738 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011739 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011740 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011741 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011742 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011743 }
11744
11745 if (changed) {
11746 requestLayout();
11747 }
11748 }
11749
11750 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011751 * Sets the relative padding. The view may add on the space required to display
11752 * the scrollbars, depending on the style and visibility of the scrollbars.
11753 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11754 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11755 * from the values set in this call.
11756 *
11757 * @attr ref android.R.styleable#View_padding
11758 * @attr ref android.R.styleable#View_paddingBottom
11759 * @attr ref android.R.styleable#View_paddingStart
11760 * @attr ref android.R.styleable#View_paddingEnd
11761 * @attr ref android.R.styleable#View_paddingTop
11762 * @param start the start padding in pixels
11763 * @param top the top padding in pixels
11764 * @param end the end padding in pixels
11765 * @param bottom the bottom padding in pixels
11766 *
11767 * @hide
11768 */
11769 public void setPaddingRelative(int start, int top, int end, int bottom) {
11770 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011771
11772 mUserPaddingStart = start;
11773 mUserPaddingEnd = end;
11774
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011775 switch(getResolvedLayoutDirection()) {
11776 case LAYOUT_DIRECTION_RTL:
11777 setPadding(end, top, start, bottom);
11778 break;
11779 case LAYOUT_DIRECTION_LTR:
11780 default:
11781 setPadding(start, top, end, bottom);
11782 }
11783 }
11784
11785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011786 * Returns the top padding of this view.
11787 *
11788 * @return the top padding in pixels
11789 */
11790 public int getPaddingTop() {
11791 return mPaddingTop;
11792 }
11793
11794 /**
11795 * Returns the bottom padding of this view. If there are inset and enabled
11796 * scrollbars, this value may include the space required to display the
11797 * scrollbars as well.
11798 *
11799 * @return the bottom padding in pixels
11800 */
11801 public int getPaddingBottom() {
11802 return mPaddingBottom;
11803 }
11804
11805 /**
11806 * Returns the left padding of this view. If there are inset and enabled
11807 * scrollbars, this value may include the space required to display the
11808 * scrollbars as well.
11809 *
11810 * @return the left padding in pixels
11811 */
11812 public int getPaddingLeft() {
11813 return mPaddingLeft;
11814 }
11815
11816 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011817 * Returns the start padding of this view. If there are inset and enabled
11818 * scrollbars, this value may include the space required to display the
11819 * scrollbars as well.
11820 *
11821 * @return the start padding in pixels
11822 *
11823 * @hide
11824 */
11825 public int getPaddingStart() {
11826 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11827 mPaddingRight : mPaddingLeft;
11828 }
11829
11830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 * Returns the right padding of this view. If there are inset and enabled
11832 * scrollbars, this value may include the space required to display the
11833 * scrollbars as well.
11834 *
11835 * @return the right padding in pixels
11836 */
11837 public int getPaddingRight() {
11838 return mPaddingRight;
11839 }
11840
11841 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011842 * Returns the end padding of this view. If there are inset and enabled
11843 * scrollbars, this value may include the space required to display the
11844 * scrollbars as well.
11845 *
11846 * @return the end padding in pixels
11847 *
11848 * @hide
11849 */
11850 public int getPaddingEnd() {
11851 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11852 mPaddingLeft : mPaddingRight;
11853 }
11854
11855 /**
11856 * Return if the padding as been set thru relative values
11857 * {@link #setPaddingRelative(int, int, int, int)} or thru
11858 * @attr ref android.R.styleable#View_paddingStart or
11859 * @attr ref android.R.styleable#View_paddingEnd
11860 *
11861 * @return true if the padding is relative or false if it is not.
11862 *
11863 * @hide
11864 */
11865 public boolean isPaddingRelative() {
11866 return mUserPaddingRelative;
11867 }
11868
11869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 * Changes the selection state of this view. A view can be selected or not.
11871 * Note that selection is not the same as focus. Views are typically
11872 * selected in the context of an AdapterView like ListView or GridView;
11873 * the selected view is the view that is highlighted.
11874 *
11875 * @param selected true if the view must be selected, false otherwise
11876 */
11877 public void setSelected(boolean selected) {
11878 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11879 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011880 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011882 refreshDrawableState();
11883 dispatchSetSelected(selected);
11884 }
11885 }
11886
11887 /**
11888 * Dispatch setSelected to all of this View's children.
11889 *
11890 * @see #setSelected(boolean)
11891 *
11892 * @param selected The new selected state
11893 */
11894 protected void dispatchSetSelected(boolean selected) {
11895 }
11896
11897 /**
11898 * Indicates the selection state of this view.
11899 *
11900 * @return true if the view is selected, false otherwise
11901 */
11902 @ViewDebug.ExportedProperty
11903 public boolean isSelected() {
11904 return (mPrivateFlags & SELECTED) != 0;
11905 }
11906
11907 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011908 * Changes the activated state of this view. A view can be activated or not.
11909 * Note that activation is not the same as selection. Selection is
11910 * a transient property, representing the view (hierarchy) the user is
11911 * currently interacting with. Activation is a longer-term state that the
11912 * user can move views in and out of. For example, in a list view with
11913 * single or multiple selection enabled, the views in the current selection
11914 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11915 * here.) The activated state is propagated down to children of the view it
11916 * is set on.
11917 *
11918 * @param activated true if the view must be activated, false otherwise
11919 */
11920 public void setActivated(boolean activated) {
11921 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11922 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011923 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011924 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011925 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011926 }
11927 }
11928
11929 /**
11930 * Dispatch setActivated to all of this View's children.
11931 *
11932 * @see #setActivated(boolean)
11933 *
11934 * @param activated The new activated state
11935 */
11936 protected void dispatchSetActivated(boolean activated) {
11937 }
11938
11939 /**
11940 * Indicates the activation state of this view.
11941 *
11942 * @return true if the view is activated, false otherwise
11943 */
11944 @ViewDebug.ExportedProperty
11945 public boolean isActivated() {
11946 return (mPrivateFlags & ACTIVATED) != 0;
11947 }
11948
11949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011950 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11951 * observer can be used to get notifications when global events, like
11952 * layout, happen.
11953 *
11954 * The returned ViewTreeObserver observer is not guaranteed to remain
11955 * valid for the lifetime of this View. If the caller of this method keeps
11956 * a long-lived reference to ViewTreeObserver, it should always check for
11957 * the return value of {@link ViewTreeObserver#isAlive()}.
11958 *
11959 * @return The ViewTreeObserver for this view's hierarchy.
11960 */
11961 public ViewTreeObserver getViewTreeObserver() {
11962 if (mAttachInfo != null) {
11963 return mAttachInfo.mTreeObserver;
11964 }
11965 if (mFloatingTreeObserver == null) {
11966 mFloatingTreeObserver = new ViewTreeObserver();
11967 }
11968 return mFloatingTreeObserver;
11969 }
11970
11971 /**
11972 * <p>Finds the topmost view in the current view hierarchy.</p>
11973 *
11974 * @return the topmost view containing this view
11975 */
11976 public View getRootView() {
11977 if (mAttachInfo != null) {
11978 final View v = mAttachInfo.mRootView;
11979 if (v != null) {
11980 return v;
11981 }
11982 }
Romain Guy8506ab42009-06-11 17:35:47 -070011983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011984 View parent = this;
11985
11986 while (parent.mParent != null && parent.mParent instanceof View) {
11987 parent = (View) parent.mParent;
11988 }
11989
11990 return parent;
11991 }
11992
11993 /**
11994 * <p>Computes the coordinates of this view on the screen. The argument
11995 * must be an array of two integers. After the method returns, the array
11996 * contains the x and y location in that order.</p>
11997 *
11998 * @param location an array of two integers in which to hold the coordinates
11999 */
12000 public void getLocationOnScreen(int[] location) {
12001 getLocationInWindow(location);
12002
12003 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012004 if (info != null) {
12005 location[0] += info.mWindowLeft;
12006 location[1] += info.mWindowTop;
12007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012008 }
12009
12010 /**
12011 * <p>Computes the coordinates of this view in its window. The argument
12012 * must be an array of two integers. After the method returns, the array
12013 * contains the x and y location in that order.</p>
12014 *
12015 * @param location an array of two integers in which to hold the coordinates
12016 */
12017 public void getLocationInWindow(int[] location) {
12018 if (location == null || location.length < 2) {
12019 throw new IllegalArgumentException("location must be an array of "
12020 + "two integers");
12021 }
12022
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012023 location[0] = mLeft;
12024 location[1] = mTop;
12025 if (mTransformationInfo != null) {
12026 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12027 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012029
12030 ViewParent viewParent = mParent;
12031 while (viewParent instanceof View) {
12032 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012033 location[0] += view.mLeft - view.mScrollX;
12034 location[1] += view.mTop - view.mScrollY;
12035 if (view.mTransformationInfo != null) {
12036 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12037 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012039 viewParent = view.mParent;
12040 }
Romain Guy8506ab42009-06-11 17:35:47 -070012041
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012042 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012043 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012044 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012045 location[1] -= vr.mCurScrollY;
12046 }
12047 }
12048
12049 /**
12050 * {@hide}
12051 * @param id the id of the view to be found
12052 * @return the view of the specified id, null if cannot be found
12053 */
12054 protected View findViewTraversal(int id) {
12055 if (id == mID) {
12056 return this;
12057 }
12058 return null;
12059 }
12060
12061 /**
12062 * {@hide}
12063 * @param tag the tag of the view to be found
12064 * @return the view of specified tag, null if cannot be found
12065 */
12066 protected View findViewWithTagTraversal(Object tag) {
12067 if (tag != null && tag.equals(mTag)) {
12068 return this;
12069 }
12070 return null;
12071 }
12072
12073 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012074 * {@hide}
12075 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012076 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012077 * @return The first view that matches the predicate or null.
12078 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012079 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012080 if (predicate.apply(this)) {
12081 return this;
12082 }
12083 return null;
12084 }
12085
12086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087 * Look for a child view with the given id. If this view has the given
12088 * id, return this view.
12089 *
12090 * @param id The id to search for.
12091 * @return The view that has the given id in the hierarchy or null
12092 */
12093 public final View findViewById(int id) {
12094 if (id < 0) {
12095 return null;
12096 }
12097 return findViewTraversal(id);
12098 }
12099
12100 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012101 * Finds a view by its unuque and stable accessibility id.
12102 *
12103 * @param accessibilityId The searched accessibility id.
12104 * @return The found view.
12105 */
12106 final View findViewByAccessibilityId(int accessibilityId) {
12107 if (accessibilityId < 0) {
12108 return null;
12109 }
12110 return findViewByAccessibilityIdTraversal(accessibilityId);
12111 }
12112
12113 /**
12114 * Performs the traversal to find a view by its unuque and stable accessibility id.
12115 *
12116 * <strong>Note:</strong>This method does not stop at the root namespace
12117 * boundary since the user can touch the screen at an arbitrary location
12118 * potentially crossing the root namespace bounday which will send an
12119 * accessibility event to accessibility services and they should be able
12120 * to obtain the event source. Also accessibility ids are guaranteed to be
12121 * unique in the window.
12122 *
12123 * @param accessibilityId The accessibility id.
12124 * @return The found view.
12125 */
12126 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12127 if (getAccessibilityViewId() == accessibilityId) {
12128 return this;
12129 }
12130 return null;
12131 }
12132
12133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012134 * Look for a child view with the given tag. If this view has the given
12135 * tag, return this view.
12136 *
12137 * @param tag The tag to search for, using "tag.equals(getTag())".
12138 * @return The View that has the given tag in the hierarchy or null
12139 */
12140 public final View findViewWithTag(Object tag) {
12141 if (tag == null) {
12142 return null;
12143 }
12144 return findViewWithTagTraversal(tag);
12145 }
12146
12147 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012148 * {@hide}
12149 * Look for a child view that matches the specified predicate.
12150 * If this view matches the predicate, return this view.
12151 *
12152 * @param predicate The predicate to evaluate.
12153 * @return The first view that matches the predicate or null.
12154 */
12155 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012156 return findViewByPredicateTraversal(predicate, null);
12157 }
12158
12159 /**
12160 * {@hide}
12161 * Look for a child view that matches the specified predicate,
12162 * starting with the specified view and its descendents and then
12163 * recusively searching the ancestors and siblings of that view
12164 * until this view is reached.
12165 *
12166 * This method is useful in cases where the predicate does not match
12167 * a single unique view (perhaps multiple views use the same id)
12168 * and we are trying to find the view that is "closest" in scope to the
12169 * starting view.
12170 *
12171 * @param start The view to start from.
12172 * @param predicate The predicate to evaluate.
12173 * @return The first view that matches the predicate or null.
12174 */
12175 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12176 View childToSkip = null;
12177 for (;;) {
12178 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12179 if (view != null || start == this) {
12180 return view;
12181 }
12182
12183 ViewParent parent = start.getParent();
12184 if (parent == null || !(parent instanceof View)) {
12185 return null;
12186 }
12187
12188 childToSkip = start;
12189 start = (View) parent;
12190 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012191 }
12192
12193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012194 * Sets the identifier for this view. The identifier does not have to be
12195 * unique in this view's hierarchy. The identifier should be a positive
12196 * number.
12197 *
12198 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012199 * @see #getId()
12200 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012201 *
12202 * @param id a number used to identify the view
12203 *
12204 * @attr ref android.R.styleable#View_id
12205 */
12206 public void setId(int id) {
12207 mID = id;
12208 }
12209
12210 /**
12211 * {@hide}
12212 *
12213 * @param isRoot true if the view belongs to the root namespace, false
12214 * otherwise
12215 */
12216 public void setIsRootNamespace(boolean isRoot) {
12217 if (isRoot) {
12218 mPrivateFlags |= IS_ROOT_NAMESPACE;
12219 } else {
12220 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12221 }
12222 }
12223
12224 /**
12225 * {@hide}
12226 *
12227 * @return true if the view belongs to the root namespace, false otherwise
12228 */
12229 public boolean isRootNamespace() {
12230 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12231 }
12232
12233 /**
12234 * Returns this view's identifier.
12235 *
12236 * @return a positive integer used to identify the view or {@link #NO_ID}
12237 * if the view has no ID
12238 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012239 * @see #setId(int)
12240 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241 * @attr ref android.R.styleable#View_id
12242 */
12243 @ViewDebug.CapturedViewProperty
12244 public int getId() {
12245 return mID;
12246 }
12247
12248 /**
12249 * Returns this view's tag.
12250 *
12251 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012252 *
12253 * @see #setTag(Object)
12254 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012255 */
12256 @ViewDebug.ExportedProperty
12257 public Object getTag() {
12258 return mTag;
12259 }
12260
12261 /**
12262 * Sets the tag associated with this view. A tag can be used to mark
12263 * a view in its hierarchy and does not have to be unique within the
12264 * hierarchy. Tags can also be used to store data within a view without
12265 * resorting to another data structure.
12266 *
12267 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012268 *
12269 * @see #getTag()
12270 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271 */
12272 public void setTag(final Object tag) {
12273 mTag = tag;
12274 }
12275
12276 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012277 * Returns the tag associated with this view and the specified key.
12278 *
12279 * @param key The key identifying the tag
12280 *
12281 * @return the Object stored in this view as a tag
12282 *
12283 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012284 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012285 */
12286 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012287 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012288 return null;
12289 }
12290
12291 /**
12292 * Sets a tag associated with this view and a key. A tag can be used
12293 * to mark a view in its hierarchy and does not have to be unique within
12294 * the hierarchy. Tags can also be used to store data within a view
12295 * without resorting to another data structure.
12296 *
12297 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012298 * application to ensure it is unique (see the <a
12299 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12300 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012301 * the Android framework or not associated with any package will cause
12302 * an {@link IllegalArgumentException} to be thrown.
12303 *
12304 * @param key The key identifying the tag
12305 * @param tag An Object to tag the view with
12306 *
12307 * @throws IllegalArgumentException If they specified key is not valid
12308 *
12309 * @see #setTag(Object)
12310 * @see #getTag(int)
12311 */
12312 public void setTag(int key, final Object tag) {
12313 // If the package id is 0x00 or 0x01, it's either an undefined package
12314 // or a framework id
12315 if ((key >>> 24) < 2) {
12316 throw new IllegalArgumentException("The key must be an application-specific "
12317 + "resource id.");
12318 }
12319
Adam Powell2b2f6d62011-09-23 11:15:39 -070012320 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012321 }
12322
12323 /**
12324 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12325 * framework id.
12326 *
12327 * @hide
12328 */
12329 public void setTagInternal(int key, Object tag) {
12330 if ((key >>> 24) != 0x1) {
12331 throw new IllegalArgumentException("The key must be a framework-specific "
12332 + "resource id.");
12333 }
12334
Adam Powell2b2f6d62011-09-23 11:15:39 -070012335 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012336 }
12337
Adam Powell2b2f6d62011-09-23 11:15:39 -070012338 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012339 if (mKeyedTags == null) {
12340 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012341 }
12342
Adam Powell7db82ac2011-09-22 19:44:04 -070012343 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012344 }
12345
12346 /**
Romain Guy13922e02009-05-12 17:56:14 -070012347 * @param consistency The type of consistency. See ViewDebug for more information.
12348 *
12349 * @hide
12350 */
12351 protected boolean dispatchConsistencyCheck(int consistency) {
12352 return onConsistencyCheck(consistency);
12353 }
12354
12355 /**
12356 * Method that subclasses should implement to check their consistency. The type of
12357 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012358 *
Romain Guy13922e02009-05-12 17:56:14 -070012359 * @param consistency The type of consistency. See ViewDebug for more information.
12360 *
12361 * @throws IllegalStateException if the view is in an inconsistent state.
12362 *
12363 * @hide
12364 */
12365 protected boolean onConsistencyCheck(int consistency) {
12366 boolean result = true;
12367
12368 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12369 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12370
12371 if (checkLayout) {
12372 if (getParent() == null) {
12373 result = false;
12374 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12375 "View " + this + " does not have a parent.");
12376 }
12377
12378 if (mAttachInfo == null) {
12379 result = false;
12380 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12381 "View " + this + " is not attached to a window.");
12382 }
12383 }
12384
12385 if (checkDrawing) {
12386 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12387 // from their draw() method
12388
12389 if ((mPrivateFlags & DRAWN) != DRAWN &&
12390 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12391 result = false;
12392 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12393 "View " + this + " was invalidated but its drawing cache is valid.");
12394 }
12395 }
12396
12397 return result;
12398 }
12399
12400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012401 * Prints information about this view in the log output, with the tag
12402 * {@link #VIEW_LOG_TAG}.
12403 *
12404 * @hide
12405 */
12406 public void debug() {
12407 debug(0);
12408 }
12409
12410 /**
12411 * Prints information about this view in the log output, with the tag
12412 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12413 * indentation defined by the <code>depth</code>.
12414 *
12415 * @param depth the indentation level
12416 *
12417 * @hide
12418 */
12419 protected void debug(int depth) {
12420 String output = debugIndent(depth - 1);
12421
12422 output += "+ " + this;
12423 int id = getId();
12424 if (id != -1) {
12425 output += " (id=" + id + ")";
12426 }
12427 Object tag = getTag();
12428 if (tag != null) {
12429 output += " (tag=" + tag + ")";
12430 }
12431 Log.d(VIEW_LOG_TAG, output);
12432
12433 if ((mPrivateFlags & FOCUSED) != 0) {
12434 output = debugIndent(depth) + " FOCUSED";
12435 Log.d(VIEW_LOG_TAG, output);
12436 }
12437
12438 output = debugIndent(depth);
12439 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12440 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12441 + "} ";
12442 Log.d(VIEW_LOG_TAG, output);
12443
12444 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12445 || mPaddingBottom != 0) {
12446 output = debugIndent(depth);
12447 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12448 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12449 Log.d(VIEW_LOG_TAG, output);
12450 }
12451
12452 output = debugIndent(depth);
12453 output += "mMeasureWidth=" + mMeasuredWidth +
12454 " mMeasureHeight=" + mMeasuredHeight;
12455 Log.d(VIEW_LOG_TAG, output);
12456
12457 output = debugIndent(depth);
12458 if (mLayoutParams == null) {
12459 output += "BAD! no layout params";
12460 } else {
12461 output = mLayoutParams.debug(output);
12462 }
12463 Log.d(VIEW_LOG_TAG, output);
12464
12465 output = debugIndent(depth);
12466 output += "flags={";
12467 output += View.printFlags(mViewFlags);
12468 output += "}";
12469 Log.d(VIEW_LOG_TAG, output);
12470
12471 output = debugIndent(depth);
12472 output += "privateFlags={";
12473 output += View.printPrivateFlags(mPrivateFlags);
12474 output += "}";
12475 Log.d(VIEW_LOG_TAG, output);
12476 }
12477
12478 /**
12479 * Creates an string of whitespaces used for indentation.
12480 *
12481 * @param depth the indentation level
12482 * @return a String containing (depth * 2 + 3) * 2 white spaces
12483 *
12484 * @hide
12485 */
12486 protected static String debugIndent(int depth) {
12487 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12488 for (int i = 0; i < (depth * 2) + 3; i++) {
12489 spaces.append(' ').append(' ');
12490 }
12491 return spaces.toString();
12492 }
12493
12494 /**
12495 * <p>Return the offset of the widget's text baseline from the widget's top
12496 * boundary. If this widget does not support baseline alignment, this
12497 * method returns -1. </p>
12498 *
12499 * @return the offset of the baseline within the widget's bounds or -1
12500 * if baseline alignment is not supported
12501 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012502 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503 public int getBaseline() {
12504 return -1;
12505 }
12506
12507 /**
12508 * Call this when something has changed which has invalidated the
12509 * layout of this view. This will schedule a layout pass of the view
12510 * tree.
12511 */
12512 public void requestLayout() {
12513 if (ViewDebug.TRACE_HIERARCHY) {
12514 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12515 }
12516
12517 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012518 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012520 if (mParent != null) {
12521 if (mLayoutParams != null) {
12522 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12523 }
12524 if (!mParent.isLayoutRequested()) {
12525 mParent.requestLayout();
12526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012527 }
12528 }
12529
12530 /**
12531 * Forces this view to be laid out during the next layout pass.
12532 * This method does not call requestLayout() or forceLayout()
12533 * on the parent.
12534 */
12535 public void forceLayout() {
12536 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012537 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012538 }
12539
12540 /**
12541 * <p>
12542 * This is called to find out how big a view should be. The parent
12543 * supplies constraint information in the width and height parameters.
12544 * </p>
12545 *
12546 * <p>
12547 * The actual mesurement work of a view is performed in
12548 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12549 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12550 * </p>
12551 *
12552 *
12553 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12554 * parent
12555 * @param heightMeasureSpec Vertical space requirements as imposed by the
12556 * parent
12557 *
12558 * @see #onMeasure(int, int)
12559 */
12560 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12561 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12562 widthMeasureSpec != mOldWidthMeasureSpec ||
12563 heightMeasureSpec != mOldHeightMeasureSpec) {
12564
12565 // first clears the measured dimension flag
12566 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12567
12568 if (ViewDebug.TRACE_HIERARCHY) {
12569 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12570 }
12571
12572 // measure ourselves, this should set the measured dimension flag back
12573 onMeasure(widthMeasureSpec, heightMeasureSpec);
12574
12575 // flag not set, setMeasuredDimension() was not invoked, we raise
12576 // an exception to warn the developer
12577 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12578 throw new IllegalStateException("onMeasure() did not set the"
12579 + " measured dimension by calling"
12580 + " setMeasuredDimension()");
12581 }
12582
12583 mPrivateFlags |= LAYOUT_REQUIRED;
12584 }
12585
12586 mOldWidthMeasureSpec = widthMeasureSpec;
12587 mOldHeightMeasureSpec = heightMeasureSpec;
12588 }
12589
12590 /**
12591 * <p>
12592 * Measure the view and its content to determine the measured width and the
12593 * measured height. This method is invoked by {@link #measure(int, int)} and
12594 * should be overriden by subclasses to provide accurate and efficient
12595 * measurement of their contents.
12596 * </p>
12597 *
12598 * <p>
12599 * <strong>CONTRACT:</strong> When overriding this method, you
12600 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12601 * measured width and height of this view. Failure to do so will trigger an
12602 * <code>IllegalStateException</code>, thrown by
12603 * {@link #measure(int, int)}. Calling the superclass'
12604 * {@link #onMeasure(int, int)} is a valid use.
12605 * </p>
12606 *
12607 * <p>
12608 * The base class implementation of measure defaults to the background size,
12609 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12610 * override {@link #onMeasure(int, int)} to provide better measurements of
12611 * their content.
12612 * </p>
12613 *
12614 * <p>
12615 * If this method is overridden, it is the subclass's responsibility to make
12616 * sure the measured height and width are at least the view's minimum height
12617 * and width ({@link #getSuggestedMinimumHeight()} and
12618 * {@link #getSuggestedMinimumWidth()}).
12619 * </p>
12620 *
12621 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12622 * The requirements are encoded with
12623 * {@link android.view.View.MeasureSpec}.
12624 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12625 * The requirements are encoded with
12626 * {@link android.view.View.MeasureSpec}.
12627 *
12628 * @see #getMeasuredWidth()
12629 * @see #getMeasuredHeight()
12630 * @see #setMeasuredDimension(int, int)
12631 * @see #getSuggestedMinimumHeight()
12632 * @see #getSuggestedMinimumWidth()
12633 * @see android.view.View.MeasureSpec#getMode(int)
12634 * @see android.view.View.MeasureSpec#getSize(int)
12635 */
12636 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12637 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12638 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12639 }
12640
12641 /**
12642 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12643 * measured width and measured height. Failing to do so will trigger an
12644 * exception at measurement time.</p>
12645 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012646 * @param measuredWidth The measured width of this view. May be a complex
12647 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12648 * {@link #MEASURED_STATE_TOO_SMALL}.
12649 * @param measuredHeight The measured height of this view. May be a complex
12650 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12651 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012652 */
12653 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12654 mMeasuredWidth = measuredWidth;
12655 mMeasuredHeight = measuredHeight;
12656
12657 mPrivateFlags |= MEASURED_DIMENSION_SET;
12658 }
12659
12660 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012661 * Merge two states as returned by {@link #getMeasuredState()}.
12662 * @param curState The current state as returned from a view or the result
12663 * of combining multiple views.
12664 * @param newState The new view state to combine.
12665 * @return Returns a new integer reflecting the combination of the two
12666 * states.
12667 */
12668 public static int combineMeasuredStates(int curState, int newState) {
12669 return curState | newState;
12670 }
12671
12672 /**
12673 * Version of {@link #resolveSizeAndState(int, int, int)}
12674 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12675 */
12676 public static int resolveSize(int size, int measureSpec) {
12677 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12678 }
12679
12680 /**
12681 * Utility to reconcile a desired size and state, with constraints imposed
12682 * by a MeasureSpec. Will take the desired size, unless a different size
12683 * is imposed by the constraints. The returned value is a compound integer,
12684 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12685 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12686 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687 *
12688 * @param size How big the view wants to be
12689 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012690 * @return Size information bit mask as defined by
12691 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012692 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012693 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012694 int result = size;
12695 int specMode = MeasureSpec.getMode(measureSpec);
12696 int specSize = MeasureSpec.getSize(measureSpec);
12697 switch (specMode) {
12698 case MeasureSpec.UNSPECIFIED:
12699 result = size;
12700 break;
12701 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012702 if (specSize < size) {
12703 result = specSize | MEASURED_STATE_TOO_SMALL;
12704 } else {
12705 result = size;
12706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012707 break;
12708 case MeasureSpec.EXACTLY:
12709 result = specSize;
12710 break;
12711 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012712 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012713 }
12714
12715 /**
12716 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012717 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012718 * by the MeasureSpec.
12719 *
12720 * @param size Default size for this view
12721 * @param measureSpec Constraints imposed by the parent
12722 * @return The size this view should be.
12723 */
12724 public static int getDefaultSize(int size, int measureSpec) {
12725 int result = size;
12726 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012727 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012728
12729 switch (specMode) {
12730 case MeasureSpec.UNSPECIFIED:
12731 result = size;
12732 break;
12733 case MeasureSpec.AT_MOST:
12734 case MeasureSpec.EXACTLY:
12735 result = specSize;
12736 break;
12737 }
12738 return result;
12739 }
12740
12741 /**
12742 * Returns the suggested minimum height that the view should use. This
12743 * returns the maximum of the view's minimum height
12744 * and the background's minimum height
12745 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12746 * <p>
12747 * When being used in {@link #onMeasure(int, int)}, the caller should still
12748 * ensure the returned height is within the requirements of the parent.
12749 *
12750 * @return The suggested minimum height of the view.
12751 */
12752 protected int getSuggestedMinimumHeight() {
12753 int suggestedMinHeight = mMinHeight;
12754
12755 if (mBGDrawable != null) {
12756 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12757 if (suggestedMinHeight < bgMinHeight) {
12758 suggestedMinHeight = bgMinHeight;
12759 }
12760 }
12761
12762 return suggestedMinHeight;
12763 }
12764
12765 /**
12766 * Returns the suggested minimum width that the view should use. This
12767 * returns the maximum of the view's minimum width)
12768 * and the background's minimum width
12769 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12770 * <p>
12771 * When being used in {@link #onMeasure(int, int)}, the caller should still
12772 * ensure the returned width is within the requirements of the parent.
12773 *
12774 * @return The suggested minimum width of the view.
12775 */
12776 protected int getSuggestedMinimumWidth() {
12777 int suggestedMinWidth = mMinWidth;
12778
12779 if (mBGDrawable != null) {
12780 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12781 if (suggestedMinWidth < bgMinWidth) {
12782 suggestedMinWidth = bgMinWidth;
12783 }
12784 }
12785
12786 return suggestedMinWidth;
12787 }
12788
12789 /**
12790 * Sets the minimum height of the view. It is not guaranteed the view will
12791 * be able to achieve this minimum height (for example, if its parent layout
12792 * constrains it with less available height).
12793 *
12794 * @param minHeight The minimum height the view will try to be.
12795 */
12796 public void setMinimumHeight(int minHeight) {
12797 mMinHeight = minHeight;
12798 }
12799
12800 /**
12801 * Sets the minimum width of the view. It is not guaranteed the view will
12802 * be able to achieve this minimum width (for example, if its parent layout
12803 * constrains it with less available width).
12804 *
12805 * @param minWidth The minimum width the view will try to be.
12806 */
12807 public void setMinimumWidth(int minWidth) {
12808 mMinWidth = minWidth;
12809 }
12810
12811 /**
12812 * Get the animation currently associated with this view.
12813 *
12814 * @return The animation that is currently playing or
12815 * scheduled to play for this view.
12816 */
12817 public Animation getAnimation() {
12818 return mCurrentAnimation;
12819 }
12820
12821 /**
12822 * Start the specified animation now.
12823 *
12824 * @param animation the animation to start now
12825 */
12826 public void startAnimation(Animation animation) {
12827 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12828 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012829 invalidateParentCaches();
12830 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012831 }
12832
12833 /**
12834 * Cancels any animations for this view.
12835 */
12836 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012837 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012838 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012840 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012841 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842 }
12843
12844 /**
12845 * Sets the next animation to play for this view.
12846 * If you want the animation to play immediately, use
12847 * startAnimation. This method provides allows fine-grained
12848 * control over the start time and invalidation, but you
12849 * must make sure that 1) the animation has a start time set, and
12850 * 2) the view will be invalidated when the animation is supposed to
12851 * start.
12852 *
12853 * @param animation The next animation, or null.
12854 */
12855 public void setAnimation(Animation animation) {
12856 mCurrentAnimation = animation;
12857 if (animation != null) {
12858 animation.reset();
12859 }
12860 }
12861
12862 /**
12863 * Invoked by a parent ViewGroup to notify the start of the animation
12864 * currently associated with this view. If you override this method,
12865 * always call super.onAnimationStart();
12866 *
12867 * @see #setAnimation(android.view.animation.Animation)
12868 * @see #getAnimation()
12869 */
12870 protected void onAnimationStart() {
12871 mPrivateFlags |= ANIMATION_STARTED;
12872 }
12873
12874 /**
12875 * Invoked by a parent ViewGroup to notify the end of the animation
12876 * currently associated with this view. If you override this method,
12877 * always call super.onAnimationEnd();
12878 *
12879 * @see #setAnimation(android.view.animation.Animation)
12880 * @see #getAnimation()
12881 */
12882 protected void onAnimationEnd() {
12883 mPrivateFlags &= ~ANIMATION_STARTED;
12884 }
12885
12886 /**
12887 * Invoked if there is a Transform that involves alpha. Subclass that can
12888 * draw themselves with the specified alpha should return true, and then
12889 * respect that alpha when their onDraw() is called. If this returns false
12890 * then the view may be redirected to draw into an offscreen buffer to
12891 * fulfill the request, which will look fine, but may be slower than if the
12892 * subclass handles it internally. The default implementation returns false.
12893 *
12894 * @param alpha The alpha (0..255) to apply to the view's drawing
12895 * @return true if the view can draw with the specified alpha.
12896 */
12897 protected boolean onSetAlpha(int alpha) {
12898 return false;
12899 }
12900
12901 /**
12902 * This is used by the RootView to perform an optimization when
12903 * the view hierarchy contains one or several SurfaceView.
12904 * SurfaceView is always considered transparent, but its children are not,
12905 * therefore all View objects remove themselves from the global transparent
12906 * region (passed as a parameter to this function).
12907 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012908 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012909 *
12910 * @return Returns true if the effective visibility of the view at this
12911 * point is opaque, regardless of the transparent region; returns false
12912 * if it is possible for underlying windows to be seen behind the view.
12913 *
12914 * {@hide}
12915 */
12916 public boolean gatherTransparentRegion(Region region) {
12917 final AttachInfo attachInfo = mAttachInfo;
12918 if (region != null && attachInfo != null) {
12919 final int pflags = mPrivateFlags;
12920 if ((pflags & SKIP_DRAW) == 0) {
12921 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12922 // remove it from the transparent region.
12923 final int[] location = attachInfo.mTransparentLocation;
12924 getLocationInWindow(location);
12925 region.op(location[0], location[1], location[0] + mRight - mLeft,
12926 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12927 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12928 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12929 // exists, so we remove the background drawable's non-transparent
12930 // parts from this transparent region.
12931 applyDrawableToTransparentRegion(mBGDrawable, region);
12932 }
12933 }
12934 return true;
12935 }
12936
12937 /**
12938 * Play a sound effect for this view.
12939 *
12940 * <p>The framework will play sound effects for some built in actions, such as
12941 * clicking, but you may wish to play these effects in your widget,
12942 * for instance, for internal navigation.
12943 *
12944 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12945 * {@link #isSoundEffectsEnabled()} is true.
12946 *
12947 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12948 */
12949 public void playSoundEffect(int soundConstant) {
12950 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12951 return;
12952 }
12953 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12954 }
12955
12956 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012957 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012958 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012959 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012960 *
12961 * <p>The framework will provide haptic feedback for some built in actions,
12962 * such as long presses, but you may wish to provide feedback for your
12963 * own widget.
12964 *
12965 * <p>The feedback will only be performed if
12966 * {@link #isHapticFeedbackEnabled()} is true.
12967 *
12968 * @param feedbackConstant One of the constants defined in
12969 * {@link HapticFeedbackConstants}
12970 */
12971 public boolean performHapticFeedback(int feedbackConstant) {
12972 return performHapticFeedback(feedbackConstant, 0);
12973 }
12974
12975 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012976 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012977 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012978 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012979 *
12980 * @param feedbackConstant One of the constants defined in
12981 * {@link HapticFeedbackConstants}
12982 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12983 */
12984 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12985 if (mAttachInfo == null) {
12986 return false;
12987 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012988 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012989 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012990 && !isHapticFeedbackEnabled()) {
12991 return false;
12992 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012993 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12994 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012995 }
12996
12997 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012998 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012999 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13000 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013001 */
13002 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013003 if (visibility != mSystemUiVisibility) {
13004 mSystemUiVisibility = visibility;
13005 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13006 mParent.recomputeViewAttributes(this);
13007 }
Joe Onorato664644d2011-01-23 17:53:23 -080013008 }
13009 }
13010
13011 /**
13012 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013013 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13014 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013015 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013016 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013017 return mSystemUiVisibility;
13018 }
13019
Scott Mainec6331b2011-05-24 16:55:56 -070013020 /**
13021 * Set a listener to receive callbacks when the visibility of the system bar changes.
13022 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13023 */
Joe Onorato664644d2011-01-23 17:53:23 -080013024 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
13025 mOnSystemUiVisibilityChangeListener = l;
13026 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13027 mParent.recomputeViewAttributes(this);
13028 }
13029 }
13030
13031 /**
13032 */
13033 public void dispatchSystemUiVisibilityChanged(int visibility) {
13034 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080013035 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013036 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013037 }
13038 }
13039
13040 /**
Joe Malin32736f02011-01-19 16:14:20 -080013041 * Creates an image that the system displays during the drag and drop
13042 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13043 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13044 * appearance as the given View. The default also positions the center of the drag shadow
13045 * directly under the touch point. If no View is provided (the constructor with no parameters
13046 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13047 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13048 * default is an invisible drag shadow.
13049 * <p>
13050 * You are not required to use the View you provide to the constructor as the basis of the
13051 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13052 * anything you want as the drag shadow.
13053 * </p>
13054 * <p>
13055 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13056 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13057 * size and position of the drag shadow. It uses this data to construct a
13058 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13059 * so that your application can draw the shadow image in the Canvas.
13060 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013061 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013062 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013063 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013064
13065 /**
Joe Malin32736f02011-01-19 16:14:20 -080013066 * Constructs a shadow image builder based on a View. By default, the resulting drag
13067 * shadow will have the same appearance and dimensions as the View, with the touch point
13068 * over the center of the View.
13069 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013070 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013071 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013072 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013073 }
13074
Christopher Tate17ed60c2011-01-18 12:50:26 -080013075 /**
13076 * Construct a shadow builder object with no associated View. This
13077 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13078 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13079 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013080 * reference to any View object. If they are not overridden, then the result is an
13081 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013082 */
13083 public DragShadowBuilder() {
13084 mView = new WeakReference<View>(null);
13085 }
13086
13087 /**
13088 * Returns the View object that had been passed to the
13089 * {@link #View.DragShadowBuilder(View)}
13090 * constructor. If that View parameter was {@code null} or if the
13091 * {@link #View.DragShadowBuilder()}
13092 * constructor was used to instantiate the builder object, this method will return
13093 * null.
13094 *
13095 * @return The View object associate with this builder object.
13096 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013097 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013098 final public View getView() {
13099 return mView.get();
13100 }
13101
Christopher Tate2c095f32010-10-04 14:13:40 -070013102 /**
Joe Malin32736f02011-01-19 16:14:20 -080013103 * Provides the metrics for the shadow image. These include the dimensions of
13104 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013105 * be centered under the touch location while dragging.
13106 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013107 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013108 * same as the dimensions of the View itself and centers the shadow under
13109 * the touch point.
13110 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013111 *
Joe Malin32736f02011-01-19 16:14:20 -080013112 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13113 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13114 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13115 * image.
13116 *
13117 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13118 * shadow image that should be underneath the touch point during the drag and drop
13119 * operation. Your application must set {@link android.graphics.Point#x} to the
13120 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013121 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013122 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013123 final View view = mView.get();
13124 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013125 shadowSize.set(view.getWidth(), view.getHeight());
13126 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013127 } else {
13128 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13129 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013130 }
13131
13132 /**
Joe Malin32736f02011-01-19 16:14:20 -080013133 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13134 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013135 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013136 *
Joe Malin32736f02011-01-19 16:14:20 -080013137 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013138 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013139 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013140 final View view = mView.get();
13141 if (view != null) {
13142 view.draw(canvas);
13143 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013144 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013145 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013146 }
13147 }
13148
13149 /**
Joe Malin32736f02011-01-19 16:14:20 -080013150 * Starts a drag and drop operation. When your application calls this method, it passes a
13151 * {@link android.view.View.DragShadowBuilder} object to the system. The
13152 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13153 * to get metrics for the drag shadow, and then calls the object's
13154 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13155 * <p>
13156 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13157 * drag events to all the View objects in your application that are currently visible. It does
13158 * this either by calling the View object's drag listener (an implementation of
13159 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13160 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13161 * Both are passed a {@link android.view.DragEvent} object that has a
13162 * {@link android.view.DragEvent#getAction()} value of
13163 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13164 * </p>
13165 * <p>
13166 * Your application can invoke startDrag() on any attached View object. The View object does not
13167 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13168 * be related to the View the user selected for dragging.
13169 * </p>
13170 * @param data A {@link android.content.ClipData} object pointing to the data to be
13171 * transferred by the drag and drop operation.
13172 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13173 * drag shadow.
13174 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13175 * drop operation. This Object is put into every DragEvent object sent by the system during the
13176 * current drag.
13177 * <p>
13178 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13179 * to the target Views. For example, it can contain flags that differentiate between a
13180 * a copy operation and a move operation.
13181 * </p>
13182 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13183 * so the parameter should be set to 0.
13184 * @return {@code true} if the method completes successfully, or
13185 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13186 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013187 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013188 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013189 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013190 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013191 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013192 }
13193 boolean okay = false;
13194
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013195 Point shadowSize = new Point();
13196 Point shadowTouchPoint = new Point();
13197 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013198
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013199 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13200 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13201 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013202 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013203
Chris Tatea32dcf72010-10-14 12:13:50 -070013204 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013205 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13206 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013207 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013208 Surface surface = new Surface();
13209 try {
13210 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013211 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013212 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013213 + " surface=" + surface);
13214 if (token != null) {
13215 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013216 try {
Chris Tate6b391282010-10-14 15:48:59 -070013217 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013218 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013219 } finally {
13220 surface.unlockCanvasAndPost(canvas);
13221 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013222
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013223 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013224
13225 // Cache the local state object for delivery with DragEvents
13226 root.setLocalDragState(myLocalState);
13227
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013228 // repurpose 'shadowSize' for the last touch point
13229 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013230
Christopher Tatea53146c2010-09-07 11:57:52 -070013231 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013232 shadowSize.x, shadowSize.y,
13233 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013234 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013235
13236 // Off and running! Release our local surface instance; the drag
13237 // shadow surface is now managed by the system process.
13238 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013239 }
13240 } catch (Exception e) {
13241 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13242 surface.destroy();
13243 }
13244
13245 return okay;
13246 }
13247
Christopher Tatea53146c2010-09-07 11:57:52 -070013248 /**
Joe Malin32736f02011-01-19 16:14:20 -080013249 * Handles drag events sent by the system following a call to
13250 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13251 *<p>
13252 * When the system calls this method, it passes a
13253 * {@link android.view.DragEvent} object. A call to
13254 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13255 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13256 * operation.
13257 * @param event The {@link android.view.DragEvent} sent by the system.
13258 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13259 * in DragEvent, indicating the type of drag event represented by this object.
13260 * @return {@code true} if the method was successful, otherwise {@code false}.
13261 * <p>
13262 * The method should return {@code true} in response to an action type of
13263 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13264 * operation.
13265 * </p>
13266 * <p>
13267 * The method should also return {@code true} in response to an action type of
13268 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13269 * {@code false} if it didn't.
13270 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013271 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013272 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013273 return false;
13274 }
13275
13276 /**
Joe Malin32736f02011-01-19 16:14:20 -080013277 * Detects if this View is enabled and has a drag event listener.
13278 * If both are true, then it calls the drag event listener with the
13279 * {@link android.view.DragEvent} it received. If the drag event listener returns
13280 * {@code true}, then dispatchDragEvent() returns {@code true}.
13281 * <p>
13282 * For all other cases, the method calls the
13283 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13284 * method and returns its result.
13285 * </p>
13286 * <p>
13287 * This ensures that a drag event is always consumed, even if the View does not have a drag
13288 * event listener. However, if the View has a listener and the listener returns true, then
13289 * onDragEvent() is not called.
13290 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013291 */
13292 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013293 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013294 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13295 && mOnDragListener.onDrag(this, event)) {
13296 return true;
13297 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013298 return onDragEvent(event);
13299 }
13300
Christopher Tate3d4bf172011-03-28 16:16:46 -070013301 boolean canAcceptDrag() {
13302 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13303 }
13304
Christopher Tatea53146c2010-09-07 11:57:52 -070013305 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013306 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13307 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013308 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013309 */
13310 public void onCloseSystemDialogs(String reason) {
13311 }
Joe Malin32736f02011-01-19 16:14:20 -080013312
Dianne Hackbornffa42482009-09-23 22:20:11 -070013313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013314 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013315 * update a Region being computed for
13316 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013317 * that any non-transparent parts of the Drawable are removed from the
13318 * given transparent region.
13319 *
13320 * @param dr The Drawable whose transparency is to be applied to the region.
13321 * @param region A Region holding the current transparency information,
13322 * where any parts of the region that are set are considered to be
13323 * transparent. On return, this region will be modified to have the
13324 * transparency information reduced by the corresponding parts of the
13325 * Drawable that are not transparent.
13326 * {@hide}
13327 */
13328 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13329 if (DBG) {
13330 Log.i("View", "Getting transparent region for: " + this);
13331 }
13332 final Region r = dr.getTransparentRegion();
13333 final Rect db = dr.getBounds();
13334 final AttachInfo attachInfo = mAttachInfo;
13335 if (r != null && attachInfo != null) {
13336 final int w = getRight()-getLeft();
13337 final int h = getBottom()-getTop();
13338 if (db.left > 0) {
13339 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13340 r.op(0, 0, db.left, h, Region.Op.UNION);
13341 }
13342 if (db.right < w) {
13343 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13344 r.op(db.right, 0, w, h, Region.Op.UNION);
13345 }
13346 if (db.top > 0) {
13347 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13348 r.op(0, 0, w, db.top, Region.Op.UNION);
13349 }
13350 if (db.bottom < h) {
13351 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13352 r.op(0, db.bottom, w, h, Region.Op.UNION);
13353 }
13354 final int[] location = attachInfo.mTransparentLocation;
13355 getLocationInWindow(location);
13356 r.translate(location[0], location[1]);
13357 region.op(r, Region.Op.INTERSECT);
13358 } else {
13359 region.op(db, Region.Op.DIFFERENCE);
13360 }
13361 }
13362
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013363 private void checkForLongClick(int delayOffset) {
13364 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13365 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013367 if (mPendingCheckForLongPress == null) {
13368 mPendingCheckForLongPress = new CheckForLongPress();
13369 }
13370 mPendingCheckForLongPress.rememberWindowAttachCount();
13371 postDelayed(mPendingCheckForLongPress,
13372 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013374 }
13375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013376 /**
13377 * Inflate a view from an XML resource. This convenience method wraps the {@link
13378 * LayoutInflater} class, which provides a full range of options for view inflation.
13379 *
13380 * @param context The Context object for your activity or application.
13381 * @param resource The resource ID to inflate
13382 * @param root A view group that will be the parent. Used to properly inflate the
13383 * layout_* parameters.
13384 * @see LayoutInflater
13385 */
13386 public static View inflate(Context context, int resource, ViewGroup root) {
13387 LayoutInflater factory = LayoutInflater.from(context);
13388 return factory.inflate(resource, root);
13389 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013391 /**
Adam Powell637d3372010-08-25 14:37:03 -070013392 * Scroll the view with standard behavior for scrolling beyond the normal
13393 * content boundaries. Views that call this method should override
13394 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13395 * results of an over-scroll operation.
13396 *
13397 * Views can use this method to handle any touch or fling-based scrolling.
13398 *
13399 * @param deltaX Change in X in pixels
13400 * @param deltaY Change in Y in pixels
13401 * @param scrollX Current X scroll value in pixels before applying deltaX
13402 * @param scrollY Current Y scroll value in pixels before applying deltaY
13403 * @param scrollRangeX Maximum content scroll range along the X axis
13404 * @param scrollRangeY Maximum content scroll range along the Y axis
13405 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13406 * along the X axis.
13407 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13408 * along the Y axis.
13409 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13410 * @return true if scrolling was clamped to an over-scroll boundary along either
13411 * axis, false otherwise.
13412 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013413 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013414 protected boolean overScrollBy(int deltaX, int deltaY,
13415 int scrollX, int scrollY,
13416 int scrollRangeX, int scrollRangeY,
13417 int maxOverScrollX, int maxOverScrollY,
13418 boolean isTouchEvent) {
13419 final int overScrollMode = mOverScrollMode;
13420 final boolean canScrollHorizontal =
13421 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13422 final boolean canScrollVertical =
13423 computeVerticalScrollRange() > computeVerticalScrollExtent();
13424 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13425 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13426 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13427 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13428
13429 int newScrollX = scrollX + deltaX;
13430 if (!overScrollHorizontal) {
13431 maxOverScrollX = 0;
13432 }
13433
13434 int newScrollY = scrollY + deltaY;
13435 if (!overScrollVertical) {
13436 maxOverScrollY = 0;
13437 }
13438
13439 // Clamp values if at the limits and record
13440 final int left = -maxOverScrollX;
13441 final int right = maxOverScrollX + scrollRangeX;
13442 final int top = -maxOverScrollY;
13443 final int bottom = maxOverScrollY + scrollRangeY;
13444
13445 boolean clampedX = false;
13446 if (newScrollX > right) {
13447 newScrollX = right;
13448 clampedX = true;
13449 } else if (newScrollX < left) {
13450 newScrollX = left;
13451 clampedX = true;
13452 }
13453
13454 boolean clampedY = false;
13455 if (newScrollY > bottom) {
13456 newScrollY = bottom;
13457 clampedY = true;
13458 } else if (newScrollY < top) {
13459 newScrollY = top;
13460 clampedY = true;
13461 }
13462
13463 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13464
13465 return clampedX || clampedY;
13466 }
13467
13468 /**
13469 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13470 * respond to the results of an over-scroll operation.
13471 *
13472 * @param scrollX New X scroll value in pixels
13473 * @param scrollY New Y scroll value in pixels
13474 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13475 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13476 */
13477 protected void onOverScrolled(int scrollX, int scrollY,
13478 boolean clampedX, boolean clampedY) {
13479 // Intentionally empty.
13480 }
13481
13482 /**
13483 * Returns the over-scroll mode for this view. The result will be
13484 * one of {@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 * @return This view's over-scroll mode.
13489 */
13490 public int getOverScrollMode() {
13491 return mOverScrollMode;
13492 }
13493
13494 /**
13495 * Set the over-scroll mode for this view. Valid over-scroll modes are
13496 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13497 * (allow over-scrolling only if the view content is larger than the container),
13498 * or {@link #OVER_SCROLL_NEVER}.
13499 *
13500 * Setting the over-scroll mode of a view will have an effect only if the
13501 * view is capable of scrolling.
13502 *
13503 * @param overScrollMode The new over-scroll mode for this view.
13504 */
13505 public void setOverScrollMode(int overScrollMode) {
13506 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13507 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13508 overScrollMode != OVER_SCROLL_NEVER) {
13509 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13510 }
13511 mOverScrollMode = overScrollMode;
13512 }
13513
13514 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013515 * Gets a scale factor that determines the distance the view should scroll
13516 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13517 * @return The vertical scroll scale factor.
13518 * @hide
13519 */
13520 protected float getVerticalScrollFactor() {
13521 if (mVerticalScrollFactor == 0) {
13522 TypedValue outValue = new TypedValue();
13523 if (!mContext.getTheme().resolveAttribute(
13524 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13525 throw new IllegalStateException(
13526 "Expected theme to define listPreferredItemHeight.");
13527 }
13528 mVerticalScrollFactor = outValue.getDimension(
13529 mContext.getResources().getDisplayMetrics());
13530 }
13531 return mVerticalScrollFactor;
13532 }
13533
13534 /**
13535 * Gets a scale factor that determines the distance the view should scroll
13536 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13537 * @return The horizontal scroll scale factor.
13538 * @hide
13539 */
13540 protected float getHorizontalScrollFactor() {
13541 // TODO: Should use something else.
13542 return getVerticalScrollFactor();
13543 }
13544
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013545 /**
13546 * Return the value specifying the text direction or policy that was set with
13547 * {@link #setTextDirection(int)}.
13548 *
13549 * @return the defined text direction. It can be one of:
13550 *
13551 * {@link #TEXT_DIRECTION_INHERIT},
13552 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13553 * {@link #TEXT_DIRECTION_ANY_RTL},
13554 * {@link #TEXT_DIRECTION_LTR},
13555 * {@link #TEXT_DIRECTION_RTL},
13556 *
13557 * @hide
13558 */
13559 public int getTextDirection() {
13560 return mTextDirection;
13561 }
13562
13563 /**
13564 * Set the text direction.
13565 *
13566 * @param textDirection the direction to set. Should be one of:
13567 *
13568 * {@link #TEXT_DIRECTION_INHERIT},
13569 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13570 * {@link #TEXT_DIRECTION_ANY_RTL},
13571 * {@link #TEXT_DIRECTION_LTR},
13572 * {@link #TEXT_DIRECTION_RTL},
13573 *
13574 * @hide
13575 */
13576 public void setTextDirection(int textDirection) {
13577 if (textDirection != mTextDirection) {
13578 mTextDirection = textDirection;
13579 resetResolvedTextDirection();
13580 requestLayout();
13581 }
13582 }
13583
13584 /**
13585 * Return the resolved text direction.
13586 *
13587 * @return the resolved text direction. Return one of:
13588 *
Doug Feltcb3791202011-07-07 11:57:48 -070013589 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13590 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013591 * {@link #TEXT_DIRECTION_LTR},
13592 * {@link #TEXT_DIRECTION_RTL},
13593 *
13594 * @hide
13595 */
13596 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013597 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013598 resolveTextDirection();
13599 }
13600 return mResolvedTextDirection;
13601 }
13602
13603 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013604 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013605 *
13606 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013607 */
13608 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013609 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13610 mResolvedTextDirection = mTextDirection;
13611 return;
13612 }
13613 if (mParent != null && mParent instanceof ViewGroup) {
13614 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13615 return;
13616 }
13617 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013618 }
13619
13620 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013621 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013622 *
13623 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013624 */
13625 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013626 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013627 }
13628
Chet Haaseb39f0512011-05-24 14:36:40 -070013629 //
13630 // Properties
13631 //
13632 /**
13633 * A Property wrapper around the <code>alpha</code> functionality handled by the
13634 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13635 */
Romain Guy02ccac62011-06-24 13:20:23 -070013636 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013637 @Override
13638 public void setValue(View object, float value) {
13639 object.setAlpha(value);
13640 }
13641
13642 @Override
13643 public Float get(View object) {
13644 return object.getAlpha();
13645 }
13646 };
13647
13648 /**
13649 * A Property wrapper around the <code>translationX</code> functionality handled by the
13650 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13651 */
13652 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13653 @Override
13654 public void setValue(View object, float value) {
13655 object.setTranslationX(value);
13656 }
13657
13658 @Override
13659 public Float get(View object) {
13660 return object.getTranslationX();
13661 }
13662 };
13663
13664 /**
13665 * A Property wrapper around the <code>translationY</code> functionality handled by the
13666 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13667 */
13668 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13669 @Override
13670 public void setValue(View object, float value) {
13671 object.setTranslationY(value);
13672 }
13673
13674 @Override
13675 public Float get(View object) {
13676 return object.getTranslationY();
13677 }
13678 };
13679
13680 /**
13681 * A Property wrapper around the <code>x</code> functionality handled by the
13682 * {@link View#setX(float)} and {@link View#getX()} methods.
13683 */
13684 public static Property<View, Float> X = new FloatProperty<View>("x") {
13685 @Override
13686 public void setValue(View object, float value) {
13687 object.setX(value);
13688 }
13689
13690 @Override
13691 public Float get(View object) {
13692 return object.getX();
13693 }
13694 };
13695
13696 /**
13697 * A Property wrapper around the <code>y</code> functionality handled by the
13698 * {@link View#setY(float)} and {@link View#getY()} methods.
13699 */
13700 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13701 @Override
13702 public void setValue(View object, float value) {
13703 object.setY(value);
13704 }
13705
13706 @Override
13707 public Float get(View object) {
13708 return object.getY();
13709 }
13710 };
13711
13712 /**
13713 * A Property wrapper around the <code>rotation</code> functionality handled by the
13714 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13715 */
13716 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13717 @Override
13718 public void setValue(View object, float value) {
13719 object.setRotation(value);
13720 }
13721
13722 @Override
13723 public Float get(View object) {
13724 return object.getRotation();
13725 }
13726 };
13727
13728 /**
13729 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13730 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13731 */
13732 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13733 @Override
13734 public void setValue(View object, float value) {
13735 object.setRotationX(value);
13736 }
13737
13738 @Override
13739 public Float get(View object) {
13740 return object.getRotationX();
13741 }
13742 };
13743
13744 /**
13745 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13746 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13747 */
13748 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13749 @Override
13750 public void setValue(View object, float value) {
13751 object.setRotationY(value);
13752 }
13753
13754 @Override
13755 public Float get(View object) {
13756 return object.getRotationY();
13757 }
13758 };
13759
13760 /**
13761 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13762 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13763 */
13764 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13765 @Override
13766 public void setValue(View object, float value) {
13767 object.setScaleX(value);
13768 }
13769
13770 @Override
13771 public Float get(View object) {
13772 return object.getScaleX();
13773 }
13774 };
13775
13776 /**
13777 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13778 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13779 */
13780 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13781 @Override
13782 public void setValue(View object, float value) {
13783 object.setScaleY(value);
13784 }
13785
13786 @Override
13787 public Float get(View object) {
13788 return object.getScaleY();
13789 }
13790 };
13791
Jeff Brown33bbfd22011-02-24 20:55:35 -080013792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013793 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13794 * Each MeasureSpec represents a requirement for either the width or the height.
13795 * A MeasureSpec is comprised of a size and a mode. There are three possible
13796 * modes:
13797 * <dl>
13798 * <dt>UNSPECIFIED</dt>
13799 * <dd>
13800 * The parent has not imposed any constraint on the child. It can be whatever size
13801 * it wants.
13802 * </dd>
13803 *
13804 * <dt>EXACTLY</dt>
13805 * <dd>
13806 * The parent has determined an exact size for the child. The child is going to be
13807 * given those bounds regardless of how big it wants to be.
13808 * </dd>
13809 *
13810 * <dt>AT_MOST</dt>
13811 * <dd>
13812 * The child can be as large as it wants up to the specified size.
13813 * </dd>
13814 * </dl>
13815 *
13816 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13817 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13818 */
13819 public static class MeasureSpec {
13820 private static final int MODE_SHIFT = 30;
13821 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13822
13823 /**
13824 * Measure specification mode: The parent has not imposed any constraint
13825 * on the child. It can be whatever size it wants.
13826 */
13827 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13828
13829 /**
13830 * Measure specification mode: The parent has determined an exact size
13831 * for the child. The child is going to be given those bounds regardless
13832 * of how big it wants to be.
13833 */
13834 public static final int EXACTLY = 1 << MODE_SHIFT;
13835
13836 /**
13837 * Measure specification mode: The child can be as large as it wants up
13838 * to the specified size.
13839 */
13840 public static final int AT_MOST = 2 << MODE_SHIFT;
13841
13842 /**
13843 * Creates a measure specification based on the supplied size and mode.
13844 *
13845 * The mode must always be one of the following:
13846 * <ul>
13847 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13848 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13849 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13850 * </ul>
13851 *
13852 * @param size the size of the measure specification
13853 * @param mode the mode of the measure specification
13854 * @return the measure specification based on size and mode
13855 */
13856 public static int makeMeasureSpec(int size, int mode) {
13857 return size + mode;
13858 }
13859
13860 /**
13861 * Extracts the mode from the supplied measure specification.
13862 *
13863 * @param measureSpec the measure specification to extract the mode from
13864 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13865 * {@link android.view.View.MeasureSpec#AT_MOST} or
13866 * {@link android.view.View.MeasureSpec#EXACTLY}
13867 */
13868 public static int getMode(int measureSpec) {
13869 return (measureSpec & MODE_MASK);
13870 }
13871
13872 /**
13873 * Extracts the size from the supplied measure specification.
13874 *
13875 * @param measureSpec the measure specification to extract the size from
13876 * @return the size in pixels defined in the supplied measure specification
13877 */
13878 public static int getSize(int measureSpec) {
13879 return (measureSpec & ~MODE_MASK);
13880 }
13881
13882 /**
13883 * Returns a String representation of the specified measure
13884 * specification.
13885 *
13886 * @param measureSpec the measure specification to convert to a String
13887 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13888 */
13889 public static String toString(int measureSpec) {
13890 int mode = getMode(measureSpec);
13891 int size = getSize(measureSpec);
13892
13893 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13894
13895 if (mode == UNSPECIFIED)
13896 sb.append("UNSPECIFIED ");
13897 else if (mode == EXACTLY)
13898 sb.append("EXACTLY ");
13899 else if (mode == AT_MOST)
13900 sb.append("AT_MOST ");
13901 else
13902 sb.append(mode).append(" ");
13903
13904 sb.append(size);
13905 return sb.toString();
13906 }
13907 }
13908
13909 class CheckForLongPress implements Runnable {
13910
13911 private int mOriginalWindowAttachCount;
13912
13913 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013914 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013915 && mOriginalWindowAttachCount == mWindowAttachCount) {
13916 if (performLongClick()) {
13917 mHasPerformedLongPress = true;
13918 }
13919 }
13920 }
13921
13922 public void rememberWindowAttachCount() {
13923 mOriginalWindowAttachCount = mWindowAttachCount;
13924 }
13925 }
Joe Malin32736f02011-01-19 16:14:20 -080013926
Adam Powelle14579b2009-12-16 18:39:52 -080013927 private final class CheckForTap implements Runnable {
13928 public void run() {
13929 mPrivateFlags &= ~PREPRESSED;
13930 mPrivateFlags |= PRESSED;
13931 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013932 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013933 }
13934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935
Adam Powella35d7682010-03-12 14:48:13 -080013936 private final class PerformClick implements Runnable {
13937 public void run() {
13938 performClick();
13939 }
13940 }
13941
Dianne Hackborn63042d62011-01-26 18:56:29 -080013942 /** @hide */
13943 public void hackTurnOffWindowResizeAnim(boolean off) {
13944 mAttachInfo.mTurnOffWindowResizeAnim = off;
13945 }
Joe Malin32736f02011-01-19 16:14:20 -080013946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013947 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013948 * This method returns a ViewPropertyAnimator object, which can be used to animate
13949 * specific properties on this View.
13950 *
13951 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13952 */
13953 public ViewPropertyAnimator animate() {
13954 if (mAnimator == null) {
13955 mAnimator = new ViewPropertyAnimator(this);
13956 }
13957 return mAnimator;
13958 }
13959
13960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 * Interface definition for a callback to be invoked when a key event is
13962 * dispatched to this view. The callback will be invoked before the key
13963 * event is given to the view.
13964 */
13965 public interface OnKeyListener {
13966 /**
13967 * Called when a key is dispatched to a view. This allows listeners to
13968 * get a chance to respond before the target view.
13969 *
13970 * @param v The view the key has been dispatched to.
13971 * @param keyCode The code for the physical key that was pressed
13972 * @param event The KeyEvent object containing full information about
13973 * the event.
13974 * @return True if the listener has consumed the event, false otherwise.
13975 */
13976 boolean onKey(View v, int keyCode, KeyEvent event);
13977 }
13978
13979 /**
13980 * Interface definition for a callback to be invoked when a touch event is
13981 * dispatched to this view. The callback will be invoked before the touch
13982 * event is given to the view.
13983 */
13984 public interface OnTouchListener {
13985 /**
13986 * Called when a touch event is dispatched to a view. This allows listeners to
13987 * get a chance to respond before the target view.
13988 *
13989 * @param v The view the touch event has been dispatched to.
13990 * @param event The MotionEvent object containing full information about
13991 * the event.
13992 * @return True if the listener has consumed the event, false otherwise.
13993 */
13994 boolean onTouch(View v, MotionEvent event);
13995 }
13996
13997 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013998 * Interface definition for a callback to be invoked when a hover event is
13999 * dispatched to this view. The callback will be invoked before the hover
14000 * event is given to the view.
14001 */
14002 public interface OnHoverListener {
14003 /**
14004 * Called when a hover event is dispatched to a view. This allows listeners to
14005 * get a chance to respond before the target view.
14006 *
14007 * @param v The view the hover event has been dispatched to.
14008 * @param event The MotionEvent object containing full information about
14009 * the event.
14010 * @return True if the listener has consumed the event, false otherwise.
14011 */
14012 boolean onHover(View v, MotionEvent event);
14013 }
14014
14015 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014016 * Interface definition for a callback to be invoked when a generic motion event is
14017 * dispatched to this view. The callback will be invoked before the generic motion
14018 * event is given to the view.
14019 */
14020 public interface OnGenericMotionListener {
14021 /**
14022 * Called when a generic motion event is dispatched to a view. This allows listeners to
14023 * get a chance to respond before the target view.
14024 *
14025 * @param v The view the generic motion event has been dispatched to.
14026 * @param event The MotionEvent object containing full information about
14027 * the event.
14028 * @return True if the listener has consumed the event, false otherwise.
14029 */
14030 boolean onGenericMotion(View v, MotionEvent event);
14031 }
14032
14033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014034 * Interface definition for a callback to be invoked when a view has been clicked and held.
14035 */
14036 public interface OnLongClickListener {
14037 /**
14038 * Called when a view has been clicked and held.
14039 *
14040 * @param v The view that was clicked and held.
14041 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014042 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014043 */
14044 boolean onLongClick(View v);
14045 }
14046
14047 /**
Chris Tate32affef2010-10-18 15:29:21 -070014048 * Interface definition for a callback to be invoked when a drag is being dispatched
14049 * to this view. The callback will be invoked before the hosting view's own
14050 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14051 * onDrag(event) behavior, it should return 'false' from this callback.
14052 */
14053 public interface OnDragListener {
14054 /**
14055 * Called when a drag event is dispatched to a view. This allows listeners
14056 * to get a chance to override base View behavior.
14057 *
Joe Malin32736f02011-01-19 16:14:20 -080014058 * @param v The View that received the drag event.
14059 * @param event The {@link android.view.DragEvent} object for the drag event.
14060 * @return {@code true} if the drag event was handled successfully, or {@code false}
14061 * if the drag event was not handled. Note that {@code false} will trigger the View
14062 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014063 */
14064 boolean onDrag(View v, DragEvent event);
14065 }
14066
14067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014068 * Interface definition for a callback to be invoked when the focus state of
14069 * a view changed.
14070 */
14071 public interface OnFocusChangeListener {
14072 /**
14073 * Called when the focus state of a view has changed.
14074 *
14075 * @param v The view whose state has changed.
14076 * @param hasFocus The new focus state of v.
14077 */
14078 void onFocusChange(View v, boolean hasFocus);
14079 }
14080
14081 /**
14082 * Interface definition for a callback to be invoked when a view is clicked.
14083 */
14084 public interface OnClickListener {
14085 /**
14086 * Called when a view has been clicked.
14087 *
14088 * @param v The view that was clicked.
14089 */
14090 void onClick(View v);
14091 }
14092
14093 /**
14094 * Interface definition for a callback to be invoked when the context menu
14095 * for this view is being built.
14096 */
14097 public interface OnCreateContextMenuListener {
14098 /**
14099 * Called when the context menu for this view is being built. It is not
14100 * safe to hold onto the menu after this method returns.
14101 *
14102 * @param menu The context menu that is being built
14103 * @param v The view for which the context menu is being built
14104 * @param menuInfo Extra information about the item for which the
14105 * context menu should be shown. This information will vary
14106 * depending on the class of v.
14107 */
14108 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14109 }
14110
Joe Onorato664644d2011-01-23 17:53:23 -080014111 /**
14112 * Interface definition for a callback to be invoked when the status bar changes
14113 * visibility.
14114 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014115 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014116 */
14117 public interface OnSystemUiVisibilityChangeListener {
14118 /**
14119 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014120 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014121 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014122 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14123 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014124 */
14125 public void onSystemUiVisibilityChange(int visibility);
14126 }
14127
Adam Powell4afd62b2011-02-18 15:02:18 -080014128 /**
14129 * Interface definition for a callback to be invoked when this view is attached
14130 * or detached from its window.
14131 */
14132 public interface OnAttachStateChangeListener {
14133 /**
14134 * Called when the view is attached to a window.
14135 * @param v The view that was attached
14136 */
14137 public void onViewAttachedToWindow(View v);
14138 /**
14139 * Called when the view is detached from a window.
14140 * @param v The view that was detached
14141 */
14142 public void onViewDetachedFromWindow(View v);
14143 }
14144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014145 private final class UnsetPressedState implements Runnable {
14146 public void run() {
14147 setPressed(false);
14148 }
14149 }
14150
14151 /**
14152 * Base class for derived classes that want to save and restore their own
14153 * state in {@link android.view.View#onSaveInstanceState()}.
14154 */
14155 public static class BaseSavedState extends AbsSavedState {
14156 /**
14157 * Constructor used when reading from a parcel. Reads the state of the superclass.
14158 *
14159 * @param source
14160 */
14161 public BaseSavedState(Parcel source) {
14162 super(source);
14163 }
14164
14165 /**
14166 * Constructor called by derived classes when creating their SavedState objects
14167 *
14168 * @param superState The state of the superclass of this view
14169 */
14170 public BaseSavedState(Parcelable superState) {
14171 super(superState);
14172 }
14173
14174 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14175 new Parcelable.Creator<BaseSavedState>() {
14176 public BaseSavedState createFromParcel(Parcel in) {
14177 return new BaseSavedState(in);
14178 }
14179
14180 public BaseSavedState[] newArray(int size) {
14181 return new BaseSavedState[size];
14182 }
14183 };
14184 }
14185
14186 /**
14187 * A set of information given to a view when it is attached to its parent
14188 * window.
14189 */
14190 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 interface Callbacks {
14192 void playSoundEffect(int effectId);
14193 boolean performHapticFeedback(int effectId, boolean always);
14194 }
14195
14196 /**
14197 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14198 * to a Handler. This class contains the target (View) to invalidate and
14199 * the coordinates of the dirty rectangle.
14200 *
14201 * For performance purposes, this class also implements a pool of up to
14202 * POOL_LIMIT objects that get reused. This reduces memory allocations
14203 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014204 */
Romain Guyd928d682009-03-31 17:52:16 -070014205 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014207 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14208 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014209 public InvalidateInfo newInstance() {
14210 return new InvalidateInfo();
14211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212
Romain Guyd928d682009-03-31 17:52:16 -070014213 public void onAcquired(InvalidateInfo element) {
14214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215
Romain Guyd928d682009-03-31 17:52:16 -070014216 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014217 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014218 }
14219 }, POOL_LIMIT)
14220 );
14221
14222 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014223 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224
14225 View target;
14226
14227 int left;
14228 int top;
14229 int right;
14230 int bottom;
14231
Romain Guyd928d682009-03-31 17:52:16 -070014232 public void setNextPoolable(InvalidateInfo element) {
14233 mNext = element;
14234 }
14235
14236 public InvalidateInfo getNextPoolable() {
14237 return mNext;
14238 }
14239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014241 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014242 }
14243
14244 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014245 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014247
14248 public boolean isPooled() {
14249 return mIsPooled;
14250 }
14251
14252 public void setPooled(boolean isPooled) {
14253 mIsPooled = isPooled;
14254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 }
14256
14257 final IWindowSession mSession;
14258
14259 final IWindow mWindow;
14260
14261 final IBinder mWindowToken;
14262
14263 final Callbacks mRootCallbacks;
14264
Romain Guy59a12ca2011-06-09 17:48:21 -070014265 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014267 /**
14268 * The top view of the hierarchy.
14269 */
14270 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 IBinder mPanelParentWindowToken;
14273 Surface mSurface;
14274
Romain Guyb051e892010-09-28 19:09:36 -070014275 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014276 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014277 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014279 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014280 * Scale factor used by the compatibility mode
14281 */
14282 float mApplicationScale;
14283
14284 /**
14285 * Indicates whether the application is in compatibility mode
14286 */
14287 boolean mScalingRequired;
14288
14289 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014290 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014291 */
14292 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014293
Dianne Hackborn63042d62011-01-26 18:56:29 -080014294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 * Left position of this view's window
14296 */
14297 int mWindowLeft;
14298
14299 /**
14300 * Top position of this view's window
14301 */
14302 int mWindowTop;
14303
14304 /**
Adam Powell26153a32010-11-08 15:22:27 -080014305 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014306 */
Adam Powell26153a32010-11-08 15:22:27 -080014307 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014308
14309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014310 * For windows that are full-screen but using insets to layout inside
14311 * of the screen decorations, these are the current insets for the
14312 * content of the window.
14313 */
14314 final Rect mContentInsets = new Rect();
14315
14316 /**
14317 * For windows that are full-screen but using insets to layout inside
14318 * of the screen decorations, these are the current insets for the
14319 * actual visible parts of the window.
14320 */
14321 final Rect mVisibleInsets = new Rect();
14322
14323 /**
14324 * The internal insets given by this window. This value is
14325 * supplied by the client (through
14326 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14327 * be given to the window manager when changed to be used in laying
14328 * out windows behind it.
14329 */
14330 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14331 = new ViewTreeObserver.InternalInsetsInfo();
14332
14333 /**
14334 * All views in the window's hierarchy that serve as scroll containers,
14335 * used to determine if the window can be resized or must be panned
14336 * to adjust for a soft input area.
14337 */
14338 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14339
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014340 final KeyEvent.DispatcherState mKeyDispatchState
14341 = new KeyEvent.DispatcherState();
14342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 /**
14344 * Indicates whether the view's window currently has the focus.
14345 */
14346 boolean mHasWindowFocus;
14347
14348 /**
14349 * The current visibility of the window.
14350 */
14351 int mWindowVisibility;
14352
14353 /**
14354 * Indicates the time at which drawing started to occur.
14355 */
14356 long mDrawingTime;
14357
14358 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014359 * Indicates whether or not ignoring the DIRTY_MASK flags.
14360 */
14361 boolean mIgnoreDirtyState;
14362
14363 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014364 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14365 * to avoid clearing that flag prematurely.
14366 */
14367 boolean mSetIgnoreDirtyState = false;
14368
14369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014370 * Indicates whether the view's window is currently in touch mode.
14371 */
14372 boolean mInTouchMode;
14373
14374 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014375 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014376 * the next time it performs a traversal
14377 */
14378 boolean mRecomputeGlobalAttributes;
14379
14380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014381 * Set during a traveral if any views want to keep the screen on.
14382 */
14383 boolean mKeepScreenOn;
14384
14385 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014386 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14387 */
14388 int mSystemUiVisibility;
14389
14390 /**
14391 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14392 * attached.
14393 */
14394 boolean mHasSystemUiListeners;
14395
14396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014397 * Set if the visibility of any views has changed.
14398 */
14399 boolean mViewVisibilityChanged;
14400
14401 /**
14402 * Set to true if a view has been scrolled.
14403 */
14404 boolean mViewScrollChanged;
14405
14406 /**
14407 * Global to the view hierarchy used as a temporary for dealing with
14408 * x/y points in the transparent region computations.
14409 */
14410 final int[] mTransparentLocation = new int[2];
14411
14412 /**
14413 * Global to the view hierarchy used as a temporary for dealing with
14414 * x/y points in the ViewGroup.invalidateChild implementation.
14415 */
14416 final int[] mInvalidateChildLocation = new int[2];
14417
Chet Haasec3aa3612010-06-17 08:50:37 -070014418
14419 /**
14420 * Global to the view hierarchy used as a temporary for dealing with
14421 * x/y location when view is transformed.
14422 */
14423 final float[] mTmpTransformLocation = new float[2];
14424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014425 /**
14426 * The view tree observer used to dispatch global events like
14427 * layout, pre-draw, touch mode change, etc.
14428 */
14429 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14430
14431 /**
14432 * A Canvas used by the view hierarchy to perform bitmap caching.
14433 */
14434 Canvas mCanvas;
14435
14436 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014437 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014438 * handler can be used to pump events in the UI events queue.
14439 */
14440 final Handler mHandler;
14441
14442 /**
14443 * Identifier for messages requesting the view to be invalidated.
14444 * Such messages should be sent to {@link #mHandler}.
14445 */
14446 static final int INVALIDATE_MSG = 0x1;
14447
14448 /**
14449 * Identifier for messages requesting the view to invalidate a region.
14450 * Such messages should be sent to {@link #mHandler}.
14451 */
14452 static final int INVALIDATE_RECT_MSG = 0x2;
14453
14454 /**
14455 * Temporary for use in computing invalidate rectangles while
14456 * calling up the hierarchy.
14457 */
14458 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014459
14460 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014461 * Temporary for use in computing hit areas with transformed views
14462 */
14463 final RectF mTmpTransformRect = new RectF();
14464
14465 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014466 * Temporary list for use in collecting focusable descendents of a view.
14467 */
14468 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014470 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014471 * The id of the window for accessibility purposes.
14472 */
14473 int mAccessibilityWindowId = View.NO_ID;
14474
14475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014476 * Creates a new set of attachment information with the specified
14477 * events handler and thread.
14478 *
14479 * @param handler the events handler the view must use
14480 */
14481 AttachInfo(IWindowSession session, IWindow window,
14482 Handler handler, Callbacks effectPlayer) {
14483 mSession = session;
14484 mWindow = window;
14485 mWindowToken = window.asBinder();
14486 mHandler = handler;
14487 mRootCallbacks = effectPlayer;
14488 }
14489 }
14490
14491 /**
14492 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14493 * is supported. This avoids keeping too many unused fields in most
14494 * instances of View.</p>
14495 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014496 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014497
Mike Cleronf116bf82009-09-27 19:14:12 -070014498 /**
14499 * Scrollbars are not visible
14500 */
14501 public static final int OFF = 0;
14502
14503 /**
14504 * Scrollbars are visible
14505 */
14506 public static final int ON = 1;
14507
14508 /**
14509 * Scrollbars are fading away
14510 */
14511 public static final int FADING = 2;
14512
14513 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014515 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014516 public int scrollBarDefaultDelayBeforeFade;
14517 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014518
14519 public int scrollBarSize;
14520 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014521 public float[] interpolatorValues;
14522 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014523
14524 public final Paint paint;
14525 public final Matrix matrix;
14526 public Shader shader;
14527
Mike Cleronf116bf82009-09-27 19:14:12 -070014528 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14529
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014530 private static final float[] OPAQUE = { 255 };
14531 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014532
Mike Cleronf116bf82009-09-27 19:14:12 -070014533 /**
14534 * When fading should start. This time moves into the future every time
14535 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14536 */
14537 public long fadeStartTime;
14538
14539
14540 /**
14541 * The current state of the scrollbars: ON, OFF, or FADING
14542 */
14543 public int state = OFF;
14544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014545 private int mLastColor;
14546
Mike Cleronf116bf82009-09-27 19:14:12 -070014547 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014548 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14549 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014550 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14551 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552
14553 paint = new Paint();
14554 matrix = new Matrix();
14555 // use use a height of 1, and then wack the matrix each time we
14556 // actually use it.
14557 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014559 paint.setShader(shader);
14560 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014561 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 }
Romain Guy8506ab42009-06-11 17:35:47 -070014563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014564 public void setFadeColor(int color) {
14565 if (color != 0 && color != mLastColor) {
14566 mLastColor = color;
14567 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014568
Romain Guye55e1a72009-08-27 10:42:26 -070014569 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14570 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014572 paint.setShader(shader);
14573 // Restore the default transfer mode (src_over)
14574 paint.setXfermode(null);
14575 }
14576 }
Joe Malin32736f02011-01-19 16:14:20 -080014577
Mike Cleronf116bf82009-09-27 19:14:12 -070014578 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014579 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014580 if (now >= fadeStartTime) {
14581
14582 // the animation fades the scrollbars out by changing
14583 // the opacity (alpha) from fully opaque to fully
14584 // transparent
14585 int nextFrame = (int) now;
14586 int framesCount = 0;
14587
14588 Interpolator interpolator = scrollBarInterpolator;
14589
14590 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014591 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014592
14593 // End transparent
14594 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014595 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014596
14597 state = FADING;
14598
14599 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014600 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014601 }
14602 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014603 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014604
Svetoslav Ganova0156172011-06-26 17:55:44 -070014605 /**
14606 * Resuable callback for sending
14607 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14608 */
14609 private class SendViewScrolledAccessibilityEvent implements Runnable {
14610 public volatile boolean mIsPending;
14611
14612 public void run() {
14613 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14614 mIsPending = false;
14615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014616 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014617
14618 /**
14619 * <p>
14620 * This class represents a delegate that can be registered in a {@link View}
14621 * to enhance accessibility support via composition rather via inheritance.
14622 * It is specifically targeted to widget developers that extend basic View
14623 * classes i.e. classes in package android.view, that would like their
14624 * applications to be backwards compatible.
14625 * </p>
14626 * <p>
14627 * A scenario in which a developer would like to use an accessibility delegate
14628 * is overriding a method introduced in a later API version then the minimal API
14629 * version supported by the application. For example, the method
14630 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14631 * in API version 4 when the accessibility APIs were first introduced. If a
14632 * developer would like his application to run on API version 4 devices (assuming
14633 * all other APIs used by the application are version 4 or lower) and take advantage
14634 * of this method, instead of overriding the method which would break the application's
14635 * backwards compatibility, he can override the corresponding method in this
14636 * delegate and register the delegate in the target View if the API version of
14637 * the system is high enough i.e. the API version is same or higher to the API
14638 * version that introduced
14639 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14640 * </p>
14641 * <p>
14642 * Here is an example implementation:
14643 * </p>
14644 * <code><pre><p>
14645 * if (Build.VERSION.SDK_INT >= 14) {
14646 * // If the API version is equal of higher than the version in
14647 * // which onInitializeAccessibilityNodeInfo was introduced we
14648 * // register a delegate with a customized implementation.
14649 * View view = findViewById(R.id.view_id);
14650 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14651 * public void onInitializeAccessibilityNodeInfo(View host,
14652 * AccessibilityNodeInfo info) {
14653 * // Let the default implementation populate the info.
14654 * super.onInitializeAccessibilityNodeInfo(host, info);
14655 * // Set some other information.
14656 * info.setEnabled(host.isEnabled());
14657 * }
14658 * });
14659 * }
14660 * </code></pre></p>
14661 * <p>
14662 * This delegate contains methods that correspond to the accessibility methods
14663 * in View. If a delegate has been specified the implementation in View hands
14664 * off handling to the corresponding method in this delegate. The default
14665 * implementation the delegate methods behaves exactly as the corresponding
14666 * method in View for the case of no accessibility delegate been set. Hence,
14667 * to customize the behavior of a View method, clients can override only the
14668 * corresponding delegate method without altering the behavior of the rest
14669 * accessibility related methods of the host view.
14670 * </p>
14671 */
14672 public static class AccessibilityDelegate {
14673
14674 /**
14675 * Sends an accessibility event of the given type. If accessibility is not
14676 * enabled this method has no effect.
14677 * <p>
14678 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14679 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14680 * been set.
14681 * </p>
14682 *
14683 * @param host The View hosting the delegate.
14684 * @param eventType The type of the event to send.
14685 *
14686 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14687 */
14688 public void sendAccessibilityEvent(View host, int eventType) {
14689 host.sendAccessibilityEventInternal(eventType);
14690 }
14691
14692 /**
14693 * Sends an accessibility event. This method behaves exactly as
14694 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14695 * empty {@link AccessibilityEvent} and does not perform a check whether
14696 * accessibility is enabled.
14697 * <p>
14698 * The default implementation behaves as
14699 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14700 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14701 * the case of no accessibility delegate been set.
14702 * </p>
14703 *
14704 * @param host The View hosting the delegate.
14705 * @param event The event to send.
14706 *
14707 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14708 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14709 */
14710 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14711 host.sendAccessibilityEventUncheckedInternal(event);
14712 }
14713
14714 /**
14715 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14716 * to its children for adding their text content to the event.
14717 * <p>
14718 * The default implementation behaves as
14719 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14720 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14721 * the case of no accessibility delegate been set.
14722 * </p>
14723 *
14724 * @param host The View hosting the delegate.
14725 * @param event The event.
14726 * @return True if the event population was completed.
14727 *
14728 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14729 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14730 */
14731 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14732 return host.dispatchPopulateAccessibilityEventInternal(event);
14733 }
14734
14735 /**
14736 * Gives a chance to the host View to populate the accessibility event with its
14737 * text content.
14738 * <p>
14739 * The default implementation behaves as
14740 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14741 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14742 * the case of no accessibility delegate been set.
14743 * </p>
14744 *
14745 * @param host The View hosting the delegate.
14746 * @param event The accessibility event which to populate.
14747 *
14748 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14749 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14750 */
14751 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14752 host.onPopulateAccessibilityEventInternal(event);
14753 }
14754
14755 /**
14756 * Initializes an {@link AccessibilityEvent} with information about the
14757 * the host View which is the event source.
14758 * <p>
14759 * The default implementation behaves as
14760 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14761 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14762 * the case of no accessibility delegate been set.
14763 * </p>
14764 *
14765 * @param host The View hosting the delegate.
14766 * @param event The event to initialize.
14767 *
14768 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14769 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14770 */
14771 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14772 host.onInitializeAccessibilityEventInternal(event);
14773 }
14774
14775 /**
14776 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14777 * <p>
14778 * The default implementation behaves as
14779 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14780 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14781 * the case of no accessibility delegate been set.
14782 * </p>
14783 *
14784 * @param host The View hosting the delegate.
14785 * @param info The instance to initialize.
14786 *
14787 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14788 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14789 */
14790 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14791 host.onInitializeAccessibilityNodeInfoInternal(info);
14792 }
14793
14794 /**
14795 * Called when a child of the host View has requested sending an
14796 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14797 * to augment the event.
14798 * <p>
14799 * The default implementation behaves as
14800 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14801 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14802 * the case of no accessibility delegate been set.
14803 * </p>
14804 *
14805 * @param host The View hosting the delegate.
14806 * @param child The child which requests sending the event.
14807 * @param event The event to be sent.
14808 * @return True if the event should be sent
14809 *
14810 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14811 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14812 */
14813 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14814 AccessibilityEvent event) {
14815 return host.onRequestSendAccessibilityEventInternal(child, event);
14816 }
14817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014818}