blob: 73e5026df6f5b1cfc175dddd6c407339b72b4ea8 [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
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001489 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001551 * The stable ID of this view for accessibility porposes.
1552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * The view's tag.
1557 * {@hide}
1558 *
1559 * @see #setTag(Object)
1560 * @see #getTag()
1561 */
1562 protected Object mTag;
1563
1564 // for mPrivateFlags:
1565 /** {@hide} */
1566 static final int WANTS_FOCUS = 0x00000001;
1567 /** {@hide} */
1568 static final int FOCUSED = 0x00000002;
1569 /** {@hide} */
1570 static final int SELECTED = 0x00000004;
1571 /** {@hide} */
1572 static final int IS_ROOT_NAMESPACE = 0x00000008;
1573 /** {@hide} */
1574 static final int HAS_BOUNDS = 0x00000010;
1575 /** {@hide} */
1576 static final int DRAWN = 0x00000020;
1577 /**
1578 * When this flag is set, this view is running an animation on behalf of its
1579 * children and should therefore not cancel invalidate requests, even if they
1580 * lie outside of this view's bounds.
1581 *
1582 * {@hide}
1583 */
1584 static final int DRAW_ANIMATION = 0x00000040;
1585 /** {@hide} */
1586 static final int SKIP_DRAW = 0x00000080;
1587 /** {@hide} */
1588 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1589 /** {@hide} */
1590 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1591 /** {@hide} */
1592 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1593 /** {@hide} */
1594 static final int MEASURED_DIMENSION_SET = 0x00000800;
1595 /** {@hide} */
1596 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001597 /** {@hide} */
1598 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 private static final int PRESSED = 0x00004000;
1601
1602 /** {@hide} */
1603 static final int DRAWING_CACHE_VALID = 0x00008000;
1604 /**
1605 * Flag used to indicate that this view should be drawn once more (and only once
1606 * more) after its animation has completed.
1607 * {@hide}
1608 */
1609 static final int ANIMATION_STARTED = 0x00010000;
1610
1611 private static final int SAVE_STATE_CALLED = 0x00020000;
1612
1613 /**
1614 * Indicates that the View returned true when onSetAlpha() was called and that
1615 * the alpha must be restored.
1616 * {@hide}
1617 */
1618 static final int ALPHA_SET = 0x00040000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER = 0x00080000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1629
1630 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001631 * View flag indicating whether this view was invalidated (fully or partially.)
1632 *
1633 * @hide
1634 */
1635 static final int DIRTY = 0x00200000;
1636
1637 /**
1638 * View flag indicating whether this view was invalidated by an opaque
1639 * invalidate request.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_OPAQUE = 0x00400000;
1644
1645 /**
1646 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1647 *
1648 * @hide
1649 */
1650 static final int DIRTY_MASK = 0x00600000;
1651
1652 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001653 * Indicates whether the background is opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_BACKGROUND = 0x00800000;
1658
1659 /**
1660 * Indicates whether the scrollbars are opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_SCROLLBARS = 0x01000000;
1665
1666 /**
1667 * Indicates whether the view is opaque.
1668 *
1669 * @hide
1670 */
1671 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001672
Adam Powelle14579b2009-12-16 18:39:52 -08001673 /**
1674 * Indicates a prepressed state;
1675 * the short time between ACTION_DOWN and recognizing
1676 * a 'real' press. Prepressed is used to recognize quick taps
1677 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001678 *
Adam Powelle14579b2009-12-16 18:39:52 -08001679 * @hide
1680 */
1681 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powellc9fbaab2010-02-16 17:16:19 -08001683 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001684 * Indicates whether the view is temporarily detached.
1685 *
1686 * @hide
1687 */
1688 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 /**
1691 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001693 * @hide
1694 */
1695 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001696
1697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001698 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1699 * @hide
1700 */
1701 private static final int HOVERED = 0x10000000;
1702
1703 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001704 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1705 * for transform operations
1706 *
1707 * @hide
1708 */
Adam Powellf37df072010-09-17 16:22:49 -07001709 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001710
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001711 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001712 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713
Chet Haasefd2b0022010-08-06 13:08:56 -07001714 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001715 * Indicates that this view was specifically invalidated, not just dirtied because some
1716 * child view was invalidated. The flag is used to determine when we need to recreate
1717 * a view's display list (as opposed to just returning a reference to its existing
1718 * display list).
1719 *
1720 * @hide
1721 */
1722 static final int INVALIDATED = 0x80000000;
1723
Christopher Tate3d4bf172011-03-28 16:16:46 -07001724 /* Masks for mPrivateFlags2 */
1725
1726 /**
1727 * Indicates that this view has reported that it can accept the current drag's content.
1728 * Cleared when the drag operation concludes.
1729 * @hide
1730 */
1731 static final int DRAG_CAN_ACCEPT = 0x00000001;
1732
1733 /**
1734 * Indicates that this view is currently directly under the drag location in a
1735 * drag-and-drop operation involving content that it can accept. Cleared when
1736 * the drag exits the view, or when the drag operation concludes.
1737 * @hide
1738 */
1739 static final int DRAG_HOVERED = 0x00000002;
1740
Cibu Johny86666632010-02-22 13:01:02 -08001741 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 * Indicates whether the view layout direction has been resolved and drawn to the
1743 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001744 *
1745 * @hide
1746 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1748
1749 /**
1750 * Indicates whether the view layout direction has been resolved.
1751 *
1752 * @hide
1753 */
1754 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1755
Cibu Johny86666632010-02-22 13:01:02 -08001756
Christopher Tate3d4bf172011-03-28 16:16:46 -07001757 /* End of masks for mPrivateFlags2 */
1758
1759 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1760
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 /**
Adam Powell637d3372010-08-25 14:37:03 -07001762 * Always allow a user to over-scroll this view, provided it is a
1763 * view that can scroll.
1764 *
1765 * @see #getOverScrollMode()
1766 * @see #setOverScrollMode(int)
1767 */
1768 public static final int OVER_SCROLL_ALWAYS = 0;
1769
1770 /**
1771 * Allow a user to over-scroll this view only if the content is large
1772 * enough to meaningfully scroll, provided it is a view that can scroll.
1773 *
1774 * @see #getOverScrollMode()
1775 * @see #setOverScrollMode(int)
1776 */
1777 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1778
1779 /**
1780 * Never allow a user to over-scroll this view.
1781 *
1782 * @see #getOverScrollMode()
1783 * @see #setOverScrollMode(int)
1784 */
1785 public static final int OVER_SCROLL_NEVER = 2;
1786
1787 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001788 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001789 *
Joe Malin32736f02011-01-19 16:14:20 -08001790 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001791 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001792 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001793
1794 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001795 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1796 *
1797 * This is for use in games, book readers, video players, or any other "immersive" application
1798 * where the usual system chrome is deemed too distracting.
1799 *
1800 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001801 *
Joe Malin32736f02011-01-19 16:14:20 -08001802 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001803 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001804 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1805
1806 /**
1807 * View has requested that the system navigation be temporarily hidden.
1808 *
1809 * This is an even less obtrusive state than that called for by
1810 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1811 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1812 * those to disappear. This is useful (in conjunction with the
1813 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1814 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1815 * window flags) for displaying content using every last pixel on the display.
1816 *
1817 * There is a limitation: because navigation controls are so important, the least user
1818 * interaction will cause them to reappear immediately.
1819 *
1820 * @see #setSystemUiVisibility(int)
1821 */
1822 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1823
1824 /**
1825 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1826 */
1827 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1828
1829 /**
1830 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1831 */
1832 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001833
1834 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001835 * @hide
1836 *
1837 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1838 * out of the public fields to keep the undefined bits out of the developer's way.
1839 *
1840 * Flag to make the status bar not expandable. Unless you also
1841 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1842 */
1843 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1844
1845 /**
1846 * @hide
1847 *
1848 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1849 * out of the public fields to keep the undefined bits out of the developer's way.
1850 *
1851 * Flag to hide notification icons and scrolling ticker text.
1852 */
1853 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1854
1855 /**
1856 * @hide
1857 *
1858 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1859 * out of the public fields to keep the undefined bits out of the developer's way.
1860 *
1861 * Flag to disable incoming notification alerts. This will not block
1862 * icons, but it will block sound, vibrating and other visual or aural notifications.
1863 */
1864 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1865
1866 /**
1867 * @hide
1868 *
1869 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1870 * out of the public fields to keep the undefined bits out of the developer's way.
1871 *
1872 * Flag to hide only the scrolling ticker. Note that
1873 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1874 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1875 */
1876 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1877
1878 /**
1879 * @hide
1880 *
1881 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1882 * out of the public fields to keep the undefined bits out of the developer's way.
1883 *
1884 * Flag to hide the center system info area.
1885 */
1886 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001894 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001895 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1896 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001897 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001898
1899 /**
1900 * @hide
1901 *
1902 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1903 * out of the public fields to keep the undefined bits out of the developer's way.
1904 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001905 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001906 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1907 */
1908 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1909
1910 /**
1911 * @hide
1912 *
1913 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1914 * out of the public fields to keep the undefined bits out of the developer's way.
1915 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001916 * Flag to hide only the clock. You might use this if your activity has
1917 * its own clock making the status bar's clock redundant.
1918 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001919 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1920
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001921 /**
1922 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001923 *
1924 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1925 * out of the public fields to keep the undefined bits out of the developer's way.
1926 *
1927 * Flag to hide only the recent apps button. Don't use this
1928 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1929 */
1930 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1931
1932 /**
1933 * @hide
1934 *
1935 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1936 *
1937 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1938 */
1939 @Deprecated
1940 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1941 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1942
1943 /**
1944 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001945 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001946 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001947
1948 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001949 * These are the system UI flags that can be cleared by events outside
1950 * of an application. Currently this is just the ability to tap on the
1951 * screen while hiding the navigation bar to have it return.
1952 * @hide
1953 */
1954 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1955 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1956
1957 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001958 * Find views that render the specified text.
1959 *
1960 * @see #findViewsWithText(ArrayList, CharSequence, int)
1961 */
1962 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1963
1964 /**
1965 * Find find views that contain the specified content description.
1966 *
1967 * @see #findViewsWithText(ArrayList, CharSequence, int)
1968 */
1969 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1970
1971 /**
Adam Powell637d3372010-08-25 14:37:03 -07001972 * Controls the over-scroll mode for this view.
1973 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1974 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1975 * and {@link #OVER_SCROLL_NEVER}.
1976 */
1977 private int mOverScrollMode;
1978
1979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 * The parent this view is attached to.
1981 * {@hide}
1982 *
1983 * @see #getParent()
1984 */
1985 protected ViewParent mParent;
1986
1987 /**
1988 * {@hide}
1989 */
1990 AttachInfo mAttachInfo;
1991
1992 /**
1993 * {@hide}
1994 */
Romain Guy809a7f62009-05-14 15:44:42 -07001995 @ViewDebug.ExportedProperty(flagMapping = {
1996 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1997 name = "FORCE_LAYOUT"),
1998 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1999 name = "LAYOUT_REQUIRED"),
2000 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002001 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002002 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2003 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2004 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2005 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2006 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002008 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009
2010 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002011 * This view's request for the visibility of the status bar.
2012 * @hide
2013 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002014 @ViewDebug.ExportedProperty(flagMapping = {
2015 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2016 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2017 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2018 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2019 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2020 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2021 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2022 equals = SYSTEM_UI_FLAG_VISIBLE,
2023 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2024 })
Joe Onorato664644d2011-01-23 17:53:23 -08002025 int mSystemUiVisibility;
2026
2027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 * Count of how many windows this view has been attached to.
2029 */
2030 int mWindowAttachCount;
2031
2032 /**
2033 * The layout parameters associated with this view and used by the parent
2034 * {@link android.view.ViewGroup} to determine how this view should be
2035 * laid out.
2036 * {@hide}
2037 */
2038 protected ViewGroup.LayoutParams mLayoutParams;
2039
2040 /**
2041 * The view flags hold various views states.
2042 * {@hide}
2043 */
2044 @ViewDebug.ExportedProperty
2045 int mViewFlags;
2046
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002047 static class TransformationInfo {
2048 /**
2049 * The transform matrix for the View. This transform is calculated internally
2050 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2051 * is used by default. Do *not* use this variable directly; instead call
2052 * getMatrix(), which will automatically recalculate the matrix if necessary
2053 * to get the correct matrix based on the latest rotation and scale properties.
2054 */
2055 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002056
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002057 /**
2058 * The transform matrix for the View. This transform is calculated internally
2059 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2060 * is used by default. Do *not* use this variable directly; instead call
2061 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2062 * to get the correct matrix based on the latest rotation and scale properties.
2063 */
2064 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002065
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002066 /**
2067 * An internal variable that tracks whether we need to recalculate the
2068 * transform matrix, based on whether the rotation or scaleX/Y properties
2069 * have changed since the matrix was last calculated.
2070 */
2071 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002072
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002073 /**
2074 * An internal variable that tracks whether we need to recalculate the
2075 * transform matrix, based on whether the rotation or scaleX/Y properties
2076 * have changed since the matrix was last calculated.
2077 */
2078 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002079
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002080 /**
2081 * A variable that tracks whether we need to recalculate the
2082 * transform matrix, based on whether the rotation or scaleX/Y properties
2083 * have changed since the matrix was last calculated. This variable
2084 * is only valid after a call to updateMatrix() or to a function that
2085 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2086 */
2087 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002088
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002089 /**
2090 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2091 */
2092 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002093
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002094 /**
2095 * This matrix is used when computing the matrix for 3D rotations.
2096 */
2097 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002098
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002099 /**
2100 * These prev values are used to recalculate a centered pivot point when necessary. The
2101 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2102 * set), so thes values are only used then as well.
2103 */
2104 private int mPrevWidth = -1;
2105 private int mPrevHeight = -1;
2106
2107 /**
2108 * The degrees rotation around the vertical axis through the pivot point.
2109 */
2110 @ViewDebug.ExportedProperty
2111 float mRotationY = 0f;
2112
2113 /**
2114 * The degrees rotation around the horizontal axis through the pivot point.
2115 */
2116 @ViewDebug.ExportedProperty
2117 float mRotationX = 0f;
2118
2119 /**
2120 * The degrees rotation around the pivot point.
2121 */
2122 @ViewDebug.ExportedProperty
2123 float mRotation = 0f;
2124
2125 /**
2126 * The amount of translation of the object away from its left property (post-layout).
2127 */
2128 @ViewDebug.ExportedProperty
2129 float mTranslationX = 0f;
2130
2131 /**
2132 * The amount of translation of the object away from its top property (post-layout).
2133 */
2134 @ViewDebug.ExportedProperty
2135 float mTranslationY = 0f;
2136
2137 /**
2138 * The amount of scale in the x direction around the pivot point. A
2139 * value of 1 means no scaling is applied.
2140 */
2141 @ViewDebug.ExportedProperty
2142 float mScaleX = 1f;
2143
2144 /**
2145 * The amount of scale in the y direction around the pivot point. A
2146 * value of 1 means no scaling is applied.
2147 */
2148 @ViewDebug.ExportedProperty
2149 float mScaleY = 1f;
2150
2151 /**
2152 * The amount of scale in the x direction around the pivot point. A
2153 * value of 1 means no scaling is applied.
2154 */
2155 @ViewDebug.ExportedProperty
2156 float mPivotX = 0f;
2157
2158 /**
2159 * The amount of scale in the y direction around the pivot point. A
2160 * value of 1 means no scaling is applied.
2161 */
2162 @ViewDebug.ExportedProperty
2163 float mPivotY = 0f;
2164
2165 /**
2166 * The opacity of the View. This is a value from 0 to 1, where 0 means
2167 * completely transparent and 1 means completely opaque.
2168 */
2169 @ViewDebug.ExportedProperty
2170 float mAlpha = 1f;
2171 }
2172
2173 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002174
Joe Malin32736f02011-01-19 16:14:20 -08002175 private boolean mLastIsOpaque;
2176
Chet Haasefd2b0022010-08-06 13:08:56 -07002177 /**
2178 * Convenience value to check for float values that are close enough to zero to be considered
2179 * zero.
2180 */
Romain Guy2542d192010-08-18 11:47:12 -07002181 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002182
2183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 * The distance in pixels from the left edge of this view's parent
2185 * to the left edge of this view.
2186 * {@hide}
2187 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002188 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 protected int mLeft;
2190 /**
2191 * The distance in pixels from the left edge of this view's parent
2192 * to the right edge of this view.
2193 * {@hide}
2194 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002195 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 protected int mRight;
2197 /**
2198 * The distance in pixels from the top edge of this view's parent
2199 * to the top edge of this view.
2200 * {@hide}
2201 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002202 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 protected int mTop;
2204 /**
2205 * The distance in pixels from the top edge of this view's parent
2206 * to the bottom edge of this view.
2207 * {@hide}
2208 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002209 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 protected int mBottom;
2211
2212 /**
2213 * The offset, in pixels, by which the content of this view is scrolled
2214 * horizontally.
2215 * {@hide}
2216 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002217 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 protected int mScrollX;
2219 /**
2220 * The offset, in pixels, by which the content of this view is scrolled
2221 * vertically.
2222 * {@hide}
2223 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002224 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 protected int mScrollY;
2226
2227 /**
2228 * The left padding in pixels, that is the distance in pixels between the
2229 * left edge of this view and the left edge of its content.
2230 * {@hide}
2231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 protected int mPaddingLeft;
2234 /**
2235 * The right padding in pixels, that is the distance in pixels between the
2236 * right edge of this view and the right edge of its content.
2237 * {@hide}
2238 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002239 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 protected int mPaddingRight;
2241 /**
2242 * The top padding in pixels, that is the distance in pixels between the
2243 * top edge of this view and the top edge of its content.
2244 * {@hide}
2245 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002246 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 protected int mPaddingTop;
2248 /**
2249 * The bottom padding in pixels, that is the distance in pixels between the
2250 * bottom edge of this view and the bottom edge of its content.
2251 * {@hide}
2252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002253 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 protected int mPaddingBottom;
2255
2256 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002257 * Briefly describes the view and is primarily used for accessibility support.
2258 */
2259 private CharSequence mContentDescription;
2260
2261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002263 *
2264 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002266 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002267 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268
2269 /**
2270 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002271 *
2272 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002274 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002275 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002277 /**
Adam Powell20232d02010-12-08 21:08:53 -08002278 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002279 *
2280 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002281 */
2282 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002283 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002284
2285 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002286 * Cache if the user padding is relative.
2287 *
2288 */
2289 @ViewDebug.ExportedProperty(category = "padding")
2290 boolean mUserPaddingRelative;
2291
2292 /**
2293 * Cache the paddingStart set by the user to append to the scrollbar's size.
2294 *
2295 */
2296 @ViewDebug.ExportedProperty(category = "padding")
2297 int mUserPaddingStart;
2298
2299 /**
2300 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2301 *
2302 */
2303 @ViewDebug.ExportedProperty(category = "padding")
2304 int mUserPaddingEnd;
2305
2306 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002307 * @hide
2308 */
2309 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2310 /**
2311 * @hide
2312 */
2313 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 private Drawable mBGDrawable;
2316
2317 private int mBackgroundResource;
2318 private boolean mBackgroundSizeChanged;
2319
2320 /**
2321 * Listener used to dispatch focus change events.
2322 * This field should be made private, so it is hidden from the SDK.
2323 * {@hide}
2324 */
2325 protected OnFocusChangeListener mOnFocusChangeListener;
2326
2327 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002328 * Listeners for layout change events.
2329 */
2330 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2331
2332 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002333 * Listeners for attach events.
2334 */
2335 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2336
2337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 * Listener used to dispatch click events.
2339 * This field should be made private, so it is hidden from the SDK.
2340 * {@hide}
2341 */
2342 protected OnClickListener mOnClickListener;
2343
2344 /**
2345 * Listener used to dispatch long click events.
2346 * This field should be made private, so it is hidden from the SDK.
2347 * {@hide}
2348 */
2349 protected OnLongClickListener mOnLongClickListener;
2350
2351 /**
2352 * Listener used to build the context menu.
2353 * This field should be made private, so it is hidden from the SDK.
2354 * {@hide}
2355 */
2356 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2357
2358 private OnKeyListener mOnKeyListener;
2359
2360 private OnTouchListener mOnTouchListener;
2361
Jeff Brown10b62902011-06-20 16:40:37 -07002362 private OnHoverListener mOnHoverListener;
2363
Jeff Brown33bbfd22011-02-24 20:55:35 -08002364 private OnGenericMotionListener mOnGenericMotionListener;
2365
Chris Tate32affef2010-10-18 15:29:21 -07002366 private OnDragListener mOnDragListener;
2367
Joe Onorato664644d2011-01-23 17:53:23 -08002368 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 /**
2371 * The application environment this view lives in.
2372 * This field should be made private, so it is hidden from the SDK.
2373 * {@hide}
2374 */
2375 protected Context mContext;
2376
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002377 private final Resources mResources;
2378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 private ScrollabilityCache mScrollCache;
2380
2381 private int[] mDrawableState = null;
2382
Romain Guy0211a0a2011-02-14 16:34:59 -08002383 /**
2384 * Set to true when drawing cache is enabled and cannot be created.
2385 *
2386 * @hide
2387 */
2388 public boolean mCachingFailed;
2389
Romain Guy02890fd2010-08-06 17:58:44 -07002390 private Bitmap mDrawingCache;
2391 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002392 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002393 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394
2395 /**
2396 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2397 * the user may specify which view to go to next.
2398 */
2399 private int mNextFocusLeftId = View.NO_ID;
2400
2401 /**
2402 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2403 * the user may specify which view to go to next.
2404 */
2405 private int mNextFocusRightId = View.NO_ID;
2406
2407 /**
2408 * When this view has focus and the next focus is {@link #FOCUS_UP},
2409 * the user may specify which view to go to next.
2410 */
2411 private int mNextFocusUpId = View.NO_ID;
2412
2413 /**
2414 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2415 * the user may specify which view to go to next.
2416 */
2417 private int mNextFocusDownId = View.NO_ID;
2418
Jeff Brown4e6319b2010-12-13 10:36:51 -08002419 /**
2420 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2421 * the user may specify which view to go to next.
2422 */
2423 int mNextFocusForwardId = View.NO_ID;
2424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002426 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002427 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002428 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 private UnsetPressedState mUnsetPressedState;
2431
2432 /**
2433 * Whether the long press's action has been invoked. The tap's action is invoked on the
2434 * up event while a long press is invoked as soon as the long press duration is reached, so
2435 * a long press could be performed before the tap is checked, in which case the tap's action
2436 * should not be invoked.
2437 */
2438 private boolean mHasPerformedLongPress;
2439
2440 /**
2441 * The minimum height of the view. We'll try our best to have the height
2442 * of this view to at least this amount.
2443 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002444 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 private int mMinHeight;
2446
2447 /**
2448 * The minimum width of the view. We'll try our best to have the width
2449 * of this view to at least this amount.
2450 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002451 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 private int mMinWidth;
2453
2454 /**
2455 * The delegate to handle touch events that are physically in this view
2456 * but should be handled by another view.
2457 */
2458 private TouchDelegate mTouchDelegate = null;
2459
2460 /**
2461 * Solid color to use as a background when creating the drawing cache. Enables
2462 * the cache to use 16 bit bitmaps instead of 32 bit.
2463 */
2464 private int mDrawingCacheBackgroundColor = 0;
2465
2466 /**
2467 * Special tree observer used when mAttachInfo is null.
2468 */
2469 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002470
Adam Powelle14579b2009-12-16 18:39:52 -08002471 /**
2472 * Cache the touch slop from the context that created the view.
2473 */
2474 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002477 * Object that handles automatic animation of view properties.
2478 */
2479 private ViewPropertyAnimator mAnimator = null;
2480
2481 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002482 * Flag indicating that a drag can cross window boundaries. When
2483 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2484 * with this flag set, all visible applications will be able to participate
2485 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002486 *
2487 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002488 */
2489 public static final int DRAG_FLAG_GLOBAL = 1;
2490
2491 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002492 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2493 */
2494 private float mVerticalScrollFactor;
2495
2496 /**
Adam Powell20232d02010-12-08 21:08:53 -08002497 * Position of the vertical scroll bar.
2498 */
2499 private int mVerticalScrollbarPosition;
2500
2501 /**
2502 * Position the scroll bar at the default position as determined by the system.
2503 */
2504 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2505
2506 /**
2507 * Position the scroll bar along the left edge.
2508 */
2509 public static final int SCROLLBAR_POSITION_LEFT = 1;
2510
2511 /**
2512 * Position the scroll bar along the right edge.
2513 */
2514 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2515
2516 /**
Romain Guy171c5922011-01-06 10:04:23 -08002517 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002518 *
2519 * @see #getLayerType()
2520 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002521 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002522 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002523 */
2524 public static final int LAYER_TYPE_NONE = 0;
2525
2526 /**
2527 * <p>Indicates that the view has a software layer. A software layer is backed
2528 * by a bitmap and causes the view to be rendered using Android's software
2529 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002530 *
Romain Guy171c5922011-01-06 10:04:23 -08002531 * <p>Software layers have various usages:</p>
2532 * <p>When the application is not using hardware acceleration, a software layer
2533 * is useful to apply a specific color filter and/or blending mode and/or
2534 * translucency to a view and all its children.</p>
2535 * <p>When the application is using hardware acceleration, a software layer
2536 * is useful to render drawing primitives not supported by the hardware
2537 * accelerated pipeline. It can also be used to cache a complex view tree
2538 * into a texture and reduce the complexity of drawing operations. For instance,
2539 * when animating a complex view tree with a translation, a software layer can
2540 * be used to render the view tree only once.</p>
2541 * <p>Software layers should be avoided when the affected view tree updates
2542 * often. Every update will require to re-render the software layer, which can
2543 * potentially be slow (particularly when hardware acceleration is turned on
2544 * since the layer will have to be uploaded into a hardware texture after every
2545 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002546 *
2547 * @see #getLayerType()
2548 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002549 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002550 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002551 */
2552 public static final int LAYER_TYPE_SOFTWARE = 1;
2553
2554 /**
2555 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2556 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2557 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2558 * rendering pipeline, but only if hardware acceleration is turned on for the
2559 * view hierarchy. When hardware acceleration is turned off, hardware layers
2560 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002561 *
Romain Guy171c5922011-01-06 10:04:23 -08002562 * <p>A hardware layer is useful to apply a specific color filter and/or
2563 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002564 * <p>A hardware layer can be used to cache a complex view tree into a
2565 * texture and reduce the complexity of drawing operations. For instance,
2566 * when animating a complex view tree with a translation, a hardware layer can
2567 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002568 * <p>A hardware layer can also be used to increase the rendering quality when
2569 * rotation transformations are applied on a view. It can also be used to
2570 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002571 *
2572 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002573 * @see #setLayerType(int, android.graphics.Paint)
2574 * @see #LAYER_TYPE_NONE
2575 * @see #LAYER_TYPE_SOFTWARE
2576 */
2577 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002578
Romain Guy3aaff3a2011-01-12 14:18:47 -08002579 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2580 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2581 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2582 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2583 })
Romain Guy171c5922011-01-06 10:04:23 -08002584 int mLayerType = LAYER_TYPE_NONE;
2585 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002586 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002587
2588 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002589 * Set to true when the view is sending hover accessibility events because it
2590 * is the innermost hovered view.
2591 */
2592 private boolean mSendingHoverAccessibilityEvents;
2593
2594 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002595 * Delegate for injecting accessiblity functionality.
2596 */
2597 AccessibilityDelegate mAccessibilityDelegate;
2598
2599 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002600 * Text direction is inherited thru {@link ViewGroup}
2601 * @hide
2602 */
2603 public static final int TEXT_DIRECTION_INHERIT = 0;
2604
2605 /**
2606 * Text direction is using "first strong algorithm". The first strong directional character
2607 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002608 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002609 *
2610 * @hide
2611 */
2612 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2613
2614 /**
2615 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2616 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002617 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002618 *
2619 * @hide
2620 */
2621 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2622
2623 /**
2624 * Text direction is forced to LTR.
2625 *
2626 * @hide
2627 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002628 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002629
2630 /**
2631 * Text direction is forced to RTL.
2632 *
2633 * @hide
2634 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002635 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002636
2637 /**
2638 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002639 *
2640 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002641 */
2642 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2643
2644 /**
2645 * The text direction that has been defined by {@link #setTextDirection(int)}.
2646 *
2647 * {@hide}
2648 */
2649 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2651 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2652 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2653 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2654 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2655 })
Doug Feltcb3791202011-07-07 11:57:48 -07002656 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002657
2658 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002659 * The resolved text direction. This needs resolution if the value is
2660 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2661 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2662 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002663 *
2664 * {@hide}
2665 */
2666 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002667 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2668 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2669 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002670 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2671 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2672 })
Doug Feltcb3791202011-07-07 11:57:48 -07002673 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002674
2675 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002676 * Consistency verifier for debugging purposes.
2677 * @hide
2678 */
2679 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2680 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2681 new InputEventConsistencyVerifier(this, 0) : null;
2682
2683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 * Simple constructor to use when creating a view from code.
2685 *
2686 * @param context The Context the view is running in, through which it can
2687 * access the current theme, resources, etc.
2688 */
2689 public View(Context context) {
2690 mContext = context;
2691 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002692 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002693 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002694 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002695 mUserPaddingStart = -1;
2696 mUserPaddingEnd = -1;
2697 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 }
2699
2700 /**
2701 * Constructor that is called when inflating a view from XML. This is called
2702 * when a view is being constructed from an XML file, supplying attributes
2703 * that were specified in the XML file. This version uses a default style of
2704 * 0, so the only attribute values applied are those in the Context's Theme
2705 * and the given AttributeSet.
2706 *
2707 * <p>
2708 * The method onFinishInflate() will be called after all children have been
2709 * added.
2710 *
2711 * @param context The Context the view is running in, through which it can
2712 * access the current theme, resources, etc.
2713 * @param attrs The attributes of the XML tag that is inflating the view.
2714 * @see #View(Context, AttributeSet, int)
2715 */
2716 public View(Context context, AttributeSet attrs) {
2717 this(context, attrs, 0);
2718 }
2719
2720 /**
2721 * Perform inflation from XML and apply a class-specific base style. This
2722 * constructor of View allows subclasses to use their own base style when
2723 * they are inflating. For example, a Button class's constructor would call
2724 * this version of the super class constructor and supply
2725 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2726 * the theme's button style to modify all of the base view attributes (in
2727 * particular its background) as well as the Button class's attributes.
2728 *
2729 * @param context The Context the view is running in, through which it can
2730 * access the current theme, resources, etc.
2731 * @param attrs The attributes of the XML tag that is inflating the view.
2732 * @param defStyle The default style to apply to this view. If 0, no style
2733 * will be applied (beyond what is included in the theme). This may
2734 * either be an attribute resource, whose value will be retrieved
2735 * from the current theme, or an explicit style resource.
2736 * @see #View(Context, AttributeSet)
2737 */
2738 public View(Context context, AttributeSet attrs, int defStyle) {
2739 this(context);
2740
2741 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2742 defStyle, 0);
2743
2744 Drawable background = null;
2745
2746 int leftPadding = -1;
2747 int topPadding = -1;
2748 int rightPadding = -1;
2749 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002750 int startPadding = -1;
2751 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752
2753 int padding = -1;
2754
2755 int viewFlagValues = 0;
2756 int viewFlagMasks = 0;
2757
2758 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 int x = 0;
2761 int y = 0;
2762
Chet Haase73066682010-11-29 15:55:32 -08002763 float tx = 0;
2764 float ty = 0;
2765 float rotation = 0;
2766 float rotationX = 0;
2767 float rotationY = 0;
2768 float sx = 1f;
2769 float sy = 1f;
2770 boolean transformSet = false;
2771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2773
Adam Powell637d3372010-08-25 14:37:03 -07002774 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 final int N = a.getIndexCount();
2776 for (int i = 0; i < N; i++) {
2777 int attr = a.getIndex(i);
2778 switch (attr) {
2779 case com.android.internal.R.styleable.View_background:
2780 background = a.getDrawable(attr);
2781 break;
2782 case com.android.internal.R.styleable.View_padding:
2783 padding = a.getDimensionPixelSize(attr, -1);
2784 break;
2785 case com.android.internal.R.styleable.View_paddingLeft:
2786 leftPadding = a.getDimensionPixelSize(attr, -1);
2787 break;
2788 case com.android.internal.R.styleable.View_paddingTop:
2789 topPadding = a.getDimensionPixelSize(attr, -1);
2790 break;
2791 case com.android.internal.R.styleable.View_paddingRight:
2792 rightPadding = a.getDimensionPixelSize(attr, -1);
2793 break;
2794 case com.android.internal.R.styleable.View_paddingBottom:
2795 bottomPadding = a.getDimensionPixelSize(attr, -1);
2796 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002797 case com.android.internal.R.styleable.View_paddingStart:
2798 startPadding = a.getDimensionPixelSize(attr, -1);
2799 break;
2800 case com.android.internal.R.styleable.View_paddingEnd:
2801 endPadding = a.getDimensionPixelSize(attr, -1);
2802 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 case com.android.internal.R.styleable.View_scrollX:
2804 x = a.getDimensionPixelOffset(attr, 0);
2805 break;
2806 case com.android.internal.R.styleable.View_scrollY:
2807 y = a.getDimensionPixelOffset(attr, 0);
2808 break;
Chet Haase73066682010-11-29 15:55:32 -08002809 case com.android.internal.R.styleable.View_alpha:
2810 setAlpha(a.getFloat(attr, 1f));
2811 break;
2812 case com.android.internal.R.styleable.View_transformPivotX:
2813 setPivotX(a.getDimensionPixelOffset(attr, 0));
2814 break;
2815 case com.android.internal.R.styleable.View_transformPivotY:
2816 setPivotY(a.getDimensionPixelOffset(attr, 0));
2817 break;
2818 case com.android.internal.R.styleable.View_translationX:
2819 tx = a.getDimensionPixelOffset(attr, 0);
2820 transformSet = true;
2821 break;
2822 case com.android.internal.R.styleable.View_translationY:
2823 ty = a.getDimensionPixelOffset(attr, 0);
2824 transformSet = true;
2825 break;
2826 case com.android.internal.R.styleable.View_rotation:
2827 rotation = a.getFloat(attr, 0);
2828 transformSet = true;
2829 break;
2830 case com.android.internal.R.styleable.View_rotationX:
2831 rotationX = a.getFloat(attr, 0);
2832 transformSet = true;
2833 break;
2834 case com.android.internal.R.styleable.View_rotationY:
2835 rotationY = a.getFloat(attr, 0);
2836 transformSet = true;
2837 break;
2838 case com.android.internal.R.styleable.View_scaleX:
2839 sx = a.getFloat(attr, 1f);
2840 transformSet = true;
2841 break;
2842 case com.android.internal.R.styleable.View_scaleY:
2843 sy = a.getFloat(attr, 1f);
2844 transformSet = true;
2845 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 case com.android.internal.R.styleable.View_id:
2847 mID = a.getResourceId(attr, NO_ID);
2848 break;
2849 case com.android.internal.R.styleable.View_tag:
2850 mTag = a.getText(attr);
2851 break;
2852 case com.android.internal.R.styleable.View_fitsSystemWindows:
2853 if (a.getBoolean(attr, false)) {
2854 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2855 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2856 }
2857 break;
2858 case com.android.internal.R.styleable.View_focusable:
2859 if (a.getBoolean(attr, false)) {
2860 viewFlagValues |= FOCUSABLE;
2861 viewFlagMasks |= FOCUSABLE_MASK;
2862 }
2863 break;
2864 case com.android.internal.R.styleable.View_focusableInTouchMode:
2865 if (a.getBoolean(attr, false)) {
2866 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2867 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2868 }
2869 break;
2870 case com.android.internal.R.styleable.View_clickable:
2871 if (a.getBoolean(attr, false)) {
2872 viewFlagValues |= CLICKABLE;
2873 viewFlagMasks |= CLICKABLE;
2874 }
2875 break;
2876 case com.android.internal.R.styleable.View_longClickable:
2877 if (a.getBoolean(attr, false)) {
2878 viewFlagValues |= LONG_CLICKABLE;
2879 viewFlagMasks |= LONG_CLICKABLE;
2880 }
2881 break;
2882 case com.android.internal.R.styleable.View_saveEnabled:
2883 if (!a.getBoolean(attr, true)) {
2884 viewFlagValues |= SAVE_DISABLED;
2885 viewFlagMasks |= SAVE_DISABLED_MASK;
2886 }
2887 break;
2888 case com.android.internal.R.styleable.View_duplicateParentState:
2889 if (a.getBoolean(attr, false)) {
2890 viewFlagValues |= DUPLICATE_PARENT_STATE;
2891 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2892 }
2893 break;
2894 case com.android.internal.R.styleable.View_visibility:
2895 final int visibility = a.getInt(attr, 0);
2896 if (visibility != 0) {
2897 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2898 viewFlagMasks |= VISIBILITY_MASK;
2899 }
2900 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002901 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002902 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002903 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002904 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002905 final int layoutDirection = a.getInt(attr, -1);
2906 if (layoutDirection != -1) {
2907 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002908 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002909 // Set to default (LAYOUT_DIRECTION_INHERIT)
2910 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002911 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002912 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002913 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 case com.android.internal.R.styleable.View_drawingCacheQuality:
2915 final int cacheQuality = a.getInt(attr, 0);
2916 if (cacheQuality != 0) {
2917 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2918 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2919 }
2920 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002921 case com.android.internal.R.styleable.View_contentDescription:
2922 mContentDescription = a.getString(attr);
2923 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2925 if (!a.getBoolean(attr, true)) {
2926 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2927 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2928 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002929 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2931 if (!a.getBoolean(attr, true)) {
2932 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2933 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2934 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002935 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 case R.styleable.View_scrollbars:
2937 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2938 if (scrollbars != SCROLLBARS_NONE) {
2939 viewFlagValues |= scrollbars;
2940 viewFlagMasks |= SCROLLBARS_MASK;
2941 initializeScrollbars(a);
2942 }
2943 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002944 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002946 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2947 // Ignore the attribute starting with ICS
2948 break;
2949 }
2950 // With builds < ICS, fall through and apply fading edges
2951 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2953 if (fadingEdge != FADING_EDGE_NONE) {
2954 viewFlagValues |= fadingEdge;
2955 viewFlagMasks |= FADING_EDGE_MASK;
2956 initializeFadingEdge(a);
2957 }
2958 break;
2959 case R.styleable.View_scrollbarStyle:
2960 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2961 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2962 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2963 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2964 }
2965 break;
2966 case R.styleable.View_isScrollContainer:
2967 setScrollContainer = true;
2968 if (a.getBoolean(attr, false)) {
2969 setScrollContainer(true);
2970 }
2971 break;
2972 case com.android.internal.R.styleable.View_keepScreenOn:
2973 if (a.getBoolean(attr, false)) {
2974 viewFlagValues |= KEEP_SCREEN_ON;
2975 viewFlagMasks |= KEEP_SCREEN_ON;
2976 }
2977 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002978 case R.styleable.View_filterTouchesWhenObscured:
2979 if (a.getBoolean(attr, false)) {
2980 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2981 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2982 }
2983 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 case R.styleable.View_nextFocusLeft:
2985 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2986 break;
2987 case R.styleable.View_nextFocusRight:
2988 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2989 break;
2990 case R.styleable.View_nextFocusUp:
2991 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2992 break;
2993 case R.styleable.View_nextFocusDown:
2994 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2995 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002996 case R.styleable.View_nextFocusForward:
2997 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2998 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 case R.styleable.View_minWidth:
3000 mMinWidth = a.getDimensionPixelSize(attr, 0);
3001 break;
3002 case R.styleable.View_minHeight:
3003 mMinHeight = a.getDimensionPixelSize(attr, 0);
3004 break;
Romain Guy9a817362009-05-01 10:57:14 -07003005 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003006 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003007 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003008 + "be used within a restricted context");
3009 }
3010
Romain Guy9a817362009-05-01 10:57:14 -07003011 final String handlerName = a.getString(attr);
3012 if (handlerName != null) {
3013 setOnClickListener(new OnClickListener() {
3014 private Method mHandler;
3015
3016 public void onClick(View v) {
3017 if (mHandler == null) {
3018 try {
3019 mHandler = getContext().getClass().getMethod(handlerName,
3020 View.class);
3021 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003022 int id = getId();
3023 String idText = id == NO_ID ? "" : " with id '"
3024 + getContext().getResources().getResourceEntryName(
3025 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003026 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003027 handlerName + "(View) in the activity "
3028 + getContext().getClass() + " for onClick handler"
3029 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003030 }
3031 }
3032
3033 try {
3034 mHandler.invoke(getContext(), View.this);
3035 } catch (IllegalAccessException e) {
3036 throw new IllegalStateException("Could not execute non "
3037 + "public method of the activity", e);
3038 } catch (InvocationTargetException e) {
3039 throw new IllegalStateException("Could not execute "
3040 + "method of the activity", e);
3041 }
3042 }
3043 });
3044 }
3045 break;
Adam Powell637d3372010-08-25 14:37:03 -07003046 case R.styleable.View_overScrollMode:
3047 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3048 break;
Adam Powell20232d02010-12-08 21:08:53 -08003049 case R.styleable.View_verticalScrollbarPosition:
3050 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3051 break;
Romain Guy171c5922011-01-06 10:04:23 -08003052 case R.styleable.View_layerType:
3053 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3054 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003055 case R.styleable.View_textDirection:
3056 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3057 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
3060
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003061 a.recycle();
3062
Adam Powell637d3372010-08-25 14:37:03 -07003063 setOverScrollMode(overScrollMode);
3064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 if (background != null) {
3066 setBackgroundDrawable(background);
3067 }
3068
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003069 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3070
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003071 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3072 // layout direction). Those cached values will be used later during padding resolution.
3073 mUserPaddingStart = startPadding;
3074 mUserPaddingEnd = endPadding;
3075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 if (padding >= 0) {
3077 leftPadding = padding;
3078 topPadding = padding;
3079 rightPadding = padding;
3080 bottomPadding = padding;
3081 }
3082
3083 // If the user specified the padding (either with android:padding or
3084 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3085 // use the default padding or the padding from the background drawable
3086 // (stored at this point in mPadding*)
3087 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3088 topPadding >= 0 ? topPadding : mPaddingTop,
3089 rightPadding >= 0 ? rightPadding : mPaddingRight,
3090 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3091
3092 if (viewFlagMasks != 0) {
3093 setFlags(viewFlagValues, viewFlagMasks);
3094 }
3095
3096 // Needs to be called after mViewFlags is set
3097 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3098 recomputePadding();
3099 }
3100
3101 if (x != 0 || y != 0) {
3102 scrollTo(x, y);
3103 }
3104
Chet Haase73066682010-11-29 15:55:32 -08003105 if (transformSet) {
3106 setTranslationX(tx);
3107 setTranslationY(ty);
3108 setRotation(rotation);
3109 setRotationX(rotationX);
3110 setRotationY(rotationY);
3111 setScaleX(sx);
3112 setScaleY(sy);
3113 }
3114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3116 setScrollContainer(true);
3117 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003118
3119 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
3121
3122 /**
3123 * Non-public constructor for use in testing
3124 */
3125 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003126 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 }
3128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 /**
3130 * <p>
3131 * Initializes the fading edges from a given set of styled attributes. This
3132 * method should be called by subclasses that need fading edges and when an
3133 * instance of these subclasses is created programmatically rather than
3134 * being inflated from XML. This method is automatically called when the XML
3135 * is inflated.
3136 * </p>
3137 *
3138 * @param a the styled attributes set to initialize the fading edges from
3139 */
3140 protected void initializeFadingEdge(TypedArray a) {
3141 initScrollCache();
3142
3143 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3144 R.styleable.View_fadingEdgeLength,
3145 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3146 }
3147
3148 /**
3149 * Returns the size of the vertical faded edges used to indicate that more
3150 * content in this view is visible.
3151 *
3152 * @return The size in pixels of the vertical faded edge or 0 if vertical
3153 * faded edges are not enabled for this view.
3154 * @attr ref android.R.styleable#View_fadingEdgeLength
3155 */
3156 public int getVerticalFadingEdgeLength() {
3157 if (isVerticalFadingEdgeEnabled()) {
3158 ScrollabilityCache cache = mScrollCache;
3159 if (cache != null) {
3160 return cache.fadingEdgeLength;
3161 }
3162 }
3163 return 0;
3164 }
3165
3166 /**
3167 * Set the size of the faded edge used to indicate that more content in this
3168 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003169 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3170 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3171 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 *
3173 * @param length The size in pixels of the faded edge used to indicate that more
3174 * content in this view is visible.
3175 */
3176 public void setFadingEdgeLength(int length) {
3177 initScrollCache();
3178 mScrollCache.fadingEdgeLength = length;
3179 }
3180
3181 /**
3182 * Returns the size of the horizontal faded edges used to indicate that more
3183 * content in this view is visible.
3184 *
3185 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3186 * faded edges are not enabled for this view.
3187 * @attr ref android.R.styleable#View_fadingEdgeLength
3188 */
3189 public int getHorizontalFadingEdgeLength() {
3190 if (isHorizontalFadingEdgeEnabled()) {
3191 ScrollabilityCache cache = mScrollCache;
3192 if (cache != null) {
3193 return cache.fadingEdgeLength;
3194 }
3195 }
3196 return 0;
3197 }
3198
3199 /**
3200 * Returns the width of the vertical scrollbar.
3201 *
3202 * @return The width in pixels of the vertical scrollbar or 0 if there
3203 * is no vertical scrollbar.
3204 */
3205 public int getVerticalScrollbarWidth() {
3206 ScrollabilityCache cache = mScrollCache;
3207 if (cache != null) {
3208 ScrollBarDrawable scrollBar = cache.scrollBar;
3209 if (scrollBar != null) {
3210 int size = scrollBar.getSize(true);
3211 if (size <= 0) {
3212 size = cache.scrollBarSize;
3213 }
3214 return size;
3215 }
3216 return 0;
3217 }
3218 return 0;
3219 }
3220
3221 /**
3222 * Returns the height of the horizontal scrollbar.
3223 *
3224 * @return The height in pixels of the horizontal scrollbar or 0 if
3225 * there is no horizontal scrollbar.
3226 */
3227 protected int getHorizontalScrollbarHeight() {
3228 ScrollabilityCache cache = mScrollCache;
3229 if (cache != null) {
3230 ScrollBarDrawable scrollBar = cache.scrollBar;
3231 if (scrollBar != null) {
3232 int size = scrollBar.getSize(false);
3233 if (size <= 0) {
3234 size = cache.scrollBarSize;
3235 }
3236 return size;
3237 }
3238 return 0;
3239 }
3240 return 0;
3241 }
3242
3243 /**
3244 * <p>
3245 * Initializes the scrollbars from a given set of styled attributes. This
3246 * method should be called by subclasses that need scrollbars and when an
3247 * instance of these subclasses is created programmatically rather than
3248 * being inflated from XML. This method is automatically called when the XML
3249 * is inflated.
3250 * </p>
3251 *
3252 * @param a the styled attributes set to initialize the scrollbars from
3253 */
3254 protected void initializeScrollbars(TypedArray a) {
3255 initScrollCache();
3256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003258
Mike Cleronf116bf82009-09-27 19:14:12 -07003259 if (scrollabilityCache.scrollBar == null) {
3260 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3261 }
Joe Malin32736f02011-01-19 16:14:20 -08003262
Romain Guy8bda2482010-03-02 11:42:11 -08003263 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264
Mike Cleronf116bf82009-09-27 19:14:12 -07003265 if (!fadeScrollbars) {
3266 scrollabilityCache.state = ScrollabilityCache.ON;
3267 }
3268 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003269
3270
Mike Cleronf116bf82009-09-27 19:14:12 -07003271 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3272 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3273 .getScrollBarFadeDuration());
3274 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3275 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3276 ViewConfiguration.getScrollDefaultDelay());
3277
Joe Malin32736f02011-01-19 16:14:20 -08003278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3280 com.android.internal.R.styleable.View_scrollbarSize,
3281 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3282
3283 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3284 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3285
3286 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3287 if (thumb != null) {
3288 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3289 }
3290
3291 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3292 false);
3293 if (alwaysDraw) {
3294 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3295 }
3296
3297 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3298 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3299
3300 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3301 if (thumb != null) {
3302 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3303 }
3304
3305 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3306 false);
3307 if (alwaysDraw) {
3308 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3309 }
3310
3311 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003312 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
3314
3315 /**
3316 * <p>
3317 * Initalizes the scrollability cache if necessary.
3318 * </p>
3319 */
3320 private void initScrollCache() {
3321 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003322 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 }
3324 }
3325
3326 /**
Adam Powell20232d02010-12-08 21:08:53 -08003327 * Set the position of the vertical scroll bar. Should be one of
3328 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3329 * {@link #SCROLLBAR_POSITION_RIGHT}.
3330 *
3331 * @param position Where the vertical scroll bar should be positioned.
3332 */
3333 public void setVerticalScrollbarPosition(int position) {
3334 if (mVerticalScrollbarPosition != position) {
3335 mVerticalScrollbarPosition = position;
3336 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003337 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003338 }
3339 }
3340
3341 /**
3342 * @return The position where the vertical scroll bar will show, if applicable.
3343 * @see #setVerticalScrollbarPosition(int)
3344 */
3345 public int getVerticalScrollbarPosition() {
3346 return mVerticalScrollbarPosition;
3347 }
3348
3349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 * Register a callback to be invoked when focus of this view changed.
3351 *
3352 * @param l The callback that will run.
3353 */
3354 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3355 mOnFocusChangeListener = l;
3356 }
3357
3358 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003359 * Add a listener that will be called when the bounds of the view change due to
3360 * layout processing.
3361 *
3362 * @param listener The listener that will be called when layout bounds change.
3363 */
3364 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3365 if (mOnLayoutChangeListeners == null) {
3366 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3367 }
Chet Haase1a76dcd2011-10-06 11:16:40 -07003368 if (!mOnLayoutChangeListeners.contains(listener)) {
3369 mOnLayoutChangeListeners.add(listener);
3370 }
Chet Haase21cd1382010-09-01 17:42:29 -07003371 }
3372
3373 /**
3374 * Remove a listener for layout changes.
3375 *
3376 * @param listener The listener for layout bounds change.
3377 */
3378 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3379 if (mOnLayoutChangeListeners == null) {
3380 return;
3381 }
3382 mOnLayoutChangeListeners.remove(listener);
3383 }
3384
3385 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003386 * Add a listener for attach state changes.
3387 *
3388 * This listener will be called whenever this view is attached or detached
3389 * from a window. Remove the listener using
3390 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3391 *
3392 * @param listener Listener to attach
3393 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3394 */
3395 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3396 if (mOnAttachStateChangeListeners == null) {
3397 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3398 }
3399 mOnAttachStateChangeListeners.add(listener);
3400 }
3401
3402 /**
3403 * Remove a listener for attach state changes. The listener will receive no further
3404 * notification of window attach/detach events.
3405 *
3406 * @param listener Listener to remove
3407 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3408 */
3409 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3410 if (mOnAttachStateChangeListeners == null) {
3411 return;
3412 }
3413 mOnAttachStateChangeListeners.remove(listener);
3414 }
3415
3416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 * Returns the focus-change callback registered for this view.
3418 *
3419 * @return The callback, or null if one is not registered.
3420 */
3421 public OnFocusChangeListener getOnFocusChangeListener() {
3422 return mOnFocusChangeListener;
3423 }
3424
3425 /**
3426 * Register a callback to be invoked when this view is clicked. If this view is not
3427 * clickable, it becomes clickable.
3428 *
3429 * @param l The callback that will run
3430 *
3431 * @see #setClickable(boolean)
3432 */
3433 public void setOnClickListener(OnClickListener l) {
3434 if (!isClickable()) {
3435 setClickable(true);
3436 }
3437 mOnClickListener = l;
3438 }
3439
3440 /**
3441 * Register a callback to be invoked when this view is clicked and held. If this view is not
3442 * long clickable, it becomes long clickable.
3443 *
3444 * @param l The callback that will run
3445 *
3446 * @see #setLongClickable(boolean)
3447 */
3448 public void setOnLongClickListener(OnLongClickListener l) {
3449 if (!isLongClickable()) {
3450 setLongClickable(true);
3451 }
3452 mOnLongClickListener = l;
3453 }
3454
3455 /**
3456 * Register a callback to be invoked when the context menu for this view is
3457 * being built. If this view is not long clickable, it becomes long clickable.
3458 *
3459 * @param l The callback that will run
3460 *
3461 */
3462 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3463 if (!isLongClickable()) {
3464 setLongClickable(true);
3465 }
3466 mOnCreateContextMenuListener = l;
3467 }
3468
3469 /**
3470 * Call this view's OnClickListener, if it is defined.
3471 *
3472 * @return True there was an assigned OnClickListener that was called, false
3473 * otherwise is returned.
3474 */
3475 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003476 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 if (mOnClickListener != null) {
3479 playSoundEffect(SoundEffectConstants.CLICK);
3480 mOnClickListener.onClick(this);
3481 return true;
3482 }
3483
3484 return false;
3485 }
3486
3487 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003488 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3489 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003491 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 */
3493 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003494 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 boolean handled = false;
3497 if (mOnLongClickListener != null) {
3498 handled = mOnLongClickListener.onLongClick(View.this);
3499 }
3500 if (!handled) {
3501 handled = showContextMenu();
3502 }
3503 if (handled) {
3504 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3505 }
3506 return handled;
3507 }
3508
3509 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003510 * Performs button-related actions during a touch down event.
3511 *
3512 * @param event The event.
3513 * @return True if the down was consumed.
3514 *
3515 * @hide
3516 */
3517 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3518 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3519 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3520 return true;
3521 }
3522 }
3523 return false;
3524 }
3525
3526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 * Bring up the context menu for this view.
3528 *
3529 * @return Whether a context menu was displayed.
3530 */
3531 public boolean showContextMenu() {
3532 return getParent().showContextMenuForChild(this);
3533 }
3534
3535 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003536 * Bring up the context menu for this view, referring to the item under the specified point.
3537 *
3538 * @param x The referenced x coordinate.
3539 * @param y The referenced y coordinate.
3540 * @param metaState The keyboard modifiers that were pressed.
3541 * @return Whether a context menu was displayed.
3542 *
3543 * @hide
3544 */
3545 public boolean showContextMenu(float x, float y, int metaState) {
3546 return showContextMenu();
3547 }
3548
3549 /**
Adam Powell6e346362010-07-23 10:18:23 -07003550 * Start an action mode.
3551 *
3552 * @param callback Callback that will control the lifecycle of the action mode
3553 * @return The new action mode if it is started, null otherwise
3554 *
3555 * @see ActionMode
3556 */
3557 public ActionMode startActionMode(ActionMode.Callback callback) {
3558 return getParent().startActionModeForChild(this, callback);
3559 }
3560
3561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 * Register a callback to be invoked when a key is pressed in this view.
3563 * @param l the key listener to attach to this view
3564 */
3565 public void setOnKeyListener(OnKeyListener l) {
3566 mOnKeyListener = l;
3567 }
3568
3569 /**
3570 * Register a callback to be invoked when a touch event is sent to this view.
3571 * @param l the touch listener to attach to this view
3572 */
3573 public void setOnTouchListener(OnTouchListener l) {
3574 mOnTouchListener = l;
3575 }
3576
3577 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003578 * Register a callback to be invoked when a generic motion event is sent to this view.
3579 * @param l the generic motion listener to attach to this view
3580 */
3581 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3582 mOnGenericMotionListener = l;
3583 }
3584
3585 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003586 * Register a callback to be invoked when a hover event is sent to this view.
3587 * @param l the hover listener to attach to this view
3588 */
3589 public void setOnHoverListener(OnHoverListener l) {
3590 mOnHoverListener = l;
3591 }
3592
3593 /**
Joe Malin32736f02011-01-19 16:14:20 -08003594 * Register a drag event listener callback object for this View. The parameter is
3595 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3596 * View, the system calls the
3597 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3598 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003599 */
3600 public void setOnDragListener(OnDragListener l) {
3601 mOnDragListener = l;
3602 }
3603
3604 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003605 * Give this view focus. This will cause
3606 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 *
3608 * Note: this does not check whether this {@link View} should get focus, it just
3609 * gives it focus no matter what. It should only be called internally by framework
3610 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3611 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003612 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3613 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 * focus moved when requestFocus() is called. It may not always
3615 * apply, in which case use the default View.FOCUS_DOWN.
3616 * @param previouslyFocusedRect The rectangle of the view that had focus
3617 * prior in this View's coordinate system.
3618 */
3619 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3620 if (DBG) {
3621 System.out.println(this + " requestFocus()");
3622 }
3623
3624 if ((mPrivateFlags & FOCUSED) == 0) {
3625 mPrivateFlags |= FOCUSED;
3626
3627 if (mParent != null) {
3628 mParent.requestChildFocus(this, this);
3629 }
3630
3631 onFocusChanged(true, direction, previouslyFocusedRect);
3632 refreshDrawableState();
3633 }
3634 }
3635
3636 /**
3637 * Request that a rectangle of this view be visible on the screen,
3638 * scrolling if necessary just enough.
3639 *
3640 * <p>A View should call this if it maintains some notion of which part
3641 * of its content is interesting. For example, a text editing view
3642 * should call this when its cursor moves.
3643 *
3644 * @param rectangle The rectangle.
3645 * @return Whether any parent scrolled.
3646 */
3647 public boolean requestRectangleOnScreen(Rect rectangle) {
3648 return requestRectangleOnScreen(rectangle, false);
3649 }
3650
3651 /**
3652 * Request that a rectangle of this view be visible on the screen,
3653 * scrolling if necessary just enough.
3654 *
3655 * <p>A View should call this if it maintains some notion of which part
3656 * of its content is interesting. For example, a text editing view
3657 * should call this when its cursor moves.
3658 *
3659 * <p>When <code>immediate</code> is set to true, scrolling will not be
3660 * animated.
3661 *
3662 * @param rectangle The rectangle.
3663 * @param immediate True to forbid animated scrolling, false otherwise
3664 * @return Whether any parent scrolled.
3665 */
3666 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3667 View child = this;
3668 ViewParent parent = mParent;
3669 boolean scrolled = false;
3670 while (parent != null) {
3671 scrolled |= parent.requestChildRectangleOnScreen(child,
3672 rectangle, immediate);
3673
3674 // offset rect so next call has the rectangle in the
3675 // coordinate system of its direct child.
3676 rectangle.offset(child.getLeft(), child.getTop());
3677 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3678
3679 if (!(parent instanceof View)) {
3680 break;
3681 }
Romain Guy8506ab42009-06-11 17:35:47 -07003682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 child = (View) parent;
3684 parent = child.getParent();
3685 }
3686 return scrolled;
3687 }
3688
3689 /**
3690 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003691 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 */
3693 public void clearFocus() {
3694 if (DBG) {
3695 System.out.println(this + " clearFocus()");
3696 }
3697
3698 if ((mPrivateFlags & FOCUSED) != 0) {
3699 mPrivateFlags &= ~FOCUSED;
3700
3701 if (mParent != null) {
3702 mParent.clearChildFocus(this);
3703 }
3704
3705 onFocusChanged(false, 0, null);
3706 refreshDrawableState();
3707 }
3708 }
3709
3710 /**
3711 * Called to clear the focus of a view that is about to be removed.
3712 * Doesn't call clearChildFocus, which prevents this view from taking
3713 * focus again before it has been removed from the parent
3714 */
3715 void clearFocusForRemoval() {
3716 if ((mPrivateFlags & FOCUSED) != 0) {
3717 mPrivateFlags &= ~FOCUSED;
3718
3719 onFocusChanged(false, 0, null);
3720 refreshDrawableState();
3721 }
3722 }
3723
3724 /**
3725 * Called internally by the view system when a new view is getting focus.
3726 * This is what clears the old focus.
3727 */
3728 void unFocus() {
3729 if (DBG) {
3730 System.out.println(this + " unFocus()");
3731 }
3732
3733 if ((mPrivateFlags & FOCUSED) != 0) {
3734 mPrivateFlags &= ~FOCUSED;
3735
3736 onFocusChanged(false, 0, null);
3737 refreshDrawableState();
3738 }
3739 }
3740
3741 /**
3742 * Returns true if this view has focus iteself, or is the ancestor of the
3743 * view that has focus.
3744 *
3745 * @return True if this view has or contains focus, false otherwise.
3746 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003747 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 public boolean hasFocus() {
3749 return (mPrivateFlags & FOCUSED) != 0;
3750 }
3751
3752 /**
3753 * Returns true if this view is focusable or if it contains a reachable View
3754 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3755 * is a View whose parents do not block descendants focus.
3756 *
3757 * Only {@link #VISIBLE} views are considered focusable.
3758 *
3759 * @return True if the view is focusable or if the view contains a focusable
3760 * View, false otherwise.
3761 *
3762 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3763 */
3764 public boolean hasFocusable() {
3765 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3766 }
3767
3768 /**
3769 * Called by the view system when the focus state of this view changes.
3770 * When the focus change event is caused by directional navigation, direction
3771 * and previouslyFocusedRect provide insight into where the focus is coming from.
3772 * When overriding, be sure to call up through to the super class so that
3773 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003774 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 * @param gainFocus True if the View has focus; false otherwise.
3776 * @param direction The direction focus has moved when requestFocus()
3777 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003778 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3779 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3780 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3782 * system, of the previously focused view. If applicable, this will be
3783 * passed in as finer grained information about where the focus is coming
3784 * from (in addition to direction). Will be <code>null</code> otherwise.
3785 */
3786 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003787 if (gainFocus) {
3788 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3789 }
3790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 InputMethodManager imm = InputMethodManager.peekInstance();
3792 if (!gainFocus) {
3793 if (isPressed()) {
3794 setPressed(false);
3795 }
3796 if (imm != null && mAttachInfo != null
3797 && mAttachInfo.mHasWindowFocus) {
3798 imm.focusOut(this);
3799 }
Romain Guya2431d02009-04-30 16:30:00 -07003800 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 } else if (imm != null && mAttachInfo != null
3802 && mAttachInfo.mHasWindowFocus) {
3803 imm.focusIn(this);
3804 }
Romain Guy8506ab42009-06-11 17:35:47 -07003805
Romain Guy0fd89bf2011-01-26 15:41:30 -08003806 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 if (mOnFocusChangeListener != null) {
3808 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3809 }
Joe Malin32736f02011-01-19 16:14:20 -08003810
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003811 if (mAttachInfo != null) {
3812 mAttachInfo.mKeyDispatchState.reset(this);
3813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 }
3815
3816 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003817 * Sends an accessibility event of the given type. If accessiiblity is
3818 * not enabled this method has no effect. The default implementation calls
3819 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3820 * to populate information about the event source (this View), then calls
3821 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3822 * populate the text content of the event source including its descendants,
3823 * and last calls
3824 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3825 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003826 * <p>
3827 * If an {@link AccessibilityDelegate} has been specified via calling
3828 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3829 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3830 * responsible for handling this call.
3831 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003832 *
3833 * @param eventType The type of the event to send.
3834 *
3835 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3836 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3837 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003838 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003839 */
3840 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003841 if (mAccessibilityDelegate != null) {
3842 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3843 } else {
3844 sendAccessibilityEventInternal(eventType);
3845 }
3846 }
3847
3848 /**
3849 * @see #sendAccessibilityEvent(int)
3850 *
3851 * Note: Called from the default {@link AccessibilityDelegate}.
3852 */
3853 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003854 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3855 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3856 }
3857 }
3858
3859 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003860 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3861 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003862 * perform a check whether accessibility is enabled.
3863 * <p>
3864 * If an {@link AccessibilityDelegate} has been specified via calling
3865 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3866 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3867 * is responsible for handling this call.
3868 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003869 *
3870 * @param event The event to send.
3871 *
3872 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003873 */
3874 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003875 if (mAccessibilityDelegate != null) {
3876 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3877 } else {
3878 sendAccessibilityEventUncheckedInternal(event);
3879 }
3880 }
3881
3882 /**
3883 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3884 *
3885 * Note: Called from the default {@link AccessibilityDelegate}.
3886 */
3887 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003888 if (!isShown()) {
3889 return;
3890 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003891 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003892 // Only a subset of accessibility events populates text content.
3893 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3894 dispatchPopulateAccessibilityEvent(event);
3895 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003896 // In the beginning we called #isShown(), so we know that getParent() is not null.
3897 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003898 }
3899
3900 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003901 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3902 * to its children for adding their text content to the event. Note that the
3903 * event text is populated in a separate dispatch path since we add to the
3904 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003905 * A typical implementation will call
3906 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3907 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3908 * on each child. Override this method if custom population of the event text
3909 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003910 * <p>
3911 * If an {@link AccessibilityDelegate} has been specified via calling
3912 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3913 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3914 * is responsible for handling this call.
3915 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003916 * <p>
3917 * <em>Note:</em> Accessibility events of certain types are not dispatched for
3918 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
3919 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003920 *
3921 * @param event The event.
3922 *
3923 * @return True if the event population was completed.
3924 */
3925 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003926 if (mAccessibilityDelegate != null) {
3927 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3928 } else {
3929 return dispatchPopulateAccessibilityEventInternal(event);
3930 }
3931 }
3932
3933 /**
3934 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3935 *
3936 * Note: Called from the default {@link AccessibilityDelegate}.
3937 */
3938 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003939 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003940 return false;
3941 }
3942
3943 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003944 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003945 * giving a chance to this View to populate the accessibility event with its
3946 * text content. While the implementation is free to modify other event
3947 * attributes this should be performed in
3948 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3949 * <p>
3950 * Example: Adding formatted date string to an accessibility event in addition
3951 * to the text added by the super implementation.
3952 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003953 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3954 * super.onPopulateAccessibilityEvent(event);
3955 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3956 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3957 * mCurrentDate.getTimeInMillis(), flags);
3958 * event.getText().add(selectedDateUtterance);
3959 * }
3960 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003961 * <p>
3962 * If an {@link AccessibilityDelegate} has been specified via calling
3963 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3964 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3965 * is responsible for handling this call.
3966 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003967 *
3968 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003969 *
3970 * @see #sendAccessibilityEvent(int)
3971 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003972 */
3973 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003974 if (mAccessibilityDelegate != null) {
3975 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3976 } else {
3977 onPopulateAccessibilityEventInternal(event);
3978 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003979 }
3980
3981 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003982 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3983 *
3984 * Note: Called from the default {@link AccessibilityDelegate}.
3985 */
3986 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3987
3988 }
3989
3990 /**
3991 * Initializes an {@link AccessibilityEvent} with information about
3992 * this View which is the event source. In other words, the source of
3993 * an accessibility event is the view whose state change triggered firing
3994 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003995 * <p>
3996 * Example: Setting the password property of an event in addition
3997 * to properties set by the super implementation.
3998 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003999 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4000 * super.onInitializeAccessibilityEvent(event);
4001 * event.setPassword(true);
4002 * }
4003 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004004 * <p>
4005 * If an {@link AccessibilityDelegate} has been specified via calling
4006 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4007 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4008 * is responsible for handling this call.
4009 * </p>
4010 *
4011 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004012 *
4013 * @see #sendAccessibilityEvent(int)
4014 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4015 */
4016 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004017 if (mAccessibilityDelegate != null) {
4018 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4019 } else {
4020 onInitializeAccessibilityEventInternal(event);
4021 }
4022 }
4023
4024 /**
4025 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4026 *
4027 * Note: Called from the default {@link AccessibilityDelegate}.
4028 */
4029 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004030 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07004031 event.setClassName(getClass().getName());
4032 event.setPackageName(getContext().getPackageName());
4033 event.setEnabled(isEnabled());
4034 event.setContentDescription(mContentDescription);
4035
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004036 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4037 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4038 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4039 FOCUSABLES_ALL);
4040 event.setItemCount(focusablesTempList.size());
4041 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4042 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004043 }
4044 }
4045
4046 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004047 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4048 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4049 * This method is responsible for obtaining an accessibility node info from a
4050 * pool of reusable instances and calling
4051 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4052 * initialize the former.
4053 * <p>
4054 * Note: The client is responsible for recycling the obtained instance by calling
4055 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4056 * </p>
4057 * @return A populated {@link AccessibilityNodeInfo}.
4058 *
4059 * @see AccessibilityNodeInfo
4060 */
4061 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4062 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4063 onInitializeAccessibilityNodeInfo(info);
4064 return info;
4065 }
4066
4067 /**
4068 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4069 * The base implementation sets:
4070 * <ul>
4071 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004072 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4073 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004074 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4075 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4076 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4077 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4078 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4079 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4080 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4081 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4082 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4083 * </ul>
4084 * <p>
4085 * Subclasses should override this method, call the super implementation,
4086 * and set additional attributes.
4087 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004088 * <p>
4089 * If an {@link AccessibilityDelegate} has been specified via calling
4090 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4091 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4092 * is responsible for handling this call.
4093 * </p>
4094 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004095 * @param info The instance to initialize.
4096 */
4097 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004098 if (mAccessibilityDelegate != null) {
4099 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4100 } else {
4101 onInitializeAccessibilityNodeInfoInternal(info);
4102 }
4103 }
4104
4105 /**
4106 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4107 *
4108 * Note: Called from the default {@link AccessibilityDelegate}.
4109 */
4110 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004111 Rect bounds = mAttachInfo.mTmpInvalRect;
4112 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004113 info.setBoundsInParent(bounds);
4114
4115 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4116 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004117 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004118 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4119 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004120
4121 ViewParent parent = getParent();
4122 if (parent instanceof View) {
4123 View parentView = (View) parent;
4124 info.setParent(parentView);
4125 }
4126
4127 info.setPackageName(mContext.getPackageName());
4128 info.setClassName(getClass().getName());
4129 info.setContentDescription(getContentDescription());
4130
4131 info.setEnabled(isEnabled());
4132 info.setClickable(isClickable());
4133 info.setFocusable(isFocusable());
4134 info.setFocused(isFocused());
4135 info.setSelected(isSelected());
4136 info.setLongClickable(isLongClickable());
4137
4138 // TODO: These make sense only if we are in an AdapterView but all
4139 // views can be selected. Maybe from accessiiblity perspective
4140 // we should report as selectable view in an AdapterView.
4141 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4142 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4143
4144 if (isFocusable()) {
4145 if (isFocused()) {
4146 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4147 } else {
4148 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4149 }
4150 }
4151 }
4152
4153 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004154 * Sets a delegate for implementing accessibility support via compositon as
4155 * opposed to inheritance. The delegate's primary use is for implementing
4156 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4157 *
4158 * @param delegate The delegate instance.
4159 *
4160 * @see AccessibilityDelegate
4161 */
4162 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4163 mAccessibilityDelegate = delegate;
4164 }
4165
4166 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004167 * Gets the unique identifier of this view on the screen for accessibility purposes.
4168 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4169 *
4170 * @return The view accessibility id.
4171 *
4172 * @hide
4173 */
4174 public int getAccessibilityViewId() {
4175 if (mAccessibilityViewId == NO_ID) {
4176 mAccessibilityViewId = sNextAccessibilityViewId++;
4177 }
4178 return mAccessibilityViewId;
4179 }
4180
4181 /**
4182 * Gets the unique identifier of the window in which this View reseides.
4183 *
4184 * @return The window accessibility id.
4185 *
4186 * @hide
4187 */
4188 public int getAccessibilityWindowId() {
4189 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4190 }
4191
4192 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004193 * Gets the {@link View} description. It briefly describes the view and is
4194 * primarily used for accessibility support. Set this property to enable
4195 * better accessibility support for your application. This is especially
4196 * true for views that do not have textual representation (For example,
4197 * ImageButton).
4198 *
4199 * @return The content descriptiopn.
4200 *
4201 * @attr ref android.R.styleable#View_contentDescription
4202 */
4203 public CharSequence getContentDescription() {
4204 return mContentDescription;
4205 }
4206
4207 /**
4208 * Sets the {@link View} description. It briefly describes the view and is
4209 * primarily used for accessibility support. Set this property to enable
4210 * better accessibility support for your application. This is especially
4211 * true for views that do not have textual representation (For example,
4212 * ImageButton).
4213 *
4214 * @param contentDescription The content description.
4215 *
4216 * @attr ref android.R.styleable#View_contentDescription
4217 */
4218 public void setContentDescription(CharSequence contentDescription) {
4219 mContentDescription = contentDescription;
4220 }
4221
4222 /**
Romain Guya2431d02009-04-30 16:30:00 -07004223 * Invoked whenever this view loses focus, either by losing window focus or by losing
4224 * focus within its window. This method can be used to clear any state tied to the
4225 * focus. For instance, if a button is held pressed with the trackball and the window
4226 * loses focus, this method can be used to cancel the press.
4227 *
4228 * Subclasses of View overriding this method should always call super.onFocusLost().
4229 *
4230 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004231 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004232 *
4233 * @hide pending API council approval
4234 */
4235 protected void onFocusLost() {
4236 resetPressedState();
4237 }
4238
4239 private void resetPressedState() {
4240 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4241 return;
4242 }
4243
4244 if (isPressed()) {
4245 setPressed(false);
4246
4247 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004248 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004249 }
4250 }
4251 }
4252
4253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 * Returns true if this view has focus
4255 *
4256 * @return True if this view has focus, false otherwise.
4257 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004258 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 public boolean isFocused() {
4260 return (mPrivateFlags & FOCUSED) != 0;
4261 }
4262
4263 /**
4264 * Find the view in the hierarchy rooted at this view that currently has
4265 * focus.
4266 *
4267 * @return The view that currently has focus, or null if no focused view can
4268 * be found.
4269 */
4270 public View findFocus() {
4271 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4272 }
4273
4274 /**
4275 * Change whether this view is one of the set of scrollable containers in
4276 * its window. This will be used to determine whether the window can
4277 * resize or must pan when a soft input area is open -- scrollable
4278 * containers allow the window to use resize mode since the container
4279 * will appropriately shrink.
4280 */
4281 public void setScrollContainer(boolean isScrollContainer) {
4282 if (isScrollContainer) {
4283 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4284 mAttachInfo.mScrollContainers.add(this);
4285 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4286 }
4287 mPrivateFlags |= SCROLL_CONTAINER;
4288 } else {
4289 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4290 mAttachInfo.mScrollContainers.remove(this);
4291 }
4292 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4293 }
4294 }
4295
4296 /**
4297 * Returns the quality of the drawing cache.
4298 *
4299 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4300 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4301 *
4302 * @see #setDrawingCacheQuality(int)
4303 * @see #setDrawingCacheEnabled(boolean)
4304 * @see #isDrawingCacheEnabled()
4305 *
4306 * @attr ref android.R.styleable#View_drawingCacheQuality
4307 */
4308 public int getDrawingCacheQuality() {
4309 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4310 }
4311
4312 /**
4313 * Set the drawing cache quality of this view. This value is used only when the
4314 * drawing cache is enabled
4315 *
4316 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4317 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4318 *
4319 * @see #getDrawingCacheQuality()
4320 * @see #setDrawingCacheEnabled(boolean)
4321 * @see #isDrawingCacheEnabled()
4322 *
4323 * @attr ref android.R.styleable#View_drawingCacheQuality
4324 */
4325 public void setDrawingCacheQuality(int quality) {
4326 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4327 }
4328
4329 /**
4330 * Returns whether the screen should remain on, corresponding to the current
4331 * value of {@link #KEEP_SCREEN_ON}.
4332 *
4333 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4334 *
4335 * @see #setKeepScreenOn(boolean)
4336 *
4337 * @attr ref android.R.styleable#View_keepScreenOn
4338 */
4339 public boolean getKeepScreenOn() {
4340 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4341 }
4342
4343 /**
4344 * Controls whether the screen should remain on, modifying the
4345 * value of {@link #KEEP_SCREEN_ON}.
4346 *
4347 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4348 *
4349 * @see #getKeepScreenOn()
4350 *
4351 * @attr ref android.R.styleable#View_keepScreenOn
4352 */
4353 public void setKeepScreenOn(boolean keepScreenOn) {
4354 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4355 }
4356
4357 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004358 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4359 * @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 -08004360 *
4361 * @attr ref android.R.styleable#View_nextFocusLeft
4362 */
4363 public int getNextFocusLeftId() {
4364 return mNextFocusLeftId;
4365 }
4366
4367 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004368 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4369 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4370 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 *
4372 * @attr ref android.R.styleable#View_nextFocusLeft
4373 */
4374 public void setNextFocusLeftId(int nextFocusLeftId) {
4375 mNextFocusLeftId = nextFocusLeftId;
4376 }
4377
4378 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004379 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4380 * @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 -08004381 *
4382 * @attr ref android.R.styleable#View_nextFocusRight
4383 */
4384 public int getNextFocusRightId() {
4385 return mNextFocusRightId;
4386 }
4387
4388 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004389 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4390 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4391 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 *
4393 * @attr ref android.R.styleable#View_nextFocusRight
4394 */
4395 public void setNextFocusRightId(int nextFocusRightId) {
4396 mNextFocusRightId = nextFocusRightId;
4397 }
4398
4399 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004400 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4401 * @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 -08004402 *
4403 * @attr ref android.R.styleable#View_nextFocusUp
4404 */
4405 public int getNextFocusUpId() {
4406 return mNextFocusUpId;
4407 }
4408
4409 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004410 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4411 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4412 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 *
4414 * @attr ref android.R.styleable#View_nextFocusUp
4415 */
4416 public void setNextFocusUpId(int nextFocusUpId) {
4417 mNextFocusUpId = nextFocusUpId;
4418 }
4419
4420 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004421 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4422 * @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 -08004423 *
4424 * @attr ref android.R.styleable#View_nextFocusDown
4425 */
4426 public int getNextFocusDownId() {
4427 return mNextFocusDownId;
4428 }
4429
4430 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004431 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4432 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4433 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 *
4435 * @attr ref android.R.styleable#View_nextFocusDown
4436 */
4437 public void setNextFocusDownId(int nextFocusDownId) {
4438 mNextFocusDownId = nextFocusDownId;
4439 }
4440
4441 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004442 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4443 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4444 *
4445 * @attr ref android.R.styleable#View_nextFocusForward
4446 */
4447 public int getNextFocusForwardId() {
4448 return mNextFocusForwardId;
4449 }
4450
4451 /**
4452 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4453 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4454 * decide automatically.
4455 *
4456 * @attr ref android.R.styleable#View_nextFocusForward
4457 */
4458 public void setNextFocusForwardId(int nextFocusForwardId) {
4459 mNextFocusForwardId = nextFocusForwardId;
4460 }
4461
4462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 * Returns the visibility of this view and all of its ancestors
4464 *
4465 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4466 */
4467 public boolean isShown() {
4468 View current = this;
4469 //noinspection ConstantConditions
4470 do {
4471 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4472 return false;
4473 }
4474 ViewParent parent = current.mParent;
4475 if (parent == null) {
4476 return false; // We are not attached to the view root
4477 }
4478 if (!(parent instanceof View)) {
4479 return true;
4480 }
4481 current = (View) parent;
4482 } while (current != null);
4483
4484 return false;
4485 }
4486
4487 /**
4488 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4489 * is set
4490 *
4491 * @param insets Insets for system windows
4492 *
4493 * @return True if this view applied the insets, false otherwise
4494 */
4495 protected boolean fitSystemWindows(Rect insets) {
4496 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4497 mPaddingLeft = insets.left;
4498 mPaddingTop = insets.top;
4499 mPaddingRight = insets.right;
4500 mPaddingBottom = insets.bottom;
4501 requestLayout();
4502 return true;
4503 }
4504 return false;
4505 }
4506
4507 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004508 * Set whether or not this view should account for system screen decorations
4509 * such as the status bar and inset its content. This allows this view to be
4510 * positioned in absolute screen coordinates and remain visible to the user.
4511 *
4512 * <p>This should only be used by top-level window decor views.
4513 *
4514 * @param fitSystemWindows true to inset content for system screen decorations, false for
4515 * default behavior.
4516 *
4517 * @attr ref android.R.styleable#View_fitsSystemWindows
4518 */
4519 public void setFitsSystemWindows(boolean fitSystemWindows) {
4520 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4521 }
4522
4523 /**
4524 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4525 * will account for system screen decorations such as the status bar and inset its
4526 * content. This allows the view to be positioned in absolute screen coordinates
4527 * and remain visible to the user.
4528 *
4529 * @return true if this view will adjust its content bounds for system screen decorations.
4530 *
4531 * @attr ref android.R.styleable#View_fitsSystemWindows
4532 */
4533 public boolean fitsSystemWindows() {
4534 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4535 }
4536
4537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 * Returns the visibility status for this view.
4539 *
4540 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4541 * @attr ref android.R.styleable#View_visibility
4542 */
4543 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004544 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4545 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4546 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 })
4548 public int getVisibility() {
4549 return mViewFlags & VISIBILITY_MASK;
4550 }
4551
4552 /**
4553 * Set the enabled state of this view.
4554 *
4555 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4556 * @attr ref android.R.styleable#View_visibility
4557 */
4558 @RemotableViewMethod
4559 public void setVisibility(int visibility) {
4560 setFlags(visibility, VISIBILITY_MASK);
4561 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4562 }
4563
4564 /**
4565 * Returns the enabled status for this view. The interpretation of the
4566 * enabled state varies by subclass.
4567 *
4568 * @return True if this view is enabled, false otherwise.
4569 */
4570 @ViewDebug.ExportedProperty
4571 public boolean isEnabled() {
4572 return (mViewFlags & ENABLED_MASK) == ENABLED;
4573 }
4574
4575 /**
4576 * Set the enabled state of this view. The interpretation of the enabled
4577 * state varies by subclass.
4578 *
4579 * @param enabled True if this view is enabled, false otherwise.
4580 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004581 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004583 if (enabled == isEnabled()) return;
4584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4586
4587 /*
4588 * The View most likely has to change its appearance, so refresh
4589 * the drawable state.
4590 */
4591 refreshDrawableState();
4592
4593 // Invalidate too, since the default behavior for views is to be
4594 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004595 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 }
4597
4598 /**
4599 * Set whether this view can receive the focus.
4600 *
4601 * Setting this to false will also ensure that this view is not focusable
4602 * in touch mode.
4603 *
4604 * @param focusable If true, this view can receive the focus.
4605 *
4606 * @see #setFocusableInTouchMode(boolean)
4607 * @attr ref android.R.styleable#View_focusable
4608 */
4609 public void setFocusable(boolean focusable) {
4610 if (!focusable) {
4611 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4612 }
4613 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4614 }
4615
4616 /**
4617 * Set whether this view can receive focus while in touch mode.
4618 *
4619 * Setting this to true will also ensure that this view is focusable.
4620 *
4621 * @param focusableInTouchMode If true, this view can receive the focus while
4622 * in touch mode.
4623 *
4624 * @see #setFocusable(boolean)
4625 * @attr ref android.R.styleable#View_focusableInTouchMode
4626 */
4627 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4628 // Focusable in touch mode should always be set before the focusable flag
4629 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4630 // which, in touch mode, will not successfully request focus on this view
4631 // because the focusable in touch mode flag is not set
4632 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4633 if (focusableInTouchMode) {
4634 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4635 }
4636 }
4637
4638 /**
4639 * Set whether this view should have sound effects enabled for events such as
4640 * clicking and touching.
4641 *
4642 * <p>You may wish to disable sound effects for a view if you already play sounds,
4643 * for instance, a dial key that plays dtmf tones.
4644 *
4645 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4646 * @see #isSoundEffectsEnabled()
4647 * @see #playSoundEffect(int)
4648 * @attr ref android.R.styleable#View_soundEffectsEnabled
4649 */
4650 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4651 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4652 }
4653
4654 /**
4655 * @return whether this view should have sound effects enabled for events such as
4656 * clicking and touching.
4657 *
4658 * @see #setSoundEffectsEnabled(boolean)
4659 * @see #playSoundEffect(int)
4660 * @attr ref android.R.styleable#View_soundEffectsEnabled
4661 */
4662 @ViewDebug.ExportedProperty
4663 public boolean isSoundEffectsEnabled() {
4664 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4665 }
4666
4667 /**
4668 * Set whether this view should have haptic feedback for events such as
4669 * long presses.
4670 *
4671 * <p>You may wish to disable haptic feedback if your view already controls
4672 * its own haptic feedback.
4673 *
4674 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4675 * @see #isHapticFeedbackEnabled()
4676 * @see #performHapticFeedback(int)
4677 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4678 */
4679 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4680 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4681 }
4682
4683 /**
4684 * @return whether this view should have haptic feedback enabled for events
4685 * long presses.
4686 *
4687 * @see #setHapticFeedbackEnabled(boolean)
4688 * @see #performHapticFeedback(int)
4689 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4690 */
4691 @ViewDebug.ExportedProperty
4692 public boolean isHapticFeedbackEnabled() {
4693 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4694 }
4695
4696 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004697 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004698 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004699 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4700 * {@link #LAYOUT_DIRECTION_RTL},
4701 * {@link #LAYOUT_DIRECTION_INHERIT} or
4702 * {@link #LAYOUT_DIRECTION_LOCALE}.
4703 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004704 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004705 * @hide
4706 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004707 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004708 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4709 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4710 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4711 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004712 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004713 public int getLayoutDirection() {
4714 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004715 }
4716
4717 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004718 * Set the layout direction for this view. This will propagate a reset of layout direction
4719 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004720 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004721 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4722 * {@link #LAYOUT_DIRECTION_RTL},
4723 * {@link #LAYOUT_DIRECTION_INHERIT} or
4724 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004725 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004726 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004727 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004728 * @hide
4729 */
4730 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004731 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004732 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004733 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004734 // Setting the flag will also request a layout.
4735 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4736 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004737 }
4738
4739 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004740 * Returns the resolved layout direction for this view.
4741 *
4742 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4743 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4744 *
4745 * @hide
4746 */
4747 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4748 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4749 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4750 })
4751 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004752 resolveLayoutDirectionIfNeeded();
4753 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004754 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4755 }
4756
4757 /**
4758 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4759 * layout attribute and/or the inherited value from the parent.</p>
4760 *
4761 * @return true if the layout is right-to-left.
4762 *
4763 * @hide
4764 */
4765 @ViewDebug.ExportedProperty(category = "layout")
4766 public boolean isLayoutRtl() {
4767 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4768 }
4769
4770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 * If this view doesn't do any drawing on its own, set this flag to
4772 * allow further optimizations. By default, this flag is not set on
4773 * View, but could be set on some View subclasses such as ViewGroup.
4774 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004775 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4776 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 *
4778 * @param willNotDraw whether or not this View draw on its own
4779 */
4780 public void setWillNotDraw(boolean willNotDraw) {
4781 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4782 }
4783
4784 /**
4785 * Returns whether or not this View draws on its own.
4786 *
4787 * @return true if this view has nothing to draw, false otherwise
4788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004789 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 public boolean willNotDraw() {
4791 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4792 }
4793
4794 /**
4795 * When a View's drawing cache is enabled, drawing is redirected to an
4796 * offscreen bitmap. Some views, like an ImageView, must be able to
4797 * bypass this mechanism if they already draw a single bitmap, to avoid
4798 * unnecessary usage of the memory.
4799 *
4800 * @param willNotCacheDrawing true if this view does not cache its
4801 * drawing, false otherwise
4802 */
4803 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4804 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4805 }
4806
4807 /**
4808 * Returns whether or not this View can cache its drawing or not.
4809 *
4810 * @return true if this view does not cache its drawing, false otherwise
4811 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004812 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 public boolean willNotCacheDrawing() {
4814 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4815 }
4816
4817 /**
4818 * Indicates whether this view reacts to click events or not.
4819 *
4820 * @return true if the view is clickable, false otherwise
4821 *
4822 * @see #setClickable(boolean)
4823 * @attr ref android.R.styleable#View_clickable
4824 */
4825 @ViewDebug.ExportedProperty
4826 public boolean isClickable() {
4827 return (mViewFlags & CLICKABLE) == CLICKABLE;
4828 }
4829
4830 /**
4831 * Enables or disables click events for this view. When a view
4832 * is clickable it will change its state to "pressed" on every click.
4833 * Subclasses should set the view clickable to visually react to
4834 * user's clicks.
4835 *
4836 * @param clickable true to make the view clickable, false otherwise
4837 *
4838 * @see #isClickable()
4839 * @attr ref android.R.styleable#View_clickable
4840 */
4841 public void setClickable(boolean clickable) {
4842 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4843 }
4844
4845 /**
4846 * Indicates whether this view reacts to long click events or not.
4847 *
4848 * @return true if the view is long clickable, false otherwise
4849 *
4850 * @see #setLongClickable(boolean)
4851 * @attr ref android.R.styleable#View_longClickable
4852 */
4853 public boolean isLongClickable() {
4854 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4855 }
4856
4857 /**
4858 * Enables or disables long click events for this view. When a view is long
4859 * clickable it reacts to the user holding down the button for a longer
4860 * duration than a tap. This event can either launch the listener or a
4861 * context menu.
4862 *
4863 * @param longClickable true to make the view long clickable, false otherwise
4864 * @see #isLongClickable()
4865 * @attr ref android.R.styleable#View_longClickable
4866 */
4867 public void setLongClickable(boolean longClickable) {
4868 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4869 }
4870
4871 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004872 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 *
4874 * @see #isClickable()
4875 * @see #setClickable(boolean)
4876 *
4877 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4878 * the View's internal state from a previously set "pressed" state.
4879 */
4880 public void setPressed(boolean pressed) {
4881 if (pressed) {
4882 mPrivateFlags |= PRESSED;
4883 } else {
4884 mPrivateFlags &= ~PRESSED;
4885 }
4886 refreshDrawableState();
4887 dispatchSetPressed(pressed);
4888 }
4889
4890 /**
4891 * Dispatch setPressed to all of this View's children.
4892 *
4893 * @see #setPressed(boolean)
4894 *
4895 * @param pressed The new pressed state
4896 */
4897 protected void dispatchSetPressed(boolean pressed) {
4898 }
4899
4900 /**
4901 * Indicates whether the view is currently in pressed state. Unless
4902 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4903 * the pressed state.
4904 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004905 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 * @see #isClickable()
4907 * @see #setClickable(boolean)
4908 *
4909 * @return true if the view is currently pressed, false otherwise
4910 */
4911 public boolean isPressed() {
4912 return (mPrivateFlags & PRESSED) == PRESSED;
4913 }
4914
4915 /**
4916 * Indicates whether this view will save its state (that is,
4917 * whether its {@link #onSaveInstanceState} method will be called).
4918 *
4919 * @return Returns true if the view state saving is enabled, else false.
4920 *
4921 * @see #setSaveEnabled(boolean)
4922 * @attr ref android.R.styleable#View_saveEnabled
4923 */
4924 public boolean isSaveEnabled() {
4925 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4926 }
4927
4928 /**
4929 * Controls whether the saving of this view's state is
4930 * enabled (that is, whether its {@link #onSaveInstanceState} method
4931 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004932 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 * for its state to be saved. This flag can only disable the
4934 * saving of this view; any child views may still have their state saved.
4935 *
4936 * @param enabled Set to false to <em>disable</em> state saving, or true
4937 * (the default) to allow it.
4938 *
4939 * @see #isSaveEnabled()
4940 * @see #setId(int)
4941 * @see #onSaveInstanceState()
4942 * @attr ref android.R.styleable#View_saveEnabled
4943 */
4944 public void setSaveEnabled(boolean enabled) {
4945 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4946 }
4947
Jeff Brown85a31762010-09-01 17:01:00 -07004948 /**
4949 * Gets whether the framework should discard touches when the view's
4950 * window is obscured by another visible window.
4951 * Refer to the {@link View} security documentation for more details.
4952 *
4953 * @return True if touch filtering is enabled.
4954 *
4955 * @see #setFilterTouchesWhenObscured(boolean)
4956 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4957 */
4958 @ViewDebug.ExportedProperty
4959 public boolean getFilterTouchesWhenObscured() {
4960 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4961 }
4962
4963 /**
4964 * Sets whether the framework should discard touches when the view's
4965 * window is obscured by another visible window.
4966 * Refer to the {@link View} security documentation for more details.
4967 *
4968 * @param enabled True if touch filtering should be enabled.
4969 *
4970 * @see #getFilterTouchesWhenObscured
4971 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4972 */
4973 public void setFilterTouchesWhenObscured(boolean enabled) {
4974 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4975 FILTER_TOUCHES_WHEN_OBSCURED);
4976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977
4978 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004979 * Indicates whether the entire hierarchy under this view will save its
4980 * state when a state saving traversal occurs from its parent. The default
4981 * is true; if false, these views will not be saved unless
4982 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4983 *
4984 * @return Returns true if the view state saving from parent is enabled, else false.
4985 *
4986 * @see #setSaveFromParentEnabled(boolean)
4987 */
4988 public boolean isSaveFromParentEnabled() {
4989 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4990 }
4991
4992 /**
4993 * Controls whether the entire hierarchy under this view will save its
4994 * state when a state saving traversal occurs from its parent. The default
4995 * is true; if false, these views will not be saved unless
4996 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4997 *
4998 * @param enabled Set to false to <em>disable</em> state saving, or true
4999 * (the default) to allow it.
5000 *
5001 * @see #isSaveFromParentEnabled()
5002 * @see #setId(int)
5003 * @see #onSaveInstanceState()
5004 */
5005 public void setSaveFromParentEnabled(boolean enabled) {
5006 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5007 }
5008
5009
5010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 * Returns whether this View is able to take focus.
5012 *
5013 * @return True if this view can take focus, or false otherwise.
5014 * @attr ref android.R.styleable#View_focusable
5015 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005016 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 public final boolean isFocusable() {
5018 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5019 }
5020
5021 /**
5022 * When a view is focusable, it may not want to take focus when in touch mode.
5023 * For example, a button would like focus when the user is navigating via a D-pad
5024 * so that the user can click on it, but once the user starts touching the screen,
5025 * the button shouldn't take focus
5026 * @return Whether the view is focusable in touch mode.
5027 * @attr ref android.R.styleable#View_focusableInTouchMode
5028 */
5029 @ViewDebug.ExportedProperty
5030 public final boolean isFocusableInTouchMode() {
5031 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5032 }
5033
5034 /**
5035 * Find the nearest view in the specified direction that can take focus.
5036 * This does not actually give focus to that view.
5037 *
5038 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5039 *
5040 * @return The nearest focusable in the specified direction, or null if none
5041 * can be found.
5042 */
5043 public View focusSearch(int direction) {
5044 if (mParent != null) {
5045 return mParent.focusSearch(this, direction);
5046 } else {
5047 return null;
5048 }
5049 }
5050
5051 /**
5052 * This method is the last chance for the focused view and its ancestors to
5053 * respond to an arrow key. This is called when the focused view did not
5054 * consume the key internally, nor could the view system find a new view in
5055 * the requested direction to give focus to.
5056 *
5057 * @param focused The currently focused view.
5058 * @param direction The direction focus wants to move. One of FOCUS_UP,
5059 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5060 * @return True if the this view consumed this unhandled move.
5061 */
5062 public boolean dispatchUnhandledMove(View focused, int direction) {
5063 return false;
5064 }
5065
5066 /**
5067 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005068 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005070 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5071 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 * @return The user specified next view, or null if there is none.
5073 */
5074 View findUserSetNextFocus(View root, int direction) {
5075 switch (direction) {
5076 case FOCUS_LEFT:
5077 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005078 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 case FOCUS_RIGHT:
5080 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005081 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 case FOCUS_UP:
5083 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005084 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 case FOCUS_DOWN:
5086 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005087 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005088 case FOCUS_FORWARD:
5089 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005090 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005091 case FOCUS_BACKWARD: {
5092 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005093 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005094 @Override
5095 public boolean apply(View t) {
5096 return t.mNextFocusForwardId == id;
5097 }
5098 });
5099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 }
5101 return null;
5102 }
5103
Jeff Brown4dfbec22011-08-15 14:55:37 -07005104 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5105 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5106 @Override
5107 public boolean apply(View t) {
5108 return t.mID == childViewId;
5109 }
5110 });
5111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 if (result == null) {
5113 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5114 + "by user for id " + childViewId);
5115 }
5116 return result;
5117 }
5118
5119 /**
5120 * Find and return all focusable views that are descendants of this view,
5121 * possibly including this view if it is focusable itself.
5122 *
5123 * @param direction The direction of the focus
5124 * @return A list of focusable views
5125 */
5126 public ArrayList<View> getFocusables(int direction) {
5127 ArrayList<View> result = new ArrayList<View>(24);
5128 addFocusables(result, direction);
5129 return result;
5130 }
5131
5132 /**
5133 * Add any focusable views that are descendants of this view (possibly
5134 * including this view if it is focusable itself) to views. If we are in touch mode,
5135 * only add views that are also focusable in touch mode.
5136 *
5137 * @param views Focusable views found so far
5138 * @param direction The direction of the focus
5139 */
5140 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005141 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143
svetoslavganov75986cf2009-05-14 22:28:01 -07005144 /**
5145 * Adds any focusable views that are descendants of this view (possibly
5146 * including this view if it is focusable itself) to views. This method
5147 * adds all focusable views regardless if we are in touch mode or
5148 * only views focusable in touch mode if we are in touch mode depending on
5149 * the focusable mode paramater.
5150 *
5151 * @param views Focusable views found so far or null if all we are interested is
5152 * the number of focusables.
5153 * @param direction The direction of the focus.
5154 * @param focusableMode The type of focusables to be added.
5155 *
5156 * @see #FOCUSABLES_ALL
5157 * @see #FOCUSABLES_TOUCH_MODE
5158 */
5159 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5160 if (!isFocusable()) {
5161 return;
5162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163
svetoslavganov75986cf2009-05-14 22:28:01 -07005164 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5165 isInTouchMode() && !isFocusableInTouchMode()) {
5166 return;
5167 }
5168
5169 if (views != null) {
5170 views.add(this);
5171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 }
5173
5174 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005175 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005176 * The search is performed by either the text that the View renders or the content
5177 * description that describes the view for accessibility purposes and the view does
5178 * not render or both. Clients can specify how the search is to be performed via
5179 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5180 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005181 *
5182 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005183 * @param searched The text to match against.
5184 *
5185 * @see #FIND_VIEWS_WITH_TEXT
5186 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5187 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005188 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005189 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5190 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5191 && !TextUtils.isEmpty(mContentDescription)) {
5192 String searchedLowerCase = searched.toString().toLowerCase();
5193 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5194 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5195 outViews.add(this);
5196 }
5197 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005198 }
5199
5200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 * Find and return all touchable views that are descendants of this view,
5202 * possibly including this view if it is touchable itself.
5203 *
5204 * @return A list of touchable views
5205 */
5206 public ArrayList<View> getTouchables() {
5207 ArrayList<View> result = new ArrayList<View>();
5208 addTouchables(result);
5209 return result;
5210 }
5211
5212 /**
5213 * Add any touchable views that are descendants of this view (possibly
5214 * including this view if it is touchable itself) to views.
5215 *
5216 * @param views Touchable views found so far
5217 */
5218 public void addTouchables(ArrayList<View> views) {
5219 final int viewFlags = mViewFlags;
5220
5221 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5222 && (viewFlags & ENABLED_MASK) == ENABLED) {
5223 views.add(this);
5224 }
5225 }
5226
5227 /**
5228 * Call this to try to give focus to a specific view or to one of its
5229 * descendants.
5230 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005231 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5232 * false), or if it is focusable and it is not focusable in touch mode
5233 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005235 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 * have focus, and you want your parent to look for the next one.
5237 *
5238 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5239 * {@link #FOCUS_DOWN} and <code>null</code>.
5240 *
5241 * @return Whether this view or one of its descendants actually took focus.
5242 */
5243 public final boolean requestFocus() {
5244 return requestFocus(View.FOCUS_DOWN);
5245 }
5246
5247
5248 /**
5249 * Call this to try to give focus to a specific view or to one of its
5250 * descendants and give it a hint about what direction focus is heading.
5251 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005252 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5253 * false), or if it is focusable and it is not focusable in touch mode
5254 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005256 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 * have focus, and you want your parent to look for the next one.
5258 *
5259 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5260 * <code>null</code> set for the previously focused rectangle.
5261 *
5262 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5263 * @return Whether this view or one of its descendants actually took focus.
5264 */
5265 public final boolean requestFocus(int direction) {
5266 return requestFocus(direction, null);
5267 }
5268
5269 /**
5270 * Call this to try to give focus to a specific view or to one of its descendants
5271 * and give it hints about the direction and a specific rectangle that the focus
5272 * is coming from. The rectangle can help give larger views a finer grained hint
5273 * about where focus is coming from, and therefore, where to show selection, or
5274 * forward focus change internally.
5275 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005276 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5277 * false), or if it is focusable and it is not focusable in touch mode
5278 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 *
5280 * A View will not take focus if it is not visible.
5281 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005282 * A View will not take focus if one of its parents has
5283 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5284 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005286 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 * have focus, and you want your parent to look for the next one.
5288 *
5289 * You may wish to override this method if your custom {@link View} has an internal
5290 * {@link View} that it wishes to forward the request to.
5291 *
5292 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5293 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5294 * to give a finer grained hint about where focus is coming from. May be null
5295 * if there is no hint.
5296 * @return Whether this view or one of its descendants actually took focus.
5297 */
5298 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5299 // need to be focusable
5300 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5301 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5302 return false;
5303 }
5304
5305 // need to be focusable in touch mode if in touch mode
5306 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005307 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5308 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 }
5310
5311 // need to not have any parents blocking us
5312 if (hasAncestorThatBlocksDescendantFocus()) {
5313 return false;
5314 }
5315
5316 handleFocusGainInternal(direction, previouslyFocusedRect);
5317 return true;
5318 }
5319
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005320 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005321 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005322 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005323 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005324 }
5325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 /**
5327 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5328 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5329 * touch mode to request focus when they are touched.
5330 *
5331 * @return Whether this view or one of its descendants actually took focus.
5332 *
5333 * @see #isInTouchMode()
5334 *
5335 */
5336 public final boolean requestFocusFromTouch() {
5337 // Leave touch mode if we need to
5338 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005339 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005340 if (viewRoot != null) {
5341 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 }
5343 }
5344 return requestFocus(View.FOCUS_DOWN);
5345 }
5346
5347 /**
5348 * @return Whether any ancestor of this view blocks descendant focus.
5349 */
5350 private boolean hasAncestorThatBlocksDescendantFocus() {
5351 ViewParent ancestor = mParent;
5352 while (ancestor instanceof ViewGroup) {
5353 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5354 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5355 return true;
5356 } else {
5357 ancestor = vgAncestor.getParent();
5358 }
5359 }
5360 return false;
5361 }
5362
5363 /**
Romain Guya440b002010-02-24 15:57:54 -08005364 * @hide
5365 */
5366 public void dispatchStartTemporaryDetach() {
5367 onStartTemporaryDetach();
5368 }
5369
5370 /**
5371 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5373 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005374 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 */
5376 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005377 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005378 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005379 }
5380
5381 /**
5382 * @hide
5383 */
5384 public void dispatchFinishTemporaryDetach() {
5385 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 }
Romain Guy8506ab42009-06-11 17:35:47 -07005387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 /**
5389 * Called after {@link #onStartTemporaryDetach} when the container is done
5390 * changing the view.
5391 */
5392 public void onFinishTemporaryDetach() {
5393 }
Romain Guy8506ab42009-06-11 17:35:47 -07005394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005396 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5397 * for this view's window. Returns null if the view is not currently attached
5398 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005399 * just use the standard high-level event callbacks like
5400 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005401 */
5402 public KeyEvent.DispatcherState getKeyDispatcherState() {
5403 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5404 }
Joe Malin32736f02011-01-19 16:14:20 -08005405
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 * Dispatch a key event before it is processed by any input method
5408 * associated with the view hierarchy. This can be used to intercept
5409 * key events in special situations before the IME consumes them; a
5410 * typical example would be handling the BACK key to update the application's
5411 * UI instead of allowing the IME to see it and close itself.
5412 *
5413 * @param event The key event to be dispatched.
5414 * @return True if the event was handled, false otherwise.
5415 */
5416 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5417 return onKeyPreIme(event.getKeyCode(), event);
5418 }
5419
5420 /**
5421 * Dispatch a key event to the next view on the focus path. This path runs
5422 * from the top of the view tree down to the currently focused view. If this
5423 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5424 * the next node down the focus path. This method also fires any key
5425 * listeners.
5426 *
5427 * @param event The key event to be dispatched.
5428 * @return True if the event was handled, false otherwise.
5429 */
5430 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005431 if (mInputEventConsistencyVerifier != null) {
5432 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434
Jeff Brown21bc5c92011-02-28 18:27:14 -08005435 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005436 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5438 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5439 return true;
5440 }
5441
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005442 if (event.dispatch(this, mAttachInfo != null
5443 ? mAttachInfo.mKeyDispatchState : null, this)) {
5444 return true;
5445 }
5446
5447 if (mInputEventConsistencyVerifier != null) {
5448 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5449 }
5450 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 }
5452
5453 /**
5454 * Dispatches a key shortcut event.
5455 *
5456 * @param event The key event to be dispatched.
5457 * @return True if the event was handled by the view, false otherwise.
5458 */
5459 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5460 return onKeyShortcut(event.getKeyCode(), event);
5461 }
5462
5463 /**
5464 * Pass the touch screen motion event down to the target view, or this
5465 * view if it is the target.
5466 *
5467 * @param event The motion event to be dispatched.
5468 * @return True if the event was handled by the view, false otherwise.
5469 */
5470 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005471 if (mInputEventConsistencyVerifier != null) {
5472 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5473 }
5474
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005475 if (onFilterTouchEventForSecurity(event)) {
5476 //noinspection SimplifiableIfStatement
5477 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5478 mOnTouchListener.onTouch(this, event)) {
5479 return true;
5480 }
5481
5482 if (onTouchEvent(event)) {
5483 return true;
5484 }
Jeff Brown85a31762010-09-01 17:01:00 -07005485 }
5486
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005487 if (mInputEventConsistencyVerifier != null) {
5488 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005490 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 }
5492
5493 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005494 * Filter the touch event to apply security policies.
5495 *
5496 * @param event The motion event to be filtered.
5497 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005498 *
Jeff Brown85a31762010-09-01 17:01:00 -07005499 * @see #getFilterTouchesWhenObscured
5500 */
5501 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005502 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005503 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5504 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5505 // Window is obscured, drop this touch.
5506 return false;
5507 }
5508 return true;
5509 }
5510
5511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 * Pass a trackball motion event down to the focused view.
5513 *
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 dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005518 if (mInputEventConsistencyVerifier != null) {
5519 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5520 }
5521
Romain Guy02ccac62011-06-24 13:20:23 -07005522 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 }
5524
5525 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005526 * Dispatch a generic motion event.
5527 * <p>
5528 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5529 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005530 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005531 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005532 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005533 *
5534 * @param event The motion event to be dispatched.
5535 * @return True if the event was handled by the view, false otherwise.
5536 */
5537 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005538 if (mInputEventConsistencyVerifier != null) {
5539 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5540 }
5541
Jeff Browna032cc02011-03-07 16:56:21 -08005542 final int source = event.getSource();
5543 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5544 final int action = event.getAction();
5545 if (action == MotionEvent.ACTION_HOVER_ENTER
5546 || action == MotionEvent.ACTION_HOVER_MOVE
5547 || action == MotionEvent.ACTION_HOVER_EXIT) {
5548 if (dispatchHoverEvent(event)) {
5549 return true;
5550 }
5551 } else if (dispatchGenericPointerEvent(event)) {
5552 return true;
5553 }
5554 } else if (dispatchGenericFocusedEvent(event)) {
5555 return true;
5556 }
5557
Jeff Brown10b62902011-06-20 16:40:37 -07005558 if (dispatchGenericMotionEventInternal(event)) {
5559 return true;
5560 }
5561
5562 if (mInputEventConsistencyVerifier != null) {
5563 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5564 }
5565 return false;
5566 }
5567
5568 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005569 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005570 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5571 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5572 return true;
5573 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005574
5575 if (onGenericMotionEvent(event)) {
5576 return true;
5577 }
5578
5579 if (mInputEventConsistencyVerifier != null) {
5580 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5581 }
5582 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005583 }
5584
5585 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005586 * Dispatch a hover event.
5587 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005588 * Do not call this method directly.
5589 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005590 * </p>
5591 *
5592 * @param event The motion event to be dispatched.
5593 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005594 */
5595 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005596 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005597 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5598 && mOnHoverListener.onHover(this, event)) {
5599 return true;
5600 }
5601
Jeff Browna032cc02011-03-07 16:56:21 -08005602 return onHoverEvent(event);
5603 }
5604
5605 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005606 * Returns true if the view has a child to which it has recently sent
5607 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5608 * it does not have a hovered child, then it must be the innermost hovered view.
5609 * @hide
5610 */
5611 protected boolean hasHoveredChild() {
5612 return false;
5613 }
5614
5615 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005616 * Dispatch a generic motion event to the view under the first pointer.
5617 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005618 * Do not call this method directly.
5619 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005620 * </p>
5621 *
5622 * @param event The motion event to be dispatched.
5623 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005624 */
5625 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5626 return false;
5627 }
5628
5629 /**
5630 * Dispatch a generic motion event to the currently focused view.
5631 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005632 * Do not call this method directly.
5633 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005634 * </p>
5635 *
5636 * @param event The motion event to be dispatched.
5637 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005638 */
5639 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5640 return false;
5641 }
5642
5643 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005644 * Dispatch a pointer event.
5645 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005646 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5647 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5648 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005649 * and should not be expected to handle other pointing device features.
5650 * </p>
5651 *
5652 * @param event The motion event to be dispatched.
5653 * @return True if the event was handled by the view, false otherwise.
5654 * @hide
5655 */
5656 public final boolean dispatchPointerEvent(MotionEvent event) {
5657 if (event.isTouchEvent()) {
5658 return dispatchTouchEvent(event);
5659 } else {
5660 return dispatchGenericMotionEvent(event);
5661 }
5662 }
5663
5664 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 * Called when the window containing this view gains or loses window focus.
5666 * ViewGroups should override to route to their children.
5667 *
5668 * @param hasFocus True if the window containing this view now has focus,
5669 * false otherwise.
5670 */
5671 public void dispatchWindowFocusChanged(boolean hasFocus) {
5672 onWindowFocusChanged(hasFocus);
5673 }
5674
5675 /**
5676 * Called when the window containing this view gains or loses focus. Note
5677 * that this is separate from view focus: to receive key events, both
5678 * your view and its window must have focus. If a window is displayed
5679 * on top of yours that takes input focus, then your own window will lose
5680 * focus but the view focus will remain unchanged.
5681 *
5682 * @param hasWindowFocus True if the window containing this view now has
5683 * focus, false otherwise.
5684 */
5685 public void onWindowFocusChanged(boolean hasWindowFocus) {
5686 InputMethodManager imm = InputMethodManager.peekInstance();
5687 if (!hasWindowFocus) {
5688 if (isPressed()) {
5689 setPressed(false);
5690 }
5691 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5692 imm.focusOut(this);
5693 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005694 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005695 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005696 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5698 imm.focusIn(this);
5699 }
5700 refreshDrawableState();
5701 }
5702
5703 /**
5704 * Returns true if this view is in a window that currently has window focus.
5705 * Note that this is not the same as the view itself having focus.
5706 *
5707 * @return True if this view is in a window that currently has window focus.
5708 */
5709 public boolean hasWindowFocus() {
5710 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5711 }
5712
5713 /**
Adam Powell326d8082009-12-09 15:10:07 -08005714 * Dispatch a view visibility change down the view hierarchy.
5715 * ViewGroups should override to route to their children.
5716 * @param changedView The view whose visibility changed. Could be 'this' or
5717 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005718 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5719 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005720 */
5721 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5722 onVisibilityChanged(changedView, visibility);
5723 }
5724
5725 /**
5726 * Called when the visibility of the view or an ancestor of the view is changed.
5727 * @param changedView The view whose visibility changed. Could be 'this' or
5728 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005729 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5730 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005731 */
5732 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005733 if (visibility == VISIBLE) {
5734 if (mAttachInfo != null) {
5735 initialAwakenScrollBars();
5736 } else {
5737 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5738 }
5739 }
Adam Powell326d8082009-12-09 15:10:07 -08005740 }
5741
5742 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005743 * Dispatch a hint about whether this view is displayed. For instance, when
5744 * a View moves out of the screen, it might receives a display hint indicating
5745 * the view is not displayed. Applications should not <em>rely</em> on this hint
5746 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005747 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005748 * @param hint A hint about whether or not this view is displayed:
5749 * {@link #VISIBLE} or {@link #INVISIBLE}.
5750 */
5751 public void dispatchDisplayHint(int hint) {
5752 onDisplayHint(hint);
5753 }
5754
5755 /**
5756 * Gives this view a hint about whether is displayed or not. For instance, when
5757 * a View moves out of the screen, it might receives a display hint indicating
5758 * the view is not displayed. Applications should not <em>rely</em> on this hint
5759 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005760 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005761 * @param hint A hint about whether or not this view is displayed:
5762 * {@link #VISIBLE} or {@link #INVISIBLE}.
5763 */
5764 protected void onDisplayHint(int hint) {
5765 }
5766
5767 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 * Dispatch a window visibility change down the view hierarchy.
5769 * ViewGroups should override to route to their children.
5770 *
5771 * @param visibility The new visibility of the window.
5772 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005773 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 */
5775 public void dispatchWindowVisibilityChanged(int visibility) {
5776 onWindowVisibilityChanged(visibility);
5777 }
5778
5779 /**
5780 * Called when the window containing has change its visibility
5781 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5782 * that this tells you whether or not your window is being made visible
5783 * to the window manager; this does <em>not</em> tell you whether or not
5784 * your window is obscured by other windows on the screen, even if it
5785 * is itself visible.
5786 *
5787 * @param visibility The new visibility of the window.
5788 */
5789 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005790 if (visibility == VISIBLE) {
5791 initialAwakenScrollBars();
5792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 }
5794
5795 /**
5796 * Returns the current visibility of the window this view is attached to
5797 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5798 *
5799 * @return Returns the current visibility of the view's window.
5800 */
5801 public int getWindowVisibility() {
5802 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5803 }
5804
5805 /**
5806 * Retrieve the overall visible display size in which the window this view is
5807 * attached to has been positioned in. This takes into account screen
5808 * decorations above the window, for both cases where the window itself
5809 * is being position inside of them or the window is being placed under
5810 * then and covered insets are used for the window to position its content
5811 * inside. In effect, this tells you the available area where content can
5812 * be placed and remain visible to users.
5813 *
5814 * <p>This function requires an IPC back to the window manager to retrieve
5815 * the requested information, so should not be used in performance critical
5816 * code like drawing.
5817 *
5818 * @param outRect Filled in with the visible display frame. If the view
5819 * is not attached to a window, this is simply the raw display size.
5820 */
5821 public void getWindowVisibleDisplayFrame(Rect outRect) {
5822 if (mAttachInfo != null) {
5823 try {
5824 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5825 } catch (RemoteException e) {
5826 return;
5827 }
5828 // XXX This is really broken, and probably all needs to be done
5829 // in the window manager, and we need to know more about whether
5830 // we want the area behind or in front of the IME.
5831 final Rect insets = mAttachInfo.mVisibleInsets;
5832 outRect.left += insets.left;
5833 outRect.top += insets.top;
5834 outRect.right -= insets.right;
5835 outRect.bottom -= insets.bottom;
5836 return;
5837 }
5838 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005839 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 }
5841
5842 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005843 * Dispatch a notification about a resource configuration change down
5844 * the view hierarchy.
5845 * ViewGroups should override to route to their children.
5846 *
5847 * @param newConfig The new resource configuration.
5848 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005849 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005850 */
5851 public void dispatchConfigurationChanged(Configuration newConfig) {
5852 onConfigurationChanged(newConfig);
5853 }
5854
5855 /**
5856 * Called when the current configuration of the resources being used
5857 * by the application have changed. You can use this to decide when
5858 * to reload resources that can changed based on orientation and other
5859 * configuration characterstics. You only need to use this if you are
5860 * not relying on the normal {@link android.app.Activity} mechanism of
5861 * recreating the activity instance upon a configuration change.
5862 *
5863 * @param newConfig The new resource configuration.
5864 */
5865 protected void onConfigurationChanged(Configuration newConfig) {
5866 }
5867
5868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 * Private function to aggregate all per-view attributes in to the view
5870 * root.
5871 */
5872 void dispatchCollectViewAttributes(int visibility) {
5873 performCollectViewAttributes(visibility);
5874 }
5875
5876 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005877 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005878 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5879 mAttachInfo.mKeepScreenOn = true;
5880 }
5881 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5882 if (mOnSystemUiVisibilityChangeListener != null) {
5883 mAttachInfo.mHasSystemUiListeners = true;
5884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 }
5886 }
5887
5888 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005889 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005891 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5892 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 ai.mRecomputeGlobalAttributes = true;
5894 }
5895 }
5896 }
5897
5898 /**
5899 * Returns whether the device is currently in touch mode. Touch mode is entered
5900 * once the user begins interacting with the device by touch, and affects various
5901 * things like whether focus is always visible to the user.
5902 *
5903 * @return Whether the device is in touch mode.
5904 */
5905 @ViewDebug.ExportedProperty
5906 public boolean isInTouchMode() {
5907 if (mAttachInfo != null) {
5908 return mAttachInfo.mInTouchMode;
5909 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005910 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 }
5912 }
5913
5914 /**
5915 * Returns the context the view is running in, through which it can
5916 * access the current theme, resources, etc.
5917 *
5918 * @return The view's Context.
5919 */
5920 @ViewDebug.CapturedViewProperty
5921 public final Context getContext() {
5922 return mContext;
5923 }
5924
5925 /**
5926 * Handle a key event before it is processed by any input method
5927 * associated with the view hierarchy. This can be used to intercept
5928 * key events in special situations before the IME consumes them; a
5929 * typical example would be handling the BACK key to update the application's
5930 * UI instead of allowing the IME to see it and close itself.
5931 *
5932 * @param keyCode The value in event.getKeyCode().
5933 * @param event Description of the key event.
5934 * @return If you handled the event, return true. If you want to allow the
5935 * event to be handled by the next receiver, return false.
5936 */
5937 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5938 return false;
5939 }
5940
5941 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005942 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5943 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5945 * is released, if the view is enabled and clickable.
5946 *
5947 * @param keyCode A key code that represents the button pressed, from
5948 * {@link android.view.KeyEvent}.
5949 * @param event The KeyEvent object that defines the button action.
5950 */
5951 public boolean onKeyDown(int keyCode, KeyEvent event) {
5952 boolean result = false;
5953
5954 switch (keyCode) {
5955 case KeyEvent.KEYCODE_DPAD_CENTER:
5956 case KeyEvent.KEYCODE_ENTER: {
5957 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5958 return true;
5959 }
5960 // Long clickable items don't necessarily have to be clickable
5961 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5962 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5963 (event.getRepeatCount() == 0)) {
5964 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005965 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 return true;
5967 }
5968 break;
5969 }
5970 }
5971 return result;
5972 }
5973
5974 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005975 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5976 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5977 * the event).
5978 */
5979 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5980 return false;
5981 }
5982
5983 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005984 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5985 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5987 * {@link KeyEvent#KEYCODE_ENTER} is released.
5988 *
5989 * @param keyCode A key code that represents the button pressed, from
5990 * {@link android.view.KeyEvent}.
5991 * @param event The KeyEvent object that defines the button action.
5992 */
5993 public boolean onKeyUp(int keyCode, KeyEvent event) {
5994 boolean result = false;
5995
5996 switch (keyCode) {
5997 case KeyEvent.KEYCODE_DPAD_CENTER:
5998 case KeyEvent.KEYCODE_ENTER: {
5999 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6000 return true;
6001 }
6002 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6003 setPressed(false);
6004
6005 if (!mHasPerformedLongPress) {
6006 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006007 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008
6009 result = performClick();
6010 }
6011 }
6012 break;
6013 }
6014 }
6015 return result;
6016 }
6017
6018 /**
6019 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6020 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6021 * the event).
6022 *
6023 * @param keyCode A key code that represents the button pressed, from
6024 * {@link android.view.KeyEvent}.
6025 * @param repeatCount The number of times the action was made.
6026 * @param event The KeyEvent object that defines the button action.
6027 */
6028 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6029 return false;
6030 }
6031
6032 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006033 * Called on the focused view when a key shortcut event is not handled.
6034 * Override this method to implement local key shortcuts for the View.
6035 * Key shortcuts can also be implemented by setting the
6036 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 *
6038 * @param keyCode The value in event.getKeyCode().
6039 * @param event Description of the key event.
6040 * @return If you handled the event, return true. If you want to allow the
6041 * event to be handled by the next receiver, return false.
6042 */
6043 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6044 return false;
6045 }
6046
6047 /**
6048 * Check whether the called view is a text editor, in which case it
6049 * would make sense to automatically display a soft input window for
6050 * it. Subclasses should override this if they implement
6051 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006052 * a call on that method would return a non-null InputConnection, and
6053 * they are really a first-class editor that the user would normally
6054 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006055 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006056 * <p>The default implementation always returns false. This does
6057 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6058 * will not be called or the user can not otherwise perform edits on your
6059 * view; it is just a hint to the system that this is not the primary
6060 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006061 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 * @return Returns true if this view is a text editor, else false.
6063 */
6064 public boolean onCheckIsTextEditor() {
6065 return false;
6066 }
Romain Guy8506ab42009-06-11 17:35:47 -07006067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 /**
6069 * Create a new InputConnection for an InputMethod to interact
6070 * with the view. The default implementation returns null, since it doesn't
6071 * support input methods. You can override this to implement such support.
6072 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 * <p>When implementing this, you probably also want to implement
6075 * {@link #onCheckIsTextEditor()} to indicate you will return a
6076 * non-null InputConnection.
6077 *
6078 * @param outAttrs Fill in with attribute information about the connection.
6079 */
6080 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6081 return null;
6082 }
6083
6084 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006085 * Called by the {@link android.view.inputmethod.InputMethodManager}
6086 * when a view who is not the current
6087 * input connection target is trying to make a call on the manager. The
6088 * default implementation returns false; you can override this to return
6089 * true for certain views if you are performing InputConnection proxying
6090 * to them.
6091 * @param view The View that is making the InputMethodManager call.
6092 * @return Return true to allow the call, false to reject.
6093 */
6094 public boolean checkInputConnectionProxy(View view) {
6095 return false;
6096 }
Romain Guy8506ab42009-06-11 17:35:47 -07006097
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 * Show the context menu for this view. It is not safe to hold on to the
6100 * menu after returning from this method.
6101 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006102 * You should normally not overload this method. Overload
6103 * {@link #onCreateContextMenu(ContextMenu)} or define an
6104 * {@link OnCreateContextMenuListener} to add items to the context menu.
6105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006106 * @param menu The context menu to populate
6107 */
6108 public void createContextMenu(ContextMenu menu) {
6109 ContextMenuInfo menuInfo = getContextMenuInfo();
6110
6111 // Sets the current menu info so all items added to menu will have
6112 // my extra info set.
6113 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6114
6115 onCreateContextMenu(menu);
6116 if (mOnCreateContextMenuListener != null) {
6117 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6118 }
6119
6120 // Clear the extra information so subsequent items that aren't mine don't
6121 // have my extra info.
6122 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6123
6124 if (mParent != null) {
6125 mParent.createContextMenu(menu);
6126 }
6127 }
6128
6129 /**
6130 * Views should implement this if they have extra information to associate
6131 * with the context menu. The return result is supplied as a parameter to
6132 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6133 * callback.
6134 *
6135 * @return Extra information about the item for which the context menu
6136 * should be shown. This information will vary across different
6137 * subclasses of View.
6138 */
6139 protected ContextMenuInfo getContextMenuInfo() {
6140 return null;
6141 }
6142
6143 /**
6144 * Views should implement this if the view itself is going to add items to
6145 * the context menu.
6146 *
6147 * @param menu the context menu to populate
6148 */
6149 protected void onCreateContextMenu(ContextMenu menu) {
6150 }
6151
6152 /**
6153 * Implement this method to handle trackball motion events. The
6154 * <em>relative</em> movement of the trackball since the last event
6155 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6156 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6157 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6158 * they will often be fractional values, representing the more fine-grained
6159 * movement information available from a trackball).
6160 *
6161 * @param event The motion event.
6162 * @return True if the event was handled, false otherwise.
6163 */
6164 public boolean onTrackballEvent(MotionEvent event) {
6165 return false;
6166 }
6167
6168 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006169 * Implement this method to handle generic motion events.
6170 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006171 * Generic motion events describe joystick movements, mouse hovers, track pad
6172 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006173 * {@link MotionEvent#getSource() source} of the motion event specifies
6174 * the class of input that was received. Implementations of this method
6175 * must examine the bits in the source before processing the event.
6176 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006177 * </p><p>
6178 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6179 * are delivered to the view under the pointer. All other generic motion events are
6180 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006181 * </p>
6182 * <code>
6183 * public boolean onGenericMotionEvent(MotionEvent event) {
6184 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006185 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6186 * // process the joystick movement...
6187 * return true;
6188 * }
6189 * }
6190 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6191 * switch (event.getAction()) {
6192 * case MotionEvent.ACTION_HOVER_MOVE:
6193 * // process the mouse hover movement...
6194 * return true;
6195 * case MotionEvent.ACTION_SCROLL:
6196 * // process the scroll wheel movement...
6197 * return true;
6198 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006199 * }
6200 * return super.onGenericMotionEvent(event);
6201 * }
6202 * </code>
6203 *
6204 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006205 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006206 */
6207 public boolean onGenericMotionEvent(MotionEvent event) {
6208 return false;
6209 }
6210
6211 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006212 * Implement this method to handle hover events.
6213 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006214 * This method is called whenever a pointer is hovering into, over, or out of the
6215 * bounds of a view and the view is not currently being touched.
6216 * Hover events are represented as pointer events with action
6217 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6218 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6219 * </p>
6220 * <ul>
6221 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6222 * when the pointer enters the bounds of the view.</li>
6223 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6224 * when the pointer has already entered the bounds of the view and has moved.</li>
6225 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6226 * when the pointer has exited the bounds of the view or when the pointer is
6227 * about to go down due to a button click, tap, or similar user action that
6228 * causes the view to be touched.</li>
6229 * </ul>
6230 * <p>
6231 * The view should implement this method to return true to indicate that it is
6232 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006233 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006234 * The default implementation calls {@link #setHovered} to update the hovered state
6235 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006236 * is enabled and is clickable. The default implementation also sends hover
6237 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006238 * </p>
6239 *
6240 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006241 * @return True if the view handled the hover event.
6242 *
6243 * @see #isHovered
6244 * @see #setHovered
6245 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006246 */
6247 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006248 // The root view may receive hover (or touch) events that are outside the bounds of
6249 // the window. This code ensures that we only send accessibility events for
6250 // hovers that are actually within the bounds of the root view.
6251 final int action = event.getAction();
6252 if (!mSendingHoverAccessibilityEvents) {
6253 if ((action == MotionEvent.ACTION_HOVER_ENTER
6254 || action == MotionEvent.ACTION_HOVER_MOVE)
6255 && !hasHoveredChild()
6256 && pointInView(event.getX(), event.getY())) {
6257 mSendingHoverAccessibilityEvents = true;
6258 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6259 }
6260 } else {
6261 if (action == MotionEvent.ACTION_HOVER_EXIT
6262 || (action == MotionEvent.ACTION_HOVER_MOVE
6263 && !pointInView(event.getX(), event.getY()))) {
6264 mSendingHoverAccessibilityEvents = false;
6265 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6266 }
Jeff Browna1b24182011-07-28 13:38:24 -07006267 }
6268
Jeff Brown87b7f802011-06-21 18:35:45 -07006269 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006270 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006271 case MotionEvent.ACTION_HOVER_ENTER:
6272 setHovered(true);
6273 break;
6274 case MotionEvent.ACTION_HOVER_EXIT:
6275 setHovered(false);
6276 break;
6277 }
Jeff Browna1b24182011-07-28 13:38:24 -07006278
6279 // Dispatch the event to onGenericMotionEvent before returning true.
6280 // This is to provide compatibility with existing applications that
6281 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6282 // break because of the new default handling for hoverable views
6283 // in onHoverEvent.
6284 // Note that onGenericMotionEvent will be called by default when
6285 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6286 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006287 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006288 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006289 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006290 }
6291
6292 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006293 * Returns true if the view should handle {@link #onHoverEvent}
6294 * by calling {@link #setHovered} to change its hovered state.
6295 *
6296 * @return True if the view is hoverable.
6297 */
6298 private boolean isHoverable() {
6299 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006300 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006301 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6302 return false;
6303 }
6304
6305 return (viewFlags & CLICKABLE) == CLICKABLE
6306 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6307 }
6308
6309 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006310 * Returns true if the view is currently hovered.
6311 *
6312 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006313 *
6314 * @see #setHovered
6315 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006316 */
Jeff Brown10b62902011-06-20 16:40:37 -07006317 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006318 public boolean isHovered() {
6319 return (mPrivateFlags & HOVERED) != 0;
6320 }
6321
6322 /**
6323 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006324 * <p>
6325 * Calling this method also changes the drawable state of the view. This
6326 * enables the view to react to hover by using different drawable resources
6327 * to change its appearance.
6328 * </p><p>
6329 * The {@link #onHoverChanged} method is called when the hovered state changes.
6330 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006331 *
6332 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006333 *
6334 * @see #isHovered
6335 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006336 */
6337 public void setHovered(boolean hovered) {
6338 if (hovered) {
6339 if ((mPrivateFlags & HOVERED) == 0) {
6340 mPrivateFlags |= HOVERED;
6341 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006342 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006343 }
6344 } else {
6345 if ((mPrivateFlags & HOVERED) != 0) {
6346 mPrivateFlags &= ~HOVERED;
6347 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006348 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006349 }
6350 }
6351 }
6352
6353 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006354 * Implement this method to handle hover state changes.
6355 * <p>
6356 * This method is called whenever the hover state changes as a result of a
6357 * call to {@link #setHovered}.
6358 * </p>
6359 *
6360 * @param hovered The current hover state, as returned by {@link #isHovered}.
6361 *
6362 * @see #isHovered
6363 * @see #setHovered
6364 */
6365 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006366 }
6367
6368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 * Implement this method to handle touch screen motion events.
6370 *
6371 * @param event The motion event.
6372 * @return True if the event was handled, false otherwise.
6373 */
6374 public boolean onTouchEvent(MotionEvent event) {
6375 final int viewFlags = mViewFlags;
6376
6377 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006378 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6379 mPrivateFlags &= ~PRESSED;
6380 refreshDrawableState();
6381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006382 // A disabled view that is clickable still consumes the touch
6383 // events, it just doesn't respond to them.
6384 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6385 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6386 }
6387
6388 if (mTouchDelegate != null) {
6389 if (mTouchDelegate.onTouchEvent(event)) {
6390 return true;
6391 }
6392 }
6393
6394 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6395 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6396 switch (event.getAction()) {
6397 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006398 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6399 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 // take focus if we don't have it already and we should in
6401 // touch mode.
6402 boolean focusTaken = false;
6403 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6404 focusTaken = requestFocus();
6405 }
6406
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006407 if (prepressed) {
6408 // The button is being released before we actually
6409 // showed it as pressed. Make it show the pressed
6410 // state now (before scheduling the click) to ensure
6411 // the user sees it.
6412 mPrivateFlags |= PRESSED;
6413 refreshDrawableState();
6414 }
Joe Malin32736f02011-01-19 16:14:20 -08006415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 if (!mHasPerformedLongPress) {
6417 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006418 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419
6420 // Only perform take click actions if we were in the pressed state
6421 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006422 // Use a Runnable and post this rather than calling
6423 // performClick directly. This lets other visual state
6424 // of the view update before click actions start.
6425 if (mPerformClick == null) {
6426 mPerformClick = new PerformClick();
6427 }
6428 if (!post(mPerformClick)) {
6429 performClick();
6430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 }
6432 }
6433
6434 if (mUnsetPressedState == null) {
6435 mUnsetPressedState = new UnsetPressedState();
6436 }
6437
Adam Powelle14579b2009-12-16 18:39:52 -08006438 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006439 postDelayed(mUnsetPressedState,
6440 ViewConfiguration.getPressedStateDuration());
6441 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 // If the post failed, unpress right now
6443 mUnsetPressedState.run();
6444 }
Adam Powelle14579b2009-12-16 18:39:52 -08006445 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 }
6447 break;
6448
6449 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006450 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006451
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006452 if (performButtonActionOnTouchDown(event)) {
6453 break;
6454 }
6455
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006456 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006457 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006458
6459 // For views inside a scrolling container, delay the pressed feedback for
6460 // a short period in case this is a scroll.
6461 if (isInScrollingContainer) {
6462 mPrivateFlags |= PREPRESSED;
6463 if (mPendingCheckForTap == null) {
6464 mPendingCheckForTap = new CheckForTap();
6465 }
6466 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6467 } else {
6468 // Not inside a scrolling container, so show the feedback right away
6469 mPrivateFlags |= PRESSED;
6470 refreshDrawableState();
6471 checkForLongClick(0);
6472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 break;
6474
6475 case MotionEvent.ACTION_CANCEL:
6476 mPrivateFlags &= ~PRESSED;
6477 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006478 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 break;
6480
6481 case MotionEvent.ACTION_MOVE:
6482 final int x = (int) event.getX();
6483 final int y = (int) event.getY();
6484
6485 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006486 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006487 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006488 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006490 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006491 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492
6493 // Need to switch from pressed to not pressed
6494 mPrivateFlags &= ~PRESSED;
6495 refreshDrawableState();
6496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 }
6498 break;
6499 }
6500 return true;
6501 }
6502
6503 return false;
6504 }
6505
6506 /**
Adam Powell10298662011-08-14 18:26:30 -07006507 * @hide
6508 */
6509 public boolean isInScrollingContainer() {
6510 ViewParent p = getParent();
6511 while (p != null && p instanceof ViewGroup) {
6512 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6513 return true;
6514 }
6515 p = p.getParent();
6516 }
6517 return false;
6518 }
6519
6520 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006521 * Remove the longpress detection timer.
6522 */
6523 private void removeLongPressCallback() {
6524 if (mPendingCheckForLongPress != null) {
6525 removeCallbacks(mPendingCheckForLongPress);
6526 }
6527 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006528
6529 /**
6530 * Remove the pending click action
6531 */
6532 private void removePerformClickCallback() {
6533 if (mPerformClick != null) {
6534 removeCallbacks(mPerformClick);
6535 }
6536 }
6537
Adam Powelle14579b2009-12-16 18:39:52 -08006538 /**
Romain Guya440b002010-02-24 15:57:54 -08006539 * Remove the prepress detection timer.
6540 */
6541 private void removeUnsetPressCallback() {
6542 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6543 setPressed(false);
6544 removeCallbacks(mUnsetPressedState);
6545 }
6546 }
6547
6548 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006549 * Remove the tap detection timer.
6550 */
6551 private void removeTapCallback() {
6552 if (mPendingCheckForTap != null) {
6553 mPrivateFlags &= ~PREPRESSED;
6554 removeCallbacks(mPendingCheckForTap);
6555 }
6556 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006557
6558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 * Cancels a pending long press. Your subclass can use this if you
6560 * want the context menu to come up if the user presses and holds
6561 * at the same place, but you don't want it to come up if they press
6562 * and then move around enough to cause scrolling.
6563 */
6564 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006565 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006566
6567 /*
6568 * The prepressed state handled by the tap callback is a display
6569 * construct, but the tap callback will post a long press callback
6570 * less its own timeout. Remove it here.
6571 */
6572 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573 }
6574
6575 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006576 * Remove the pending callback for sending a
6577 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6578 */
6579 private void removeSendViewScrolledAccessibilityEventCallback() {
6580 if (mSendViewScrolledAccessibilityEvent != null) {
6581 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6582 }
6583 }
6584
6585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 * Sets the TouchDelegate for this View.
6587 */
6588 public void setTouchDelegate(TouchDelegate delegate) {
6589 mTouchDelegate = delegate;
6590 }
6591
6592 /**
6593 * Gets the TouchDelegate for this View.
6594 */
6595 public TouchDelegate getTouchDelegate() {
6596 return mTouchDelegate;
6597 }
6598
6599 /**
6600 * Set flags controlling behavior of this view.
6601 *
6602 * @param flags Constant indicating the value which should be set
6603 * @param mask Constant indicating the bit range that should be changed
6604 */
6605 void setFlags(int flags, int mask) {
6606 int old = mViewFlags;
6607 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6608
6609 int changed = mViewFlags ^ old;
6610 if (changed == 0) {
6611 return;
6612 }
6613 int privateFlags = mPrivateFlags;
6614
6615 /* Check if the FOCUSABLE bit has changed */
6616 if (((changed & FOCUSABLE_MASK) != 0) &&
6617 ((privateFlags & HAS_BOUNDS) !=0)) {
6618 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6619 && ((privateFlags & FOCUSED) != 0)) {
6620 /* Give up focus if we are no longer focusable */
6621 clearFocus();
6622 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6623 && ((privateFlags & FOCUSED) == 0)) {
6624 /*
6625 * Tell the view system that we are now available to take focus
6626 * if no one else already has it.
6627 */
6628 if (mParent != null) mParent.focusableViewAvailable(this);
6629 }
6630 }
6631
6632 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6633 if ((changed & VISIBILITY_MASK) != 0) {
6634 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006635 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006636 * it was not visible. Marking it drawn ensures that the invalidation will
6637 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006639 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006640 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641
6642 needGlobalAttributesUpdate(true);
6643
6644 // a view becoming visible is worth notifying the parent
6645 // about in case nothing has focus. even if this specific view
6646 // isn't focusable, it may contain something that is, so let
6647 // the root view try to give this focus if nothing else does.
6648 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6649 mParent.focusableViewAvailable(this);
6650 }
6651 }
6652 }
6653
6654 /* Check if the GONE bit has changed */
6655 if ((changed & GONE) != 0) {
6656 needGlobalAttributesUpdate(false);
6657 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658
Romain Guyecd80ee2009-12-03 17:13:02 -08006659 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6660 if (hasFocus()) clearFocus();
6661 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006662 if (mParent instanceof View) {
6663 // GONE views noop invalidation, so invalidate the parent
6664 ((View) mParent).invalidate(true);
6665 }
6666 // Mark the view drawn to ensure that it gets invalidated properly the next
6667 // time it is visible and gets invalidated
6668 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 }
6670 if (mAttachInfo != null) {
6671 mAttachInfo.mViewVisibilityChanged = true;
6672 }
6673 }
6674
6675 /* Check if the VISIBLE bit has changed */
6676 if ((changed & INVISIBLE) != 0) {
6677 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006678 /*
6679 * If this view is becoming invisible, set the DRAWN flag so that
6680 * the next invalidate() will not be skipped.
6681 */
6682 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683
6684 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6685 // root view becoming invisible shouldn't clear focus
6686 if (getRootView() != this) {
6687 clearFocus();
6688 }
6689 }
6690 if (mAttachInfo != null) {
6691 mAttachInfo.mViewVisibilityChanged = true;
6692 }
6693 }
6694
Adam Powell326d8082009-12-09 15:10:07 -08006695 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006696 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006697 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6698 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006699 } else if (mParent != null) {
6700 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006701 }
Adam Powell326d8082009-12-09 15:10:07 -08006702 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6703 }
6704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6706 destroyDrawingCache();
6707 }
6708
6709 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6710 destroyDrawingCache();
6711 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006712 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 }
6714
6715 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6716 destroyDrawingCache();
6717 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6718 }
6719
6720 if ((changed & DRAW_MASK) != 0) {
6721 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6722 if (mBGDrawable != null) {
6723 mPrivateFlags &= ~SKIP_DRAW;
6724 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6725 } else {
6726 mPrivateFlags |= SKIP_DRAW;
6727 }
6728 } else {
6729 mPrivateFlags &= ~SKIP_DRAW;
6730 }
6731 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006732 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 }
6734
6735 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006736 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 mParent.recomputeViewAttributes(this);
6738 }
6739 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006740
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006741 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006742 requestLayout();
6743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 }
6745
6746 /**
6747 * Change the view's z order in the tree, so it's on top of other sibling
6748 * views
6749 */
6750 public void bringToFront() {
6751 if (mParent != null) {
6752 mParent.bringChildToFront(this);
6753 }
6754 }
6755
6756 /**
6757 * This is called in response to an internal scroll in this view (i.e., the
6758 * view scrolled its own contents). This is typically as a result of
6759 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6760 * called.
6761 *
6762 * @param l Current horizontal scroll origin.
6763 * @param t Current vertical scroll origin.
6764 * @param oldl Previous horizontal scroll origin.
6765 * @param oldt Previous vertical scroll origin.
6766 */
6767 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006768 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6769 postSendViewScrolledAccessibilityEventCallback();
6770 }
6771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 mBackgroundSizeChanged = true;
6773
6774 final AttachInfo ai = mAttachInfo;
6775 if (ai != null) {
6776 ai.mViewScrollChanged = true;
6777 }
6778 }
6779
6780 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006781 * Interface definition for a callback to be invoked when the layout bounds of a view
6782 * changes due to layout processing.
6783 */
6784 public interface OnLayoutChangeListener {
6785 /**
6786 * Called when the focus state of a view has changed.
6787 *
6788 * @param v The view whose state has changed.
6789 * @param left The new value of the view's left property.
6790 * @param top The new value of the view's top property.
6791 * @param right The new value of the view's right property.
6792 * @param bottom The new value of the view's bottom property.
6793 * @param oldLeft The previous value of the view's left property.
6794 * @param oldTop The previous value of the view's top property.
6795 * @param oldRight The previous value of the view's right property.
6796 * @param oldBottom The previous value of the view's bottom property.
6797 */
6798 void onLayoutChange(View v, int left, int top, int right, int bottom,
6799 int oldLeft, int oldTop, int oldRight, int oldBottom);
6800 }
6801
6802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 * This is called during layout when the size of this view has changed. If
6804 * you were just added to the view hierarchy, you're called with the old
6805 * values of 0.
6806 *
6807 * @param w Current width of this view.
6808 * @param h Current height of this view.
6809 * @param oldw Old width of this view.
6810 * @param oldh Old height of this view.
6811 */
6812 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6813 }
6814
6815 /**
6816 * Called by draw to draw the child views. This may be overridden
6817 * by derived classes to gain control just before its children are drawn
6818 * (but after its own view has been drawn).
6819 * @param canvas the canvas on which to draw the view
6820 */
6821 protected void dispatchDraw(Canvas canvas) {
6822 }
6823
6824 /**
6825 * Gets the parent of this view. Note that the parent is a
6826 * ViewParent and not necessarily a View.
6827 *
6828 * @return Parent of this view.
6829 */
6830 public final ViewParent getParent() {
6831 return mParent;
6832 }
6833
6834 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006835 * Set the horizontal scrolled position of your view. This will cause a call to
6836 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6837 * invalidated.
6838 * @param value the x position to scroll to
6839 */
6840 public void setScrollX(int value) {
6841 scrollTo(value, mScrollY);
6842 }
6843
6844 /**
6845 * Set the vertical scrolled position of your view. This will cause a call to
6846 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6847 * invalidated.
6848 * @param value the y position to scroll to
6849 */
6850 public void setScrollY(int value) {
6851 scrollTo(mScrollX, value);
6852 }
6853
6854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 * Return the scrolled left position of this view. This is the left edge of
6856 * the displayed part of your view. You do not need to draw any pixels
6857 * farther left, since those are outside of the frame of your view on
6858 * screen.
6859 *
6860 * @return The left edge of the displayed part of your view, in pixels.
6861 */
6862 public final int getScrollX() {
6863 return mScrollX;
6864 }
6865
6866 /**
6867 * Return the scrolled top position of this view. This is the top edge of
6868 * the displayed part of your view. You do not need to draw any pixels above
6869 * it, since those are outside of the frame of your view on screen.
6870 *
6871 * @return The top edge of the displayed part of your view, in pixels.
6872 */
6873 public final int getScrollY() {
6874 return mScrollY;
6875 }
6876
6877 /**
6878 * Return the width of the your view.
6879 *
6880 * @return The width of your view, in pixels.
6881 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006882 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 public final int getWidth() {
6884 return mRight - mLeft;
6885 }
6886
6887 /**
6888 * Return the height of your view.
6889 *
6890 * @return The height of your view, in pixels.
6891 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006892 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 public final int getHeight() {
6894 return mBottom - mTop;
6895 }
6896
6897 /**
6898 * Return the visible drawing bounds of your view. Fills in the output
6899 * rectangle with the values from getScrollX(), getScrollY(),
6900 * getWidth(), and getHeight().
6901 *
6902 * @param outRect The (scrolled) drawing bounds of the view.
6903 */
6904 public void getDrawingRect(Rect outRect) {
6905 outRect.left = mScrollX;
6906 outRect.top = mScrollY;
6907 outRect.right = mScrollX + (mRight - mLeft);
6908 outRect.bottom = mScrollY + (mBottom - mTop);
6909 }
6910
6911 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006912 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6913 * raw width component (that is the result is masked by
6914 * {@link #MEASURED_SIZE_MASK}).
6915 *
6916 * @return The raw measured width of this view.
6917 */
6918 public final int getMeasuredWidth() {
6919 return mMeasuredWidth & MEASURED_SIZE_MASK;
6920 }
6921
6922 /**
6923 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006924 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006925 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 * This should be used during measurement and layout calculations only. Use
6927 * {@link #getWidth()} to see how wide a view is after layout.
6928 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006929 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006931 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 return mMeasuredWidth;
6933 }
6934
6935 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006936 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6937 * raw width component (that is the result is masked by
6938 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006940 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 */
6942 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006943 return mMeasuredHeight & MEASURED_SIZE_MASK;
6944 }
6945
6946 /**
6947 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006948 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006949 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6950 * This should be used during measurement and layout calculations only. Use
6951 * {@link #getHeight()} to see how wide a view is after layout.
6952 *
6953 * @return The measured width of this view as a bit mask.
6954 */
6955 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 return mMeasuredHeight;
6957 }
6958
6959 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006960 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6961 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6962 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6963 * and the height component is at the shifted bits
6964 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6965 */
6966 public final int getMeasuredState() {
6967 return (mMeasuredWidth&MEASURED_STATE_MASK)
6968 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6969 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6970 }
6971
6972 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006973 * The transform matrix of this view, which is calculated based on the current
6974 * roation, scale, and pivot properties.
6975 *
6976 * @see #getRotation()
6977 * @see #getScaleX()
6978 * @see #getScaleY()
6979 * @see #getPivotX()
6980 * @see #getPivotY()
6981 * @return The current transform matrix for the view
6982 */
6983 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006984 if (mTransformationInfo != null) {
6985 updateMatrix();
6986 return mTransformationInfo.mMatrix;
6987 }
6988 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006989 }
6990
6991 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006992 * Utility function to determine if the value is far enough away from zero to be
6993 * considered non-zero.
6994 * @param value A floating point value to check for zero-ness
6995 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6996 */
6997 private static boolean nonzero(float value) {
6998 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6999 }
7000
7001 /**
Jeff Brown86671742010-09-30 20:00:15 -07007002 * Returns true if the transform matrix is the identity matrix.
7003 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007004 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007005 * @return True if the transform matrix is the identity matrix, false otherwise.
7006 */
Jeff Brown86671742010-09-30 20:00:15 -07007007 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007008 if (mTransformationInfo != null) {
7009 updateMatrix();
7010 return mTransformationInfo.mMatrixIsIdentity;
7011 }
7012 return true;
7013 }
7014
7015 void ensureTransformationInfo() {
7016 if (mTransformationInfo == null) {
7017 mTransformationInfo = new TransformationInfo();
7018 }
Jeff Brown86671742010-09-30 20:00:15 -07007019 }
7020
7021 /**
7022 * Recomputes the transform matrix if necessary.
7023 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007024 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007025 final TransformationInfo info = mTransformationInfo;
7026 if (info == null) {
7027 return;
7028 }
7029 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007030 // transform-related properties have changed since the last time someone
7031 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007032
7033 // Figure out if we need to update the pivot point
7034 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007035 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7036 info.mPrevWidth = mRight - mLeft;
7037 info.mPrevHeight = mBottom - mTop;
7038 info.mPivotX = info.mPrevWidth / 2f;
7039 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007040 }
7041 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007042 info.mMatrix.reset();
7043 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7044 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7045 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7046 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007047 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007048 if (info.mCamera == null) {
7049 info.mCamera = new Camera();
7050 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007051 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007052 info.mCamera.save();
7053 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7054 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7055 info.mCamera.getMatrix(info.matrix3D);
7056 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7057 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7058 info.mPivotY + info.mTranslationY);
7059 info.mMatrix.postConcat(info.matrix3D);
7060 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007061 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007062 info.mMatrixDirty = false;
7063 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7064 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007065 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007066 }
7067
7068 /**
7069 * Utility method to retrieve the inverse of the current mMatrix property.
7070 * We cache the matrix to avoid recalculating it when transform properties
7071 * have not changed.
7072 *
7073 * @return The inverse of the current matrix of this view.
7074 */
Jeff Brown86671742010-09-30 20:00:15 -07007075 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007076 final TransformationInfo info = mTransformationInfo;
7077 if (info != null) {
7078 updateMatrix();
7079 if (info.mInverseMatrixDirty) {
7080 if (info.mInverseMatrix == null) {
7081 info.mInverseMatrix = new Matrix();
7082 }
7083 info.mMatrix.invert(info.mInverseMatrix);
7084 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007085 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007086 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007087 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007088 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007089 }
7090
7091 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007092 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7093 * views are drawn) from the camera to this view. The camera's distance
7094 * affects 3D transformations, for instance rotations around the X and Y
7095 * axis. If the rotationX or rotationY properties are changed and this view is
7096 * large (more than half the size of the screen), it is recommended to always
7097 * use a camera distance that's greater than the height (X axis rotation) or
7098 * the width (Y axis rotation) of this view.</p>
7099 *
7100 * <p>The distance of the camera from the view plane can have an affect on the
7101 * perspective distortion of the view when it is rotated around the x or y axis.
7102 * For example, a large distance will result in a large viewing angle, and there
7103 * will not be much perspective distortion of the view as it rotates. A short
7104 * distance may cause much more perspective distortion upon rotation, and can
7105 * also result in some drawing artifacts if the rotated view ends up partially
7106 * behind the camera (which is why the recommendation is to use a distance at
7107 * least as far as the size of the view, if the view is to be rotated.)</p>
7108 *
7109 * <p>The distance is expressed in "depth pixels." The default distance depends
7110 * on the screen density. For instance, on a medium density display, the
7111 * default distance is 1280. On a high density display, the default distance
7112 * is 1920.</p>
7113 *
7114 * <p>If you want to specify a distance that leads to visually consistent
7115 * results across various densities, use the following formula:</p>
7116 * <pre>
7117 * float scale = context.getResources().getDisplayMetrics().density;
7118 * view.setCameraDistance(distance * scale);
7119 * </pre>
7120 *
7121 * <p>The density scale factor of a high density display is 1.5,
7122 * and 1920 = 1280 * 1.5.</p>
7123 *
7124 * @param distance The distance in "depth pixels", if negative the opposite
7125 * value is used
7126 *
7127 * @see #setRotationX(float)
7128 * @see #setRotationY(float)
7129 */
7130 public void setCameraDistance(float distance) {
7131 invalidateParentCaches();
7132 invalidate(false);
7133
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007134 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007135 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007136 final TransformationInfo info = mTransformationInfo;
7137 if (info.mCamera == null) {
7138 info.mCamera = new Camera();
7139 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007140 }
7141
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007142 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7143 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007144
7145 invalidate(false);
7146 }
7147
7148 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 * The degrees that the view is rotated around the pivot point.
7150 *
Romain Guya5364ee2011-02-24 14:46:04 -08007151 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 * @see #getPivotX()
7153 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007154 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007155 * @return The degrees of rotation.
7156 */
7157 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007158 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007159 }
7160
7161 /**
Chet Haase897247b2010-09-09 14:54:47 -07007162 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7163 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007164 *
7165 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007166 *
7167 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007168 * @see #getPivotX()
7169 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007170 * @see #setRotationX(float)
7171 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007172 *
7173 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007174 */
7175 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007176 ensureTransformationInfo();
7177 final TransformationInfo info = mTransformationInfo;
7178 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007179 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007180 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007181 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007182 info.mRotation = rotation;
7183 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007184 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007185 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007186 }
7187 }
7188
7189 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007190 * The degrees that the view is rotated around the vertical axis through the pivot point.
7191 *
7192 * @see #getPivotX()
7193 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007194 * @see #setRotationY(float)
7195 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007196 * @return The degrees of Y rotation.
7197 */
7198 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007199 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007200 }
7201
7202 /**
Chet Haase897247b2010-09-09 14:54:47 -07007203 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7204 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7205 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007206 *
7207 * When rotating large views, it is recommended to adjust the camera distance
7208 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007209 *
7210 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007211 *
7212 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007213 * @see #getPivotX()
7214 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007215 * @see #setRotation(float)
7216 * @see #setRotationX(float)
7217 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007218 *
7219 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007220 */
7221 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007222 ensureTransformationInfo();
7223 final TransformationInfo info = mTransformationInfo;
7224 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007225 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007226 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007227 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007228 info.mRotationY = rotationY;
7229 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007230 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007231 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007232 }
7233 }
7234
7235 /**
7236 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7237 *
7238 * @see #getPivotX()
7239 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007240 * @see #setRotationX(float)
7241 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007242 * @return The degrees of X rotation.
7243 */
7244 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007245 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007246 }
7247
7248 /**
Chet Haase897247b2010-09-09 14:54:47 -07007249 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7250 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7251 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007252 *
7253 * When rotating large views, it is recommended to adjust the camera distance
7254 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007255 *
7256 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007257 *
7258 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007259 * @see #getPivotX()
7260 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007261 * @see #setRotation(float)
7262 * @see #setRotationY(float)
7263 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007264 *
7265 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007266 */
7267 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007268 ensureTransformationInfo();
7269 final TransformationInfo info = mTransformationInfo;
7270 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007271 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007272 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007273 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007274 info.mRotationX = rotationX;
7275 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007276 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007277 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007278 }
7279 }
7280
7281 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007282 * The amount that the view is scaled in x around the pivot point, as a proportion of
7283 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7284 *
Joe Onorato93162322010-09-16 15:42:01 -04007285 * <p>By default, this is 1.0f.
7286 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007287 * @see #getPivotX()
7288 * @see #getPivotY()
7289 * @return The scaling factor.
7290 */
7291 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007292 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007293 }
7294
7295 /**
7296 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7297 * the view's unscaled width. A value of 1 means that no scaling is applied.
7298 *
7299 * @param scaleX The scaling factor.
7300 * @see #getPivotX()
7301 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007302 *
7303 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007304 */
7305 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007306 ensureTransformationInfo();
7307 final TransformationInfo info = mTransformationInfo;
7308 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007309 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007310 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007311 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007312 info.mScaleX = scaleX;
7313 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007314 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007315 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007316 }
7317 }
7318
7319 /**
7320 * The amount that the view is scaled in y around the pivot point, as a proportion of
7321 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7322 *
Joe Onorato93162322010-09-16 15:42:01 -04007323 * <p>By default, this is 1.0f.
7324 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007325 * @see #getPivotX()
7326 * @see #getPivotY()
7327 * @return The scaling factor.
7328 */
7329 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007330 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007331 }
7332
7333 /**
7334 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7335 * the view's unscaled width. A value of 1 means that no scaling is applied.
7336 *
7337 * @param scaleY The scaling factor.
7338 * @see #getPivotX()
7339 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007340 *
7341 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 */
7343 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007344 ensureTransformationInfo();
7345 final TransformationInfo info = mTransformationInfo;
7346 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007347 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007348 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007349 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007350 info.mScaleY = scaleY;
7351 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007352 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007353 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007354 }
7355 }
7356
7357 /**
7358 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7359 * and {@link #setScaleX(float) scaled}.
7360 *
7361 * @see #getRotation()
7362 * @see #getScaleX()
7363 * @see #getScaleY()
7364 * @see #getPivotY()
7365 * @return The x location of the pivot point.
7366 */
7367 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007368 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007369 }
7370
7371 /**
7372 * Sets the x location of the point around which the view is
7373 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007374 * By default, the pivot point is centered on the object.
7375 * Setting this property disables this behavior and causes the view to use only the
7376 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007377 *
7378 * @param pivotX The x location of the pivot point.
7379 * @see #getRotation()
7380 * @see #getScaleX()
7381 * @see #getScaleY()
7382 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007383 *
7384 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007385 */
7386 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007387 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007388 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007389 final TransformationInfo info = mTransformationInfo;
7390 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007391 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007392 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007393 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007394 info.mPivotX = pivotX;
7395 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007396 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007397 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007398 }
7399 }
7400
7401 /**
7402 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7403 * and {@link #setScaleY(float) scaled}.
7404 *
7405 * @see #getRotation()
7406 * @see #getScaleX()
7407 * @see #getScaleY()
7408 * @see #getPivotY()
7409 * @return The y location of the pivot point.
7410 */
7411 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007412 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007413 }
7414
7415 /**
7416 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007417 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7418 * Setting this property disables this behavior and causes the view to use only the
7419 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007420 *
7421 * @param pivotY The y location of the pivot point.
7422 * @see #getRotation()
7423 * @see #getScaleX()
7424 * @see #getScaleY()
7425 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007426 *
7427 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007428 */
7429 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007430 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007431 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007432 final TransformationInfo info = mTransformationInfo;
7433 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007434 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007435 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007436 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007437 info.mPivotY = pivotY;
7438 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007439 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007440 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007441 }
7442 }
7443
7444 /**
7445 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7446 * completely transparent and 1 means the view is completely opaque.
7447 *
Joe Onorato93162322010-09-16 15:42:01 -04007448 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007449 * @return The opacity of the view.
7450 */
7451 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007452 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007453 }
7454
7455 /**
Romain Guy171c5922011-01-06 10:04:23 -08007456 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7457 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007458 *
Romain Guy171c5922011-01-06 10:04:23 -08007459 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7460 * responsible for applying the opacity itself. Otherwise, calling this method is
7461 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007462 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007463 *
7464 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007465 *
Joe Malin32736f02011-01-19 16:14:20 -08007466 * @see #setLayerType(int, android.graphics.Paint)
7467 *
Chet Haase73066682010-11-29 15:55:32 -08007468 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007469 */
7470 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007471 ensureTransformationInfo();
7472 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007473 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007474 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007475 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007476 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007477 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007478 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007479 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007480 invalidate(false);
7481 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 }
7483
7484 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007485 * Faster version of setAlpha() which performs the same steps except there are
7486 * no calls to invalidate(). The caller of this function should perform proper invalidation
7487 * on the parent and this object. The return value indicates whether the subclass handles
7488 * alpha (the return value for onSetAlpha()).
7489 *
7490 * @param alpha The new value for the alpha property
7491 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7492 */
7493 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007494 ensureTransformationInfo();
7495 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007496 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7497 if (subclassHandlesAlpha) {
7498 mPrivateFlags |= ALPHA_SET;
7499 } else {
7500 mPrivateFlags &= ~ALPHA_SET;
7501 }
7502 return subclassHandlesAlpha;
7503 }
7504
7505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 * Top position of this view relative to its parent.
7507 *
7508 * @return The top of this view, in pixels.
7509 */
7510 @ViewDebug.CapturedViewProperty
7511 public final int getTop() {
7512 return mTop;
7513 }
7514
7515 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007516 * Sets the top position of this view relative to its parent. This method is meant to be called
7517 * by the layout system and should not generally be called otherwise, because the property
7518 * may be changed at any time by the layout.
7519 *
7520 * @param top The top of this view, in pixels.
7521 */
7522 public final void setTop(int top) {
7523 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007524 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007525 final boolean matrixIsIdentity = mTransformationInfo == null
7526 || mTransformationInfo.mMatrixIsIdentity;
7527 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007528 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007529 int minTop;
7530 int yLoc;
7531 if (top < mTop) {
7532 minTop = top;
7533 yLoc = top - mTop;
7534 } else {
7535 minTop = mTop;
7536 yLoc = 0;
7537 }
Chet Haasee9140a72011-02-16 16:23:29 -08007538 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007539 }
7540 } else {
7541 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007542 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007543 }
7544
Chet Haaseed032702010-10-01 14:05:54 -07007545 int width = mRight - mLeft;
7546 int oldHeight = mBottom - mTop;
7547
Chet Haase21cd1382010-09-01 17:42:29 -07007548 mTop = top;
7549
Chet Haaseed032702010-10-01 14:05:54 -07007550 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7551
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007552 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007553 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7554 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007555 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007556 }
Chet Haase21cd1382010-09-01 17:42:29 -07007557 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007558 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007559 }
Chet Haase55dbb652010-12-21 20:15:08 -08007560 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007561 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007562 }
7563 }
7564
7565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 * Bottom position of this view relative to its parent.
7567 *
7568 * @return The bottom of this view, in pixels.
7569 */
7570 @ViewDebug.CapturedViewProperty
7571 public final int getBottom() {
7572 return mBottom;
7573 }
7574
7575 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007576 * True if this view has changed since the last time being drawn.
7577 *
7578 * @return The dirty state of this view.
7579 */
7580 public boolean isDirty() {
7581 return (mPrivateFlags & DIRTY_MASK) != 0;
7582 }
7583
7584 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007585 * Sets the bottom position of this view relative to its parent. This method is meant to be
7586 * called by the layout system and should not generally be called otherwise, because the
7587 * property may be changed at any time by the layout.
7588 *
7589 * @param bottom The bottom of this view, in pixels.
7590 */
7591 public final void setBottom(int bottom) {
7592 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007593 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007594 final boolean matrixIsIdentity = mTransformationInfo == null
7595 || mTransformationInfo.mMatrixIsIdentity;
7596 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007597 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007598 int maxBottom;
7599 if (bottom < mBottom) {
7600 maxBottom = mBottom;
7601 } else {
7602 maxBottom = bottom;
7603 }
Chet Haasee9140a72011-02-16 16:23:29 -08007604 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007605 }
7606 } else {
7607 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007608 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007609 }
7610
Chet Haaseed032702010-10-01 14:05:54 -07007611 int width = mRight - mLeft;
7612 int oldHeight = mBottom - mTop;
7613
Chet Haase21cd1382010-09-01 17:42:29 -07007614 mBottom = bottom;
7615
Chet Haaseed032702010-10-01 14:05:54 -07007616 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7617
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007618 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007619 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7620 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007621 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007622 }
Chet Haase21cd1382010-09-01 17:42:29 -07007623 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007624 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007625 }
Chet Haase55dbb652010-12-21 20:15:08 -08007626 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007627 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007628 }
7629 }
7630
7631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 * Left position of this view relative to its parent.
7633 *
7634 * @return The left edge of this view, in pixels.
7635 */
7636 @ViewDebug.CapturedViewProperty
7637 public final int getLeft() {
7638 return mLeft;
7639 }
7640
7641 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007642 * Sets the left position of this view relative to its parent. This method is meant to be called
7643 * by the layout system and should not generally be called otherwise, because the property
7644 * may be changed at any time by the layout.
7645 *
7646 * @param left The bottom of this view, in pixels.
7647 */
7648 public final void setLeft(int left) {
7649 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007650 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007651 final boolean matrixIsIdentity = mTransformationInfo == null
7652 || mTransformationInfo.mMatrixIsIdentity;
7653 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007654 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007655 int minLeft;
7656 int xLoc;
7657 if (left < mLeft) {
7658 minLeft = left;
7659 xLoc = left - mLeft;
7660 } else {
7661 minLeft = mLeft;
7662 xLoc = 0;
7663 }
Chet Haasee9140a72011-02-16 16:23:29 -08007664 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007665 }
7666 } else {
7667 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007668 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007669 }
7670
Chet Haaseed032702010-10-01 14:05:54 -07007671 int oldWidth = mRight - mLeft;
7672 int height = mBottom - mTop;
7673
Chet Haase21cd1382010-09-01 17:42:29 -07007674 mLeft = left;
7675
Chet Haaseed032702010-10-01 14:05:54 -07007676 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7677
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007678 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007679 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7680 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007681 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007682 }
Chet Haase21cd1382010-09-01 17:42:29 -07007683 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007684 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007685 }
Chet Haase55dbb652010-12-21 20:15:08 -08007686 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007687 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007688 }
7689 }
7690
7691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 * Right position of this view relative to its parent.
7693 *
7694 * @return The right edge of this view, in pixels.
7695 */
7696 @ViewDebug.CapturedViewProperty
7697 public final int getRight() {
7698 return mRight;
7699 }
7700
7701 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007702 * Sets the right position of this view relative to its parent. This method is meant to be called
7703 * by the layout system and should not generally be called otherwise, because the property
7704 * may be changed at any time by the layout.
7705 *
7706 * @param right The bottom of this view, in pixels.
7707 */
7708 public final void setRight(int right) {
7709 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007710 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007711 final boolean matrixIsIdentity = mTransformationInfo == null
7712 || mTransformationInfo.mMatrixIsIdentity;
7713 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007714 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007715 int maxRight;
7716 if (right < mRight) {
7717 maxRight = mRight;
7718 } else {
7719 maxRight = right;
7720 }
Chet Haasee9140a72011-02-16 16:23:29 -08007721 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007722 }
7723 } else {
7724 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007725 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007726 }
7727
Chet Haaseed032702010-10-01 14:05:54 -07007728 int oldWidth = mRight - mLeft;
7729 int height = mBottom - mTop;
7730
Chet Haase21cd1382010-09-01 17:42:29 -07007731 mRight = right;
7732
Chet Haaseed032702010-10-01 14:05:54 -07007733 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7734
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007735 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007736 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7737 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007738 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007739 }
Chet Haase21cd1382010-09-01 17:42:29 -07007740 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007741 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007742 }
Chet Haase55dbb652010-12-21 20:15:08 -08007743 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007744 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007745 }
7746 }
7747
7748 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007749 * The visual x position of this view, in pixels. This is equivalent to the
7750 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007751 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007752 *
Chet Haasedf030d22010-07-30 17:22:38 -07007753 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007754 */
Chet Haasedf030d22010-07-30 17:22:38 -07007755 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007756 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007757 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007758
Chet Haasedf030d22010-07-30 17:22:38 -07007759 /**
7760 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7761 * {@link #setTranslationX(float) translationX} property to be the difference between
7762 * the x value passed in and the current {@link #getLeft() left} property.
7763 *
7764 * @param x The visual x position of this view, in pixels.
7765 */
7766 public void setX(float x) {
7767 setTranslationX(x - mLeft);
7768 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007769
Chet Haasedf030d22010-07-30 17:22:38 -07007770 /**
7771 * The visual y position of this view, in pixels. This is equivalent to the
7772 * {@link #setTranslationY(float) translationY} property plus the current
7773 * {@link #getTop() top} property.
7774 *
7775 * @return The visual y position of this view, in pixels.
7776 */
7777 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007778 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007779 }
7780
7781 /**
7782 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7783 * {@link #setTranslationY(float) translationY} property to be the difference between
7784 * the y value passed in and the current {@link #getTop() top} property.
7785 *
7786 * @param y The visual y position of this view, in pixels.
7787 */
7788 public void setY(float y) {
7789 setTranslationY(y - mTop);
7790 }
7791
7792
7793 /**
7794 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7795 * This position is post-layout, in addition to wherever the object's
7796 * layout placed it.
7797 *
7798 * @return The horizontal position of this view relative to its left position, in pixels.
7799 */
7800 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007801 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007802 }
7803
7804 /**
7805 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7806 * This effectively positions the object post-layout, in addition to wherever the object's
7807 * layout placed it.
7808 *
7809 * @param translationX The horizontal position of this view relative to its left position,
7810 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007811 *
7812 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007813 */
7814 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007815 ensureTransformationInfo();
7816 final TransformationInfo info = mTransformationInfo;
7817 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007818 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007819 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007820 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007821 info.mTranslationX = translationX;
7822 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007823 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007824 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007825 }
7826 }
7827
7828 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007829 * The horizontal location of this view relative to its {@link #getTop() top} position.
7830 * This position is post-layout, in addition to wherever the object's
7831 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007832 *
Chet Haasedf030d22010-07-30 17:22:38 -07007833 * @return The vertical position of this view relative to its top position,
7834 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007835 */
Chet Haasedf030d22010-07-30 17:22:38 -07007836 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007837 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007838 }
7839
7840 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007841 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7842 * This effectively positions the object post-layout, in addition to wherever the object's
7843 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007844 *
Chet Haasedf030d22010-07-30 17:22:38 -07007845 * @param translationY The vertical position of this view relative to its top position,
7846 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007847 *
7848 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007849 */
Chet Haasedf030d22010-07-30 17:22:38 -07007850 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007851 ensureTransformationInfo();
7852 final TransformationInfo info = mTransformationInfo;
7853 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007854 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007855 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007856 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007857 info.mTranslationY = translationY;
7858 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007859 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007860 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007861 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007862 }
7863
7864 /**
Romain Guyda489792011-02-03 01:05:15 -08007865 * @hide
7866 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007867 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007868 ensureTransformationInfo();
7869 final TransformationInfo info = mTransformationInfo;
7870 info.mTranslationX = x;
7871 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007872 }
7873
7874 /**
7875 * @hide
7876 */
7877 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007878 ensureTransformationInfo();
7879 final TransformationInfo info = mTransformationInfo;
7880 info.mTranslationY = y;
7881 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007882 }
7883
7884 /**
7885 * @hide
7886 */
Romain Guyda489792011-02-03 01:05:15 -08007887 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007888 ensureTransformationInfo();
7889 final TransformationInfo info = mTransformationInfo;
7890 info.mTranslationX = x - mLeft;
7891 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007892 }
7893
7894 /**
7895 * @hide
7896 */
7897 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007898 ensureTransformationInfo();
7899 final TransformationInfo info = mTransformationInfo;
7900 info.mTranslationY = y - mTop;
7901 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007902 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007903
Romain Guyda489792011-02-03 01:05:15 -08007904 /**
7905 * @hide
7906 */
7907 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007908 ensureTransformationInfo();
7909 final TransformationInfo info = mTransformationInfo;
7910 info.mScaleX = x;
7911 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007912 }
7913
7914 /**
7915 * @hide
7916 */
7917 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007918 ensureTransformationInfo();
7919 final TransformationInfo info = mTransformationInfo;
7920 info.mScaleY = y;
7921 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007922 }
7923
7924 /**
7925 * @hide
7926 */
7927 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007928 ensureTransformationInfo();
7929 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007930 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007931
Romain Guyda489792011-02-03 01:05:15 -08007932 /**
7933 * @hide
7934 */
7935 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007936 ensureTransformationInfo();
7937 final TransformationInfo info = mTransformationInfo;
7938 info.mRotationY = y;
7939 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007940 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007941
Romain Guyda489792011-02-03 01:05:15 -08007942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 * Hit rectangle in parent's coordinates
7944 *
7945 * @param outRect The hit rectangle of the view.
7946 */
7947 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007948 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007949 final TransformationInfo info = mTransformationInfo;
7950 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007951 outRect.set(mLeft, mTop, mRight, mBottom);
7952 } else {
7953 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007954 tmpRect.set(-info.mPivotX, -info.mPivotY,
7955 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7956 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007957 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7958 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007959 }
7960 }
7961
7962 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007963 * Determines whether the given point, in local coordinates is inside the view.
7964 */
7965 /*package*/ final boolean pointInView(float localX, float localY) {
7966 return localX >= 0 && localX < (mRight - mLeft)
7967 && localY >= 0 && localY < (mBottom - mTop);
7968 }
7969
7970 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007971 * Utility method to determine whether the given point, in local coordinates,
7972 * is inside the view, where the area of the view is expanded by the slop factor.
7973 * This method is called while processing touch-move events to determine if the event
7974 * is still within the view.
7975 */
7976 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007977 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007978 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007979 }
7980
7981 /**
7982 * When a view has focus and the user navigates away from it, the next view is searched for
7983 * starting from the rectangle filled in by this method.
7984 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007985 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7986 * of the view. However, if your view maintains some idea of internal selection,
7987 * such as a cursor, or a selected row or column, you should override this method and
7988 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007989 *
7990 * @param r The rectangle to fill in, in this view's coordinates.
7991 */
7992 public void getFocusedRect(Rect r) {
7993 getDrawingRect(r);
7994 }
7995
7996 /**
7997 * If some part of this view is not clipped by any of its parents, then
7998 * return that area in r in global (root) coordinates. To convert r to local
7999 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
8000 * -globalOffset.y)) If the view is completely clipped or translated out,
8001 * return false.
8002 *
8003 * @param r If true is returned, r holds the global coordinates of the
8004 * visible portion of this view.
8005 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8006 * between this view and its root. globalOffet may be null.
8007 * @return true if r is non-empty (i.e. part of the view is visible at the
8008 * root level.
8009 */
8010 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8011 int width = mRight - mLeft;
8012 int height = mBottom - mTop;
8013 if (width > 0 && height > 0) {
8014 r.set(0, 0, width, height);
8015 if (globalOffset != null) {
8016 globalOffset.set(-mScrollX, -mScrollY);
8017 }
8018 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8019 }
8020 return false;
8021 }
8022
8023 public final boolean getGlobalVisibleRect(Rect r) {
8024 return getGlobalVisibleRect(r, null);
8025 }
8026
8027 public final boolean getLocalVisibleRect(Rect r) {
8028 Point offset = new Point();
8029 if (getGlobalVisibleRect(r, offset)) {
8030 r.offset(-offset.x, -offset.y); // make r local
8031 return true;
8032 }
8033 return false;
8034 }
8035
8036 /**
8037 * Offset this view's vertical location by the specified number of pixels.
8038 *
8039 * @param offset the number of pixels to offset the view by
8040 */
8041 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008042 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008043 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008044 final boolean matrixIsIdentity = mTransformationInfo == null
8045 || mTransformationInfo.mMatrixIsIdentity;
8046 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008047 final ViewParent p = mParent;
8048 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008049 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008050 int minTop;
8051 int maxBottom;
8052 int yLoc;
8053 if (offset < 0) {
8054 minTop = mTop + offset;
8055 maxBottom = mBottom;
8056 yLoc = offset;
8057 } else {
8058 minTop = mTop;
8059 maxBottom = mBottom + offset;
8060 yLoc = 0;
8061 }
8062 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8063 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008064 }
8065 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008066 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008067 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008068
Chet Haasec3aa3612010-06-17 08:50:37 -07008069 mTop += offset;
8070 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008071
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008072 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008073 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008074 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008075 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008076 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 }
8079
8080 /**
8081 * Offset this view's horizontal location by the specified amount of pixels.
8082 *
8083 * @param offset the numer of pixels to offset the view by
8084 */
8085 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008086 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008087 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008088 final boolean matrixIsIdentity = mTransformationInfo == null
8089 || mTransformationInfo.mMatrixIsIdentity;
8090 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008091 final ViewParent p = mParent;
8092 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008093 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008094 int minLeft;
8095 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008096 if (offset < 0) {
8097 minLeft = mLeft + offset;
8098 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008099 } else {
8100 minLeft = mLeft;
8101 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008102 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008103 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008104 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008105 }
8106 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008107 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008108 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008109
Chet Haasec3aa3612010-06-17 08:50:37 -07008110 mLeft += offset;
8111 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008112
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008113 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008114 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008115 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008116 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008117 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 }
8120
8121 /**
8122 * Get the LayoutParams associated with this view. All views should have
8123 * layout parameters. These supply parameters to the <i>parent</i> of this
8124 * view specifying how it should be arranged. There are many subclasses of
8125 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8126 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008127 *
8128 * This method may return null if this View is not attached to a parent
8129 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8130 * was not invoked successfully. When a View is attached to a parent
8131 * ViewGroup, this method must not return null.
8132 *
8133 * @return The LayoutParams associated with this view, or null if no
8134 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008136 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 public ViewGroup.LayoutParams getLayoutParams() {
8138 return mLayoutParams;
8139 }
8140
8141 /**
8142 * Set the layout parameters associated with this view. These supply
8143 * parameters to the <i>parent</i> of this view specifying how it should be
8144 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8145 * correspond to the different subclasses of ViewGroup that are responsible
8146 * for arranging their children.
8147 *
Romain Guy01c174b2011-02-22 11:51:06 -08008148 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 */
8150 public void setLayoutParams(ViewGroup.LayoutParams params) {
8151 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008152 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153 }
8154 mLayoutParams = params;
8155 requestLayout();
8156 }
8157
8158 /**
8159 * Set the scrolled position of your view. This will cause a call to
8160 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8161 * invalidated.
8162 * @param x the x position to scroll to
8163 * @param y the y position to scroll to
8164 */
8165 public void scrollTo(int x, int y) {
8166 if (mScrollX != x || mScrollY != y) {
8167 int oldX = mScrollX;
8168 int oldY = mScrollY;
8169 mScrollX = x;
8170 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008171 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008172 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008173 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008174 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 }
8177 }
8178
8179 /**
8180 * Move the scrolled position of your view. This will cause a call to
8181 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8182 * invalidated.
8183 * @param x the amount of pixels to scroll by horizontally
8184 * @param y the amount of pixels to scroll by vertically
8185 */
8186 public void scrollBy(int x, int y) {
8187 scrollTo(mScrollX + x, mScrollY + y);
8188 }
8189
8190 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008191 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8192 * animation to fade the scrollbars out after a default delay. If a subclass
8193 * provides animated scrolling, the start delay should equal the duration
8194 * of the scrolling animation.</p>
8195 *
8196 * <p>The animation starts only if at least one of the scrollbars is
8197 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8198 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8199 * this method returns true, and false otherwise. If the animation is
8200 * started, this method calls {@link #invalidate()}; in that case the
8201 * caller should not call {@link #invalidate()}.</p>
8202 *
8203 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008204 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008205 *
8206 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8207 * and {@link #scrollTo(int, int)}.</p>
8208 *
8209 * @return true if the animation is played, false otherwise
8210 *
8211 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008212 * @see #scrollBy(int, int)
8213 * @see #scrollTo(int, int)
8214 * @see #isHorizontalScrollBarEnabled()
8215 * @see #isVerticalScrollBarEnabled()
8216 * @see #setHorizontalScrollBarEnabled(boolean)
8217 * @see #setVerticalScrollBarEnabled(boolean)
8218 */
8219 protected boolean awakenScrollBars() {
8220 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008221 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008222 }
8223
8224 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008225 * Trigger the scrollbars to draw.
8226 * This method differs from awakenScrollBars() only in its default duration.
8227 * initialAwakenScrollBars() will show the scroll bars for longer than
8228 * usual to give the user more of a chance to notice them.
8229 *
8230 * @return true if the animation is played, false otherwise.
8231 */
8232 private boolean initialAwakenScrollBars() {
8233 return mScrollCache != null &&
8234 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8235 }
8236
8237 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008238 * <p>
8239 * Trigger the scrollbars to draw. When invoked this method starts an
8240 * animation to fade the scrollbars out after a fixed delay. If a subclass
8241 * provides animated scrolling, the start delay should equal the duration of
8242 * the scrolling animation.
8243 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008244 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008245 * <p>
8246 * The animation starts only if at least one of the scrollbars is enabled,
8247 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8248 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8249 * this method returns true, and false otherwise. If the animation is
8250 * started, this method calls {@link #invalidate()}; in that case the caller
8251 * should not call {@link #invalidate()}.
8252 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008253 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008254 * <p>
8255 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008256 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008257 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008258 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008259 * @param startDelay the delay, in milliseconds, after which the animation
8260 * should start; when the delay is 0, the animation starts
8261 * immediately
8262 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008263 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008264 * @see #scrollBy(int, int)
8265 * @see #scrollTo(int, int)
8266 * @see #isHorizontalScrollBarEnabled()
8267 * @see #isVerticalScrollBarEnabled()
8268 * @see #setHorizontalScrollBarEnabled(boolean)
8269 * @see #setVerticalScrollBarEnabled(boolean)
8270 */
8271 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008272 return awakenScrollBars(startDelay, true);
8273 }
Joe Malin32736f02011-01-19 16:14:20 -08008274
Mike Cleron290947b2009-09-29 18:34:32 -07008275 /**
8276 * <p>
8277 * Trigger the scrollbars to draw. When invoked this method starts an
8278 * animation to fade the scrollbars out after a fixed delay. If a subclass
8279 * provides animated scrolling, the start delay should equal the duration of
8280 * the scrolling animation.
8281 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008282 *
Mike Cleron290947b2009-09-29 18:34:32 -07008283 * <p>
8284 * The animation starts only if at least one of the scrollbars is enabled,
8285 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8286 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8287 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008288 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008289 * is set to true; in that case the caller
8290 * should not call {@link #invalidate()}.
8291 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008292 *
Mike Cleron290947b2009-09-29 18:34:32 -07008293 * <p>
8294 * This method should be invoked everytime a subclass directly updates the
8295 * scroll parameters.
8296 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008297 *
Mike Cleron290947b2009-09-29 18:34:32 -07008298 * @param startDelay the delay, in milliseconds, after which the animation
8299 * should start; when the delay is 0, the animation starts
8300 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008301 *
Mike Cleron290947b2009-09-29 18:34:32 -07008302 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008303 *
Mike Cleron290947b2009-09-29 18:34:32 -07008304 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008305 *
Mike Cleron290947b2009-09-29 18:34:32 -07008306 * @see #scrollBy(int, int)
8307 * @see #scrollTo(int, int)
8308 * @see #isHorizontalScrollBarEnabled()
8309 * @see #isVerticalScrollBarEnabled()
8310 * @see #setHorizontalScrollBarEnabled(boolean)
8311 * @see #setVerticalScrollBarEnabled(boolean)
8312 */
8313 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008314 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008315
Mike Cleronf116bf82009-09-27 19:14:12 -07008316 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8317 return false;
8318 }
8319
8320 if (scrollCache.scrollBar == null) {
8321 scrollCache.scrollBar = new ScrollBarDrawable();
8322 }
8323
8324 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8325
Mike Cleron290947b2009-09-29 18:34:32 -07008326 if (invalidate) {
8327 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008328 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008329 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008330
8331 if (scrollCache.state == ScrollabilityCache.OFF) {
8332 // FIXME: this is copied from WindowManagerService.
8333 // We should get this value from the system when it
8334 // is possible to do so.
8335 final int KEY_REPEAT_FIRST_DELAY = 750;
8336 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8337 }
8338
8339 // Tell mScrollCache when we should start fading. This may
8340 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008341 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008342 scrollCache.fadeStartTime = fadeStartTime;
8343 scrollCache.state = ScrollabilityCache.ON;
8344
8345 // Schedule our fader to run, unscheduling any old ones first
8346 if (mAttachInfo != null) {
8347 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8348 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8349 }
8350
8351 return true;
8352 }
8353
8354 return false;
8355 }
8356
8357 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008358 * Do not invalidate views which are not visible and which are not running an animation. They
8359 * will not get drawn and they should not set dirty flags as if they will be drawn
8360 */
8361 private boolean skipInvalidate() {
8362 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8363 (!(mParent instanceof ViewGroup) ||
8364 !((ViewGroup) mParent).isViewTransitioning(this));
8365 }
8366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008368 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8369 * in the future. This must be called from a UI thread. To call from a non-UI
8370 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 *
8372 * WARNING: This method is destructive to dirty.
8373 * @param dirty the rectangle representing the bounds of the dirty region
8374 */
8375 public void invalidate(Rect dirty) {
8376 if (ViewDebug.TRACE_HIERARCHY) {
8377 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8378 }
8379
Chet Haaseaceafe62011-08-26 15:44:33 -07008380 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008381 return;
8382 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008383 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008384 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8385 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008387 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008388 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008389 final ViewParent p = mParent;
8390 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008391 //noinspection PointlessBooleanExpression,ConstantConditions
8392 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8393 if (p != null && ai != null && ai.mHardwareAccelerated) {
8394 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008395 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008396 p.invalidateChild(this, null);
8397 return;
8398 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 if (p != null && ai != null) {
8401 final int scrollX = mScrollX;
8402 final int scrollY = mScrollY;
8403 final Rect r = ai.mTmpInvalRect;
8404 r.set(dirty.left - scrollX, dirty.top - scrollY,
8405 dirty.right - scrollX, dirty.bottom - scrollY);
8406 mParent.invalidateChild(this, r);
8407 }
8408 }
8409 }
8410
8411 /**
8412 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8413 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008414 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8415 * will be called at some point in the future. This must be called from
8416 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 * @param l the left position of the dirty region
8418 * @param t the top position of the dirty region
8419 * @param r the right position of the dirty region
8420 * @param b the bottom position of the dirty region
8421 */
8422 public void invalidate(int l, int t, int r, int b) {
8423 if (ViewDebug.TRACE_HIERARCHY) {
8424 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8425 }
8426
Chet Haaseaceafe62011-08-26 15:44:33 -07008427 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008428 return;
8429 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008430 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008431 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8432 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008434 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008435 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 final ViewParent p = mParent;
8437 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008438 //noinspection PointlessBooleanExpression,ConstantConditions
8439 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8440 if (p != null && ai != null && ai.mHardwareAccelerated) {
8441 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008442 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008443 p.invalidateChild(this, null);
8444 return;
8445 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 if (p != null && ai != null && l < r && t < b) {
8448 final int scrollX = mScrollX;
8449 final int scrollY = mScrollY;
8450 final Rect tmpr = ai.mTmpInvalRect;
8451 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8452 p.invalidateChild(this, tmpr);
8453 }
8454 }
8455 }
8456
8457 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008458 * Invalidate the whole view. If the view is visible,
8459 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8460 * the future. This must be called from a UI thread. To call from a non-UI thread,
8461 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 */
8463 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008464 invalidate(true);
8465 }
Joe Malin32736f02011-01-19 16:14:20 -08008466
Chet Haaseed032702010-10-01 14:05:54 -07008467 /**
8468 * This is where the invalidate() work actually happens. A full invalidate()
8469 * causes the drawing cache to be invalidated, but this function can be called with
8470 * invalidateCache set to false to skip that invalidation step for cases that do not
8471 * need it (for example, a component that remains at the same dimensions with the same
8472 * content).
8473 *
8474 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8475 * well. This is usually true for a full invalidate, but may be set to false if the
8476 * View's contents or dimensions have not changed.
8477 */
Romain Guy849d0a32011-02-01 17:20:48 -08008478 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 if (ViewDebug.TRACE_HIERARCHY) {
8480 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8481 }
8482
Chet Haaseaceafe62011-08-26 15:44:33 -07008483 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008484 return;
8485 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008486 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008487 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008488 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8489 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008490 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008491 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008492 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008493 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008494 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008497 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008498 //noinspection PointlessBooleanExpression,ConstantConditions
8499 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8500 if (p != null && ai != null && ai.mHardwareAccelerated) {
8501 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008502 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008503 p.invalidateChild(this, null);
8504 return;
8505 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008506 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 if (p != null && ai != null) {
8509 final Rect r = ai.mTmpInvalRect;
8510 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8511 // Don't call invalidate -- we don't want to internally scroll
8512 // our own bounds
8513 p.invalidateChild(this, r);
8514 }
8515 }
8516 }
8517
8518 /**
Romain Guyda489792011-02-03 01:05:15 -08008519 * @hide
8520 */
8521 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008522 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008523 return;
8524 }
Romain Guyda489792011-02-03 01:05:15 -08008525 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8526 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8527 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8528 if (mParent instanceof View) {
8529 ((View) mParent).mPrivateFlags |= INVALIDATED;
8530 }
8531 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008532 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008533 mPrivateFlags |= INVALIDATED;
8534 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008535 if (mParent != null && mAttachInfo != null) {
8536 if (mAttachInfo.mHardwareAccelerated) {
8537 mParent.invalidateChild(this, null);
8538 } else {
8539 final Rect r = mAttachInfo.mTmpInvalRect;
8540 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8541 // Don't call invalidate -- we don't want to internally scroll
8542 // our own bounds
8543 mParent.invalidateChild(this, r);
8544 }
Romain Guyda489792011-02-03 01:05:15 -08008545 }
8546 }
8547 }
8548
8549 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008550 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008551 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8552 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008553 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8554 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008555 *
8556 * @hide
8557 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008558 protected void invalidateParentCaches() {
8559 if (mParent instanceof View) {
8560 ((View) mParent).mPrivateFlags |= INVALIDATED;
8561 }
8562 }
Joe Malin32736f02011-01-19 16:14:20 -08008563
Romain Guy0fd89bf2011-01-26 15:41:30 -08008564 /**
8565 * Used to indicate that the parent of this view should be invalidated. This functionality
8566 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8567 * which is necessary when various parent-managed properties of the view change, such as
8568 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8569 * an invalidation event to the parent.
8570 *
8571 * @hide
8572 */
8573 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008574 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008575 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008576 }
8577 }
8578
8579 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008580 * Indicates whether this View is opaque. An opaque View guarantees that it will
8581 * draw all the pixels overlapping its bounds using a fully opaque color.
8582 *
8583 * Subclasses of View should override this method whenever possible to indicate
8584 * whether an instance is opaque. Opaque Views are treated in a special way by
8585 * the View hierarchy, possibly allowing it to perform optimizations during
8586 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008587 *
Romain Guy24443ea2009-05-11 11:56:30 -07008588 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008589 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008590 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008591 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008592 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008593 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8594 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008595 }
8596
Adam Powell20232d02010-12-08 21:08:53 -08008597 /**
8598 * @hide
8599 */
8600 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008601 // Opaque if:
8602 // - Has a background
8603 // - Background is opaque
8604 // - Doesn't have scrollbars or scrollbars are inside overlay
8605
8606 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8607 mPrivateFlags |= OPAQUE_BACKGROUND;
8608 } else {
8609 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8610 }
8611
8612 final int flags = mViewFlags;
8613 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8614 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8615 mPrivateFlags |= OPAQUE_SCROLLBARS;
8616 } else {
8617 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8618 }
8619 }
8620
8621 /**
8622 * @hide
8623 */
8624 protected boolean hasOpaqueScrollbars() {
8625 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008626 }
8627
8628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 * @return A handler associated with the thread running the View. This
8630 * handler can be used to pump events in the UI events queue.
8631 */
8632 public Handler getHandler() {
8633 if (mAttachInfo != null) {
8634 return mAttachInfo.mHandler;
8635 }
8636 return null;
8637 }
8638
8639 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008640 * <p>Causes the Runnable to be added to the message queue.
8641 * The runnable will be run on the user interface thread.</p>
8642 *
8643 * <p>This method can be invoked from outside of the UI thread
8644 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645 *
8646 * @param action The Runnable that will be executed.
8647 *
8648 * @return Returns true if the Runnable was successfully placed in to the
8649 * message queue. Returns false on failure, usually because the
8650 * looper processing the message queue is exiting.
8651 */
8652 public boolean post(Runnable action) {
8653 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008654 AttachInfo attachInfo = mAttachInfo;
8655 if (attachInfo != null) {
8656 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008657 } else {
8658 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008659 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 return true;
8661 }
8662
8663 return handler.post(action);
8664 }
8665
8666 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008667 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008669 * The runnable will be run on the user interface thread.</p>
8670 *
8671 * <p>This method can be invoked from outside of the UI thread
8672 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 *
8674 * @param action The Runnable that will be executed.
8675 * @param delayMillis The delay (in milliseconds) until the Runnable
8676 * will be executed.
8677 *
8678 * @return true if the Runnable was successfully placed in to the
8679 * message queue. Returns false on failure, usually because the
8680 * looper processing the message queue is exiting. Note that a
8681 * result of true does not mean the Runnable will be processed --
8682 * if the looper is quit before the delivery time of the message
8683 * occurs then the message will be dropped.
8684 */
8685 public boolean postDelayed(Runnable action, long delayMillis) {
8686 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008687 AttachInfo attachInfo = mAttachInfo;
8688 if (attachInfo != null) {
8689 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008690 } else {
8691 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008692 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 return true;
8694 }
8695
8696 return handler.postDelayed(action, delayMillis);
8697 }
8698
8699 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008700 * <p>Removes the specified Runnable from the message queue.</p>
8701 *
8702 * <p>This method can be invoked from outside of the UI thread
8703 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008704 *
8705 * @param action The Runnable to remove from the message handling queue
8706 *
8707 * @return true if this view could ask the Handler to remove the Runnable,
8708 * false otherwise. When the returned value is true, the Runnable
8709 * may or may not have been actually removed from the message queue
8710 * (for instance, if the Runnable was not in the queue already.)
8711 */
8712 public boolean removeCallbacks(Runnable action) {
8713 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008714 AttachInfo attachInfo = mAttachInfo;
8715 if (attachInfo != null) {
8716 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 } else {
8718 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008719 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 return true;
8721 }
8722
8723 handler.removeCallbacks(action);
8724 return true;
8725 }
8726
8727 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008728 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8729 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 *
Romain Guye63a4f32011-08-11 11:33:31 -07008731 * <p>This method can be invoked from outside of the UI thread
8732 * only when this View is attached to a window.</p>
8733 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008734 * @see #invalidate()
8735 */
8736 public void postInvalidate() {
8737 postInvalidateDelayed(0);
8738 }
8739
8740 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008741 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8742 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8743 *
8744 * <p>This method can be invoked from outside of the UI thread
8745 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 *
8747 * @param left The left coordinate of the rectangle to invalidate.
8748 * @param top The top coordinate of the rectangle to invalidate.
8749 * @param right The right coordinate of the rectangle to invalidate.
8750 * @param bottom The bottom coordinate of the rectangle to invalidate.
8751 *
8752 * @see #invalidate(int, int, int, int)
8753 * @see #invalidate(Rect)
8754 */
8755 public void postInvalidate(int left, int top, int right, int bottom) {
8756 postInvalidateDelayed(0, left, top, right, bottom);
8757 }
8758
8759 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008760 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8761 * loop. Waits for the specified amount of time.</p>
8762 *
8763 * <p>This method can be invoked from outside of the UI thread
8764 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 *
8766 * @param delayMilliseconds the duration in milliseconds to delay the
8767 * invalidation by
8768 */
8769 public void postInvalidateDelayed(long delayMilliseconds) {
8770 // We try only with the AttachInfo because there's no point in invalidating
8771 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008772 AttachInfo attachInfo = mAttachInfo;
8773 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 Message msg = Message.obtain();
8775 msg.what = AttachInfo.INVALIDATE_MSG;
8776 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008777 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 }
8779 }
8780
8781 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008782 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8783 * through the event loop. Waits for the specified amount of time.</p>
8784 *
8785 * <p>This method can be invoked from outside of the UI thread
8786 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 *
8788 * @param delayMilliseconds the duration in milliseconds to delay the
8789 * invalidation by
8790 * @param left The left coordinate of the rectangle to invalidate.
8791 * @param top The top coordinate of the rectangle to invalidate.
8792 * @param right The right coordinate of the rectangle to invalidate.
8793 * @param bottom The bottom coordinate of the rectangle to invalidate.
8794 */
8795 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8796 int right, int bottom) {
8797
8798 // We try only with the AttachInfo because there's no point in invalidating
8799 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008800 AttachInfo attachInfo = mAttachInfo;
8801 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008802 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8803 info.target = this;
8804 info.left = left;
8805 info.top = top;
8806 info.right = right;
8807 info.bottom = bottom;
8808
8809 final Message msg = Message.obtain();
8810 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8811 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008812 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 }
8814 }
8815
8816 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008817 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8818 * This event is sent at most once every
8819 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8820 */
8821 private void postSendViewScrolledAccessibilityEventCallback() {
8822 if (mSendViewScrolledAccessibilityEvent == null) {
8823 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8824 }
8825 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8826 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8827 postDelayed(mSendViewScrolledAccessibilityEvent,
8828 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8829 }
8830 }
8831
8832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 * Called by a parent to request that a child update its values for mScrollX
8834 * and mScrollY if necessary. This will typically be done if the child is
8835 * animating a scroll using a {@link android.widget.Scroller Scroller}
8836 * object.
8837 */
8838 public void computeScroll() {
8839 }
8840
8841 /**
8842 * <p>Indicate whether the horizontal edges are faded when the view is
8843 * scrolled horizontally.</p>
8844 *
8845 * @return true if the horizontal edges should are faded on scroll, false
8846 * otherwise
8847 *
8848 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008849 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 */
8851 public boolean isHorizontalFadingEdgeEnabled() {
8852 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8853 }
8854
8855 /**
8856 * <p>Define whether the horizontal edges should be faded when this view
8857 * is scrolled horizontally.</p>
8858 *
8859 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8860 * be faded when the view is scrolled
8861 * horizontally
8862 *
8863 * @see #isHorizontalFadingEdgeEnabled()
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 void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8867 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8868 if (horizontalFadingEdgeEnabled) {
8869 initScrollCache();
8870 }
8871
8872 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8873 }
8874 }
8875
8876 /**
8877 * <p>Indicate whether the vertical edges are faded when the view is
8878 * scrolled horizontally.</p>
8879 *
8880 * @return true if the vertical edges should are faded on scroll, false
8881 * otherwise
8882 *
8883 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008884 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 */
8886 public boolean isVerticalFadingEdgeEnabled() {
8887 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8888 }
8889
8890 /**
8891 * <p>Define whether the vertical edges should be faded when this view
8892 * is scrolled vertically.</p>
8893 *
8894 * @param verticalFadingEdgeEnabled true if the vertical edges should
8895 * be faded when the view is scrolled
8896 * vertically
8897 *
8898 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008899 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008900 */
8901 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8902 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8903 if (verticalFadingEdgeEnabled) {
8904 initScrollCache();
8905 }
8906
8907 mViewFlags ^= FADING_EDGE_VERTICAL;
8908 }
8909 }
8910
8911 /**
8912 * Returns the strength, or intensity, of the top faded edge. The strength is
8913 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8914 * returns 0.0 or 1.0 but no value in between.
8915 *
8916 * Subclasses should override this method to provide a smoother fade transition
8917 * when scrolling occurs.
8918 *
8919 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8920 */
8921 protected float getTopFadingEdgeStrength() {
8922 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8923 }
8924
8925 /**
8926 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8927 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8928 * returns 0.0 or 1.0 but no value in between.
8929 *
8930 * Subclasses should override this method to provide a smoother fade transition
8931 * when scrolling occurs.
8932 *
8933 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8934 */
8935 protected float getBottomFadingEdgeStrength() {
8936 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8937 computeVerticalScrollRange() ? 1.0f : 0.0f;
8938 }
8939
8940 /**
8941 * Returns the strength, or intensity, of the left faded edge. The strength is
8942 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8943 * returns 0.0 or 1.0 but no value in between.
8944 *
8945 * Subclasses should override this method to provide a smoother fade transition
8946 * when scrolling occurs.
8947 *
8948 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8949 */
8950 protected float getLeftFadingEdgeStrength() {
8951 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8952 }
8953
8954 /**
8955 * Returns the strength, or intensity, of the right faded edge. The strength is
8956 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8957 * returns 0.0 or 1.0 but no value in between.
8958 *
8959 * Subclasses should override this method to provide a smoother fade transition
8960 * when scrolling occurs.
8961 *
8962 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8963 */
8964 protected float getRightFadingEdgeStrength() {
8965 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8966 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8967 }
8968
8969 /**
8970 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8971 * scrollbar is not drawn by default.</p>
8972 *
8973 * @return true if the horizontal scrollbar should be painted, false
8974 * otherwise
8975 *
8976 * @see #setHorizontalScrollBarEnabled(boolean)
8977 */
8978 public boolean isHorizontalScrollBarEnabled() {
8979 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8980 }
8981
8982 /**
8983 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8984 * scrollbar is not drawn by default.</p>
8985 *
8986 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8987 * be painted
8988 *
8989 * @see #isHorizontalScrollBarEnabled()
8990 */
8991 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8992 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8993 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008994 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008995 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 }
8997 }
8998
8999 /**
9000 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9001 * scrollbar is not drawn by default.</p>
9002 *
9003 * @return true if the vertical scrollbar should be painted, false
9004 * otherwise
9005 *
9006 * @see #setVerticalScrollBarEnabled(boolean)
9007 */
9008 public boolean isVerticalScrollBarEnabled() {
9009 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9010 }
9011
9012 /**
9013 * <p>Define whether the vertical scrollbar should be drawn or not. The
9014 * scrollbar is not drawn by default.</p>
9015 *
9016 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9017 * be painted
9018 *
9019 * @see #isVerticalScrollBarEnabled()
9020 */
9021 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9022 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9023 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009024 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009025 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 }
9027 }
9028
Adam Powell20232d02010-12-08 21:08:53 -08009029 /**
9030 * @hide
9031 */
9032 protected void recomputePadding() {
9033 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 }
Joe Malin32736f02011-01-19 16:14:20 -08009035
Mike Cleronfe81d382009-09-28 14:22:16 -07009036 /**
9037 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009038 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009039 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009040 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009041 */
9042 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9043 initScrollCache();
9044 final ScrollabilityCache scrollabilityCache = mScrollCache;
9045 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009046 if (fadeScrollbars) {
9047 scrollabilityCache.state = ScrollabilityCache.OFF;
9048 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009049 scrollabilityCache.state = ScrollabilityCache.ON;
9050 }
9051 }
Joe Malin32736f02011-01-19 16:14:20 -08009052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 /**
Joe Malin32736f02011-01-19 16:14:20 -08009054 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009055 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009056 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009057 * @return true if scrollbar fading is enabled
9058 */
9059 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009060 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009061 }
Joe Malin32736f02011-01-19 16:14:20 -08009062
Mike Cleron52f0a642009-09-28 18:21:37 -07009063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9065 * inset. When inset, they add to the padding of the view. And the scrollbars
9066 * can be drawn inside the padding area or on the edge of the view. For example,
9067 * if a view has a background drawable and you want to draw the scrollbars
9068 * inside the padding specified by the drawable, you can use
9069 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9070 * appear at the edge of the view, ignoring the padding, then you can use
9071 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9072 * @param style the style of the scrollbars. Should be one of
9073 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9074 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9075 * @see #SCROLLBARS_INSIDE_OVERLAY
9076 * @see #SCROLLBARS_INSIDE_INSET
9077 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9078 * @see #SCROLLBARS_OUTSIDE_INSET
9079 */
9080 public void setScrollBarStyle(int style) {
9081 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9082 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009083 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009084 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 }
9086 }
9087
9088 /**
9089 * <p>Returns the current scrollbar style.</p>
9090 * @return the current scrollbar style
9091 * @see #SCROLLBARS_INSIDE_OVERLAY
9092 * @see #SCROLLBARS_INSIDE_INSET
9093 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9094 * @see #SCROLLBARS_OUTSIDE_INSET
9095 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009096 @ViewDebug.ExportedProperty(mapping = {
9097 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9098 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9099 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9100 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9101 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 public int getScrollBarStyle() {
9103 return mViewFlags & SCROLLBARS_STYLE_MASK;
9104 }
9105
9106 /**
9107 * <p>Compute the horizontal range that the horizontal scrollbar
9108 * represents.</p>
9109 *
9110 * <p>The range is expressed in arbitrary units that must be the same as the
9111 * units used by {@link #computeHorizontalScrollExtent()} and
9112 * {@link #computeHorizontalScrollOffset()}.</p>
9113 *
9114 * <p>The default range is the drawing width of this view.</p>
9115 *
9116 * @return the total horizontal range represented by the horizontal
9117 * scrollbar
9118 *
9119 * @see #computeHorizontalScrollExtent()
9120 * @see #computeHorizontalScrollOffset()
9121 * @see android.widget.ScrollBarDrawable
9122 */
9123 protected int computeHorizontalScrollRange() {
9124 return getWidth();
9125 }
9126
9127 /**
9128 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9129 * within the horizontal range. This value is used to compute the position
9130 * of the thumb within the scrollbar's track.</p>
9131 *
9132 * <p>The range is expressed in arbitrary units that must be the same as the
9133 * units used by {@link #computeHorizontalScrollRange()} and
9134 * {@link #computeHorizontalScrollExtent()}.</p>
9135 *
9136 * <p>The default offset is the scroll offset of this view.</p>
9137 *
9138 * @return the horizontal offset of the scrollbar's thumb
9139 *
9140 * @see #computeHorizontalScrollRange()
9141 * @see #computeHorizontalScrollExtent()
9142 * @see android.widget.ScrollBarDrawable
9143 */
9144 protected int computeHorizontalScrollOffset() {
9145 return mScrollX;
9146 }
9147
9148 /**
9149 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9150 * within the horizontal range. This value is used to compute the length
9151 * of the thumb within the scrollbar's track.</p>
9152 *
9153 * <p>The range is expressed in arbitrary units that must be the same as the
9154 * units used by {@link #computeHorizontalScrollRange()} and
9155 * {@link #computeHorizontalScrollOffset()}.</p>
9156 *
9157 * <p>The default extent is the drawing width of this view.</p>
9158 *
9159 * @return the horizontal extent of the scrollbar's thumb
9160 *
9161 * @see #computeHorizontalScrollRange()
9162 * @see #computeHorizontalScrollOffset()
9163 * @see android.widget.ScrollBarDrawable
9164 */
9165 protected int computeHorizontalScrollExtent() {
9166 return getWidth();
9167 }
9168
9169 /**
9170 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9171 *
9172 * <p>The range is expressed in arbitrary units that must be the same as the
9173 * units used by {@link #computeVerticalScrollExtent()} and
9174 * {@link #computeVerticalScrollOffset()}.</p>
9175 *
9176 * @return the total vertical range represented by the vertical scrollbar
9177 *
9178 * <p>The default range is the drawing height of this view.</p>
9179 *
9180 * @see #computeVerticalScrollExtent()
9181 * @see #computeVerticalScrollOffset()
9182 * @see android.widget.ScrollBarDrawable
9183 */
9184 protected int computeVerticalScrollRange() {
9185 return getHeight();
9186 }
9187
9188 /**
9189 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9190 * within the horizontal range. This value is used to compute the position
9191 * of the thumb within the scrollbar's track.</p>
9192 *
9193 * <p>The range is expressed in arbitrary units that must be the same as the
9194 * units used by {@link #computeVerticalScrollRange()} and
9195 * {@link #computeVerticalScrollExtent()}.</p>
9196 *
9197 * <p>The default offset is the scroll offset of this view.</p>
9198 *
9199 * @return the vertical offset of the scrollbar's thumb
9200 *
9201 * @see #computeVerticalScrollRange()
9202 * @see #computeVerticalScrollExtent()
9203 * @see android.widget.ScrollBarDrawable
9204 */
9205 protected int computeVerticalScrollOffset() {
9206 return mScrollY;
9207 }
9208
9209 /**
9210 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9211 * within the vertical range. This value is used to compute the length
9212 * of the thumb within the scrollbar's track.</p>
9213 *
9214 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009215 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 * {@link #computeVerticalScrollOffset()}.</p>
9217 *
9218 * <p>The default extent is the drawing height of this view.</p>
9219 *
9220 * @return the vertical extent of the scrollbar's thumb
9221 *
9222 * @see #computeVerticalScrollRange()
9223 * @see #computeVerticalScrollOffset()
9224 * @see android.widget.ScrollBarDrawable
9225 */
9226 protected int computeVerticalScrollExtent() {
9227 return getHeight();
9228 }
9229
9230 /**
Adam Powell69159442011-06-13 17:53:06 -07009231 * Check if this view can be scrolled horizontally in a certain direction.
9232 *
9233 * @param direction Negative to check scrolling left, positive to check scrolling right.
9234 * @return true if this view can be scrolled in the specified direction, false otherwise.
9235 */
9236 public boolean canScrollHorizontally(int direction) {
9237 final int offset = computeHorizontalScrollOffset();
9238 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9239 if (range == 0) return false;
9240 if (direction < 0) {
9241 return offset > 0;
9242 } else {
9243 return offset < range - 1;
9244 }
9245 }
9246
9247 /**
9248 * Check if this view can be scrolled vertically in a certain direction.
9249 *
9250 * @param direction Negative to check scrolling up, positive to check scrolling down.
9251 * @return true if this view can be scrolled in the specified direction, false otherwise.
9252 */
9253 public boolean canScrollVertically(int direction) {
9254 final int offset = computeVerticalScrollOffset();
9255 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9256 if (range == 0) return false;
9257 if (direction < 0) {
9258 return offset > 0;
9259 } else {
9260 return offset < range - 1;
9261 }
9262 }
9263
9264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9266 * scrollbars are painted only if they have been awakened first.</p>
9267 *
9268 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009269 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009270 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009272 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 // scrollbars are drawn only when the animation is running
9274 final ScrollabilityCache cache = mScrollCache;
9275 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009276
Mike Cleronf116bf82009-09-27 19:14:12 -07009277 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009278
Mike Cleronf116bf82009-09-27 19:14:12 -07009279 if (state == ScrollabilityCache.OFF) {
9280 return;
9281 }
Joe Malin32736f02011-01-19 16:14:20 -08009282
Mike Cleronf116bf82009-09-27 19:14:12 -07009283 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009284
Mike Cleronf116bf82009-09-27 19:14:12 -07009285 if (state == ScrollabilityCache.FADING) {
9286 // We're fading -- get our fade interpolation
9287 if (cache.interpolatorValues == null) {
9288 cache.interpolatorValues = new float[1];
9289 }
Joe Malin32736f02011-01-19 16:14:20 -08009290
Mike Cleronf116bf82009-09-27 19:14:12 -07009291 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009292
Mike Cleronf116bf82009-09-27 19:14:12 -07009293 // Stops the animation if we're done
9294 if (cache.scrollBarInterpolator.timeToValues(values) ==
9295 Interpolator.Result.FREEZE_END) {
9296 cache.state = ScrollabilityCache.OFF;
9297 } else {
9298 cache.scrollBar.setAlpha(Math.round(values[0]));
9299 }
Joe Malin32736f02011-01-19 16:14:20 -08009300
9301 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009302 // drawing. We only want this when we're fading so that
9303 // we prevent excessive redraws
9304 invalidate = true;
9305 } else {
9306 // We're just on -- but we may have been fading before so
9307 // reset alpha
9308 cache.scrollBar.setAlpha(255);
9309 }
9310
Joe Malin32736f02011-01-19 16:14:20 -08009311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 final int viewFlags = mViewFlags;
9313
9314 final boolean drawHorizontalScrollBar =
9315 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9316 final boolean drawVerticalScrollBar =
9317 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9318 && !isVerticalScrollBarHidden();
9319
9320 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9321 final int width = mRight - mLeft;
9322 final int height = mBottom - mTop;
9323
9324 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009325
Mike Reede8853fc2009-09-04 14:01:48 -04009326 final int scrollX = mScrollX;
9327 final int scrollY = mScrollY;
9328 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9329
Mike Cleronf116bf82009-09-27 19:14:12 -07009330 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009332 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009333 int size = scrollBar.getSize(false);
9334 if (size <= 0) {
9335 size = cache.scrollBarSize;
9336 }
9337
Mike Cleronf116bf82009-09-27 19:14:12 -07009338 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009339 computeHorizontalScrollOffset(),
9340 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009341 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009342 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009343 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009344 left = scrollX + (mPaddingLeft & inside);
9345 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9346 bottom = top + size;
9347 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9348 if (invalidate) {
9349 invalidate(left, top, right, bottom);
9350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009351 }
9352
9353 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009354 int size = scrollBar.getSize(true);
9355 if (size <= 0) {
9356 size = cache.scrollBarSize;
9357 }
9358
Mike Reede8853fc2009-09-04 14:01:48 -04009359 scrollBar.setParameters(computeVerticalScrollRange(),
9360 computeVerticalScrollOffset(),
9361 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009362 switch (mVerticalScrollbarPosition) {
9363 default:
9364 case SCROLLBAR_POSITION_DEFAULT:
9365 case SCROLLBAR_POSITION_RIGHT:
9366 left = scrollX + width - size - (mUserPaddingRight & inside);
9367 break;
9368 case SCROLLBAR_POSITION_LEFT:
9369 left = scrollX + (mUserPaddingLeft & inside);
9370 break;
9371 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009372 top = scrollY + (mPaddingTop & inside);
9373 right = left + size;
9374 bottom = scrollY + height - (mUserPaddingBottom & inside);
9375 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9376 if (invalidate) {
9377 invalidate(left, top, right, bottom);
9378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009379 }
9380 }
9381 }
9382 }
Romain Guy8506ab42009-06-11 17:35:47 -07009383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009385 * 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 -08009386 * FastScroller is visible.
9387 * @return whether to temporarily hide the vertical scrollbar
9388 * @hide
9389 */
9390 protected boolean isVerticalScrollBarHidden() {
9391 return false;
9392 }
9393
9394 /**
9395 * <p>Draw the horizontal scrollbar if
9396 * {@link #isHorizontalScrollBarEnabled()} 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 #isHorizontalScrollBarEnabled()
9402 * @see #computeHorizontalScrollRange()
9403 * @see #computeHorizontalScrollExtent()
9404 * @see #computeHorizontalScrollOffset()
9405 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009406 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009407 */
Romain Guy8fb95422010-08-17 18:38:51 -07009408 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9409 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009410 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009411 scrollBar.draw(canvas);
9412 }
Mike Reede8853fc2009-09-04 14:01:48 -04009413
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9416 * returns true.</p>
9417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009418 * @param canvas the canvas on which to draw the scrollbar
9419 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 *
9421 * @see #isVerticalScrollBarEnabled()
9422 * @see #computeVerticalScrollRange()
9423 * @see #computeVerticalScrollExtent()
9424 * @see #computeVerticalScrollOffset()
9425 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009426 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 */
Romain Guy8fb95422010-08-17 18:38:51 -07009428 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9429 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009430 scrollBar.setBounds(l, t, r, b);
9431 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009432 }
9433
9434 /**
9435 * Implement this to do your drawing.
9436 *
9437 * @param canvas the canvas on which the background will be drawn
9438 */
9439 protected void onDraw(Canvas canvas) {
9440 }
9441
9442 /*
9443 * Caller is responsible for calling requestLayout if necessary.
9444 * (This allows addViewInLayout to not request a new layout.)
9445 */
9446 void assignParent(ViewParent parent) {
9447 if (mParent == null) {
9448 mParent = parent;
9449 } else if (parent == null) {
9450 mParent = null;
9451 } else {
9452 throw new RuntimeException("view " + this + " being added, but"
9453 + " it already has a parent");
9454 }
9455 }
9456
9457 /**
9458 * This is called when the view is attached to a window. At this point it
9459 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009460 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9461 * however it may be called any time before the first onDraw -- including
9462 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 *
9464 * @see #onDetachedFromWindow()
9465 */
9466 protected void onAttachedToWindow() {
9467 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9468 mParent.requestTransparentRegion(this);
9469 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009470 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9471 initialAwakenScrollBars();
9472 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9473 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009474 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009475 // Order is important here: LayoutDirection MUST be resolved before Padding
9476 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009477 resolveLayoutDirectionIfNeeded();
9478 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009479 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009480 if (isFocused()) {
9481 InputMethodManager imm = InputMethodManager.peekInstance();
9482 imm.focusIn(this);
9483 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009484 }
Cibu Johny86666632010-02-22 13:01:02 -08009485
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009486 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009487 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9488 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009489 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009490 private void resolveLayoutDirectionIfNeeded() {
9491 // Do not resolve if it is not needed
9492 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9493
9494 // Clear any previous layout direction resolution
9495 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9496
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009497 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9498 // TextDirectionHeuristic
9499 resetResolvedTextDirection();
9500
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009501 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009502 switch (getLayoutDirection()) {
9503 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009504 // We cannot do the resolution if there is no parent
9505 if (mParent == null) return;
9506
Cibu Johny86666632010-02-22 13:01:02 -08009507 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009508 if (mParent instanceof ViewGroup) {
9509 ViewGroup viewGroup = ((ViewGroup) mParent);
9510
9511 // Check if the parent view group can resolve
9512 if (! viewGroup.canResolveLayoutDirection()) {
9513 return;
9514 }
9515 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9516 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9517 }
Cibu Johny86666632010-02-22 13:01:02 -08009518 }
9519 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009520 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009521 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009522 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009523 case LAYOUT_DIRECTION_LOCALE:
9524 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009525 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009526 }
9527 break;
9528 default:
9529 // Nothing to do, LTR by default
9530 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009531
9532 // Set to resolved
9533 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9534 }
9535
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009536 /**
9537 * @hide
9538 */
9539 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009540 // If the user specified the absolute padding (either with android:padding or
9541 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9542 // use the default padding or the padding from the background drawable
9543 // (stored at this point in mPadding*)
9544 switch (getResolvedLayoutDirection()) {
9545 case LAYOUT_DIRECTION_RTL:
9546 // Start user padding override Right user padding. Otherwise, if Right user
9547 // padding is not defined, use the default Right padding. If Right user padding
9548 // is defined, just use it.
9549 if (mUserPaddingStart >= 0) {
9550 mUserPaddingRight = mUserPaddingStart;
9551 } else if (mUserPaddingRight < 0) {
9552 mUserPaddingRight = mPaddingRight;
9553 }
9554 if (mUserPaddingEnd >= 0) {
9555 mUserPaddingLeft = mUserPaddingEnd;
9556 } else if (mUserPaddingLeft < 0) {
9557 mUserPaddingLeft = mPaddingLeft;
9558 }
9559 break;
9560 case LAYOUT_DIRECTION_LTR:
9561 default:
9562 // Start user padding override Left user padding. Otherwise, if Left user
9563 // padding is not defined, use the default left padding. If Left user padding
9564 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009565 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009566 mUserPaddingLeft = mUserPaddingStart;
9567 } else if (mUserPaddingLeft < 0) {
9568 mUserPaddingLeft = mPaddingLeft;
9569 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009570 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009571 mUserPaddingRight = mUserPaddingEnd;
9572 } else if (mUserPaddingRight < 0) {
9573 mUserPaddingRight = mPaddingRight;
9574 }
9575 }
9576
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009577 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9578
9579 recomputePadding();
9580 }
9581
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009582 /**
9583 * Return true if layout direction resolution can be done
9584 *
9585 * @hide
9586 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009587 protected boolean canResolveLayoutDirection() {
9588 switch (getLayoutDirection()) {
9589 case LAYOUT_DIRECTION_INHERIT:
9590 return (mParent != null);
9591 default:
9592 return true;
9593 }
9594 }
9595
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009596 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009597 * Reset the resolved layout direction.
9598 *
9599 * Subclasses need to override this method to clear cached information that depends on the
9600 * resolved layout direction, or to inform child views that inherit their layout direction.
9601 * Overrides must also call the superclass implementation at the start of their implementation.
9602 *
9603 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009604 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009605 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009606 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009607 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009608 }
9609
9610 /**
9611 * Check if a Locale is corresponding to a RTL script.
9612 *
9613 * @param locale Locale to check
9614 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009615 *
9616 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009617 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009618 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009619 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9620 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 }
9622
9623 /**
9624 * This is called when the view is detached from a window. At this point it
9625 * no longer has a surface for drawing.
9626 *
9627 * @see #onAttachedToWindow()
9628 */
9629 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009630 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009631
Romain Guya440b002010-02-24 15:57:54 -08009632 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009633 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009634 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009635 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009638
Romain Guy6d7475d2011-07-27 16:28:21 -07009639 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009640
Chet Haasedaf98e92011-01-10 14:10:36 -08009641 if (mDisplayList != null) {
9642 mDisplayList.invalidate();
9643 }
9644
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009645 if (mAttachInfo != null) {
9646 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009647 }
9648
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009649 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009650
9651 resetResolvedLayoutDirection();
9652 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 }
9654
9655 /**
9656 * @return The number of times this view has been attached to a window
9657 */
9658 protected int getWindowAttachCount() {
9659 return mWindowAttachCount;
9660 }
9661
9662 /**
9663 * Retrieve a unique token identifying the window this view is attached to.
9664 * @return Return the window's token for use in
9665 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9666 */
9667 public IBinder getWindowToken() {
9668 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9669 }
9670
9671 /**
9672 * Retrieve a unique token identifying the top-level "real" window of
9673 * the window that this view is attached to. That is, this is like
9674 * {@link #getWindowToken}, except if the window this view in is a panel
9675 * window (attached to another containing window), then the token of
9676 * the containing window is returned instead.
9677 *
9678 * @return Returns the associated window token, either
9679 * {@link #getWindowToken()} or the containing window's token.
9680 */
9681 public IBinder getApplicationWindowToken() {
9682 AttachInfo ai = mAttachInfo;
9683 if (ai != null) {
9684 IBinder appWindowToken = ai.mPanelParentWindowToken;
9685 if (appWindowToken == null) {
9686 appWindowToken = ai.mWindowToken;
9687 }
9688 return appWindowToken;
9689 }
9690 return null;
9691 }
9692
9693 /**
9694 * Retrieve private session object this view hierarchy is using to
9695 * communicate with the window manager.
9696 * @return the session object to communicate with the window manager
9697 */
9698 /*package*/ IWindowSession getWindowSession() {
9699 return mAttachInfo != null ? mAttachInfo.mSession : null;
9700 }
9701
9702 /**
9703 * @param info the {@link android.view.View.AttachInfo} to associated with
9704 * this view
9705 */
9706 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9707 //System.out.println("Attached! " + this);
9708 mAttachInfo = info;
9709 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009710 // We will need to evaluate the drawable state at least once.
9711 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 if (mFloatingTreeObserver != null) {
9713 info.mTreeObserver.merge(mFloatingTreeObserver);
9714 mFloatingTreeObserver = null;
9715 }
9716 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9717 mAttachInfo.mScrollContainers.add(this);
9718 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9719 }
9720 performCollectViewAttributes(visibility);
9721 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009722
9723 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9724 mOnAttachStateChangeListeners;
9725 if (listeners != null && listeners.size() > 0) {
9726 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9727 // perform the dispatching. The iterator is a safe guard against listeners that
9728 // could mutate the list by calling the various add/remove methods. This prevents
9729 // the array from being modified while we iterate it.
9730 for (OnAttachStateChangeListener listener : listeners) {
9731 listener.onViewAttachedToWindow(this);
9732 }
9733 }
9734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009735 int vis = info.mWindowVisibility;
9736 if (vis != GONE) {
9737 onWindowVisibilityChanged(vis);
9738 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009739 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9740 // If nobody has evaluated the drawable state yet, then do it now.
9741 refreshDrawableState();
9742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009743 }
9744
9745 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009746 AttachInfo info = mAttachInfo;
9747 if (info != null) {
9748 int vis = info.mWindowVisibility;
9749 if (vis != GONE) {
9750 onWindowVisibilityChanged(GONE);
9751 }
9752 }
9753
9754 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009755
Adam Powell4afd62b2011-02-18 15:02:18 -08009756 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9757 mOnAttachStateChangeListeners;
9758 if (listeners != null && listeners.size() > 0) {
9759 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9760 // perform the dispatching. The iterator is a safe guard against listeners that
9761 // could mutate the list by calling the various add/remove methods. This prevents
9762 // the array from being modified while we iterate it.
9763 for (OnAttachStateChangeListener listener : listeners) {
9764 listener.onViewDetachedFromWindow(this);
9765 }
9766 }
9767
Romain Guy01d5edc2011-01-28 11:28:53 -08009768 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 mAttachInfo.mScrollContainers.remove(this);
9770 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9771 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 mAttachInfo = null;
9774 }
9775
9776 /**
9777 * Store this view hierarchy's frozen state into the given container.
9778 *
9779 * @param container The SparseArray in which to save the view's state.
9780 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009781 * @see #restoreHierarchyState(android.util.SparseArray)
9782 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9783 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 */
9785 public void saveHierarchyState(SparseArray<Parcelable> container) {
9786 dispatchSaveInstanceState(container);
9787 }
9788
9789 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009790 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9791 * this view and its children. May be overridden to modify how freezing happens to a
9792 * 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 -08009793 *
9794 * @param container The SparseArray in which to save the view's state.
9795 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009796 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9797 * @see #saveHierarchyState(android.util.SparseArray)
9798 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 */
9800 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9801 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9802 mPrivateFlags &= ~SAVE_STATE_CALLED;
9803 Parcelable state = onSaveInstanceState();
9804 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9805 throw new IllegalStateException(
9806 "Derived class did not call super.onSaveInstanceState()");
9807 }
9808 if (state != null) {
9809 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9810 // + ": " + state);
9811 container.put(mID, state);
9812 }
9813 }
9814 }
9815
9816 /**
9817 * Hook allowing a view to generate a representation of its internal state
9818 * that can later be used to create a new instance with that same state.
9819 * This state should only contain information that is not persistent or can
9820 * not be reconstructed later. For example, you will never store your
9821 * current position on screen because that will be computed again when a
9822 * new instance of the view is placed in its view hierarchy.
9823 * <p>
9824 * Some examples of things you may store here: the current cursor position
9825 * in a text view (but usually not the text itself since that is stored in a
9826 * content provider or other persistent storage), the currently selected
9827 * item in a list view.
9828 *
9829 * @return Returns a Parcelable object containing the view's current dynamic
9830 * state, or null if there is nothing interesting to save. The
9831 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009832 * @see #onRestoreInstanceState(android.os.Parcelable)
9833 * @see #saveHierarchyState(android.util.SparseArray)
9834 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 * @see #setSaveEnabled(boolean)
9836 */
9837 protected Parcelable onSaveInstanceState() {
9838 mPrivateFlags |= SAVE_STATE_CALLED;
9839 return BaseSavedState.EMPTY_STATE;
9840 }
9841
9842 /**
9843 * Restore this view hierarchy's frozen state from the given container.
9844 *
9845 * @param container The SparseArray which holds previously frozen states.
9846 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009847 * @see #saveHierarchyState(android.util.SparseArray)
9848 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9849 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 */
9851 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9852 dispatchRestoreInstanceState(container);
9853 }
9854
9855 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009856 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9857 * state for this view and its children. May be overridden to modify how restoring
9858 * happens to a view's children; for example, some views may want to not store state
9859 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009860 *
9861 * @param container The SparseArray which holds previously saved state.
9862 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009863 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9864 * @see #restoreHierarchyState(android.util.SparseArray)
9865 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 */
9867 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9868 if (mID != NO_ID) {
9869 Parcelable state = container.get(mID);
9870 if (state != null) {
9871 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9872 // + ": " + state);
9873 mPrivateFlags &= ~SAVE_STATE_CALLED;
9874 onRestoreInstanceState(state);
9875 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9876 throw new IllegalStateException(
9877 "Derived class did not call super.onRestoreInstanceState()");
9878 }
9879 }
9880 }
9881 }
9882
9883 /**
9884 * Hook allowing a view to re-apply a representation of its internal state that had previously
9885 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9886 * null state.
9887 *
9888 * @param state The frozen state that had previously been returned by
9889 * {@link #onSaveInstanceState}.
9890 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009891 * @see #onSaveInstanceState()
9892 * @see #restoreHierarchyState(android.util.SparseArray)
9893 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 */
9895 protected void onRestoreInstanceState(Parcelable state) {
9896 mPrivateFlags |= SAVE_STATE_CALLED;
9897 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009898 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9899 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009900 + "when two views of different type have the same id in the same hierarchy. "
9901 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009902 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 }
9904 }
9905
9906 /**
9907 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9908 *
9909 * @return the drawing start time in milliseconds
9910 */
9911 public long getDrawingTime() {
9912 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9913 }
9914
9915 /**
9916 * <p>Enables or disables the duplication of the parent's state into this view. When
9917 * duplication is enabled, this view gets its drawable state from its parent rather
9918 * than from its own internal properties.</p>
9919 *
9920 * <p>Note: in the current implementation, setting this property to true after the
9921 * view was added to a ViewGroup might have no effect at all. This property should
9922 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9923 *
9924 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9925 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009926 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009927 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9928 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 *
9930 * @param enabled True to enable duplication of the parent's drawable state, false
9931 * to disable it.
9932 *
9933 * @see #getDrawableState()
9934 * @see #isDuplicateParentStateEnabled()
9935 */
9936 public void setDuplicateParentStateEnabled(boolean enabled) {
9937 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9938 }
9939
9940 /**
9941 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9942 *
9943 * @return True if this view's drawable state is duplicated from the parent,
9944 * false otherwise
9945 *
9946 * @see #getDrawableState()
9947 * @see #setDuplicateParentStateEnabled(boolean)
9948 */
9949 public boolean isDuplicateParentStateEnabled() {
9950 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9951 }
9952
9953 /**
Romain Guy171c5922011-01-06 10:04:23 -08009954 * <p>Specifies the type of layer backing this view. The layer can be
9955 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9956 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009957 *
Romain Guy171c5922011-01-06 10:04:23 -08009958 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9959 * instance that controls how the layer is composed on screen. The following
9960 * properties of the paint are taken into account when composing the layer:</p>
9961 * <ul>
9962 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9963 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9964 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9965 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009966 *
Romain Guy171c5922011-01-06 10:04:23 -08009967 * <p>If this view has an alpha value set to < 1.0 by calling
9968 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9969 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9970 * equivalent to setting a hardware layer on this view and providing a paint with
9971 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009972 *
Romain Guy171c5922011-01-06 10:04:23 -08009973 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9974 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9975 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009976 *
Romain Guy171c5922011-01-06 10:04:23 -08009977 * @param layerType The ype of layer to use with this view, must be one of
9978 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9979 * {@link #LAYER_TYPE_HARDWARE}
9980 * @param paint The paint used to compose the layer. This argument is optional
9981 * and can be null. It is ignored when the layer type is
9982 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009983 *
9984 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009985 * @see #LAYER_TYPE_NONE
9986 * @see #LAYER_TYPE_SOFTWARE
9987 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009988 * @see #setAlpha(float)
9989 *
Romain Guy171c5922011-01-06 10:04:23 -08009990 * @attr ref android.R.styleable#View_layerType
9991 */
9992 public void setLayerType(int layerType, Paint paint) {
9993 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009994 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009995 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9996 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009997
Romain Guyd6cd5722011-01-17 14:42:41 -08009998 if (layerType == mLayerType) {
9999 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10000 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010001 invalidateParentCaches();
10002 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010003 }
10004 return;
10005 }
Romain Guy171c5922011-01-06 10:04:23 -080010006
10007 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010008 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010009 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010010 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -070010011 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010012 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010013 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010014 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010015 default:
10016 break;
Romain Guy171c5922011-01-06 10:04:23 -080010017 }
10018
10019 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010020 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10021 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10022 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010023
Romain Guy0fd89bf2011-01-26 15:41:30 -080010024 invalidateParentCaches();
10025 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010026 }
10027
10028 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010029 * Indicates whether this view has a static layer. A view with layer type
10030 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10031 * dynamic.
10032 */
10033 boolean hasStaticLayer() {
10034 return mLayerType == LAYER_TYPE_NONE;
10035 }
10036
10037 /**
Romain Guy171c5922011-01-06 10:04:23 -080010038 * Indicates what type of layer is currently associated with this view. By default
10039 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10040 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10041 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010042 *
Romain Guy171c5922011-01-06 10:04:23 -080010043 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10044 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010045 *
10046 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010047 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010048 * @see #LAYER_TYPE_NONE
10049 * @see #LAYER_TYPE_SOFTWARE
10050 * @see #LAYER_TYPE_HARDWARE
10051 */
10052 public int getLayerType() {
10053 return mLayerType;
10054 }
Joe Malin32736f02011-01-19 16:14:20 -080010055
Romain Guy6c319ca2011-01-11 14:29:25 -080010056 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010057 * Forces this view's layer to be created and this view to be rendered
10058 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10059 * invoking this method will have no effect.
10060 *
10061 * This method can for instance be used to render a view into its layer before
10062 * starting an animation. If this view is complex, rendering into the layer
10063 * before starting the animation will avoid skipping frames.
10064 *
10065 * @throws IllegalStateException If this view is not attached to a window
10066 *
10067 * @see #setLayerType(int, android.graphics.Paint)
10068 */
10069 public void buildLayer() {
10070 if (mLayerType == LAYER_TYPE_NONE) return;
10071
10072 if (mAttachInfo == null) {
10073 throw new IllegalStateException("This view must be attached to a window first");
10074 }
10075
10076 switch (mLayerType) {
10077 case LAYER_TYPE_HARDWARE:
10078 getHardwareLayer();
10079 break;
10080 case LAYER_TYPE_SOFTWARE:
10081 buildDrawingCache(true);
10082 break;
10083 }
10084 }
10085
10086 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010087 * <p>Returns a hardware layer that can be used to draw this view again
10088 * without executing its draw method.</p>
10089 *
10090 * @return A HardwareLayer ready to render, or null if an error occurred.
10091 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010092 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010093 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10094 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010095 return null;
10096 }
10097
10098 final int width = mRight - mLeft;
10099 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010100
Romain Guy6c319ca2011-01-11 14:29:25 -080010101 if (width == 0 || height == 0) {
10102 return null;
10103 }
10104
10105 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10106 if (mHardwareLayer == null) {
10107 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10108 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010109 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010110 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10111 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010112 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010113 }
10114
Romain Guy59a12ca2011-06-09 17:48:21 -070010115 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010116 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10117 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010118 try {
10119 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010120 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010121 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010122
Chet Haase88172fe2011-03-07 17:36:33 -080010123 final int restoreCount = canvas.save();
10124
Romain Guy6c319ca2011-01-11 14:29:25 -080010125 computeScroll();
10126 canvas.translate(-mScrollX, -mScrollY);
10127
Romain Guy6c319ca2011-01-11 14:29:25 -080010128 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010129
Romain Guy6c319ca2011-01-11 14:29:25 -080010130 // Fast path for layouts with no backgrounds
10131 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10132 mPrivateFlags &= ~DIRTY_MASK;
10133 dispatchDraw(canvas);
10134 } else {
10135 draw(canvas);
10136 }
Joe Malin32736f02011-01-19 16:14:20 -080010137
Chet Haase88172fe2011-03-07 17:36:33 -080010138 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010139 } finally {
10140 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010141 mHardwareLayer.end(currentCanvas);
10142 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010143 }
10144 }
10145
10146 return mHardwareLayer;
10147 }
Romain Guy171c5922011-01-06 10:04:23 -080010148
Romain Guy589b0bb2011-10-10 13:57:47 -070010149 /**
10150 * Destroys this View's hardware layer if possible.
10151 *
10152 * @return True if the layer was destroyed, false otherwise.
10153 *
10154 * @see #setLayerType(int, android.graphics.Paint)
10155 * @see #LAYER_TYPE_HARDWARE
10156 */
Romain Guy65b345f2011-07-27 18:51:50 -070010157 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010158 if (mHardwareLayer != null) {
10159 mHardwareLayer.destroy();
10160 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010161 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010162 }
Romain Guy65b345f2011-07-27 18:51:50 -070010163 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010164 }
10165
Romain Guy171c5922011-01-06 10:04:23 -080010166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10168 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10169 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10170 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10171 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10172 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010173 *
Romain Guy171c5922011-01-06 10:04:23 -080010174 * <p>Enabling the drawing cache is similar to
10175 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010176 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10177 * drawing cache has no effect on rendering because the system uses a different mechanism
10178 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10179 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10180 * for information on how to enable software and hardware layers.</p>
10181 *
10182 * <p>This API can be used to manually generate
10183 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10184 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 *
10186 * @param enabled true to enable the drawing cache, false otherwise
10187 *
10188 * @see #isDrawingCacheEnabled()
10189 * @see #getDrawingCache()
10190 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010191 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 */
10193 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010194 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10196 }
10197
10198 /**
10199 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10200 *
10201 * @return true if the drawing cache is enabled
10202 *
10203 * @see #setDrawingCacheEnabled(boolean)
10204 * @see #getDrawingCache()
10205 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010206 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 public boolean isDrawingCacheEnabled() {
10208 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10209 }
10210
10211 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010212 * Debugging utility which recursively outputs the dirty state of a view and its
10213 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010214 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010215 * @hide
10216 */
Romain Guy676b1732011-02-14 14:45:33 -080010217 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010218 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10219 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10220 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10221 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10222 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10223 if (clear) {
10224 mPrivateFlags &= clearMask;
10225 }
10226 if (this instanceof ViewGroup) {
10227 ViewGroup parent = (ViewGroup) this;
10228 final int count = parent.getChildCount();
10229 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010230 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010231 child.outputDirtyFlags(indent + " ", clear, clearMask);
10232 }
10233 }
10234 }
10235
10236 /**
10237 * This method is used by ViewGroup to cause its children to restore or recreate their
10238 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10239 * to recreate its own display list, which would happen if it went through the normal
10240 * draw/dispatchDraw mechanisms.
10241 *
10242 * @hide
10243 */
10244 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010245
10246 /**
10247 * A view that is not attached or hardware accelerated cannot create a display list.
10248 * This method checks these conditions and returns the appropriate result.
10249 *
10250 * @return true if view has the ability to create a display list, false otherwise.
10251 *
10252 * @hide
10253 */
10254 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010255 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010256 }
Joe Malin32736f02011-01-19 16:14:20 -080010257
Chet Haasedaf98e92011-01-10 14:10:36 -080010258 /**
Romain Guyb051e892010-09-28 19:09:36 -070010259 * <p>Returns a display list that can be used to draw this view again
10260 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010261 *
Romain Guyb051e892010-09-28 19:09:36 -070010262 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010263 *
10264 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010265 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010266 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010267 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010268 return null;
10269 }
10270
Chet Haasedaf98e92011-01-10 14:10:36 -080010271 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10272 mDisplayList == null || !mDisplayList.isValid() ||
10273 mRecreateDisplayList)) {
10274 // Don't need to recreate the display list, just need to tell our
10275 // children to restore/recreate theirs
10276 if (mDisplayList != null && mDisplayList.isValid() &&
10277 !mRecreateDisplayList) {
10278 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10279 mPrivateFlags &= ~DIRTY_MASK;
10280 dispatchGetDisplayList();
10281
10282 return mDisplayList;
10283 }
10284
10285 // If we got here, we're recreating it. Mark it as such to ensure that
10286 // we copy in child display lists into ours in drawChild()
10287 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010288 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010289 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010290 // If we're creating a new display list, make sure our parent gets invalidated
10291 // since they will need to recreate their display list to account for this
10292 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010293 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010294 }
Romain Guyb051e892010-09-28 19:09:36 -070010295
10296 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010297 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010298 try {
10299 int width = mRight - mLeft;
10300 int height = mBottom - mTop;
10301
10302 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010303 // The dirty rect should always be null for a display list
10304 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010305
Chet Haasedaf98e92011-01-10 14:10:36 -080010306 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010307
10308 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010309 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010310 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010311 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010312
Romain Guyb051e892010-09-28 19:09:36 -070010313 // Fast path for layouts with no backgrounds
10314 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010315 dispatchDraw(canvas);
10316 } else {
10317 draw(canvas);
10318 }
Romain Guyb051e892010-09-28 19:09:36 -070010319 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010320 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010321 canvas.onPostDraw();
10322
10323 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010324 }
Chet Haase77785f92011-01-25 23:22:09 -080010325 } else {
10326 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10327 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010328 }
10329
10330 return mDisplayList;
10331 }
10332
10333 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010334 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010335 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010336 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010337 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010338 * @see #getDrawingCache(boolean)
10339 */
10340 public Bitmap getDrawingCache() {
10341 return getDrawingCache(false);
10342 }
10343
10344 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10346 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10347 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10348 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10349 * request the drawing cache by calling this method and draw it on screen if the
10350 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010351 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010352 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10353 * this method will create a bitmap of the same size as this view. Because this bitmap
10354 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10355 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10356 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10357 * size than the view. This implies that your application must be able to handle this
10358 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010359 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010360 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10361 * the current density of the screen when the application is in compatibility
10362 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010364 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 * @see #setDrawingCacheEnabled(boolean)
10367 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010368 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 * @see #destroyDrawingCache()
10370 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010371 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10373 return null;
10374 }
10375 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010376 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 }
Romain Guy02890fd2010-08-06 17:58:44 -070010378 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 }
10380
10381 /**
10382 * <p>Frees the resources used by the drawing cache. If you call
10383 * {@link #buildDrawingCache()} manually without calling
10384 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10385 * should cleanup the cache with this method afterwards.</p>
10386 *
10387 * @see #setDrawingCacheEnabled(boolean)
10388 * @see #buildDrawingCache()
10389 * @see #getDrawingCache()
10390 */
10391 public void destroyDrawingCache() {
10392 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010393 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 mDrawingCache = null;
10395 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010396 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010397 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010398 mUnscaledDrawingCache = null;
10399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 }
10401
10402 /**
10403 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010404 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 * view will always be drawn on top of a solid color.
10406 *
10407 * @param color The background color to use for the drawing cache's bitmap
10408 *
10409 * @see #setDrawingCacheEnabled(boolean)
10410 * @see #buildDrawingCache()
10411 * @see #getDrawingCache()
10412 */
10413 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010414 if (color != mDrawingCacheBackgroundColor) {
10415 mDrawingCacheBackgroundColor = color;
10416 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418 }
10419
10420 /**
10421 * @see #setDrawingCacheBackgroundColor(int)
10422 *
10423 * @return The background color to used for the drawing cache's bitmap
10424 */
10425 public int getDrawingCacheBackgroundColor() {
10426 return mDrawingCacheBackgroundColor;
10427 }
10428
10429 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010430 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010431 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010432 * @see #buildDrawingCache(boolean)
10433 */
10434 public void buildDrawingCache() {
10435 buildDrawingCache(false);
10436 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010437
Romain Guyfbd8f692009-06-26 14:51:58 -070010438 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10440 *
10441 * <p>If you call {@link #buildDrawingCache()} manually without calling
10442 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10443 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010444 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010445 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10446 * this method will create a bitmap of the same size as this view. Because this bitmap
10447 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10448 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10449 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10450 * size than the view. This implies that your application must be able to handle this
10451 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010452 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010453 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10454 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010455 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010456 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 *
10458 * @see #getDrawingCache()
10459 * @see #destroyDrawingCache()
10460 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010461 public void buildDrawingCache(boolean autoScale) {
10462 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010463 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010464 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465
10466 if (ViewDebug.TRACE_HIERARCHY) {
10467 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469
Romain Guy8506ab42009-06-11 17:35:47 -070010470 int width = mRight - mLeft;
10471 int height = mBottom - mTop;
10472
10473 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010474 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010475
Romain Guye1123222009-06-29 14:24:56 -070010476 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010477 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10478 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480
10481 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010482 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010483 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010484
10485 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010486 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010487 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10489 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010490 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 return;
10492 }
10493
10494 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010495 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496
10497 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 Bitmap.Config quality;
10499 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010500 // Never pick ARGB_4444 because it looks awful
10501 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10503 case DRAWING_CACHE_QUALITY_AUTO:
10504 quality = Bitmap.Config.ARGB_8888;
10505 break;
10506 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010507 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 break;
10509 case DRAWING_CACHE_QUALITY_HIGH:
10510 quality = Bitmap.Config.ARGB_8888;
10511 break;
10512 default:
10513 quality = Bitmap.Config.ARGB_8888;
10514 break;
10515 }
10516 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010517 // Optimization for translucent windows
10518 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010519 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 }
10521
10522 // Try to cleanup memory
10523 if (bitmap != null) bitmap.recycle();
10524
10525 try {
10526 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010527 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010528 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010529 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010530 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010531 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010532 }
Adam Powell26153a32010-11-08 15:22:27 -080010533 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 } catch (OutOfMemoryError e) {
10535 // If there is not enough memory to create the bitmap cache, just
10536 // ignore the issue as bitmap caches are not required to draw the
10537 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010538 if (autoScale) {
10539 mDrawingCache = null;
10540 } else {
10541 mUnscaledDrawingCache = null;
10542 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010543 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 return;
10545 }
10546
10547 clear = drawingCacheBackgroundColor != 0;
10548 }
10549
10550 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 if (attachInfo != null) {
10552 canvas = attachInfo.mCanvas;
10553 if (canvas == null) {
10554 canvas = new Canvas();
10555 }
10556 canvas.setBitmap(bitmap);
10557 // Temporarily clobber the cached Canvas in case one of our children
10558 // is also using a drawing cache. Without this, the children would
10559 // steal the canvas by attaching their own bitmap to it and bad, bad
10560 // thing would happen (invisible views, corrupted drawings, etc.)
10561 attachInfo.mCanvas = null;
10562 } else {
10563 // This case should hopefully never or seldom happen
10564 canvas = new Canvas(bitmap);
10565 }
10566
10567 if (clear) {
10568 bitmap.eraseColor(drawingCacheBackgroundColor);
10569 }
10570
10571 computeScroll();
10572 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010573
Romain Guye1123222009-06-29 14:24:56 -070010574 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010575 final float scale = attachInfo.mApplicationScale;
10576 canvas.scale(scale, scale);
10577 }
Joe Malin32736f02011-01-19 16:14:20 -080010578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 canvas.translate(-mScrollX, -mScrollY);
10580
Romain Guy5bcdff42009-05-14 21:27:18 -070010581 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010582 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10583 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010584 mPrivateFlags |= DRAWING_CACHE_VALID;
10585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586
10587 // Fast path for layouts with no backgrounds
10588 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10589 if (ViewDebug.TRACE_HIERARCHY) {
10590 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10591 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010592 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 dispatchDraw(canvas);
10594 } else {
10595 draw(canvas);
10596 }
10597
10598 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010599 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600
10601 if (attachInfo != null) {
10602 // Restore the cached Canvas for our siblings
10603 attachInfo.mCanvas = canvas;
10604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 }
10606 }
10607
10608 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010609 * Create a snapshot of the view into a bitmap. We should probably make
10610 * some form of this public, but should think about the API.
10611 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010612 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010613 int width = mRight - mLeft;
10614 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010615
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010616 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010617 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010618 width = (int) ((width * scale) + 0.5f);
10619 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010620
Romain Guy8c11e312009-09-14 15:15:30 -070010621 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010622 if (bitmap == null) {
10623 throw new OutOfMemoryError();
10624 }
10625
Romain Guyc529d8d2011-09-06 15:01:39 -070010626 Resources resources = getResources();
10627 if (resources != null) {
10628 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10629 }
Joe Malin32736f02011-01-19 16:14:20 -080010630
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010631 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010632 if (attachInfo != null) {
10633 canvas = attachInfo.mCanvas;
10634 if (canvas == null) {
10635 canvas = new Canvas();
10636 }
10637 canvas.setBitmap(bitmap);
10638 // Temporarily clobber the cached Canvas in case one of our children
10639 // is also using a drawing cache. Without this, the children would
10640 // steal the canvas by attaching their own bitmap to it and bad, bad
10641 // things would happen (invisible views, corrupted drawings, etc.)
10642 attachInfo.mCanvas = null;
10643 } else {
10644 // This case should hopefully never or seldom happen
10645 canvas = new Canvas(bitmap);
10646 }
10647
Romain Guy5bcdff42009-05-14 21:27:18 -070010648 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010649 bitmap.eraseColor(backgroundColor);
10650 }
10651
10652 computeScroll();
10653 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010654 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010655 canvas.translate(-mScrollX, -mScrollY);
10656
Romain Guy5bcdff42009-05-14 21:27:18 -070010657 // Temporarily remove the dirty mask
10658 int flags = mPrivateFlags;
10659 mPrivateFlags &= ~DIRTY_MASK;
10660
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010661 // Fast path for layouts with no backgrounds
10662 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10663 dispatchDraw(canvas);
10664 } else {
10665 draw(canvas);
10666 }
10667
Romain Guy5bcdff42009-05-14 21:27:18 -070010668 mPrivateFlags = flags;
10669
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010670 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010671 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010672
10673 if (attachInfo != null) {
10674 // Restore the cached Canvas for our siblings
10675 attachInfo.mCanvas = canvas;
10676 }
Romain Guy8506ab42009-06-11 17:35:47 -070010677
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010678 return bitmap;
10679 }
10680
10681 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 * Indicates whether this View is currently in edit mode. A View is usually
10683 * in edit mode when displayed within a developer tool. For instance, if
10684 * this View is being drawn by a visual user interface builder, this method
10685 * should return true.
10686 *
10687 * Subclasses should check the return value of this method to provide
10688 * different behaviors if their normal behavior might interfere with the
10689 * host environment. For instance: the class spawns a thread in its
10690 * constructor, the drawing code relies on device-specific features, etc.
10691 *
10692 * This method is usually checked in the drawing code of custom widgets.
10693 *
10694 * @return True if this View is in edit mode, false otherwise.
10695 */
10696 public boolean isInEditMode() {
10697 return false;
10698 }
10699
10700 /**
10701 * If the View draws content inside its padding and enables fading edges,
10702 * it needs to support padding offsets. Padding offsets are added to the
10703 * fading edges to extend the length of the fade so that it covers pixels
10704 * drawn inside the padding.
10705 *
10706 * Subclasses of this class should override this method if they need
10707 * to draw content inside the padding.
10708 *
10709 * @return True if padding offset must be applied, false otherwise.
10710 *
10711 * @see #getLeftPaddingOffset()
10712 * @see #getRightPaddingOffset()
10713 * @see #getTopPaddingOffset()
10714 * @see #getBottomPaddingOffset()
10715 *
10716 * @since CURRENT
10717 */
10718 protected boolean isPaddingOffsetRequired() {
10719 return false;
10720 }
10721
10722 /**
10723 * Amount by which to extend the left fading region. Called only when
10724 * {@link #isPaddingOffsetRequired()} returns true.
10725 *
10726 * @return The left padding offset in pixels.
10727 *
10728 * @see #isPaddingOffsetRequired()
10729 *
10730 * @since CURRENT
10731 */
10732 protected int getLeftPaddingOffset() {
10733 return 0;
10734 }
10735
10736 /**
10737 * Amount by which to extend the right fading region. Called only when
10738 * {@link #isPaddingOffsetRequired()} returns true.
10739 *
10740 * @return The right padding offset in pixels.
10741 *
10742 * @see #isPaddingOffsetRequired()
10743 *
10744 * @since CURRENT
10745 */
10746 protected int getRightPaddingOffset() {
10747 return 0;
10748 }
10749
10750 /**
10751 * Amount by which to extend the top fading region. Called only when
10752 * {@link #isPaddingOffsetRequired()} returns true.
10753 *
10754 * @return The top padding offset in pixels.
10755 *
10756 * @see #isPaddingOffsetRequired()
10757 *
10758 * @since CURRENT
10759 */
10760 protected int getTopPaddingOffset() {
10761 return 0;
10762 }
10763
10764 /**
10765 * Amount by which to extend the bottom fading region. Called only when
10766 * {@link #isPaddingOffsetRequired()} returns true.
10767 *
10768 * @return The bottom padding offset in pixels.
10769 *
10770 * @see #isPaddingOffsetRequired()
10771 *
10772 * @since CURRENT
10773 */
10774 protected int getBottomPaddingOffset() {
10775 return 0;
10776 }
10777
10778 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010779 * @hide
10780 * @param offsetRequired
10781 */
10782 protected int getFadeTop(boolean offsetRequired) {
10783 int top = mPaddingTop;
10784 if (offsetRequired) top += getTopPaddingOffset();
10785 return top;
10786 }
10787
10788 /**
10789 * @hide
10790 * @param offsetRequired
10791 */
10792 protected int getFadeHeight(boolean offsetRequired) {
10793 int padding = mPaddingTop;
10794 if (offsetRequired) padding += getTopPaddingOffset();
10795 return mBottom - mTop - mPaddingBottom - padding;
10796 }
10797
10798 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010799 * <p>Indicates whether this view is attached to an hardware accelerated
10800 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010801 *
Romain Guy2bffd262010-09-12 17:40:02 -070010802 * <p>Even if this method returns true, it does not mean that every call
10803 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10804 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10805 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10806 * window is hardware accelerated,
10807 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10808 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010809 *
Romain Guy2bffd262010-09-12 17:40:02 -070010810 * @return True if the view is attached to a window and the window is
10811 * hardware accelerated; false in any other case.
10812 */
10813 public boolean isHardwareAccelerated() {
10814 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10815 }
Joe Malin32736f02011-01-19 16:14:20 -080010816
Romain Guy2bffd262010-09-12 17:40:02 -070010817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818 * Manually render this view (and all of its children) to the given Canvas.
10819 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010820 * called. When implementing a view, implement
10821 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10822 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823 *
10824 * @param canvas The Canvas to which the View is rendered.
10825 */
10826 public void draw(Canvas canvas) {
10827 if (ViewDebug.TRACE_HIERARCHY) {
10828 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10829 }
10830
Romain Guy5bcdff42009-05-14 21:27:18 -070010831 final int privateFlags = mPrivateFlags;
10832 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10833 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10834 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 /*
10837 * Draw traversal performs several drawing steps which must be executed
10838 * in the appropriate order:
10839 *
10840 * 1. Draw the background
10841 * 2. If necessary, save the canvas' layers to prepare for fading
10842 * 3. Draw view's content
10843 * 4. Draw children
10844 * 5. If necessary, draw the fading edges and restore layers
10845 * 6. Draw decorations (scrollbars for instance)
10846 */
10847
10848 // Step 1, draw the background, if needed
10849 int saveCount;
10850
Romain Guy24443ea2009-05-11 11:56:30 -070010851 if (!dirtyOpaque) {
10852 final Drawable background = mBGDrawable;
10853 if (background != null) {
10854 final int scrollX = mScrollX;
10855 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856
Romain Guy24443ea2009-05-11 11:56:30 -070010857 if (mBackgroundSizeChanged) {
10858 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10859 mBackgroundSizeChanged = false;
10860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010861
Romain Guy24443ea2009-05-11 11:56:30 -070010862 if ((scrollX | scrollY) == 0) {
10863 background.draw(canvas);
10864 } else {
10865 canvas.translate(scrollX, scrollY);
10866 background.draw(canvas);
10867 canvas.translate(-scrollX, -scrollY);
10868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 }
10870 }
10871
10872 // skip step 2 & 5 if possible (common case)
10873 final int viewFlags = mViewFlags;
10874 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10875 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10876 if (!verticalEdges && !horizontalEdges) {
10877 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010878 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010879
10880 // Step 4, draw the children
10881 dispatchDraw(canvas);
10882
10883 // Step 6, draw decorations (scrollbars)
10884 onDrawScrollBars(canvas);
10885
10886 // we're done...
10887 return;
10888 }
10889
10890 /*
10891 * Here we do the full fledged routine...
10892 * (this is an uncommon case where speed matters less,
10893 * this is why we repeat some of the tests that have been
10894 * done above)
10895 */
10896
10897 boolean drawTop = false;
10898 boolean drawBottom = false;
10899 boolean drawLeft = false;
10900 boolean drawRight = false;
10901
10902 float topFadeStrength = 0.0f;
10903 float bottomFadeStrength = 0.0f;
10904 float leftFadeStrength = 0.0f;
10905 float rightFadeStrength = 0.0f;
10906
10907 // Step 2, save the canvas' layers
10908 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909
10910 final boolean offsetRequired = isPaddingOffsetRequired();
10911 if (offsetRequired) {
10912 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010913 }
10914
10915 int left = mScrollX + paddingLeft;
10916 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010917 int top = mScrollY + getFadeTop(offsetRequired);
10918 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919
10920 if (offsetRequired) {
10921 right += getRightPaddingOffset();
10922 bottom += getBottomPaddingOffset();
10923 }
10924
10925 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010926 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10927 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928
10929 // clip the fade length if top and bottom fades overlap
10930 // overlapping fades produce odd-looking artifacts
10931 if (verticalEdges && (top + length > bottom - length)) {
10932 length = (bottom - top) / 2;
10933 }
10934
10935 // also clip horizontal fades if necessary
10936 if (horizontalEdges && (left + length > right - length)) {
10937 length = (right - left) / 2;
10938 }
10939
10940 if (verticalEdges) {
10941 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010942 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010944 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010945 }
10946
10947 if (horizontalEdges) {
10948 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010949 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010950 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010951 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 }
10953
10954 saveCount = canvas.getSaveCount();
10955
10956 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010957 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10959
10960 if (drawTop) {
10961 canvas.saveLayer(left, top, right, top + length, null, flags);
10962 }
10963
10964 if (drawBottom) {
10965 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10966 }
10967
10968 if (drawLeft) {
10969 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10970 }
10971
10972 if (drawRight) {
10973 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10974 }
10975 } else {
10976 scrollabilityCache.setFadeColor(solidColor);
10977 }
10978
10979 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010980 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981
10982 // Step 4, draw the children
10983 dispatchDraw(canvas);
10984
10985 // Step 5, draw the fade effect and restore layers
10986 final Paint p = scrollabilityCache.paint;
10987 final Matrix matrix = scrollabilityCache.matrix;
10988 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989
10990 if (drawTop) {
10991 matrix.setScale(1, fadeHeight * topFadeStrength);
10992 matrix.postTranslate(left, top);
10993 fade.setLocalMatrix(matrix);
10994 canvas.drawRect(left, top, right, top + length, p);
10995 }
10996
10997 if (drawBottom) {
10998 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10999 matrix.postRotate(180);
11000 matrix.postTranslate(left, bottom);
11001 fade.setLocalMatrix(matrix);
11002 canvas.drawRect(left, bottom - length, right, bottom, p);
11003 }
11004
11005 if (drawLeft) {
11006 matrix.setScale(1, fadeHeight * leftFadeStrength);
11007 matrix.postRotate(-90);
11008 matrix.postTranslate(left, top);
11009 fade.setLocalMatrix(matrix);
11010 canvas.drawRect(left, top, left + length, bottom, p);
11011 }
11012
11013 if (drawRight) {
11014 matrix.setScale(1, fadeHeight * rightFadeStrength);
11015 matrix.postRotate(90);
11016 matrix.postTranslate(right, top);
11017 fade.setLocalMatrix(matrix);
11018 canvas.drawRect(right - length, top, right, bottom, p);
11019 }
11020
11021 canvas.restoreToCount(saveCount);
11022
11023 // Step 6, draw decorations (scrollbars)
11024 onDrawScrollBars(canvas);
11025 }
11026
11027 /**
11028 * Override this if your view is known to always be drawn on top of a solid color background,
11029 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11030 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11031 * should be set to 0xFF.
11032 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011033 * @see #setVerticalFadingEdgeEnabled(boolean)
11034 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 *
11036 * @return The known solid color background for this view, or 0 if the color may vary
11037 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011038 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011039 public int getSolidColor() {
11040 return 0;
11041 }
11042
11043 /**
11044 * Build a human readable string representation of the specified view flags.
11045 *
11046 * @param flags the view flags to convert to a string
11047 * @return a String representing the supplied flags
11048 */
11049 private static String printFlags(int flags) {
11050 String output = "";
11051 int numFlags = 0;
11052 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11053 output += "TAKES_FOCUS";
11054 numFlags++;
11055 }
11056
11057 switch (flags & VISIBILITY_MASK) {
11058 case INVISIBLE:
11059 if (numFlags > 0) {
11060 output += " ";
11061 }
11062 output += "INVISIBLE";
11063 // USELESS HERE numFlags++;
11064 break;
11065 case GONE:
11066 if (numFlags > 0) {
11067 output += " ";
11068 }
11069 output += "GONE";
11070 // USELESS HERE numFlags++;
11071 break;
11072 default:
11073 break;
11074 }
11075 return output;
11076 }
11077
11078 /**
11079 * Build a human readable string representation of the specified private
11080 * view flags.
11081 *
11082 * @param privateFlags the private view flags to convert to a string
11083 * @return a String representing the supplied flags
11084 */
11085 private static String printPrivateFlags(int privateFlags) {
11086 String output = "";
11087 int numFlags = 0;
11088
11089 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11090 output += "WANTS_FOCUS";
11091 numFlags++;
11092 }
11093
11094 if ((privateFlags & FOCUSED) == FOCUSED) {
11095 if (numFlags > 0) {
11096 output += " ";
11097 }
11098 output += "FOCUSED";
11099 numFlags++;
11100 }
11101
11102 if ((privateFlags & SELECTED) == SELECTED) {
11103 if (numFlags > 0) {
11104 output += " ";
11105 }
11106 output += "SELECTED";
11107 numFlags++;
11108 }
11109
11110 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11111 if (numFlags > 0) {
11112 output += " ";
11113 }
11114 output += "IS_ROOT_NAMESPACE";
11115 numFlags++;
11116 }
11117
11118 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11119 if (numFlags > 0) {
11120 output += " ";
11121 }
11122 output += "HAS_BOUNDS";
11123 numFlags++;
11124 }
11125
11126 if ((privateFlags & DRAWN) == DRAWN) {
11127 if (numFlags > 0) {
11128 output += " ";
11129 }
11130 output += "DRAWN";
11131 // USELESS HERE numFlags++;
11132 }
11133 return output;
11134 }
11135
11136 /**
11137 * <p>Indicates whether or not this view's layout will be requested during
11138 * the next hierarchy layout pass.</p>
11139 *
11140 * @return true if the layout will be forced during next layout pass
11141 */
11142 public boolean isLayoutRequested() {
11143 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11144 }
11145
11146 /**
11147 * Assign a size and position to a view and all of its
11148 * descendants
11149 *
11150 * <p>This is the second phase of the layout mechanism.
11151 * (The first is measuring). In this phase, each parent calls
11152 * layout on all of its children to position them.
11153 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011154 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 *
Chet Haase9c087442011-01-12 16:20:16 -080011156 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 * Derived classes with children should override
11158 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011159 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 *
11161 * @param l Left position, relative to parent
11162 * @param t Top position, relative to parent
11163 * @param r Right position, relative to parent
11164 * @param b Bottom position, relative to parent
11165 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011166 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011167 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011168 int oldL = mLeft;
11169 int oldT = mTop;
11170 int oldB = mBottom;
11171 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 boolean changed = setFrame(l, t, r, b);
11173 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11174 if (ViewDebug.TRACE_HIERARCHY) {
11175 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11176 }
11177
11178 onLayout(changed, l, t, r, b);
11179 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011180
11181 if (mOnLayoutChangeListeners != null) {
11182 ArrayList<OnLayoutChangeListener> listenersCopy =
11183 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11184 int numListeners = listenersCopy.size();
11185 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011186 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011187 }
11188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 }
11190 mPrivateFlags &= ~FORCE_LAYOUT;
11191 }
11192
11193 /**
11194 * Called from layout when this view should
11195 * assign a size and position to each of its children.
11196 *
11197 * Derived classes with children should override
11198 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011199 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011200 * @param changed This is a new size or position for this view
11201 * @param left Left position, relative to parent
11202 * @param top Top position, relative to parent
11203 * @param right Right position, relative to parent
11204 * @param bottom Bottom position, relative to parent
11205 */
11206 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11207 }
11208
11209 /**
11210 * Assign a size and position to this view.
11211 *
11212 * This is called from layout.
11213 *
11214 * @param left Left position, relative to parent
11215 * @param top Top position, relative to parent
11216 * @param right Right position, relative to parent
11217 * @param bottom Bottom position, relative to parent
11218 * @return true if the new size and position are different than the
11219 * previous ones
11220 * {@hide}
11221 */
11222 protected boolean setFrame(int left, int top, int right, int bottom) {
11223 boolean changed = false;
11224
11225 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011226 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011227 + right + "," + bottom + ")");
11228 }
11229
11230 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11231 changed = true;
11232
11233 // Remember our drawn bit
11234 int drawn = mPrivateFlags & DRAWN;
11235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011236 int oldWidth = mRight - mLeft;
11237 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011238 int newWidth = right - left;
11239 int newHeight = bottom - top;
11240 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11241
11242 // Invalidate our old position
11243 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011244
11245 mLeft = left;
11246 mTop = top;
11247 mRight = right;
11248 mBottom = bottom;
11249
11250 mPrivateFlags |= HAS_BOUNDS;
11251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011252
Chet Haase75755e22011-07-18 17:48:25 -070011253 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011254 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11255 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011256 if (mTransformationInfo != null) {
11257 mTransformationInfo.mMatrixDirty = true;
11258 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11261 }
11262
11263 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11264 // If we are visible, force the DRAWN bit to on so that
11265 // this invalidate will go through (at least to our parent).
11266 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011267 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011268 // the DRAWN bit.
11269 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011270 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011271 // parent display list may need to be recreated based on a change in the bounds
11272 // of any child
11273 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 }
11275
11276 // Reset drawn bit to original value (invalidate turns it off)
11277 mPrivateFlags |= drawn;
11278
11279 mBackgroundSizeChanged = true;
11280 }
11281 return changed;
11282 }
11283
11284 /**
11285 * Finalize inflating a view from XML. This is called as the last phase
11286 * of inflation, after all child views have been added.
11287 *
11288 * <p>Even if the subclass overrides onFinishInflate, they should always be
11289 * sure to call the super method, so that we get called.
11290 */
11291 protected void onFinishInflate() {
11292 }
11293
11294 /**
11295 * Returns the resources associated with this view.
11296 *
11297 * @return Resources object.
11298 */
11299 public Resources getResources() {
11300 return mResources;
11301 }
11302
11303 /**
11304 * Invalidates the specified Drawable.
11305 *
11306 * @param drawable the drawable to invalidate
11307 */
11308 public void invalidateDrawable(Drawable drawable) {
11309 if (verifyDrawable(drawable)) {
11310 final Rect dirty = drawable.getBounds();
11311 final int scrollX = mScrollX;
11312 final int scrollY = mScrollY;
11313
11314 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11315 dirty.right + scrollX, dirty.bottom + scrollY);
11316 }
11317 }
11318
11319 /**
11320 * Schedules an action on a drawable to occur at a specified time.
11321 *
11322 * @param who the recipient of the action
11323 * @param what the action to run on the drawable
11324 * @param when the time at which the action must occur. Uses the
11325 * {@link SystemClock#uptimeMillis} timebase.
11326 */
11327 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11328 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11329 mAttachInfo.mHandler.postAtTime(what, who, when);
11330 }
11331 }
11332
11333 /**
11334 * Cancels a scheduled action on a drawable.
11335 *
11336 * @param who the recipient of the action
11337 * @param what the action to cancel
11338 */
11339 public void unscheduleDrawable(Drawable who, Runnable what) {
11340 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11341 mAttachInfo.mHandler.removeCallbacks(what, who);
11342 }
11343 }
11344
11345 /**
11346 * Unschedule any events associated with the given Drawable. This can be
11347 * used when selecting a new Drawable into a view, so that the previous
11348 * one is completely unscheduled.
11349 *
11350 * @param who The Drawable to unschedule.
11351 *
11352 * @see #drawableStateChanged
11353 */
11354 public void unscheduleDrawable(Drawable who) {
11355 if (mAttachInfo != null) {
11356 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11357 }
11358 }
11359
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011360 /**
11361 * Return the layout direction of a given Drawable.
11362 *
11363 * @param who the Drawable to query
11364 *
11365 * @hide
11366 */
11367 public int getResolvedLayoutDirection(Drawable who) {
11368 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011369 }
11370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011371 /**
11372 * If your view subclass is displaying its own Drawable objects, it should
11373 * override this function and return true for any Drawable it is
11374 * displaying. This allows animations for those drawables to be
11375 * scheduled.
11376 *
11377 * <p>Be sure to call through to the super class when overriding this
11378 * function.
11379 *
11380 * @param who The Drawable to verify. Return true if it is one you are
11381 * displaying, else return the result of calling through to the
11382 * super class.
11383 *
11384 * @return boolean If true than the Drawable is being displayed in the
11385 * view; else false and it is not allowed to animate.
11386 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011387 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11388 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011389 */
11390 protected boolean verifyDrawable(Drawable who) {
11391 return who == mBGDrawable;
11392 }
11393
11394 /**
11395 * This function is called whenever the state of the view changes in such
11396 * a way that it impacts the state of drawables being shown.
11397 *
11398 * <p>Be sure to call through to the superclass when overriding this
11399 * function.
11400 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011401 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011402 */
11403 protected void drawableStateChanged() {
11404 Drawable d = mBGDrawable;
11405 if (d != null && d.isStateful()) {
11406 d.setState(getDrawableState());
11407 }
11408 }
11409
11410 /**
11411 * Call this to force a view to update its drawable state. This will cause
11412 * drawableStateChanged to be called on this view. Views that are interested
11413 * in the new state should call getDrawableState.
11414 *
11415 * @see #drawableStateChanged
11416 * @see #getDrawableState
11417 */
11418 public void refreshDrawableState() {
11419 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11420 drawableStateChanged();
11421
11422 ViewParent parent = mParent;
11423 if (parent != null) {
11424 parent.childDrawableStateChanged(this);
11425 }
11426 }
11427
11428 /**
11429 * Return an array of resource IDs of the drawable states representing the
11430 * current state of the view.
11431 *
11432 * @return The current drawable state
11433 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011434 * @see Drawable#setState(int[])
11435 * @see #drawableStateChanged()
11436 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011437 */
11438 public final int[] getDrawableState() {
11439 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11440 return mDrawableState;
11441 } else {
11442 mDrawableState = onCreateDrawableState(0);
11443 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11444 return mDrawableState;
11445 }
11446 }
11447
11448 /**
11449 * Generate the new {@link android.graphics.drawable.Drawable} state for
11450 * this view. This is called by the view
11451 * system when the cached Drawable state is determined to be invalid. To
11452 * retrieve the current state, you should use {@link #getDrawableState}.
11453 *
11454 * @param extraSpace if non-zero, this is the number of extra entries you
11455 * would like in the returned array in which you can place your own
11456 * states.
11457 *
11458 * @return Returns an array holding the current {@link Drawable} state of
11459 * the view.
11460 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011461 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 */
11463 protected int[] onCreateDrawableState(int extraSpace) {
11464 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11465 mParent instanceof View) {
11466 return ((View) mParent).onCreateDrawableState(extraSpace);
11467 }
11468
11469 int[] drawableState;
11470
11471 int privateFlags = mPrivateFlags;
11472
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011473 int viewStateIndex = 0;
11474 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11475 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11476 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011477 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011478 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11479 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011480 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11481 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011482 // This is set if HW acceleration is requested, even if the current
11483 // process doesn't allow it. This is just to allow app preview
11484 // windows to better match their app.
11485 viewStateIndex |= VIEW_STATE_ACCELERATED;
11486 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011487 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011488
Christopher Tate3d4bf172011-03-28 16:16:46 -070011489 final int privateFlags2 = mPrivateFlags2;
11490 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11491 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 drawableState = VIEW_STATE_SETS[viewStateIndex];
11494
11495 //noinspection ConstantIfStatement
11496 if (false) {
11497 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11498 Log.i("View", toString()
11499 + " pressed=" + ((privateFlags & PRESSED) != 0)
11500 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11501 + " fo=" + hasFocus()
11502 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011503 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011504 + ": " + Arrays.toString(drawableState));
11505 }
11506
11507 if (extraSpace == 0) {
11508 return drawableState;
11509 }
11510
11511 final int[] fullState;
11512 if (drawableState != null) {
11513 fullState = new int[drawableState.length + extraSpace];
11514 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11515 } else {
11516 fullState = new int[extraSpace];
11517 }
11518
11519 return fullState;
11520 }
11521
11522 /**
11523 * Merge your own state values in <var>additionalState</var> into the base
11524 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011525 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011526 *
11527 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011528 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529 * own additional state values.
11530 *
11531 * @param additionalState The additional state values you would like
11532 * added to <var>baseState</var>; this array is not modified.
11533 *
11534 * @return As a convenience, the <var>baseState</var> array you originally
11535 * passed into the function is returned.
11536 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011537 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011538 */
11539 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11540 final int N = baseState.length;
11541 int i = N - 1;
11542 while (i >= 0 && baseState[i] == 0) {
11543 i--;
11544 }
11545 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11546 return baseState;
11547 }
11548
11549 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011550 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11551 * on all Drawable objects associated with this view.
11552 */
11553 public void jumpDrawablesToCurrentState() {
11554 if (mBGDrawable != null) {
11555 mBGDrawable.jumpToCurrentState();
11556 }
11557 }
11558
11559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011560 * Sets the background color for this view.
11561 * @param color the color of the background
11562 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011563 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011564 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011565 if (mBGDrawable instanceof ColorDrawable) {
11566 ((ColorDrawable) mBGDrawable).setColor(color);
11567 } else {
11568 setBackgroundDrawable(new ColorDrawable(color));
11569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 }
11571
11572 /**
11573 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011574 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 * @param resid The identifier of the resource.
11576 * @attr ref android.R.styleable#View_background
11577 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011578 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011579 public void setBackgroundResource(int resid) {
11580 if (resid != 0 && resid == mBackgroundResource) {
11581 return;
11582 }
11583
11584 Drawable d= null;
11585 if (resid != 0) {
11586 d = mResources.getDrawable(resid);
11587 }
11588 setBackgroundDrawable(d);
11589
11590 mBackgroundResource = resid;
11591 }
11592
11593 /**
11594 * Set the background to a given Drawable, or remove the background. If the
11595 * background has padding, this View's padding is set to the background's
11596 * padding. However, when a background is removed, this View's padding isn't
11597 * touched. If setting the padding is desired, please use
11598 * {@link #setPadding(int, int, int, int)}.
11599 *
11600 * @param d The Drawable to use as the background, or null to remove the
11601 * background
11602 */
11603 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011604 if (d == mBGDrawable) {
11605 return;
11606 }
11607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011608 boolean requestLayout = false;
11609
11610 mBackgroundResource = 0;
11611
11612 /*
11613 * Regardless of whether we're setting a new background or not, we want
11614 * to clear the previous drawable.
11615 */
11616 if (mBGDrawable != null) {
11617 mBGDrawable.setCallback(null);
11618 unscheduleDrawable(mBGDrawable);
11619 }
11620
11621 if (d != null) {
11622 Rect padding = sThreadLocal.get();
11623 if (padding == null) {
11624 padding = new Rect();
11625 sThreadLocal.set(padding);
11626 }
11627 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011628 switch (d.getResolvedLayoutDirectionSelf()) {
11629 case LAYOUT_DIRECTION_RTL:
11630 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11631 break;
11632 case LAYOUT_DIRECTION_LTR:
11633 default:
11634 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011636 }
11637
11638 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11639 // if it has a different minimum size, we should layout again
11640 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11641 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11642 requestLayout = true;
11643 }
11644
11645 d.setCallback(this);
11646 if (d.isStateful()) {
11647 d.setState(getDrawableState());
11648 }
11649 d.setVisible(getVisibility() == VISIBLE, false);
11650 mBGDrawable = d;
11651
11652 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11653 mPrivateFlags &= ~SKIP_DRAW;
11654 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11655 requestLayout = true;
11656 }
11657 } else {
11658 /* Remove the background */
11659 mBGDrawable = null;
11660
11661 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11662 /*
11663 * This view ONLY drew the background before and we're removing
11664 * the background, so now it won't draw anything
11665 * (hence we SKIP_DRAW)
11666 */
11667 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11668 mPrivateFlags |= SKIP_DRAW;
11669 }
11670
11671 /*
11672 * When the background is set, we try to apply its padding to this
11673 * View. When the background is removed, we don't touch this View's
11674 * padding. This is noted in the Javadocs. Hence, we don't need to
11675 * requestLayout(), the invalidate() below is sufficient.
11676 */
11677
11678 // The old background's minimum size could have affected this
11679 // View's layout, so let's requestLayout
11680 requestLayout = true;
11681 }
11682
Romain Guy8f1344f52009-05-15 16:03:59 -070011683 computeOpaqueFlags();
11684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 if (requestLayout) {
11686 requestLayout();
11687 }
11688
11689 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011690 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011691 }
11692
11693 /**
11694 * Gets the background drawable
11695 * @return The drawable used as the background for this view, if any.
11696 */
11697 public Drawable getBackground() {
11698 return mBGDrawable;
11699 }
11700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 /**
11702 * Sets the padding. The view may add on the space required to display
11703 * the scrollbars, depending on the style and visibility of the scrollbars.
11704 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11705 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11706 * from the values set in this call.
11707 *
11708 * @attr ref android.R.styleable#View_padding
11709 * @attr ref android.R.styleable#View_paddingBottom
11710 * @attr ref android.R.styleable#View_paddingLeft
11711 * @attr ref android.R.styleable#View_paddingRight
11712 * @attr ref android.R.styleable#View_paddingTop
11713 * @param left the left padding in pixels
11714 * @param top the top padding in pixels
11715 * @param right the right padding in pixels
11716 * @param bottom the bottom padding in pixels
11717 */
11718 public void setPadding(int left, int top, int right, int bottom) {
11719 boolean changed = false;
11720
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011721 mUserPaddingRelative = false;
11722
Adam Powell20232d02010-12-08 21:08:53 -080011723 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011724 mUserPaddingRight = right;
11725 mUserPaddingBottom = bottom;
11726
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011727 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011728
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011729 // Common case is there are no scroll bars.
11730 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011731 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011732 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011733 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011734 switch (mVerticalScrollbarPosition) {
11735 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011736 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11737 left += offset;
11738 } else {
11739 right += offset;
11740 }
11741 break;
Adam Powell20232d02010-12-08 21:08:53 -080011742 case SCROLLBAR_POSITION_RIGHT:
11743 right += offset;
11744 break;
11745 case SCROLLBAR_POSITION_LEFT:
11746 left += offset;
11747 break;
11748 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011749 }
Adam Powell20232d02010-12-08 21:08:53 -080011750 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011751 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11752 ? 0 : getHorizontalScrollbarHeight();
11753 }
11754 }
Romain Guy8506ab42009-06-11 17:35:47 -070011755
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011756 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 changed = true;
11758 mPaddingLeft = left;
11759 }
11760 if (mPaddingTop != top) {
11761 changed = true;
11762 mPaddingTop = top;
11763 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011764 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011766 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011767 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011768 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011769 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011770 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011771 }
11772
11773 if (changed) {
11774 requestLayout();
11775 }
11776 }
11777
11778 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011779 * Sets the relative padding. The view may add on the space required to display
11780 * the scrollbars, depending on the style and visibility of the scrollbars.
11781 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11782 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11783 * from the values set in this call.
11784 *
11785 * @attr ref android.R.styleable#View_padding
11786 * @attr ref android.R.styleable#View_paddingBottom
11787 * @attr ref android.R.styleable#View_paddingStart
11788 * @attr ref android.R.styleable#View_paddingEnd
11789 * @attr ref android.R.styleable#View_paddingTop
11790 * @param start the start padding in pixels
11791 * @param top the top padding in pixels
11792 * @param end the end padding in pixels
11793 * @param bottom the bottom padding in pixels
11794 *
11795 * @hide
11796 */
11797 public void setPaddingRelative(int start, int top, int end, int bottom) {
11798 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011799
11800 mUserPaddingStart = start;
11801 mUserPaddingEnd = end;
11802
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011803 switch(getResolvedLayoutDirection()) {
11804 case LAYOUT_DIRECTION_RTL:
11805 setPadding(end, top, start, bottom);
11806 break;
11807 case LAYOUT_DIRECTION_LTR:
11808 default:
11809 setPadding(start, top, end, bottom);
11810 }
11811 }
11812
11813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011814 * Returns the top padding of this view.
11815 *
11816 * @return the top padding in pixels
11817 */
11818 public int getPaddingTop() {
11819 return mPaddingTop;
11820 }
11821
11822 /**
11823 * Returns the bottom padding of this view. If there are inset and enabled
11824 * scrollbars, this value may include the space required to display the
11825 * scrollbars as well.
11826 *
11827 * @return the bottom padding in pixels
11828 */
11829 public int getPaddingBottom() {
11830 return mPaddingBottom;
11831 }
11832
11833 /**
11834 * Returns the left padding of this view. If there are inset and enabled
11835 * scrollbars, this value may include the space required to display the
11836 * scrollbars as well.
11837 *
11838 * @return the left padding in pixels
11839 */
11840 public int getPaddingLeft() {
11841 return mPaddingLeft;
11842 }
11843
11844 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011845 * Returns the start padding of this view. If there are inset and enabled
11846 * scrollbars, this value may include the space required to display the
11847 * scrollbars as well.
11848 *
11849 * @return the start padding in pixels
11850 *
11851 * @hide
11852 */
11853 public int getPaddingStart() {
11854 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11855 mPaddingRight : mPaddingLeft;
11856 }
11857
11858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 * Returns the right padding of this view. If there are inset and enabled
11860 * scrollbars, this value may include the space required to display the
11861 * scrollbars as well.
11862 *
11863 * @return the right padding in pixels
11864 */
11865 public int getPaddingRight() {
11866 return mPaddingRight;
11867 }
11868
11869 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011870 * Returns the end padding of this view. If there are inset and enabled
11871 * scrollbars, this value may include the space required to display the
11872 * scrollbars as well.
11873 *
11874 * @return the end padding in pixels
11875 *
11876 * @hide
11877 */
11878 public int getPaddingEnd() {
11879 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11880 mPaddingLeft : mPaddingRight;
11881 }
11882
11883 /**
11884 * Return if the padding as been set thru relative values
11885 * {@link #setPaddingRelative(int, int, int, int)} or thru
11886 * @attr ref android.R.styleable#View_paddingStart or
11887 * @attr ref android.R.styleable#View_paddingEnd
11888 *
11889 * @return true if the padding is relative or false if it is not.
11890 *
11891 * @hide
11892 */
11893 public boolean isPaddingRelative() {
11894 return mUserPaddingRelative;
11895 }
11896
11897 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898 * Changes the selection state of this view. A view can be selected or not.
11899 * Note that selection is not the same as focus. Views are typically
11900 * selected in the context of an AdapterView like ListView or GridView;
11901 * the selected view is the view that is highlighted.
11902 *
11903 * @param selected true if the view must be selected, false otherwise
11904 */
11905 public void setSelected(boolean selected) {
11906 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11907 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011908 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011909 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 refreshDrawableState();
11911 dispatchSetSelected(selected);
11912 }
11913 }
11914
11915 /**
11916 * Dispatch setSelected to all of this View's children.
11917 *
11918 * @see #setSelected(boolean)
11919 *
11920 * @param selected The new selected state
11921 */
11922 protected void dispatchSetSelected(boolean selected) {
11923 }
11924
11925 /**
11926 * Indicates the selection state of this view.
11927 *
11928 * @return true if the view is selected, false otherwise
11929 */
11930 @ViewDebug.ExportedProperty
11931 public boolean isSelected() {
11932 return (mPrivateFlags & SELECTED) != 0;
11933 }
11934
11935 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011936 * Changes the activated state of this view. A view can be activated or not.
11937 * Note that activation is not the same as selection. Selection is
11938 * a transient property, representing the view (hierarchy) the user is
11939 * currently interacting with. Activation is a longer-term state that the
11940 * user can move views in and out of. For example, in a list view with
11941 * single or multiple selection enabled, the views in the current selection
11942 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11943 * here.) The activated state is propagated down to children of the view it
11944 * is set on.
11945 *
11946 * @param activated true if the view must be activated, false otherwise
11947 */
11948 public void setActivated(boolean activated) {
11949 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11950 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011951 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011952 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011953 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011954 }
11955 }
11956
11957 /**
11958 * Dispatch setActivated to all of this View's children.
11959 *
11960 * @see #setActivated(boolean)
11961 *
11962 * @param activated The new activated state
11963 */
11964 protected void dispatchSetActivated(boolean activated) {
11965 }
11966
11967 /**
11968 * Indicates the activation state of this view.
11969 *
11970 * @return true if the view is activated, false otherwise
11971 */
11972 @ViewDebug.ExportedProperty
11973 public boolean isActivated() {
11974 return (mPrivateFlags & ACTIVATED) != 0;
11975 }
11976
11977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11979 * observer can be used to get notifications when global events, like
11980 * layout, happen.
11981 *
11982 * The returned ViewTreeObserver observer is not guaranteed to remain
11983 * valid for the lifetime of this View. If the caller of this method keeps
11984 * a long-lived reference to ViewTreeObserver, it should always check for
11985 * the return value of {@link ViewTreeObserver#isAlive()}.
11986 *
11987 * @return The ViewTreeObserver for this view's hierarchy.
11988 */
11989 public ViewTreeObserver getViewTreeObserver() {
11990 if (mAttachInfo != null) {
11991 return mAttachInfo.mTreeObserver;
11992 }
11993 if (mFloatingTreeObserver == null) {
11994 mFloatingTreeObserver = new ViewTreeObserver();
11995 }
11996 return mFloatingTreeObserver;
11997 }
11998
11999 /**
12000 * <p>Finds the topmost view in the current view hierarchy.</p>
12001 *
12002 * @return the topmost view containing this view
12003 */
12004 public View getRootView() {
12005 if (mAttachInfo != null) {
12006 final View v = mAttachInfo.mRootView;
12007 if (v != null) {
12008 return v;
12009 }
12010 }
Romain Guy8506ab42009-06-11 17:35:47 -070012011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012012 View parent = this;
12013
12014 while (parent.mParent != null && parent.mParent instanceof View) {
12015 parent = (View) parent.mParent;
12016 }
12017
12018 return parent;
12019 }
12020
12021 /**
12022 * <p>Computes the coordinates of this view on the screen. The argument
12023 * must be an array of two integers. After the method returns, the array
12024 * contains the x and y location in that order.</p>
12025 *
12026 * @param location an array of two integers in which to hold the coordinates
12027 */
12028 public void getLocationOnScreen(int[] location) {
12029 getLocationInWindow(location);
12030
12031 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012032 if (info != null) {
12033 location[0] += info.mWindowLeft;
12034 location[1] += info.mWindowTop;
12035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012036 }
12037
12038 /**
12039 * <p>Computes the coordinates of this view in its window. The argument
12040 * must be an array of two integers. After the method returns, the array
12041 * contains the x and y location in that order.</p>
12042 *
12043 * @param location an array of two integers in which to hold the coordinates
12044 */
12045 public void getLocationInWindow(int[] location) {
12046 if (location == null || location.length < 2) {
12047 throw new IllegalArgumentException("location must be an array of "
12048 + "two integers");
12049 }
12050
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012051 location[0] = mLeft;
12052 location[1] = mTop;
12053 if (mTransformationInfo != null) {
12054 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12055 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012057
12058 ViewParent viewParent = mParent;
12059 while (viewParent instanceof View) {
12060 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012061 location[0] += view.mLeft - view.mScrollX;
12062 location[1] += view.mTop - view.mScrollY;
12063 if (view.mTransformationInfo != null) {
12064 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12065 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012067 viewParent = view.mParent;
12068 }
Romain Guy8506ab42009-06-11 17:35:47 -070012069
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012070 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012071 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012072 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012073 location[1] -= vr.mCurScrollY;
12074 }
12075 }
12076
12077 /**
12078 * {@hide}
12079 * @param id the id of the view to be found
12080 * @return the view of the specified id, null if cannot be found
12081 */
12082 protected View findViewTraversal(int id) {
12083 if (id == mID) {
12084 return this;
12085 }
12086 return null;
12087 }
12088
12089 /**
12090 * {@hide}
12091 * @param tag the tag of the view to be found
12092 * @return the view of specified tag, null if cannot be found
12093 */
12094 protected View findViewWithTagTraversal(Object tag) {
12095 if (tag != null && tag.equals(mTag)) {
12096 return this;
12097 }
12098 return null;
12099 }
12100
12101 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012102 * {@hide}
12103 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012104 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012105 * @return The first view that matches the predicate or null.
12106 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012107 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012108 if (predicate.apply(this)) {
12109 return this;
12110 }
12111 return null;
12112 }
12113
12114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115 * Look for a child view with the given id. If this view has the given
12116 * id, return this view.
12117 *
12118 * @param id The id to search for.
12119 * @return The view that has the given id in the hierarchy or null
12120 */
12121 public final View findViewById(int id) {
12122 if (id < 0) {
12123 return null;
12124 }
12125 return findViewTraversal(id);
12126 }
12127
12128 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012129 * Finds a view by its unuque and stable accessibility id.
12130 *
12131 * @param accessibilityId The searched accessibility id.
12132 * @return The found view.
12133 */
12134 final View findViewByAccessibilityId(int accessibilityId) {
12135 if (accessibilityId < 0) {
12136 return null;
12137 }
12138 return findViewByAccessibilityIdTraversal(accessibilityId);
12139 }
12140
12141 /**
12142 * Performs the traversal to find a view by its unuque and stable accessibility id.
12143 *
12144 * <strong>Note:</strong>This method does not stop at the root namespace
12145 * boundary since the user can touch the screen at an arbitrary location
12146 * potentially crossing the root namespace bounday which will send an
12147 * accessibility event to accessibility services and they should be able
12148 * to obtain the event source. Also accessibility ids are guaranteed to be
12149 * unique in the window.
12150 *
12151 * @param accessibilityId The accessibility id.
12152 * @return The found view.
12153 */
12154 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12155 if (getAccessibilityViewId() == accessibilityId) {
12156 return this;
12157 }
12158 return null;
12159 }
12160
12161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 * Look for a child view with the given tag. If this view has the given
12163 * tag, return this view.
12164 *
12165 * @param tag The tag to search for, using "tag.equals(getTag())".
12166 * @return The View that has the given tag in the hierarchy or null
12167 */
12168 public final View findViewWithTag(Object tag) {
12169 if (tag == null) {
12170 return null;
12171 }
12172 return findViewWithTagTraversal(tag);
12173 }
12174
12175 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012176 * {@hide}
12177 * Look for a child view that matches the specified predicate.
12178 * If this view matches the predicate, return this view.
12179 *
12180 * @param predicate The predicate to evaluate.
12181 * @return The first view that matches the predicate or null.
12182 */
12183 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012184 return findViewByPredicateTraversal(predicate, null);
12185 }
12186
12187 /**
12188 * {@hide}
12189 * Look for a child view that matches the specified predicate,
12190 * starting with the specified view and its descendents and then
12191 * recusively searching the ancestors and siblings of that view
12192 * until this view is reached.
12193 *
12194 * This method is useful in cases where the predicate does not match
12195 * a single unique view (perhaps multiple views use the same id)
12196 * and we are trying to find the view that is "closest" in scope to the
12197 * starting view.
12198 *
12199 * @param start The view to start from.
12200 * @param predicate The predicate to evaluate.
12201 * @return The first view that matches the predicate or null.
12202 */
12203 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12204 View childToSkip = null;
12205 for (;;) {
12206 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12207 if (view != null || start == this) {
12208 return view;
12209 }
12210
12211 ViewParent parent = start.getParent();
12212 if (parent == null || !(parent instanceof View)) {
12213 return null;
12214 }
12215
12216 childToSkip = start;
12217 start = (View) parent;
12218 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012219 }
12220
12221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012222 * Sets the identifier for this view. The identifier does not have to be
12223 * unique in this view's hierarchy. The identifier should be a positive
12224 * number.
12225 *
12226 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012227 * @see #getId()
12228 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012229 *
12230 * @param id a number used to identify the view
12231 *
12232 * @attr ref android.R.styleable#View_id
12233 */
12234 public void setId(int id) {
12235 mID = id;
12236 }
12237
12238 /**
12239 * {@hide}
12240 *
12241 * @param isRoot true if the view belongs to the root namespace, false
12242 * otherwise
12243 */
12244 public void setIsRootNamespace(boolean isRoot) {
12245 if (isRoot) {
12246 mPrivateFlags |= IS_ROOT_NAMESPACE;
12247 } else {
12248 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12249 }
12250 }
12251
12252 /**
12253 * {@hide}
12254 *
12255 * @return true if the view belongs to the root namespace, false otherwise
12256 */
12257 public boolean isRootNamespace() {
12258 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12259 }
12260
12261 /**
12262 * Returns this view's identifier.
12263 *
12264 * @return a positive integer used to identify the view or {@link #NO_ID}
12265 * if the view has no ID
12266 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012267 * @see #setId(int)
12268 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012269 * @attr ref android.R.styleable#View_id
12270 */
12271 @ViewDebug.CapturedViewProperty
12272 public int getId() {
12273 return mID;
12274 }
12275
12276 /**
12277 * Returns this view's tag.
12278 *
12279 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012280 *
12281 * @see #setTag(Object)
12282 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 */
12284 @ViewDebug.ExportedProperty
12285 public Object getTag() {
12286 return mTag;
12287 }
12288
12289 /**
12290 * Sets the tag associated with this view. A tag can be used to mark
12291 * a view in its hierarchy and does not have to be unique within the
12292 * hierarchy. Tags can also be used to store data within a view without
12293 * resorting to another data structure.
12294 *
12295 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012296 *
12297 * @see #getTag()
12298 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012299 */
12300 public void setTag(final Object tag) {
12301 mTag = tag;
12302 }
12303
12304 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012305 * Returns the tag associated with this view and the specified key.
12306 *
12307 * @param key The key identifying the tag
12308 *
12309 * @return the Object stored in this view as a tag
12310 *
12311 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012312 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012313 */
12314 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012315 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012316 return null;
12317 }
12318
12319 /**
12320 * Sets a tag associated with this view and a key. A tag can be used
12321 * to mark a view in its hierarchy and does not have to be unique within
12322 * the hierarchy. Tags can also be used to store data within a view
12323 * without resorting to another data structure.
12324 *
12325 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012326 * application to ensure it is unique (see the <a
12327 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12328 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012329 * the Android framework or not associated with any package will cause
12330 * an {@link IllegalArgumentException} to be thrown.
12331 *
12332 * @param key The key identifying the tag
12333 * @param tag An Object to tag the view with
12334 *
12335 * @throws IllegalArgumentException If they specified key is not valid
12336 *
12337 * @see #setTag(Object)
12338 * @see #getTag(int)
12339 */
12340 public void setTag(int key, final Object tag) {
12341 // If the package id is 0x00 or 0x01, it's either an undefined package
12342 // or a framework id
12343 if ((key >>> 24) < 2) {
12344 throw new IllegalArgumentException("The key must be an application-specific "
12345 + "resource id.");
12346 }
12347
Adam Powell2b2f6d62011-09-23 11:15:39 -070012348 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012349 }
12350
12351 /**
12352 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12353 * framework id.
12354 *
12355 * @hide
12356 */
12357 public void setTagInternal(int key, Object tag) {
12358 if ((key >>> 24) != 0x1) {
12359 throw new IllegalArgumentException("The key must be a framework-specific "
12360 + "resource id.");
12361 }
12362
Adam Powell2b2f6d62011-09-23 11:15:39 -070012363 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012364 }
12365
Adam Powell2b2f6d62011-09-23 11:15:39 -070012366 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012367 if (mKeyedTags == null) {
12368 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012369 }
12370
Adam Powell7db82ac2011-09-22 19:44:04 -070012371 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012372 }
12373
12374 /**
Romain Guy13922e02009-05-12 17:56:14 -070012375 * @param consistency The type of consistency. See ViewDebug for more information.
12376 *
12377 * @hide
12378 */
12379 protected boolean dispatchConsistencyCheck(int consistency) {
12380 return onConsistencyCheck(consistency);
12381 }
12382
12383 /**
12384 * Method that subclasses should implement to check their consistency. The type of
12385 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012386 *
Romain Guy13922e02009-05-12 17:56:14 -070012387 * @param consistency The type of consistency. See ViewDebug for more information.
12388 *
12389 * @throws IllegalStateException if the view is in an inconsistent state.
12390 *
12391 * @hide
12392 */
12393 protected boolean onConsistencyCheck(int consistency) {
12394 boolean result = true;
12395
12396 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12397 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12398
12399 if (checkLayout) {
12400 if (getParent() == null) {
12401 result = false;
12402 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12403 "View " + this + " does not have a parent.");
12404 }
12405
12406 if (mAttachInfo == null) {
12407 result = false;
12408 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12409 "View " + this + " is not attached to a window.");
12410 }
12411 }
12412
12413 if (checkDrawing) {
12414 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12415 // from their draw() method
12416
12417 if ((mPrivateFlags & DRAWN) != DRAWN &&
12418 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12419 result = false;
12420 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12421 "View " + this + " was invalidated but its drawing cache is valid.");
12422 }
12423 }
12424
12425 return result;
12426 }
12427
12428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429 * Prints information about this view in the log output, with the tag
12430 * {@link #VIEW_LOG_TAG}.
12431 *
12432 * @hide
12433 */
12434 public void debug() {
12435 debug(0);
12436 }
12437
12438 /**
12439 * Prints information about this view in the log output, with the tag
12440 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12441 * indentation defined by the <code>depth</code>.
12442 *
12443 * @param depth the indentation level
12444 *
12445 * @hide
12446 */
12447 protected void debug(int depth) {
12448 String output = debugIndent(depth - 1);
12449
12450 output += "+ " + this;
12451 int id = getId();
12452 if (id != -1) {
12453 output += " (id=" + id + ")";
12454 }
12455 Object tag = getTag();
12456 if (tag != null) {
12457 output += " (tag=" + tag + ")";
12458 }
12459 Log.d(VIEW_LOG_TAG, output);
12460
12461 if ((mPrivateFlags & FOCUSED) != 0) {
12462 output = debugIndent(depth) + " FOCUSED";
12463 Log.d(VIEW_LOG_TAG, output);
12464 }
12465
12466 output = debugIndent(depth);
12467 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12468 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12469 + "} ";
12470 Log.d(VIEW_LOG_TAG, output);
12471
12472 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12473 || mPaddingBottom != 0) {
12474 output = debugIndent(depth);
12475 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12476 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12477 Log.d(VIEW_LOG_TAG, output);
12478 }
12479
12480 output = debugIndent(depth);
12481 output += "mMeasureWidth=" + mMeasuredWidth +
12482 " mMeasureHeight=" + mMeasuredHeight;
12483 Log.d(VIEW_LOG_TAG, output);
12484
12485 output = debugIndent(depth);
12486 if (mLayoutParams == null) {
12487 output += "BAD! no layout params";
12488 } else {
12489 output = mLayoutParams.debug(output);
12490 }
12491 Log.d(VIEW_LOG_TAG, output);
12492
12493 output = debugIndent(depth);
12494 output += "flags={";
12495 output += View.printFlags(mViewFlags);
12496 output += "}";
12497 Log.d(VIEW_LOG_TAG, output);
12498
12499 output = debugIndent(depth);
12500 output += "privateFlags={";
12501 output += View.printPrivateFlags(mPrivateFlags);
12502 output += "}";
12503 Log.d(VIEW_LOG_TAG, output);
12504 }
12505
12506 /**
12507 * Creates an string of whitespaces used for indentation.
12508 *
12509 * @param depth the indentation level
12510 * @return a String containing (depth * 2 + 3) * 2 white spaces
12511 *
12512 * @hide
12513 */
12514 protected static String debugIndent(int depth) {
12515 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12516 for (int i = 0; i < (depth * 2) + 3; i++) {
12517 spaces.append(' ').append(' ');
12518 }
12519 return spaces.toString();
12520 }
12521
12522 /**
12523 * <p>Return the offset of the widget's text baseline from the widget's top
12524 * boundary. If this widget does not support baseline alignment, this
12525 * method returns -1. </p>
12526 *
12527 * @return the offset of the baseline within the widget's bounds or -1
12528 * if baseline alignment is not supported
12529 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012530 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 public int getBaseline() {
12532 return -1;
12533 }
12534
12535 /**
12536 * Call this when something has changed which has invalidated the
12537 * layout of this view. This will schedule a layout pass of the view
12538 * tree.
12539 */
12540 public void requestLayout() {
12541 if (ViewDebug.TRACE_HIERARCHY) {
12542 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12543 }
12544
12545 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012546 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012547
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012548 if (mParent != null) {
12549 if (mLayoutParams != null) {
12550 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12551 }
12552 if (!mParent.isLayoutRequested()) {
12553 mParent.requestLayout();
12554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012555 }
12556 }
12557
12558 /**
12559 * Forces this view to be laid out during the next layout pass.
12560 * This method does not call requestLayout() or forceLayout()
12561 * on the parent.
12562 */
12563 public void forceLayout() {
12564 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012565 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012566 }
12567
12568 /**
12569 * <p>
12570 * This is called to find out how big a view should be. The parent
12571 * supplies constraint information in the width and height parameters.
12572 * </p>
12573 *
12574 * <p>
12575 * The actual mesurement work of a view is performed in
12576 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12577 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12578 * </p>
12579 *
12580 *
12581 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12582 * parent
12583 * @param heightMeasureSpec Vertical space requirements as imposed by the
12584 * parent
12585 *
12586 * @see #onMeasure(int, int)
12587 */
12588 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12589 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12590 widthMeasureSpec != mOldWidthMeasureSpec ||
12591 heightMeasureSpec != mOldHeightMeasureSpec) {
12592
12593 // first clears the measured dimension flag
12594 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12595
12596 if (ViewDebug.TRACE_HIERARCHY) {
12597 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12598 }
12599
12600 // measure ourselves, this should set the measured dimension flag back
12601 onMeasure(widthMeasureSpec, heightMeasureSpec);
12602
12603 // flag not set, setMeasuredDimension() was not invoked, we raise
12604 // an exception to warn the developer
12605 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12606 throw new IllegalStateException("onMeasure() did not set the"
12607 + " measured dimension by calling"
12608 + " setMeasuredDimension()");
12609 }
12610
12611 mPrivateFlags |= LAYOUT_REQUIRED;
12612 }
12613
12614 mOldWidthMeasureSpec = widthMeasureSpec;
12615 mOldHeightMeasureSpec = heightMeasureSpec;
12616 }
12617
12618 /**
12619 * <p>
12620 * Measure the view and its content to determine the measured width and the
12621 * measured height. This method is invoked by {@link #measure(int, int)} and
12622 * should be overriden by subclasses to provide accurate and efficient
12623 * measurement of their contents.
12624 * </p>
12625 *
12626 * <p>
12627 * <strong>CONTRACT:</strong> When overriding this method, you
12628 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12629 * measured width and height of this view. Failure to do so will trigger an
12630 * <code>IllegalStateException</code>, thrown by
12631 * {@link #measure(int, int)}. Calling the superclass'
12632 * {@link #onMeasure(int, int)} is a valid use.
12633 * </p>
12634 *
12635 * <p>
12636 * The base class implementation of measure defaults to the background size,
12637 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12638 * override {@link #onMeasure(int, int)} to provide better measurements of
12639 * their content.
12640 * </p>
12641 *
12642 * <p>
12643 * If this method is overridden, it is the subclass's responsibility to make
12644 * sure the measured height and width are at least the view's minimum height
12645 * and width ({@link #getSuggestedMinimumHeight()} and
12646 * {@link #getSuggestedMinimumWidth()}).
12647 * </p>
12648 *
12649 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12650 * The requirements are encoded with
12651 * {@link android.view.View.MeasureSpec}.
12652 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12653 * The requirements are encoded with
12654 * {@link android.view.View.MeasureSpec}.
12655 *
12656 * @see #getMeasuredWidth()
12657 * @see #getMeasuredHeight()
12658 * @see #setMeasuredDimension(int, int)
12659 * @see #getSuggestedMinimumHeight()
12660 * @see #getSuggestedMinimumWidth()
12661 * @see android.view.View.MeasureSpec#getMode(int)
12662 * @see android.view.View.MeasureSpec#getSize(int)
12663 */
12664 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12665 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12666 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12667 }
12668
12669 /**
12670 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12671 * measured width and measured height. Failing to do so will trigger an
12672 * exception at measurement time.</p>
12673 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012674 * @param measuredWidth The measured width of this view. May be a complex
12675 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12676 * {@link #MEASURED_STATE_TOO_SMALL}.
12677 * @param measuredHeight The measured height of this view. May be a complex
12678 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12679 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012680 */
12681 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12682 mMeasuredWidth = measuredWidth;
12683 mMeasuredHeight = measuredHeight;
12684
12685 mPrivateFlags |= MEASURED_DIMENSION_SET;
12686 }
12687
12688 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012689 * Merge two states as returned by {@link #getMeasuredState()}.
12690 * @param curState The current state as returned from a view or the result
12691 * of combining multiple views.
12692 * @param newState The new view state to combine.
12693 * @return Returns a new integer reflecting the combination of the two
12694 * states.
12695 */
12696 public static int combineMeasuredStates(int curState, int newState) {
12697 return curState | newState;
12698 }
12699
12700 /**
12701 * Version of {@link #resolveSizeAndState(int, int, int)}
12702 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12703 */
12704 public static int resolveSize(int size, int measureSpec) {
12705 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12706 }
12707
12708 /**
12709 * Utility to reconcile a desired size and state, with constraints imposed
12710 * by a MeasureSpec. Will take the desired size, unless a different size
12711 * is imposed by the constraints. The returned value is a compound integer,
12712 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12713 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12714 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012715 *
12716 * @param size How big the view wants to be
12717 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012718 * @return Size information bit mask as defined by
12719 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012720 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012721 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722 int result = size;
12723 int specMode = MeasureSpec.getMode(measureSpec);
12724 int specSize = MeasureSpec.getSize(measureSpec);
12725 switch (specMode) {
12726 case MeasureSpec.UNSPECIFIED:
12727 result = size;
12728 break;
12729 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012730 if (specSize < size) {
12731 result = specSize | MEASURED_STATE_TOO_SMALL;
12732 } else {
12733 result = size;
12734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 break;
12736 case MeasureSpec.EXACTLY:
12737 result = specSize;
12738 break;
12739 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012740 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 }
12742
12743 /**
12744 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012745 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012746 * by the MeasureSpec.
12747 *
12748 * @param size Default size for this view
12749 * @param measureSpec Constraints imposed by the parent
12750 * @return The size this view should be.
12751 */
12752 public static int getDefaultSize(int size, int measureSpec) {
12753 int result = size;
12754 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012755 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756
12757 switch (specMode) {
12758 case MeasureSpec.UNSPECIFIED:
12759 result = size;
12760 break;
12761 case MeasureSpec.AT_MOST:
12762 case MeasureSpec.EXACTLY:
12763 result = specSize;
12764 break;
12765 }
12766 return result;
12767 }
12768
12769 /**
12770 * Returns the suggested minimum height that the view should use. This
12771 * returns the maximum of the view's minimum height
12772 * and the background's minimum height
12773 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12774 * <p>
12775 * When being used in {@link #onMeasure(int, int)}, the caller should still
12776 * ensure the returned height is within the requirements of the parent.
12777 *
12778 * @return The suggested minimum height of the view.
12779 */
12780 protected int getSuggestedMinimumHeight() {
12781 int suggestedMinHeight = mMinHeight;
12782
12783 if (mBGDrawable != null) {
12784 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12785 if (suggestedMinHeight < bgMinHeight) {
12786 suggestedMinHeight = bgMinHeight;
12787 }
12788 }
12789
12790 return suggestedMinHeight;
12791 }
12792
12793 /**
12794 * Returns the suggested minimum width that the view should use. This
12795 * returns the maximum of the view's minimum width)
12796 * and the background's minimum width
12797 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12798 * <p>
12799 * When being used in {@link #onMeasure(int, int)}, the caller should still
12800 * ensure the returned width is within the requirements of the parent.
12801 *
12802 * @return The suggested minimum width of the view.
12803 */
12804 protected int getSuggestedMinimumWidth() {
12805 int suggestedMinWidth = mMinWidth;
12806
12807 if (mBGDrawable != null) {
12808 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12809 if (suggestedMinWidth < bgMinWidth) {
12810 suggestedMinWidth = bgMinWidth;
12811 }
12812 }
12813
12814 return suggestedMinWidth;
12815 }
12816
12817 /**
12818 * Sets the minimum height of the view. It is not guaranteed the view will
12819 * be able to achieve this minimum height (for example, if its parent layout
12820 * constrains it with less available height).
12821 *
12822 * @param minHeight The minimum height the view will try to be.
12823 */
12824 public void setMinimumHeight(int minHeight) {
12825 mMinHeight = minHeight;
12826 }
12827
12828 /**
12829 * Sets the minimum width of the view. It is not guaranteed the view will
12830 * be able to achieve this minimum width (for example, if its parent layout
12831 * constrains it with less available width).
12832 *
12833 * @param minWidth The minimum width the view will try to be.
12834 */
12835 public void setMinimumWidth(int minWidth) {
12836 mMinWidth = minWidth;
12837 }
12838
12839 /**
12840 * Get the animation currently associated with this view.
12841 *
12842 * @return The animation that is currently playing or
12843 * scheduled to play for this view.
12844 */
12845 public Animation getAnimation() {
12846 return mCurrentAnimation;
12847 }
12848
12849 /**
12850 * Start the specified animation now.
12851 *
12852 * @param animation the animation to start now
12853 */
12854 public void startAnimation(Animation animation) {
12855 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12856 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012857 invalidateParentCaches();
12858 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859 }
12860
12861 /**
12862 * Cancels any animations for this view.
12863 */
12864 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012865 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012866 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012868 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012869 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012870 }
12871
12872 /**
12873 * Sets the next animation to play for this view.
12874 * If you want the animation to play immediately, use
12875 * startAnimation. This method provides allows fine-grained
12876 * control over the start time and invalidation, but you
12877 * must make sure that 1) the animation has a start time set, and
12878 * 2) the view will be invalidated when the animation is supposed to
12879 * start.
12880 *
12881 * @param animation The next animation, or null.
12882 */
12883 public void setAnimation(Animation animation) {
12884 mCurrentAnimation = animation;
12885 if (animation != null) {
12886 animation.reset();
12887 }
12888 }
12889
12890 /**
12891 * Invoked by a parent ViewGroup to notify the start of the animation
12892 * currently associated with this view. If you override this method,
12893 * always call super.onAnimationStart();
12894 *
12895 * @see #setAnimation(android.view.animation.Animation)
12896 * @see #getAnimation()
12897 */
12898 protected void onAnimationStart() {
12899 mPrivateFlags |= ANIMATION_STARTED;
12900 }
12901
12902 /**
12903 * Invoked by a parent ViewGroup to notify the end of the animation
12904 * currently associated with this view. If you override this method,
12905 * always call super.onAnimationEnd();
12906 *
12907 * @see #setAnimation(android.view.animation.Animation)
12908 * @see #getAnimation()
12909 */
12910 protected void onAnimationEnd() {
12911 mPrivateFlags &= ~ANIMATION_STARTED;
12912 }
12913
12914 /**
12915 * Invoked if there is a Transform that involves alpha. Subclass that can
12916 * draw themselves with the specified alpha should return true, and then
12917 * respect that alpha when their onDraw() is called. If this returns false
12918 * then the view may be redirected to draw into an offscreen buffer to
12919 * fulfill the request, which will look fine, but may be slower than if the
12920 * subclass handles it internally. The default implementation returns false.
12921 *
12922 * @param alpha The alpha (0..255) to apply to the view's drawing
12923 * @return true if the view can draw with the specified alpha.
12924 */
12925 protected boolean onSetAlpha(int alpha) {
12926 return false;
12927 }
12928
12929 /**
12930 * This is used by the RootView to perform an optimization when
12931 * the view hierarchy contains one or several SurfaceView.
12932 * SurfaceView is always considered transparent, but its children are not,
12933 * therefore all View objects remove themselves from the global transparent
12934 * region (passed as a parameter to this function).
12935 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012936 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 *
12938 * @return Returns true if the effective visibility of the view at this
12939 * point is opaque, regardless of the transparent region; returns false
12940 * if it is possible for underlying windows to be seen behind the view.
12941 *
12942 * {@hide}
12943 */
12944 public boolean gatherTransparentRegion(Region region) {
12945 final AttachInfo attachInfo = mAttachInfo;
12946 if (region != null && attachInfo != null) {
12947 final int pflags = mPrivateFlags;
12948 if ((pflags & SKIP_DRAW) == 0) {
12949 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12950 // remove it from the transparent region.
12951 final int[] location = attachInfo.mTransparentLocation;
12952 getLocationInWindow(location);
12953 region.op(location[0], location[1], location[0] + mRight - mLeft,
12954 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12955 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12956 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12957 // exists, so we remove the background drawable's non-transparent
12958 // parts from this transparent region.
12959 applyDrawableToTransparentRegion(mBGDrawable, region);
12960 }
12961 }
12962 return true;
12963 }
12964
12965 /**
12966 * Play a sound effect for this view.
12967 *
12968 * <p>The framework will play sound effects for some built in actions, such as
12969 * clicking, but you may wish to play these effects in your widget,
12970 * for instance, for internal navigation.
12971 *
12972 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12973 * {@link #isSoundEffectsEnabled()} is true.
12974 *
12975 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12976 */
12977 public void playSoundEffect(int soundConstant) {
12978 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12979 return;
12980 }
12981 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12982 }
12983
12984 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012985 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012986 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012987 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012988 *
12989 * <p>The framework will provide haptic feedback for some built in actions,
12990 * such as long presses, but you may wish to provide feedback for your
12991 * own widget.
12992 *
12993 * <p>The feedback will only be performed if
12994 * {@link #isHapticFeedbackEnabled()} is true.
12995 *
12996 * @param feedbackConstant One of the constants defined in
12997 * {@link HapticFeedbackConstants}
12998 */
12999 public boolean performHapticFeedback(int feedbackConstant) {
13000 return performHapticFeedback(feedbackConstant, 0);
13001 }
13002
13003 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013004 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013005 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013006 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013007 *
13008 * @param feedbackConstant One of the constants defined in
13009 * {@link HapticFeedbackConstants}
13010 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13011 */
13012 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13013 if (mAttachInfo == null) {
13014 return false;
13015 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013016 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013017 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013018 && !isHapticFeedbackEnabled()) {
13019 return false;
13020 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013021 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13022 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013023 }
13024
13025 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013026 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013027 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13028 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013029 */
13030 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013031 if (visibility != mSystemUiVisibility) {
13032 mSystemUiVisibility = visibility;
13033 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13034 mParent.recomputeViewAttributes(this);
13035 }
Joe Onorato664644d2011-01-23 17:53:23 -080013036 }
13037 }
13038
13039 /**
13040 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013041 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13042 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013043 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013044 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013045 return mSystemUiVisibility;
13046 }
13047
Scott Mainec6331b2011-05-24 16:55:56 -070013048 /**
13049 * Set a listener to receive callbacks when the visibility of the system bar changes.
13050 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13051 */
Joe Onorato664644d2011-01-23 17:53:23 -080013052 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
13053 mOnSystemUiVisibilityChangeListener = l;
13054 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13055 mParent.recomputeViewAttributes(this);
13056 }
13057 }
13058
13059 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013060 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13061 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013062 */
13063 public void dispatchSystemUiVisibilityChanged(int visibility) {
13064 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080013065 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013066 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013067 }
13068 }
13069
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013070 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13071 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13072 if (val != mSystemUiVisibility) {
13073 setSystemUiVisibility(val);
13074 }
13075 }
13076
Joe Onorato664644d2011-01-23 17:53:23 -080013077 /**
Joe Malin32736f02011-01-19 16:14:20 -080013078 * Creates an image that the system displays during the drag and drop
13079 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13080 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13081 * appearance as the given View. The default also positions the center of the drag shadow
13082 * directly under the touch point. If no View is provided (the constructor with no parameters
13083 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13084 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13085 * default is an invisible drag shadow.
13086 * <p>
13087 * You are not required to use the View you provide to the constructor as the basis of the
13088 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13089 * anything you want as the drag shadow.
13090 * </p>
13091 * <p>
13092 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13093 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13094 * size and position of the drag shadow. It uses this data to construct a
13095 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13096 * so that your application can draw the shadow image in the Canvas.
13097 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013098 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013099 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013100 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013101
13102 /**
Joe Malin32736f02011-01-19 16:14:20 -080013103 * Constructs a shadow image builder based on a View. By default, the resulting drag
13104 * shadow will have the same appearance and dimensions as the View, with the touch point
13105 * over the center of the View.
13106 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013107 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013108 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013109 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013110 }
13111
Christopher Tate17ed60c2011-01-18 12:50:26 -080013112 /**
13113 * Construct a shadow builder object with no associated View. This
13114 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13115 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13116 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013117 * reference to any View object. If they are not overridden, then the result is an
13118 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013119 */
13120 public DragShadowBuilder() {
13121 mView = new WeakReference<View>(null);
13122 }
13123
13124 /**
13125 * Returns the View object that had been passed to the
13126 * {@link #View.DragShadowBuilder(View)}
13127 * constructor. If that View parameter was {@code null} or if the
13128 * {@link #View.DragShadowBuilder()}
13129 * constructor was used to instantiate the builder object, this method will return
13130 * null.
13131 *
13132 * @return The View object associate with this builder object.
13133 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013134 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013135 final public View getView() {
13136 return mView.get();
13137 }
13138
Christopher Tate2c095f32010-10-04 14:13:40 -070013139 /**
Joe Malin32736f02011-01-19 16:14:20 -080013140 * Provides the metrics for the shadow image. These include the dimensions of
13141 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013142 * be centered under the touch location while dragging.
13143 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013144 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013145 * same as the dimensions of the View itself and centers the shadow under
13146 * the touch point.
13147 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013148 *
Joe Malin32736f02011-01-19 16:14:20 -080013149 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13150 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13151 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13152 * image.
13153 *
13154 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13155 * shadow image that should be underneath the touch point during the drag and drop
13156 * operation. Your application must set {@link android.graphics.Point#x} to the
13157 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013158 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013159 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013160 final View view = mView.get();
13161 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013162 shadowSize.set(view.getWidth(), view.getHeight());
13163 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013164 } else {
13165 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13166 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013167 }
13168
13169 /**
Joe Malin32736f02011-01-19 16:14:20 -080013170 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13171 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013172 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013173 *
Joe Malin32736f02011-01-19 16:14:20 -080013174 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013175 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013176 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013177 final View view = mView.get();
13178 if (view != null) {
13179 view.draw(canvas);
13180 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013181 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013182 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013183 }
13184 }
13185
13186 /**
Joe Malin32736f02011-01-19 16:14:20 -080013187 * Starts a drag and drop operation. When your application calls this method, it passes a
13188 * {@link android.view.View.DragShadowBuilder} object to the system. The
13189 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13190 * to get metrics for the drag shadow, and then calls the object's
13191 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13192 * <p>
13193 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13194 * drag events to all the View objects in your application that are currently visible. It does
13195 * this either by calling the View object's drag listener (an implementation of
13196 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13197 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13198 * Both are passed a {@link android.view.DragEvent} object that has a
13199 * {@link android.view.DragEvent#getAction()} value of
13200 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13201 * </p>
13202 * <p>
13203 * Your application can invoke startDrag() on any attached View object. The View object does not
13204 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13205 * be related to the View the user selected for dragging.
13206 * </p>
13207 * @param data A {@link android.content.ClipData} object pointing to the data to be
13208 * transferred by the drag and drop operation.
13209 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13210 * drag shadow.
13211 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13212 * drop operation. This Object is put into every DragEvent object sent by the system during the
13213 * current drag.
13214 * <p>
13215 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13216 * to the target Views. For example, it can contain flags that differentiate between a
13217 * a copy operation and a move operation.
13218 * </p>
13219 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13220 * so the parameter should be set to 0.
13221 * @return {@code true} if the method completes successfully, or
13222 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13223 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013224 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013225 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013226 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013227 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013228 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013229 }
13230 boolean okay = false;
13231
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013232 Point shadowSize = new Point();
13233 Point shadowTouchPoint = new Point();
13234 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013235
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013236 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13237 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13238 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013239 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013240
Chris Tatea32dcf72010-10-14 12:13:50 -070013241 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013242 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13243 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013244 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013245 Surface surface = new Surface();
13246 try {
13247 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013248 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013249 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013250 + " surface=" + surface);
13251 if (token != null) {
13252 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013253 try {
Chris Tate6b391282010-10-14 15:48:59 -070013254 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013255 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013256 } finally {
13257 surface.unlockCanvasAndPost(canvas);
13258 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013259
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013260 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013261
13262 // Cache the local state object for delivery with DragEvents
13263 root.setLocalDragState(myLocalState);
13264
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013265 // repurpose 'shadowSize' for the last touch point
13266 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013267
Christopher Tatea53146c2010-09-07 11:57:52 -070013268 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013269 shadowSize.x, shadowSize.y,
13270 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013271 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013272
13273 // Off and running! Release our local surface instance; the drag
13274 // shadow surface is now managed by the system process.
13275 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013276 }
13277 } catch (Exception e) {
13278 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13279 surface.destroy();
13280 }
13281
13282 return okay;
13283 }
13284
Christopher Tatea53146c2010-09-07 11:57:52 -070013285 /**
Joe Malin32736f02011-01-19 16:14:20 -080013286 * Handles drag events sent by the system following a call to
13287 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13288 *<p>
13289 * When the system calls this method, it passes a
13290 * {@link android.view.DragEvent} object. A call to
13291 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13292 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13293 * operation.
13294 * @param event The {@link android.view.DragEvent} sent by the system.
13295 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13296 * in DragEvent, indicating the type of drag event represented by this object.
13297 * @return {@code true} if the method was successful, otherwise {@code false}.
13298 * <p>
13299 * The method should return {@code true} in response to an action type of
13300 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13301 * operation.
13302 * </p>
13303 * <p>
13304 * The method should also return {@code true} in response to an action type of
13305 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13306 * {@code false} if it didn't.
13307 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013308 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013309 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013310 return false;
13311 }
13312
13313 /**
Joe Malin32736f02011-01-19 16:14:20 -080013314 * Detects if this View is enabled and has a drag event listener.
13315 * If both are true, then it calls the drag event listener with the
13316 * {@link android.view.DragEvent} it received. If the drag event listener returns
13317 * {@code true}, then dispatchDragEvent() returns {@code true}.
13318 * <p>
13319 * For all other cases, the method calls the
13320 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13321 * method and returns its result.
13322 * </p>
13323 * <p>
13324 * This ensures that a drag event is always consumed, even if the View does not have a drag
13325 * event listener. However, if the View has a listener and the listener returns true, then
13326 * onDragEvent() is not called.
13327 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013328 */
13329 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013330 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013331 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13332 && mOnDragListener.onDrag(this, event)) {
13333 return true;
13334 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013335 return onDragEvent(event);
13336 }
13337
Christopher Tate3d4bf172011-03-28 16:16:46 -070013338 boolean canAcceptDrag() {
13339 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13340 }
13341
Christopher Tatea53146c2010-09-07 11:57:52 -070013342 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013343 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13344 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013345 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013346 */
13347 public void onCloseSystemDialogs(String reason) {
13348 }
Joe Malin32736f02011-01-19 16:14:20 -080013349
Dianne Hackbornffa42482009-09-23 22:20:11 -070013350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013351 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013352 * update a Region being computed for
13353 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013354 * that any non-transparent parts of the Drawable are removed from the
13355 * given transparent region.
13356 *
13357 * @param dr The Drawable whose transparency is to be applied to the region.
13358 * @param region A Region holding the current transparency information,
13359 * where any parts of the region that are set are considered to be
13360 * transparent. On return, this region will be modified to have the
13361 * transparency information reduced by the corresponding parts of the
13362 * Drawable that are not transparent.
13363 * {@hide}
13364 */
13365 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13366 if (DBG) {
13367 Log.i("View", "Getting transparent region for: " + this);
13368 }
13369 final Region r = dr.getTransparentRegion();
13370 final Rect db = dr.getBounds();
13371 final AttachInfo attachInfo = mAttachInfo;
13372 if (r != null && attachInfo != null) {
13373 final int w = getRight()-getLeft();
13374 final int h = getBottom()-getTop();
13375 if (db.left > 0) {
13376 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13377 r.op(0, 0, db.left, h, Region.Op.UNION);
13378 }
13379 if (db.right < w) {
13380 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13381 r.op(db.right, 0, w, h, Region.Op.UNION);
13382 }
13383 if (db.top > 0) {
13384 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13385 r.op(0, 0, w, db.top, Region.Op.UNION);
13386 }
13387 if (db.bottom < h) {
13388 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13389 r.op(0, db.bottom, w, h, Region.Op.UNION);
13390 }
13391 final int[] location = attachInfo.mTransparentLocation;
13392 getLocationInWindow(location);
13393 r.translate(location[0], location[1]);
13394 region.op(r, Region.Op.INTERSECT);
13395 } else {
13396 region.op(db, Region.Op.DIFFERENCE);
13397 }
13398 }
13399
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013400 private void checkForLongClick(int delayOffset) {
13401 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13402 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013403
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013404 if (mPendingCheckForLongPress == null) {
13405 mPendingCheckForLongPress = new CheckForLongPress();
13406 }
13407 mPendingCheckForLongPress.rememberWindowAttachCount();
13408 postDelayed(mPendingCheckForLongPress,
13409 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 }
13412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013413 /**
13414 * Inflate a view from an XML resource. This convenience method wraps the {@link
13415 * LayoutInflater} class, which provides a full range of options for view inflation.
13416 *
13417 * @param context The Context object for your activity or application.
13418 * @param resource The resource ID to inflate
13419 * @param root A view group that will be the parent. Used to properly inflate the
13420 * layout_* parameters.
13421 * @see LayoutInflater
13422 */
13423 public static View inflate(Context context, int resource, ViewGroup root) {
13424 LayoutInflater factory = LayoutInflater.from(context);
13425 return factory.inflate(resource, root);
13426 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013428 /**
Adam Powell637d3372010-08-25 14:37:03 -070013429 * Scroll the view with standard behavior for scrolling beyond the normal
13430 * content boundaries. Views that call this method should override
13431 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13432 * results of an over-scroll operation.
13433 *
13434 * Views can use this method to handle any touch or fling-based scrolling.
13435 *
13436 * @param deltaX Change in X in pixels
13437 * @param deltaY Change in Y in pixels
13438 * @param scrollX Current X scroll value in pixels before applying deltaX
13439 * @param scrollY Current Y scroll value in pixels before applying deltaY
13440 * @param scrollRangeX Maximum content scroll range along the X axis
13441 * @param scrollRangeY Maximum content scroll range along the Y axis
13442 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13443 * along the X axis.
13444 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13445 * along the Y axis.
13446 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13447 * @return true if scrolling was clamped to an over-scroll boundary along either
13448 * axis, false otherwise.
13449 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013450 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013451 protected boolean overScrollBy(int deltaX, int deltaY,
13452 int scrollX, int scrollY,
13453 int scrollRangeX, int scrollRangeY,
13454 int maxOverScrollX, int maxOverScrollY,
13455 boolean isTouchEvent) {
13456 final int overScrollMode = mOverScrollMode;
13457 final boolean canScrollHorizontal =
13458 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13459 final boolean canScrollVertical =
13460 computeVerticalScrollRange() > computeVerticalScrollExtent();
13461 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13462 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13463 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13464 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13465
13466 int newScrollX = scrollX + deltaX;
13467 if (!overScrollHorizontal) {
13468 maxOverScrollX = 0;
13469 }
13470
13471 int newScrollY = scrollY + deltaY;
13472 if (!overScrollVertical) {
13473 maxOverScrollY = 0;
13474 }
13475
13476 // Clamp values if at the limits and record
13477 final int left = -maxOverScrollX;
13478 final int right = maxOverScrollX + scrollRangeX;
13479 final int top = -maxOverScrollY;
13480 final int bottom = maxOverScrollY + scrollRangeY;
13481
13482 boolean clampedX = false;
13483 if (newScrollX > right) {
13484 newScrollX = right;
13485 clampedX = true;
13486 } else if (newScrollX < left) {
13487 newScrollX = left;
13488 clampedX = true;
13489 }
13490
13491 boolean clampedY = false;
13492 if (newScrollY > bottom) {
13493 newScrollY = bottom;
13494 clampedY = true;
13495 } else if (newScrollY < top) {
13496 newScrollY = top;
13497 clampedY = true;
13498 }
13499
13500 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13501
13502 return clampedX || clampedY;
13503 }
13504
13505 /**
13506 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13507 * respond to the results of an over-scroll operation.
13508 *
13509 * @param scrollX New X scroll value in pixels
13510 * @param scrollY New Y scroll value in pixels
13511 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13512 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13513 */
13514 protected void onOverScrolled(int scrollX, int scrollY,
13515 boolean clampedX, boolean clampedY) {
13516 // Intentionally empty.
13517 }
13518
13519 /**
13520 * Returns the over-scroll mode for this view. The result will be
13521 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13522 * (allow over-scrolling only if the view content is larger than the container),
13523 * or {@link #OVER_SCROLL_NEVER}.
13524 *
13525 * @return This view's over-scroll mode.
13526 */
13527 public int getOverScrollMode() {
13528 return mOverScrollMode;
13529 }
13530
13531 /**
13532 * Set the over-scroll mode for this view. Valid over-scroll modes are
13533 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13534 * (allow over-scrolling only if the view content is larger than the container),
13535 * or {@link #OVER_SCROLL_NEVER}.
13536 *
13537 * Setting the over-scroll mode of a view will have an effect only if the
13538 * view is capable of scrolling.
13539 *
13540 * @param overScrollMode The new over-scroll mode for this view.
13541 */
13542 public void setOverScrollMode(int overScrollMode) {
13543 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13544 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13545 overScrollMode != OVER_SCROLL_NEVER) {
13546 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13547 }
13548 mOverScrollMode = overScrollMode;
13549 }
13550
13551 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013552 * Gets a scale factor that determines the distance the view should scroll
13553 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13554 * @return The vertical scroll scale factor.
13555 * @hide
13556 */
13557 protected float getVerticalScrollFactor() {
13558 if (mVerticalScrollFactor == 0) {
13559 TypedValue outValue = new TypedValue();
13560 if (!mContext.getTheme().resolveAttribute(
13561 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13562 throw new IllegalStateException(
13563 "Expected theme to define listPreferredItemHeight.");
13564 }
13565 mVerticalScrollFactor = outValue.getDimension(
13566 mContext.getResources().getDisplayMetrics());
13567 }
13568 return mVerticalScrollFactor;
13569 }
13570
13571 /**
13572 * Gets a scale factor that determines the distance the view should scroll
13573 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13574 * @return The horizontal scroll scale factor.
13575 * @hide
13576 */
13577 protected float getHorizontalScrollFactor() {
13578 // TODO: Should use something else.
13579 return getVerticalScrollFactor();
13580 }
13581
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013582 /**
13583 * Return the value specifying the text direction or policy that was set with
13584 * {@link #setTextDirection(int)}.
13585 *
13586 * @return the defined text direction. It can be one of:
13587 *
13588 * {@link #TEXT_DIRECTION_INHERIT},
13589 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13590 * {@link #TEXT_DIRECTION_ANY_RTL},
13591 * {@link #TEXT_DIRECTION_LTR},
13592 * {@link #TEXT_DIRECTION_RTL},
13593 *
13594 * @hide
13595 */
13596 public int getTextDirection() {
13597 return mTextDirection;
13598 }
13599
13600 /**
13601 * Set the text direction.
13602 *
13603 * @param textDirection the direction to set. Should be one of:
13604 *
13605 * {@link #TEXT_DIRECTION_INHERIT},
13606 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13607 * {@link #TEXT_DIRECTION_ANY_RTL},
13608 * {@link #TEXT_DIRECTION_LTR},
13609 * {@link #TEXT_DIRECTION_RTL},
13610 *
13611 * @hide
13612 */
13613 public void setTextDirection(int textDirection) {
13614 if (textDirection != mTextDirection) {
13615 mTextDirection = textDirection;
13616 resetResolvedTextDirection();
13617 requestLayout();
13618 }
13619 }
13620
13621 /**
13622 * Return the resolved text direction.
13623 *
13624 * @return the resolved text direction. Return one of:
13625 *
Doug Feltcb3791202011-07-07 11:57:48 -070013626 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13627 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013628 * {@link #TEXT_DIRECTION_LTR},
13629 * {@link #TEXT_DIRECTION_RTL},
13630 *
13631 * @hide
13632 */
13633 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013634 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013635 resolveTextDirection();
13636 }
13637 return mResolvedTextDirection;
13638 }
13639
13640 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013641 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013642 *
13643 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013644 */
13645 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013646 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13647 mResolvedTextDirection = mTextDirection;
13648 return;
13649 }
13650 if (mParent != null && mParent instanceof ViewGroup) {
13651 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13652 return;
13653 }
13654 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013655 }
13656
13657 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013658 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013659 *
13660 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013661 */
13662 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013663 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013664 }
13665
Chet Haaseb39f0512011-05-24 14:36:40 -070013666 //
13667 // Properties
13668 //
13669 /**
13670 * A Property wrapper around the <code>alpha</code> functionality handled by the
13671 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13672 */
Romain Guy02ccac62011-06-24 13:20:23 -070013673 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013674 @Override
13675 public void setValue(View object, float value) {
13676 object.setAlpha(value);
13677 }
13678
13679 @Override
13680 public Float get(View object) {
13681 return object.getAlpha();
13682 }
13683 };
13684
13685 /**
13686 * A Property wrapper around the <code>translationX</code> functionality handled by the
13687 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13688 */
13689 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13690 @Override
13691 public void setValue(View object, float value) {
13692 object.setTranslationX(value);
13693 }
13694
13695 @Override
13696 public Float get(View object) {
13697 return object.getTranslationX();
13698 }
13699 };
13700
13701 /**
13702 * A Property wrapper around the <code>translationY</code> functionality handled by the
13703 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13704 */
13705 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13706 @Override
13707 public void setValue(View object, float value) {
13708 object.setTranslationY(value);
13709 }
13710
13711 @Override
13712 public Float get(View object) {
13713 return object.getTranslationY();
13714 }
13715 };
13716
13717 /**
13718 * A Property wrapper around the <code>x</code> functionality handled by the
13719 * {@link View#setX(float)} and {@link View#getX()} methods.
13720 */
13721 public static Property<View, Float> X = new FloatProperty<View>("x") {
13722 @Override
13723 public void setValue(View object, float value) {
13724 object.setX(value);
13725 }
13726
13727 @Override
13728 public Float get(View object) {
13729 return object.getX();
13730 }
13731 };
13732
13733 /**
13734 * A Property wrapper around the <code>y</code> functionality handled by the
13735 * {@link View#setY(float)} and {@link View#getY()} methods.
13736 */
13737 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13738 @Override
13739 public void setValue(View object, float value) {
13740 object.setY(value);
13741 }
13742
13743 @Override
13744 public Float get(View object) {
13745 return object.getY();
13746 }
13747 };
13748
13749 /**
13750 * A Property wrapper around the <code>rotation</code> functionality handled by the
13751 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13752 */
13753 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13754 @Override
13755 public void setValue(View object, float value) {
13756 object.setRotation(value);
13757 }
13758
13759 @Override
13760 public Float get(View object) {
13761 return object.getRotation();
13762 }
13763 };
13764
13765 /**
13766 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13767 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13768 */
13769 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13770 @Override
13771 public void setValue(View object, float value) {
13772 object.setRotationX(value);
13773 }
13774
13775 @Override
13776 public Float get(View object) {
13777 return object.getRotationX();
13778 }
13779 };
13780
13781 /**
13782 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13783 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13784 */
13785 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13786 @Override
13787 public void setValue(View object, float value) {
13788 object.setRotationY(value);
13789 }
13790
13791 @Override
13792 public Float get(View object) {
13793 return object.getRotationY();
13794 }
13795 };
13796
13797 /**
13798 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13799 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13800 */
13801 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13802 @Override
13803 public void setValue(View object, float value) {
13804 object.setScaleX(value);
13805 }
13806
13807 @Override
13808 public Float get(View object) {
13809 return object.getScaleX();
13810 }
13811 };
13812
13813 /**
13814 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13815 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13816 */
13817 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13818 @Override
13819 public void setValue(View object, float value) {
13820 object.setScaleY(value);
13821 }
13822
13823 @Override
13824 public Float get(View object) {
13825 return object.getScaleY();
13826 }
13827 };
13828
Jeff Brown33bbfd22011-02-24 20:55:35 -080013829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013830 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13831 * Each MeasureSpec represents a requirement for either the width or the height.
13832 * A MeasureSpec is comprised of a size and a mode. There are three possible
13833 * modes:
13834 * <dl>
13835 * <dt>UNSPECIFIED</dt>
13836 * <dd>
13837 * The parent has not imposed any constraint on the child. It can be whatever size
13838 * it wants.
13839 * </dd>
13840 *
13841 * <dt>EXACTLY</dt>
13842 * <dd>
13843 * The parent has determined an exact size for the child. The child is going to be
13844 * given those bounds regardless of how big it wants to be.
13845 * </dd>
13846 *
13847 * <dt>AT_MOST</dt>
13848 * <dd>
13849 * The child can be as large as it wants up to the specified size.
13850 * </dd>
13851 * </dl>
13852 *
13853 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13854 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13855 */
13856 public static class MeasureSpec {
13857 private static final int MODE_SHIFT = 30;
13858 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13859
13860 /**
13861 * Measure specification mode: The parent has not imposed any constraint
13862 * on the child. It can be whatever size it wants.
13863 */
13864 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13865
13866 /**
13867 * Measure specification mode: The parent has determined an exact size
13868 * for the child. The child is going to be given those bounds regardless
13869 * of how big it wants to be.
13870 */
13871 public static final int EXACTLY = 1 << MODE_SHIFT;
13872
13873 /**
13874 * Measure specification mode: The child can be as large as it wants up
13875 * to the specified size.
13876 */
13877 public static final int AT_MOST = 2 << MODE_SHIFT;
13878
13879 /**
13880 * Creates a measure specification based on the supplied size and mode.
13881 *
13882 * The mode must always be one of the following:
13883 * <ul>
13884 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13885 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13886 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13887 * </ul>
13888 *
13889 * @param size the size of the measure specification
13890 * @param mode the mode of the measure specification
13891 * @return the measure specification based on size and mode
13892 */
13893 public static int makeMeasureSpec(int size, int mode) {
13894 return size + mode;
13895 }
13896
13897 /**
13898 * Extracts the mode from the supplied measure specification.
13899 *
13900 * @param measureSpec the measure specification to extract the mode from
13901 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13902 * {@link android.view.View.MeasureSpec#AT_MOST} or
13903 * {@link android.view.View.MeasureSpec#EXACTLY}
13904 */
13905 public static int getMode(int measureSpec) {
13906 return (measureSpec & MODE_MASK);
13907 }
13908
13909 /**
13910 * Extracts the size from the supplied measure specification.
13911 *
13912 * @param measureSpec the measure specification to extract the size from
13913 * @return the size in pixels defined in the supplied measure specification
13914 */
13915 public static int getSize(int measureSpec) {
13916 return (measureSpec & ~MODE_MASK);
13917 }
13918
13919 /**
13920 * Returns a String representation of the specified measure
13921 * specification.
13922 *
13923 * @param measureSpec the measure specification to convert to a String
13924 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13925 */
13926 public static String toString(int measureSpec) {
13927 int mode = getMode(measureSpec);
13928 int size = getSize(measureSpec);
13929
13930 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13931
13932 if (mode == UNSPECIFIED)
13933 sb.append("UNSPECIFIED ");
13934 else if (mode == EXACTLY)
13935 sb.append("EXACTLY ");
13936 else if (mode == AT_MOST)
13937 sb.append("AT_MOST ");
13938 else
13939 sb.append(mode).append(" ");
13940
13941 sb.append(size);
13942 return sb.toString();
13943 }
13944 }
13945
13946 class CheckForLongPress implements Runnable {
13947
13948 private int mOriginalWindowAttachCount;
13949
13950 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013951 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013952 && mOriginalWindowAttachCount == mWindowAttachCount) {
13953 if (performLongClick()) {
13954 mHasPerformedLongPress = true;
13955 }
13956 }
13957 }
13958
13959 public void rememberWindowAttachCount() {
13960 mOriginalWindowAttachCount = mWindowAttachCount;
13961 }
13962 }
Joe Malin32736f02011-01-19 16:14:20 -080013963
Adam Powelle14579b2009-12-16 18:39:52 -080013964 private final class CheckForTap implements Runnable {
13965 public void run() {
13966 mPrivateFlags &= ~PREPRESSED;
13967 mPrivateFlags |= PRESSED;
13968 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013969 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013970 }
13971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972
Adam Powella35d7682010-03-12 14:48:13 -080013973 private final class PerformClick implements Runnable {
13974 public void run() {
13975 performClick();
13976 }
13977 }
13978
Dianne Hackborn63042d62011-01-26 18:56:29 -080013979 /** @hide */
13980 public void hackTurnOffWindowResizeAnim(boolean off) {
13981 mAttachInfo.mTurnOffWindowResizeAnim = off;
13982 }
Joe Malin32736f02011-01-19 16:14:20 -080013983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013984 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013985 * This method returns a ViewPropertyAnimator object, which can be used to animate
13986 * specific properties on this View.
13987 *
13988 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13989 */
13990 public ViewPropertyAnimator animate() {
13991 if (mAnimator == null) {
13992 mAnimator = new ViewPropertyAnimator(this);
13993 }
13994 return mAnimator;
13995 }
13996
13997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013998 * Interface definition for a callback to be invoked when a key event is
13999 * dispatched to this view. The callback will be invoked before the key
14000 * event is given to the view.
14001 */
14002 public interface OnKeyListener {
14003 /**
14004 * Called when a key 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 key has been dispatched to.
14008 * @param keyCode The code for the physical key that was pressed
14009 * @param event The KeyEvent object containing full information about
14010 * the event.
14011 * @return True if the listener has consumed the event, false otherwise.
14012 */
14013 boolean onKey(View v, int keyCode, KeyEvent event);
14014 }
14015
14016 /**
14017 * Interface definition for a callback to be invoked when a touch event is
14018 * dispatched to this view. The callback will be invoked before the touch
14019 * event is given to the view.
14020 */
14021 public interface OnTouchListener {
14022 /**
14023 * Called when a touch event is dispatched to a view. This allows listeners to
14024 * get a chance to respond before the target view.
14025 *
14026 * @param v The view the touch event has been dispatched to.
14027 * @param event The MotionEvent object containing full information about
14028 * the event.
14029 * @return True if the listener has consumed the event, false otherwise.
14030 */
14031 boolean onTouch(View v, MotionEvent event);
14032 }
14033
14034 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014035 * Interface definition for a callback to be invoked when a hover event is
14036 * dispatched to this view. The callback will be invoked before the hover
14037 * event is given to the view.
14038 */
14039 public interface OnHoverListener {
14040 /**
14041 * Called when a hover event is dispatched to a view. This allows listeners to
14042 * get a chance to respond before the target view.
14043 *
14044 * @param v The view the hover event has been dispatched to.
14045 * @param event The MotionEvent object containing full information about
14046 * the event.
14047 * @return True if the listener has consumed the event, false otherwise.
14048 */
14049 boolean onHover(View v, MotionEvent event);
14050 }
14051
14052 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014053 * Interface definition for a callback to be invoked when a generic motion event is
14054 * dispatched to this view. The callback will be invoked before the generic motion
14055 * event is given to the view.
14056 */
14057 public interface OnGenericMotionListener {
14058 /**
14059 * Called when a generic motion event is dispatched to a view. This allows listeners to
14060 * get a chance to respond before the target view.
14061 *
14062 * @param v The view the generic motion event has been dispatched to.
14063 * @param event The MotionEvent object containing full information about
14064 * the event.
14065 * @return True if the listener has consumed the event, false otherwise.
14066 */
14067 boolean onGenericMotion(View v, MotionEvent event);
14068 }
14069
14070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014071 * Interface definition for a callback to be invoked when a view has been clicked and held.
14072 */
14073 public interface OnLongClickListener {
14074 /**
14075 * Called when a view has been clicked and held.
14076 *
14077 * @param v The view that was clicked and held.
14078 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014079 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 */
14081 boolean onLongClick(View v);
14082 }
14083
14084 /**
Chris Tate32affef2010-10-18 15:29:21 -070014085 * Interface definition for a callback to be invoked when a drag is being dispatched
14086 * to this view. The callback will be invoked before the hosting view's own
14087 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14088 * onDrag(event) behavior, it should return 'false' from this callback.
14089 */
14090 public interface OnDragListener {
14091 /**
14092 * Called when a drag event is dispatched to a view. This allows listeners
14093 * to get a chance to override base View behavior.
14094 *
Joe Malin32736f02011-01-19 16:14:20 -080014095 * @param v The View that received the drag event.
14096 * @param event The {@link android.view.DragEvent} object for the drag event.
14097 * @return {@code true} if the drag event was handled successfully, or {@code false}
14098 * if the drag event was not handled. Note that {@code false} will trigger the View
14099 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014100 */
14101 boolean onDrag(View v, DragEvent event);
14102 }
14103
14104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105 * Interface definition for a callback to be invoked when the focus state of
14106 * a view changed.
14107 */
14108 public interface OnFocusChangeListener {
14109 /**
14110 * Called when the focus state of a view has changed.
14111 *
14112 * @param v The view whose state has changed.
14113 * @param hasFocus The new focus state of v.
14114 */
14115 void onFocusChange(View v, boolean hasFocus);
14116 }
14117
14118 /**
14119 * Interface definition for a callback to be invoked when a view is clicked.
14120 */
14121 public interface OnClickListener {
14122 /**
14123 * Called when a view has been clicked.
14124 *
14125 * @param v The view that was clicked.
14126 */
14127 void onClick(View v);
14128 }
14129
14130 /**
14131 * Interface definition for a callback to be invoked when the context menu
14132 * for this view is being built.
14133 */
14134 public interface OnCreateContextMenuListener {
14135 /**
14136 * Called when the context menu for this view is being built. It is not
14137 * safe to hold onto the menu after this method returns.
14138 *
14139 * @param menu The context menu that is being built
14140 * @param v The view for which the context menu is being built
14141 * @param menuInfo Extra information about the item for which the
14142 * context menu should be shown. This information will vary
14143 * depending on the class of v.
14144 */
14145 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14146 }
14147
Joe Onorato664644d2011-01-23 17:53:23 -080014148 /**
14149 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014150 * visibility. This reports <strong>global</strong> changes to the system UI
14151 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014152 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014153 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014154 */
14155 public interface OnSystemUiVisibilityChangeListener {
14156 /**
14157 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014158 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014159 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014160 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014161 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14162 * <strong>global</strong> state of the UI visibility flags, not what your
14163 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014164 */
14165 public void onSystemUiVisibilityChange(int visibility);
14166 }
14167
Adam Powell4afd62b2011-02-18 15:02:18 -080014168 /**
14169 * Interface definition for a callback to be invoked when this view is attached
14170 * or detached from its window.
14171 */
14172 public interface OnAttachStateChangeListener {
14173 /**
14174 * Called when the view is attached to a window.
14175 * @param v The view that was attached
14176 */
14177 public void onViewAttachedToWindow(View v);
14178 /**
14179 * Called when the view is detached from a window.
14180 * @param v The view that was detached
14181 */
14182 public void onViewDetachedFromWindow(View v);
14183 }
14184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 private final class UnsetPressedState implements Runnable {
14186 public void run() {
14187 setPressed(false);
14188 }
14189 }
14190
14191 /**
14192 * Base class for derived classes that want to save and restore their own
14193 * state in {@link android.view.View#onSaveInstanceState()}.
14194 */
14195 public static class BaseSavedState extends AbsSavedState {
14196 /**
14197 * Constructor used when reading from a parcel. Reads the state of the superclass.
14198 *
14199 * @param source
14200 */
14201 public BaseSavedState(Parcel source) {
14202 super(source);
14203 }
14204
14205 /**
14206 * Constructor called by derived classes when creating their SavedState objects
14207 *
14208 * @param superState The state of the superclass of this view
14209 */
14210 public BaseSavedState(Parcelable superState) {
14211 super(superState);
14212 }
14213
14214 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14215 new Parcelable.Creator<BaseSavedState>() {
14216 public BaseSavedState createFromParcel(Parcel in) {
14217 return new BaseSavedState(in);
14218 }
14219
14220 public BaseSavedState[] newArray(int size) {
14221 return new BaseSavedState[size];
14222 }
14223 };
14224 }
14225
14226 /**
14227 * A set of information given to a view when it is attached to its parent
14228 * window.
14229 */
14230 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014231 interface Callbacks {
14232 void playSoundEffect(int effectId);
14233 boolean performHapticFeedback(int effectId, boolean always);
14234 }
14235
14236 /**
14237 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14238 * to a Handler. This class contains the target (View) to invalidate and
14239 * the coordinates of the dirty rectangle.
14240 *
14241 * For performance purposes, this class also implements a pool of up to
14242 * POOL_LIMIT objects that get reused. This reduces memory allocations
14243 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014244 */
Romain Guyd928d682009-03-31 17:52:16 -070014245 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014247 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14248 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014249 public InvalidateInfo newInstance() {
14250 return new InvalidateInfo();
14251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014252
Romain Guyd928d682009-03-31 17:52:16 -070014253 public void onAcquired(InvalidateInfo element) {
14254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255
Romain Guyd928d682009-03-31 17:52:16 -070014256 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014257 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014258 }
14259 }, POOL_LIMIT)
14260 );
14261
14262 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014263 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014264
14265 View target;
14266
14267 int left;
14268 int top;
14269 int right;
14270 int bottom;
14271
Romain Guyd928d682009-03-31 17:52:16 -070014272 public void setNextPoolable(InvalidateInfo element) {
14273 mNext = element;
14274 }
14275
14276 public InvalidateInfo getNextPoolable() {
14277 return mNext;
14278 }
14279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014280 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014281 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282 }
14283
14284 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014285 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014286 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014287
14288 public boolean isPooled() {
14289 return mIsPooled;
14290 }
14291
14292 public void setPooled(boolean isPooled) {
14293 mIsPooled = isPooled;
14294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 }
14296
14297 final IWindowSession mSession;
14298
14299 final IWindow mWindow;
14300
14301 final IBinder mWindowToken;
14302
14303 final Callbacks mRootCallbacks;
14304
Romain Guy59a12ca2011-06-09 17:48:21 -070014305 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014307 /**
14308 * The top view of the hierarchy.
14309 */
14310 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 IBinder mPanelParentWindowToken;
14313 Surface mSurface;
14314
Romain Guyb051e892010-09-28 19:09:36 -070014315 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014316 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014317 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014319 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014320 * Scale factor used by the compatibility mode
14321 */
14322 float mApplicationScale;
14323
14324 /**
14325 * Indicates whether the application is in compatibility mode
14326 */
14327 boolean mScalingRequired;
14328
14329 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014330 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014331 */
14332 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014333
Dianne Hackborn63042d62011-01-26 18:56:29 -080014334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014335 * Left position of this view's window
14336 */
14337 int mWindowLeft;
14338
14339 /**
14340 * Top position of this view's window
14341 */
14342 int mWindowTop;
14343
14344 /**
Adam Powell26153a32010-11-08 15:22:27 -080014345 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014346 */
Adam Powell26153a32010-11-08 15:22:27 -080014347 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014348
14349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014350 * For windows that are full-screen but using insets to layout inside
14351 * of the screen decorations, these are the current insets for the
14352 * content of the window.
14353 */
14354 final Rect mContentInsets = new Rect();
14355
14356 /**
14357 * For windows that are full-screen but using insets to layout inside
14358 * of the screen decorations, these are the current insets for the
14359 * actual visible parts of the window.
14360 */
14361 final Rect mVisibleInsets = new Rect();
14362
14363 /**
14364 * The internal insets given by this window. This value is
14365 * supplied by the client (through
14366 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14367 * be given to the window manager when changed to be used in laying
14368 * out windows behind it.
14369 */
14370 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14371 = new ViewTreeObserver.InternalInsetsInfo();
14372
14373 /**
14374 * All views in the window's hierarchy that serve as scroll containers,
14375 * used to determine if the window can be resized or must be panned
14376 * to adjust for a soft input area.
14377 */
14378 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14379
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014380 final KeyEvent.DispatcherState mKeyDispatchState
14381 = new KeyEvent.DispatcherState();
14382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014383 /**
14384 * Indicates whether the view's window currently has the focus.
14385 */
14386 boolean mHasWindowFocus;
14387
14388 /**
14389 * The current visibility of the window.
14390 */
14391 int mWindowVisibility;
14392
14393 /**
14394 * Indicates the time at which drawing started to occur.
14395 */
14396 long mDrawingTime;
14397
14398 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014399 * Indicates whether or not ignoring the DIRTY_MASK flags.
14400 */
14401 boolean mIgnoreDirtyState;
14402
14403 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014404 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14405 * to avoid clearing that flag prematurely.
14406 */
14407 boolean mSetIgnoreDirtyState = false;
14408
14409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014410 * Indicates whether the view's window is currently in touch mode.
14411 */
14412 boolean mInTouchMode;
14413
14414 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014415 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014416 * the next time it performs a traversal
14417 */
14418 boolean mRecomputeGlobalAttributes;
14419
14420 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014421 * Always report new attributes at next traversal.
14422 */
14423 boolean mForceReportNewAttributes;
14424
14425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 * Set during a traveral if any views want to keep the screen on.
14427 */
14428 boolean mKeepScreenOn;
14429
14430 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014431 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14432 */
14433 int mSystemUiVisibility;
14434
14435 /**
14436 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14437 * attached.
14438 */
14439 boolean mHasSystemUiListeners;
14440
14441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014442 * Set if the visibility of any views has changed.
14443 */
14444 boolean mViewVisibilityChanged;
14445
14446 /**
14447 * Set to true if a view has been scrolled.
14448 */
14449 boolean mViewScrollChanged;
14450
14451 /**
14452 * Global to the view hierarchy used as a temporary for dealing with
14453 * x/y points in the transparent region computations.
14454 */
14455 final int[] mTransparentLocation = new int[2];
14456
14457 /**
14458 * Global to the view hierarchy used as a temporary for dealing with
14459 * x/y points in the ViewGroup.invalidateChild implementation.
14460 */
14461 final int[] mInvalidateChildLocation = new int[2];
14462
Chet Haasec3aa3612010-06-17 08:50:37 -070014463
14464 /**
14465 * Global to the view hierarchy used as a temporary for dealing with
14466 * x/y location when view is transformed.
14467 */
14468 final float[] mTmpTransformLocation = new float[2];
14469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014470 /**
14471 * The view tree observer used to dispatch global events like
14472 * layout, pre-draw, touch mode change, etc.
14473 */
14474 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14475
14476 /**
14477 * A Canvas used by the view hierarchy to perform bitmap caching.
14478 */
14479 Canvas mCanvas;
14480
14481 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014482 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014483 * handler can be used to pump events in the UI events queue.
14484 */
14485 final Handler mHandler;
14486
14487 /**
14488 * Identifier for messages requesting the view to be invalidated.
14489 * Such messages should be sent to {@link #mHandler}.
14490 */
14491 static final int INVALIDATE_MSG = 0x1;
14492
14493 /**
14494 * Identifier for messages requesting the view to invalidate a region.
14495 * Such messages should be sent to {@link #mHandler}.
14496 */
14497 static final int INVALIDATE_RECT_MSG = 0x2;
14498
14499 /**
14500 * Temporary for use in computing invalidate rectangles while
14501 * calling up the hierarchy.
14502 */
14503 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014504
14505 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014506 * Temporary for use in computing hit areas with transformed views
14507 */
14508 final RectF mTmpTransformRect = new RectF();
14509
14510 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014511 * Temporary list for use in collecting focusable descendents of a view.
14512 */
14513 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014515 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014516 * The id of the window for accessibility purposes.
14517 */
14518 int mAccessibilityWindowId = View.NO_ID;
14519
14520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014521 * Creates a new set of attachment information with the specified
14522 * events handler and thread.
14523 *
14524 * @param handler the events handler the view must use
14525 */
14526 AttachInfo(IWindowSession session, IWindow window,
14527 Handler handler, Callbacks effectPlayer) {
14528 mSession = session;
14529 mWindow = window;
14530 mWindowToken = window.asBinder();
14531 mHandler = handler;
14532 mRootCallbacks = effectPlayer;
14533 }
14534 }
14535
14536 /**
14537 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14538 * is supported. This avoids keeping too many unused fields in most
14539 * instances of View.</p>
14540 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014541 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014542
Mike Cleronf116bf82009-09-27 19:14:12 -070014543 /**
14544 * Scrollbars are not visible
14545 */
14546 public static final int OFF = 0;
14547
14548 /**
14549 * Scrollbars are visible
14550 */
14551 public static final int ON = 1;
14552
14553 /**
14554 * Scrollbars are fading away
14555 */
14556 public static final int FADING = 2;
14557
14558 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014560 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014561 public int scrollBarDefaultDelayBeforeFade;
14562 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014563
14564 public int scrollBarSize;
14565 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014566 public float[] interpolatorValues;
14567 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014568
14569 public final Paint paint;
14570 public final Matrix matrix;
14571 public Shader shader;
14572
Mike Cleronf116bf82009-09-27 19:14:12 -070014573 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14574
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014575 private static final float[] OPAQUE = { 255 };
14576 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014577
Mike Cleronf116bf82009-09-27 19:14:12 -070014578 /**
14579 * When fading should start. This time moves into the future every time
14580 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14581 */
14582 public long fadeStartTime;
14583
14584
14585 /**
14586 * The current state of the scrollbars: ON, OFF, or FADING
14587 */
14588 public int state = OFF;
14589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 private int mLastColor;
14591
Mike Cleronf116bf82009-09-27 19:14:12 -070014592 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014593 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14594 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014595 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14596 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014597
14598 paint = new Paint();
14599 matrix = new Matrix();
14600 // use use a height of 1, and then wack the matrix each time we
14601 // actually use it.
14602 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014604 paint.setShader(shader);
14605 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014606 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 }
Romain Guy8506ab42009-06-11 17:35:47 -070014608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014609 public void setFadeColor(int color) {
14610 if (color != 0 && color != mLastColor) {
14611 mLastColor = color;
14612 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014613
Romain Guye55e1a72009-08-27 10:42:26 -070014614 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14615 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014617 paint.setShader(shader);
14618 // Restore the default transfer mode (src_over)
14619 paint.setXfermode(null);
14620 }
14621 }
Joe Malin32736f02011-01-19 16:14:20 -080014622
Mike Cleronf116bf82009-09-27 19:14:12 -070014623 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014624 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014625 if (now >= fadeStartTime) {
14626
14627 // the animation fades the scrollbars out by changing
14628 // the opacity (alpha) from fully opaque to fully
14629 // transparent
14630 int nextFrame = (int) now;
14631 int framesCount = 0;
14632
14633 Interpolator interpolator = scrollBarInterpolator;
14634
14635 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014636 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014637
14638 // End transparent
14639 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014640 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014641
14642 state = FADING;
14643
14644 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014645 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014646 }
14647 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014648 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014649
Svetoslav Ganova0156172011-06-26 17:55:44 -070014650 /**
14651 * Resuable callback for sending
14652 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14653 */
14654 private class SendViewScrolledAccessibilityEvent implements Runnable {
14655 public volatile boolean mIsPending;
14656
14657 public void run() {
14658 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14659 mIsPending = false;
14660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014661 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014662
14663 /**
14664 * <p>
14665 * This class represents a delegate that can be registered in a {@link View}
14666 * to enhance accessibility support via composition rather via inheritance.
14667 * It is specifically targeted to widget developers that extend basic View
14668 * classes i.e. classes in package android.view, that would like their
14669 * applications to be backwards compatible.
14670 * </p>
14671 * <p>
14672 * A scenario in which a developer would like to use an accessibility delegate
14673 * is overriding a method introduced in a later API version then the minimal API
14674 * version supported by the application. For example, the method
14675 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14676 * in API version 4 when the accessibility APIs were first introduced. If a
14677 * developer would like his application to run on API version 4 devices (assuming
14678 * all other APIs used by the application are version 4 or lower) and take advantage
14679 * of this method, instead of overriding the method which would break the application's
14680 * backwards compatibility, he can override the corresponding method in this
14681 * delegate and register the delegate in the target View if the API version of
14682 * the system is high enough i.e. the API version is same or higher to the API
14683 * version that introduced
14684 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14685 * </p>
14686 * <p>
14687 * Here is an example implementation:
14688 * </p>
14689 * <code><pre><p>
14690 * if (Build.VERSION.SDK_INT >= 14) {
14691 * // If the API version is equal of higher than the version in
14692 * // which onInitializeAccessibilityNodeInfo was introduced we
14693 * // register a delegate with a customized implementation.
14694 * View view = findViewById(R.id.view_id);
14695 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14696 * public void onInitializeAccessibilityNodeInfo(View host,
14697 * AccessibilityNodeInfo info) {
14698 * // Let the default implementation populate the info.
14699 * super.onInitializeAccessibilityNodeInfo(host, info);
14700 * // Set some other information.
14701 * info.setEnabled(host.isEnabled());
14702 * }
14703 * });
14704 * }
14705 * </code></pre></p>
14706 * <p>
14707 * This delegate contains methods that correspond to the accessibility methods
14708 * in View. If a delegate has been specified the implementation in View hands
14709 * off handling to the corresponding method in this delegate. The default
14710 * implementation the delegate methods behaves exactly as the corresponding
14711 * method in View for the case of no accessibility delegate been set. Hence,
14712 * to customize the behavior of a View method, clients can override only the
14713 * corresponding delegate method without altering the behavior of the rest
14714 * accessibility related methods of the host view.
14715 * </p>
14716 */
14717 public static class AccessibilityDelegate {
14718
14719 /**
14720 * Sends an accessibility event of the given type. If accessibility is not
14721 * enabled this method has no effect.
14722 * <p>
14723 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14724 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14725 * been set.
14726 * </p>
14727 *
14728 * @param host The View hosting the delegate.
14729 * @param eventType The type of the event to send.
14730 *
14731 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14732 */
14733 public void sendAccessibilityEvent(View host, int eventType) {
14734 host.sendAccessibilityEventInternal(eventType);
14735 }
14736
14737 /**
14738 * Sends an accessibility event. This method behaves exactly as
14739 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14740 * empty {@link AccessibilityEvent} and does not perform a check whether
14741 * accessibility is enabled.
14742 * <p>
14743 * The default implementation behaves as
14744 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14745 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14746 * the case of no accessibility delegate been set.
14747 * </p>
14748 *
14749 * @param host The View hosting the delegate.
14750 * @param event The event to send.
14751 *
14752 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14753 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14754 */
14755 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14756 host.sendAccessibilityEventUncheckedInternal(event);
14757 }
14758
14759 /**
14760 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14761 * to its children for adding their text content to the event.
14762 * <p>
14763 * The default implementation behaves as
14764 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14765 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14766 * the case of no accessibility delegate been set.
14767 * </p>
14768 *
14769 * @param host The View hosting the delegate.
14770 * @param event The event.
14771 * @return True if the event population was completed.
14772 *
14773 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14774 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14775 */
14776 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14777 return host.dispatchPopulateAccessibilityEventInternal(event);
14778 }
14779
14780 /**
14781 * Gives a chance to the host View to populate the accessibility event with its
14782 * text content.
14783 * <p>
14784 * The default implementation behaves as
14785 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14786 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14787 * the case of no accessibility delegate been set.
14788 * </p>
14789 *
14790 * @param host The View hosting the delegate.
14791 * @param event The accessibility event which to populate.
14792 *
14793 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14794 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14795 */
14796 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14797 host.onPopulateAccessibilityEventInternal(event);
14798 }
14799
14800 /**
14801 * Initializes an {@link AccessibilityEvent} with information about the
14802 * the host View which is the event source.
14803 * <p>
14804 * The default implementation behaves as
14805 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14806 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14807 * the case of no accessibility delegate been set.
14808 * </p>
14809 *
14810 * @param host The View hosting the delegate.
14811 * @param event The event to initialize.
14812 *
14813 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14814 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14815 */
14816 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14817 host.onInitializeAccessibilityEventInternal(event);
14818 }
14819
14820 /**
14821 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14822 * <p>
14823 * The default implementation behaves as
14824 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14825 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14826 * the case of no accessibility delegate been set.
14827 * </p>
14828 *
14829 * @param host The View hosting the delegate.
14830 * @param info The instance to initialize.
14831 *
14832 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14833 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14834 */
14835 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14836 host.onInitializeAccessibilityNodeInfoInternal(info);
14837 }
14838
14839 /**
14840 * Called when a child of the host View has requested sending an
14841 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14842 * to augment the event.
14843 * <p>
14844 * The default implementation behaves as
14845 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14846 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14847 * the case of no accessibility delegate been set.
14848 * </p>
14849 *
14850 * @param host The View hosting the delegate.
14851 * @param child The child which requests sending the event.
14852 * @param event The event to be sent.
14853 * @return True if the event should be sent
14854 *
14855 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14856 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14857 */
14858 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14859 AccessibilityEvent event) {
14860 return host.onRequestSendAccessibilityEventInternal(child, event);
14861 }
14862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014863}