blob: 8cb2200d4ff5a10bab7d671f77a3224954b91d2e [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070064import android.view.accessibility.AccessibilityNodeProvider;
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;
Chet Haase64a48c12012-02-13 16:33:29 -080067import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070074import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075
Doug Feltcb3791202011-07-07 11:57:48 -070076import com.android.internal.R;
77import com.android.internal.util.Predicate;
78import com.android.internal.view.menu.MenuBuilder;
79
Christopher Tatea0374192010-10-05 13:06:41 -070080import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070081import java.lang.reflect.InvocationTargetException;
82import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070085import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080086import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * <p>
90 * This class represents the basic building block for user interface components. A View
91 * occupies a rectangular area on the screen and is responsible for drawing and
92 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070093 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
95 * are invisible containers that hold other Views (or other ViewGroups) and define
96 * their layout properties.
97 * </p>
98 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For information about using this class to develop your application's user interface,
102 * 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 -0800103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700129 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700130 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700174 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700247 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700340 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
341 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
342 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
343 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * </p>
345 *
346 * <p>
347 * Even though a view can define a padding, it does not provide any support for
348 * margins. However, view groups provide such a support. Refer to
349 * {@link android.view.ViewGroup} and
350 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
351 * </p>
352 *
353 * <a name="Layout"></a>
354 * <h3>Layout</h3>
355 * <p>
356 * Layout is a two pass process: a measure pass and a layout pass. The measuring
357 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
358 * of the view tree. Each view pushes dimension specifications down the tree
359 * during the recursion. At the end of the measure pass, every view has stored
360 * its measurements. The second pass happens in
361 * {@link #layout(int,int,int,int)} and is also top-down. During
362 * this pass each parent is responsible for positioning all of its children
363 * using the sizes computed in the measure pass.
364 * </p>
365 *
366 * <p>
367 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
368 * {@link #getMeasuredHeight()} values must be set, along with those for all of
369 * that view's descendants. A view's measured width and measured height values
370 * must respect the constraints imposed by the view's parents. This guarantees
371 * that at the end of the measure pass, all parents accept all of their
372 * children's measurements. A parent view may call measure() more than once on
373 * its children. For example, the parent may measure each child once with
374 * unspecified dimensions to find out how big they want to be, then call
375 * measure() on them again with actual numbers if the sum of all the children's
376 * unconstrained sizes is too big or too small.
377 * </p>
378 *
379 * <p>
380 * The measure pass uses two classes to communicate dimensions. The
381 * {@link MeasureSpec} class is used by views to tell their parents how they
382 * want to be measured and positioned. The base LayoutParams class just
383 * describes how big the view wants to be for both width and height. For each
384 * dimension, it can specify one of:
385 * <ul>
386 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800387 * <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 -0800388 * (minus padding)
389 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
390 * enclose its content (plus padding).
391 * </ul>
392 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
393 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
394 * an X and Y value.
395 * </p>
396 *
397 * <p>
398 * MeasureSpecs are used to push requirements down the tree from parent to
399 * child. A MeasureSpec can be in one of three modes:
400 * <ul>
401 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
402 * of a child view. For example, a LinearLayout may call measure() on its child
403 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
404 * tall the child view wants to be given a width of 240 pixels.
405 * <li>EXACTLY: This is used by the parent to impose an exact size on the
406 * child. The child must use this size, and guarantee that all of its
407 * descendants will fit within this size.
408 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
409 * child. The child must gurantee that it and all of its descendants will fit
410 * within this size.
411 * </ul>
412 * </p>
413 *
414 * <p>
415 * To intiate a layout, call {@link #requestLayout}. This method is typically
416 * called by a view on itself when it believes that is can no longer fit within
417 * its current bounds.
418 * </p>
419 *
420 * <a name="Drawing"></a>
421 * <h3>Drawing</h3>
422 * <p>
423 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700424 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * this means that parents will draw before (i.e., behind) their children, with
426 * siblings drawn in the order they appear in the tree.
427 * If you set a background drawable for a View, then the View will draw it for you
428 * before calling back to its <code>onDraw()</code> method.
429 * </p>
430 *
431 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700432 * 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 -0800433 * </p>
434 *
435 * <p>
436 * To force a view to draw, call {@link #invalidate()}.
437 * </p>
438 *
439 * <a name="EventHandlingThreading"></a>
440 * <h3>Event Handling and Threading</h3>
441 * <p>
442 * The basic cycle of a view is as follows:
443 * <ol>
444 * <li>An event comes in and is dispatched to the appropriate view. The view
445 * handles the event and notifies any listeners.</li>
446 * <li>If in the course of processing the event, the view's bounds may need
447 * to be changed, the view will call {@link #requestLayout()}.</li>
448 * <li>Similarly, if in the course of processing the event the view's appearance
449 * may need to be changed, the view will call {@link #invalidate()}.</li>
450 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
451 * the framework will take care of measuring, laying out, and drawing the tree
452 * as appropriate.</li>
453 * </ol>
454 * </p>
455 *
456 * <p><em>Note: The entire view tree is single threaded. You must always be on
457 * the UI thread when calling any method on any view.</em>
458 * If you are doing work on other threads and want to update the state of a view
459 * from that thread, you should use a {@link Handler}.
460 * </p>
461 *
462 * <a name="FocusHandling"></a>
463 * <h3>Focus Handling</h3>
464 * <p>
465 * The framework will handle routine focus movement in response to user input.
466 * This includes changing the focus as views are removed or hidden, or as new
467 * views become available. Views indicate their willingness to take focus
468 * through the {@link #isFocusable} method. To change whether a view can take
469 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
470 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
471 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
472 * </p>
473 * <p>
474 * Focus movement is based on an algorithm which finds the nearest neighbor in a
475 * given direction. In rare cases, the default algorithm may not match the
476 * intended behavior of the developer. In these situations, you can provide
477 * explicit overrides by using these XML attributes in the layout file:
478 * <pre>
479 * nextFocusDown
480 * nextFocusLeft
481 * nextFocusRight
482 * nextFocusUp
483 * </pre>
484 * </p>
485 *
486 *
487 * <p>
488 * To get a particular view to take focus, call {@link #requestFocus()}.
489 * </p>
490 *
491 * <a name="TouchMode"></a>
492 * <h3>Touch Mode</h3>
493 * <p>
494 * When a user is navigating a user interface via directional keys such as a D-pad, it is
495 * necessary to give focus to actionable items such as buttons so the user can see
496 * what will take input. If the device has touch capabilities, however, and the user
497 * begins interacting with the interface by touching it, it is no longer necessary to
498 * always highlight, or give focus to, a particular view. This motivates a mode
499 * for interaction named 'touch mode'.
500 * </p>
501 * <p>
502 * For a touch capable device, once the user touches the screen, the device
503 * will enter touch mode. From this point onward, only views for which
504 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
505 * Other views that are touchable, like buttons, will not take focus when touched; they will
506 * only fire the on click listeners.
507 * </p>
508 * <p>
509 * Any time a user hits a directional key, such as a D-pad direction, the view device will
510 * exit touch mode, and find a view to take focus, so that the user may resume interacting
511 * with the user interface without touching the screen again.
512 * </p>
513 * <p>
514 * The touch mode state is maintained across {@link android.app.Activity}s. Call
515 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
516 * </p>
517 *
518 * <a name="Scrolling"></a>
519 * <h3>Scrolling</h3>
520 * <p>
521 * The framework provides basic support for views that wish to internally
522 * scroll their content. This includes keeping track of the X and Y scroll
523 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800524 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700525 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * </p>
527 *
528 * <a name="Tags"></a>
529 * <h3>Tags</h3>
530 * <p>
531 * Unlike IDs, tags are not used to identify views. Tags are essentially an
532 * extra piece of information that can be associated with a view. They are most
533 * often used as a convenience to store data related to views in the views
534 * themselves rather than by putting them in a separate structure.
535 * </p>
536 *
537 * <a name="Animation"></a>
538 * <h3>Animation</h3>
539 * <p>
540 * You can attach an {@link Animation} object to a view using
541 * {@link #setAnimation(Animation)} or
542 * {@link #startAnimation(Animation)}. The animation can alter the scale,
543 * rotation, translation and alpha of a view over time. If the animation is
544 * attached to a view that has children, the animation will affect the entire
545 * subtree rooted by that node. When an animation is started, the framework will
546 * take care of redrawing the appropriate views until the animation completes.
547 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800548 * <p>
549 * Starting with Android 3.0, the preferred way of animating views is to use the
550 * {@link android.animation} package APIs.
551 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 *
Jeff Brown85a31762010-09-01 17:01:00 -0700553 * <a name="Security"></a>
554 * <h3>Security</h3>
555 * <p>
556 * Sometimes it is essential that an application be able to verify that an action
557 * is being performed with the full knowledge and consent of the user, such as
558 * granting a permission request, making a purchase or clicking on an advertisement.
559 * Unfortunately, a malicious application could try to spoof the user into
560 * performing these actions, unaware, by concealing the intended purpose of the view.
561 * As a remedy, the framework offers a touch filtering mechanism that can be used to
562 * improve the security of views that provide access to sensitive functionality.
563 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700564 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800565 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700566 * will discard touches that are received whenever the view's window is obscured by
567 * another visible window. As a result, the view will not receive touches whenever a
568 * toast, dialog or other window appears above the view's window.
569 * </p><p>
570 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
572 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * </p>
574 *
Romain Guy171c5922011-01-06 10:04:23 -0800575 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700576 * @attr ref android.R.styleable#View_background
577 * @attr ref android.R.styleable#View_clickable
578 * @attr ref android.R.styleable#View_contentDescription
579 * @attr ref android.R.styleable#View_drawingCacheQuality
580 * @attr ref android.R.styleable#View_duplicateParentState
581 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700582 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700583 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800606 * @attr ref android.R.styleable#View_paddingStart
607 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800609 * @attr ref android.R.styleable#View_rotation
610 * @attr ref android.R.styleable#View_rotationX
611 * @attr ref android.R.styleable#View_rotationY
612 * @attr ref android.R.styleable#View_scaleX
613 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * @attr ref android.R.styleable#View_scrollX
615 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_scrollbarSize
617 * @attr ref android.R.styleable#View_scrollbarStyle
618 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700619 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
620 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
622 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarThumbVertical
624 * @attr ref android.R.styleable#View_scrollbarTrackVertical
625 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_soundEffectsEnabled
628 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700629 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800630 * @attr ref android.R.styleable#View_transformPivotX
631 * @attr ref android.R.styleable#View_transformPivotY
632 * @attr ref android.R.styleable#View_translationX
633 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 *
636 * @see android.view.ViewGroup
637 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700638public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
639 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private static final boolean DBG = false;
641
642 /**
643 * The logging tag used by this class with android.util.Log.
644 */
645 protected static final String VIEW_LOG_TAG = "View";
646
647 /**
648 * Used to mark a View that has no ID.
649 */
650 public static final int NO_ID = -1;
651
652 /**
653 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
654 * calling setFlags.
655 */
656 private static final int NOT_FOCUSABLE = 0x00000000;
657
658 /**
659 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
660 * setFlags.
661 */
662 private static final int FOCUSABLE = 0x00000001;
663
664 /**
665 * Mask for use with setFlags indicating bits used for focus.
666 */
667 private static final int FOCUSABLE_MASK = 0x00000001;
668
669 /**
670 * This view will adjust its padding to fit sytem windows (e.g. status bar)
671 */
672 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
673
674 /**
Scott Main812634c22011-07-27 13:22:35 -0700675 * This view is visible.
676 * 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 VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700683 * 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 INVISIBLE = 0x00000004;
687
688 /**
689 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700690 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
691 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 */
693 public static final int GONE = 0x00000008;
694
695 /**
696 * Mask for use with setFlags indicating bits used for visibility.
697 * {@hide}
698 */
699 static final int VISIBILITY_MASK = 0x0000000C;
700
701 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
702
703 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700704 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 * Use with ENABLED_MASK when calling setFlags.
706 * {@hide}
707 */
708 static final int ENABLED = 0x00000000;
709
710 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700711 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * Use with ENABLED_MASK when calling setFlags.
713 * {@hide}
714 */
715 static final int DISABLED = 0x00000020;
716
717 /**
718 * Mask for use with setFlags indicating bits used for indicating whether
719 * this view is enabled
720 * {@hide}
721 */
722 static final int ENABLED_MASK = 0x00000020;
723
724 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700725 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
726 * called and further optimizations will be performed. It is okay to have
727 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 * {@hide}
729 */
730 static final int WILL_NOT_DRAW = 0x00000080;
731
732 /**
733 * Mask for use with setFlags indicating bits used for indicating whether
734 * this view is will draw
735 * {@hide}
736 */
737 static final int DRAW_MASK = 0x00000080;
738
739 /**
740 * <p>This view doesn't show scrollbars.</p>
741 * {@hide}
742 */
743 static final int SCROLLBARS_NONE = 0x00000000;
744
745 /**
746 * <p>This view shows horizontal scrollbars.</p>
747 * {@hide}
748 */
749 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
750
751 /**
752 * <p>This view shows vertical scrollbars.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_VERTICAL = 0x00000200;
756
757 /**
758 * <p>Mask for use with setFlags indicating bits used for indicating which
759 * scrollbars are enabled.</p>
760 * {@hide}
761 */
762 static final int SCROLLBARS_MASK = 0x00000300;
763
Jeff Brown85a31762010-09-01 17:01:00 -0700764 /**
765 * Indicates that the view should filter touches when its window is obscured.
766 * Refer to the class comments for more information about this security feature.
767 * {@hide}
768 */
769 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
770
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700771 /**
772 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
773 * that they are optional and should be skipped if the window has
774 * requested system UI flags that ignore those insets for layout.
775 */
776 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777
778 /**
779 * <p>This view doesn't show fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_NONE = 0x00000000;
783
784 /**
785 * <p>This view shows horizontal fading edges.</p>
786 * {@hide}
787 */
788 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
789
790 /**
791 * <p>This view shows vertical fading edges.</p>
792 * {@hide}
793 */
794 static final int FADING_EDGE_VERTICAL = 0x00002000;
795
796 /**
797 * <p>Mask for use with setFlags indicating bits used for indicating which
798 * fading edges are enabled.</p>
799 * {@hide}
800 */
801 static final int FADING_EDGE_MASK = 0x00003000;
802
803 /**
804 * <p>Indicates this view can be clicked. When clickable, a View reacts
805 * to clicks by notifying the OnClickListener.<p>
806 * {@hide}
807 */
808 static final int CLICKABLE = 0x00004000;
809
810 /**
811 * <p>Indicates this view is caching its drawing into a bitmap.</p>
812 * {@hide}
813 */
814 static final int DRAWING_CACHE_ENABLED = 0x00008000;
815
816 /**
817 * <p>Indicates that no icicle should be saved for this view.<p>
818 * {@hide}
819 */
820 static final int SAVE_DISABLED = 0x000010000;
821
822 /**
823 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
824 * property.</p>
825 * {@hide}
826 */
827 static final int SAVE_DISABLED_MASK = 0x000010000;
828
829 /**
830 * <p>Indicates that no drawing cache should ever be created for this view.<p>
831 * {@hide}
832 */
833 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
834
835 /**
836 * <p>Indicates this view can take / keep focus when int touch mode.</p>
837 * {@hide}
838 */
839 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
840
841 /**
842 * <p>Enables low quality mode for the drawing cache.</p>
843 */
844 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
845
846 /**
847 * <p>Enables high quality mode for the drawing cache.</p>
848 */
849 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
850
851 /**
852 * <p>Enables automatic quality mode for the drawing cache.</p>
853 */
854 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
855
856 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
857 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
858 };
859
860 /**
861 * <p>Mask for use with setFlags indicating bits used for the cache
862 * quality property.</p>
863 * {@hide}
864 */
865 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
866
867 /**
868 * <p>
869 * Indicates this view can be long clicked. When long clickable, a View
870 * reacts to long clicks by notifying the OnLongClickListener or showing a
871 * context menu.
872 * </p>
873 * {@hide}
874 */
875 static final int LONG_CLICKABLE = 0x00200000;
876
877 /**
878 * <p>Indicates that this view gets its drawable states from its direct parent
879 * and ignores its original internal states.</p>
880 *
881 * @hide
882 */
883 static final int DUPLICATE_PARENT_STATE = 0x00400000;
884
885 /**
886 * The scrollbar style to display the scrollbars inside the content area,
887 * without increasing the padding. The scrollbars will be overlaid with
888 * translucency on the view's content.
889 */
890 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
891
892 /**
893 * The scrollbar style to display the scrollbars inside the padded area,
894 * increasing the padding of the view. The scrollbars will not overlap the
895 * content area of the view.
896 */
897 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
898
899 /**
900 * The scrollbar style to display the scrollbars at the edge of the view,
901 * without increasing the padding. The scrollbars will be overlaid with
902 * translucency.
903 */
904 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
905
906 /**
907 * The scrollbar style to display the scrollbars at the edge of the view,
908 * increasing the padding of the view. The scrollbars will only overlap the
909 * background, if any.
910 */
911 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
912
913 /**
914 * Mask to check if the scrollbar style is overlay or inset.
915 * {@hide}
916 */
917 static final int SCROLLBARS_INSET_MASK = 0x01000000;
918
919 /**
920 * Mask to check if the scrollbar style is inside or outside.
921 * {@hide}
922 */
923 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
924
925 /**
926 * Mask for scrollbar style.
927 * {@hide}
928 */
929 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
930
931 /**
932 * View flag indicating that the screen should remain on while the
933 * window containing this view is visible to the user. This effectively
934 * takes care of automatically setting the WindowManager's
935 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
936 */
937 public static final int KEEP_SCREEN_ON = 0x04000000;
938
939 /**
940 * View flag indicating whether this view should have sound effects enabled
941 * for events such as clicking and touching.
942 */
943 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
944
945 /**
946 * View flag indicating whether this view should have haptic feedback
947 * enabled for events such as long presses.
948 */
949 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
950
951 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700952 * <p>Indicates that the view hierarchy should stop saving state when
953 * it reaches this view. If state saving is initiated immediately at
954 * the view, it will be allowed.
955 * {@hide}
956 */
957 static final int PARENT_SAVE_DISABLED = 0x20000000;
958
959 /**
960 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
961 * {@hide}
962 */
963 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
964
965 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700966 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
967 * should add all focusable Views regardless if they are focusable in touch mode.
968 */
969 public static final int FOCUSABLES_ALL = 0x00000000;
970
971 /**
972 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
973 * should add only Views focusable in touch mode.
974 */
975 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
976
977 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700978 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 * item.
980 */
981 public static final int FOCUS_BACKWARD = 0x00000001;
982
983 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700984 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 * item.
986 */
987 public static final int FOCUS_FORWARD = 0x00000002;
988
989 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700990 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 */
992 public static final int FOCUS_LEFT = 0x00000011;
993
994 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700995 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 */
997 public static final int FOCUS_UP = 0x00000021;
998
999 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001000 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public static final int FOCUS_RIGHT = 0x00000042;
1003
1004 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001005 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 */
1007 public static final int FOCUS_DOWN = 0x00000082;
1008
1009 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001010 * Bits of {@link #getMeasuredWidthAndState()} and
1011 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1012 */
1013 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1014
1015 /**
1016 * Bits of {@link #getMeasuredWidthAndState()} and
1017 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1018 */
1019 public static final int MEASURED_STATE_MASK = 0xff000000;
1020
1021 /**
1022 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1023 * for functions that combine both width and height into a single int,
1024 * such as {@link #getMeasuredState()} and the childState argument of
1025 * {@link #resolveSizeAndState(int, int, int)}.
1026 */
1027 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1028
1029 /**
1030 * Bit of {@link #getMeasuredWidthAndState()} and
1031 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1032 * is smaller that the space the view would like to have.
1033 */
1034 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1035
1036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 * Base View state sets
1038 */
1039 // Singles
1040 /**
1041 * Indicates the view has no states set. States are used with
1042 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1043 * view depending on its state.
1044 *
1045 * @see android.graphics.drawable.Drawable
1046 * @see #getDrawableState()
1047 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001048 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 /**
1050 * Indicates the view is enabled. States are used with
1051 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1052 * view depending on its state.
1053 *
1054 * @see android.graphics.drawable.Drawable
1055 * @see #getDrawableState()
1056 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001057 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 /**
1059 * Indicates the view is focused. States are used with
1060 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1061 * view depending on its state.
1062 *
1063 * @see android.graphics.drawable.Drawable
1064 * @see #getDrawableState()
1065 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001066 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /**
1068 * Indicates the view is selected. States are used with
1069 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1070 * view depending on its state.
1071 *
1072 * @see android.graphics.drawable.Drawable
1073 * @see #getDrawableState()
1074 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001075 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 /**
1077 * Indicates the view is pressed. 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 * @hide
1084 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001085 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 /**
1087 * Indicates the view's window has focus. States are used with
1088 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1089 * view depending on its state.
1090 *
1091 * @see android.graphics.drawable.Drawable
1092 * @see #getDrawableState()
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 // Doubles
1096 /**
1097 * Indicates the view is enabled and has the focus.
1098 *
1099 * @see #ENABLED_STATE_SET
1100 * @see #FOCUSED_STATE_SET
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is enabled and selected.
1105 *
1106 * @see #ENABLED_STATE_SET
1107 * @see #SELECTED_STATE_SET
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is enabled and that its window has focus.
1112 *
1113 * @see #ENABLED_STATE_SET
1114 * @see #WINDOW_FOCUSED_STATE_SET
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is focused and selected.
1119 *
1120 * @see #FOCUSED_STATE_SET
1121 * @see #SELECTED_STATE_SET
1122 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001123 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 /**
1125 * Indicates the view has the focus and that its window has the focus.
1126 *
1127 * @see #FOCUSED_STATE_SET
1128 * @see #WINDOW_FOCUSED_STATE_SET
1129 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001130 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 /**
1132 * Indicates the view is selected and that its window has the focus.
1133 *
1134 * @see #SELECTED_STATE_SET
1135 * @see #WINDOW_FOCUSED_STATE_SET
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 // Triples
1139 /**
1140 * Indicates the view is enabled, focused and selected.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 * @see #SELECTED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is enabled, focused and its window has the focus.
1149 *
1150 * @see #ENABLED_STATE_SET
1151 * @see #FOCUSED_STATE_SET
1152 * @see #WINDOW_FOCUSED_STATE_SET
1153 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001154 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 /**
1156 * Indicates the view is enabled, selected and its window has the focus.
1157 *
1158 * @see #ENABLED_STATE_SET
1159 * @see #SELECTED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is focused, selected and its window has the focus.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #SELECTED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is enabled, focused, selected and its window
1173 * has the focus.
1174 *
1175 * @see #ENABLED_STATE_SET
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 * @see #WINDOW_FOCUSED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is pressed and its window has the focus.
1183 *
1184 * @see #PRESSED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is pressed and selected.
1190 *
1191 * @see #PRESSED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is pressed, selected and its window has the focus.
1197 *
1198 * @see #PRESSED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is pressed and focused.
1205 *
1206 * @see #PRESSED_STATE_SET
1207 * @see #FOCUSED_STATE_SET
1208 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001209 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 /**
1211 * Indicates the view is pressed, focused and its window has the focus.
1212 *
1213 * @see #PRESSED_STATE_SET
1214 * @see #FOCUSED_STATE_SET
1215 * @see #WINDOW_FOCUSED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is pressed, focused and selected.
1220 *
1221 * @see #PRESSED_STATE_SET
1222 * @see #SELECTED_STATE_SET
1223 * @see #FOCUSED_STATE_SET
1224 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001225 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 /**
1227 * Indicates the view is pressed, focused, selected and its window has the focus.
1228 *
1229 * @see #PRESSED_STATE_SET
1230 * @see #FOCUSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 * @see #WINDOW_FOCUSED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed and enabled.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #ENABLED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed, enabled and its window has the focus.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #ENABLED_STATE_SET
1247 * @see #WINDOW_FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, enabled and selected.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #ENABLED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, enabled, selected and its window has the
1260 * focus.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #ENABLED_STATE_SET
1264 * @see #SELECTED_STATE_SET
1265 * @see #WINDOW_FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed, enabled and focused.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #ENABLED_STATE_SET
1273 * @see #FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, enabled, focused and its window has the
1278 * focus.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 * @see #FOCUSED_STATE_SET
1283 * @see #WINDOW_FOCUSED_STATE_SET
1284 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001285 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, enabled, focused and selected.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #ENABLED_STATE_SET
1291 * @see #SELECTED_STATE_SET
1292 * @see #FOCUSED_STATE_SET
1293 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001294 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 /**
1296 * Indicates the view is pressed, enabled, focused, selected and its window
1297 * has the focus.
1298 *
1299 * @see #PRESSED_STATE_SET
1300 * @see #ENABLED_STATE_SET
1301 * @see #SELECTED_STATE_SET
1302 * @see #FOCUSED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306
1307 /**
1308 * The order here is very important to {@link #getDrawableState()}
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 private static final int[][] VIEW_STATE_SETS;
1311
Romain Guyb051e892010-09-28 19:09:36 -07001312 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1313 static final int VIEW_STATE_SELECTED = 1 << 1;
1314 static final int VIEW_STATE_FOCUSED = 1 << 2;
1315 static final int VIEW_STATE_ENABLED = 1 << 3;
1316 static final int VIEW_STATE_PRESSED = 1 << 4;
1317 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001318 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001319 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001320 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1321 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001322
1323 static final int[] VIEW_STATE_IDS = new int[] {
1324 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1325 R.attr.state_selected, VIEW_STATE_SELECTED,
1326 R.attr.state_focused, VIEW_STATE_FOCUSED,
1327 R.attr.state_enabled, VIEW_STATE_ENABLED,
1328 R.attr.state_pressed, VIEW_STATE_PRESSED,
1329 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001330 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001331 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001332 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1333 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 };
1335
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001337 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1338 throw new IllegalStateException(
1339 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1340 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001342 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001344 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001346 orderedIds[i * 2] = viewState;
1347 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001348 }
1349 }
1350 }
Romain Guyb051e892010-09-28 19:09:36 -07001351 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1352 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1353 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001354 int numBits = Integer.bitCount(i);
1355 int[] set = new int[numBits];
1356 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001357 for (int j = 0; j < orderedIds.length; j += 2) {
1358 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 set[pos++] = orderedIds[j];
1360 }
1361 }
1362 VIEW_STATE_SETS[i] = set;
1363 }
1364
1365 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1366 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1367 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1368 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1370 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1371 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1373 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1375 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1376 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1377 | VIEW_STATE_FOCUSED];
1378 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1379 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1381 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1383 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1385 | VIEW_STATE_ENABLED];
1386 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1387 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1388 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1390 | VIEW_STATE_ENABLED];
1391 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1393 | VIEW_STATE_ENABLED];
1394 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1396 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1397
1398 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1399 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1401 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1403 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1405 | VIEW_STATE_PRESSED];
1406 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1408 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1410 | VIEW_STATE_PRESSED];
1411 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1413 | VIEW_STATE_PRESSED];
1414 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1416 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1417 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1421 | VIEW_STATE_PRESSED];
1422 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1424 | VIEW_STATE_PRESSED];
1425 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1427 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1428 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1430 | VIEW_STATE_PRESSED];
1431 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1434 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1437 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1440 | VIEW_STATE_PRESSED];
1441 }
1442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001444 * Accessibility event types that are dispatched for text population.
1445 */
1446 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1447 AccessibilityEvent.TYPE_VIEW_CLICKED
1448 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1449 | AccessibilityEvent.TYPE_VIEW_SELECTED
1450 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1451 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1452 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001453 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001454 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1455 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001456
1457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 * Temporary Rect currently for use in setBackground(). This will probably
1459 * be extended in the future to hold our own class with more than just
1460 * a Rect. :)
1461 */
1462 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001463
1464 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001465 * Temporary flag, used to enable processing of View properties in the native DisplayList
1466 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1467 * apps.
1468 * @hide
1469 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001470 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001471
1472 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001473 * Map used to store views' tags.
1474 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001475 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001478 * The next available accessiiblity id.
1479 */
1480 private static int sNextAccessibilityViewId;
1481
1482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 * The animation currently associated with this view.
1484 * @hide
1485 */
1486 protected Animation mCurrentAnimation = null;
1487
1488 /**
1489 * Width as measured during measure pass.
1490 * {@hide}
1491 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001492 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001493 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494
1495 /**
1496 * Height as measured during measure pass.
1497 * {@hide}
1498 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001499 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001500 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501
1502 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001503 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1504 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1505 * its display list. This flag, used only when hw accelerated, allows us to clear the
1506 * flag while retaining this information until it's needed (at getDisplayList() time and
1507 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1508 *
1509 * {@hide}
1510 */
1511 boolean mRecreateDisplayList = false;
1512
1513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * The view's identifier.
1515 * {@hide}
1516 *
1517 * @see #setId(int)
1518 * @see #getId()
1519 */
1520 @ViewDebug.ExportedProperty(resolveId = true)
1521 int mID = NO_ID;
1522
1523 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001524 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001525 */
1526 int mAccessibilityViewId = NO_ID;
1527
1528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 * The view's tag.
1530 * {@hide}
1531 *
1532 * @see #setTag(Object)
1533 * @see #getTag()
1534 */
1535 protected Object mTag;
1536
1537 // for mPrivateFlags:
1538 /** {@hide} */
1539 static final int WANTS_FOCUS = 0x00000001;
1540 /** {@hide} */
1541 static final int FOCUSED = 0x00000002;
1542 /** {@hide} */
1543 static final int SELECTED = 0x00000004;
1544 /** {@hide} */
1545 static final int IS_ROOT_NAMESPACE = 0x00000008;
1546 /** {@hide} */
1547 static final int HAS_BOUNDS = 0x00000010;
1548 /** {@hide} */
1549 static final int DRAWN = 0x00000020;
1550 /**
1551 * When this flag is set, this view is running an animation on behalf of its
1552 * children and should therefore not cancel invalidate requests, even if they
1553 * lie outside of this view's bounds.
1554 *
1555 * {@hide}
1556 */
1557 static final int DRAW_ANIMATION = 0x00000040;
1558 /** {@hide} */
1559 static final int SKIP_DRAW = 0x00000080;
1560 /** {@hide} */
1561 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1562 /** {@hide} */
1563 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1564 /** {@hide} */
1565 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1566 /** {@hide} */
1567 static final int MEASURED_DIMENSION_SET = 0x00000800;
1568 /** {@hide} */
1569 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001570 /** {@hide} */
1571 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572
1573 private static final int PRESSED = 0x00004000;
1574
1575 /** {@hide} */
1576 static final int DRAWING_CACHE_VALID = 0x00008000;
1577 /**
1578 * Flag used to indicate that this view should be drawn once more (and only once
1579 * more) after its animation has completed.
1580 * {@hide}
1581 */
1582 static final int ANIMATION_STARTED = 0x00010000;
1583
1584 private static final int SAVE_STATE_CALLED = 0x00020000;
1585
1586 /**
1587 * Indicates that the View returned true when onSetAlpha() was called and that
1588 * the alpha must be restored.
1589 * {@hide}
1590 */
1591 static final int ALPHA_SET = 0x00040000;
1592
1593 /**
1594 * Set by {@link #setScrollContainer(boolean)}.
1595 */
1596 static final int SCROLL_CONTAINER = 0x00080000;
1597
1598 /**
1599 * Set by {@link #setScrollContainer(boolean)}.
1600 */
1601 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1602
1603 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001604 * View flag indicating whether this view was invalidated (fully or partially.)
1605 *
1606 * @hide
1607 */
1608 static final int DIRTY = 0x00200000;
1609
1610 /**
1611 * View flag indicating whether this view was invalidated by an opaque
1612 * invalidate request.
1613 *
1614 * @hide
1615 */
1616 static final int DIRTY_OPAQUE = 0x00400000;
1617
1618 /**
1619 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1620 *
1621 * @hide
1622 */
1623 static final int DIRTY_MASK = 0x00600000;
1624
1625 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001626 * Indicates whether the background is opaque.
1627 *
1628 * @hide
1629 */
1630 static final int OPAQUE_BACKGROUND = 0x00800000;
1631
1632 /**
1633 * Indicates whether the scrollbars are opaque.
1634 *
1635 * @hide
1636 */
1637 static final int OPAQUE_SCROLLBARS = 0x01000000;
1638
1639 /**
1640 * Indicates whether the view is opaque.
1641 *
1642 * @hide
1643 */
1644 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001645
Adam Powelle14579b2009-12-16 18:39:52 -08001646 /**
1647 * Indicates a prepressed state;
1648 * the short time between ACTION_DOWN and recognizing
1649 * a 'real' press. Prepressed is used to recognize quick taps
1650 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001651 *
Adam Powelle14579b2009-12-16 18:39:52 -08001652 * @hide
1653 */
1654 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001655
Adam Powellc9fbaab2010-02-16 17:16:19 -08001656 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001657 * Indicates whether the view is temporarily detached.
1658 *
1659 * @hide
1660 */
1661 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001662
Adam Powell8568c3a2010-04-19 14:26:11 -07001663 /**
1664 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001665 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001666 * @hide
1667 */
1668 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001669
1670 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001671 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1672 * @hide
1673 */
1674 private static final int HOVERED = 0x10000000;
1675
1676 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001677 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1678 * for transform operations
1679 *
1680 * @hide
1681 */
Adam Powellf37df072010-09-17 16:22:49 -07001682 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001683
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001684 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001685 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001686
Chet Haasefd2b0022010-08-06 13:08:56 -07001687 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001688 * Indicates that this view was specifically invalidated, not just dirtied because some
1689 * child view was invalidated. The flag is used to determine when we need to recreate
1690 * a view's display list (as opposed to just returning a reference to its existing
1691 * display list).
1692 *
1693 * @hide
1694 */
1695 static final int INVALIDATED = 0x80000000;
1696
Christopher Tate3d4bf172011-03-28 16:16:46 -07001697 /* Masks for mPrivateFlags2 */
1698
1699 /**
1700 * Indicates that this view has reported that it can accept the current drag's content.
1701 * Cleared when the drag operation concludes.
1702 * @hide
1703 */
1704 static final int DRAG_CAN_ACCEPT = 0x00000001;
1705
1706 /**
1707 * Indicates that this view is currently directly under the drag location in a
1708 * drag-and-drop operation involving content that it can accept. Cleared when
1709 * the drag exits the view, or when the drag operation concludes.
1710 * @hide
1711 */
1712 static final int DRAG_HOVERED = 0x00000002;
1713
Cibu Johny86666632010-02-22 13:01:02 -08001714 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001715 * Horizontal layout direction of this view is from Left to Right.
1716 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001717 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001718 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001719
1720 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001721 * Horizontal layout direction of this view is from Right to Left.
1722 * Use with {@link #setLayoutDirection}.
1723 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001724 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001725
1726 /**
1727 * Horizontal layout direction of this view is inherited from its parent.
1728 * Use with {@link #setLayoutDirection}.
1729 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001730 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001731
1732 /**
1733 * Horizontal layout direction of this view is from deduced from the default language
1734 * script for the locale. Use with {@link #setLayoutDirection}.
1735 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001736 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001737
1738 /**
1739 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001740 * @hide
1741 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001742 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1743
1744 /**
1745 * Mask for use with private flags indicating bits used for horizontal layout direction.
1746 * @hide
1747 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001748 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001749
1750 /**
1751 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1752 * right-to-left direction.
1753 * @hide
1754 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001755 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001756
1757 /**
1758 * Indicates whether the view horizontal layout direction has been resolved.
1759 * @hide
1760 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001761 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001762
1763 /**
1764 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1765 * @hide
1766 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001767 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001768
1769 /*
1770 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1771 * flag value.
1772 * @hide
1773 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001774 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1775 LAYOUT_DIRECTION_LTR,
1776 LAYOUT_DIRECTION_RTL,
1777 LAYOUT_DIRECTION_INHERIT,
1778 LAYOUT_DIRECTION_LOCALE
1779 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780
1781 /**
1782 * Default horizontal layout direction.
1783 * @hide
1784 */
1785 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001786
Cibu Johny86666632010-02-22 13:01:02 -08001787
Adam Powell539ee872012-02-03 19:00:49 -08001788 /**
1789 * Indicates that the view is tracking some sort of transient state
1790 * that the app should not need to be aware of, but that the framework
1791 * should take special care to preserve.
1792 *
1793 * @hide
1794 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001796
1797
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001798 /**
1799 * Text direction is inherited thru {@link ViewGroup}
1800 */
1801 public static final int TEXT_DIRECTION_INHERIT = 0;
1802
1803 /**
1804 * Text direction is using "first strong algorithm". The first strong directional character
1805 * determines the paragraph direction. If there is no strong directional character, the
1806 * paragraph direction is the view's resolved layout direction.
1807 */
1808 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1809
1810 /**
1811 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1812 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1813 * If there are neither, the paragraph direction is the view's resolved layout direction.
1814 */
1815 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1816
1817 /**
1818 * Text direction is forced to LTR.
1819 */
1820 public static final int TEXT_DIRECTION_LTR = 3;
1821
1822 /**
1823 * Text direction is forced to RTL.
1824 */
1825 public static final int TEXT_DIRECTION_RTL = 4;
1826
1827 /**
1828 * Text direction is coming from the system Locale.
1829 */
1830 public static final int TEXT_DIRECTION_LOCALE = 5;
1831
1832 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001833 * Default text direction is inherited
1834 */
1835 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1836
1837 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1839 * @hide
1840 */
1841 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1842
1843 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 * Mask for use with private flags indicating bits used for text direction.
1845 * @hide
1846 */
1847 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1848
1849 /**
1850 * Array of text direction flags for mapping attribute "textDirection" to correct
1851 * flag value.
1852 * @hide
1853 */
1854 private static final int[] TEXT_DIRECTION_FLAGS = {
1855 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1856 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1857 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1858 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1859 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1860 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1861 };
1862
1863 /**
1864 * Indicates whether the view text direction has been resolved.
1865 * @hide
1866 */
1867 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1868
1869 /**
1870 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1871 * @hide
1872 */
1873 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1874
1875 /**
1876 * Mask for use with private flags indicating bits used for resolved text direction.
1877 * @hide
1878 */
1879 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1880
1881 /**
1882 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1883 * @hide
1884 */
1885 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1886 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1887
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001888 /*
1889 * Default text alignment. The text alignment of this View is inherited from its parent.
1890 * Use with {@link #setTextAlignment(int)}
1891 */
1892 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1893
1894 /**
1895 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1896 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1897 *
1898 * Use with {@link #setTextAlignment(int)}
1899 */
1900 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1901
1902 /**
1903 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1904 *
1905 * Use with {@link #setTextAlignment(int)}
1906 */
1907 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1908
1909 /**
1910 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1911 *
1912 * Use with {@link #setTextAlignment(int)}
1913 */
1914 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1915
1916 /**
1917 * Center the paragraph, e.g. ALIGN_CENTER.
1918 *
1919 * Use with {@link #setTextAlignment(int)}
1920 */
1921 public static final int TEXT_ALIGNMENT_CENTER = 4;
1922
1923 /**
1924 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1925 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1926 *
1927 * Use with {@link #setTextAlignment(int)}
1928 */
1929 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1930
1931 /**
1932 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1933 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1934 *
1935 * Use with {@link #setTextAlignment(int)}
1936 */
1937 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1938
1939 /**
1940 * Default text alignment is inherited
1941 */
1942 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1943
1944 /**
1945 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1946 * @hide
1947 */
1948 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1949
1950 /**
1951 * Mask for use with private flags indicating bits used for text alignment.
1952 * @hide
1953 */
1954 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1955
1956 /**
1957 * Array of text direction flags for mapping attribute "textAlignment" to correct
1958 * flag value.
1959 * @hide
1960 */
1961 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1962 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1963 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1964 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1965 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1966 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1967 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1968 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
1969 };
1970
1971 /**
1972 * Indicates whether the view text alignment has been resolved.
1973 * @hide
1974 */
1975 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
1976
1977 /**
1978 * Bit shift to get the resolved text alignment.
1979 * @hide
1980 */
1981 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
1982
1983 /**
1984 * Mask for use with private flags indicating bits used for text alignment.
1985 * @hide
1986 */
1987 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
1988
1989 /**
1990 * Indicates whether if the view text alignment has been resolved to gravity
1991 */
1992 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
1993 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
1994
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001995
Christopher Tate3d4bf172011-03-28 16:16:46 -07001996 /* End of masks for mPrivateFlags2 */
1997
1998 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1999
Chet Haasedaf98e92011-01-10 14:10:36 -08002000 /**
Adam Powell637d3372010-08-25 14:37:03 -07002001 * Always allow a user to over-scroll this view, provided it is a
2002 * view that can scroll.
2003 *
2004 * @see #getOverScrollMode()
2005 * @see #setOverScrollMode(int)
2006 */
2007 public static final int OVER_SCROLL_ALWAYS = 0;
2008
2009 /**
2010 * Allow a user to over-scroll this view only if the content is large
2011 * enough to meaningfully scroll, provided it is a view that can scroll.
2012 *
2013 * @see #getOverScrollMode()
2014 * @see #setOverScrollMode(int)
2015 */
2016 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2017
2018 /**
2019 * Never allow a user to over-scroll this view.
2020 *
2021 * @see #getOverScrollMode()
2022 * @see #setOverScrollMode(int)
2023 */
2024 public static final int OVER_SCROLL_NEVER = 2;
2025
2026 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002027 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2028 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002029 *
Joe Malin32736f02011-01-19 16:14:20 -08002030 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002031 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002032 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002033
2034 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002035 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2036 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002037 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002038 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002039 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002040 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002041 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002042 *
Joe Malin32736f02011-01-19 16:14:20 -08002043 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002044 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002045 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2046
2047 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002048 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2049 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002050 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002051 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002052 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2053 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2054 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002055 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002056 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2057 * window flags) for displaying content using every last pixel on the display.
2058 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002059 * <p>There is a limitation: because navigation controls are so important, the least user
2060 * interaction will cause them to reappear immediately. When this happens, both
2061 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2062 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002063 *
2064 * @see #setSystemUiVisibility(int)
2065 */
2066 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2067
2068 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002069 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2070 * into the normal fullscreen mode so that its content can take over the screen
2071 * while still allowing the user to interact with the application.
2072 *
2073 * <p>This has the same visual effect as
2074 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2075 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2076 * meaning that non-critical screen decorations (such as the status bar) will be
2077 * hidden while the user is in the View's window, focusing the experience on
2078 * that content. Unlike the window flag, if you are using ActionBar in
2079 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2080 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2081 * hide the action bar.
2082 *
2083 * <p>This approach to going fullscreen is best used over the window flag when
2084 * it is a transient state -- that is, the application does this at certain
2085 * points in its user interaction where it wants to allow the user to focus
2086 * on content, but not as a continuous state. For situations where the application
2087 * would like to simply stay full screen the entire time (such as a game that
2088 * wants to take over the screen), the
2089 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2090 * is usually a better approach. The state set here will be removed by the system
2091 * in various situations (such as the user moving to another application) like
2092 * the other system UI states.
2093 *
2094 * <p>When using this flag, the application should provide some easy facility
2095 * for the user to go out of it. A common example would be in an e-book
2096 * reader, where tapping on the screen brings back whatever screen and UI
2097 * decorations that had been hidden while the user was immersed in reading
2098 * the book.
2099 *
2100 * @see #setSystemUiVisibility(int)
2101 */
2102 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2103
2104 /**
2105 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2106 * flags, we would like a stable view of the content insets given to
2107 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2108 * will always represent the worst case that the application can expect
2109 * as a continue state. In practice this means with any of system bar,
2110 * nav bar, and status bar shown, but not the space that would be needed
2111 * for an input method.
2112 *
2113 * <p>If you are using ActionBar in
2114 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2115 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2116 * insets it adds to those given to the application.
2117 */
2118 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2119
2120 /**
2121 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2122 * to be layed out as if it has requested
2123 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2124 * allows it to avoid artifacts when switching in and out of that mode, at
2125 * the expense that some of its user interface may be covered by screen
2126 * decorations when they are shown. You can perform layout of your inner
2127 * UI elements to account for the navagation system UI through the
2128 * {@link #fitSystemWindows(Rect)} method.
2129 */
2130 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2131
2132 /**
2133 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2134 * to be layed out as if it has requested
2135 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2136 * allows it to avoid artifacts when switching in and out of that mode, at
2137 * the expense that some of its user interface may be covered by screen
2138 * decorations when they are shown. You can perform layout of your inner
2139 * UI elements to account for non-fullscreen system UI through the
2140 * {@link #fitSystemWindows(Rect)} method.
2141 */
2142 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2143
2144 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002145 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2146 */
2147 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2148
2149 /**
2150 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2151 */
2152 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002153
2154 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002155 * @hide
2156 *
2157 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2158 * out of the public fields to keep the undefined bits out of the developer's way.
2159 *
2160 * Flag to make the status bar not expandable. Unless you also
2161 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2162 */
2163 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2164
2165 /**
2166 * @hide
2167 *
2168 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2169 * out of the public fields to keep the undefined bits out of the developer's way.
2170 *
2171 * Flag to hide notification icons and scrolling ticker text.
2172 */
2173 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2174
2175 /**
2176 * @hide
2177 *
2178 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2179 * out of the public fields to keep the undefined bits out of the developer's way.
2180 *
2181 * Flag to disable incoming notification alerts. This will not block
2182 * icons, but it will block sound, vibrating and other visual or aural notifications.
2183 */
2184 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2185
2186 /**
2187 * @hide
2188 *
2189 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2190 * out of the public fields to keep the undefined bits out of the developer's way.
2191 *
2192 * Flag to hide only the scrolling ticker. Note that
2193 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2194 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2195 */
2196 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2197
2198 /**
2199 * @hide
2200 *
2201 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2202 * out of the public fields to keep the undefined bits out of the developer's way.
2203 *
2204 * Flag to hide the center system info area.
2205 */
2206 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2207
2208 /**
2209 * @hide
2210 *
2211 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2212 * out of the public fields to keep the undefined bits out of the developer's way.
2213 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002214 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002215 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2216 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002217 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002218
2219 /**
2220 * @hide
2221 *
2222 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2223 * out of the public fields to keep the undefined bits out of the developer's way.
2224 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002225 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002226 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2227 */
2228 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2229
2230 /**
2231 * @hide
2232 *
2233 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2234 * out of the public fields to keep the undefined bits out of the developer's way.
2235 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002236 * Flag to hide only the clock. You might use this if your activity has
2237 * its own clock making the status bar's clock redundant.
2238 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002239 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2240
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002241 /**
2242 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002243 *
2244 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2245 * out of the public fields to keep the undefined bits out of the developer's way.
2246 *
2247 * Flag to hide only the recent apps button. Don't use this
2248 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2249 */
2250 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2251
2252 /**
2253 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002254 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002255 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002256
2257 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002258 * These are the system UI flags that can be cleared by events outside
2259 * of an application. Currently this is just the ability to tap on the
2260 * screen while hiding the navigation bar to have it return.
2261 * @hide
2262 */
2263 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002264 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2265 | SYSTEM_UI_FLAG_FULLSCREEN;
2266
2267 /**
2268 * Flags that can impact the layout in relation to system UI.
2269 */
2270 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2271 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2272 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002273
2274 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002275 * Find views that render the specified text.
2276 *
2277 * @see #findViewsWithText(ArrayList, CharSequence, int)
2278 */
2279 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2280
2281 /**
2282 * Find find views that contain the specified content description.
2283 *
2284 * @see #findViewsWithText(ArrayList, CharSequence, int)
2285 */
2286 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2287
2288 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002289 * Find views that contain {@link AccessibilityNodeProvider}. Such
2290 * a View is a root of virtual view hierarchy and may contain the searched
2291 * text. If this flag is set Views with providers are automatically
2292 * added and it is a responsibility of the client to call the APIs of
2293 * the provider to determine whether the virtual tree rooted at this View
2294 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2295 * represeting the virtual views with this text.
2296 *
2297 * @see #findViewsWithText(ArrayList, CharSequence, int)
2298 *
2299 * @hide
2300 */
2301 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2302
2303 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002304 * Indicates that the screen has changed state and is now off.
2305 *
2306 * @see #onScreenStateChanged(int)
2307 */
2308 public static final int SCREEN_STATE_OFF = 0x0;
2309
2310 /**
2311 * Indicates that the screen has changed state and is now on.
2312 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002313 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002314 */
2315 public static final int SCREEN_STATE_ON = 0x1;
2316
2317 /**
Adam Powell637d3372010-08-25 14:37:03 -07002318 * Controls the over-scroll mode for this view.
2319 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2320 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2321 * and {@link #OVER_SCROLL_NEVER}.
2322 */
2323 private int mOverScrollMode;
2324
2325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 * The parent this view is attached to.
2327 * {@hide}
2328 *
2329 * @see #getParent()
2330 */
2331 protected ViewParent mParent;
2332
2333 /**
2334 * {@hide}
2335 */
2336 AttachInfo mAttachInfo;
2337
2338 /**
2339 * {@hide}
2340 */
Romain Guy809a7f62009-05-14 15:44:42 -07002341 @ViewDebug.ExportedProperty(flagMapping = {
2342 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2343 name = "FORCE_LAYOUT"),
2344 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2345 name = "LAYOUT_REQUIRED"),
2346 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002347 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002348 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2349 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2350 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2351 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2352 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002354 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355
2356 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002357 * This view's request for the visibility of the status bar.
2358 * @hide
2359 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002360 @ViewDebug.ExportedProperty(flagMapping = {
2361 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2362 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2363 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2364 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2365 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2366 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2367 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2368 equals = SYSTEM_UI_FLAG_VISIBLE,
2369 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2370 })
Joe Onorato664644d2011-01-23 17:53:23 -08002371 int mSystemUiVisibility;
2372
2373 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 * Count of how many windows this view has been attached to.
2375 */
2376 int mWindowAttachCount;
2377
2378 /**
2379 * The layout parameters associated with this view and used by the parent
2380 * {@link android.view.ViewGroup} to determine how this view should be
2381 * laid out.
2382 * {@hide}
2383 */
2384 protected ViewGroup.LayoutParams mLayoutParams;
2385
2386 /**
2387 * The view flags hold various views states.
2388 * {@hide}
2389 */
2390 @ViewDebug.ExportedProperty
2391 int mViewFlags;
2392
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002393 static class TransformationInfo {
2394 /**
2395 * The transform matrix for the View. This transform is calculated internally
2396 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2397 * is used by default. Do *not* use this variable directly; instead call
2398 * getMatrix(), which will automatically recalculate the matrix if necessary
2399 * to get the correct matrix based on the latest rotation and scale properties.
2400 */
2401 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002402
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002403 /**
2404 * The transform matrix for the View. This transform is calculated internally
2405 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2406 * is used by default. Do *not* use this variable directly; instead call
2407 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2408 * to get the correct matrix based on the latest rotation and scale properties.
2409 */
2410 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002411
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002412 /**
2413 * An internal variable that tracks whether we need to recalculate the
2414 * transform matrix, based on whether the rotation or scaleX/Y properties
2415 * have changed since the matrix was last calculated.
2416 */
2417 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002418
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002419 /**
2420 * An internal variable that tracks whether we need to recalculate the
2421 * transform matrix, based on whether the rotation or scaleX/Y properties
2422 * have changed since the matrix was last calculated.
2423 */
2424 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002425
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002426 /**
2427 * A variable that tracks whether we need to recalculate the
2428 * transform matrix, based on whether the rotation or scaleX/Y properties
2429 * have changed since the matrix was last calculated. This variable
2430 * is only valid after a call to updateMatrix() or to a function that
2431 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2432 */
2433 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002434
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002435 /**
2436 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2437 */
2438 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002439
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002440 /**
2441 * This matrix is used when computing the matrix for 3D rotations.
2442 */
2443 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002444
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002445 /**
2446 * These prev values are used to recalculate a centered pivot point when necessary. The
2447 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2448 * set), so thes values are only used then as well.
2449 */
2450 private int mPrevWidth = -1;
2451 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002452
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002453 /**
2454 * The degrees rotation around the vertical axis through the pivot point.
2455 */
2456 @ViewDebug.ExportedProperty
2457 float mRotationY = 0f;
2458
2459 /**
2460 * The degrees rotation around the horizontal axis through the pivot point.
2461 */
2462 @ViewDebug.ExportedProperty
2463 float mRotationX = 0f;
2464
2465 /**
2466 * The degrees rotation around the pivot point.
2467 */
2468 @ViewDebug.ExportedProperty
2469 float mRotation = 0f;
2470
2471 /**
2472 * The amount of translation of the object away from its left property (post-layout).
2473 */
2474 @ViewDebug.ExportedProperty
2475 float mTranslationX = 0f;
2476
2477 /**
2478 * The amount of translation of the object away from its top property (post-layout).
2479 */
2480 @ViewDebug.ExportedProperty
2481 float mTranslationY = 0f;
2482
2483 /**
2484 * The amount of scale in the x direction around the pivot point. A
2485 * value of 1 means no scaling is applied.
2486 */
2487 @ViewDebug.ExportedProperty
2488 float mScaleX = 1f;
2489
2490 /**
2491 * The amount of scale in the y direction around the pivot point. A
2492 * value of 1 means no scaling is applied.
2493 */
2494 @ViewDebug.ExportedProperty
2495 float mScaleY = 1f;
2496
2497 /**
Chet Haasea33de552012-02-03 16:28:24 -08002498 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002499 */
2500 @ViewDebug.ExportedProperty
2501 float mPivotX = 0f;
2502
2503 /**
Chet Haasea33de552012-02-03 16:28:24 -08002504 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002505 */
2506 @ViewDebug.ExportedProperty
2507 float mPivotY = 0f;
2508
2509 /**
2510 * The opacity of the View. This is a value from 0 to 1, where 0 means
2511 * completely transparent and 1 means completely opaque.
2512 */
2513 @ViewDebug.ExportedProperty
2514 float mAlpha = 1f;
2515 }
2516
2517 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002518
Joe Malin32736f02011-01-19 16:14:20 -08002519 private boolean mLastIsOpaque;
2520
Chet Haasefd2b0022010-08-06 13:08:56 -07002521 /**
2522 * Convenience value to check for float values that are close enough to zero to be considered
2523 * zero.
2524 */
Romain Guy2542d192010-08-18 11:47:12 -07002525 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002526
2527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * The distance in pixels from the left edge of this view's parent
2529 * to the left edge of this view.
2530 * {@hide}
2531 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002532 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 protected int mLeft;
2534 /**
2535 * The distance in pixels from the left edge of this view's parent
2536 * to the right edge of this view.
2537 * {@hide}
2538 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002539 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 protected int mRight;
2541 /**
2542 * The distance in pixels from the top edge of this view's parent
2543 * to the top edge of this view.
2544 * {@hide}
2545 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002546 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 protected int mTop;
2548 /**
2549 * The distance in pixels from the top edge of this view's parent
2550 * to the bottom edge of this view.
2551 * {@hide}
2552 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002553 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 protected int mBottom;
2555
2556 /**
2557 * The offset, in pixels, by which the content of this view is scrolled
2558 * horizontally.
2559 * {@hide}
2560 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002561 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 protected int mScrollX;
2563 /**
2564 * The offset, in pixels, by which the content of this view is scrolled
2565 * vertically.
2566 * {@hide}
2567 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002568 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 protected int mScrollY;
2570
2571 /**
2572 * The left padding in pixels, that is the distance in pixels between the
2573 * left edge of this view and the left edge of its content.
2574 * {@hide}
2575 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002576 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 protected int mPaddingLeft;
2578 /**
2579 * The right padding in pixels, that is the distance in pixels between the
2580 * right edge of this view and the right edge of its content.
2581 * {@hide}
2582 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002583 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 protected int mPaddingRight;
2585 /**
2586 * The top padding in pixels, that is the distance in pixels between the
2587 * top edge of this view and the top edge of its content.
2588 * {@hide}
2589 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002590 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 protected int mPaddingTop;
2592 /**
2593 * The bottom padding in pixels, that is the distance in pixels between the
2594 * bottom edge of this view and the bottom edge of its content.
2595 * {@hide}
2596 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002597 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 protected int mPaddingBottom;
2599
2600 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002601 * Briefly describes the view and is primarily used for accessibility support.
2602 */
2603 private CharSequence mContentDescription;
2604
2605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002607 *
2608 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002610 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002611 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612
2613 /**
2614 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002615 *
2616 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002618 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002619 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002621 /**
Adam Powell20232d02010-12-08 21:08:53 -08002622 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002623 *
2624 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002625 */
2626 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002627 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002628
2629 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002630 * Cache if the user padding is relative.
2631 *
2632 */
2633 @ViewDebug.ExportedProperty(category = "padding")
2634 boolean mUserPaddingRelative;
2635
2636 /**
2637 * Cache the paddingStart set by the user to append to the scrollbar's size.
2638 *
2639 */
2640 @ViewDebug.ExportedProperty(category = "padding")
2641 int mUserPaddingStart;
2642
2643 /**
2644 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2645 *
2646 */
2647 @ViewDebug.ExportedProperty(category = "padding")
2648 int mUserPaddingEnd;
2649
2650 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002651 * @hide
2652 */
2653 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2654 /**
2655 * @hide
2656 */
2657 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658
Philip Milne6c8ea062012-04-03 17:38:43 -07002659 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660
2661 private int mBackgroundResource;
2662 private boolean mBackgroundSizeChanged;
2663
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002664 static class ListenerInfo {
2665 /**
2666 * Listener used to dispatch focus change events.
2667 * This field should be made private, so it is hidden from the SDK.
2668 * {@hide}
2669 */
2670 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002672 /**
2673 * Listeners for layout change events.
2674 */
2675 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002676
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002677 /**
2678 * Listeners for attach events.
2679 */
2680 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002681
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002682 /**
2683 * Listener used to dispatch click events.
2684 * This field should be made private, so it is hidden from the SDK.
2685 * {@hide}
2686 */
2687 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002689 /**
2690 * Listener used to dispatch long click events.
2691 * This field should be made private, so it is hidden from the SDK.
2692 * {@hide}
2693 */
2694 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002696 /**
2697 * Listener used to build the context menu.
2698 * This field should be made private, so it is hidden from the SDK.
2699 * {@hide}
2700 */
2701 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002703 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002705 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002707 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002708
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002709 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002710
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002711 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002712
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002713 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2714 }
2715
2716 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 /**
2719 * The application environment this view lives in.
2720 * This field should be made private, so it is hidden from the SDK.
2721 * {@hide}
2722 */
2723 protected Context mContext;
2724
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002725 private final Resources mResources;
2726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 private ScrollabilityCache mScrollCache;
2728
2729 private int[] mDrawableState = null;
2730
Romain Guy0211a0a2011-02-14 16:34:59 -08002731 /**
2732 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002733 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002734 * @hide
2735 */
2736 public boolean mCachingFailed;
2737
Romain Guy02890fd2010-08-06 17:58:44 -07002738 private Bitmap mDrawingCache;
2739 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002740 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002741 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742
2743 /**
2744 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2745 * the user may specify which view to go to next.
2746 */
2747 private int mNextFocusLeftId = View.NO_ID;
2748
2749 /**
2750 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2751 * the user may specify which view to go to next.
2752 */
2753 private int mNextFocusRightId = View.NO_ID;
2754
2755 /**
2756 * When this view has focus and the next focus is {@link #FOCUS_UP},
2757 * the user may specify which view to go to next.
2758 */
2759 private int mNextFocusUpId = View.NO_ID;
2760
2761 /**
2762 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2763 * the user may specify which view to go to next.
2764 */
2765 private int mNextFocusDownId = View.NO_ID;
2766
Jeff Brown4e6319b2010-12-13 10:36:51 -08002767 /**
2768 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2769 * the user may specify which view to go to next.
2770 */
2771 int mNextFocusForwardId = View.NO_ID;
2772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002774 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002775 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002776 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 private UnsetPressedState mUnsetPressedState;
2779
2780 /**
2781 * Whether the long press's action has been invoked. The tap's action is invoked on the
2782 * up event while a long press is invoked as soon as the long press duration is reached, so
2783 * a long press could be performed before the tap is checked, in which case the tap's action
2784 * should not be invoked.
2785 */
2786 private boolean mHasPerformedLongPress;
2787
2788 /**
2789 * The minimum height of the view. We'll try our best to have the height
2790 * of this view to at least this amount.
2791 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002792 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 private int mMinHeight;
2794
2795 /**
2796 * The minimum width of the view. We'll try our best to have the width
2797 * of this view to at least this amount.
2798 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002799 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 private int mMinWidth;
2801
2802 /**
2803 * The delegate to handle touch events that are physically in this view
2804 * but should be handled by another view.
2805 */
2806 private TouchDelegate mTouchDelegate = null;
2807
2808 /**
2809 * Solid color to use as a background when creating the drawing cache. Enables
2810 * the cache to use 16 bit bitmaps instead of 32 bit.
2811 */
2812 private int mDrawingCacheBackgroundColor = 0;
2813
2814 /**
2815 * Special tree observer used when mAttachInfo is null.
2816 */
2817 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002818
Adam Powelle14579b2009-12-16 18:39:52 -08002819 /**
2820 * Cache the touch slop from the context that created the view.
2821 */
2822 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002825 * Object that handles automatic animation of view properties.
2826 */
2827 private ViewPropertyAnimator mAnimator = null;
2828
2829 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002830 * Flag indicating that a drag can cross window boundaries. When
2831 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2832 * with this flag set, all visible applications will be able to participate
2833 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002834 *
2835 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002836 */
2837 public static final int DRAG_FLAG_GLOBAL = 1;
2838
2839 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002840 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2841 */
2842 private float mVerticalScrollFactor;
2843
2844 /**
Adam Powell20232d02010-12-08 21:08:53 -08002845 * Position of the vertical scroll bar.
2846 */
2847 private int mVerticalScrollbarPosition;
2848
2849 /**
2850 * Position the scroll bar at the default position as determined by the system.
2851 */
2852 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2853
2854 /**
2855 * Position the scroll bar along the left edge.
2856 */
2857 public static final int SCROLLBAR_POSITION_LEFT = 1;
2858
2859 /**
2860 * Position the scroll bar along the right edge.
2861 */
2862 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2863
2864 /**
Romain Guy171c5922011-01-06 10:04:23 -08002865 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002866 *
2867 * @see #getLayerType()
2868 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002869 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002870 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002871 */
2872 public static final int LAYER_TYPE_NONE = 0;
2873
2874 /**
2875 * <p>Indicates that the view has a software layer. A software layer is backed
2876 * by a bitmap and causes the view to be rendered using Android's software
2877 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002878 *
Romain Guy171c5922011-01-06 10:04:23 -08002879 * <p>Software layers have various usages:</p>
2880 * <p>When the application is not using hardware acceleration, a software layer
2881 * is useful to apply a specific color filter and/or blending mode and/or
2882 * translucency to a view and all its children.</p>
2883 * <p>When the application is using hardware acceleration, a software layer
2884 * is useful to render drawing primitives not supported by the hardware
2885 * accelerated pipeline. It can also be used to cache a complex view tree
2886 * into a texture and reduce the complexity of drawing operations. For instance,
2887 * when animating a complex view tree with a translation, a software layer can
2888 * be used to render the view tree only once.</p>
2889 * <p>Software layers should be avoided when the affected view tree updates
2890 * often. Every update will require to re-render the software layer, which can
2891 * potentially be slow (particularly when hardware acceleration is turned on
2892 * since the layer will have to be uploaded into a hardware texture after every
2893 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002894 *
2895 * @see #getLayerType()
2896 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002897 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002898 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002899 */
2900 public static final int LAYER_TYPE_SOFTWARE = 1;
2901
2902 /**
2903 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2904 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2905 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2906 * rendering pipeline, but only if hardware acceleration is turned on for the
2907 * view hierarchy. When hardware acceleration is turned off, hardware layers
2908 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002909 *
Romain Guy171c5922011-01-06 10:04:23 -08002910 * <p>A hardware layer is useful to apply a specific color filter and/or
2911 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002912 * <p>A hardware layer can be used to cache a complex view tree into a
2913 * texture and reduce the complexity of drawing operations. For instance,
2914 * when animating a complex view tree with a translation, a hardware layer can
2915 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002916 * <p>A hardware layer can also be used to increase the rendering quality when
2917 * rotation transformations are applied on a view. It can also be used to
2918 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002919 *
2920 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002921 * @see #setLayerType(int, android.graphics.Paint)
2922 * @see #LAYER_TYPE_NONE
2923 * @see #LAYER_TYPE_SOFTWARE
2924 */
2925 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002926
Romain Guy3aaff3a2011-01-12 14:18:47 -08002927 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2928 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2929 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2930 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2931 })
Romain Guy171c5922011-01-06 10:04:23 -08002932 int mLayerType = LAYER_TYPE_NONE;
2933 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002934 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002935
2936 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002937 * Set to true when the view is sending hover accessibility events because it
2938 * is the innermost hovered view.
2939 */
2940 private boolean mSendingHoverAccessibilityEvents;
2941
2942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 * Simple constructor to use when creating a view from code.
2944 *
2945 * @param context The Context the view is running in, through which it can
2946 * access the current theme, resources, etc.
2947 */
2948 public View(Context context) {
2949 mContext = context;
2950 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002951 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002952 // Set layout and text direction defaults
2953 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002954 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
2955 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08002956 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002957 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002958 mUserPaddingStart = -1;
2959 mUserPaddingEnd = -1;
2960 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
2962
2963 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07002964 * Delegate for injecting accessiblity functionality.
2965 */
2966 AccessibilityDelegate mAccessibilityDelegate;
2967
2968 /**
2969 * Consistency verifier for debugging purposes.
2970 * @hide
2971 */
2972 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2973 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2974 new InputEventConsistencyVerifier(this, 0) : null;
2975
2976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 * Constructor that is called when inflating a view from XML. This is called
2978 * when a view is being constructed from an XML file, supplying attributes
2979 * that were specified in the XML file. This version uses a default style of
2980 * 0, so the only attribute values applied are those in the Context's Theme
2981 * and the given AttributeSet.
2982 *
2983 * <p>
2984 * The method onFinishInflate() will be called after all children have been
2985 * added.
2986 *
2987 * @param context The Context the view is running in, through which it can
2988 * access the current theme, resources, etc.
2989 * @param attrs The attributes of the XML tag that is inflating the view.
2990 * @see #View(Context, AttributeSet, int)
2991 */
2992 public View(Context context, AttributeSet attrs) {
2993 this(context, attrs, 0);
2994 }
2995
2996 /**
2997 * Perform inflation from XML and apply a class-specific base style. This
2998 * constructor of View allows subclasses to use their own base style when
2999 * they are inflating. For example, a Button class's constructor would call
3000 * this version of the super class constructor and supply
3001 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3002 * the theme's button style to modify all of the base view attributes (in
3003 * particular its background) as well as the Button class's attributes.
3004 *
3005 * @param context The Context the view is running in, through which it can
3006 * access the current theme, resources, etc.
3007 * @param attrs The attributes of the XML tag that is inflating the view.
3008 * @param defStyle The default style to apply to this view. If 0, no style
3009 * will be applied (beyond what is included in the theme). This may
3010 * either be an attribute resource, whose value will be retrieved
3011 * from the current theme, or an explicit style resource.
3012 * @see #View(Context, AttributeSet)
3013 */
3014 public View(Context context, AttributeSet attrs, int defStyle) {
3015 this(context);
3016
3017 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3018 defStyle, 0);
3019
3020 Drawable background = null;
3021
3022 int leftPadding = -1;
3023 int topPadding = -1;
3024 int rightPadding = -1;
3025 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003026 int startPadding = -1;
3027 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028
3029 int padding = -1;
3030
3031 int viewFlagValues = 0;
3032 int viewFlagMasks = 0;
3033
3034 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 int x = 0;
3037 int y = 0;
3038
Chet Haase73066682010-11-29 15:55:32 -08003039 float tx = 0;
3040 float ty = 0;
3041 float rotation = 0;
3042 float rotationX = 0;
3043 float rotationY = 0;
3044 float sx = 1f;
3045 float sy = 1f;
3046 boolean transformSet = false;
3047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3049
Adam Powell637d3372010-08-25 14:37:03 -07003050 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 final int N = a.getIndexCount();
3052 for (int i = 0; i < N; i++) {
3053 int attr = a.getIndex(i);
3054 switch (attr) {
3055 case com.android.internal.R.styleable.View_background:
3056 background = a.getDrawable(attr);
3057 break;
3058 case com.android.internal.R.styleable.View_padding:
3059 padding = a.getDimensionPixelSize(attr, -1);
3060 break;
3061 case com.android.internal.R.styleable.View_paddingLeft:
3062 leftPadding = a.getDimensionPixelSize(attr, -1);
3063 break;
3064 case com.android.internal.R.styleable.View_paddingTop:
3065 topPadding = a.getDimensionPixelSize(attr, -1);
3066 break;
3067 case com.android.internal.R.styleable.View_paddingRight:
3068 rightPadding = a.getDimensionPixelSize(attr, -1);
3069 break;
3070 case com.android.internal.R.styleable.View_paddingBottom:
3071 bottomPadding = a.getDimensionPixelSize(attr, -1);
3072 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003073 case com.android.internal.R.styleable.View_paddingStart:
3074 startPadding = a.getDimensionPixelSize(attr, -1);
3075 break;
3076 case com.android.internal.R.styleable.View_paddingEnd:
3077 endPadding = a.getDimensionPixelSize(attr, -1);
3078 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 case com.android.internal.R.styleable.View_scrollX:
3080 x = a.getDimensionPixelOffset(attr, 0);
3081 break;
3082 case com.android.internal.R.styleable.View_scrollY:
3083 y = a.getDimensionPixelOffset(attr, 0);
3084 break;
Chet Haase73066682010-11-29 15:55:32 -08003085 case com.android.internal.R.styleable.View_alpha:
3086 setAlpha(a.getFloat(attr, 1f));
3087 break;
3088 case com.android.internal.R.styleable.View_transformPivotX:
3089 setPivotX(a.getDimensionPixelOffset(attr, 0));
3090 break;
3091 case com.android.internal.R.styleable.View_transformPivotY:
3092 setPivotY(a.getDimensionPixelOffset(attr, 0));
3093 break;
3094 case com.android.internal.R.styleable.View_translationX:
3095 tx = a.getDimensionPixelOffset(attr, 0);
3096 transformSet = true;
3097 break;
3098 case com.android.internal.R.styleable.View_translationY:
3099 ty = a.getDimensionPixelOffset(attr, 0);
3100 transformSet = true;
3101 break;
3102 case com.android.internal.R.styleable.View_rotation:
3103 rotation = a.getFloat(attr, 0);
3104 transformSet = true;
3105 break;
3106 case com.android.internal.R.styleable.View_rotationX:
3107 rotationX = a.getFloat(attr, 0);
3108 transformSet = true;
3109 break;
3110 case com.android.internal.R.styleable.View_rotationY:
3111 rotationY = a.getFloat(attr, 0);
3112 transformSet = true;
3113 break;
3114 case com.android.internal.R.styleable.View_scaleX:
3115 sx = a.getFloat(attr, 1f);
3116 transformSet = true;
3117 break;
3118 case com.android.internal.R.styleable.View_scaleY:
3119 sy = a.getFloat(attr, 1f);
3120 transformSet = true;
3121 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 case com.android.internal.R.styleable.View_id:
3123 mID = a.getResourceId(attr, NO_ID);
3124 break;
3125 case com.android.internal.R.styleable.View_tag:
3126 mTag = a.getText(attr);
3127 break;
3128 case com.android.internal.R.styleable.View_fitsSystemWindows:
3129 if (a.getBoolean(attr, false)) {
3130 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3131 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3132 }
3133 break;
3134 case com.android.internal.R.styleable.View_focusable:
3135 if (a.getBoolean(attr, false)) {
3136 viewFlagValues |= FOCUSABLE;
3137 viewFlagMasks |= FOCUSABLE_MASK;
3138 }
3139 break;
3140 case com.android.internal.R.styleable.View_focusableInTouchMode:
3141 if (a.getBoolean(attr, false)) {
3142 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3143 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3144 }
3145 break;
3146 case com.android.internal.R.styleable.View_clickable:
3147 if (a.getBoolean(attr, false)) {
3148 viewFlagValues |= CLICKABLE;
3149 viewFlagMasks |= CLICKABLE;
3150 }
3151 break;
3152 case com.android.internal.R.styleable.View_longClickable:
3153 if (a.getBoolean(attr, false)) {
3154 viewFlagValues |= LONG_CLICKABLE;
3155 viewFlagMasks |= LONG_CLICKABLE;
3156 }
3157 break;
3158 case com.android.internal.R.styleable.View_saveEnabled:
3159 if (!a.getBoolean(attr, true)) {
3160 viewFlagValues |= SAVE_DISABLED;
3161 viewFlagMasks |= SAVE_DISABLED_MASK;
3162 }
3163 break;
3164 case com.android.internal.R.styleable.View_duplicateParentState:
3165 if (a.getBoolean(attr, false)) {
3166 viewFlagValues |= DUPLICATE_PARENT_STATE;
3167 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3168 }
3169 break;
3170 case com.android.internal.R.styleable.View_visibility:
3171 final int visibility = a.getInt(attr, 0);
3172 if (visibility != 0) {
3173 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3174 viewFlagMasks |= VISIBILITY_MASK;
3175 }
3176 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003177 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003178 // Clear any layout direction flags (included resolved bits) already set
3179 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3180 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003181 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003182 final int value = (layoutDirection != -1) ?
3183 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3184 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003185 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 case com.android.internal.R.styleable.View_drawingCacheQuality:
3187 final int cacheQuality = a.getInt(attr, 0);
3188 if (cacheQuality != 0) {
3189 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3190 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3191 }
3192 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003193 case com.android.internal.R.styleable.View_contentDescription:
3194 mContentDescription = a.getString(attr);
3195 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3197 if (!a.getBoolean(attr, true)) {
3198 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3199 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3200 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003201 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3203 if (!a.getBoolean(attr, true)) {
3204 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3205 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3206 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003207 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 case R.styleable.View_scrollbars:
3209 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3210 if (scrollbars != SCROLLBARS_NONE) {
3211 viewFlagValues |= scrollbars;
3212 viewFlagMasks |= SCROLLBARS_MASK;
3213 initializeScrollbars(a);
3214 }
3215 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003216 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003218 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3219 // Ignore the attribute starting with ICS
3220 break;
3221 }
3222 // With builds < ICS, fall through and apply fading edges
3223 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3225 if (fadingEdge != FADING_EDGE_NONE) {
3226 viewFlagValues |= fadingEdge;
3227 viewFlagMasks |= FADING_EDGE_MASK;
3228 initializeFadingEdge(a);
3229 }
3230 break;
3231 case R.styleable.View_scrollbarStyle:
3232 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3233 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3234 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3235 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3236 }
3237 break;
3238 case R.styleable.View_isScrollContainer:
3239 setScrollContainer = true;
3240 if (a.getBoolean(attr, false)) {
3241 setScrollContainer(true);
3242 }
3243 break;
3244 case com.android.internal.R.styleable.View_keepScreenOn:
3245 if (a.getBoolean(attr, false)) {
3246 viewFlagValues |= KEEP_SCREEN_ON;
3247 viewFlagMasks |= KEEP_SCREEN_ON;
3248 }
3249 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003250 case R.styleable.View_filterTouchesWhenObscured:
3251 if (a.getBoolean(attr, false)) {
3252 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3253 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3254 }
3255 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 case R.styleable.View_nextFocusLeft:
3257 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3258 break;
3259 case R.styleable.View_nextFocusRight:
3260 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3261 break;
3262 case R.styleable.View_nextFocusUp:
3263 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3264 break;
3265 case R.styleable.View_nextFocusDown:
3266 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3267 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003268 case R.styleable.View_nextFocusForward:
3269 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3270 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 case R.styleable.View_minWidth:
3272 mMinWidth = a.getDimensionPixelSize(attr, 0);
3273 break;
3274 case R.styleable.View_minHeight:
3275 mMinHeight = a.getDimensionPixelSize(attr, 0);
3276 break;
Romain Guy9a817362009-05-01 10:57:14 -07003277 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003278 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003279 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003280 + "be used within a restricted context");
3281 }
3282
Romain Guy9a817362009-05-01 10:57:14 -07003283 final String handlerName = a.getString(attr);
3284 if (handlerName != null) {
3285 setOnClickListener(new OnClickListener() {
3286 private Method mHandler;
3287
3288 public void onClick(View v) {
3289 if (mHandler == null) {
3290 try {
3291 mHandler = getContext().getClass().getMethod(handlerName,
3292 View.class);
3293 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003294 int id = getId();
3295 String idText = id == NO_ID ? "" : " with id '"
3296 + getContext().getResources().getResourceEntryName(
3297 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003298 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003299 handlerName + "(View) in the activity "
3300 + getContext().getClass() + " for onClick handler"
3301 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003302 }
3303 }
3304
3305 try {
3306 mHandler.invoke(getContext(), View.this);
3307 } catch (IllegalAccessException e) {
3308 throw new IllegalStateException("Could not execute non "
3309 + "public method of the activity", e);
3310 } catch (InvocationTargetException e) {
3311 throw new IllegalStateException("Could not execute "
3312 + "method of the activity", e);
3313 }
3314 }
3315 });
3316 }
3317 break;
Adam Powell637d3372010-08-25 14:37:03 -07003318 case R.styleable.View_overScrollMode:
3319 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3320 break;
Adam Powell20232d02010-12-08 21:08:53 -08003321 case R.styleable.View_verticalScrollbarPosition:
3322 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3323 break;
Romain Guy171c5922011-01-06 10:04:23 -08003324 case R.styleable.View_layerType:
3325 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3326 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003327 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003328 // Clear any text direction flag already set
3329 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3330 // Set the text direction flags depending on the value of the attribute
3331 final int textDirection = a.getInt(attr, -1);
3332 if (textDirection != -1) {
3333 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3334 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003335 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003336 case R.styleable.View_textAlignment:
3337 // Clear any text alignment flag already set
3338 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3339 // Set the text alignment flag depending on the value of the attribute
3340 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3341 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3342 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
3344 }
3345
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003346 a.recycle();
3347
Adam Powell637d3372010-08-25 14:37:03 -07003348 setOverScrollMode(overScrollMode);
3349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003351 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
3353
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003354 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3355 // layout direction). Those cached values will be used later during padding resolution.
3356 mUserPaddingStart = startPadding;
3357 mUserPaddingEnd = endPadding;
3358
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003359 updateUserPaddingRelative();
3360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 if (padding >= 0) {
3362 leftPadding = padding;
3363 topPadding = padding;
3364 rightPadding = padding;
3365 bottomPadding = padding;
3366 }
3367
3368 // If the user specified the padding (either with android:padding or
3369 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3370 // use the default padding or the padding from the background drawable
3371 // (stored at this point in mPadding*)
3372 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3373 topPadding >= 0 ? topPadding : mPaddingTop,
3374 rightPadding >= 0 ? rightPadding : mPaddingRight,
3375 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3376
3377 if (viewFlagMasks != 0) {
3378 setFlags(viewFlagValues, viewFlagMasks);
3379 }
3380
3381 // Needs to be called after mViewFlags is set
3382 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3383 recomputePadding();
3384 }
3385
3386 if (x != 0 || y != 0) {
3387 scrollTo(x, y);
3388 }
3389
Chet Haase73066682010-11-29 15:55:32 -08003390 if (transformSet) {
3391 setTranslationX(tx);
3392 setTranslationY(ty);
3393 setRotation(rotation);
3394 setRotationX(rotationX);
3395 setRotationY(rotationY);
3396 setScaleX(sx);
3397 setScaleY(sy);
3398 }
3399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3401 setScrollContainer(true);
3402 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003403
3404 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 }
3406
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003407 private void updateUserPaddingRelative() {
3408 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3409 }
3410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 /**
3412 * Non-public constructor for use in testing
3413 */
3414 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003415 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 /**
3419 * <p>
3420 * Initializes the fading edges from a given set of styled attributes. This
3421 * method should be called by subclasses that need fading edges and when an
3422 * instance of these subclasses is created programmatically rather than
3423 * being inflated from XML. This method is automatically called when the XML
3424 * is inflated.
3425 * </p>
3426 *
3427 * @param a the styled attributes set to initialize the fading edges from
3428 */
3429 protected void initializeFadingEdge(TypedArray a) {
3430 initScrollCache();
3431
3432 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3433 R.styleable.View_fadingEdgeLength,
3434 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3435 }
3436
3437 /**
3438 * Returns the size of the vertical faded edges used to indicate that more
3439 * content in this view is visible.
3440 *
3441 * @return The size in pixels of the vertical faded edge or 0 if vertical
3442 * faded edges are not enabled for this view.
3443 * @attr ref android.R.styleable#View_fadingEdgeLength
3444 */
3445 public int getVerticalFadingEdgeLength() {
3446 if (isVerticalFadingEdgeEnabled()) {
3447 ScrollabilityCache cache = mScrollCache;
3448 if (cache != null) {
3449 return cache.fadingEdgeLength;
3450 }
3451 }
3452 return 0;
3453 }
3454
3455 /**
3456 * Set the size of the faded edge used to indicate that more content in this
3457 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003458 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3459 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3460 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 *
3462 * @param length The size in pixels of the faded edge used to indicate that more
3463 * content in this view is visible.
3464 */
3465 public void setFadingEdgeLength(int length) {
3466 initScrollCache();
3467 mScrollCache.fadingEdgeLength = length;
3468 }
3469
3470 /**
3471 * Returns the size of the horizontal faded edges used to indicate that more
3472 * content in this view is visible.
3473 *
3474 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3475 * faded edges are not enabled for this view.
3476 * @attr ref android.R.styleable#View_fadingEdgeLength
3477 */
3478 public int getHorizontalFadingEdgeLength() {
3479 if (isHorizontalFadingEdgeEnabled()) {
3480 ScrollabilityCache cache = mScrollCache;
3481 if (cache != null) {
3482 return cache.fadingEdgeLength;
3483 }
3484 }
3485 return 0;
3486 }
3487
3488 /**
3489 * Returns the width of the vertical scrollbar.
3490 *
3491 * @return The width in pixels of the vertical scrollbar or 0 if there
3492 * is no vertical scrollbar.
3493 */
3494 public int getVerticalScrollbarWidth() {
3495 ScrollabilityCache cache = mScrollCache;
3496 if (cache != null) {
3497 ScrollBarDrawable scrollBar = cache.scrollBar;
3498 if (scrollBar != null) {
3499 int size = scrollBar.getSize(true);
3500 if (size <= 0) {
3501 size = cache.scrollBarSize;
3502 }
3503 return size;
3504 }
3505 return 0;
3506 }
3507 return 0;
3508 }
3509
3510 /**
3511 * Returns the height of the horizontal scrollbar.
3512 *
3513 * @return The height in pixels of the horizontal scrollbar or 0 if
3514 * there is no horizontal scrollbar.
3515 */
3516 protected int getHorizontalScrollbarHeight() {
3517 ScrollabilityCache cache = mScrollCache;
3518 if (cache != null) {
3519 ScrollBarDrawable scrollBar = cache.scrollBar;
3520 if (scrollBar != null) {
3521 int size = scrollBar.getSize(false);
3522 if (size <= 0) {
3523 size = cache.scrollBarSize;
3524 }
3525 return size;
3526 }
3527 return 0;
3528 }
3529 return 0;
3530 }
3531
3532 /**
3533 * <p>
3534 * Initializes the scrollbars from a given set of styled attributes. This
3535 * method should be called by subclasses that need scrollbars and when an
3536 * instance of these subclasses is created programmatically rather than
3537 * being inflated from XML. This method is automatically called when the XML
3538 * is inflated.
3539 * </p>
3540 *
3541 * @param a the styled attributes set to initialize the scrollbars from
3542 */
3543 protected void initializeScrollbars(TypedArray a) {
3544 initScrollCache();
3545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003547
Mike Cleronf116bf82009-09-27 19:14:12 -07003548 if (scrollabilityCache.scrollBar == null) {
3549 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3550 }
Joe Malin32736f02011-01-19 16:14:20 -08003551
Romain Guy8bda2482010-03-02 11:42:11 -08003552 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553
Mike Cleronf116bf82009-09-27 19:14:12 -07003554 if (!fadeScrollbars) {
3555 scrollabilityCache.state = ScrollabilityCache.ON;
3556 }
3557 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003558
3559
Mike Cleronf116bf82009-09-27 19:14:12 -07003560 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3561 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3562 .getScrollBarFadeDuration());
3563 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3564 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3565 ViewConfiguration.getScrollDefaultDelay());
3566
Joe Malin32736f02011-01-19 16:14:20 -08003567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3569 com.android.internal.R.styleable.View_scrollbarSize,
3570 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3571
3572 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3573 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3574
3575 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3576 if (thumb != null) {
3577 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3578 }
3579
3580 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3581 false);
3582 if (alwaysDraw) {
3583 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3584 }
3585
3586 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3587 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3588
3589 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3590 if (thumb != null) {
3591 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3592 }
3593
3594 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3595 false);
3596 if (alwaysDraw) {
3597 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3598 }
3599
3600 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003601 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 }
3603
3604 /**
3605 * <p>
3606 * Initalizes the scrollability cache if necessary.
3607 * </p>
3608 */
3609 private void initScrollCache() {
3610 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003611 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613 }
3614
Philip Milne6c8ea062012-04-03 17:38:43 -07003615 private ScrollabilityCache getScrollCache() {
3616 initScrollCache();
3617 return mScrollCache;
3618 }
3619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 /**
Adam Powell20232d02010-12-08 21:08:53 -08003621 * Set the position of the vertical scroll bar. Should be one of
3622 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3623 * {@link #SCROLLBAR_POSITION_RIGHT}.
3624 *
3625 * @param position Where the vertical scroll bar should be positioned.
3626 */
3627 public void setVerticalScrollbarPosition(int position) {
3628 if (mVerticalScrollbarPosition != position) {
3629 mVerticalScrollbarPosition = position;
3630 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003631 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003632 }
3633 }
3634
3635 /**
3636 * @return The position where the vertical scroll bar will show, if applicable.
3637 * @see #setVerticalScrollbarPosition(int)
3638 */
3639 public int getVerticalScrollbarPosition() {
3640 return mVerticalScrollbarPosition;
3641 }
3642
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003643 ListenerInfo getListenerInfo() {
3644 if (mListenerInfo != null) {
3645 return mListenerInfo;
3646 }
3647 mListenerInfo = new ListenerInfo();
3648 return mListenerInfo;
3649 }
3650
Adam Powell20232d02010-12-08 21:08:53 -08003651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 * Register a callback to be invoked when focus of this view changed.
3653 *
3654 * @param l The callback that will run.
3655 */
3656 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003657 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 }
3659
3660 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003661 * Add a listener that will be called when the bounds of the view change due to
3662 * layout processing.
3663 *
3664 * @param listener The listener that will be called when layout bounds change.
3665 */
3666 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003667 ListenerInfo li = getListenerInfo();
3668 if (li.mOnLayoutChangeListeners == null) {
3669 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003670 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003671 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3672 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003673 }
Chet Haase21cd1382010-09-01 17:42:29 -07003674 }
3675
3676 /**
3677 * Remove a listener for layout changes.
3678 *
3679 * @param listener The listener for layout bounds change.
3680 */
3681 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003682 ListenerInfo li = mListenerInfo;
3683 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003684 return;
3685 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003686 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003687 }
3688
3689 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003690 * Add a listener for attach state changes.
3691 *
3692 * This listener will be called whenever this view is attached or detached
3693 * from a window. Remove the listener using
3694 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3695 *
3696 * @param listener Listener to attach
3697 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3698 */
3699 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003700 ListenerInfo li = getListenerInfo();
3701 if (li.mOnAttachStateChangeListeners == null) {
3702 li.mOnAttachStateChangeListeners
3703 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003704 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003705 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003706 }
3707
3708 /**
3709 * Remove a listener for attach state changes. The listener will receive no further
3710 * notification of window attach/detach events.
3711 *
3712 * @param listener Listener to remove
3713 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3714 */
3715 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003716 ListenerInfo li = mListenerInfo;
3717 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003718 return;
3719 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003720 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003721 }
3722
3723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 * Returns the focus-change callback registered for this view.
3725 *
3726 * @return The callback, or null if one is not registered.
3727 */
3728 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003729 ListenerInfo li = mListenerInfo;
3730 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 }
3732
3733 /**
3734 * Register a callback to be invoked when this view is clicked. If this view is not
3735 * clickable, it becomes clickable.
3736 *
3737 * @param l The callback that will run
3738 *
3739 * @see #setClickable(boolean)
3740 */
3741 public void setOnClickListener(OnClickListener l) {
3742 if (!isClickable()) {
3743 setClickable(true);
3744 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003745 getListenerInfo().mOnClickListener = l;
3746 }
3747
3748 /**
3749 * Return whether this view has an attached OnClickListener. Returns
3750 * true if there is a listener, false if there is none.
3751 */
3752 public boolean hasOnClickListeners() {
3753 ListenerInfo li = mListenerInfo;
3754 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
3756
3757 /**
3758 * Register a callback to be invoked when this view is clicked and held. If this view is not
3759 * long clickable, it becomes long clickable.
3760 *
3761 * @param l The callback that will run
3762 *
3763 * @see #setLongClickable(boolean)
3764 */
3765 public void setOnLongClickListener(OnLongClickListener l) {
3766 if (!isLongClickable()) {
3767 setLongClickable(true);
3768 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003769 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771
3772 /**
3773 * Register a callback to be invoked when the context menu for this view is
3774 * being built. If this view is not long clickable, it becomes long clickable.
3775 *
3776 * @param l The callback that will run
3777 *
3778 */
3779 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3780 if (!isLongClickable()) {
3781 setLongClickable(true);
3782 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003783 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 }
3785
3786 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003787 * Call this view's OnClickListener, if it is defined. Performs all normal
3788 * actions associated with clicking: reporting accessibility event, playing
3789 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 *
3791 * @return True there was an assigned OnClickListener that was called, false
3792 * otherwise is returned.
3793 */
3794 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003795 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3796
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003797 ListenerInfo li = mListenerInfo;
3798 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003800 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 return true;
3802 }
3803
3804 return false;
3805 }
3806
3807 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3809 * this only calls the listener, and does not do any associated clicking
3810 * actions like reporting an accessibility event.
3811 *
3812 * @return True there was an assigned OnClickListener that was called, false
3813 * otherwise is returned.
3814 */
3815 public boolean callOnClick() {
3816 ListenerInfo li = mListenerInfo;
3817 if (li != null && li.mOnClickListener != null) {
3818 li.mOnClickListener.onClick(this);
3819 return true;
3820 }
3821 return false;
3822 }
3823
3824 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003825 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3826 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003828 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 */
3830 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003831 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003834 ListenerInfo li = mListenerInfo;
3835 if (li != null && li.mOnLongClickListener != null) {
3836 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
3838 if (!handled) {
3839 handled = showContextMenu();
3840 }
3841 if (handled) {
3842 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3843 }
3844 return handled;
3845 }
3846
3847 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003848 * Performs button-related actions during a touch down event.
3849 *
3850 * @param event The event.
3851 * @return True if the down was consumed.
3852 *
3853 * @hide
3854 */
3855 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3856 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3857 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3858 return true;
3859 }
3860 }
3861 return false;
3862 }
3863
3864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * Bring up the context menu for this view.
3866 *
3867 * @return Whether a context menu was displayed.
3868 */
3869 public boolean showContextMenu() {
3870 return getParent().showContextMenuForChild(this);
3871 }
3872
3873 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003874 * Bring up the context menu for this view, referring to the item under the specified point.
3875 *
3876 * @param x The referenced x coordinate.
3877 * @param y The referenced y coordinate.
3878 * @param metaState The keyboard modifiers that were pressed.
3879 * @return Whether a context menu was displayed.
3880 *
3881 * @hide
3882 */
3883 public boolean showContextMenu(float x, float y, int metaState) {
3884 return showContextMenu();
3885 }
3886
3887 /**
Adam Powell6e346362010-07-23 10:18:23 -07003888 * Start an action mode.
3889 *
3890 * @param callback Callback that will control the lifecycle of the action mode
3891 * @return The new action mode if it is started, null otherwise
3892 *
3893 * @see ActionMode
3894 */
3895 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003896 ViewParent parent = getParent();
3897 if (parent == null) return null;
3898 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003899 }
3900
3901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 * Register a callback to be invoked when a key is pressed in this view.
3903 * @param l the key listener to attach to this view
3904 */
3905 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003906 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 }
3908
3909 /**
3910 * Register a callback to be invoked when a touch event is sent to this view.
3911 * @param l the touch listener to attach to this view
3912 */
3913 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003914 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 }
3916
3917 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003918 * Register a callback to be invoked when a generic motion event is sent to this view.
3919 * @param l the generic motion listener to attach to this view
3920 */
3921 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003922 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003923 }
3924
3925 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003926 * Register a callback to be invoked when a hover event is sent to this view.
3927 * @param l the hover listener to attach to this view
3928 */
3929 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003930 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003931 }
3932
3933 /**
Joe Malin32736f02011-01-19 16:14:20 -08003934 * Register a drag event listener callback object for this View. The parameter is
3935 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3936 * View, the system calls the
3937 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3938 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003939 */
3940 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003941 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003942 }
3943
3944 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003945 * Give this view focus. This will cause
3946 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 *
3948 * Note: this does not check whether this {@link View} should get focus, it just
3949 * gives it focus no matter what. It should only be called internally by framework
3950 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3951 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003952 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3953 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 * focus moved when requestFocus() is called. It may not always
3955 * apply, in which case use the default View.FOCUS_DOWN.
3956 * @param previouslyFocusedRect The rectangle of the view that had focus
3957 * prior in this View's coordinate system.
3958 */
3959 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3960 if (DBG) {
3961 System.out.println(this + " requestFocus()");
3962 }
3963
3964 if ((mPrivateFlags & FOCUSED) == 0) {
3965 mPrivateFlags |= FOCUSED;
3966
3967 if (mParent != null) {
3968 mParent.requestChildFocus(this, this);
3969 }
3970
3971 onFocusChanged(true, direction, previouslyFocusedRect);
3972 refreshDrawableState();
3973 }
3974 }
3975
3976 /**
3977 * Request that a rectangle of this view be visible on the screen,
3978 * scrolling if necessary just enough.
3979 *
3980 * <p>A View should call this if it maintains some notion of which part
3981 * of its content is interesting. For example, a text editing view
3982 * should call this when its cursor moves.
3983 *
3984 * @param rectangle The rectangle.
3985 * @return Whether any parent scrolled.
3986 */
3987 public boolean requestRectangleOnScreen(Rect rectangle) {
3988 return requestRectangleOnScreen(rectangle, false);
3989 }
3990
3991 /**
3992 * Request that a rectangle of this view be visible on the screen,
3993 * scrolling if necessary just enough.
3994 *
3995 * <p>A View should call this if it maintains some notion of which part
3996 * of its content is interesting. For example, a text editing view
3997 * should call this when its cursor moves.
3998 *
3999 * <p>When <code>immediate</code> is set to true, scrolling will not be
4000 * animated.
4001 *
4002 * @param rectangle The rectangle.
4003 * @param immediate True to forbid animated scrolling, false otherwise
4004 * @return Whether any parent scrolled.
4005 */
4006 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4007 View child = this;
4008 ViewParent parent = mParent;
4009 boolean scrolled = false;
4010 while (parent != null) {
4011 scrolled |= parent.requestChildRectangleOnScreen(child,
4012 rectangle, immediate);
4013
4014 // offset rect so next call has the rectangle in the
4015 // coordinate system of its direct child.
4016 rectangle.offset(child.getLeft(), child.getTop());
4017 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4018
4019 if (!(parent instanceof View)) {
4020 break;
4021 }
Romain Guy8506ab42009-06-11 17:35:47 -07004022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 child = (View) parent;
4024 parent = child.getParent();
4025 }
4026 return scrolled;
4027 }
4028
4029 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004030 * Called when this view wants to give up focus. If focus is cleared
4031 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4032 * <p>
4033 * <strong>Note:</strong> When a View clears focus the framework is trying
4034 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004035 * View is the first from the top that can take focus, then all callbacks
4036 * related to clearing focus will be invoked after wich the framework will
4037 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004038 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 */
4040 public void clearFocus() {
4041 if (DBG) {
4042 System.out.println(this + " clearFocus()");
4043 }
4044
4045 if ((mPrivateFlags & FOCUSED) != 0) {
4046 mPrivateFlags &= ~FOCUSED;
4047
4048 if (mParent != null) {
4049 mParent.clearChildFocus(this);
4050 }
4051
4052 onFocusChanged(false, 0, null);
4053 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004054
4055 ensureInputFocusOnFirstFocusable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 }
4057 }
4058
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004059 void ensureInputFocusOnFirstFocusable() {
4060 View root = getRootView();
4061 if (root != null) {
Svetoslav Ganov5012ebb2012-04-05 17:08:46 -07004062 root.requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064 }
4065
4066 /**
4067 * Called internally by the view system when a new view is getting focus.
4068 * This is what clears the old focus.
4069 */
4070 void unFocus() {
4071 if (DBG) {
4072 System.out.println(this + " unFocus()");
4073 }
4074
4075 if ((mPrivateFlags & FOCUSED) != 0) {
4076 mPrivateFlags &= ~FOCUSED;
4077
4078 onFocusChanged(false, 0, null);
4079 refreshDrawableState();
4080 }
4081 }
4082
4083 /**
4084 * Returns true if this view has focus iteself, or is the ancestor of the
4085 * view that has focus.
4086 *
4087 * @return True if this view has or contains focus, false otherwise.
4088 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004089 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 public boolean hasFocus() {
4091 return (mPrivateFlags & FOCUSED) != 0;
4092 }
4093
4094 /**
4095 * Returns true if this view is focusable or if it contains a reachable View
4096 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4097 * is a View whose parents do not block descendants focus.
4098 *
4099 * Only {@link #VISIBLE} views are considered focusable.
4100 *
4101 * @return True if the view is focusable or if the view contains a focusable
4102 * View, false otherwise.
4103 *
4104 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4105 */
4106 public boolean hasFocusable() {
4107 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4108 }
4109
4110 /**
4111 * Called by the view system when the focus state of this view changes.
4112 * When the focus change event is caused by directional navigation, direction
4113 * and previouslyFocusedRect provide insight into where the focus is coming from.
4114 * When overriding, be sure to call up through to the super class so that
4115 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 * @param gainFocus True if the View has focus; false otherwise.
4118 * @param direction The direction focus has moved when requestFocus()
4119 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004120 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4121 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4122 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4124 * system, of the previously focused view. If applicable, this will be
4125 * passed in as finer grained information about where the focus is coming
4126 * from (in addition to direction). Will be <code>null</code> otherwise.
4127 */
4128 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004129 if (gainFocus) {
4130 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4131 }
4132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 InputMethodManager imm = InputMethodManager.peekInstance();
4134 if (!gainFocus) {
4135 if (isPressed()) {
4136 setPressed(false);
4137 }
4138 if (imm != null && mAttachInfo != null
4139 && mAttachInfo.mHasWindowFocus) {
4140 imm.focusOut(this);
4141 }
Romain Guya2431d02009-04-30 16:30:00 -07004142 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 } else if (imm != null && mAttachInfo != null
4144 && mAttachInfo.mHasWindowFocus) {
4145 imm.focusIn(this);
4146 }
Romain Guy8506ab42009-06-11 17:35:47 -07004147
Romain Guy0fd89bf2011-01-26 15:41:30 -08004148 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004149 ListenerInfo li = mListenerInfo;
4150 if (li != null && li.mOnFocusChangeListener != null) {
4151 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
Joe Malin32736f02011-01-19 16:14:20 -08004153
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004154 if (mAttachInfo != null) {
4155 mAttachInfo.mKeyDispatchState.reset(this);
4156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 }
4158
4159 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004160 * Sends an accessibility event of the given type. If accessiiblity is
4161 * not enabled this method has no effect. The default implementation calls
4162 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4163 * to populate information about the event source (this View), then calls
4164 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4165 * populate the text content of the event source including its descendants,
4166 * and last calls
4167 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4168 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004169 * <p>
4170 * If an {@link AccessibilityDelegate} has been specified via calling
4171 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4172 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4173 * responsible for handling this call.
4174 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004175 *
Scott Mainb303d832011-10-12 16:45:18 -07004176 * @param eventType The type of the event to send, as defined by several types from
4177 * {@link android.view.accessibility.AccessibilityEvent}, such as
4178 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4179 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004180 *
4181 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4182 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4183 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004184 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004185 */
4186 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004187 if (mAccessibilityDelegate != null) {
4188 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4189 } else {
4190 sendAccessibilityEventInternal(eventType);
4191 }
4192 }
4193
4194 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004195 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4196 * {@link AccessibilityEvent} to make an announcement which is related to some
4197 * sort of a context change for which none of the events representing UI transitions
4198 * is a good fit. For example, announcing a new page in a book. If accessibility
4199 * is not enabled this method does nothing.
4200 *
4201 * @param text The announcement text.
4202 */
4203 public void announceForAccessibility(CharSequence text) {
4204 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4205 AccessibilityEvent event = AccessibilityEvent.obtain(
4206 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4207 event.getText().add(text);
4208 sendAccessibilityEventUnchecked(event);
4209 }
4210 }
4211
4212 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004213 * @see #sendAccessibilityEvent(int)
4214 *
4215 * Note: Called from the default {@link AccessibilityDelegate}.
4216 */
4217 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004218 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4219 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4220 }
4221 }
4222
4223 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004224 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4225 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004226 * perform a check whether accessibility is enabled.
4227 * <p>
4228 * If an {@link AccessibilityDelegate} has been specified via calling
4229 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4230 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4231 * is responsible for handling this call.
4232 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004233 *
4234 * @param event The event to send.
4235 *
4236 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004237 */
4238 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004239 if (mAccessibilityDelegate != null) {
4240 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4241 } else {
4242 sendAccessibilityEventUncheckedInternal(event);
4243 }
4244 }
4245
4246 /**
4247 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4248 *
4249 * Note: Called from the default {@link AccessibilityDelegate}.
4250 */
4251 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004252 if (!isShown()) {
4253 return;
4254 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004255 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004256 // Only a subset of accessibility events populates text content.
4257 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4258 dispatchPopulateAccessibilityEvent(event);
4259 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004260 // In the beginning we called #isShown(), so we know that getParent() is not null.
4261 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004262 }
4263
4264 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004265 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4266 * to its children for adding their text content to the event. Note that the
4267 * event text is populated in a separate dispatch path since we add to the
4268 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004269 * A typical implementation will call
4270 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4271 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4272 * on each child. Override this method if custom population of the event text
4273 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004274 * <p>
4275 * If an {@link AccessibilityDelegate} has been specified via calling
4276 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4277 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4278 * is responsible for handling this call.
4279 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004280 * <p>
4281 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4282 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4283 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004284 *
4285 * @param event The event.
4286 *
4287 * @return True if the event population was completed.
4288 */
4289 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004290 if (mAccessibilityDelegate != null) {
4291 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4292 } else {
4293 return dispatchPopulateAccessibilityEventInternal(event);
4294 }
4295 }
4296
4297 /**
4298 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4299 *
4300 * Note: Called from the default {@link AccessibilityDelegate}.
4301 */
4302 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004303 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004304 return false;
4305 }
4306
4307 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004308 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004309 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004310 * text content. While this method is free to modify event
4311 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004312 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4313 * <p>
4314 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004315 * to the text added by the super implementation:
4316 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004317 * super.onPopulateAccessibilityEvent(event);
4318 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4319 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4320 * mCurrentDate.getTimeInMillis(), flags);
4321 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004322 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004323 * <p>
4324 * If an {@link AccessibilityDelegate} has been specified via calling
4325 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4326 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4327 * is responsible for handling this call.
4328 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004329 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4330 * information to the event, in case the default implementation has basic information to add.
4331 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004332 *
4333 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004334 *
4335 * @see #sendAccessibilityEvent(int)
4336 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004337 */
4338 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004339 if (mAccessibilityDelegate != null) {
4340 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4341 } else {
4342 onPopulateAccessibilityEventInternal(event);
4343 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004344 }
4345
4346 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004347 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4348 *
4349 * Note: Called from the default {@link AccessibilityDelegate}.
4350 */
4351 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4352
4353 }
4354
4355 /**
4356 * Initializes an {@link AccessibilityEvent} with information about
4357 * this View which is the event source. In other words, the source of
4358 * an accessibility event is the view whose state change triggered firing
4359 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004360 * <p>
4361 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004362 * to properties set by the super implementation:
4363 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4364 * super.onInitializeAccessibilityEvent(event);
4365 * event.setPassword(true);
4366 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004367 * <p>
4368 * If an {@link AccessibilityDelegate} has been specified via calling
4369 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4370 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4371 * is responsible for handling this call.
4372 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004373 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4374 * information to the event, in case the default implementation has basic information to add.
4375 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004376 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004377 *
4378 * @see #sendAccessibilityEvent(int)
4379 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4380 */
4381 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004382 if (mAccessibilityDelegate != null) {
4383 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4384 } else {
4385 onInitializeAccessibilityEventInternal(event);
4386 }
4387 }
4388
4389 /**
4390 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4391 *
4392 * Note: Called from the default {@link AccessibilityDelegate}.
4393 */
4394 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004395 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004396 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004397 event.setPackageName(getContext().getPackageName());
4398 event.setEnabled(isEnabled());
4399 event.setContentDescription(mContentDescription);
4400
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004401 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4402 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4403 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4404 FOCUSABLES_ALL);
4405 event.setItemCount(focusablesTempList.size());
4406 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4407 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004408 }
4409 }
4410
4411 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004412 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4413 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4414 * This method is responsible for obtaining an accessibility node info from a
4415 * pool of reusable instances and calling
4416 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4417 * initialize the former.
4418 * <p>
4419 * Note: The client is responsible for recycling the obtained instance by calling
4420 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4421 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004422 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004423 * @return A populated {@link AccessibilityNodeInfo}.
4424 *
4425 * @see AccessibilityNodeInfo
4426 */
4427 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004428 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4429 if (provider != null) {
4430 return provider.createAccessibilityNodeInfo(View.NO_ID);
4431 } else {
4432 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4433 onInitializeAccessibilityNodeInfo(info);
4434 return info;
4435 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004436 }
4437
4438 /**
4439 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4440 * The base implementation sets:
4441 * <ul>
4442 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004443 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4444 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004445 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4446 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4447 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4448 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4449 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4450 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4451 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4452 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4453 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4454 * </ul>
4455 * <p>
4456 * Subclasses should override this method, call the super implementation,
4457 * and set additional attributes.
4458 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004459 * <p>
4460 * If an {@link AccessibilityDelegate} has been specified via calling
4461 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4462 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4463 * is responsible for handling this call.
4464 * </p>
4465 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004466 * @param info The instance to initialize.
4467 */
4468 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004469 if (mAccessibilityDelegate != null) {
4470 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4471 } else {
4472 onInitializeAccessibilityNodeInfoInternal(info);
4473 }
4474 }
4475
4476 /**
4477 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4478 *
4479 * Note: Called from the default {@link AccessibilityDelegate}.
4480 */
4481 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004482 Rect bounds = mAttachInfo.mTmpInvalRect;
4483 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004484 info.setBoundsInParent(bounds);
4485
4486 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4487 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004488 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004489 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4490 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004491
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004492 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4493 ViewParent parent = getParent();
4494 if (parent instanceof View) {
4495 View parentView = (View) parent;
4496 info.setParent(parentView);
4497 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004498 }
4499
4500 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004501 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004502 info.setContentDescription(getContentDescription());
4503
4504 info.setEnabled(isEnabled());
4505 info.setClickable(isClickable());
4506 info.setFocusable(isFocusable());
4507 info.setFocused(isFocused());
4508 info.setSelected(isSelected());
4509 info.setLongClickable(isLongClickable());
4510
4511 // TODO: These make sense only if we are in an AdapterView but all
4512 // views can be selected. Maybe from accessiiblity perspective
4513 // we should report as selectable view in an AdapterView.
4514 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4515 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4516
4517 if (isFocusable()) {
4518 if (isFocused()) {
4519 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4520 } else {
4521 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4522 }
4523 }
4524 }
4525
4526 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004527 * Sets a delegate for implementing accessibility support via compositon as
4528 * opposed to inheritance. The delegate's primary use is for implementing
4529 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4530 *
4531 * @param delegate The delegate instance.
4532 *
4533 * @see AccessibilityDelegate
4534 */
4535 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4536 mAccessibilityDelegate = delegate;
4537 }
4538
4539 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004540 * Gets the provider for managing a virtual view hierarchy rooted at this View
4541 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4542 * that explore the window content.
4543 * <p>
4544 * If this method returns an instance, this instance is responsible for managing
4545 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4546 * View including the one representing the View itself. Similarly the returned
4547 * instance is responsible for performing accessibility actions on any virtual
4548 * view or the root view itself.
4549 * </p>
4550 * <p>
4551 * If an {@link AccessibilityDelegate} has been specified via calling
4552 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4553 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4554 * is responsible for handling this call.
4555 * </p>
4556 *
4557 * @return The provider.
4558 *
4559 * @see AccessibilityNodeProvider
4560 */
4561 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4562 if (mAccessibilityDelegate != null) {
4563 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4564 } else {
4565 return null;
4566 }
4567 }
4568
4569 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004570 * Gets the unique identifier of this view on the screen for accessibility purposes.
4571 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4572 *
4573 * @return The view accessibility id.
4574 *
4575 * @hide
4576 */
4577 public int getAccessibilityViewId() {
4578 if (mAccessibilityViewId == NO_ID) {
4579 mAccessibilityViewId = sNextAccessibilityViewId++;
4580 }
4581 return mAccessibilityViewId;
4582 }
4583
4584 /**
4585 * Gets the unique identifier of the window in which this View reseides.
4586 *
4587 * @return The window accessibility id.
4588 *
4589 * @hide
4590 */
4591 public int getAccessibilityWindowId() {
4592 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4593 }
4594
4595 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004596 * Gets the {@link View} description. It briefly describes the view and is
4597 * primarily used for accessibility support. Set this property to enable
4598 * better accessibility support for your application. This is especially
4599 * true for views that do not have textual representation (For example,
4600 * ImageButton).
4601 *
4602 * @return The content descriptiopn.
4603 *
4604 * @attr ref android.R.styleable#View_contentDescription
4605 */
4606 public CharSequence getContentDescription() {
4607 return mContentDescription;
4608 }
4609
4610 /**
4611 * Sets the {@link View} description. It briefly describes the view and is
4612 * primarily used for accessibility support. Set this property to enable
4613 * better accessibility support for your application. This is especially
4614 * true for views that do not have textual representation (For example,
4615 * ImageButton).
4616 *
4617 * @param contentDescription The content description.
4618 *
4619 * @attr ref android.R.styleable#View_contentDescription
4620 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004621 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004622 public void setContentDescription(CharSequence contentDescription) {
4623 mContentDescription = contentDescription;
4624 }
4625
4626 /**
Romain Guya2431d02009-04-30 16:30:00 -07004627 * Invoked whenever this view loses focus, either by losing window focus or by losing
4628 * focus within its window. This method can be used to clear any state tied to the
4629 * focus. For instance, if a button is held pressed with the trackball and the window
4630 * loses focus, this method can be used to cancel the press.
4631 *
4632 * Subclasses of View overriding this method should always call super.onFocusLost().
4633 *
4634 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004635 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004636 *
4637 * @hide pending API council approval
4638 */
4639 protected void onFocusLost() {
4640 resetPressedState();
4641 }
4642
4643 private void resetPressedState() {
4644 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4645 return;
4646 }
4647
4648 if (isPressed()) {
4649 setPressed(false);
4650
4651 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004652 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004653 }
4654 }
4655 }
4656
4657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 * Returns true if this view has focus
4659 *
4660 * @return True if this view has focus, false otherwise.
4661 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004662 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 public boolean isFocused() {
4664 return (mPrivateFlags & FOCUSED) != 0;
4665 }
4666
4667 /**
4668 * Find the view in the hierarchy rooted at this view that currently has
4669 * focus.
4670 *
4671 * @return The view that currently has focus, or null if no focused view can
4672 * be found.
4673 */
4674 public View findFocus() {
4675 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4676 }
4677
4678 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004679 * Indicates whether this view is one of the set of scrollable containers in
4680 * its window.
4681 *
4682 * @return whether this view is one of the set of scrollable containers in
4683 * its window
4684 *
4685 * @attr ref android.R.styleable#View_isScrollContainer
4686 */
4687 public boolean isScrollContainer() {
4688 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4689 }
4690
4691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 * Change whether this view is one of the set of scrollable containers in
4693 * its window. This will be used to determine whether the window can
4694 * resize or must pan when a soft input area is open -- scrollable
4695 * containers allow the window to use resize mode since the container
4696 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004697 *
4698 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 */
4700 public void setScrollContainer(boolean isScrollContainer) {
4701 if (isScrollContainer) {
4702 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4703 mAttachInfo.mScrollContainers.add(this);
4704 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4705 }
4706 mPrivateFlags |= SCROLL_CONTAINER;
4707 } else {
4708 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4709 mAttachInfo.mScrollContainers.remove(this);
4710 }
4711 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4712 }
4713 }
4714
4715 /**
4716 * Returns the quality of the drawing cache.
4717 *
4718 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4719 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4720 *
4721 * @see #setDrawingCacheQuality(int)
4722 * @see #setDrawingCacheEnabled(boolean)
4723 * @see #isDrawingCacheEnabled()
4724 *
4725 * @attr ref android.R.styleable#View_drawingCacheQuality
4726 */
4727 public int getDrawingCacheQuality() {
4728 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4729 }
4730
4731 /**
4732 * Set the drawing cache quality of this view. This value is used only when the
4733 * drawing cache is enabled
4734 *
4735 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4736 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4737 *
4738 * @see #getDrawingCacheQuality()
4739 * @see #setDrawingCacheEnabled(boolean)
4740 * @see #isDrawingCacheEnabled()
4741 *
4742 * @attr ref android.R.styleable#View_drawingCacheQuality
4743 */
4744 public void setDrawingCacheQuality(int quality) {
4745 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4746 }
4747
4748 /**
4749 * Returns whether the screen should remain on, corresponding to the current
4750 * value of {@link #KEEP_SCREEN_ON}.
4751 *
4752 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4753 *
4754 * @see #setKeepScreenOn(boolean)
4755 *
4756 * @attr ref android.R.styleable#View_keepScreenOn
4757 */
4758 public boolean getKeepScreenOn() {
4759 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4760 }
4761
4762 /**
4763 * Controls whether the screen should remain on, modifying the
4764 * value of {@link #KEEP_SCREEN_ON}.
4765 *
4766 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4767 *
4768 * @see #getKeepScreenOn()
4769 *
4770 * @attr ref android.R.styleable#View_keepScreenOn
4771 */
4772 public void setKeepScreenOn(boolean keepScreenOn) {
4773 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4774 }
4775
4776 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004777 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4778 * @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 -08004779 *
4780 * @attr ref android.R.styleable#View_nextFocusLeft
4781 */
4782 public int getNextFocusLeftId() {
4783 return mNextFocusLeftId;
4784 }
4785
4786 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004787 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4788 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4789 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 *
4791 * @attr ref android.R.styleable#View_nextFocusLeft
4792 */
4793 public void setNextFocusLeftId(int nextFocusLeftId) {
4794 mNextFocusLeftId = nextFocusLeftId;
4795 }
4796
4797 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004798 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4799 * @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 -08004800 *
4801 * @attr ref android.R.styleable#View_nextFocusRight
4802 */
4803 public int getNextFocusRightId() {
4804 return mNextFocusRightId;
4805 }
4806
4807 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004808 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4809 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4810 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 *
4812 * @attr ref android.R.styleable#View_nextFocusRight
4813 */
4814 public void setNextFocusRightId(int nextFocusRightId) {
4815 mNextFocusRightId = nextFocusRightId;
4816 }
4817
4818 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004819 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4820 * @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 -08004821 *
4822 * @attr ref android.R.styleable#View_nextFocusUp
4823 */
4824 public int getNextFocusUpId() {
4825 return mNextFocusUpId;
4826 }
4827
4828 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004829 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4830 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4831 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 *
4833 * @attr ref android.R.styleable#View_nextFocusUp
4834 */
4835 public void setNextFocusUpId(int nextFocusUpId) {
4836 mNextFocusUpId = nextFocusUpId;
4837 }
4838
4839 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004840 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4841 * @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 -08004842 *
4843 * @attr ref android.R.styleable#View_nextFocusDown
4844 */
4845 public int getNextFocusDownId() {
4846 return mNextFocusDownId;
4847 }
4848
4849 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004850 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4851 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4852 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 *
4854 * @attr ref android.R.styleable#View_nextFocusDown
4855 */
4856 public void setNextFocusDownId(int nextFocusDownId) {
4857 mNextFocusDownId = nextFocusDownId;
4858 }
4859
4860 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004861 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4862 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4863 *
4864 * @attr ref android.R.styleable#View_nextFocusForward
4865 */
4866 public int getNextFocusForwardId() {
4867 return mNextFocusForwardId;
4868 }
4869
4870 /**
4871 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4872 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4873 * decide automatically.
4874 *
4875 * @attr ref android.R.styleable#View_nextFocusForward
4876 */
4877 public void setNextFocusForwardId(int nextFocusForwardId) {
4878 mNextFocusForwardId = nextFocusForwardId;
4879 }
4880
4881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 * Returns the visibility of this view and all of its ancestors
4883 *
4884 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4885 */
4886 public boolean isShown() {
4887 View current = this;
4888 //noinspection ConstantConditions
4889 do {
4890 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4891 return false;
4892 }
4893 ViewParent parent = current.mParent;
4894 if (parent == null) {
4895 return false; // We are not attached to the view root
4896 }
4897 if (!(parent instanceof View)) {
4898 return true;
4899 }
4900 current = (View) parent;
4901 } while (current != null);
4902
4903 return false;
4904 }
4905
4906 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004907 * Called by the view hierarchy when the content insets for a window have
4908 * changed, to allow it to adjust its content to fit within those windows.
4909 * The content insets tell you the space that the status bar, input method,
4910 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004912 * <p>You do not normally need to deal with this function, since the default
4913 * window decoration given to applications takes care of applying it to the
4914 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
4915 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
4916 * and your content can be placed under those system elements. You can then
4917 * use this method within your view hierarchy if you have parts of your UI
4918 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004920 * <p>The default implementation of this method simply applies the content
4921 * inset's to the view's padding. This can be enabled through
4922 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
4923 * the method and handle the insets however you would like. Note that the
4924 * insets provided by the framework are always relative to the far edges
4925 * of the window, not accounting for the location of the called view within
4926 * that window. (In fact when this method is called you do not yet know
4927 * where the layout will place the view, as it is done before layout happens.)
4928 *
4929 * <p>Note: unlike many View methods, there is no dispatch phase to this
4930 * call. If you are overriding it in a ViewGroup and want to allow the
4931 * call to continue to your children, you must be sure to call the super
4932 * implementation.
4933 *
4934 * @param insets Current content insets of the window. Prior to
4935 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
4936 * the insets or else you and Android will be unhappy.
4937 *
4938 * @return Return true if this view applied the insets and it should not
4939 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 */
4941 protected boolean fitSystemWindows(Rect insets) {
4942 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004943 mUserPaddingStart = -1;
4944 mUserPaddingEnd = -1;
4945 mUserPaddingRelative = false;
4946 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
4947 || mAttachInfo == null
4948 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
4949 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
4950 return true;
4951 } else {
4952 internalSetPadding(0, 0, 0, 0);
4953 return false;
4954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 }
4956 return false;
4957 }
4958
4959 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004960 * Set whether or not this view should account for system screen decorations
4961 * such as the status bar and inset its content. This allows this view to be
4962 * positioned in absolute screen coordinates and remain visible to the user.
4963 *
4964 * <p>This should only be used by top-level window decor views.
4965 *
4966 * @param fitSystemWindows true to inset content for system screen decorations, false for
4967 * default behavior.
4968 *
4969 * @attr ref android.R.styleable#View_fitsSystemWindows
4970 */
4971 public void setFitsSystemWindows(boolean fitSystemWindows) {
4972 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4973 }
4974
4975 /**
4976 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4977 * will account for system screen decorations such as the status bar and inset its
4978 * content. This allows the view to be positioned in absolute screen coordinates
4979 * and remain visible to the user.
4980 *
4981 * @return true if this view will adjust its content bounds for system screen decorations.
4982 *
4983 * @attr ref android.R.styleable#View_fitsSystemWindows
4984 */
4985 public boolean fitsSystemWindows() {
4986 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4987 }
4988
4989 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004990 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
4991 */
4992 public void requestFitSystemWindows() {
4993 if (mParent != null) {
4994 mParent.requestFitSystemWindows();
4995 }
4996 }
4997
4998 /**
4999 * For use by PhoneWindow to make its own system window fitting optional.
5000 * @hide
5001 */
5002 public void makeOptionalFitsSystemWindows() {
5003 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5004 }
5005
5006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 * Returns the visibility status for this view.
5008 *
5009 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5010 * @attr ref android.R.styleable#View_visibility
5011 */
5012 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005013 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5014 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5015 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 })
5017 public int getVisibility() {
5018 return mViewFlags & VISIBILITY_MASK;
5019 }
5020
5021 /**
5022 * Set the enabled state of this view.
5023 *
5024 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5025 * @attr ref android.R.styleable#View_visibility
5026 */
5027 @RemotableViewMethod
5028 public void setVisibility(int visibility) {
5029 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005030 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 }
5032
5033 /**
5034 * Returns the enabled status for this view. The interpretation of the
5035 * enabled state varies by subclass.
5036 *
5037 * @return True if this view is enabled, false otherwise.
5038 */
5039 @ViewDebug.ExportedProperty
5040 public boolean isEnabled() {
5041 return (mViewFlags & ENABLED_MASK) == ENABLED;
5042 }
5043
5044 /**
5045 * Set the enabled state of this view. The interpretation of the enabled
5046 * state varies by subclass.
5047 *
5048 * @param enabled True if this view is enabled, false otherwise.
5049 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005050 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005052 if (enabled == isEnabled()) return;
5053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5055
5056 /*
5057 * The View most likely has to change its appearance, so refresh
5058 * the drawable state.
5059 */
5060 refreshDrawableState();
5061
5062 // Invalidate too, since the default behavior for views is to be
5063 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005064 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 }
5066
5067 /**
5068 * Set whether this view can receive the focus.
5069 *
5070 * Setting this to false will also ensure that this view is not focusable
5071 * in touch mode.
5072 *
5073 * @param focusable If true, this view can receive the focus.
5074 *
5075 * @see #setFocusableInTouchMode(boolean)
5076 * @attr ref android.R.styleable#View_focusable
5077 */
5078 public void setFocusable(boolean focusable) {
5079 if (!focusable) {
5080 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5081 }
5082 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5083 }
5084
5085 /**
5086 * Set whether this view can receive focus while in touch mode.
5087 *
5088 * Setting this to true will also ensure that this view is focusable.
5089 *
5090 * @param focusableInTouchMode If true, this view can receive the focus while
5091 * in touch mode.
5092 *
5093 * @see #setFocusable(boolean)
5094 * @attr ref android.R.styleable#View_focusableInTouchMode
5095 */
5096 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5097 // Focusable in touch mode should always be set before the focusable flag
5098 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5099 // which, in touch mode, will not successfully request focus on this view
5100 // because the focusable in touch mode flag is not set
5101 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5102 if (focusableInTouchMode) {
5103 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5104 }
5105 }
5106
5107 /**
5108 * Set whether this view should have sound effects enabled for events such as
5109 * clicking and touching.
5110 *
5111 * <p>You may wish to disable sound effects for a view if you already play sounds,
5112 * for instance, a dial key that plays dtmf tones.
5113 *
5114 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5115 * @see #isSoundEffectsEnabled()
5116 * @see #playSoundEffect(int)
5117 * @attr ref android.R.styleable#View_soundEffectsEnabled
5118 */
5119 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5120 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5121 }
5122
5123 /**
5124 * @return whether this view should have sound effects enabled for events such as
5125 * clicking and touching.
5126 *
5127 * @see #setSoundEffectsEnabled(boolean)
5128 * @see #playSoundEffect(int)
5129 * @attr ref android.R.styleable#View_soundEffectsEnabled
5130 */
5131 @ViewDebug.ExportedProperty
5132 public boolean isSoundEffectsEnabled() {
5133 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5134 }
5135
5136 /**
5137 * Set whether this view should have haptic feedback for events such as
5138 * long presses.
5139 *
5140 * <p>You may wish to disable haptic feedback if your view already controls
5141 * its own haptic feedback.
5142 *
5143 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5144 * @see #isHapticFeedbackEnabled()
5145 * @see #performHapticFeedback(int)
5146 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5147 */
5148 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5149 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5150 }
5151
5152 /**
5153 * @return whether this view should have haptic feedback enabled for events
5154 * long presses.
5155 *
5156 * @see #setHapticFeedbackEnabled(boolean)
5157 * @see #performHapticFeedback(int)
5158 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5159 */
5160 @ViewDebug.ExportedProperty
5161 public boolean isHapticFeedbackEnabled() {
5162 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5163 }
5164
5165 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005166 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005167 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005168 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5169 * {@link #LAYOUT_DIRECTION_RTL},
5170 * {@link #LAYOUT_DIRECTION_INHERIT} or
5171 * {@link #LAYOUT_DIRECTION_LOCALE}.
5172 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005173 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005174 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005175 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5176 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5177 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5178 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005179 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005180 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005181 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005182 }
5183
5184 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005185 * Set the layout direction for this view. This will propagate a reset of layout direction
5186 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005187 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005188 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5189 * {@link #LAYOUT_DIRECTION_RTL},
5190 * {@link #LAYOUT_DIRECTION_INHERIT} or
5191 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005192 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005193 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005194 */
5195 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005196 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005197 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005198 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005199 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005200 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005201 // Set the new layout direction (filtered) and ask for a layout pass
5202 mPrivateFlags2 |=
5203 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5204 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005205 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005206 }
5207
5208 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005209 * Returns the resolved layout direction for this view.
5210 *
5211 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005212 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005213 */
5214 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005215 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5216 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005217 })
5218 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005219 // The layout diretion will be resolved only if needed
5220 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5221 resolveLayoutDirection();
5222 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005223 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005224 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5225 }
5226
5227 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005228 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5229 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005230 *
5231 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005232 */
5233 @ViewDebug.ExportedProperty(category = "layout")
5234 public boolean isLayoutRtl() {
5235 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5236 }
5237
5238 /**
Adam Powell539ee872012-02-03 19:00:49 -08005239 * Indicates whether the view is currently tracking transient state that the
5240 * app should not need to concern itself with saving and restoring, but that
5241 * the framework should take special note to preserve when possible.
5242 *
5243 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005244 */
5245 @ViewDebug.ExportedProperty(category = "layout")
5246 public boolean hasTransientState() {
5247 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5248 }
5249
5250 /**
5251 * Set whether this view is currently tracking transient state that the
5252 * framework should attempt to preserve when possible.
5253 *
5254 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005255 */
5256 public void setHasTransientState(boolean hasTransientState) {
5257 if (hasTransientState() == hasTransientState) return;
5258
5259 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5260 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5261 if (mParent != null) {
5262 try {
5263 mParent.childHasTransientStateChanged(this, hasTransientState);
5264 } catch (AbstractMethodError e) {
5265 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5266 " does not fully implement ViewParent", e);
5267 }
5268 }
5269 }
5270
5271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 * If this view doesn't do any drawing on its own, set this flag to
5273 * allow further optimizations. By default, this flag is not set on
5274 * View, but could be set on some View subclasses such as ViewGroup.
5275 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005276 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5277 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005278 *
5279 * @param willNotDraw whether or not this View draw on its own
5280 */
5281 public void setWillNotDraw(boolean willNotDraw) {
5282 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5283 }
5284
5285 /**
5286 * Returns whether or not this View draws on its own.
5287 *
5288 * @return true if this view has nothing to draw, false otherwise
5289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005290 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 public boolean willNotDraw() {
5292 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5293 }
5294
5295 /**
5296 * When a View's drawing cache is enabled, drawing is redirected to an
5297 * offscreen bitmap. Some views, like an ImageView, must be able to
5298 * bypass this mechanism if they already draw a single bitmap, to avoid
5299 * unnecessary usage of the memory.
5300 *
5301 * @param willNotCacheDrawing true if this view does not cache its
5302 * drawing, false otherwise
5303 */
5304 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5305 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5306 }
5307
5308 /**
5309 * Returns whether or not this View can cache its drawing or not.
5310 *
5311 * @return true if this view does not cache its drawing, false otherwise
5312 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005313 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 public boolean willNotCacheDrawing() {
5315 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5316 }
5317
5318 /**
5319 * Indicates whether this view reacts to click events or not.
5320 *
5321 * @return true if the view is clickable, false otherwise
5322 *
5323 * @see #setClickable(boolean)
5324 * @attr ref android.R.styleable#View_clickable
5325 */
5326 @ViewDebug.ExportedProperty
5327 public boolean isClickable() {
5328 return (mViewFlags & CLICKABLE) == CLICKABLE;
5329 }
5330
5331 /**
5332 * Enables or disables click events for this view. When a view
5333 * is clickable it will change its state to "pressed" on every click.
5334 * Subclasses should set the view clickable to visually react to
5335 * user's clicks.
5336 *
5337 * @param clickable true to make the view clickable, false otherwise
5338 *
5339 * @see #isClickable()
5340 * @attr ref android.R.styleable#View_clickable
5341 */
5342 public void setClickable(boolean clickable) {
5343 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5344 }
5345
5346 /**
5347 * Indicates whether this view reacts to long click events or not.
5348 *
5349 * @return true if the view is long clickable, false otherwise
5350 *
5351 * @see #setLongClickable(boolean)
5352 * @attr ref android.R.styleable#View_longClickable
5353 */
5354 public boolean isLongClickable() {
5355 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5356 }
5357
5358 /**
5359 * Enables or disables long click events for this view. When a view is long
5360 * clickable it reacts to the user holding down the button for a longer
5361 * duration than a tap. This event can either launch the listener or a
5362 * context menu.
5363 *
5364 * @param longClickable true to make the view long clickable, false otherwise
5365 * @see #isLongClickable()
5366 * @attr ref android.R.styleable#View_longClickable
5367 */
5368 public void setLongClickable(boolean longClickable) {
5369 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5370 }
5371
5372 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005373 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 *
5375 * @see #isClickable()
5376 * @see #setClickable(boolean)
5377 *
5378 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5379 * the View's internal state from a previously set "pressed" state.
5380 */
5381 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005382 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 if (pressed) {
5385 mPrivateFlags |= PRESSED;
5386 } else {
5387 mPrivateFlags &= ~PRESSED;
5388 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005389
5390 if (needsRefresh) {
5391 refreshDrawableState();
5392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 dispatchSetPressed(pressed);
5394 }
5395
5396 /**
5397 * Dispatch setPressed to all of this View's children.
5398 *
5399 * @see #setPressed(boolean)
5400 *
5401 * @param pressed The new pressed state
5402 */
5403 protected void dispatchSetPressed(boolean pressed) {
5404 }
5405
5406 /**
5407 * Indicates whether the view is currently in pressed state. Unless
5408 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5409 * the pressed state.
5410 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005411 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 * @see #isClickable()
5413 * @see #setClickable(boolean)
5414 *
5415 * @return true if the view is currently pressed, false otherwise
5416 */
5417 public boolean isPressed() {
5418 return (mPrivateFlags & PRESSED) == PRESSED;
5419 }
5420
5421 /**
5422 * Indicates whether this view will save its state (that is,
5423 * whether its {@link #onSaveInstanceState} method will be called).
5424 *
5425 * @return Returns true if the view state saving is enabled, else false.
5426 *
5427 * @see #setSaveEnabled(boolean)
5428 * @attr ref android.R.styleable#View_saveEnabled
5429 */
5430 public boolean isSaveEnabled() {
5431 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5432 }
5433
5434 /**
5435 * Controls whether the saving of this view's state is
5436 * enabled (that is, whether its {@link #onSaveInstanceState} method
5437 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005438 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 * for its state to be saved. This flag can only disable the
5440 * saving of this view; any child views may still have their state saved.
5441 *
5442 * @param enabled Set to false to <em>disable</em> state saving, or true
5443 * (the default) to allow it.
5444 *
5445 * @see #isSaveEnabled()
5446 * @see #setId(int)
5447 * @see #onSaveInstanceState()
5448 * @attr ref android.R.styleable#View_saveEnabled
5449 */
5450 public void setSaveEnabled(boolean enabled) {
5451 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5452 }
5453
Jeff Brown85a31762010-09-01 17:01:00 -07005454 /**
5455 * Gets whether the framework should discard touches when the view's
5456 * window is obscured by another visible window.
5457 * Refer to the {@link View} security documentation for more details.
5458 *
5459 * @return True if touch filtering is enabled.
5460 *
5461 * @see #setFilterTouchesWhenObscured(boolean)
5462 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5463 */
5464 @ViewDebug.ExportedProperty
5465 public boolean getFilterTouchesWhenObscured() {
5466 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5467 }
5468
5469 /**
5470 * Sets whether the framework should discard touches when the view's
5471 * window is obscured by another visible window.
5472 * Refer to the {@link View} security documentation for more details.
5473 *
5474 * @param enabled True if touch filtering should be enabled.
5475 *
5476 * @see #getFilterTouchesWhenObscured
5477 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5478 */
5479 public void setFilterTouchesWhenObscured(boolean enabled) {
5480 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5481 FILTER_TOUCHES_WHEN_OBSCURED);
5482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483
5484 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005485 * Indicates whether the entire hierarchy under this view will save its
5486 * state when a state saving traversal occurs from its parent. The default
5487 * is true; if false, these views will not be saved unless
5488 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5489 *
5490 * @return Returns true if the view state saving from parent is enabled, else false.
5491 *
5492 * @see #setSaveFromParentEnabled(boolean)
5493 */
5494 public boolean isSaveFromParentEnabled() {
5495 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5496 }
5497
5498 /**
5499 * Controls whether the entire hierarchy under this view will save its
5500 * state when a state saving traversal occurs from its parent. The default
5501 * is true; if false, these views will not be saved unless
5502 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5503 *
5504 * @param enabled Set to false to <em>disable</em> state saving, or true
5505 * (the default) to allow it.
5506 *
5507 * @see #isSaveFromParentEnabled()
5508 * @see #setId(int)
5509 * @see #onSaveInstanceState()
5510 */
5511 public void setSaveFromParentEnabled(boolean enabled) {
5512 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5513 }
5514
5515
5516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 * Returns whether this View is able to take focus.
5518 *
5519 * @return True if this view can take focus, or false otherwise.
5520 * @attr ref android.R.styleable#View_focusable
5521 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005522 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 public final boolean isFocusable() {
5524 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5525 }
5526
5527 /**
5528 * When a view is focusable, it may not want to take focus when in touch mode.
5529 * For example, a button would like focus when the user is navigating via a D-pad
5530 * so that the user can click on it, but once the user starts touching the screen,
5531 * the button shouldn't take focus
5532 * @return Whether the view is focusable in touch mode.
5533 * @attr ref android.R.styleable#View_focusableInTouchMode
5534 */
5535 @ViewDebug.ExportedProperty
5536 public final boolean isFocusableInTouchMode() {
5537 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5538 }
5539
5540 /**
5541 * Find the nearest view in the specified direction that can take focus.
5542 * This does not actually give focus to that view.
5543 *
5544 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5545 *
5546 * @return The nearest focusable in the specified direction, or null if none
5547 * can be found.
5548 */
5549 public View focusSearch(int direction) {
5550 if (mParent != null) {
5551 return mParent.focusSearch(this, direction);
5552 } else {
5553 return null;
5554 }
5555 }
5556
5557 /**
5558 * This method is the last chance for the focused view and its ancestors to
5559 * respond to an arrow key. This is called when the focused view did not
5560 * consume the key internally, nor could the view system find a new view in
5561 * the requested direction to give focus to.
5562 *
5563 * @param focused The currently focused view.
5564 * @param direction The direction focus wants to move. One of FOCUS_UP,
5565 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5566 * @return True if the this view consumed this unhandled move.
5567 */
5568 public boolean dispatchUnhandledMove(View focused, int direction) {
5569 return false;
5570 }
5571
5572 /**
5573 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005574 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005576 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5577 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005578 * @return The user specified next view, or null if there is none.
5579 */
5580 View findUserSetNextFocus(View root, int direction) {
5581 switch (direction) {
5582 case FOCUS_LEFT:
5583 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005584 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 case FOCUS_RIGHT:
5586 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005587 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 case FOCUS_UP:
5589 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005590 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 case FOCUS_DOWN:
5592 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005593 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005594 case FOCUS_FORWARD:
5595 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005596 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005597 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005598 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005599 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005600 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005601 @Override
5602 public boolean apply(View t) {
5603 return t.mNextFocusForwardId == id;
5604 }
5605 });
5606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 }
5608 return null;
5609 }
5610
Jeff Brown4dfbec22011-08-15 14:55:37 -07005611 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5612 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5613 @Override
5614 public boolean apply(View t) {
5615 return t.mID == childViewId;
5616 }
5617 });
5618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 if (result == null) {
5620 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5621 + "by user for id " + childViewId);
5622 }
5623 return result;
5624 }
5625
5626 /**
5627 * Find and return all focusable views that are descendants of this view,
5628 * possibly including this view if it is focusable itself.
5629 *
5630 * @param direction The direction of the focus
5631 * @return A list of focusable views
5632 */
5633 public ArrayList<View> getFocusables(int direction) {
5634 ArrayList<View> result = new ArrayList<View>(24);
5635 addFocusables(result, direction);
5636 return result;
5637 }
5638
5639 /**
5640 * Add any focusable views that are descendants of this view (possibly
5641 * including this view if it is focusable itself) to views. If we are in touch mode,
5642 * only add views that are also focusable in touch mode.
5643 *
5644 * @param views Focusable views found so far
5645 * @param direction The direction of the focus
5646 */
5647 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005648 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650
svetoslavganov75986cf2009-05-14 22:28:01 -07005651 /**
5652 * Adds any focusable views that are descendants of this view (possibly
5653 * including this view if it is focusable itself) to views. This method
5654 * adds all focusable views regardless if we are in touch mode or
5655 * only views focusable in touch mode if we are in touch mode depending on
5656 * the focusable mode paramater.
5657 *
5658 * @param views Focusable views found so far or null if all we are interested is
5659 * the number of focusables.
5660 * @param direction The direction of the focus.
5661 * @param focusableMode The type of focusables to be added.
5662 *
5663 * @see #FOCUSABLES_ALL
5664 * @see #FOCUSABLES_TOUCH_MODE
5665 */
5666 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5667 if (!isFocusable()) {
5668 return;
5669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670
svetoslavganov75986cf2009-05-14 22:28:01 -07005671 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5672 isInTouchMode() && !isFocusableInTouchMode()) {
5673 return;
5674 }
5675
5676 if (views != null) {
5677 views.add(this);
5678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 }
5680
5681 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005682 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005683 * The search is performed by either the text that the View renders or the content
5684 * description that describes the view for accessibility purposes and the view does
5685 * not render or both. Clients can specify how the search is to be performed via
5686 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5687 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005688 *
5689 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005690 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005691 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005692 * @see #FIND_VIEWS_WITH_TEXT
5693 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5694 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005695 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005696 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005697 if (getAccessibilityNodeProvider() != null) {
5698 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5699 outViews.add(this);
5700 }
5701 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5702 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005703 String searchedLowerCase = searched.toString().toLowerCase();
5704 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5705 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5706 outViews.add(this);
5707 }
5708 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005709 }
5710
5711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 * Find and return all touchable views that are descendants of this view,
5713 * possibly including this view if it is touchable itself.
5714 *
5715 * @return A list of touchable views
5716 */
5717 public ArrayList<View> getTouchables() {
5718 ArrayList<View> result = new ArrayList<View>();
5719 addTouchables(result);
5720 return result;
5721 }
5722
5723 /**
5724 * Add any touchable views that are descendants of this view (possibly
5725 * including this view if it is touchable itself) to views.
5726 *
5727 * @param views Touchable views found so far
5728 */
5729 public void addTouchables(ArrayList<View> views) {
5730 final int viewFlags = mViewFlags;
5731
5732 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5733 && (viewFlags & ENABLED_MASK) == ENABLED) {
5734 views.add(this);
5735 }
5736 }
5737
5738 /**
5739 * Call this to try to give focus to a specific view or to one of its
5740 * descendants.
5741 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005742 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5743 * false), or if it is focusable and it is not focusable in touch mode
5744 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005746 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 * have focus, and you want your parent to look for the next one.
5748 *
5749 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5750 * {@link #FOCUS_DOWN} and <code>null</code>.
5751 *
5752 * @return Whether this view or one of its descendants actually took focus.
5753 */
5754 public final boolean requestFocus() {
5755 return requestFocus(View.FOCUS_DOWN);
5756 }
5757
5758
5759 /**
5760 * Call this to try to give focus to a specific view or to one of its
5761 * descendants and give it a hint about what direction focus is heading.
5762 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005763 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5764 * false), or if it is focusable and it is not focusable in touch mode
5765 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005767 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 * have focus, and you want your parent to look for the next one.
5769 *
5770 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5771 * <code>null</code> set for the previously focused rectangle.
5772 *
5773 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5774 * @return Whether this view or one of its descendants actually took focus.
5775 */
5776 public final boolean requestFocus(int direction) {
5777 return requestFocus(direction, null);
5778 }
5779
5780 /**
5781 * Call this to try to give focus to a specific view or to one of its descendants
5782 * and give it hints about the direction and a specific rectangle that the focus
5783 * is coming from. The rectangle can help give larger views a finer grained hint
5784 * about where focus is coming from, and therefore, where to show selection, or
5785 * forward focus change internally.
5786 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005787 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5788 * false), or if it is focusable and it is not focusable in touch mode
5789 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 *
5791 * A View will not take focus if it is not visible.
5792 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005793 * A View will not take focus if one of its parents has
5794 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5795 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005797 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 * have focus, and you want your parent to look for the next one.
5799 *
5800 * You may wish to override this method if your custom {@link View} has an internal
5801 * {@link View} that it wishes to forward the request to.
5802 *
5803 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5804 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5805 * to give a finer grained hint about where focus is coming from. May be null
5806 * if there is no hint.
5807 * @return Whether this view or one of its descendants actually took focus.
5808 */
5809 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5810 // need to be focusable
5811 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5812 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5813 return false;
5814 }
5815
5816 // need to be focusable in touch mode if in touch mode
5817 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005818 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5819 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 }
5821
5822 // need to not have any parents blocking us
5823 if (hasAncestorThatBlocksDescendantFocus()) {
5824 return false;
5825 }
5826
5827 handleFocusGainInternal(direction, previouslyFocusedRect);
5828 return true;
5829 }
5830
5831 /**
5832 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5833 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5834 * touch mode to request focus when they are touched.
5835 *
5836 * @return Whether this view or one of its descendants actually took focus.
5837 *
5838 * @see #isInTouchMode()
5839 *
5840 */
5841 public final boolean requestFocusFromTouch() {
5842 // Leave touch mode if we need to
5843 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005844 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005845 if (viewRoot != null) {
5846 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 }
5848 }
5849 return requestFocus(View.FOCUS_DOWN);
5850 }
5851
5852 /**
5853 * @return Whether any ancestor of this view blocks descendant focus.
5854 */
5855 private boolean hasAncestorThatBlocksDescendantFocus() {
5856 ViewParent ancestor = mParent;
5857 while (ancestor instanceof ViewGroup) {
5858 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5859 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5860 return true;
5861 } else {
5862 ancestor = vgAncestor.getParent();
5863 }
5864 }
5865 return false;
5866 }
5867
5868 /**
Romain Guya440b002010-02-24 15:57:54 -08005869 * @hide
5870 */
5871 public void dispatchStartTemporaryDetach() {
5872 onStartTemporaryDetach();
5873 }
5874
5875 /**
5876 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5878 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005879 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 */
5881 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005882 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005883 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005884 }
5885
5886 /**
5887 * @hide
5888 */
5889 public void dispatchFinishTemporaryDetach() {
5890 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 }
Romain Guy8506ab42009-06-11 17:35:47 -07005892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 /**
5894 * Called after {@link #onStartTemporaryDetach} when the container is done
5895 * changing the view.
5896 */
5897 public void onFinishTemporaryDetach() {
5898 }
Romain Guy8506ab42009-06-11 17:35:47 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005901 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5902 * for this view's window. Returns null if the view is not currently attached
5903 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005904 * just use the standard high-level event callbacks like
5905 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005906 */
5907 public KeyEvent.DispatcherState getKeyDispatcherState() {
5908 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5909 }
Joe Malin32736f02011-01-19 16:14:20 -08005910
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * Dispatch a key event before it is processed by any input method
5913 * associated with the view hierarchy. This can be used to intercept
5914 * key events in special situations before the IME consumes them; a
5915 * typical example would be handling the BACK key to update the application's
5916 * UI instead of allowing the IME to see it and close itself.
5917 *
5918 * @param event The key event to be dispatched.
5919 * @return True if the event was handled, false otherwise.
5920 */
5921 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5922 return onKeyPreIme(event.getKeyCode(), event);
5923 }
5924
5925 /**
5926 * Dispatch a key event to the next view on the focus path. This path runs
5927 * from the top of the view tree down to the currently focused view. If this
5928 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5929 * the next node down the focus path. This method also fires any key
5930 * listeners.
5931 *
5932 * @param event The key event to be dispatched.
5933 * @return True if the event was handled, false otherwise.
5934 */
5935 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005936 if (mInputEventConsistencyVerifier != null) {
5937 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939
Jeff Brown21bc5c92011-02-28 18:27:14 -08005940 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005941 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005942 ListenerInfo li = mListenerInfo;
5943 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5944 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005945 return true;
5946 }
5947
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005948 if (event.dispatch(this, mAttachInfo != null
5949 ? mAttachInfo.mKeyDispatchState : null, this)) {
5950 return true;
5951 }
5952
5953 if (mInputEventConsistencyVerifier != null) {
5954 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5955 }
5956 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 }
5958
5959 /**
5960 * Dispatches a key shortcut event.
5961 *
5962 * @param event The key event to be dispatched.
5963 * @return True if the event was handled by the view, false otherwise.
5964 */
5965 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5966 return onKeyShortcut(event.getKeyCode(), event);
5967 }
5968
5969 /**
5970 * Pass the touch screen motion event down to the target view, or this
5971 * view if it is the target.
5972 *
5973 * @param event The motion event to be dispatched.
5974 * @return True if the event was handled by the view, false otherwise.
5975 */
5976 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005977 if (mInputEventConsistencyVerifier != null) {
5978 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5979 }
5980
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005981 if (onFilterTouchEventForSecurity(event)) {
5982 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005983 ListenerInfo li = mListenerInfo;
5984 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5985 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005986 return true;
5987 }
5988
5989 if (onTouchEvent(event)) {
5990 return true;
5991 }
Jeff Brown85a31762010-09-01 17:01:00 -07005992 }
5993
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005994 if (mInputEventConsistencyVerifier != null) {
5995 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005997 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 }
5999
6000 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006001 * Filter the touch event to apply security policies.
6002 *
6003 * @param event The motion event to be filtered.
6004 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006005 *
Jeff Brown85a31762010-09-01 17:01:00 -07006006 * @see #getFilterTouchesWhenObscured
6007 */
6008 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006009 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006010 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6011 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6012 // Window is obscured, drop this touch.
6013 return false;
6014 }
6015 return true;
6016 }
6017
6018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 * Pass a trackball motion event down to the focused view.
6020 *
6021 * @param event The motion event to be dispatched.
6022 * @return True if the event was handled by the view, false otherwise.
6023 */
6024 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006025 if (mInputEventConsistencyVerifier != null) {
6026 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6027 }
6028
Romain Guy02ccac62011-06-24 13:20:23 -07006029 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 }
6031
6032 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006033 * Dispatch a generic motion event.
6034 * <p>
6035 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6036 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006037 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006038 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006039 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006040 *
6041 * @param event The motion event to be dispatched.
6042 * @return True if the event was handled by the view, false otherwise.
6043 */
6044 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006045 if (mInputEventConsistencyVerifier != null) {
6046 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6047 }
6048
Jeff Browna032cc02011-03-07 16:56:21 -08006049 final int source = event.getSource();
6050 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6051 final int action = event.getAction();
6052 if (action == MotionEvent.ACTION_HOVER_ENTER
6053 || action == MotionEvent.ACTION_HOVER_MOVE
6054 || action == MotionEvent.ACTION_HOVER_EXIT) {
6055 if (dispatchHoverEvent(event)) {
6056 return true;
6057 }
6058 } else if (dispatchGenericPointerEvent(event)) {
6059 return true;
6060 }
6061 } else if (dispatchGenericFocusedEvent(event)) {
6062 return true;
6063 }
6064
Jeff Brown10b62902011-06-20 16:40:37 -07006065 if (dispatchGenericMotionEventInternal(event)) {
6066 return true;
6067 }
6068
6069 if (mInputEventConsistencyVerifier != null) {
6070 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6071 }
6072 return false;
6073 }
6074
6075 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006076 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006077 ListenerInfo li = mListenerInfo;
6078 if (li != null && li.mOnGenericMotionListener != null
6079 && (mViewFlags & ENABLED_MASK) == ENABLED
6080 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006081 return true;
6082 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006083
6084 if (onGenericMotionEvent(event)) {
6085 return true;
6086 }
6087
6088 if (mInputEventConsistencyVerifier != null) {
6089 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6090 }
6091 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006092 }
6093
6094 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006095 * Dispatch a hover event.
6096 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006097 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006098 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006099 * </p>
6100 *
6101 * @param event The motion event to be dispatched.
6102 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006103 */
6104 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006105 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006106 ListenerInfo li = mListenerInfo;
6107 if (li != null && li.mOnHoverListener != null
6108 && (mViewFlags & ENABLED_MASK) == ENABLED
6109 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006110 return true;
6111 }
6112
Jeff Browna032cc02011-03-07 16:56:21 -08006113 return onHoverEvent(event);
6114 }
6115
6116 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006117 * Returns true if the view has a child to which it has recently sent
6118 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6119 * it does not have a hovered child, then it must be the innermost hovered view.
6120 * @hide
6121 */
6122 protected boolean hasHoveredChild() {
6123 return false;
6124 }
6125
6126 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006127 * Dispatch a generic motion event to the view under the first pointer.
6128 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006129 * Do not call this method directly.
6130 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006131 * </p>
6132 *
6133 * @param event The motion event to be dispatched.
6134 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006135 */
6136 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6137 return false;
6138 }
6139
6140 /**
6141 * Dispatch a generic motion event to the currently focused view.
6142 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006143 * Do not call this method directly.
6144 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006145 * </p>
6146 *
6147 * @param event The motion event to be dispatched.
6148 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006149 */
6150 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6151 return false;
6152 }
6153
6154 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006155 * Dispatch a pointer event.
6156 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006157 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6158 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6159 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006160 * and should not be expected to handle other pointing device features.
6161 * </p>
6162 *
6163 * @param event The motion event to be dispatched.
6164 * @return True if the event was handled by the view, false otherwise.
6165 * @hide
6166 */
6167 public final boolean dispatchPointerEvent(MotionEvent event) {
6168 if (event.isTouchEvent()) {
6169 return dispatchTouchEvent(event);
6170 } else {
6171 return dispatchGenericMotionEvent(event);
6172 }
6173 }
6174
6175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 * Called when the window containing this view gains or loses window focus.
6177 * ViewGroups should override to route to their children.
6178 *
6179 * @param hasFocus True if the window containing this view now has focus,
6180 * false otherwise.
6181 */
6182 public void dispatchWindowFocusChanged(boolean hasFocus) {
6183 onWindowFocusChanged(hasFocus);
6184 }
6185
6186 /**
6187 * Called when the window containing this view gains or loses focus. Note
6188 * that this is separate from view focus: to receive key events, both
6189 * your view and its window must have focus. If a window is displayed
6190 * on top of yours that takes input focus, then your own window will lose
6191 * focus but the view focus will remain unchanged.
6192 *
6193 * @param hasWindowFocus True if the window containing this view now has
6194 * focus, false otherwise.
6195 */
6196 public void onWindowFocusChanged(boolean hasWindowFocus) {
6197 InputMethodManager imm = InputMethodManager.peekInstance();
6198 if (!hasWindowFocus) {
6199 if (isPressed()) {
6200 setPressed(false);
6201 }
6202 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6203 imm.focusOut(this);
6204 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006205 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006206 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006207 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6209 imm.focusIn(this);
6210 }
6211 refreshDrawableState();
6212 }
6213
6214 /**
6215 * Returns true if this view is in a window that currently has window focus.
6216 * Note that this is not the same as the view itself having focus.
6217 *
6218 * @return True if this view is in a window that currently has window focus.
6219 */
6220 public boolean hasWindowFocus() {
6221 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6222 }
6223
6224 /**
Adam Powell326d8082009-12-09 15:10:07 -08006225 * Dispatch a view visibility change down the view hierarchy.
6226 * ViewGroups should override to route to their children.
6227 * @param changedView The view whose visibility changed. Could be 'this' or
6228 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006229 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6230 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006231 */
6232 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6233 onVisibilityChanged(changedView, visibility);
6234 }
6235
6236 /**
6237 * Called when the visibility of the view or an ancestor of the view is changed.
6238 * @param changedView The view whose visibility changed. Could be 'this' or
6239 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006240 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6241 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006242 */
6243 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006244 if (visibility == VISIBLE) {
6245 if (mAttachInfo != null) {
6246 initialAwakenScrollBars();
6247 } else {
6248 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6249 }
6250 }
Adam Powell326d8082009-12-09 15:10:07 -08006251 }
6252
6253 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006254 * Dispatch a hint about whether this view is displayed. For instance, when
6255 * a View moves out of the screen, it might receives a display hint indicating
6256 * the view is not displayed. Applications should not <em>rely</em> on this hint
6257 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006258 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006259 * @param hint A hint about whether or not this view is displayed:
6260 * {@link #VISIBLE} or {@link #INVISIBLE}.
6261 */
6262 public void dispatchDisplayHint(int hint) {
6263 onDisplayHint(hint);
6264 }
6265
6266 /**
6267 * Gives this view a hint about whether is displayed or not. For instance, when
6268 * a View moves out of the screen, it might receives a display hint indicating
6269 * the view is not displayed. Applications should not <em>rely</em> on this hint
6270 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006271 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006272 * @param hint A hint about whether or not this view is displayed:
6273 * {@link #VISIBLE} or {@link #INVISIBLE}.
6274 */
6275 protected void onDisplayHint(int hint) {
6276 }
6277
6278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 * Dispatch a window visibility change down the view hierarchy.
6280 * ViewGroups should override to route to their children.
6281 *
6282 * @param visibility The new visibility of the window.
6283 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006284 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 */
6286 public void dispatchWindowVisibilityChanged(int visibility) {
6287 onWindowVisibilityChanged(visibility);
6288 }
6289
6290 /**
6291 * Called when the window containing has change its visibility
6292 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6293 * that this tells you whether or not your window is being made visible
6294 * to the window manager; this does <em>not</em> tell you whether or not
6295 * your window is obscured by other windows on the screen, even if it
6296 * is itself visible.
6297 *
6298 * @param visibility The new visibility of the window.
6299 */
6300 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006301 if (visibility == VISIBLE) {
6302 initialAwakenScrollBars();
6303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 }
6305
6306 /**
6307 * Returns the current visibility of the window this view is attached to
6308 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6309 *
6310 * @return Returns the current visibility of the view's window.
6311 */
6312 public int getWindowVisibility() {
6313 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6314 }
6315
6316 /**
6317 * Retrieve the overall visible display size in which the window this view is
6318 * attached to has been positioned in. This takes into account screen
6319 * decorations above the window, for both cases where the window itself
6320 * is being position inside of them or the window is being placed under
6321 * then and covered insets are used for the window to position its content
6322 * inside. In effect, this tells you the available area where content can
6323 * be placed and remain visible to users.
6324 *
6325 * <p>This function requires an IPC back to the window manager to retrieve
6326 * the requested information, so should not be used in performance critical
6327 * code like drawing.
6328 *
6329 * @param outRect Filled in with the visible display frame. If the view
6330 * is not attached to a window, this is simply the raw display size.
6331 */
6332 public void getWindowVisibleDisplayFrame(Rect outRect) {
6333 if (mAttachInfo != null) {
6334 try {
6335 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6336 } catch (RemoteException e) {
6337 return;
6338 }
6339 // XXX This is really broken, and probably all needs to be done
6340 // in the window manager, and we need to know more about whether
6341 // we want the area behind or in front of the IME.
6342 final Rect insets = mAttachInfo.mVisibleInsets;
6343 outRect.left += insets.left;
6344 outRect.top += insets.top;
6345 outRect.right -= insets.right;
6346 outRect.bottom -= insets.bottom;
6347 return;
6348 }
6349 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006350 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 }
6352
6353 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006354 * Dispatch a notification about a resource configuration change down
6355 * the view hierarchy.
6356 * ViewGroups should override to route to their children.
6357 *
6358 * @param newConfig The new resource configuration.
6359 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006360 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006361 */
6362 public void dispatchConfigurationChanged(Configuration newConfig) {
6363 onConfigurationChanged(newConfig);
6364 }
6365
6366 /**
6367 * Called when the current configuration of the resources being used
6368 * by the application have changed. You can use this to decide when
6369 * to reload resources that can changed based on orientation and other
6370 * configuration characterstics. You only need to use this if you are
6371 * not relying on the normal {@link android.app.Activity} mechanism of
6372 * recreating the activity instance upon a configuration change.
6373 *
6374 * @param newConfig The new resource configuration.
6375 */
6376 protected void onConfigurationChanged(Configuration newConfig) {
6377 }
6378
6379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 * Private function to aggregate all per-view attributes in to the view
6381 * root.
6382 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006383 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6384 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 }
6386
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006387 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6388 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006389 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006390 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006391 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006392 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006393 ListenerInfo li = mListenerInfo;
6394 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006395 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 }
6398 }
6399
6400 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006401 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006403 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6404 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 ai.mRecomputeGlobalAttributes = true;
6406 }
6407 }
6408 }
6409
6410 /**
6411 * Returns whether the device is currently in touch mode. Touch mode is entered
6412 * once the user begins interacting with the device by touch, and affects various
6413 * things like whether focus is always visible to the user.
6414 *
6415 * @return Whether the device is in touch mode.
6416 */
6417 @ViewDebug.ExportedProperty
6418 public boolean isInTouchMode() {
6419 if (mAttachInfo != null) {
6420 return mAttachInfo.mInTouchMode;
6421 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006422 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 }
6424 }
6425
6426 /**
6427 * Returns the context the view is running in, through which it can
6428 * access the current theme, resources, etc.
6429 *
6430 * @return The view's Context.
6431 */
6432 @ViewDebug.CapturedViewProperty
6433 public final Context getContext() {
6434 return mContext;
6435 }
6436
6437 /**
6438 * Handle a key event before it is processed by any input method
6439 * associated with the view hierarchy. This can be used to intercept
6440 * key events in special situations before the IME consumes them; a
6441 * typical example would be handling the BACK key to update the application's
6442 * UI instead of allowing the IME to see it and close itself.
6443 *
6444 * @param keyCode The value in event.getKeyCode().
6445 * @param event Description of the key event.
6446 * @return If you handled the event, return true. If you want to allow the
6447 * event to be handled by the next receiver, return false.
6448 */
6449 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6450 return false;
6451 }
6452
6453 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006454 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6455 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6457 * is released, if the view is enabled and clickable.
6458 *
6459 * @param keyCode A key code that represents the button pressed, from
6460 * {@link android.view.KeyEvent}.
6461 * @param event The KeyEvent object that defines the button action.
6462 */
6463 public boolean onKeyDown(int keyCode, KeyEvent event) {
6464 boolean result = false;
6465
6466 switch (keyCode) {
6467 case KeyEvent.KEYCODE_DPAD_CENTER:
6468 case KeyEvent.KEYCODE_ENTER: {
6469 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6470 return true;
6471 }
6472 // Long clickable items don't necessarily have to be clickable
6473 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6474 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6475 (event.getRepeatCount() == 0)) {
6476 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006477 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 return true;
6479 }
6480 break;
6481 }
6482 }
6483 return result;
6484 }
6485
6486 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006487 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6488 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6489 * the event).
6490 */
6491 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6492 return false;
6493 }
6494
6495 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006496 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6497 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6499 * {@link KeyEvent#KEYCODE_ENTER} is released.
6500 *
6501 * @param keyCode A key code that represents the button pressed, from
6502 * {@link android.view.KeyEvent}.
6503 * @param event The KeyEvent object that defines the button action.
6504 */
6505 public boolean onKeyUp(int keyCode, KeyEvent event) {
6506 boolean result = false;
6507
6508 switch (keyCode) {
6509 case KeyEvent.KEYCODE_DPAD_CENTER:
6510 case KeyEvent.KEYCODE_ENTER: {
6511 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6512 return true;
6513 }
6514 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6515 setPressed(false);
6516
6517 if (!mHasPerformedLongPress) {
6518 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006519 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520
6521 result = performClick();
6522 }
6523 }
6524 break;
6525 }
6526 }
6527 return result;
6528 }
6529
6530 /**
6531 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6532 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6533 * the event).
6534 *
6535 * @param keyCode A key code that represents the button pressed, from
6536 * {@link android.view.KeyEvent}.
6537 * @param repeatCount The number of times the action was made.
6538 * @param event The KeyEvent object that defines the button action.
6539 */
6540 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6541 return false;
6542 }
6543
6544 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006545 * Called on the focused view when a key shortcut event is not handled.
6546 * Override this method to implement local key shortcuts for the View.
6547 * Key shortcuts can also be implemented by setting the
6548 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 *
6550 * @param keyCode The value in event.getKeyCode().
6551 * @param event Description of the key event.
6552 * @return If you handled the event, return true. If you want to allow the
6553 * event to be handled by the next receiver, return false.
6554 */
6555 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6556 return false;
6557 }
6558
6559 /**
6560 * Check whether the called view is a text editor, in which case it
6561 * would make sense to automatically display a soft input window for
6562 * it. Subclasses should override this if they implement
6563 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006564 * a call on that method would return a non-null InputConnection, and
6565 * they are really a first-class editor that the user would normally
6566 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006567 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006568 * <p>The default implementation always returns false. This does
6569 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6570 * will not be called or the user can not otherwise perform edits on your
6571 * view; it is just a hint to the system that this is not the primary
6572 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 * @return Returns true if this view is a text editor, else false.
6575 */
6576 public boolean onCheckIsTextEditor() {
6577 return false;
6578 }
Romain Guy8506ab42009-06-11 17:35:47 -07006579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 /**
6581 * Create a new InputConnection for an InputMethod to interact
6582 * with the view. The default implementation returns null, since it doesn't
6583 * support input methods. You can override this to implement such support.
6584 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006585 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 * <p>When implementing this, you probably also want to implement
6587 * {@link #onCheckIsTextEditor()} to indicate you will return a
6588 * non-null InputConnection.
6589 *
6590 * @param outAttrs Fill in with attribute information about the connection.
6591 */
6592 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6593 return null;
6594 }
6595
6596 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006597 * Called by the {@link android.view.inputmethod.InputMethodManager}
6598 * when a view who is not the current
6599 * input connection target is trying to make a call on the manager. The
6600 * default implementation returns false; you can override this to return
6601 * true for certain views if you are performing InputConnection proxying
6602 * to them.
6603 * @param view The View that is making the InputMethodManager call.
6604 * @return Return true to allow the call, false to reject.
6605 */
6606 public boolean checkInputConnectionProxy(View view) {
6607 return false;
6608 }
Romain Guy8506ab42009-06-11 17:35:47 -07006609
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 * Show the context menu for this view. It is not safe to hold on to the
6612 * menu after returning from this method.
6613 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006614 * You should normally not overload this method. Overload
6615 * {@link #onCreateContextMenu(ContextMenu)} or define an
6616 * {@link OnCreateContextMenuListener} to add items to the context menu.
6617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 * @param menu The context menu to populate
6619 */
6620 public void createContextMenu(ContextMenu menu) {
6621 ContextMenuInfo menuInfo = getContextMenuInfo();
6622
6623 // Sets the current menu info so all items added to menu will have
6624 // my extra info set.
6625 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6626
6627 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006628 ListenerInfo li = mListenerInfo;
6629 if (li != null && li.mOnCreateContextMenuListener != null) {
6630 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 }
6632
6633 // Clear the extra information so subsequent items that aren't mine don't
6634 // have my extra info.
6635 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6636
6637 if (mParent != null) {
6638 mParent.createContextMenu(menu);
6639 }
6640 }
6641
6642 /**
6643 * Views should implement this if they have extra information to associate
6644 * with the context menu. The return result is supplied as a parameter to
6645 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6646 * callback.
6647 *
6648 * @return Extra information about the item for which the context menu
6649 * should be shown. This information will vary across different
6650 * subclasses of View.
6651 */
6652 protected ContextMenuInfo getContextMenuInfo() {
6653 return null;
6654 }
6655
6656 /**
6657 * Views should implement this if the view itself is going to add items to
6658 * the context menu.
6659 *
6660 * @param menu the context menu to populate
6661 */
6662 protected void onCreateContextMenu(ContextMenu menu) {
6663 }
6664
6665 /**
6666 * Implement this method to handle trackball motion events. The
6667 * <em>relative</em> movement of the trackball since the last event
6668 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6669 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6670 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6671 * they will often be fractional values, representing the more fine-grained
6672 * movement information available from a trackball).
6673 *
6674 * @param event The motion event.
6675 * @return True if the event was handled, false otherwise.
6676 */
6677 public boolean onTrackballEvent(MotionEvent event) {
6678 return false;
6679 }
6680
6681 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006682 * Implement this method to handle generic motion events.
6683 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006684 * Generic motion events describe joystick movements, mouse hovers, track pad
6685 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006686 * {@link MotionEvent#getSource() source} of the motion event specifies
6687 * the class of input that was received. Implementations of this method
6688 * must examine the bits in the source before processing the event.
6689 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006690 * </p><p>
6691 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6692 * are delivered to the view under the pointer. All other generic motion events are
6693 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006694 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006695 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006696 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006697 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6698 * // process the joystick movement...
6699 * return true;
6700 * }
6701 * }
6702 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6703 * switch (event.getAction()) {
6704 * case MotionEvent.ACTION_HOVER_MOVE:
6705 * // process the mouse hover movement...
6706 * return true;
6707 * case MotionEvent.ACTION_SCROLL:
6708 * // process the scroll wheel movement...
6709 * return true;
6710 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006711 * }
6712 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006713 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006714 *
6715 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006716 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006717 */
6718 public boolean onGenericMotionEvent(MotionEvent event) {
6719 return false;
6720 }
6721
6722 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006723 * Implement this method to handle hover events.
6724 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006725 * This method is called whenever a pointer is hovering into, over, or out of the
6726 * bounds of a view and the view is not currently being touched.
6727 * Hover events are represented as pointer events with action
6728 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6729 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6730 * </p>
6731 * <ul>
6732 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6733 * when the pointer enters the bounds of the view.</li>
6734 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6735 * when the pointer has already entered the bounds of the view and has moved.</li>
6736 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6737 * when the pointer has exited the bounds of the view or when the pointer is
6738 * about to go down due to a button click, tap, or similar user action that
6739 * causes the view to be touched.</li>
6740 * </ul>
6741 * <p>
6742 * The view should implement this method to return true to indicate that it is
6743 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006744 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006745 * The default implementation calls {@link #setHovered} to update the hovered state
6746 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006747 * is enabled and is clickable. The default implementation also sends hover
6748 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006749 * </p>
6750 *
6751 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006752 * @return True if the view handled the hover event.
6753 *
6754 * @see #isHovered
6755 * @see #setHovered
6756 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006757 */
6758 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006759 // The root view may receive hover (or touch) events that are outside the bounds of
6760 // the window. This code ensures that we only send accessibility events for
6761 // hovers that are actually within the bounds of the root view.
6762 final int action = event.getAction();
6763 if (!mSendingHoverAccessibilityEvents) {
6764 if ((action == MotionEvent.ACTION_HOVER_ENTER
6765 || action == MotionEvent.ACTION_HOVER_MOVE)
6766 && !hasHoveredChild()
6767 && pointInView(event.getX(), event.getY())) {
6768 mSendingHoverAccessibilityEvents = true;
6769 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6770 }
6771 } else {
6772 if (action == MotionEvent.ACTION_HOVER_EXIT
6773 || (action == MotionEvent.ACTION_HOVER_MOVE
6774 && !pointInView(event.getX(), event.getY()))) {
6775 mSendingHoverAccessibilityEvents = false;
6776 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6777 }
Jeff Browna1b24182011-07-28 13:38:24 -07006778 }
6779
Jeff Brown87b7f802011-06-21 18:35:45 -07006780 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006781 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006782 case MotionEvent.ACTION_HOVER_ENTER:
6783 setHovered(true);
6784 break;
6785 case MotionEvent.ACTION_HOVER_EXIT:
6786 setHovered(false);
6787 break;
6788 }
Jeff Browna1b24182011-07-28 13:38:24 -07006789
6790 // Dispatch the event to onGenericMotionEvent before returning true.
6791 // This is to provide compatibility with existing applications that
6792 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6793 // break because of the new default handling for hoverable views
6794 // in onHoverEvent.
6795 // Note that onGenericMotionEvent will be called by default when
6796 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6797 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006798 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006799 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006800 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006801 }
6802
6803 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006804 * Returns true if the view should handle {@link #onHoverEvent}
6805 * by calling {@link #setHovered} to change its hovered state.
6806 *
6807 * @return True if the view is hoverable.
6808 */
6809 private boolean isHoverable() {
6810 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006811 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006812 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6813 return false;
6814 }
6815
6816 return (viewFlags & CLICKABLE) == CLICKABLE
6817 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6818 }
6819
6820 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006821 * Returns true if the view is currently hovered.
6822 *
6823 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006824 *
6825 * @see #setHovered
6826 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006827 */
Jeff Brown10b62902011-06-20 16:40:37 -07006828 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006829 public boolean isHovered() {
6830 return (mPrivateFlags & HOVERED) != 0;
6831 }
6832
6833 /**
6834 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006835 * <p>
6836 * Calling this method also changes the drawable state of the view. This
6837 * enables the view to react to hover by using different drawable resources
6838 * to change its appearance.
6839 * </p><p>
6840 * The {@link #onHoverChanged} method is called when the hovered state changes.
6841 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006842 *
6843 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006844 *
6845 * @see #isHovered
6846 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006847 */
6848 public void setHovered(boolean hovered) {
6849 if (hovered) {
6850 if ((mPrivateFlags & HOVERED) == 0) {
6851 mPrivateFlags |= HOVERED;
6852 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006853 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006854 }
6855 } else {
6856 if ((mPrivateFlags & HOVERED) != 0) {
6857 mPrivateFlags &= ~HOVERED;
6858 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006859 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006860 }
6861 }
6862 }
6863
6864 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006865 * Implement this method to handle hover state changes.
6866 * <p>
6867 * This method is called whenever the hover state changes as a result of a
6868 * call to {@link #setHovered}.
6869 * </p>
6870 *
6871 * @param hovered The current hover state, as returned by {@link #isHovered}.
6872 *
6873 * @see #isHovered
6874 * @see #setHovered
6875 */
6876 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006877 }
6878
6879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 * Implement this method to handle touch screen motion events.
6881 *
6882 * @param event The motion event.
6883 * @return True if the event was handled, false otherwise.
6884 */
6885 public boolean onTouchEvent(MotionEvent event) {
6886 final int viewFlags = mViewFlags;
6887
6888 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006889 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006890 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 // A disabled view that is clickable still consumes the touch
6893 // events, it just doesn't respond to them.
6894 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6895 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6896 }
6897
6898 if (mTouchDelegate != null) {
6899 if (mTouchDelegate.onTouchEvent(event)) {
6900 return true;
6901 }
6902 }
6903
6904 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6905 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6906 switch (event.getAction()) {
6907 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006908 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6909 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 // take focus if we don't have it already and we should in
6911 // touch mode.
6912 boolean focusTaken = false;
6913 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6914 focusTaken = requestFocus();
6915 }
6916
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006917 if (prepressed) {
6918 // The button is being released before we actually
6919 // showed it as pressed. Make it show the pressed
6920 // state now (before scheduling the click) to ensure
6921 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006922 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006923 }
Joe Malin32736f02011-01-19 16:14:20 -08006924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 if (!mHasPerformedLongPress) {
6926 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006927 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928
6929 // Only perform take click actions if we were in the pressed state
6930 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006931 // Use a Runnable and post this rather than calling
6932 // performClick directly. This lets other visual state
6933 // of the view update before click actions start.
6934 if (mPerformClick == null) {
6935 mPerformClick = new PerformClick();
6936 }
6937 if (!post(mPerformClick)) {
6938 performClick();
6939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 }
6941 }
6942
6943 if (mUnsetPressedState == null) {
6944 mUnsetPressedState = new UnsetPressedState();
6945 }
6946
Adam Powelle14579b2009-12-16 18:39:52 -08006947 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006948 postDelayed(mUnsetPressedState,
6949 ViewConfiguration.getPressedStateDuration());
6950 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 // If the post failed, unpress right now
6952 mUnsetPressedState.run();
6953 }
Adam Powelle14579b2009-12-16 18:39:52 -08006954 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 }
6956 break;
6957
6958 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006959 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006960
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006961 if (performButtonActionOnTouchDown(event)) {
6962 break;
6963 }
6964
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006965 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006966 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006967
6968 // For views inside a scrolling container, delay the pressed feedback for
6969 // a short period in case this is a scroll.
6970 if (isInScrollingContainer) {
6971 mPrivateFlags |= PREPRESSED;
6972 if (mPendingCheckForTap == null) {
6973 mPendingCheckForTap = new CheckForTap();
6974 }
6975 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6976 } else {
6977 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006978 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006979 checkForLongClick(0);
6980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 break;
6982
6983 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006984 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006985 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 break;
6987
6988 case MotionEvent.ACTION_MOVE:
6989 final int x = (int) event.getX();
6990 final int y = (int) event.getY();
6991
6992 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006993 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006995 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006997 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006998 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999
Adam Powell4d6f0662012-02-21 15:11:11 -08007000 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 }
7003 break;
7004 }
7005 return true;
7006 }
7007
7008 return false;
7009 }
7010
7011 /**
Adam Powell10298662011-08-14 18:26:30 -07007012 * @hide
7013 */
7014 public boolean isInScrollingContainer() {
7015 ViewParent p = getParent();
7016 while (p != null && p instanceof ViewGroup) {
7017 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7018 return true;
7019 }
7020 p = p.getParent();
7021 }
7022 return false;
7023 }
7024
7025 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007026 * Remove the longpress detection timer.
7027 */
7028 private void removeLongPressCallback() {
7029 if (mPendingCheckForLongPress != null) {
7030 removeCallbacks(mPendingCheckForLongPress);
7031 }
7032 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007033
7034 /**
7035 * Remove the pending click action
7036 */
7037 private void removePerformClickCallback() {
7038 if (mPerformClick != null) {
7039 removeCallbacks(mPerformClick);
7040 }
7041 }
7042
Adam Powelle14579b2009-12-16 18:39:52 -08007043 /**
Romain Guya440b002010-02-24 15:57:54 -08007044 * Remove the prepress detection timer.
7045 */
7046 private void removeUnsetPressCallback() {
7047 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7048 setPressed(false);
7049 removeCallbacks(mUnsetPressedState);
7050 }
7051 }
7052
7053 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007054 * Remove the tap detection timer.
7055 */
7056 private void removeTapCallback() {
7057 if (mPendingCheckForTap != null) {
7058 mPrivateFlags &= ~PREPRESSED;
7059 removeCallbacks(mPendingCheckForTap);
7060 }
7061 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007062
7063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 * Cancels a pending long press. Your subclass can use this if you
7065 * want the context menu to come up if the user presses and holds
7066 * at the same place, but you don't want it to come up if they press
7067 * and then move around enough to cause scrolling.
7068 */
7069 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007070 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007071
7072 /*
7073 * The prepressed state handled by the tap callback is a display
7074 * construct, but the tap callback will post a long press callback
7075 * less its own timeout. Remove it here.
7076 */
7077 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 }
7079
7080 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007081 * Remove the pending callback for sending a
7082 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7083 */
7084 private void removeSendViewScrolledAccessibilityEventCallback() {
7085 if (mSendViewScrolledAccessibilityEvent != null) {
7086 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7087 }
7088 }
7089
7090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 * Sets the TouchDelegate for this View.
7092 */
7093 public void setTouchDelegate(TouchDelegate delegate) {
7094 mTouchDelegate = delegate;
7095 }
7096
7097 /**
7098 * Gets the TouchDelegate for this View.
7099 */
7100 public TouchDelegate getTouchDelegate() {
7101 return mTouchDelegate;
7102 }
7103
7104 /**
7105 * Set flags controlling behavior of this view.
7106 *
7107 * @param flags Constant indicating the value which should be set
7108 * @param mask Constant indicating the bit range that should be changed
7109 */
7110 void setFlags(int flags, int mask) {
7111 int old = mViewFlags;
7112 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7113
7114 int changed = mViewFlags ^ old;
7115 if (changed == 0) {
7116 return;
7117 }
7118 int privateFlags = mPrivateFlags;
7119
7120 /* Check if the FOCUSABLE bit has changed */
7121 if (((changed & FOCUSABLE_MASK) != 0) &&
7122 ((privateFlags & HAS_BOUNDS) !=0)) {
7123 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7124 && ((privateFlags & FOCUSED) != 0)) {
7125 /* Give up focus if we are no longer focusable */
7126 clearFocus();
7127 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7128 && ((privateFlags & FOCUSED) == 0)) {
7129 /*
7130 * Tell the view system that we are now available to take focus
7131 * if no one else already has it.
7132 */
7133 if (mParent != null) mParent.focusableViewAvailable(this);
7134 }
7135 }
7136
7137 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7138 if ((changed & VISIBILITY_MASK) != 0) {
7139 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007140 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007141 * it was not visible. Marking it drawn ensures that the invalidation will
7142 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007144 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007145 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146
7147 needGlobalAttributesUpdate(true);
7148
7149 // a view becoming visible is worth notifying the parent
7150 // about in case nothing has focus. even if this specific view
7151 // isn't focusable, it may contain something that is, so let
7152 // the root view try to give this focus if nothing else does.
7153 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7154 mParent.focusableViewAvailable(this);
7155 }
7156 }
7157 }
7158
7159 /* Check if the GONE bit has changed */
7160 if ((changed & GONE) != 0) {
7161 needGlobalAttributesUpdate(false);
7162 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007163
Romain Guyecd80ee2009-12-03 17:13:02 -08007164 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7165 if (hasFocus()) clearFocus();
7166 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007167 if (mParent instanceof View) {
7168 // GONE views noop invalidation, so invalidate the parent
7169 ((View) mParent).invalidate(true);
7170 }
7171 // Mark the view drawn to ensure that it gets invalidated properly the next
7172 // time it is visible and gets invalidated
7173 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 }
7175 if (mAttachInfo != null) {
7176 mAttachInfo.mViewVisibilityChanged = true;
7177 }
7178 }
7179
7180 /* Check if the VISIBLE bit has changed */
7181 if ((changed & INVISIBLE) != 0) {
7182 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007183 /*
7184 * If this view is becoming invisible, set the DRAWN flag so that
7185 * the next invalidate() will not be skipped.
7186 */
7187 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188
7189 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
7190 // root view becoming invisible shouldn't clear focus
7191 if (getRootView() != this) {
7192 clearFocus();
7193 }
7194 }
7195 if (mAttachInfo != null) {
7196 mAttachInfo.mViewVisibilityChanged = true;
7197 }
7198 }
7199
Adam Powell326d8082009-12-09 15:10:07 -08007200 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007201 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007202 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7203 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007204 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007205 } else if (mParent != null) {
7206 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007207 }
Adam Powell326d8082009-12-09 15:10:07 -08007208 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7209 }
7210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7212 destroyDrawingCache();
7213 }
7214
7215 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7216 destroyDrawingCache();
7217 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007218 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 }
7220
7221 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7222 destroyDrawingCache();
7223 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7224 }
7225
7226 if ((changed & DRAW_MASK) != 0) {
7227 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007228 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 mPrivateFlags &= ~SKIP_DRAW;
7230 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7231 } else {
7232 mPrivateFlags |= SKIP_DRAW;
7233 }
7234 } else {
7235 mPrivateFlags &= ~SKIP_DRAW;
7236 }
7237 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007238 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 }
7240
7241 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007242 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 mParent.recomputeViewAttributes(this);
7244 }
7245 }
7246 }
7247
7248 /**
7249 * Change the view's z order in the tree, so it's on top of other sibling
7250 * views
7251 */
7252 public void bringToFront() {
7253 if (mParent != null) {
7254 mParent.bringChildToFront(this);
7255 }
7256 }
7257
7258 /**
7259 * This is called in response to an internal scroll in this view (i.e., the
7260 * view scrolled its own contents). This is typically as a result of
7261 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7262 * called.
7263 *
7264 * @param l Current horizontal scroll origin.
7265 * @param t Current vertical scroll origin.
7266 * @param oldl Previous horizontal scroll origin.
7267 * @param oldt Previous vertical scroll origin.
7268 */
7269 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007270 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7271 postSendViewScrolledAccessibilityEventCallback();
7272 }
7273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 mBackgroundSizeChanged = true;
7275
7276 final AttachInfo ai = mAttachInfo;
7277 if (ai != null) {
7278 ai.mViewScrollChanged = true;
7279 }
7280 }
7281
7282 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007283 * Interface definition for a callback to be invoked when the layout bounds of a view
7284 * changes due to layout processing.
7285 */
7286 public interface OnLayoutChangeListener {
7287 /**
7288 * Called when the focus state of a view has changed.
7289 *
7290 * @param v The view whose state has changed.
7291 * @param left The new value of the view's left property.
7292 * @param top The new value of the view's top property.
7293 * @param right The new value of the view's right property.
7294 * @param bottom The new value of the view's bottom property.
7295 * @param oldLeft The previous value of the view's left property.
7296 * @param oldTop The previous value of the view's top property.
7297 * @param oldRight The previous value of the view's right property.
7298 * @param oldBottom The previous value of the view's bottom property.
7299 */
7300 void onLayoutChange(View v, int left, int top, int right, int bottom,
7301 int oldLeft, int oldTop, int oldRight, int oldBottom);
7302 }
7303
7304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 * This is called during layout when the size of this view has changed. If
7306 * you were just added to the view hierarchy, you're called with the old
7307 * values of 0.
7308 *
7309 * @param w Current width of this view.
7310 * @param h Current height of this view.
7311 * @param oldw Old width of this view.
7312 * @param oldh Old height of this view.
7313 */
7314 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7315 }
7316
7317 /**
7318 * Called by draw to draw the child views. This may be overridden
7319 * by derived classes to gain control just before its children are drawn
7320 * (but after its own view has been drawn).
7321 * @param canvas the canvas on which to draw the view
7322 */
7323 protected void dispatchDraw(Canvas canvas) {
7324 }
7325
7326 /**
7327 * Gets the parent of this view. Note that the parent is a
7328 * ViewParent and not necessarily a View.
7329 *
7330 * @return Parent of this view.
7331 */
7332 public final ViewParent getParent() {
7333 return mParent;
7334 }
7335
7336 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007337 * Set the horizontal scrolled position of your view. This will cause a call to
7338 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7339 * invalidated.
7340 * @param value the x position to scroll to
7341 */
7342 public void setScrollX(int value) {
7343 scrollTo(value, mScrollY);
7344 }
7345
7346 /**
7347 * Set the vertical scrolled position of your view. This will cause a call to
7348 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7349 * invalidated.
7350 * @param value the y position to scroll to
7351 */
7352 public void setScrollY(int value) {
7353 scrollTo(mScrollX, value);
7354 }
7355
7356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 * Return the scrolled left position of this view. This is the left edge of
7358 * the displayed part of your view. You do not need to draw any pixels
7359 * farther left, since those are outside of the frame of your view on
7360 * screen.
7361 *
7362 * @return The left edge of the displayed part of your view, in pixels.
7363 */
7364 public final int getScrollX() {
7365 return mScrollX;
7366 }
7367
7368 /**
7369 * Return the scrolled top position of this view. This is the top edge of
7370 * the displayed part of your view. You do not need to draw any pixels above
7371 * it, since those are outside of the frame of your view on screen.
7372 *
7373 * @return The top edge of the displayed part of your view, in pixels.
7374 */
7375 public final int getScrollY() {
7376 return mScrollY;
7377 }
7378
7379 /**
7380 * Return the width of the your view.
7381 *
7382 * @return The width of your view, in pixels.
7383 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007384 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 public final int getWidth() {
7386 return mRight - mLeft;
7387 }
7388
7389 /**
7390 * Return the height of your view.
7391 *
7392 * @return The height of your view, in pixels.
7393 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007394 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 public final int getHeight() {
7396 return mBottom - mTop;
7397 }
7398
7399 /**
7400 * Return the visible drawing bounds of your view. Fills in the output
7401 * rectangle with the values from getScrollX(), getScrollY(),
7402 * getWidth(), and getHeight().
7403 *
7404 * @param outRect The (scrolled) drawing bounds of the view.
7405 */
7406 public void getDrawingRect(Rect outRect) {
7407 outRect.left = mScrollX;
7408 outRect.top = mScrollY;
7409 outRect.right = mScrollX + (mRight - mLeft);
7410 outRect.bottom = mScrollY + (mBottom - mTop);
7411 }
7412
7413 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007414 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7415 * raw width component (that is the result is masked by
7416 * {@link #MEASURED_SIZE_MASK}).
7417 *
7418 * @return The raw measured width of this view.
7419 */
7420 public final int getMeasuredWidth() {
7421 return mMeasuredWidth & MEASURED_SIZE_MASK;
7422 }
7423
7424 /**
7425 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007426 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007427 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 * This should be used during measurement and layout calculations only. Use
7429 * {@link #getWidth()} to see how wide a view is after layout.
7430 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007431 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007433 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 return mMeasuredWidth;
7435 }
7436
7437 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007438 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7439 * raw width component (that is the result is masked by
7440 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007442 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 */
7444 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007445 return mMeasuredHeight & MEASURED_SIZE_MASK;
7446 }
7447
7448 /**
7449 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007450 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007451 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7452 * This should be used during measurement and layout calculations only. Use
7453 * {@link #getHeight()} to see how wide a view is after layout.
7454 *
7455 * @return The measured width of this view as a bit mask.
7456 */
7457 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007458 return mMeasuredHeight;
7459 }
7460
7461 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007462 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7463 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7464 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7465 * and the height component is at the shifted bits
7466 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7467 */
7468 public final int getMeasuredState() {
7469 return (mMeasuredWidth&MEASURED_STATE_MASK)
7470 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7471 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7472 }
7473
7474 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007475 * The transform matrix of this view, which is calculated based on the current
7476 * roation, scale, and pivot properties.
7477 *
7478 * @see #getRotation()
7479 * @see #getScaleX()
7480 * @see #getScaleY()
7481 * @see #getPivotX()
7482 * @see #getPivotY()
7483 * @return The current transform matrix for the view
7484 */
7485 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007486 if (mTransformationInfo != null) {
7487 updateMatrix();
7488 return mTransformationInfo.mMatrix;
7489 }
7490 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007491 }
7492
7493 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007494 * Utility function to determine if the value is far enough away from zero to be
7495 * considered non-zero.
7496 * @param value A floating point value to check for zero-ness
7497 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7498 */
7499 private static boolean nonzero(float value) {
7500 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7501 }
7502
7503 /**
Jeff Brown86671742010-09-30 20:00:15 -07007504 * Returns true if the transform matrix is the identity matrix.
7505 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007506 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007507 * @return True if the transform matrix is the identity matrix, false otherwise.
7508 */
Jeff Brown86671742010-09-30 20:00:15 -07007509 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007510 if (mTransformationInfo != null) {
7511 updateMatrix();
7512 return mTransformationInfo.mMatrixIsIdentity;
7513 }
7514 return true;
7515 }
7516
7517 void ensureTransformationInfo() {
7518 if (mTransformationInfo == null) {
7519 mTransformationInfo = new TransformationInfo();
7520 }
Jeff Brown86671742010-09-30 20:00:15 -07007521 }
7522
7523 /**
7524 * Recomputes the transform matrix if necessary.
7525 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007526 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007527 final TransformationInfo info = mTransformationInfo;
7528 if (info == null) {
7529 return;
7530 }
7531 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 // transform-related properties have changed since the last time someone
7533 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007534
7535 // Figure out if we need to update the pivot point
7536 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007537 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7538 info.mPrevWidth = mRight - mLeft;
7539 info.mPrevHeight = mBottom - mTop;
7540 info.mPivotX = info.mPrevWidth / 2f;
7541 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007542 }
7543 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007544 info.mMatrix.reset();
7545 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7546 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7547 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7548 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007549 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007550 if (info.mCamera == null) {
7551 info.mCamera = new Camera();
7552 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007553 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007554 info.mCamera.save();
7555 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7556 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7557 info.mCamera.getMatrix(info.matrix3D);
7558 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7559 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7560 info.mPivotY + info.mTranslationY);
7561 info.mMatrix.postConcat(info.matrix3D);
7562 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007563 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007564 info.mMatrixDirty = false;
7565 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7566 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007567 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007568 }
7569
7570 /**
7571 * Utility method to retrieve the inverse of the current mMatrix property.
7572 * We cache the matrix to avoid recalculating it when transform properties
7573 * have not changed.
7574 *
7575 * @return The inverse of the current matrix of this view.
7576 */
Jeff Brown86671742010-09-30 20:00:15 -07007577 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007578 final TransformationInfo info = mTransformationInfo;
7579 if (info != null) {
7580 updateMatrix();
7581 if (info.mInverseMatrixDirty) {
7582 if (info.mInverseMatrix == null) {
7583 info.mInverseMatrix = new Matrix();
7584 }
7585 info.mMatrix.invert(info.mInverseMatrix);
7586 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007587 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007588 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007589 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007590 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007591 }
7592
7593 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007594 * Gets the distance along the Z axis from the camera to this view.
7595 *
7596 * @see #setCameraDistance(float)
7597 *
7598 * @return The distance along the Z axis.
7599 */
7600 public float getCameraDistance() {
7601 ensureTransformationInfo();
7602 final float dpi = mResources.getDisplayMetrics().densityDpi;
7603 final TransformationInfo info = mTransformationInfo;
7604 if (info.mCamera == null) {
7605 info.mCamera = new Camera();
7606 info.matrix3D = new Matrix();
7607 }
7608 return -(info.mCamera.getLocationZ() * dpi);
7609 }
7610
7611 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007612 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7613 * views are drawn) from the camera to this view. The camera's distance
7614 * affects 3D transformations, for instance rotations around the X and Y
7615 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07007616 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08007617 * use a camera distance that's greater than the height (X axis rotation) or
7618 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007619 *
Romain Guya5364ee2011-02-24 14:46:04 -08007620 * <p>The distance of the camera from the view plane can have an affect on the
7621 * perspective distortion of the view when it is rotated around the x or y axis.
7622 * For example, a large distance will result in a large viewing angle, and there
7623 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07007624 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08007625 * also result in some drawing artifacts if the rotated view ends up partially
7626 * behind the camera (which is why the recommendation is to use a distance at
7627 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007628 *
Romain Guya5364ee2011-02-24 14:46:04 -08007629 * <p>The distance is expressed in "depth pixels." The default distance depends
7630 * on the screen density. For instance, on a medium density display, the
7631 * default distance is 1280. On a high density display, the default distance
7632 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007633 *
Romain Guya5364ee2011-02-24 14:46:04 -08007634 * <p>If you want to specify a distance that leads to visually consistent
7635 * results across various densities, use the following formula:</p>
7636 * <pre>
7637 * float scale = context.getResources().getDisplayMetrics().density;
7638 * view.setCameraDistance(distance * scale);
7639 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07007640 *
Romain Guya5364ee2011-02-24 14:46:04 -08007641 * <p>The density scale factor of a high density display is 1.5,
7642 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007643 *
Romain Guya5364ee2011-02-24 14:46:04 -08007644 * @param distance The distance in "depth pixels", if negative the opposite
7645 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07007646 *
7647 * @see #setRotationX(float)
7648 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08007649 */
7650 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007651 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007652
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007653 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007654 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007655 final TransformationInfo info = mTransformationInfo;
7656 if (info.mCamera == null) {
7657 info.mCamera = new Camera();
7658 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007659 }
7660
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007661 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7662 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007663
Chet Haase9d1992d2012-03-13 11:03:25 -07007664 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007665 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07007666 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08007667 }
Romain Guya5364ee2011-02-24 14:46:04 -08007668 }
7669
7670 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007671 * The degrees that the view is rotated around the pivot point.
7672 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007673 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007674 * @see #getPivotX()
7675 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007676 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007677 * @return The degrees of rotation.
7678 */
Chet Haasea5531132012-02-02 13:41:44 -08007679 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007680 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007681 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007682 }
7683
7684 /**
Chet Haase897247b2010-09-09 14:54:47 -07007685 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7686 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007687 *
7688 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007689 *
7690 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007691 * @see #getPivotX()
7692 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007693 * @see #setRotationX(float)
7694 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007695 *
7696 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007697 */
7698 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007699 ensureTransformationInfo();
7700 final TransformationInfo info = mTransformationInfo;
7701 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007702 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007703 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007704 info.mRotation = rotation;
7705 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007706 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007707 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7708 mDisplayList.setRotation(rotation);
7709 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007710 }
7711 }
7712
7713 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007714 * The degrees that the view is rotated around the vertical axis through the pivot point.
7715 *
7716 * @see #getPivotX()
7717 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007718 * @see #setRotationY(float)
7719 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007720 * @return The degrees of Y rotation.
7721 */
Chet Haasea5531132012-02-02 13:41:44 -08007722 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007723 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007724 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007725 }
7726
7727 /**
Chet Haase897247b2010-09-09 14:54:47 -07007728 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7729 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7730 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007731 *
Romain Guya5364ee2011-02-24 14:46:04 -08007732 * When rotating large views, it is recommended to adjust the camera distance
7733 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007734 *
7735 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007736 *
7737 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007738 * @see #getPivotX()
7739 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007740 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007741 * @see #setRotationX(float)
7742 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007743 *
7744 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007745 */
7746 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007747 ensureTransformationInfo();
7748 final TransformationInfo info = mTransformationInfo;
7749 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007750 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007751 info.mRotationY = rotationY;
7752 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007753 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007754 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7755 mDisplayList.setRotationY(rotationY);
7756 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007757 }
7758 }
7759
7760 /**
7761 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7762 *
7763 * @see #getPivotX()
7764 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007765 * @see #setRotationX(float)
7766 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007767 * @return The degrees of X rotation.
7768 */
Chet Haasea5531132012-02-02 13:41:44 -08007769 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007770 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007771 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007772 }
7773
7774 /**
Chet Haase897247b2010-09-09 14:54:47 -07007775 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7776 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7777 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007778 *
Romain Guya5364ee2011-02-24 14:46:04 -08007779 * When rotating large views, it is recommended to adjust the camera distance
7780 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007781 *
7782 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007783 *
7784 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007785 * @see #getPivotX()
7786 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007787 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007788 * @see #setRotationY(float)
7789 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007790 *
7791 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007792 */
7793 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007794 ensureTransformationInfo();
7795 final TransformationInfo info = mTransformationInfo;
7796 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007797 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007798 info.mRotationX = rotationX;
7799 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007800 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007801 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7802 mDisplayList.setRotationX(rotationX);
7803 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007804 }
7805 }
7806
7807 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007808 * The amount that the view is scaled in x around the pivot point, as a proportion of
7809 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7810 *
Joe Onorato93162322010-09-16 15:42:01 -04007811 * <p>By default, this is 1.0f.
7812 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007813 * @see #getPivotX()
7814 * @see #getPivotY()
7815 * @return The scaling factor.
7816 */
Chet Haasea5531132012-02-02 13:41:44 -08007817 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007818 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007819 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007820 }
7821
7822 /**
7823 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7824 * the view's unscaled width. A value of 1 means that no scaling is applied.
7825 *
7826 * @param scaleX The scaling factor.
7827 * @see #getPivotX()
7828 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007829 *
7830 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007831 */
7832 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007833 ensureTransformationInfo();
7834 final TransformationInfo info = mTransformationInfo;
7835 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007836 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007837 info.mScaleX = scaleX;
7838 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007839 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007840 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7841 mDisplayList.setScaleX(scaleX);
7842 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007843 }
7844 }
7845
7846 /**
7847 * The amount that the view is scaled in y around the pivot point, as a proportion of
7848 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7849 *
Joe Onorato93162322010-09-16 15:42:01 -04007850 * <p>By default, this is 1.0f.
7851 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007852 * @see #getPivotX()
7853 * @see #getPivotY()
7854 * @return The scaling factor.
7855 */
Chet Haasea5531132012-02-02 13:41:44 -08007856 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007857 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007858 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007859 }
7860
7861 /**
7862 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7863 * the view's unscaled width. A value of 1 means that no scaling is applied.
7864 *
7865 * @param scaleY The scaling factor.
7866 * @see #getPivotX()
7867 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007868 *
7869 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007870 */
7871 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007872 ensureTransformationInfo();
7873 final TransformationInfo info = mTransformationInfo;
7874 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007875 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007876 info.mScaleY = scaleY;
7877 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007878 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007879 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7880 mDisplayList.setScaleY(scaleY);
7881 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007882 }
7883 }
7884
7885 /**
7886 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7887 * and {@link #setScaleX(float) scaled}.
7888 *
7889 * @see #getRotation()
7890 * @see #getScaleX()
7891 * @see #getScaleY()
7892 * @see #getPivotY()
7893 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007894 *
7895 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007896 */
Chet Haasea5531132012-02-02 13:41:44 -08007897 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007898 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007899 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007900 }
7901
7902 /**
7903 * Sets the x location of the point around which the view is
7904 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007905 * By default, the pivot point is centered on the object.
7906 * Setting this property disables this behavior and causes the view to use only the
7907 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007908 *
7909 * @param pivotX The x location of the pivot point.
7910 * @see #getRotation()
7911 * @see #getScaleX()
7912 * @see #getScaleY()
7913 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007914 *
7915 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007916 */
7917 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007918 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007919 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007920 final TransformationInfo info = mTransformationInfo;
7921 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007922 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007923 info.mPivotX = pivotX;
7924 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007925 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007926 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7927 mDisplayList.setPivotX(pivotX);
7928 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007929 }
7930 }
7931
7932 /**
7933 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7934 * and {@link #setScaleY(float) scaled}.
7935 *
7936 * @see #getRotation()
7937 * @see #getScaleX()
7938 * @see #getScaleY()
7939 * @see #getPivotY()
7940 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007941 *
7942 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007943 */
Chet Haasea5531132012-02-02 13:41:44 -08007944 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007945 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007946 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007947 }
7948
7949 /**
7950 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007951 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7952 * Setting this property disables this behavior and causes the view to use only the
7953 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007954 *
7955 * @param pivotY The y location of the pivot point.
7956 * @see #getRotation()
7957 * @see #getScaleX()
7958 * @see #getScaleY()
7959 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007960 *
7961 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007962 */
7963 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007964 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007965 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007966 final TransformationInfo info = mTransformationInfo;
7967 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007968 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007969 info.mPivotY = pivotY;
7970 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007971 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007972 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7973 mDisplayList.setPivotY(pivotY);
7974 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007975 }
7976 }
7977
7978 /**
7979 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7980 * completely transparent and 1 means the view is completely opaque.
7981 *
Joe Onorato93162322010-09-16 15:42:01 -04007982 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007983 * @return The opacity of the view.
7984 */
Chet Haasea5531132012-02-02 13:41:44 -08007985 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007986 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007987 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007988 }
7989
7990 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07007991 * Returns whether this View has content which overlaps. This function, intended to be
7992 * overridden by specific View types, is an optimization when alpha is set on a view. If
7993 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
7994 * and then composited it into place, which can be expensive. If the view has no overlapping
7995 * rendering, the view can draw each primitive with the appropriate alpha value directly.
7996 * An example of overlapping rendering is a TextView with a background image, such as a
7997 * Button. An example of non-overlapping rendering is a TextView with no background, or
7998 * an ImageView with only the foreground image. The default implementation returns true;
7999 * subclasses should override if they have cases which can be optimized.
8000 *
8001 * @return true if the content in this view might overlap, false otherwise.
8002 */
8003 public boolean hasOverlappingRendering() {
8004 return true;
8005 }
8006
8007 /**
Romain Guy171c5922011-01-06 10:04:23 -08008008 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8009 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008010 *
Romain Guy171c5922011-01-06 10:04:23 -08008011 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8012 * responsible for applying the opacity itself. Otherwise, calling this method is
8013 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008014 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008015 *
Chet Haasea5531132012-02-02 13:41:44 -08008016 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8017 * performance implications. It is generally best to use the alpha property sparingly and
8018 * transiently, as in the case of fading animations.</p>
8019 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008020 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008021 *
Joe Malin32736f02011-01-19 16:14:20 -08008022 * @see #setLayerType(int, android.graphics.Paint)
8023 *
Chet Haase73066682010-11-29 15:55:32 -08008024 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008025 */
8026 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008027 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008028 if (mTransformationInfo.mAlpha != alpha) {
8029 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008030 if (onSetAlpha((int) (alpha * 255))) {
8031 mPrivateFlags |= ALPHA_SET;
8032 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008033 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008034 invalidate(true);
8035 } else {
8036 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008037 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008038 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8039 mDisplayList.setAlpha(alpha);
8040 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008041 }
Chet Haaseed032702010-10-01 14:05:54 -07008042 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008043 }
8044
8045 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008046 * Faster version of setAlpha() which performs the same steps except there are
8047 * no calls to invalidate(). The caller of this function should perform proper invalidation
8048 * on the parent and this object. The return value indicates whether the subclass handles
8049 * alpha (the return value for onSetAlpha()).
8050 *
8051 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008052 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8053 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008054 */
8055 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008056 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008057 if (mTransformationInfo.mAlpha != alpha) {
8058 mTransformationInfo.mAlpha = alpha;
8059 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8060 if (subclassHandlesAlpha) {
8061 mPrivateFlags |= ALPHA_SET;
8062 return true;
8063 } else {
8064 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008065 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8066 mDisplayList.setAlpha(alpha);
8067 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008068 }
Chet Haasea00f3862011-02-22 06:34:40 -08008069 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008070 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008071 }
8072
8073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074 * Top position of this view relative to its parent.
8075 *
8076 * @return The top of this view, in pixels.
8077 */
8078 @ViewDebug.CapturedViewProperty
8079 public final int getTop() {
8080 return mTop;
8081 }
8082
8083 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008084 * Sets the top position of this view relative to its parent. This method is meant to be called
8085 * by the layout system and should not generally be called otherwise, because the property
8086 * may be changed at any time by the layout.
8087 *
8088 * @param top The top of this view, in pixels.
8089 */
8090 public final void setTop(int top) {
8091 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008092 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008093 final boolean matrixIsIdentity = mTransformationInfo == null
8094 || mTransformationInfo.mMatrixIsIdentity;
8095 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008096 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008097 int minTop;
8098 int yLoc;
8099 if (top < mTop) {
8100 minTop = top;
8101 yLoc = top - mTop;
8102 } else {
8103 minTop = mTop;
8104 yLoc = 0;
8105 }
Chet Haasee9140a72011-02-16 16:23:29 -08008106 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008107 }
8108 } else {
8109 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008110 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008111 }
8112
Chet Haaseed032702010-10-01 14:05:54 -07008113 int width = mRight - mLeft;
8114 int oldHeight = mBottom - mTop;
8115
Chet Haase21cd1382010-09-01 17:42:29 -07008116 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008117 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8118 mDisplayList.setTop(mTop);
8119 }
Chet Haase21cd1382010-09-01 17:42:29 -07008120
Chet Haaseed032702010-10-01 14:05:54 -07008121 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8122
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008123 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008124 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8125 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008126 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008127 }
Chet Haase21cd1382010-09-01 17:42:29 -07008128 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008129 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008130 }
Chet Haase55dbb652010-12-21 20:15:08 -08008131 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008132 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008133 }
8134 }
8135
8136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 * Bottom position of this view relative to its parent.
8138 *
8139 * @return The bottom of this view, in pixels.
8140 */
8141 @ViewDebug.CapturedViewProperty
8142 public final int getBottom() {
8143 return mBottom;
8144 }
8145
8146 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008147 * True if this view has changed since the last time being drawn.
8148 *
8149 * @return The dirty state of this view.
8150 */
8151 public boolean isDirty() {
8152 return (mPrivateFlags & DIRTY_MASK) != 0;
8153 }
8154
8155 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008156 * Sets the bottom position of this view relative to its parent. This method is meant to be
8157 * called by the layout system and should not generally be called otherwise, because the
8158 * property may be changed at any time by the layout.
8159 *
8160 * @param bottom The bottom of this view, in pixels.
8161 */
8162 public final void setBottom(int bottom) {
8163 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008164 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008165 final boolean matrixIsIdentity = mTransformationInfo == null
8166 || mTransformationInfo.mMatrixIsIdentity;
8167 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008168 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008169 int maxBottom;
8170 if (bottom < mBottom) {
8171 maxBottom = mBottom;
8172 } else {
8173 maxBottom = bottom;
8174 }
Chet Haasee9140a72011-02-16 16:23:29 -08008175 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008176 }
8177 } else {
8178 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008179 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008180 }
8181
Chet Haaseed032702010-10-01 14:05:54 -07008182 int width = mRight - mLeft;
8183 int oldHeight = mBottom - mTop;
8184
Chet Haase21cd1382010-09-01 17:42:29 -07008185 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008186 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8187 mDisplayList.setBottom(mBottom);
8188 }
Chet Haase21cd1382010-09-01 17:42:29 -07008189
Chet Haaseed032702010-10-01 14:05:54 -07008190 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8191
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008192 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008193 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8194 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008195 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008196 }
Chet Haase21cd1382010-09-01 17:42:29 -07008197 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008198 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008199 }
Chet Haase55dbb652010-12-21 20:15:08 -08008200 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008201 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008202 }
8203 }
8204
8205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 * Left position of this view relative to its parent.
8207 *
8208 * @return The left edge of this view, in pixels.
8209 */
8210 @ViewDebug.CapturedViewProperty
8211 public final int getLeft() {
8212 return mLeft;
8213 }
8214
8215 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008216 * Sets the left position of this view relative to its parent. This method is meant to be called
8217 * by the layout system and should not generally be called otherwise, because the property
8218 * may be changed at any time by the layout.
8219 *
8220 * @param left The bottom of this view, in pixels.
8221 */
8222 public final void setLeft(int left) {
8223 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008224 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008225 final boolean matrixIsIdentity = mTransformationInfo == null
8226 || mTransformationInfo.mMatrixIsIdentity;
8227 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008228 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008229 int minLeft;
8230 int xLoc;
8231 if (left < mLeft) {
8232 minLeft = left;
8233 xLoc = left - mLeft;
8234 } else {
8235 minLeft = mLeft;
8236 xLoc = 0;
8237 }
Chet Haasee9140a72011-02-16 16:23:29 -08008238 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008239 }
8240 } else {
8241 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008242 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008243 }
8244
Chet Haaseed032702010-10-01 14:05:54 -07008245 int oldWidth = mRight - mLeft;
8246 int height = mBottom - mTop;
8247
Chet Haase21cd1382010-09-01 17:42:29 -07008248 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008249 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8250 mDisplayList.setLeft(left);
8251 }
Chet Haase21cd1382010-09-01 17:42:29 -07008252
Chet Haaseed032702010-10-01 14:05:54 -07008253 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8254
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008255 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008256 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8257 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008258 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008259 }
Chet Haase21cd1382010-09-01 17:42:29 -07008260 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008261 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008262 }
Chet Haase55dbb652010-12-21 20:15:08 -08008263 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008264 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008265 if (USE_DISPLAY_LIST_PROPERTIES) {
8266
8267 }
Chet Haase21cd1382010-09-01 17:42:29 -07008268 }
8269 }
8270
8271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 * Right position of this view relative to its parent.
8273 *
8274 * @return The right edge of this view, in pixels.
8275 */
8276 @ViewDebug.CapturedViewProperty
8277 public final int getRight() {
8278 return mRight;
8279 }
8280
8281 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008282 * Sets the right position of this view relative to its parent. This method is meant to be called
8283 * by the layout system and should not generally be called otherwise, because the property
8284 * may be changed at any time by the layout.
8285 *
8286 * @param right The bottom of this view, in pixels.
8287 */
8288 public final void setRight(int right) {
8289 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008290 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008291 final boolean matrixIsIdentity = mTransformationInfo == null
8292 || mTransformationInfo.mMatrixIsIdentity;
8293 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008294 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008295 int maxRight;
8296 if (right < mRight) {
8297 maxRight = mRight;
8298 } else {
8299 maxRight = right;
8300 }
Chet Haasee9140a72011-02-16 16:23:29 -08008301 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008302 }
8303 } else {
8304 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008305 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008306 }
8307
Chet Haaseed032702010-10-01 14:05:54 -07008308 int oldWidth = mRight - mLeft;
8309 int height = mBottom - mTop;
8310
Chet Haase21cd1382010-09-01 17:42:29 -07008311 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008312 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8313 mDisplayList.setRight(mRight);
8314 }
Chet Haase21cd1382010-09-01 17:42:29 -07008315
Chet Haaseed032702010-10-01 14:05:54 -07008316 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8317
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008318 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008319 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8320 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008321 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008322 }
Chet Haase21cd1382010-09-01 17:42:29 -07008323 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008324 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008325 }
Chet Haase55dbb652010-12-21 20:15:08 -08008326 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008327 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008328 }
8329 }
8330
8331 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008332 * The visual x position of this view, in pixels. This is equivalent to the
8333 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008334 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008335 *
Chet Haasedf030d22010-07-30 17:22:38 -07008336 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008337 */
Chet Haasea5531132012-02-02 13:41:44 -08008338 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008339 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008340 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008341 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008342
Chet Haasedf030d22010-07-30 17:22:38 -07008343 /**
8344 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8345 * {@link #setTranslationX(float) translationX} property to be the difference between
8346 * the x value passed in and the current {@link #getLeft() left} property.
8347 *
8348 * @param x The visual x position of this view, in pixels.
8349 */
8350 public void setX(float x) {
8351 setTranslationX(x - mLeft);
8352 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008353
Chet Haasedf030d22010-07-30 17:22:38 -07008354 /**
8355 * The visual y position of this view, in pixels. This is equivalent to the
8356 * {@link #setTranslationY(float) translationY} property plus the current
8357 * {@link #getTop() top} property.
8358 *
8359 * @return The visual y position of this view, in pixels.
8360 */
Chet Haasea5531132012-02-02 13:41:44 -08008361 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008362 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008363 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008364 }
8365
8366 /**
8367 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8368 * {@link #setTranslationY(float) translationY} property to be the difference between
8369 * the y value passed in and the current {@link #getTop() top} property.
8370 *
8371 * @param y The visual y position of this view, in pixels.
8372 */
8373 public void setY(float y) {
8374 setTranslationY(y - mTop);
8375 }
8376
8377
8378 /**
8379 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8380 * This position is post-layout, in addition to wherever the object's
8381 * layout placed it.
8382 *
8383 * @return The horizontal position of this view relative to its left position, in pixels.
8384 */
Chet Haasea5531132012-02-02 13:41:44 -08008385 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008386 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008387 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008388 }
8389
8390 /**
8391 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8392 * This effectively positions the object post-layout, in addition to wherever the object's
8393 * layout placed it.
8394 *
8395 * @param translationX The horizontal position of this view relative to its left position,
8396 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008397 *
8398 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008399 */
8400 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008401 ensureTransformationInfo();
8402 final TransformationInfo info = mTransformationInfo;
8403 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008404 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008405 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008406 info.mTranslationX = translationX;
8407 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008408 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008409 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8410 mDisplayList.setTranslationX(translationX);
8411 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008412 }
8413 }
8414
8415 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008416 * The horizontal location of this view relative to its {@link #getTop() top} position.
8417 * This position is post-layout, in addition to wherever the object's
8418 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008419 *
Chet Haasedf030d22010-07-30 17:22:38 -07008420 * @return The vertical position of this view relative to its top position,
8421 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008422 */
Chet Haasea5531132012-02-02 13:41:44 -08008423 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008424 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008425 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008426 }
8427
8428 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008429 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8430 * This effectively positions the object post-layout, in addition to wherever the object's
8431 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008432 *
Chet Haasedf030d22010-07-30 17:22:38 -07008433 * @param translationY The vertical position of this view relative to its top position,
8434 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008435 *
8436 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008437 */
Chet Haasedf030d22010-07-30 17:22:38 -07008438 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008439 ensureTransformationInfo();
8440 final TransformationInfo info = mTransformationInfo;
8441 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008442 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008443 info.mTranslationY = translationY;
8444 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008445 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008446 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8447 mDisplayList.setTranslationY(translationY);
8448 }
Chet Haasedf030d22010-07-30 17:22:38 -07008449 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008450 }
8451
8452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008453 * Hit rectangle in parent's coordinates
8454 *
8455 * @param outRect The hit rectangle of the view.
8456 */
8457 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008458 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008459 final TransformationInfo info = mTransformationInfo;
8460 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008461 outRect.set(mLeft, mTop, mRight, mBottom);
8462 } else {
8463 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008464 tmpRect.set(-info.mPivotX, -info.mPivotY,
8465 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8466 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008467 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8468 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008469 }
8470 }
8471
8472 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008473 * Determines whether the given point, in local coordinates is inside the view.
8474 */
8475 /*package*/ final boolean pointInView(float localX, float localY) {
8476 return localX >= 0 && localX < (mRight - mLeft)
8477 && localY >= 0 && localY < (mBottom - mTop);
8478 }
8479
8480 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008481 * Utility method to determine whether the given point, in local coordinates,
8482 * is inside the view, where the area of the view is expanded by the slop factor.
8483 * This method is called while processing touch-move events to determine if the event
8484 * is still within the view.
8485 */
8486 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008487 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008488 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 }
8490
8491 /**
8492 * When a view has focus and the user navigates away from it, the next view is searched for
8493 * starting from the rectangle filled in by this method.
8494 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008495 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8496 * of the view. However, if your view maintains some idea of internal selection,
8497 * such as a cursor, or a selected row or column, you should override this method and
8498 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 *
8500 * @param r The rectangle to fill in, in this view's coordinates.
8501 */
8502 public void getFocusedRect(Rect r) {
8503 getDrawingRect(r);
8504 }
8505
8506 /**
8507 * If some part of this view is not clipped by any of its parents, then
8508 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008509 * coordinates (without taking possible View rotations into account), offset
8510 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8511 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 *
8513 * @param r If true is returned, r holds the global coordinates of the
8514 * visible portion of this view.
8515 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8516 * between this view and its root. globalOffet may be null.
8517 * @return true if r is non-empty (i.e. part of the view is visible at the
8518 * root level.
8519 */
8520 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8521 int width = mRight - mLeft;
8522 int height = mBottom - mTop;
8523 if (width > 0 && height > 0) {
8524 r.set(0, 0, width, height);
8525 if (globalOffset != null) {
8526 globalOffset.set(-mScrollX, -mScrollY);
8527 }
8528 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8529 }
8530 return false;
8531 }
8532
8533 public final boolean getGlobalVisibleRect(Rect r) {
8534 return getGlobalVisibleRect(r, null);
8535 }
8536
8537 public final boolean getLocalVisibleRect(Rect r) {
8538 Point offset = new Point();
8539 if (getGlobalVisibleRect(r, offset)) {
8540 r.offset(-offset.x, -offset.y); // make r local
8541 return true;
8542 }
8543 return false;
8544 }
8545
8546 /**
8547 * Offset this view's vertical location by the specified number of pixels.
8548 *
8549 * @param offset the number of pixels to offset the view by
8550 */
8551 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008552 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008553 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008554 final boolean matrixIsIdentity = mTransformationInfo == null
8555 || mTransformationInfo.mMatrixIsIdentity;
8556 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008557 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8558 invalidateViewProperty(false, false);
8559 } else {
8560 final ViewParent p = mParent;
8561 if (p != null && mAttachInfo != null) {
8562 final Rect r = mAttachInfo.mTmpInvalRect;
8563 int minTop;
8564 int maxBottom;
8565 int yLoc;
8566 if (offset < 0) {
8567 minTop = mTop + offset;
8568 maxBottom = mBottom;
8569 yLoc = offset;
8570 } else {
8571 minTop = mTop;
8572 maxBottom = mBottom + offset;
8573 yLoc = 0;
8574 }
8575 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8576 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008577 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008578 }
8579 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008580 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008581 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008582
Chet Haasec3aa3612010-06-17 08:50:37 -07008583 mTop += offset;
8584 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008585 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8586 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008587 invalidateViewProperty(false, false);
8588 } else {
8589 if (!matrixIsIdentity) {
8590 invalidateViewProperty(false, true);
8591 }
8592 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008593 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 }
8596
8597 /**
8598 * Offset this view's horizontal location by the specified amount of pixels.
8599 *
8600 * @param offset the numer of pixels to offset the view by
8601 */
8602 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008603 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008604 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008605 final boolean matrixIsIdentity = mTransformationInfo == null
8606 || mTransformationInfo.mMatrixIsIdentity;
8607 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008608 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8609 invalidateViewProperty(false, false);
8610 } else {
8611 final ViewParent p = mParent;
8612 if (p != null && mAttachInfo != null) {
8613 final Rect r = mAttachInfo.mTmpInvalRect;
8614 int minLeft;
8615 int maxRight;
8616 if (offset < 0) {
8617 minLeft = mLeft + offset;
8618 maxRight = mRight;
8619 } else {
8620 minLeft = mLeft;
8621 maxRight = mRight + offset;
8622 }
8623 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8624 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008625 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008626 }
8627 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008628 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008629 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008630
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 mLeft += offset;
8632 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008633 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8634 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008635 invalidateViewProperty(false, false);
8636 } else {
8637 if (!matrixIsIdentity) {
8638 invalidateViewProperty(false, true);
8639 }
8640 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008641 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 }
8644
8645 /**
8646 * Get the LayoutParams associated with this view. All views should have
8647 * layout parameters. These supply parameters to the <i>parent</i> of this
8648 * view specifying how it should be arranged. There are many subclasses of
8649 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8650 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008651 *
8652 * This method may return null if this View is not attached to a parent
8653 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8654 * was not invoked successfully. When a View is attached to a parent
8655 * ViewGroup, this method must not return null.
8656 *
8657 * @return The LayoutParams associated with this view, or null if no
8658 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008660 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 public ViewGroup.LayoutParams getLayoutParams() {
8662 return mLayoutParams;
8663 }
8664
8665 /**
8666 * Set the layout parameters associated with this view. These supply
8667 * parameters to the <i>parent</i> of this view specifying how it should be
8668 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8669 * correspond to the different subclasses of ViewGroup that are responsible
8670 * for arranging their children.
8671 *
Romain Guy01c174b2011-02-22 11:51:06 -08008672 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 */
8674 public void setLayoutParams(ViewGroup.LayoutParams params) {
8675 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008676 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 }
8678 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008679 if (mParent instanceof ViewGroup) {
8680 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 requestLayout();
8683 }
8684
8685 /**
8686 * Set the scrolled position of your view. This will cause a call to
8687 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8688 * invalidated.
8689 * @param x the x position to scroll to
8690 * @param y the y position to scroll to
8691 */
8692 public void scrollTo(int x, int y) {
8693 if (mScrollX != x || mScrollY != y) {
8694 int oldX = mScrollX;
8695 int oldY = mScrollY;
8696 mScrollX = x;
8697 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008698 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008700 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008701 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 }
8704 }
8705
8706 /**
8707 * Move the scrolled position of your view. This will cause a call to
8708 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8709 * invalidated.
8710 * @param x the amount of pixels to scroll by horizontally
8711 * @param y the amount of pixels to scroll by vertically
8712 */
8713 public void scrollBy(int x, int y) {
8714 scrollTo(mScrollX + x, mScrollY + y);
8715 }
8716
8717 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008718 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8719 * animation to fade the scrollbars out after a default delay. If a subclass
8720 * provides animated scrolling, the start delay should equal the duration
8721 * of the scrolling animation.</p>
8722 *
8723 * <p>The animation starts only if at least one of the scrollbars is
8724 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8725 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8726 * this method returns true, and false otherwise. If the animation is
8727 * started, this method calls {@link #invalidate()}; in that case the
8728 * caller should not call {@link #invalidate()}.</p>
8729 *
8730 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008731 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008732 *
8733 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8734 * and {@link #scrollTo(int, int)}.</p>
8735 *
8736 * @return true if the animation is played, false otherwise
8737 *
8738 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008739 * @see #scrollBy(int, int)
8740 * @see #scrollTo(int, int)
8741 * @see #isHorizontalScrollBarEnabled()
8742 * @see #isVerticalScrollBarEnabled()
8743 * @see #setHorizontalScrollBarEnabled(boolean)
8744 * @see #setVerticalScrollBarEnabled(boolean)
8745 */
8746 protected boolean awakenScrollBars() {
8747 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008748 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008749 }
8750
8751 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008752 * Trigger the scrollbars to draw.
8753 * This method differs from awakenScrollBars() only in its default duration.
8754 * initialAwakenScrollBars() will show the scroll bars for longer than
8755 * usual to give the user more of a chance to notice them.
8756 *
8757 * @return true if the animation is played, false otherwise.
8758 */
8759 private boolean initialAwakenScrollBars() {
8760 return mScrollCache != null &&
8761 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8762 }
8763
8764 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008765 * <p>
8766 * Trigger the scrollbars to draw. When invoked this method starts an
8767 * animation to fade the scrollbars out after a fixed delay. If a subclass
8768 * provides animated scrolling, the start delay should equal the duration of
8769 * the scrolling animation.
8770 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008771 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008772 * <p>
8773 * The animation starts only if at least one of the scrollbars is enabled,
8774 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8775 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8776 * this method returns true, and false otherwise. If the animation is
8777 * started, this method calls {@link #invalidate()}; in that case the caller
8778 * should not call {@link #invalidate()}.
8779 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008780 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008781 * <p>
8782 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008783 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008784 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008785 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008786 * @param startDelay the delay, in milliseconds, after which the animation
8787 * should start; when the delay is 0, the animation starts
8788 * immediately
8789 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008790 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008791 * @see #scrollBy(int, int)
8792 * @see #scrollTo(int, int)
8793 * @see #isHorizontalScrollBarEnabled()
8794 * @see #isVerticalScrollBarEnabled()
8795 * @see #setHorizontalScrollBarEnabled(boolean)
8796 * @see #setVerticalScrollBarEnabled(boolean)
8797 */
8798 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008799 return awakenScrollBars(startDelay, true);
8800 }
Joe Malin32736f02011-01-19 16:14:20 -08008801
Mike Cleron290947b2009-09-29 18:34:32 -07008802 /**
8803 * <p>
8804 * Trigger the scrollbars to draw. When invoked this method starts an
8805 * animation to fade the scrollbars out after a fixed delay. If a subclass
8806 * provides animated scrolling, the start delay should equal the duration of
8807 * the scrolling animation.
8808 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008809 *
Mike Cleron290947b2009-09-29 18:34:32 -07008810 * <p>
8811 * The animation starts only if at least one of the scrollbars is enabled,
8812 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8813 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8814 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008815 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008816 * is set to true; in that case the caller
8817 * should not call {@link #invalidate()}.
8818 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008819 *
Mike Cleron290947b2009-09-29 18:34:32 -07008820 * <p>
8821 * This method should be invoked everytime a subclass directly updates the
8822 * scroll parameters.
8823 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008824 *
Mike Cleron290947b2009-09-29 18:34:32 -07008825 * @param startDelay the delay, in milliseconds, after which the animation
8826 * should start; when the delay is 0, the animation starts
8827 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008828 *
Mike Cleron290947b2009-09-29 18:34:32 -07008829 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008830 *
Mike Cleron290947b2009-09-29 18:34:32 -07008831 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008832 *
Mike Cleron290947b2009-09-29 18:34:32 -07008833 * @see #scrollBy(int, int)
8834 * @see #scrollTo(int, int)
8835 * @see #isHorizontalScrollBarEnabled()
8836 * @see #isVerticalScrollBarEnabled()
8837 * @see #setHorizontalScrollBarEnabled(boolean)
8838 * @see #setVerticalScrollBarEnabled(boolean)
8839 */
8840 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008841 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008842
Mike Cleronf116bf82009-09-27 19:14:12 -07008843 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8844 return false;
8845 }
8846
8847 if (scrollCache.scrollBar == null) {
8848 scrollCache.scrollBar = new ScrollBarDrawable();
8849 }
8850
8851 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8852
Mike Cleron290947b2009-09-29 18:34:32 -07008853 if (invalidate) {
8854 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008855 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008856 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008857
8858 if (scrollCache.state == ScrollabilityCache.OFF) {
8859 // FIXME: this is copied from WindowManagerService.
8860 // We should get this value from the system when it
8861 // is possible to do so.
8862 final int KEY_REPEAT_FIRST_DELAY = 750;
8863 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8864 }
8865
8866 // Tell mScrollCache when we should start fading. This may
8867 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008868 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008869 scrollCache.fadeStartTime = fadeStartTime;
8870 scrollCache.state = ScrollabilityCache.ON;
8871
8872 // Schedule our fader to run, unscheduling any old ones first
8873 if (mAttachInfo != null) {
8874 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8875 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8876 }
8877
8878 return true;
8879 }
8880
8881 return false;
8882 }
8883
8884 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008885 * Do not invalidate views which are not visible and which are not running an animation. They
8886 * will not get drawn and they should not set dirty flags as if they will be drawn
8887 */
8888 private boolean skipInvalidate() {
8889 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8890 (!(mParent instanceof ViewGroup) ||
8891 !((ViewGroup) mParent).isViewTransitioning(this));
8892 }
8893 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008894 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008895 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8896 * in the future. This must be called from a UI thread. To call from a non-UI
8897 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 *
8899 * WARNING: This method is destructive to dirty.
8900 * @param dirty the rectangle representing the bounds of the dirty region
8901 */
8902 public void invalidate(Rect dirty) {
8903 if (ViewDebug.TRACE_HIERARCHY) {
8904 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8905 }
8906
Chet Haaseaceafe62011-08-26 15:44:33 -07008907 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008908 return;
8909 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008910 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008911 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8912 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008914 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008915 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 final ViewParent p = mParent;
8917 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008918 //noinspection PointlessBooleanExpression,ConstantConditions
8919 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8920 if (p != null && ai != null && ai.mHardwareAccelerated) {
8921 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008922 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008923 p.invalidateChild(this, null);
8924 return;
8925 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 if (p != null && ai != null) {
8928 final int scrollX = mScrollX;
8929 final int scrollY = mScrollY;
8930 final Rect r = ai.mTmpInvalRect;
8931 r.set(dirty.left - scrollX, dirty.top - scrollY,
8932 dirty.right - scrollX, dirty.bottom - scrollY);
8933 mParent.invalidateChild(this, r);
8934 }
8935 }
8936 }
8937
8938 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008939 * Mark the area defined by the rect (l,t,r,b) as needing to be drawn.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008940 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008941 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8942 * will be called at some point in the future. This must be called from
8943 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008944 * @param l the left position of the dirty region
8945 * @param t the top position of the dirty region
8946 * @param r the right position of the dirty region
8947 * @param b the bottom position of the dirty region
8948 */
8949 public void invalidate(int l, int t, int r, int b) {
8950 if (ViewDebug.TRACE_HIERARCHY) {
8951 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8952 }
8953
Chet Haaseaceafe62011-08-26 15:44:33 -07008954 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008955 return;
8956 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008957 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008958 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8959 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008961 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008962 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 final ViewParent p = mParent;
8964 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008965 //noinspection PointlessBooleanExpression,ConstantConditions
8966 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8967 if (p != null && ai != null && ai.mHardwareAccelerated) {
8968 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008969 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008970 p.invalidateChild(this, null);
8971 return;
8972 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 if (p != null && ai != null && l < r && t < b) {
8975 final int scrollX = mScrollX;
8976 final int scrollY = mScrollY;
8977 final Rect tmpr = ai.mTmpInvalRect;
8978 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8979 p.invalidateChild(this, tmpr);
8980 }
8981 }
8982 }
8983
8984 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008985 * Invalidate the whole view. If the view is visible,
8986 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8987 * the future. This must be called from a UI thread. To call from a non-UI thread,
8988 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 */
8990 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008991 invalidate(true);
8992 }
Joe Malin32736f02011-01-19 16:14:20 -08008993
Chet Haaseed032702010-10-01 14:05:54 -07008994 /**
8995 * This is where the invalidate() work actually happens. A full invalidate()
8996 * causes the drawing cache to be invalidated, but this function can be called with
8997 * invalidateCache set to false to skip that invalidation step for cases that do not
8998 * need it (for example, a component that remains at the same dimensions with the same
8999 * content).
9000 *
9001 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9002 * well. This is usually true for a full invalidate, but may be set to false if the
9003 * View's contents or dimensions have not changed.
9004 */
Romain Guy849d0a32011-02-01 17:20:48 -08009005 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 if (ViewDebug.TRACE_HIERARCHY) {
9007 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9008 }
9009
Chet Haaseaceafe62011-08-26 15:44:33 -07009010 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009011 return;
9012 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009013 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009014 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009015 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9016 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009017 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009018 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009019 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009020 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009021 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009024 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009025 //noinspection PointlessBooleanExpression,ConstantConditions
9026 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9027 if (p != null && ai != null && ai.mHardwareAccelerated) {
9028 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009029 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009030 p.invalidateChild(this, null);
9031 return;
9032 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009033 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 if (p != null && ai != null) {
9036 final Rect r = ai.mTmpInvalRect;
9037 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9038 // Don't call invalidate -- we don't want to internally scroll
9039 // our own bounds
9040 p.invalidateChild(this, r);
9041 }
9042 }
9043 }
9044
9045 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009046 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9047 * set any flags or handle all of the cases handled by the default invalidation methods.
9048 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9049 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9050 * walk up the hierarchy, transforming the dirty rect as necessary.
9051 *
9052 * The method also handles normal invalidation logic if display list properties are not
9053 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9054 * backup approach, to handle these cases used in the various property-setting methods.
9055 *
9056 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9057 * are not being used in this view
9058 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9059 * list properties are not being used in this view
9060 */
9061 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9062 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9063 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9064 if (invalidateParent) {
9065 invalidateParentCaches();
9066 }
9067 if (forceRedraw) {
9068 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9069 }
9070 invalidate(false);
9071 } else {
9072 final AttachInfo ai = mAttachInfo;
9073 final ViewParent p = mParent;
9074 if (p != null && ai != null) {
9075 final Rect r = ai.mTmpInvalRect;
9076 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9077 if (mParent instanceof ViewGroup) {
9078 ((ViewGroup) mParent).invalidateChildFast(this, r);
9079 } else {
9080 mParent.invalidateChild(this, r);
9081 }
9082 }
9083 }
9084 }
9085
9086 /**
9087 * Utility method to transform a given Rect by the current matrix of this view.
9088 */
9089 void transformRect(final Rect rect) {
9090 if (!getMatrix().isIdentity()) {
9091 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9092 boundingRect.set(rect);
9093 getMatrix().mapRect(boundingRect);
9094 rect.set((int) (boundingRect.left - 0.5f),
9095 (int) (boundingRect.top - 0.5f),
9096 (int) (boundingRect.right + 0.5f),
9097 (int) (boundingRect.bottom + 0.5f));
9098 }
9099 }
9100
9101 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009102 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009103 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9104 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009105 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9106 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009107 *
9108 * @hide
9109 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009110 protected void invalidateParentCaches() {
9111 if (mParent instanceof View) {
9112 ((View) mParent).mPrivateFlags |= INVALIDATED;
9113 }
9114 }
Joe Malin32736f02011-01-19 16:14:20 -08009115
Romain Guy0fd89bf2011-01-26 15:41:30 -08009116 /**
9117 * Used to indicate that the parent of this view should be invalidated. This functionality
9118 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9119 * which is necessary when various parent-managed properties of the view change, such as
9120 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9121 * an invalidation event to the parent.
9122 *
9123 * @hide
9124 */
9125 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009126 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009127 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009128 }
9129 }
9130
9131 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009132 * Indicates whether this View is opaque. An opaque View guarantees that it will
9133 * draw all the pixels overlapping its bounds using a fully opaque color.
9134 *
9135 * Subclasses of View should override this method whenever possible to indicate
9136 * whether an instance is opaque. Opaque Views are treated in a special way by
9137 * the View hierarchy, possibly allowing it to perform optimizations during
9138 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009139 *
Romain Guy24443ea2009-05-11 11:56:30 -07009140 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009141 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009142 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009143 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009144 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009145 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9146 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009147 }
9148
Adam Powell20232d02010-12-08 21:08:53 -08009149 /**
9150 * @hide
9151 */
9152 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009153 // Opaque if:
9154 // - Has a background
9155 // - Background is opaque
9156 // - Doesn't have scrollbars or scrollbars are inside overlay
9157
Philip Milne6c8ea062012-04-03 17:38:43 -07009158 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009159 mPrivateFlags |= OPAQUE_BACKGROUND;
9160 } else {
9161 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9162 }
9163
9164 final int flags = mViewFlags;
9165 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9166 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9167 mPrivateFlags |= OPAQUE_SCROLLBARS;
9168 } else {
9169 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9170 }
9171 }
9172
9173 /**
9174 * @hide
9175 */
9176 protected boolean hasOpaqueScrollbars() {
9177 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009178 }
9179
9180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 * @return A handler associated with the thread running the View. This
9182 * handler can be used to pump events in the UI events queue.
9183 */
9184 public Handler getHandler() {
9185 if (mAttachInfo != null) {
9186 return mAttachInfo.mHandler;
9187 }
9188 return null;
9189 }
9190
9191 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009192 * Gets the view root associated with the View.
9193 * @return The view root, or null if none.
9194 * @hide
9195 */
9196 public ViewRootImpl getViewRootImpl() {
9197 if (mAttachInfo != null) {
9198 return mAttachInfo.mViewRootImpl;
9199 }
9200 return null;
9201 }
9202
9203 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009204 * <p>Causes the Runnable to be added to the message queue.
9205 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009206 *
Romain Guye63a4f32011-08-11 11:33:31 -07009207 * <p>This method can be invoked from outside of the UI thread
9208 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 *
9210 * @param action The Runnable that will be executed.
9211 *
9212 * @return Returns true if the Runnable was successfully placed in to the
9213 * message queue. Returns false on failure, usually because the
9214 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009215 *
9216 * @see #postDelayed
9217 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 */
9219 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009220 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009221 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009222 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009223 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009224 // Assume that post will succeed later
9225 ViewRootImpl.getRunQueue().post(action);
9226 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 }
9228
9229 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009230 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009232 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009233 *
Romain Guye63a4f32011-08-11 11:33:31 -07009234 * <p>This method can be invoked from outside of the UI thread
9235 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 *
9237 * @param action The Runnable that will be executed.
9238 * @param delayMillis The delay (in milliseconds) until the Runnable
9239 * will be executed.
9240 *
9241 * @return true if the Runnable was successfully placed in to the
9242 * message queue. Returns false on failure, usually because the
9243 * looper processing the message queue is exiting. Note that a
9244 * result of true does not mean the Runnable will be processed --
9245 * if the looper is quit before the delivery time of the message
9246 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009247 *
9248 * @see #post
9249 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 */
9251 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009252 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009253 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009254 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009256 // Assume that post will succeed later
9257 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9258 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 }
9260
9261 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009262 * <p>Causes the Runnable to execute on the next animation time step.
9263 * The runnable will be run on the user interface thread.</p>
9264 *
9265 * <p>This method can be invoked from outside of the UI thread
9266 * only when this View is attached to a window.</p>
9267 *
9268 * @param action The Runnable that will be executed.
9269 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009270 * @see #postOnAnimationDelayed
9271 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009272 */
9273 public void postOnAnimation(Runnable action) {
9274 final AttachInfo attachInfo = mAttachInfo;
9275 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009276 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9277 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009278 } else {
9279 // Assume that post will succeed later
9280 ViewRootImpl.getRunQueue().post(action);
9281 }
9282 }
9283
9284 /**
9285 * <p>Causes the Runnable to execute on the next animation time step,
9286 * after the specified amount of time elapses.
9287 * The runnable will be run on the user interface thread.</p>
9288 *
9289 * <p>This method can be invoked from outside of the UI thread
9290 * only when this View is attached to a window.</p>
9291 *
9292 * @param action The Runnable that will be executed.
9293 * @param delayMillis The delay (in milliseconds) until the Runnable
9294 * will be executed.
9295 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009296 * @see #postOnAnimation
9297 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009298 */
9299 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9300 final AttachInfo attachInfo = mAttachInfo;
9301 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009302 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9303 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009304 } else {
9305 // Assume that post will succeed later
9306 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9307 }
9308 }
9309
9310 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009311 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009312 *
Romain Guye63a4f32011-08-11 11:33:31 -07009313 * <p>This method can be invoked from outside of the UI thread
9314 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 *
9316 * @param action The Runnable to remove from the message handling queue
9317 *
9318 * @return true if this view could ask the Handler to remove the Runnable,
9319 * false otherwise. When the returned value is true, the Runnable
9320 * may or may not have been actually removed from the message queue
9321 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009322 *
9323 * @see #post
9324 * @see #postDelayed
9325 * @see #postOnAnimation
9326 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 */
9328 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009329 if (action != null) {
9330 final AttachInfo attachInfo = mAttachInfo;
9331 if (attachInfo != null) {
9332 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009333 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9334 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009335 } else {
9336 // Assume that post will succeed later
9337 ViewRootImpl.getRunQueue().removeCallbacks(action);
9338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 return true;
9341 }
9342
9343 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009344 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9345 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 *
Romain Guye63a4f32011-08-11 11:33:31 -07009347 * <p>This method can be invoked from outside of the UI thread
9348 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009349 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009350 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009351 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 */
9353 public void postInvalidate() {
9354 postInvalidateDelayed(0);
9355 }
9356
9357 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009358 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9359 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009360 *
Romain Guye63a4f32011-08-11 11:33:31 -07009361 * <p>This method can be invoked from outside of the UI thread
9362 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 *
9364 * @param left The left coordinate of the rectangle to invalidate.
9365 * @param top The top coordinate of the rectangle to invalidate.
9366 * @param right The right coordinate of the rectangle to invalidate.
9367 * @param bottom The bottom coordinate of the rectangle to invalidate.
9368 *
9369 * @see #invalidate(int, int, int, int)
9370 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009371 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009372 */
9373 public void postInvalidate(int left, int top, int right, int bottom) {
9374 postInvalidateDelayed(0, left, top, right, bottom);
9375 }
9376
9377 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009378 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9379 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009380 *
Romain Guye63a4f32011-08-11 11:33:31 -07009381 * <p>This method can be invoked from outside of the UI thread
9382 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 *
9384 * @param delayMilliseconds the duration in milliseconds to delay the
9385 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009386 *
9387 * @see #invalidate()
9388 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009389 */
9390 public void postInvalidateDelayed(long delayMilliseconds) {
9391 // We try only with the AttachInfo because there's no point in invalidating
9392 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009393 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009394 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009395 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 }
9397 }
9398
9399 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009400 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9401 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009402 *
Romain Guye63a4f32011-08-11 11:33:31 -07009403 * <p>This method can be invoked from outside of the UI thread
9404 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 *
9406 * @param delayMilliseconds the duration in milliseconds to delay the
9407 * invalidation by
9408 * @param left The left coordinate of the rectangle to invalidate.
9409 * @param top The top coordinate of the rectangle to invalidate.
9410 * @param right The right coordinate of the rectangle to invalidate.
9411 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009412 *
9413 * @see #invalidate(int, int, int, int)
9414 * @see #invalidate(Rect)
9415 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 */
9417 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9418 int right, int bottom) {
9419
9420 // We try only with the AttachInfo because there's no point in invalidating
9421 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009422 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009423 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9425 info.target = this;
9426 info.left = left;
9427 info.top = top;
9428 info.right = right;
9429 info.bottom = bottom;
9430
Jeff Browna175a5b2012-02-15 19:18:31 -08009431 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009432 }
9433 }
9434
9435 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009436 * <p>Cause an invalidate to happen on the next animation time step, typically the
9437 * next display frame.</p>
9438 *
9439 * <p>This method can be invoked from outside of the UI thread
9440 * only when this View is attached to a window.</p>
9441 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009442 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -08009443 */
9444 public void postInvalidateOnAnimation() {
9445 // We try only with the AttachInfo because there's no point in invalidating
9446 // if we are not attached to our window
9447 final AttachInfo attachInfo = mAttachInfo;
9448 if (attachInfo != null) {
9449 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9450 }
9451 }
9452
9453 /**
9454 * <p>Cause an invalidate of the specified area to happen on the next animation
9455 * time step, typically the next display frame.</p>
9456 *
9457 * <p>This method can be invoked from outside of the UI thread
9458 * only when this View is attached to a window.</p>
9459 *
9460 * @param left The left coordinate of the rectangle to invalidate.
9461 * @param top The top coordinate of the rectangle to invalidate.
9462 * @param right The right coordinate of the rectangle to invalidate.
9463 * @param bottom The bottom coordinate of the rectangle to invalidate.
9464 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009465 * @see #invalidate(int, int, int, int)
9466 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -08009467 */
9468 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9469 // We try only with the AttachInfo because there's no point in invalidating
9470 // if we are not attached to our window
9471 final AttachInfo attachInfo = mAttachInfo;
9472 if (attachInfo != null) {
9473 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9474 info.target = this;
9475 info.left = left;
9476 info.top = top;
9477 info.right = right;
9478 info.bottom = bottom;
9479
9480 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9481 }
9482 }
9483
9484 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009485 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9486 * This event is sent at most once every
9487 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9488 */
9489 private void postSendViewScrolledAccessibilityEventCallback() {
9490 if (mSendViewScrolledAccessibilityEvent == null) {
9491 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9492 }
9493 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9494 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9495 postDelayed(mSendViewScrolledAccessibilityEvent,
9496 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9497 }
9498 }
9499
9500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 * Called by a parent to request that a child update its values for mScrollX
9502 * and mScrollY if necessary. This will typically be done if the child is
9503 * animating a scroll using a {@link android.widget.Scroller Scroller}
9504 * object.
9505 */
9506 public void computeScroll() {
9507 }
9508
9509 /**
9510 * <p>Indicate whether the horizontal edges are faded when the view is
9511 * scrolled horizontally.</p>
9512 *
9513 * @return true if the horizontal edges should are faded on scroll, false
9514 * otherwise
9515 *
9516 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009517 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009518 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009519 */
9520 public boolean isHorizontalFadingEdgeEnabled() {
9521 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9522 }
9523
9524 /**
9525 * <p>Define whether the horizontal edges should be faded when this view
9526 * is scrolled horizontally.</p>
9527 *
9528 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9529 * be faded when the view is scrolled
9530 * horizontally
9531 *
9532 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009533 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009534 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 */
9536 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9537 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9538 if (horizontalFadingEdgeEnabled) {
9539 initScrollCache();
9540 }
9541
9542 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9543 }
9544 }
9545
9546 /**
9547 * <p>Indicate whether the vertical edges are faded when the view is
9548 * scrolled horizontally.</p>
9549 *
9550 * @return true if the vertical edges should are faded on scroll, false
9551 * otherwise
9552 *
9553 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009554 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009555 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 */
9557 public boolean isVerticalFadingEdgeEnabled() {
9558 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9559 }
9560
9561 /**
9562 * <p>Define whether the vertical edges should be faded when this view
9563 * is scrolled vertically.</p>
9564 *
9565 * @param verticalFadingEdgeEnabled true if the vertical edges should
9566 * be faded when the view is scrolled
9567 * vertically
9568 *
9569 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009570 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009571 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009572 */
9573 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9574 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9575 if (verticalFadingEdgeEnabled) {
9576 initScrollCache();
9577 }
9578
9579 mViewFlags ^= FADING_EDGE_VERTICAL;
9580 }
9581 }
9582
9583 /**
9584 * Returns the strength, or intensity, of the top faded edge. The strength is
9585 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9586 * returns 0.0 or 1.0 but no value in between.
9587 *
9588 * Subclasses should override this method to provide a smoother fade transition
9589 * when scrolling occurs.
9590 *
9591 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9592 */
9593 protected float getTopFadingEdgeStrength() {
9594 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9595 }
9596
9597 /**
9598 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9599 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9600 * returns 0.0 or 1.0 but no value in between.
9601 *
9602 * Subclasses should override this method to provide a smoother fade transition
9603 * when scrolling occurs.
9604 *
9605 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9606 */
9607 protected float getBottomFadingEdgeStrength() {
9608 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9609 computeVerticalScrollRange() ? 1.0f : 0.0f;
9610 }
9611
9612 /**
9613 * Returns the strength, or intensity, of the left faded edge. The strength is
9614 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9615 * returns 0.0 or 1.0 but no value in between.
9616 *
9617 * Subclasses should override this method to provide a smoother fade transition
9618 * when scrolling occurs.
9619 *
9620 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9621 */
9622 protected float getLeftFadingEdgeStrength() {
9623 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9624 }
9625
9626 /**
9627 * Returns the strength, or intensity, of the right faded edge. The strength is
9628 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9629 * returns 0.0 or 1.0 but no value in between.
9630 *
9631 * Subclasses should override this method to provide a smoother fade transition
9632 * when scrolling occurs.
9633 *
9634 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9635 */
9636 protected float getRightFadingEdgeStrength() {
9637 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9638 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9639 }
9640
9641 /**
9642 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9643 * scrollbar is not drawn by default.</p>
9644 *
9645 * @return true if the horizontal scrollbar should be painted, false
9646 * otherwise
9647 *
9648 * @see #setHorizontalScrollBarEnabled(boolean)
9649 */
9650 public boolean isHorizontalScrollBarEnabled() {
9651 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9652 }
9653
9654 /**
9655 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9656 * scrollbar is not drawn by default.</p>
9657 *
9658 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9659 * be painted
9660 *
9661 * @see #isHorizontalScrollBarEnabled()
9662 */
9663 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9664 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9665 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009666 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009667 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009668 }
9669 }
9670
9671 /**
9672 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9673 * scrollbar is not drawn by default.</p>
9674 *
9675 * @return true if the vertical scrollbar should be painted, false
9676 * otherwise
9677 *
9678 * @see #setVerticalScrollBarEnabled(boolean)
9679 */
9680 public boolean isVerticalScrollBarEnabled() {
9681 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9682 }
9683
9684 /**
9685 * <p>Define whether the vertical scrollbar should be drawn or not. The
9686 * scrollbar is not drawn by default.</p>
9687 *
9688 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9689 * be painted
9690 *
9691 * @see #isVerticalScrollBarEnabled()
9692 */
9693 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9694 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9695 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009696 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009697 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 }
9699 }
9700
Adam Powell20232d02010-12-08 21:08:53 -08009701 /**
9702 * @hide
9703 */
9704 protected void recomputePadding() {
9705 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 }
Joe Malin32736f02011-01-19 16:14:20 -08009707
Mike Cleronfe81d382009-09-28 14:22:16 -07009708 /**
9709 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009710 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009711 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009712 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009713 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -07009714 */
9715 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9716 initScrollCache();
9717 final ScrollabilityCache scrollabilityCache = mScrollCache;
9718 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009719 if (fadeScrollbars) {
9720 scrollabilityCache.state = ScrollabilityCache.OFF;
9721 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009722 scrollabilityCache.state = ScrollabilityCache.ON;
9723 }
9724 }
Joe Malin32736f02011-01-19 16:14:20 -08009725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009726 /**
Joe Malin32736f02011-01-19 16:14:20 -08009727 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009728 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009729 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009730 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -07009731 *
9732 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -07009733 */
9734 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009735 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009736 }
Joe Malin32736f02011-01-19 16:14:20 -08009737
Mike Cleron52f0a642009-09-28 18:21:37 -07009738 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07009739 *
9740 * Returns the delay before scrollbars fade.
9741 *
9742 * @return the delay before scrollbars fade
9743 *
9744 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9745 */
9746 public int getScrollBarDefaultDelayBeforeFade() {
9747 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
9748 mScrollCache.scrollBarDefaultDelayBeforeFade;
9749 }
9750
9751 /**
9752 * Define the delay before scrollbars fade.
9753 *
9754 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
9755 *
9756 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9757 */
9758 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
9759 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
9760 }
9761
9762 /**
9763 *
9764 * Returns the scrollbar fade duration.
9765 *
9766 * @return the scrollbar fade duration
9767 *
9768 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9769 */
9770 public int getScrollBarFadeDuration() {
9771 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
9772 mScrollCache.scrollBarFadeDuration;
9773 }
9774
9775 /**
9776 * Define the scrollbar fade duration.
9777 *
9778 * @param scrollBarFadeDuration - the scrollbar fade duration
9779 *
9780 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9781 */
9782 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
9783 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
9784 }
9785
9786 /**
9787 *
9788 * Returns the scrollbar size.
9789 *
9790 * @return the scrollbar size
9791 *
9792 * @attr ref android.R.styleable#View_scrollbarSize
9793 */
9794 public int getScrollBarSize() {
9795 return mScrollCache == null ? ViewConfiguration.getScrollBarSize() :
9796 mScrollCache.scrollBarSize;
9797 }
9798
9799 /**
9800 * Define the scrollbar size.
9801 *
9802 * @param scrollBarSize - the scrollbar size
9803 *
9804 * @attr ref android.R.styleable#View_scrollbarSize
9805 */
9806 public void setScrollBarSize(int scrollBarSize) {
9807 getScrollCache().scrollBarSize = scrollBarSize;
9808 }
9809
9810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9812 * inset. When inset, they add to the padding of the view. And the scrollbars
9813 * can be drawn inside the padding area or on the edge of the view. For example,
9814 * if a view has a background drawable and you want to draw the scrollbars
9815 * inside the padding specified by the drawable, you can use
9816 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9817 * appear at the edge of the view, ignoring the padding, then you can use
9818 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9819 * @param style the style of the scrollbars. Should be one of
9820 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9821 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9822 * @see #SCROLLBARS_INSIDE_OVERLAY
9823 * @see #SCROLLBARS_INSIDE_INSET
9824 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9825 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009826 *
9827 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828 */
9829 public void setScrollBarStyle(int style) {
9830 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9831 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009832 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009833 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 }
9835 }
9836
9837 /**
9838 * <p>Returns the current scrollbar style.</p>
9839 * @return the current scrollbar style
9840 * @see #SCROLLBARS_INSIDE_OVERLAY
9841 * @see #SCROLLBARS_INSIDE_INSET
9842 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9843 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009844 *
9845 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009847 @ViewDebug.ExportedProperty(mapping = {
9848 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9849 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9850 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9851 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9852 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 public int getScrollBarStyle() {
9854 return mViewFlags & SCROLLBARS_STYLE_MASK;
9855 }
9856
9857 /**
9858 * <p>Compute the horizontal range that the horizontal scrollbar
9859 * represents.</p>
9860 *
9861 * <p>The range is expressed in arbitrary units that must be the same as the
9862 * units used by {@link #computeHorizontalScrollExtent()} and
9863 * {@link #computeHorizontalScrollOffset()}.</p>
9864 *
9865 * <p>The default range is the drawing width of this view.</p>
9866 *
9867 * @return the total horizontal range represented by the horizontal
9868 * scrollbar
9869 *
9870 * @see #computeHorizontalScrollExtent()
9871 * @see #computeHorizontalScrollOffset()
9872 * @see android.widget.ScrollBarDrawable
9873 */
9874 protected int computeHorizontalScrollRange() {
9875 return getWidth();
9876 }
9877
9878 /**
9879 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9880 * within the horizontal range. This value is used to compute the position
9881 * of the thumb within the scrollbar's track.</p>
9882 *
9883 * <p>The range is expressed in arbitrary units that must be the same as the
9884 * units used by {@link #computeHorizontalScrollRange()} and
9885 * {@link #computeHorizontalScrollExtent()}.</p>
9886 *
9887 * <p>The default offset is the scroll offset of this view.</p>
9888 *
9889 * @return the horizontal offset of the scrollbar's thumb
9890 *
9891 * @see #computeHorizontalScrollRange()
9892 * @see #computeHorizontalScrollExtent()
9893 * @see android.widget.ScrollBarDrawable
9894 */
9895 protected int computeHorizontalScrollOffset() {
9896 return mScrollX;
9897 }
9898
9899 /**
9900 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9901 * within the horizontal range. This value is used to compute the length
9902 * of the thumb within the scrollbar's track.</p>
9903 *
9904 * <p>The range is expressed in arbitrary units that must be the same as the
9905 * units used by {@link #computeHorizontalScrollRange()} and
9906 * {@link #computeHorizontalScrollOffset()}.</p>
9907 *
9908 * <p>The default extent is the drawing width of this view.</p>
9909 *
9910 * @return the horizontal extent of the scrollbar's thumb
9911 *
9912 * @see #computeHorizontalScrollRange()
9913 * @see #computeHorizontalScrollOffset()
9914 * @see android.widget.ScrollBarDrawable
9915 */
9916 protected int computeHorizontalScrollExtent() {
9917 return getWidth();
9918 }
9919
9920 /**
9921 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9922 *
9923 * <p>The range is expressed in arbitrary units that must be the same as the
9924 * units used by {@link #computeVerticalScrollExtent()} and
9925 * {@link #computeVerticalScrollOffset()}.</p>
9926 *
9927 * @return the total vertical range represented by the vertical scrollbar
9928 *
9929 * <p>The default range is the drawing height of this view.</p>
9930 *
9931 * @see #computeVerticalScrollExtent()
9932 * @see #computeVerticalScrollOffset()
9933 * @see android.widget.ScrollBarDrawable
9934 */
9935 protected int computeVerticalScrollRange() {
9936 return getHeight();
9937 }
9938
9939 /**
9940 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9941 * within the horizontal range. This value is used to compute the position
9942 * of the thumb within the scrollbar's track.</p>
9943 *
9944 * <p>The range is expressed in arbitrary units that must be the same as the
9945 * units used by {@link #computeVerticalScrollRange()} and
9946 * {@link #computeVerticalScrollExtent()}.</p>
9947 *
9948 * <p>The default offset is the scroll offset of this view.</p>
9949 *
9950 * @return the vertical offset of the scrollbar's thumb
9951 *
9952 * @see #computeVerticalScrollRange()
9953 * @see #computeVerticalScrollExtent()
9954 * @see android.widget.ScrollBarDrawable
9955 */
9956 protected int computeVerticalScrollOffset() {
9957 return mScrollY;
9958 }
9959
9960 /**
9961 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9962 * within the vertical range. This value is used to compute the length
9963 * of the thumb within the scrollbar's track.</p>
9964 *
9965 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009966 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 * {@link #computeVerticalScrollOffset()}.</p>
9968 *
9969 * <p>The default extent is the drawing height of this view.</p>
9970 *
9971 * @return the vertical extent of the scrollbar's thumb
9972 *
9973 * @see #computeVerticalScrollRange()
9974 * @see #computeVerticalScrollOffset()
9975 * @see android.widget.ScrollBarDrawable
9976 */
9977 protected int computeVerticalScrollExtent() {
9978 return getHeight();
9979 }
9980
9981 /**
Adam Powell69159442011-06-13 17:53:06 -07009982 * Check if this view can be scrolled horizontally in a certain direction.
9983 *
9984 * @param direction Negative to check scrolling left, positive to check scrolling right.
9985 * @return true if this view can be scrolled in the specified direction, false otherwise.
9986 */
9987 public boolean canScrollHorizontally(int direction) {
9988 final int offset = computeHorizontalScrollOffset();
9989 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9990 if (range == 0) return false;
9991 if (direction < 0) {
9992 return offset > 0;
9993 } else {
9994 return offset < range - 1;
9995 }
9996 }
9997
9998 /**
9999 * Check if this view can be scrolled vertically in a certain direction.
10000 *
10001 * @param direction Negative to check scrolling up, positive to check scrolling down.
10002 * @return true if this view can be scrolled in the specified direction, false otherwise.
10003 */
10004 public boolean canScrollVertically(int direction) {
10005 final int offset = computeVerticalScrollOffset();
10006 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10007 if (range == 0) return false;
10008 if (direction < 0) {
10009 return offset > 0;
10010 } else {
10011 return offset < range - 1;
10012 }
10013 }
10014
10015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10017 * scrollbars are painted only if they have been awakened first.</p>
10018 *
10019 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010020 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010021 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010023 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 // scrollbars are drawn only when the animation is running
10025 final ScrollabilityCache cache = mScrollCache;
10026 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010027
Mike Cleronf116bf82009-09-27 19:14:12 -070010028 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010029
Mike Cleronf116bf82009-09-27 19:14:12 -070010030 if (state == ScrollabilityCache.OFF) {
10031 return;
10032 }
Joe Malin32736f02011-01-19 16:14:20 -080010033
Mike Cleronf116bf82009-09-27 19:14:12 -070010034 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010035
Mike Cleronf116bf82009-09-27 19:14:12 -070010036 if (state == ScrollabilityCache.FADING) {
10037 // We're fading -- get our fade interpolation
10038 if (cache.interpolatorValues == null) {
10039 cache.interpolatorValues = new float[1];
10040 }
Joe Malin32736f02011-01-19 16:14:20 -080010041
Mike Cleronf116bf82009-09-27 19:14:12 -070010042 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010043
Mike Cleronf116bf82009-09-27 19:14:12 -070010044 // Stops the animation if we're done
10045 if (cache.scrollBarInterpolator.timeToValues(values) ==
10046 Interpolator.Result.FREEZE_END) {
10047 cache.state = ScrollabilityCache.OFF;
10048 } else {
10049 cache.scrollBar.setAlpha(Math.round(values[0]));
10050 }
Joe Malin32736f02011-01-19 16:14:20 -080010051
10052 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010053 // drawing. We only want this when we're fading so that
10054 // we prevent excessive redraws
10055 invalidate = true;
10056 } else {
10057 // We're just on -- but we may have been fading before so
10058 // reset alpha
10059 cache.scrollBar.setAlpha(255);
10060 }
10061
Joe Malin32736f02011-01-19 16:14:20 -080010062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 final int viewFlags = mViewFlags;
10064
10065 final boolean drawHorizontalScrollBar =
10066 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10067 final boolean drawVerticalScrollBar =
10068 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10069 && !isVerticalScrollBarHidden();
10070
10071 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10072 final int width = mRight - mLeft;
10073 final int height = mBottom - mTop;
10074
10075 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076
Mike Reede8853fc2009-09-04 14:01:48 -040010077 final int scrollX = mScrollX;
10078 final int scrollY = mScrollY;
10079 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10080
Mike Cleronf116bf82009-09-27 19:14:12 -070010081 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010084 int size = scrollBar.getSize(false);
10085 if (size <= 0) {
10086 size = cache.scrollBarSize;
10087 }
10088
Mike Cleronf116bf82009-09-27 19:14:12 -070010089 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010090 computeHorizontalScrollOffset(),
10091 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010092 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010093 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010094 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010095 left = scrollX + (mPaddingLeft & inside);
10096 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10097 bottom = top + size;
10098 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10099 if (invalidate) {
10100 invalidate(left, top, right, bottom);
10101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 }
10103
10104 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010105 int size = scrollBar.getSize(true);
10106 if (size <= 0) {
10107 size = cache.scrollBarSize;
10108 }
10109
Mike Reede8853fc2009-09-04 14:01:48 -040010110 scrollBar.setParameters(computeVerticalScrollRange(),
10111 computeVerticalScrollOffset(),
10112 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010113 switch (mVerticalScrollbarPosition) {
10114 default:
10115 case SCROLLBAR_POSITION_DEFAULT:
10116 case SCROLLBAR_POSITION_RIGHT:
10117 left = scrollX + width - size - (mUserPaddingRight & inside);
10118 break;
10119 case SCROLLBAR_POSITION_LEFT:
10120 left = scrollX + (mUserPaddingLeft & inside);
10121 break;
10122 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010123 top = scrollY + (mPaddingTop & inside);
10124 right = left + size;
10125 bottom = scrollY + height - (mUserPaddingBottom & inside);
10126 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10127 if (invalidate) {
10128 invalidate(left, top, right, bottom);
10129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 }
10131 }
10132 }
10133 }
Romain Guy8506ab42009-06-11 17:35:47 -070010134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010136 * 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 -080010137 * FastScroller is visible.
10138 * @return whether to temporarily hide the vertical scrollbar
10139 * @hide
10140 */
10141 protected boolean isVerticalScrollBarHidden() {
10142 return false;
10143 }
10144
10145 /**
10146 * <p>Draw the horizontal scrollbar if
10147 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10148 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 * @param canvas the canvas on which to draw the scrollbar
10150 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 *
10152 * @see #isHorizontalScrollBarEnabled()
10153 * @see #computeHorizontalScrollRange()
10154 * @see #computeHorizontalScrollExtent()
10155 * @see #computeHorizontalScrollOffset()
10156 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010157 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010158 */
Romain Guy8fb95422010-08-17 18:38:51 -070010159 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10160 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010161 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010162 scrollBar.draw(canvas);
10163 }
Mike Reede8853fc2009-09-04 14:01:48 -040010164
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010166 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10167 * returns true.</p>
10168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010169 * @param canvas the canvas on which to draw the scrollbar
10170 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 *
10172 * @see #isVerticalScrollBarEnabled()
10173 * @see #computeVerticalScrollRange()
10174 * @see #computeVerticalScrollExtent()
10175 * @see #computeVerticalScrollOffset()
10176 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010177 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010178 */
Romain Guy8fb95422010-08-17 18:38:51 -070010179 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10180 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010181 scrollBar.setBounds(l, t, r, b);
10182 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183 }
10184
10185 /**
10186 * Implement this to do your drawing.
10187 *
10188 * @param canvas the canvas on which the background will be drawn
10189 */
10190 protected void onDraw(Canvas canvas) {
10191 }
10192
10193 /*
10194 * Caller is responsible for calling requestLayout if necessary.
10195 * (This allows addViewInLayout to not request a new layout.)
10196 */
10197 void assignParent(ViewParent parent) {
10198 if (mParent == null) {
10199 mParent = parent;
10200 } else if (parent == null) {
10201 mParent = null;
10202 } else {
10203 throw new RuntimeException("view " + this + " being added, but"
10204 + " it already has a parent");
10205 }
10206 }
10207
10208 /**
10209 * This is called when the view is attached to a window. At this point it
10210 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010211 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10212 * however it may be called any time before the first onDraw -- including
10213 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 *
10215 * @see #onDetachedFromWindow()
10216 */
10217 protected void onAttachedToWindow() {
10218 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10219 mParent.requestTransparentRegion(this);
10220 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010221 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10222 initialAwakenScrollBars();
10223 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10224 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010225 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010226 // Order is important here: LayoutDirection MUST be resolved before Padding
10227 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010228 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010229 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010230 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010231 resolveTextAlignment();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010232 if (isFocused()) {
10233 InputMethodManager imm = InputMethodManager.peekInstance();
10234 imm.focusIn(this);
10235 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010236 }
Cibu Johny86666632010-02-22 13:01:02 -080010237
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010238 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010239 * @see #onScreenStateChanged(int)
10240 */
10241 void dispatchScreenStateChanged(int screenState) {
10242 onScreenStateChanged(screenState);
10243 }
10244
10245 /**
10246 * This method is called whenever the state of the screen this view is
10247 * attached to changes. A state change will usually occurs when the screen
10248 * turns on or off (whether it happens automatically or the user does it
10249 * manually.)
10250 *
10251 * @param screenState The new state of the screen. Can be either
10252 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10253 */
10254 public void onScreenStateChanged(int screenState) {
10255 }
10256
10257 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010258 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10259 */
10260 private boolean hasRtlSupport() {
10261 return mContext.getApplicationInfo().hasRtlSupport();
10262 }
10263
10264 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010265 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10266 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010267 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010268 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010269 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010270 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010271 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010272
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010273 if (hasRtlSupport()) {
10274 // Set resolved depending on layout direction
10275 switch (getLayoutDirection()) {
10276 case LAYOUT_DIRECTION_INHERIT:
10277 // If this is root view, no need to look at parent's layout dir.
10278 if (canResolveLayoutDirection()) {
10279 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010280
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010281 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10282 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10283 }
10284 } else {
10285 // Nothing to do, LTR by default
10286 }
10287 break;
10288 case LAYOUT_DIRECTION_RTL:
10289 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10290 break;
10291 case LAYOUT_DIRECTION_LOCALE:
10292 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010293 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10294 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010295 break;
10296 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010297 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010298 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010299 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010300
10301 // Set to resolved
10302 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010303 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010304 // Resolve padding
10305 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010306 }
10307
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010308 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010309 * Called when layout direction has been resolved.
10310 *
10311 * The default implementation does nothing.
10312 */
10313 public void onResolvedLayoutDirectionChanged() {
10314 }
10315
10316 /**
10317 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010318 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010319 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010320 // If the user specified the absolute padding (either with android:padding or
10321 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10322 // use the default padding or the padding from the background drawable
10323 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010324 int resolvedLayoutDirection = getResolvedLayoutDirection();
10325 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010326 case LAYOUT_DIRECTION_RTL:
10327 // Start user padding override Right user padding. Otherwise, if Right user
10328 // padding is not defined, use the default Right padding. If Right user padding
10329 // is defined, just use it.
10330 if (mUserPaddingStart >= 0) {
10331 mUserPaddingRight = mUserPaddingStart;
10332 } else if (mUserPaddingRight < 0) {
10333 mUserPaddingRight = mPaddingRight;
10334 }
10335 if (mUserPaddingEnd >= 0) {
10336 mUserPaddingLeft = mUserPaddingEnd;
10337 } else if (mUserPaddingLeft < 0) {
10338 mUserPaddingLeft = mPaddingLeft;
10339 }
10340 break;
10341 case LAYOUT_DIRECTION_LTR:
10342 default:
10343 // Start user padding override Left user padding. Otherwise, if Left user
10344 // padding is not defined, use the default left padding. If Left user padding
10345 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010346 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010347 mUserPaddingLeft = mUserPaddingStart;
10348 } else if (mUserPaddingLeft < 0) {
10349 mUserPaddingLeft = mPaddingLeft;
10350 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010351 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010352 mUserPaddingRight = mUserPaddingEnd;
10353 } else if (mUserPaddingRight < 0) {
10354 mUserPaddingRight = mPaddingRight;
10355 }
10356 }
10357
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010358 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10359
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010360 if(isPaddingRelative()) {
10361 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10362 } else {
10363 recomputePadding();
10364 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010365 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010366 }
10367
10368 /**
10369 * Resolve padding depending on the layout direction. Subclasses that care about
10370 * padding resolution should override this method. The default implementation does
10371 * nothing.
10372 *
10373 * @param layoutDirection the direction of the layout
10374 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010375 * @see {@link #LAYOUT_DIRECTION_LTR}
10376 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010377 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010378 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010379 }
10380
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010381 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010382 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010383 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010384 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010385 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010386 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010387 switch (getLayoutDirection()) {
10388 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010389 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010390 default:
10391 return true;
10392 }
10393 }
10394
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010395 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010396 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10397 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010398 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010399 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010400 // Reset the current resolved bits
10401 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010402 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010403 // Reset also the text direction
10404 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010405 }
10406
10407 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010408 * Called during reset of resolved layout direction.
10409 *
10410 * Subclasses need to override this method to clear cached information that depends on the
10411 * resolved layout direction, or to inform child views that inherit their layout direction.
10412 *
10413 * The default implementation does nothing.
10414 */
10415 public void onResolvedLayoutDirectionReset() {
10416 }
10417
10418 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010419 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010420 *
10421 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010422 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010423 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010424 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010425 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 }
10427
10428 /**
10429 * This is called when the view is detached from a window. At this point it
10430 * no longer has a surface for drawing.
10431 *
10432 * @see #onAttachedToWindow()
10433 */
10434 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010435 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010436
Romain Guya440b002010-02-24 15:57:54 -080010437 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010438 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010439 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010440 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010443
Romain Guya998dff2012-03-23 18:58:36 -070010444 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010445
10446 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010447 if (mDisplayList != null) {
10448 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10449 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010450 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010451 } else {
10452 if (mDisplayList != null) {
10453 // Should never happen
10454 mDisplayList.invalidate();
10455 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010456 }
10457
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010458 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010459
10460 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010461 resetResolvedTextAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 }
10463
10464 /**
10465 * @return The number of times this view has been attached to a window
10466 */
10467 protected int getWindowAttachCount() {
10468 return mWindowAttachCount;
10469 }
10470
10471 /**
10472 * Retrieve a unique token identifying the window this view is attached to.
10473 * @return Return the window's token for use in
10474 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10475 */
10476 public IBinder getWindowToken() {
10477 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10478 }
10479
10480 /**
10481 * Retrieve a unique token identifying the top-level "real" window of
10482 * the window that this view is attached to. That is, this is like
10483 * {@link #getWindowToken}, except if the window this view in is a panel
10484 * window (attached to another containing window), then the token of
10485 * the containing window is returned instead.
10486 *
10487 * @return Returns the associated window token, either
10488 * {@link #getWindowToken()} or the containing window's token.
10489 */
10490 public IBinder getApplicationWindowToken() {
10491 AttachInfo ai = mAttachInfo;
10492 if (ai != null) {
10493 IBinder appWindowToken = ai.mPanelParentWindowToken;
10494 if (appWindowToken == null) {
10495 appWindowToken = ai.mWindowToken;
10496 }
10497 return appWindowToken;
10498 }
10499 return null;
10500 }
10501
10502 /**
10503 * Retrieve private session object this view hierarchy is using to
10504 * communicate with the window manager.
10505 * @return the session object to communicate with the window manager
10506 */
10507 /*package*/ IWindowSession getWindowSession() {
10508 return mAttachInfo != null ? mAttachInfo.mSession : null;
10509 }
10510
10511 /**
10512 * @param info the {@link android.view.View.AttachInfo} to associated with
10513 * this view
10514 */
10515 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10516 //System.out.println("Attached! " + this);
10517 mAttachInfo = info;
10518 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010519 // We will need to evaluate the drawable state at least once.
10520 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 if (mFloatingTreeObserver != null) {
10522 info.mTreeObserver.merge(mFloatingTreeObserver);
10523 mFloatingTreeObserver = null;
10524 }
10525 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10526 mAttachInfo.mScrollContainers.add(this);
10527 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10528 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070010529 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010531
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010532 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010533 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010534 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010535 if (listeners != null && listeners.size() > 0) {
10536 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10537 // perform the dispatching. The iterator is a safe guard against listeners that
10538 // could mutate the list by calling the various add/remove methods. This prevents
10539 // the array from being modified while we iterate it.
10540 for (OnAttachStateChangeListener listener : listeners) {
10541 listener.onViewAttachedToWindow(this);
10542 }
10543 }
10544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545 int vis = info.mWindowVisibility;
10546 if (vis != GONE) {
10547 onWindowVisibilityChanged(vis);
10548 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010549 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10550 // If nobody has evaluated the drawable state yet, then do it now.
10551 refreshDrawableState();
10552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 }
10554
10555 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 AttachInfo info = mAttachInfo;
10557 if (info != null) {
10558 int vis = info.mWindowVisibility;
10559 if (vis != GONE) {
10560 onWindowVisibilityChanged(GONE);
10561 }
10562 }
10563
10564 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010565
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010566 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010567 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010568 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010569 if (listeners != null && listeners.size() > 0) {
10570 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10571 // perform the dispatching. The iterator is a safe guard against listeners that
10572 // could mutate the list by calling the various add/remove methods. This prevents
10573 // the array from being modified while we iterate it.
10574 for (OnAttachStateChangeListener listener : listeners) {
10575 listener.onViewDetachedFromWindow(this);
10576 }
10577 }
10578
Romain Guy01d5edc2011-01-28 11:28:53 -080010579 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 mAttachInfo.mScrollContainers.remove(this);
10581 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10582 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584 mAttachInfo = null;
10585 }
10586
10587 /**
10588 * Store this view hierarchy's frozen state into the given container.
10589 *
10590 * @param container The SparseArray in which to save the view's state.
10591 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010592 * @see #restoreHierarchyState(android.util.SparseArray)
10593 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10594 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 */
10596 public void saveHierarchyState(SparseArray<Parcelable> container) {
10597 dispatchSaveInstanceState(container);
10598 }
10599
10600 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010601 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10602 * this view and its children. May be overridden to modify how freezing happens to a
10603 * 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 -080010604 *
10605 * @param container The SparseArray in which to save the view's state.
10606 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010607 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10608 * @see #saveHierarchyState(android.util.SparseArray)
10609 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 */
10611 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10612 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10613 mPrivateFlags &= ~SAVE_STATE_CALLED;
10614 Parcelable state = onSaveInstanceState();
10615 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10616 throw new IllegalStateException(
10617 "Derived class did not call super.onSaveInstanceState()");
10618 }
10619 if (state != null) {
10620 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10621 // + ": " + state);
10622 container.put(mID, state);
10623 }
10624 }
10625 }
10626
10627 /**
10628 * Hook allowing a view to generate a representation of its internal state
10629 * that can later be used to create a new instance with that same state.
10630 * This state should only contain information that is not persistent or can
10631 * not be reconstructed later. For example, you will never store your
10632 * current position on screen because that will be computed again when a
10633 * new instance of the view is placed in its view hierarchy.
10634 * <p>
10635 * Some examples of things you may store here: the current cursor position
10636 * in a text view (but usually not the text itself since that is stored in a
10637 * content provider or other persistent storage), the currently selected
10638 * item in a list view.
10639 *
10640 * @return Returns a Parcelable object containing the view's current dynamic
10641 * state, or null if there is nothing interesting to save. The
10642 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070010643 * @see #onRestoreInstanceState(android.os.Parcelable)
10644 * @see #saveHierarchyState(android.util.SparseArray)
10645 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 * @see #setSaveEnabled(boolean)
10647 */
10648 protected Parcelable onSaveInstanceState() {
10649 mPrivateFlags |= SAVE_STATE_CALLED;
10650 return BaseSavedState.EMPTY_STATE;
10651 }
10652
10653 /**
10654 * Restore this view hierarchy's frozen state from the given container.
10655 *
10656 * @param container The SparseArray which holds previously frozen states.
10657 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010658 * @see #saveHierarchyState(android.util.SparseArray)
10659 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10660 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 */
10662 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10663 dispatchRestoreInstanceState(container);
10664 }
10665
10666 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010667 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10668 * state for this view and its children. May be overridden to modify how restoring
10669 * happens to a view's children; for example, some views may want to not store state
10670 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 *
10672 * @param container The SparseArray which holds previously saved state.
10673 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010674 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10675 * @see #restoreHierarchyState(android.util.SparseArray)
10676 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 */
10678 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10679 if (mID != NO_ID) {
10680 Parcelable state = container.get(mID);
10681 if (state != null) {
10682 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10683 // + ": " + state);
10684 mPrivateFlags &= ~SAVE_STATE_CALLED;
10685 onRestoreInstanceState(state);
10686 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10687 throw new IllegalStateException(
10688 "Derived class did not call super.onRestoreInstanceState()");
10689 }
10690 }
10691 }
10692 }
10693
10694 /**
10695 * Hook allowing a view to re-apply a representation of its internal state that had previously
10696 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10697 * null state.
10698 *
10699 * @param state The frozen state that had previously been returned by
10700 * {@link #onSaveInstanceState}.
10701 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010702 * @see #onSaveInstanceState()
10703 * @see #restoreHierarchyState(android.util.SparseArray)
10704 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 */
10706 protected void onRestoreInstanceState(Parcelable state) {
10707 mPrivateFlags |= SAVE_STATE_CALLED;
10708 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010709 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10710 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010711 + "when two views of different type have the same id in the same hierarchy. "
10712 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010713 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010714 }
10715 }
10716
10717 /**
10718 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10719 *
10720 * @return the drawing start time in milliseconds
10721 */
10722 public long getDrawingTime() {
10723 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10724 }
10725
10726 /**
10727 * <p>Enables or disables the duplication of the parent's state into this view. When
10728 * duplication is enabled, this view gets its drawable state from its parent rather
10729 * than from its own internal properties.</p>
10730 *
10731 * <p>Note: in the current implementation, setting this property to true after the
10732 * view was added to a ViewGroup might have no effect at all. This property should
10733 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10734 *
10735 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10736 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010737 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010738 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10739 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 *
10741 * @param enabled True to enable duplication of the parent's drawable state, false
10742 * to disable it.
10743 *
10744 * @see #getDrawableState()
10745 * @see #isDuplicateParentStateEnabled()
10746 */
10747 public void setDuplicateParentStateEnabled(boolean enabled) {
10748 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10749 }
10750
10751 /**
10752 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10753 *
10754 * @return True if this view's drawable state is duplicated from the parent,
10755 * false otherwise
10756 *
10757 * @see #getDrawableState()
10758 * @see #setDuplicateParentStateEnabled(boolean)
10759 */
10760 public boolean isDuplicateParentStateEnabled() {
10761 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10762 }
10763
10764 /**
Romain Guy171c5922011-01-06 10:04:23 -080010765 * <p>Specifies the type of layer backing this view. The layer can be
10766 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10767 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010768 *
Romain Guy171c5922011-01-06 10:04:23 -080010769 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10770 * instance that controls how the layer is composed on screen. The following
10771 * properties of the paint are taken into account when composing the layer:</p>
10772 * <ul>
10773 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10774 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10775 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10776 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010777 *
Romain Guy171c5922011-01-06 10:04:23 -080010778 * <p>If this view has an alpha value set to < 1.0 by calling
10779 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10780 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10781 * equivalent to setting a hardware layer on this view and providing a paint with
10782 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010783 *
Romain Guy171c5922011-01-06 10:04:23 -080010784 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10785 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10786 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010787 *
Romain Guy171c5922011-01-06 10:04:23 -080010788 * @param layerType The ype of layer to use with this view, must be one of
10789 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10790 * {@link #LAYER_TYPE_HARDWARE}
10791 * @param paint The paint used to compose the layer. This argument is optional
10792 * and can be null. It is ignored when the layer type is
10793 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010794 *
10795 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010796 * @see #LAYER_TYPE_NONE
10797 * @see #LAYER_TYPE_SOFTWARE
10798 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010799 * @see #setAlpha(float)
10800 *
Romain Guy171c5922011-01-06 10:04:23 -080010801 * @attr ref android.R.styleable#View_layerType
10802 */
10803 public void setLayerType(int layerType, Paint paint) {
10804 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010805 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010806 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10807 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010808
Romain Guyd6cd5722011-01-17 14:42:41 -080010809 if (layerType == mLayerType) {
10810 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10811 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010812 invalidateParentCaches();
10813 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010814 }
10815 return;
10816 }
Romain Guy171c5922011-01-06 10:04:23 -080010817
10818 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010819 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010820 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070010821 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010822 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010823 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010824 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010825 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010826 default:
10827 break;
Romain Guy171c5922011-01-06 10:04:23 -080010828 }
10829
10830 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010831 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10832 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10833 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010834
Romain Guy0fd89bf2011-01-26 15:41:30 -080010835 invalidateParentCaches();
10836 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010837 }
10838
10839 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010840 * Indicates whether this view has a static layer. A view with layer type
10841 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10842 * dynamic.
10843 */
10844 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010845 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010846 }
10847
10848 /**
Romain Guy171c5922011-01-06 10:04:23 -080010849 * Indicates what type of layer is currently associated with this view. By default
10850 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10851 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10852 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010853 *
Romain Guy171c5922011-01-06 10:04:23 -080010854 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10855 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010856 *
10857 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070010858 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010859 * @see #LAYER_TYPE_NONE
10860 * @see #LAYER_TYPE_SOFTWARE
10861 * @see #LAYER_TYPE_HARDWARE
10862 */
10863 public int getLayerType() {
10864 return mLayerType;
10865 }
Joe Malin32736f02011-01-19 16:14:20 -080010866
Romain Guy6c319ca2011-01-11 14:29:25 -080010867 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010868 * Forces this view's layer to be created and this view to be rendered
10869 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10870 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070010871 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010872 * This method can for instance be used to render a view into its layer before
10873 * starting an animation. If this view is complex, rendering into the layer
10874 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070010875 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010876 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070010877 *
10878 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010879 */
10880 public void buildLayer() {
10881 if (mLayerType == LAYER_TYPE_NONE) return;
10882
10883 if (mAttachInfo == null) {
10884 throw new IllegalStateException("This view must be attached to a window first");
10885 }
10886
10887 switch (mLayerType) {
10888 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010889 if (mAttachInfo.mHardwareRenderer != null &&
10890 mAttachInfo.mHardwareRenderer.isEnabled() &&
10891 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010892 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010893 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010894 break;
10895 case LAYER_TYPE_SOFTWARE:
10896 buildDrawingCache(true);
10897 break;
10898 }
10899 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010900
Romain Guy9c4b79a2011-11-10 19:23:58 -080010901 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10902 void flushLayer() {
10903 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10904 mHardwareLayer.flush();
10905 }
10906 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010907
10908 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010909 * <p>Returns a hardware layer that can be used to draw this view again
10910 * without executing its draw method.</p>
10911 *
10912 * @return A HardwareLayer ready to render, or null if an error occurred.
10913 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010914 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010915 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10916 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010917 return null;
10918 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010919
Romain Guy9c4b79a2011-11-10 19:23:58 -080010920 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010921
10922 final int width = mRight - mLeft;
10923 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010924
Romain Guy6c319ca2011-01-11 14:29:25 -080010925 if (width == 0 || height == 0) {
10926 return null;
10927 }
10928
10929 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10930 if (mHardwareLayer == null) {
10931 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10932 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010933 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010934 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10935 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010936 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010937 }
10938
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010939 // The layer is not valid if the underlying GPU resources cannot be allocated
10940 if (!mHardwareLayer.isValid()) {
10941 return null;
10942 }
10943
Chet Haasea1cff502012-02-21 13:43:44 -080010944 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010945 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010946 }
10947
10948 return mHardwareLayer;
10949 }
Romain Guy171c5922011-01-06 10:04:23 -080010950
Romain Guy589b0bb2011-10-10 13:57:47 -070010951 /**
10952 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070010953 *
Romain Guy589b0bb2011-10-10 13:57:47 -070010954 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070010955 *
10956 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070010957 * @see #LAYER_TYPE_HARDWARE
10958 */
Romain Guya998dff2012-03-23 18:58:36 -070010959 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070010960 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010961 AttachInfo info = mAttachInfo;
10962 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070010963 info.mHardwareRenderer.isEnabled() &&
10964 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010965 mHardwareLayer.destroy();
10966 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010967
Romain Guy9c4b79a2011-11-10 19:23:58 -080010968 invalidate(true);
10969 invalidateParentCaches();
10970 }
Romain Guy65b345f2011-07-27 18:51:50 -070010971 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010972 }
Romain Guy65b345f2011-07-27 18:51:50 -070010973 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010974 }
10975
Romain Guy171c5922011-01-06 10:04:23 -080010976 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010977 * Destroys all hardware rendering resources. This method is invoked
10978 * when the system needs to reclaim resources. Upon execution of this
10979 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070010980 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010981 * Note: you <strong>must</strong> call
10982 * <code>super.destroyHardwareResources()</code> when overriding
10983 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070010984 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010985 * @hide
10986 */
10987 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070010988 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010989 }
10990
10991 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010992 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10993 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10994 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10995 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10996 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10997 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010998 *
Romain Guy171c5922011-01-06 10:04:23 -080010999 * <p>Enabling the drawing cache is similar to
11000 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011001 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11002 * drawing cache has no effect on rendering because the system uses a different mechanism
11003 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11004 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11005 * for information on how to enable software and hardware layers.</p>
11006 *
11007 * <p>This API can be used to manually generate
11008 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11009 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 *
11011 * @param enabled true to enable the drawing cache, false otherwise
11012 *
11013 * @see #isDrawingCacheEnabled()
11014 * @see #getDrawingCache()
11015 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011016 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011017 */
11018 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011019 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11021 }
11022
11023 /**
11024 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11025 *
11026 * @return true if the drawing cache is enabled
11027 *
11028 * @see #setDrawingCacheEnabled(boolean)
11029 * @see #getDrawingCache()
11030 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011031 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011032 public boolean isDrawingCacheEnabled() {
11033 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11034 }
11035
11036 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011037 * Debugging utility which recursively outputs the dirty state of a view and its
11038 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011039 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011040 * @hide
11041 */
Romain Guy676b1732011-02-14 14:45:33 -080011042 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011043 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11044 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11045 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11046 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11047 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11048 if (clear) {
11049 mPrivateFlags &= clearMask;
11050 }
11051 if (this instanceof ViewGroup) {
11052 ViewGroup parent = (ViewGroup) this;
11053 final int count = parent.getChildCount();
11054 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011055 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011056 child.outputDirtyFlags(indent + " ", clear, clearMask);
11057 }
11058 }
11059 }
11060
11061 /**
11062 * This method is used by ViewGroup to cause its children to restore or recreate their
11063 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11064 * to recreate its own display list, which would happen if it went through the normal
11065 * draw/dispatchDraw mechanisms.
11066 *
11067 * @hide
11068 */
11069 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011070
11071 /**
11072 * A view that is not attached or hardware accelerated cannot create a display list.
11073 * This method checks these conditions and returns the appropriate result.
11074 *
11075 * @return true if view has the ability to create a display list, false otherwise.
11076 *
11077 * @hide
11078 */
11079 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011080 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011081 }
Joe Malin32736f02011-01-19 16:14:20 -080011082
Chet Haasedaf98e92011-01-10 14:10:36 -080011083 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011084 * @return The HardwareRenderer associated with that view or null if hardware rendering
11085 * is not supported or this this has not been attached to a window.
11086 *
11087 * @hide
11088 */
11089 public HardwareRenderer getHardwareRenderer() {
11090 if (mAttachInfo != null) {
11091 return mAttachInfo.mHardwareRenderer;
11092 }
11093 return null;
11094 }
11095
11096 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011097 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11098 * Otherwise, the same display list will be returned (after having been rendered into
11099 * along the way, depending on the invalidation state of the view).
11100 *
11101 * @param displayList The previous version of this displayList, could be null.
11102 * @param isLayer Whether the requester of the display list is a layer. If so,
11103 * the view will avoid creating a layer inside the resulting display list.
11104 * @return A new or reused DisplayList object.
11105 */
11106 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11107 if (!canHaveDisplayList()) {
11108 return null;
11109 }
11110
11111 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11112 displayList == null || !displayList.isValid() ||
11113 (!isLayer && mRecreateDisplayList))) {
11114 // Don't need to recreate the display list, just need to tell our
11115 // children to restore/recreate theirs
11116 if (displayList != null && displayList.isValid() &&
11117 !isLayer && !mRecreateDisplayList) {
11118 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11119 mPrivateFlags &= ~DIRTY_MASK;
11120 dispatchGetDisplayList();
11121
11122 return displayList;
11123 }
11124
11125 if (!isLayer) {
11126 // If we got here, we're recreating it. Mark it as such to ensure that
11127 // we copy in child display lists into ours in drawChild()
11128 mRecreateDisplayList = true;
11129 }
11130 if (displayList == null) {
11131 final String name = getClass().getSimpleName();
11132 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11133 // If we're creating a new display list, make sure our parent gets invalidated
11134 // since they will need to recreate their display list to account for this
11135 // new child display list.
11136 invalidateParentCaches();
11137 }
11138
11139 boolean caching = false;
11140 final HardwareCanvas canvas = displayList.start();
11141 int restoreCount = 0;
11142 int width = mRight - mLeft;
11143 int height = mBottom - mTop;
11144
11145 try {
11146 canvas.setViewport(width, height);
11147 // The dirty rect should always be null for a display list
11148 canvas.onPreDraw(null);
11149 int layerType = (
11150 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11151 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011152 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11153 if (layerType == LAYER_TYPE_HARDWARE) {
11154 final HardwareLayer layer = getHardwareLayer();
11155 if (layer != null && layer.isValid()) {
11156 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11157 } else {
11158 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11159 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11160 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11161 }
11162 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011163 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011164 buildDrawingCache(true);
11165 Bitmap cache = getDrawingCache(true);
11166 if (cache != null) {
11167 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11168 caching = true;
11169 }
Chet Haasea1cff502012-02-21 13:43:44 -080011170 }
Chet Haasea1cff502012-02-21 13:43:44 -080011171 } else {
11172
11173 computeScroll();
11174
11175 if (!USE_DISPLAY_LIST_PROPERTIES) {
11176 restoreCount = canvas.save();
11177 }
11178 canvas.translate(-mScrollX, -mScrollY);
11179 if (!isLayer) {
11180 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11181 mPrivateFlags &= ~DIRTY_MASK;
11182 }
11183
11184 // Fast path for layouts with no backgrounds
11185 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11186 dispatchDraw(canvas);
11187 } else {
11188 draw(canvas);
11189 }
11190 }
11191 } finally {
11192 if (USE_DISPLAY_LIST_PROPERTIES) {
11193 canvas.restoreToCount(restoreCount);
11194 }
11195 canvas.onPostDraw();
11196
11197 displayList.end();
11198 if (USE_DISPLAY_LIST_PROPERTIES) {
11199 displayList.setCaching(caching);
11200 }
11201 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11202 displayList.setLeftTopRightBottom(0, 0, width, height);
11203 } else {
11204 setDisplayListProperties(displayList);
11205 }
11206 }
11207 } else if (!isLayer) {
11208 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11209 mPrivateFlags &= ~DIRTY_MASK;
11210 }
11211
11212 return displayList;
11213 }
11214
11215 /**
11216 * Get the DisplayList for the HardwareLayer
11217 *
11218 * @param layer The HardwareLayer whose DisplayList we want
11219 * @return A DisplayList fopr the specified HardwareLayer
11220 */
11221 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11222 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11223 layer.setDisplayList(displayList);
11224 return displayList;
11225 }
11226
11227
11228 /**
Romain Guyb051e892010-09-28 19:09:36 -070011229 * <p>Returns a display list that can be used to draw this view again
11230 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011231 *
Romain Guyb051e892010-09-28 19:09:36 -070011232 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011233 *
11234 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011235 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011236 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011237 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011238 return mDisplayList;
11239 }
11240
11241 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011242 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011243 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011244 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011245 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011246 * @see #getDrawingCache(boolean)
11247 */
11248 public Bitmap getDrawingCache() {
11249 return getDrawingCache(false);
11250 }
11251
11252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011253 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11254 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11255 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11256 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11257 * request the drawing cache by calling this method and draw it on screen if the
11258 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011259 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011260 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11261 * this method will create a bitmap of the same size as this view. Because this bitmap
11262 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11263 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11264 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11265 * size than the view. This implies that your application must be able to handle this
11266 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011267 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011268 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11269 * the current density of the screen when the application is in compatibility
11270 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011272 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 * @see #setDrawingCacheEnabled(boolean)
11275 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011276 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 * @see #destroyDrawingCache()
11278 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011279 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011280 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11281 return null;
11282 }
11283 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011284 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011285 }
Romain Guy02890fd2010-08-06 17:58:44 -070011286 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011287 }
11288
11289 /**
11290 * <p>Frees the resources used by the drawing cache. If you call
11291 * {@link #buildDrawingCache()} manually without calling
11292 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11293 * should cleanup the cache with this method afterwards.</p>
11294 *
11295 * @see #setDrawingCacheEnabled(boolean)
11296 * @see #buildDrawingCache()
11297 * @see #getDrawingCache()
11298 */
11299 public void destroyDrawingCache() {
11300 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011301 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011302 mDrawingCache = null;
11303 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011304 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011305 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011306 mUnscaledDrawingCache = null;
11307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011308 }
11309
11310 /**
11311 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011312 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 * view will always be drawn on top of a solid color.
11314 *
11315 * @param color The background color to use for the drawing cache's bitmap
11316 *
11317 * @see #setDrawingCacheEnabled(boolean)
11318 * @see #buildDrawingCache()
11319 * @see #getDrawingCache()
11320 */
11321 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011322 if (color != mDrawingCacheBackgroundColor) {
11323 mDrawingCacheBackgroundColor = color;
11324 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011326 }
11327
11328 /**
11329 * @see #setDrawingCacheBackgroundColor(int)
11330 *
11331 * @return The background color to used for the drawing cache's bitmap
11332 */
11333 public int getDrawingCacheBackgroundColor() {
11334 return mDrawingCacheBackgroundColor;
11335 }
11336
11337 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011338 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011339 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011340 * @see #buildDrawingCache(boolean)
11341 */
11342 public void buildDrawingCache() {
11343 buildDrawingCache(false);
11344 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011345
Romain Guyfbd8f692009-06-26 14:51:58 -070011346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011347 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11348 *
11349 * <p>If you call {@link #buildDrawingCache()} manually without calling
11350 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11351 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011352 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011353 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11354 * this method will create a bitmap of the same size as this view. Because this bitmap
11355 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11356 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11357 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11358 * size than the view. This implies that your application must be able to handle this
11359 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011360 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011361 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11362 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011363 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011364 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 *
11366 * @see #getDrawingCache()
11367 * @see #destroyDrawingCache()
11368 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011369 public void buildDrawingCache(boolean autoScale) {
11370 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011371 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011372 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011373
11374 if (ViewDebug.TRACE_HIERARCHY) {
11375 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377
Romain Guy8506ab42009-06-11 17:35:47 -070011378 int width = mRight - mLeft;
11379 int height = mBottom - mTop;
11380
11381 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011382 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011383
Romain Guye1123222009-06-29 14:24:56 -070011384 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011385 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11386 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388
11389 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011390 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011391 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011392
11393 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011394 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011395 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11397 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011398 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011399 return;
11400 }
11401
11402 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011403 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404
11405 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011406 Bitmap.Config quality;
11407 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011408 // Never pick ARGB_4444 because it looks awful
11409 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11411 case DRAWING_CACHE_QUALITY_AUTO:
11412 quality = Bitmap.Config.ARGB_8888;
11413 break;
11414 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011415 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416 break;
11417 case DRAWING_CACHE_QUALITY_HIGH:
11418 quality = Bitmap.Config.ARGB_8888;
11419 break;
11420 default:
11421 quality = Bitmap.Config.ARGB_8888;
11422 break;
11423 }
11424 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011425 // Optimization for translucent windows
11426 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011427 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428 }
11429
11430 // Try to cleanup memory
11431 if (bitmap != null) bitmap.recycle();
11432
11433 try {
11434 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011435 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011436 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011437 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011438 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011439 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011440 }
Adam Powell26153a32010-11-08 15:22:27 -080011441 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011442 } catch (OutOfMemoryError e) {
11443 // If there is not enough memory to create the bitmap cache, just
11444 // ignore the issue as bitmap caches are not required to draw the
11445 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011446 if (autoScale) {
11447 mDrawingCache = null;
11448 } else {
11449 mUnscaledDrawingCache = null;
11450 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011451 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011452 return;
11453 }
11454
11455 clear = drawingCacheBackgroundColor != 0;
11456 }
11457
11458 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 if (attachInfo != null) {
11460 canvas = attachInfo.mCanvas;
11461 if (canvas == null) {
11462 canvas = new Canvas();
11463 }
11464 canvas.setBitmap(bitmap);
11465 // Temporarily clobber the cached Canvas in case one of our children
11466 // is also using a drawing cache. Without this, the children would
11467 // steal the canvas by attaching their own bitmap to it and bad, bad
11468 // thing would happen (invisible views, corrupted drawings, etc.)
11469 attachInfo.mCanvas = null;
11470 } else {
11471 // This case should hopefully never or seldom happen
11472 canvas = new Canvas(bitmap);
11473 }
11474
11475 if (clear) {
11476 bitmap.eraseColor(drawingCacheBackgroundColor);
11477 }
11478
11479 computeScroll();
11480 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011481
Romain Guye1123222009-06-29 14:24:56 -070011482 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011483 final float scale = attachInfo.mApplicationScale;
11484 canvas.scale(scale, scale);
11485 }
Joe Malin32736f02011-01-19 16:14:20 -080011486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011487 canvas.translate(-mScrollX, -mScrollY);
11488
Romain Guy5bcdff42009-05-14 21:27:18 -070011489 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011490 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11491 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011492 mPrivateFlags |= DRAWING_CACHE_VALID;
11493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011494
11495 // Fast path for layouts with no backgrounds
11496 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11497 if (ViewDebug.TRACE_HIERARCHY) {
11498 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11499 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011500 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011501 dispatchDraw(canvas);
11502 } else {
11503 draw(canvas);
11504 }
11505
11506 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011507 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508
11509 if (attachInfo != null) {
11510 // Restore the cached Canvas for our siblings
11511 attachInfo.mCanvas = canvas;
11512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 }
11514 }
11515
11516 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011517 * Create a snapshot of the view into a bitmap. We should probably make
11518 * some form of this public, but should think about the API.
11519 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011520 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011521 int width = mRight - mLeft;
11522 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011523
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011524 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011525 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011526 width = (int) ((width * scale) + 0.5f);
11527 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011528
Romain Guy8c11e312009-09-14 15:15:30 -070011529 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011530 if (bitmap == null) {
11531 throw new OutOfMemoryError();
11532 }
11533
Romain Guyc529d8d2011-09-06 15:01:39 -070011534 Resources resources = getResources();
11535 if (resources != null) {
11536 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11537 }
Joe Malin32736f02011-01-19 16:14:20 -080011538
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011539 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011540 if (attachInfo != null) {
11541 canvas = attachInfo.mCanvas;
11542 if (canvas == null) {
11543 canvas = new Canvas();
11544 }
11545 canvas.setBitmap(bitmap);
11546 // Temporarily clobber the cached Canvas in case one of our children
11547 // is also using a drawing cache. Without this, the children would
11548 // steal the canvas by attaching their own bitmap to it and bad, bad
11549 // things would happen (invisible views, corrupted drawings, etc.)
11550 attachInfo.mCanvas = null;
11551 } else {
11552 // This case should hopefully never or seldom happen
11553 canvas = new Canvas(bitmap);
11554 }
11555
Romain Guy5bcdff42009-05-14 21:27:18 -070011556 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011557 bitmap.eraseColor(backgroundColor);
11558 }
11559
11560 computeScroll();
11561 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011562 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011563 canvas.translate(-mScrollX, -mScrollY);
11564
Romain Guy5bcdff42009-05-14 21:27:18 -070011565 // Temporarily remove the dirty mask
11566 int flags = mPrivateFlags;
11567 mPrivateFlags &= ~DIRTY_MASK;
11568
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011569 // Fast path for layouts with no backgrounds
11570 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11571 dispatchDraw(canvas);
11572 } else {
11573 draw(canvas);
11574 }
11575
Romain Guy5bcdff42009-05-14 21:27:18 -070011576 mPrivateFlags = flags;
11577
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011578 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011579 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011580
11581 if (attachInfo != null) {
11582 // Restore the cached Canvas for our siblings
11583 attachInfo.mCanvas = canvas;
11584 }
Romain Guy8506ab42009-06-11 17:35:47 -070011585
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011586 return bitmap;
11587 }
11588
11589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011590 * Indicates whether this View is currently in edit mode. A View is usually
11591 * in edit mode when displayed within a developer tool. For instance, if
11592 * this View is being drawn by a visual user interface builder, this method
11593 * should return true.
11594 *
11595 * Subclasses should check the return value of this method to provide
11596 * different behaviors if their normal behavior might interfere with the
11597 * host environment. For instance: the class spawns a thread in its
11598 * constructor, the drawing code relies on device-specific features, etc.
11599 *
11600 * This method is usually checked in the drawing code of custom widgets.
11601 *
11602 * @return True if this View is in edit mode, false otherwise.
11603 */
11604 public boolean isInEditMode() {
11605 return false;
11606 }
11607
11608 /**
11609 * If the View draws content inside its padding and enables fading edges,
11610 * it needs to support padding offsets. Padding offsets are added to the
11611 * fading edges to extend the length of the fade so that it covers pixels
11612 * drawn inside the padding.
11613 *
11614 * Subclasses of this class should override this method if they need
11615 * to draw content inside the padding.
11616 *
11617 * @return True if padding offset must be applied, false otherwise.
11618 *
11619 * @see #getLeftPaddingOffset()
11620 * @see #getRightPaddingOffset()
11621 * @see #getTopPaddingOffset()
11622 * @see #getBottomPaddingOffset()
11623 *
11624 * @since CURRENT
11625 */
11626 protected boolean isPaddingOffsetRequired() {
11627 return false;
11628 }
11629
11630 /**
11631 * Amount by which to extend the left fading region. Called only when
11632 * {@link #isPaddingOffsetRequired()} returns true.
11633 *
11634 * @return The left padding offset in pixels.
11635 *
11636 * @see #isPaddingOffsetRequired()
11637 *
11638 * @since CURRENT
11639 */
11640 protected int getLeftPaddingOffset() {
11641 return 0;
11642 }
11643
11644 /**
11645 * Amount by which to extend the right fading region. Called only when
11646 * {@link #isPaddingOffsetRequired()} returns true.
11647 *
11648 * @return The right padding offset in pixels.
11649 *
11650 * @see #isPaddingOffsetRequired()
11651 *
11652 * @since CURRENT
11653 */
11654 protected int getRightPaddingOffset() {
11655 return 0;
11656 }
11657
11658 /**
11659 * Amount by which to extend the top fading region. Called only when
11660 * {@link #isPaddingOffsetRequired()} returns true.
11661 *
11662 * @return The top padding offset in pixels.
11663 *
11664 * @see #isPaddingOffsetRequired()
11665 *
11666 * @since CURRENT
11667 */
11668 protected int getTopPaddingOffset() {
11669 return 0;
11670 }
11671
11672 /**
11673 * Amount by which to extend the bottom fading region. Called only when
11674 * {@link #isPaddingOffsetRequired()} returns true.
11675 *
11676 * @return The bottom padding offset in pixels.
11677 *
11678 * @see #isPaddingOffsetRequired()
11679 *
11680 * @since CURRENT
11681 */
11682 protected int getBottomPaddingOffset() {
11683 return 0;
11684 }
11685
11686 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011687 * @hide
11688 * @param offsetRequired
11689 */
11690 protected int getFadeTop(boolean offsetRequired) {
11691 int top = mPaddingTop;
11692 if (offsetRequired) top += getTopPaddingOffset();
11693 return top;
11694 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011695
Romain Guyf2fc4602011-07-19 15:20:03 -070011696 /**
11697 * @hide
11698 * @param offsetRequired
11699 */
11700 protected int getFadeHeight(boolean offsetRequired) {
11701 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070011702 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070011703 return mBottom - mTop - mPaddingBottom - padding;
11704 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011705
Romain Guyf2fc4602011-07-19 15:20:03 -070011706 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011707 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011708 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011709 *
Romain Guy2bffd262010-09-12 17:40:02 -070011710 * <p>Even if this method returns true, it does not mean that every call
11711 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11712 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011713 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011714 * window is hardware accelerated,
11715 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11716 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011717 *
Romain Guy2bffd262010-09-12 17:40:02 -070011718 * @return True if the view is attached to a window and the window is
11719 * hardware accelerated; false in any other case.
11720 */
11721 public boolean isHardwareAccelerated() {
11722 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11723 }
Joe Malin32736f02011-01-19 16:14:20 -080011724
Romain Guy2bffd262010-09-12 17:40:02 -070011725 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011726 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11727 * case of an active Animation being run on the view.
11728 */
11729 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11730 Animation a, boolean scalingRequired) {
11731 Transformation invalidationTransform;
11732 final int flags = parent.mGroupFlags;
11733 final boolean initialized = a.isInitialized();
11734 if (!initialized) {
11735 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11736 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11737 onAnimationStart();
11738 }
11739
11740 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11741 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11742 if (parent.mInvalidationTransformation == null) {
11743 parent.mInvalidationTransformation = new Transformation();
11744 }
11745 invalidationTransform = parent.mInvalidationTransformation;
11746 a.getTransformation(drawingTime, invalidationTransform, 1f);
11747 } else {
11748 invalidationTransform = parent.mChildTransformation;
11749 }
11750 if (more) {
11751 if (!a.willChangeBounds()) {
11752 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11753 parent.FLAG_OPTIMIZE_INVALIDATE) {
11754 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11755 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11756 // The child need to draw an animation, potentially offscreen, so
11757 // make sure we do not cancel invalidate requests
11758 parent.mPrivateFlags |= DRAW_ANIMATION;
11759 parent.invalidate(mLeft, mTop, mRight, mBottom);
11760 }
11761 } else {
11762 if (parent.mInvalidateRegion == null) {
11763 parent.mInvalidateRegion = new RectF();
11764 }
11765 final RectF region = parent.mInvalidateRegion;
11766 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11767 invalidationTransform);
11768
11769 // The child need to draw an animation, potentially offscreen, so
11770 // make sure we do not cancel invalidate requests
11771 parent.mPrivateFlags |= DRAW_ANIMATION;
11772
11773 final int left = mLeft + (int) region.left;
11774 final int top = mTop + (int) region.top;
11775 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11776 top + (int) (region.height() + .5f));
11777 }
11778 }
11779 return more;
11780 }
11781
Chet Haasea1cff502012-02-21 13:43:44 -080011782 void setDisplayListProperties() {
11783 setDisplayListProperties(mDisplayList);
11784 }
11785
11786 /**
11787 * This method is called by getDisplayList() when a display list is created or re-rendered.
11788 * It sets or resets the current value of all properties on that display list (resetting is
11789 * necessary when a display list is being re-created, because we need to make sure that
11790 * previously-set transform values
11791 */
11792 void setDisplayListProperties(DisplayList displayList) {
11793 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11794 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070011795 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080011796 if (mParent instanceof ViewGroup) {
11797 displayList.setClipChildren(
11798 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11799 }
Chet Haase9420abd2012-03-29 16:28:32 -070011800 float alpha = 1;
11801 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
11802 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
11803 ViewGroup parentVG = (ViewGroup) mParent;
11804 final boolean hasTransform =
11805 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
11806 if (hasTransform) {
11807 Transformation transform = parentVG.mChildTransformation;
11808 final int transformType = parentVG.mChildTransformation.getTransformationType();
11809 if (transformType != Transformation.TYPE_IDENTITY) {
11810 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
11811 alpha = transform.getAlpha();
11812 }
11813 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
11814 displayList.setStaticMatrix(transform.getMatrix());
11815 }
11816 }
11817 }
Chet Haasea1cff502012-02-21 13:43:44 -080011818 }
11819 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070011820 alpha *= mTransformationInfo.mAlpha;
11821 if (alpha < 1) {
11822 final int multipliedAlpha = (int) (255 * alpha);
11823 if (onSetAlpha(multipliedAlpha)) {
11824 alpha = 1;
11825 }
11826 }
11827 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080011828 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11829 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11830 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11831 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070011832 if (mTransformationInfo.mCamera == null) {
11833 mTransformationInfo.mCamera = new Camera();
11834 mTransformationInfo.matrix3D = new Matrix();
11835 }
11836 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080011837 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11838 displayList.setPivotX(getPivotX());
11839 displayList.setPivotY(getPivotY());
11840 }
Chet Haase9420abd2012-03-29 16:28:32 -070011841 } else if (alpha < 1) {
11842 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080011843 }
11844 }
11845 }
11846
Chet Haasebcca79a2012-02-14 08:45:14 -080011847 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011848 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11849 * This draw() method is an implementation detail and is not intended to be overridden or
11850 * to be called from anywhere else other than ViewGroup.drawChild().
11851 */
11852 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011853 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11854 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011855 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011856 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011857 final int flags = parent.mGroupFlags;
11858
Chet Haasea1cff502012-02-21 13:43:44 -080011859 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011860 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011861 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011862 }
11863
11864 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011865 boolean concatMatrix = false;
11866
11867 boolean scalingRequired = false;
11868 boolean caching;
11869 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11870
11871 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011872 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11873 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011874 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070011875 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080011876 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11877 } else {
11878 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11879 }
11880
Chet Haasebcca79a2012-02-14 08:45:14 -080011881 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011882 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011883 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011884 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011885 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070011886 } else if (!useDisplayListProperties &&
11887 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011888 final boolean hasTransform =
11889 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011890 if (hasTransform) {
11891 final int transformType = parent.mChildTransformation.getTransformationType();
11892 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11893 parent.mChildTransformation : null;
11894 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11895 }
11896 }
11897
11898 concatMatrix |= !childHasIdentityMatrix;
11899
11900 // Sets the flag as early as possible to allow draw() implementations
11901 // to call invalidate() successfully when doing animations
11902 mPrivateFlags |= DRAWN;
11903
Chet Haasebcca79a2012-02-14 08:45:14 -080011904 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011905 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11906 return more;
11907 }
11908
11909 if (hardwareAccelerated) {
11910 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11911 // retain the flag's value temporarily in the mRecreateDisplayList flag
11912 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11913 mPrivateFlags &= ~INVALIDATED;
11914 }
11915
11916 computeScroll();
11917
11918 final int sx = mScrollX;
11919 final int sy = mScrollY;
11920
11921 DisplayList displayList = null;
11922 Bitmap cache = null;
11923 boolean hasDisplayList = false;
11924 if (caching) {
11925 if (!hardwareAccelerated) {
11926 if (layerType != LAYER_TYPE_NONE) {
11927 layerType = LAYER_TYPE_SOFTWARE;
11928 buildDrawingCache(true);
11929 }
11930 cache = getDrawingCache(true);
11931 } else {
11932 switch (layerType) {
11933 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070011934 if (useDisplayListProperties) {
11935 hasDisplayList = canHaveDisplayList();
11936 } else {
11937 buildDrawingCache(true);
11938 cache = getDrawingCache(true);
11939 }
Chet Haase64a48c12012-02-13 16:33:29 -080011940 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011941 case LAYER_TYPE_HARDWARE:
11942 if (useDisplayListProperties) {
11943 hasDisplayList = canHaveDisplayList();
11944 }
11945 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011946 case LAYER_TYPE_NONE:
11947 // Delay getting the display list until animation-driven alpha values are
11948 // set up and possibly passed on to the view
11949 hasDisplayList = canHaveDisplayList();
11950 break;
11951 }
11952 }
11953 }
Chet Haasea1cff502012-02-21 13:43:44 -080011954 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070011955 if (useDisplayListProperties) {
11956 displayList = getDisplayList();
11957 if (!displayList.isValid()) {
11958 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11959 // to getDisplayList(), the display list will be marked invalid and we should not
11960 // try to use it again.
11961 displayList = null;
11962 hasDisplayList = false;
11963 useDisplayListProperties = false;
11964 }
11965 }
Chet Haase64a48c12012-02-13 16:33:29 -080011966
11967 final boolean hasNoCache = cache == null || hasDisplayList;
11968 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11969 layerType != LAYER_TYPE_HARDWARE;
11970
Chet Haasea1cff502012-02-21 13:43:44 -080011971 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011972 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011973 restoreTo = canvas.save();
11974 }
Chet Haase64a48c12012-02-13 16:33:29 -080011975 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011976 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011977 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011978 if (!useDisplayListProperties) {
11979 canvas.translate(mLeft, mTop);
11980 }
Chet Haase64a48c12012-02-13 16:33:29 -080011981 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011982 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070011983 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080011984 restoreTo = canvas.save();
11985 }
Chet Haase64a48c12012-02-13 16:33:29 -080011986 // mAttachInfo cannot be null, otherwise scalingRequired == false
11987 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11988 canvas.scale(scale, scale);
11989 }
11990 }
11991
Chet Haasea1cff502012-02-21 13:43:44 -080011992 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011993 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080011994 if (transformToApply != null || !childHasIdentityMatrix) {
11995 int transX = 0;
11996 int transY = 0;
11997
11998 if (offsetForScroll) {
11999 transX = -sx;
12000 transY = -sy;
12001 }
12002
12003 if (transformToApply != null) {
12004 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012005 if (useDisplayListProperties) {
12006 displayList.setAnimationMatrix(transformToApply.getMatrix());
12007 } else {
12008 // Undo the scroll translation, apply the transformation matrix,
12009 // then redo the scroll translate to get the correct result.
12010 canvas.translate(-transX, -transY);
12011 canvas.concat(transformToApply.getMatrix());
12012 canvas.translate(transX, transY);
12013 }
Chet Haasea1cff502012-02-21 13:43:44 -080012014 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012015 }
12016
12017 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012018 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012019 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012020 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012021 }
12022 }
12023
Chet Haasea1cff502012-02-21 13:43:44 -080012024 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012025 canvas.translate(-transX, -transY);
12026 canvas.concat(getMatrix());
12027 canvas.translate(transX, transY);
12028 }
12029 }
12030
Chet Haase9420abd2012-03-29 16:28:32 -070012031 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012032 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012033 if (hasNoCache) {
12034 final int multipliedAlpha = (int) (255 * alpha);
12035 if (!onSetAlpha(multipliedAlpha)) {
12036 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012037 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012038 layerType != LAYER_TYPE_NONE) {
12039 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12040 }
Chet Haase9420abd2012-03-29 16:28:32 -070012041 if (useDisplayListProperties) {
12042 displayList.setAlpha(alpha * getAlpha());
12043 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012044 final int scrollX = hasDisplayList ? 0 : sx;
12045 final int scrollY = hasDisplayList ? 0 : sy;
12046 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12047 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012048 }
12049 } else {
12050 // Alpha is handled by the child directly, clobber the layer's alpha
12051 mPrivateFlags |= ALPHA_SET;
12052 }
12053 }
12054 }
12055 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12056 onSetAlpha(255);
12057 mPrivateFlags &= ~ALPHA_SET;
12058 }
12059
Chet Haasea1cff502012-02-21 13:43:44 -080012060 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12061 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012062 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012063 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012064 } else {
12065 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012066 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012067 } else {
12068 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12069 }
12070 }
12071 }
12072
Chet Haase9420abd2012-03-29 16:28:32 -070012073 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012074 displayList = getDisplayList();
12075 if (!displayList.isValid()) {
12076 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12077 // to getDisplayList(), the display list will be marked invalid and we should not
12078 // try to use it again.
12079 displayList = null;
12080 hasDisplayList = false;
12081 }
12082 }
12083
12084 if (hasNoCache) {
12085 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012086 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012087 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012088 if (layer != null && layer.isValid()) {
12089 mLayerPaint.setAlpha((int) (alpha * 255));
12090 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12091 layerRendered = true;
12092 } else {
12093 final int scrollX = hasDisplayList ? 0 : sx;
12094 final int scrollY = hasDisplayList ? 0 : sy;
12095 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012096 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012097 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12098 }
12099 }
12100
12101 if (!layerRendered) {
12102 if (!hasDisplayList) {
12103 // Fast path for layouts with no backgrounds
12104 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12105 if (ViewDebug.TRACE_HIERARCHY) {
12106 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12107 }
12108 mPrivateFlags &= ~DIRTY_MASK;
12109 dispatchDraw(canvas);
12110 } else {
12111 draw(canvas);
12112 }
12113 } else {
12114 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012115 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012116 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012117 }
12118 }
12119 } else if (cache != null) {
12120 mPrivateFlags &= ~DIRTY_MASK;
12121 Paint cachePaint;
12122
12123 if (layerType == LAYER_TYPE_NONE) {
12124 cachePaint = parent.mCachePaint;
12125 if (cachePaint == null) {
12126 cachePaint = new Paint();
12127 cachePaint.setDither(false);
12128 parent.mCachePaint = cachePaint;
12129 }
Chet Haase9420abd2012-03-29 16:28:32 -070012130 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012131 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012132 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12133 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012134 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012135 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012136 }
12137 } else {
12138 cachePaint = mLayerPaint;
12139 cachePaint.setAlpha((int) (alpha * 255));
12140 }
12141 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12142 }
12143
Chet Haasea1cff502012-02-21 13:43:44 -080012144 if (restoreTo >= 0) {
12145 canvas.restoreToCount(restoreTo);
12146 }
Chet Haase64a48c12012-02-13 16:33:29 -080012147
12148 if (a != null && !more) {
12149 if (!hardwareAccelerated && !a.getFillAfter()) {
12150 onSetAlpha(255);
12151 }
12152 parent.finishAnimatingView(this, a);
12153 }
12154
12155 if (more && hardwareAccelerated) {
12156 // invalidation is the trigger to recreate display lists, so if we're using
12157 // display lists to render, force an invalidate to allow the animation to
12158 // continue drawing another frame
12159 parent.invalidate(true);
12160 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12161 // alpha animations should cause the child to recreate its display list
12162 invalidate(true);
12163 }
12164 }
12165
12166 mRecreateDisplayList = false;
12167
12168 return more;
12169 }
12170
12171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 * Manually render this view (and all of its children) to the given Canvas.
12173 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012174 * called. When implementing a view, implement
12175 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12176 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177 *
12178 * @param canvas The Canvas to which the View is rendered.
12179 */
12180 public void draw(Canvas canvas) {
12181 if (ViewDebug.TRACE_HIERARCHY) {
12182 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12183 }
12184
Romain Guy5bcdff42009-05-14 21:27:18 -070012185 final int privateFlags = mPrivateFlags;
12186 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12187 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12188 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 /*
12191 * Draw traversal performs several drawing steps which must be executed
12192 * in the appropriate order:
12193 *
12194 * 1. Draw the background
12195 * 2. If necessary, save the canvas' layers to prepare for fading
12196 * 3. Draw view's content
12197 * 4. Draw children
12198 * 5. If necessary, draw the fading edges and restore layers
12199 * 6. Draw decorations (scrollbars for instance)
12200 */
12201
12202 // Step 1, draw the background, if needed
12203 int saveCount;
12204
Romain Guy24443ea2009-05-11 11:56:30 -070012205 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012206 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012207 if (background != null) {
12208 final int scrollX = mScrollX;
12209 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210
Romain Guy24443ea2009-05-11 11:56:30 -070012211 if (mBackgroundSizeChanged) {
12212 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12213 mBackgroundSizeChanged = false;
12214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215
Romain Guy24443ea2009-05-11 11:56:30 -070012216 if ((scrollX | scrollY) == 0) {
12217 background.draw(canvas);
12218 } else {
12219 canvas.translate(scrollX, scrollY);
12220 background.draw(canvas);
12221 canvas.translate(-scrollX, -scrollY);
12222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012223 }
12224 }
12225
12226 // skip step 2 & 5 if possible (common case)
12227 final int viewFlags = mViewFlags;
12228 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12229 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12230 if (!verticalEdges && !horizontalEdges) {
12231 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012232 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012233
12234 // Step 4, draw the children
12235 dispatchDraw(canvas);
12236
12237 // Step 6, draw decorations (scrollbars)
12238 onDrawScrollBars(canvas);
12239
12240 // we're done...
12241 return;
12242 }
12243
12244 /*
12245 * Here we do the full fledged routine...
12246 * (this is an uncommon case where speed matters less,
12247 * this is why we repeat some of the tests that have been
12248 * done above)
12249 */
12250
12251 boolean drawTop = false;
12252 boolean drawBottom = false;
12253 boolean drawLeft = false;
12254 boolean drawRight = false;
12255
12256 float topFadeStrength = 0.0f;
12257 float bottomFadeStrength = 0.0f;
12258 float leftFadeStrength = 0.0f;
12259 float rightFadeStrength = 0.0f;
12260
12261 // Step 2, save the canvas' layers
12262 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012263
12264 final boolean offsetRequired = isPaddingOffsetRequired();
12265 if (offsetRequired) {
12266 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012267 }
12268
12269 int left = mScrollX + paddingLeft;
12270 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012271 int top = mScrollY + getFadeTop(offsetRequired);
12272 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273
12274 if (offsetRequired) {
12275 right += getRightPaddingOffset();
12276 bottom += getBottomPaddingOffset();
12277 }
12278
12279 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012280 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012281 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012282
12283 // clip the fade length if top and bottom fades overlap
12284 // overlapping fades produce odd-looking artifacts
12285 if (verticalEdges && (top + length > bottom - length)) {
12286 length = (bottom - top) / 2;
12287 }
12288
12289 // also clip horizontal fades if necessary
12290 if (horizontalEdges && (left + length > right - length)) {
12291 length = (right - left) / 2;
12292 }
12293
12294 if (verticalEdges) {
12295 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012296 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012297 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012298 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012299 }
12300
12301 if (horizontalEdges) {
12302 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012303 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012305 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012306 }
12307
12308 saveCount = canvas.getSaveCount();
12309
12310 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012311 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012312 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12313
12314 if (drawTop) {
12315 canvas.saveLayer(left, top, right, top + length, null, flags);
12316 }
12317
12318 if (drawBottom) {
12319 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12320 }
12321
12322 if (drawLeft) {
12323 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12324 }
12325
12326 if (drawRight) {
12327 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12328 }
12329 } else {
12330 scrollabilityCache.setFadeColor(solidColor);
12331 }
12332
12333 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012334 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012335
12336 // Step 4, draw the children
12337 dispatchDraw(canvas);
12338
12339 // Step 5, draw the fade effect and restore layers
12340 final Paint p = scrollabilityCache.paint;
12341 final Matrix matrix = scrollabilityCache.matrix;
12342 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012343
12344 if (drawTop) {
12345 matrix.setScale(1, fadeHeight * topFadeStrength);
12346 matrix.postTranslate(left, top);
12347 fade.setLocalMatrix(matrix);
12348 canvas.drawRect(left, top, right, top + length, p);
12349 }
12350
12351 if (drawBottom) {
12352 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12353 matrix.postRotate(180);
12354 matrix.postTranslate(left, bottom);
12355 fade.setLocalMatrix(matrix);
12356 canvas.drawRect(left, bottom - length, right, bottom, p);
12357 }
12358
12359 if (drawLeft) {
12360 matrix.setScale(1, fadeHeight * leftFadeStrength);
12361 matrix.postRotate(-90);
12362 matrix.postTranslate(left, top);
12363 fade.setLocalMatrix(matrix);
12364 canvas.drawRect(left, top, left + length, bottom, p);
12365 }
12366
12367 if (drawRight) {
12368 matrix.setScale(1, fadeHeight * rightFadeStrength);
12369 matrix.postRotate(90);
12370 matrix.postTranslate(right, top);
12371 fade.setLocalMatrix(matrix);
12372 canvas.drawRect(right - length, top, right, bottom, p);
12373 }
12374
12375 canvas.restoreToCount(saveCount);
12376
12377 // Step 6, draw decorations (scrollbars)
12378 onDrawScrollBars(canvas);
12379 }
12380
12381 /**
12382 * Override this if your view is known to always be drawn on top of a solid color background,
12383 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12384 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12385 * should be set to 0xFF.
12386 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012387 * @see #setVerticalFadingEdgeEnabled(boolean)
12388 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 *
12390 * @return The known solid color background for this view, or 0 if the color may vary
12391 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012392 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012393 public int getSolidColor() {
12394 return 0;
12395 }
12396
12397 /**
12398 * Build a human readable string representation of the specified view flags.
12399 *
12400 * @param flags the view flags to convert to a string
12401 * @return a String representing the supplied flags
12402 */
12403 private static String printFlags(int flags) {
12404 String output = "";
12405 int numFlags = 0;
12406 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12407 output += "TAKES_FOCUS";
12408 numFlags++;
12409 }
12410
12411 switch (flags & VISIBILITY_MASK) {
12412 case INVISIBLE:
12413 if (numFlags > 0) {
12414 output += " ";
12415 }
12416 output += "INVISIBLE";
12417 // USELESS HERE numFlags++;
12418 break;
12419 case GONE:
12420 if (numFlags > 0) {
12421 output += " ";
12422 }
12423 output += "GONE";
12424 // USELESS HERE numFlags++;
12425 break;
12426 default:
12427 break;
12428 }
12429 return output;
12430 }
12431
12432 /**
12433 * Build a human readable string representation of the specified private
12434 * view flags.
12435 *
12436 * @param privateFlags the private view flags to convert to a string
12437 * @return a String representing the supplied flags
12438 */
12439 private static String printPrivateFlags(int privateFlags) {
12440 String output = "";
12441 int numFlags = 0;
12442
12443 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
12444 output += "WANTS_FOCUS";
12445 numFlags++;
12446 }
12447
12448 if ((privateFlags & FOCUSED) == FOCUSED) {
12449 if (numFlags > 0) {
12450 output += " ";
12451 }
12452 output += "FOCUSED";
12453 numFlags++;
12454 }
12455
12456 if ((privateFlags & SELECTED) == SELECTED) {
12457 if (numFlags > 0) {
12458 output += " ";
12459 }
12460 output += "SELECTED";
12461 numFlags++;
12462 }
12463
12464 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12465 if (numFlags > 0) {
12466 output += " ";
12467 }
12468 output += "IS_ROOT_NAMESPACE";
12469 numFlags++;
12470 }
12471
12472 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12473 if (numFlags > 0) {
12474 output += " ";
12475 }
12476 output += "HAS_BOUNDS";
12477 numFlags++;
12478 }
12479
12480 if ((privateFlags & DRAWN) == DRAWN) {
12481 if (numFlags > 0) {
12482 output += " ";
12483 }
12484 output += "DRAWN";
12485 // USELESS HERE numFlags++;
12486 }
12487 return output;
12488 }
12489
12490 /**
12491 * <p>Indicates whether or not this view's layout will be requested during
12492 * the next hierarchy layout pass.</p>
12493 *
12494 * @return true if the layout will be forced during next layout pass
12495 */
12496 public boolean isLayoutRequested() {
12497 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12498 }
12499
12500 /**
12501 * Assign a size and position to a view and all of its
12502 * descendants
12503 *
12504 * <p>This is the second phase of the layout mechanism.
12505 * (The first is measuring). In this phase, each parent calls
12506 * layout on all of its children to position them.
12507 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012508 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509 *
Chet Haase9c087442011-01-12 16:20:16 -080012510 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012511 * Derived classes with children should override
12512 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012513 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 *
12515 * @param l Left position, relative to parent
12516 * @param t Top position, relative to parent
12517 * @param r Right position, relative to parent
12518 * @param b Bottom position, relative to parent
12519 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012520 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012521 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012522 int oldL = mLeft;
12523 int oldT = mTop;
12524 int oldB = mBottom;
12525 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526 boolean changed = setFrame(l, t, r, b);
12527 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12528 if (ViewDebug.TRACE_HIERARCHY) {
12529 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12530 }
12531
12532 onLayout(changed, l, t, r, b);
12533 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012534
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012535 ListenerInfo li = mListenerInfo;
12536 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012537 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012538 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012539 int numListeners = listenersCopy.size();
12540 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012541 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012542 }
12543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 }
12545 mPrivateFlags &= ~FORCE_LAYOUT;
12546 }
12547
12548 /**
12549 * Called from layout when this view should
12550 * assign a size and position to each of its children.
12551 *
12552 * Derived classes with children should override
12553 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012554 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012555 * @param changed This is a new size or position for this view
12556 * @param left Left position, relative to parent
12557 * @param top Top position, relative to parent
12558 * @param right Right position, relative to parent
12559 * @param bottom Bottom position, relative to parent
12560 */
12561 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12562 }
12563
12564 /**
12565 * Assign a size and position to this view.
12566 *
12567 * This is called from layout.
12568 *
12569 * @param left Left position, relative to parent
12570 * @param top Top position, relative to parent
12571 * @param right Right position, relative to parent
12572 * @param bottom Bottom position, relative to parent
12573 * @return true if the new size and position are different than the
12574 * previous ones
12575 * {@hide}
12576 */
12577 protected boolean setFrame(int left, int top, int right, int bottom) {
12578 boolean changed = false;
12579
12580 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012581 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012582 + right + "," + bottom + ")");
12583 }
12584
12585 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12586 changed = true;
12587
12588 // Remember our drawn bit
12589 int drawn = mPrivateFlags & DRAWN;
12590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012591 int oldWidth = mRight - mLeft;
12592 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012593 int newWidth = right - left;
12594 int newHeight = bottom - top;
12595 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12596
12597 // Invalidate our old position
12598 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012599
12600 mLeft = left;
12601 mTop = top;
12602 mRight = right;
12603 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012604 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12605 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012607
12608 mPrivateFlags |= HAS_BOUNDS;
12609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610
Chet Haase75755e22011-07-18 17:48:25 -070012611 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012612 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12613 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012614 if (mTransformationInfo != null) {
12615 mTransformationInfo.mMatrixDirty = true;
12616 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012618 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12619 }
12620
12621 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12622 // If we are visible, force the DRAWN bit to on so that
12623 // this invalidate will go through (at least to our parent).
12624 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012625 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012626 // the DRAWN bit.
12627 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012628 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012629 // parent display list may need to be recreated based on a change in the bounds
12630 // of any child
12631 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012632 }
12633
12634 // Reset drawn bit to original value (invalidate turns it off)
12635 mPrivateFlags |= drawn;
12636
12637 mBackgroundSizeChanged = true;
12638 }
12639 return changed;
12640 }
12641
12642 /**
12643 * Finalize inflating a view from XML. This is called as the last phase
12644 * of inflation, after all child views have been added.
12645 *
12646 * <p>Even if the subclass overrides onFinishInflate, they should always be
12647 * sure to call the super method, so that we get called.
12648 */
12649 protected void onFinishInflate() {
12650 }
12651
12652 /**
12653 * Returns the resources associated with this view.
12654 *
12655 * @return Resources object.
12656 */
12657 public Resources getResources() {
12658 return mResources;
12659 }
12660
12661 /**
12662 * Invalidates the specified Drawable.
12663 *
12664 * @param drawable the drawable to invalidate
12665 */
12666 public void invalidateDrawable(Drawable drawable) {
12667 if (verifyDrawable(drawable)) {
12668 final Rect dirty = drawable.getBounds();
12669 final int scrollX = mScrollX;
12670 final int scrollY = mScrollY;
12671
12672 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12673 dirty.right + scrollX, dirty.bottom + scrollY);
12674 }
12675 }
12676
12677 /**
12678 * Schedules an action on a drawable to occur at a specified time.
12679 *
12680 * @param who the recipient of the action
12681 * @param what the action to run on the drawable
12682 * @param when the time at which the action must occur. Uses the
12683 * {@link SystemClock#uptimeMillis} timebase.
12684 */
12685 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012686 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012687 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012688 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012689 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12690 Choreographer.CALLBACK_ANIMATION, what, who,
12691 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012692 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012693 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012695 }
12696 }
12697
12698 /**
12699 * Cancels a scheduled action on a drawable.
12700 *
12701 * @param who the recipient of the action
12702 * @param what the action to cancel
12703 */
12704 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012705 if (verifyDrawable(who) && what != null) {
12706 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012707 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12708 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012709 } else {
12710 ViewRootImpl.getRunQueue().removeCallbacks(what);
12711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712 }
12713 }
12714
12715 /**
12716 * Unschedule any events associated with the given Drawable. This can be
12717 * used when selecting a new Drawable into a view, so that the previous
12718 * one is completely unscheduled.
12719 *
12720 * @param who The Drawable to unschedule.
12721 *
12722 * @see #drawableStateChanged
12723 */
12724 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012725 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012726 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12727 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012728 }
12729 }
12730
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012731 /**
12732 * Return the layout direction of a given Drawable.
12733 *
12734 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012735 */
12736 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012737 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012738 }
12739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012740 /**
12741 * If your view subclass is displaying its own Drawable objects, it should
12742 * override this function and return true for any Drawable it is
12743 * displaying. This allows animations for those drawables to be
12744 * scheduled.
12745 *
12746 * <p>Be sure to call through to the super class when overriding this
12747 * function.
12748 *
12749 * @param who The Drawable to verify. Return true if it is one you are
12750 * displaying, else return the result of calling through to the
12751 * super class.
12752 *
12753 * @return boolean If true than the Drawable is being displayed in the
12754 * view; else false and it is not allowed to animate.
12755 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012756 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12757 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012758 */
12759 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012760 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012761 }
12762
12763 /**
12764 * This function is called whenever the state of the view changes in such
12765 * a way that it impacts the state of drawables being shown.
12766 *
12767 * <p>Be sure to call through to the superclass when overriding this
12768 * function.
12769 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012770 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771 */
12772 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012773 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012774 if (d != null && d.isStateful()) {
12775 d.setState(getDrawableState());
12776 }
12777 }
12778
12779 /**
12780 * Call this to force a view to update its drawable state. This will cause
12781 * drawableStateChanged to be called on this view. Views that are interested
12782 * in the new state should call getDrawableState.
12783 *
12784 * @see #drawableStateChanged
12785 * @see #getDrawableState
12786 */
12787 public void refreshDrawableState() {
12788 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12789 drawableStateChanged();
12790
12791 ViewParent parent = mParent;
12792 if (parent != null) {
12793 parent.childDrawableStateChanged(this);
12794 }
12795 }
12796
12797 /**
12798 * Return an array of resource IDs of the drawable states representing the
12799 * current state of the view.
12800 *
12801 * @return The current drawable state
12802 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012803 * @see Drawable#setState(int[])
12804 * @see #drawableStateChanged()
12805 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012806 */
12807 public final int[] getDrawableState() {
12808 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12809 return mDrawableState;
12810 } else {
12811 mDrawableState = onCreateDrawableState(0);
12812 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12813 return mDrawableState;
12814 }
12815 }
12816
12817 /**
12818 * Generate the new {@link android.graphics.drawable.Drawable} state for
12819 * this view. This is called by the view
12820 * system when the cached Drawable state is determined to be invalid. To
12821 * retrieve the current state, you should use {@link #getDrawableState}.
12822 *
12823 * @param extraSpace if non-zero, this is the number of extra entries you
12824 * would like in the returned array in which you can place your own
12825 * states.
12826 *
12827 * @return Returns an array holding the current {@link Drawable} state of
12828 * the view.
12829 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012830 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012831 */
12832 protected int[] onCreateDrawableState(int extraSpace) {
12833 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12834 mParent instanceof View) {
12835 return ((View) mParent).onCreateDrawableState(extraSpace);
12836 }
12837
12838 int[] drawableState;
12839
12840 int privateFlags = mPrivateFlags;
12841
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012842 int viewStateIndex = 0;
12843 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12844 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12845 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012846 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012847 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12848 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012849 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12850 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012851 // This is set if HW acceleration is requested, even if the current
12852 // process doesn't allow it. This is just to allow app preview
12853 // windows to better match their app.
12854 viewStateIndex |= VIEW_STATE_ACCELERATED;
12855 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012856 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857
Christopher Tate3d4bf172011-03-28 16:16:46 -070012858 final int privateFlags2 = mPrivateFlags2;
12859 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12860 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012862 drawableState = VIEW_STATE_SETS[viewStateIndex];
12863
12864 //noinspection ConstantIfStatement
12865 if (false) {
12866 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12867 Log.i("View", toString()
12868 + " pressed=" + ((privateFlags & PRESSED) != 0)
12869 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12870 + " fo=" + hasFocus()
12871 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012872 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 + ": " + Arrays.toString(drawableState));
12874 }
12875
12876 if (extraSpace == 0) {
12877 return drawableState;
12878 }
12879
12880 final int[] fullState;
12881 if (drawableState != null) {
12882 fullState = new int[drawableState.length + extraSpace];
12883 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12884 } else {
12885 fullState = new int[extraSpace];
12886 }
12887
12888 return fullState;
12889 }
12890
12891 /**
12892 * Merge your own state values in <var>additionalState</var> into the base
12893 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012894 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012895 *
12896 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012897 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012898 * own additional state values.
12899 *
12900 * @param additionalState The additional state values you would like
12901 * added to <var>baseState</var>; this array is not modified.
12902 *
12903 * @return As a convenience, the <var>baseState</var> array you originally
12904 * passed into the function is returned.
12905 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012906 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012907 */
12908 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12909 final int N = baseState.length;
12910 int i = N - 1;
12911 while (i >= 0 && baseState[i] == 0) {
12912 i--;
12913 }
12914 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12915 return baseState;
12916 }
12917
12918 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012919 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12920 * on all Drawable objects associated with this view.
12921 */
12922 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012923 if (mBackground != null) {
12924 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070012925 }
12926 }
12927
12928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012929 * Sets the background color for this view.
12930 * @param color the color of the background
12931 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012932 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012933 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012934 if (mBackground instanceof ColorDrawable) {
12935 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070012936 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070012937 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070012938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012939 }
12940
12941 /**
12942 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012943 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012944 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070012945 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012946 * @attr ref android.R.styleable#View_background
12947 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012948 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012949 public void setBackgroundResource(int resid) {
12950 if (resid != 0 && resid == mBackgroundResource) {
12951 return;
12952 }
12953
12954 Drawable d= null;
12955 if (resid != 0) {
12956 d = mResources.getDrawable(resid);
12957 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012958 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012959
12960 mBackgroundResource = resid;
12961 }
12962
12963 /**
12964 * Set the background to a given Drawable, or remove the background. If the
12965 * background has padding, this View's padding is set to the background's
12966 * padding. However, when a background is removed, this View's padding isn't
12967 * touched. If setting the padding is desired, please use
12968 * {@link #setPadding(int, int, int, int)}.
12969 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012970 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012971 * background
12972 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012973 public void setBackground(Drawable background) {
12974 setBackgroundDrawable(background);
12975 }
12976
12977 /**
12978 * @deprecated use {@link #setBackground(Drawable)} instead
12979 */
12980 @Deprecated
12981 public void setBackgroundDrawable(Drawable background) {
12982 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012983 return;
12984 }
12985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012986 boolean requestLayout = false;
12987
12988 mBackgroundResource = 0;
12989
12990 /*
12991 * Regardless of whether we're setting a new background or not, we want
12992 * to clear the previous drawable.
12993 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012994 if (mBackground != null) {
12995 mBackground.setCallback(null);
12996 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012997 }
12998
Philip Milne6c8ea062012-04-03 17:38:43 -070012999 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013000 Rect padding = sThreadLocal.get();
13001 if (padding == null) {
13002 padding = new Rect();
13003 sThreadLocal.set(padding);
13004 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013005 if (background.getPadding(padding)) {
13006 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013007 case LAYOUT_DIRECTION_RTL:
13008 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13009 break;
13010 case LAYOUT_DIRECTION_LTR:
13011 default:
13012 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013014 }
13015
13016 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13017 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013018 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13019 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013020 requestLayout = true;
13021 }
13022
Philip Milne6c8ea062012-04-03 17:38:43 -070013023 background.setCallback(this);
13024 if (background.isStateful()) {
13025 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013026 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013027 background.setVisible(getVisibility() == VISIBLE, false);
13028 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013029
13030 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13031 mPrivateFlags &= ~SKIP_DRAW;
13032 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13033 requestLayout = true;
13034 }
13035 } else {
13036 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013037 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013038
13039 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13040 /*
13041 * This view ONLY drew the background before and we're removing
13042 * the background, so now it won't draw anything
13043 * (hence we SKIP_DRAW)
13044 */
13045 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13046 mPrivateFlags |= SKIP_DRAW;
13047 }
13048
13049 /*
13050 * When the background is set, we try to apply its padding to this
13051 * View. When the background is removed, we don't touch this View's
13052 * padding. This is noted in the Javadocs. Hence, we don't need to
13053 * requestLayout(), the invalidate() below is sufficient.
13054 */
13055
13056 // The old background's minimum size could have affected this
13057 // View's layout, so let's requestLayout
13058 requestLayout = true;
13059 }
13060
Romain Guy8f1344f52009-05-15 16:03:59 -070013061 computeOpaqueFlags();
13062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013063 if (requestLayout) {
13064 requestLayout();
13065 }
13066
13067 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013068 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013069 }
13070
13071 /**
13072 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013074 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013075 *
13076 * @see #setBackground(Drawable)
13077 *
13078 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013079 */
13080 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013081 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013082 }
13083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013084 /**
13085 * Sets the padding. The view may add on the space required to display
13086 * the scrollbars, depending on the style and visibility of the scrollbars.
13087 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13088 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13089 * from the values set in this call.
13090 *
13091 * @attr ref android.R.styleable#View_padding
13092 * @attr ref android.R.styleable#View_paddingBottom
13093 * @attr ref android.R.styleable#View_paddingLeft
13094 * @attr ref android.R.styleable#View_paddingRight
13095 * @attr ref android.R.styleable#View_paddingTop
13096 * @param left the left padding in pixels
13097 * @param top the top padding in pixels
13098 * @param right the right padding in pixels
13099 * @param bottom the bottom padding in pixels
13100 */
13101 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013102 mUserPaddingStart = -1;
13103 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013104 mUserPaddingRelative = false;
13105
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013106 internalSetPadding(left, top, right, bottom);
13107 }
13108
13109 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013110 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013111 mUserPaddingRight = right;
13112 mUserPaddingBottom = bottom;
13113
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013114 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013115 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013116
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013117 // Common case is there are no scroll bars.
13118 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013119 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013120 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013121 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013122 switch (mVerticalScrollbarPosition) {
13123 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013124 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13125 left += offset;
13126 } else {
13127 right += offset;
13128 }
13129 break;
Adam Powell20232d02010-12-08 21:08:53 -080013130 case SCROLLBAR_POSITION_RIGHT:
13131 right += offset;
13132 break;
13133 case SCROLLBAR_POSITION_LEFT:
13134 left += offset;
13135 break;
13136 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013137 }
Adam Powell20232d02010-12-08 21:08:53 -080013138 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013139 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13140 ? 0 : getHorizontalScrollbarHeight();
13141 }
13142 }
Romain Guy8506ab42009-06-11 17:35:47 -070013143
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013144 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013145 changed = true;
13146 mPaddingLeft = left;
13147 }
13148 if (mPaddingTop != top) {
13149 changed = true;
13150 mPaddingTop = top;
13151 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013152 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013153 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013154 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013155 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013156 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013157 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013158 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013159 }
13160
13161 if (changed) {
13162 requestLayout();
13163 }
13164 }
13165
13166 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013167 * Sets the relative padding. The view may add on the space required to display
13168 * the scrollbars, depending on the style and visibility of the scrollbars.
13169 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13170 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13171 * from the values set in this call.
13172 *
13173 * @attr ref android.R.styleable#View_padding
13174 * @attr ref android.R.styleable#View_paddingBottom
13175 * @attr ref android.R.styleable#View_paddingStart
13176 * @attr ref android.R.styleable#View_paddingEnd
13177 * @attr ref android.R.styleable#View_paddingTop
13178 * @param start the start padding in pixels
13179 * @param top the top padding in pixels
13180 * @param end the end padding in pixels
13181 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013182 */
13183 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013184 mUserPaddingStart = start;
13185 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013186 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013187
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013188 switch(getResolvedLayoutDirection()) {
13189 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013190 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013191 break;
13192 case LAYOUT_DIRECTION_LTR:
13193 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013194 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013195 }
13196 }
13197
13198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013199 * Returns the top padding of this view.
13200 *
13201 * @return the top padding in pixels
13202 */
13203 public int getPaddingTop() {
13204 return mPaddingTop;
13205 }
13206
13207 /**
13208 * Returns the bottom padding of this view. If there are inset and enabled
13209 * scrollbars, this value may include the space required to display the
13210 * scrollbars as well.
13211 *
13212 * @return the bottom padding in pixels
13213 */
13214 public int getPaddingBottom() {
13215 return mPaddingBottom;
13216 }
13217
13218 /**
13219 * Returns the left padding of this view. If there are inset and enabled
13220 * scrollbars, this value may include the space required to display the
13221 * scrollbars as well.
13222 *
13223 * @return the left padding in pixels
13224 */
13225 public int getPaddingLeft() {
13226 return mPaddingLeft;
13227 }
13228
13229 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013230 * Returns the start padding of this view depending on its resolved layout direction.
13231 * If there are inset and enabled scrollbars, this value may include the space
13232 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013233 *
13234 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013235 */
13236 public int getPaddingStart() {
13237 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13238 mPaddingRight : mPaddingLeft;
13239 }
13240
13241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013242 * Returns the right padding of this view. If there are inset and enabled
13243 * scrollbars, this value may include the space required to display the
13244 * scrollbars as well.
13245 *
13246 * @return the right padding in pixels
13247 */
13248 public int getPaddingRight() {
13249 return mPaddingRight;
13250 }
13251
13252 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013253 * Returns the end padding of this view depending on its resolved layout direction.
13254 * If there are inset and enabled scrollbars, this value may include the space
13255 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013256 *
13257 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013258 */
13259 public int getPaddingEnd() {
13260 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13261 mPaddingLeft : mPaddingRight;
13262 }
13263
13264 /**
13265 * Return if the padding as been set thru relative values
13266 * {@link #setPaddingRelative(int, int, int, int)} or thru
13267 * @attr ref android.R.styleable#View_paddingStart or
13268 * @attr ref android.R.styleable#View_paddingEnd
13269 *
13270 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013271 */
13272 public boolean isPaddingRelative() {
13273 return mUserPaddingRelative;
13274 }
13275
13276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013277 * Changes the selection state of this view. A view can be selected or not.
13278 * Note that selection is not the same as focus. Views are typically
13279 * selected in the context of an AdapterView like ListView or GridView;
13280 * the selected view is the view that is highlighted.
13281 *
13282 * @param selected true if the view must be selected, false otherwise
13283 */
13284 public void setSelected(boolean selected) {
13285 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13286 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013287 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013288 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013289 refreshDrawableState();
13290 dispatchSetSelected(selected);
13291 }
13292 }
13293
13294 /**
13295 * Dispatch setSelected to all of this View's children.
13296 *
13297 * @see #setSelected(boolean)
13298 *
13299 * @param selected The new selected state
13300 */
13301 protected void dispatchSetSelected(boolean selected) {
13302 }
13303
13304 /**
13305 * Indicates the selection state of this view.
13306 *
13307 * @return true if the view is selected, false otherwise
13308 */
13309 @ViewDebug.ExportedProperty
13310 public boolean isSelected() {
13311 return (mPrivateFlags & SELECTED) != 0;
13312 }
13313
13314 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013315 * Changes the activated state of this view. A view can be activated or not.
13316 * Note that activation is not the same as selection. Selection is
13317 * a transient property, representing the view (hierarchy) the user is
13318 * currently interacting with. Activation is a longer-term state that the
13319 * user can move views in and out of. For example, in a list view with
13320 * single or multiple selection enabled, the views in the current selection
13321 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13322 * here.) The activated state is propagated down to children of the view it
13323 * is set on.
13324 *
13325 * @param activated true if the view must be activated, false otherwise
13326 */
13327 public void setActivated(boolean activated) {
13328 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13329 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013330 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013331 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013332 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013333 }
13334 }
13335
13336 /**
13337 * Dispatch setActivated to all of this View's children.
13338 *
13339 * @see #setActivated(boolean)
13340 *
13341 * @param activated The new activated state
13342 */
13343 protected void dispatchSetActivated(boolean activated) {
13344 }
13345
13346 /**
13347 * Indicates the activation state of this view.
13348 *
13349 * @return true if the view is activated, false otherwise
13350 */
13351 @ViewDebug.ExportedProperty
13352 public boolean isActivated() {
13353 return (mPrivateFlags & ACTIVATED) != 0;
13354 }
13355
13356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013357 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13358 * observer can be used to get notifications when global events, like
13359 * layout, happen.
13360 *
13361 * The returned ViewTreeObserver observer is not guaranteed to remain
13362 * valid for the lifetime of this View. If the caller of this method keeps
13363 * a long-lived reference to ViewTreeObserver, it should always check for
13364 * the return value of {@link ViewTreeObserver#isAlive()}.
13365 *
13366 * @return The ViewTreeObserver for this view's hierarchy.
13367 */
13368 public ViewTreeObserver getViewTreeObserver() {
13369 if (mAttachInfo != null) {
13370 return mAttachInfo.mTreeObserver;
13371 }
13372 if (mFloatingTreeObserver == null) {
13373 mFloatingTreeObserver = new ViewTreeObserver();
13374 }
13375 return mFloatingTreeObserver;
13376 }
13377
13378 /**
13379 * <p>Finds the topmost view in the current view hierarchy.</p>
13380 *
13381 * @return the topmost view containing this view
13382 */
13383 public View getRootView() {
13384 if (mAttachInfo != null) {
13385 final View v = mAttachInfo.mRootView;
13386 if (v != null) {
13387 return v;
13388 }
13389 }
Romain Guy8506ab42009-06-11 17:35:47 -070013390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013391 View parent = this;
13392
13393 while (parent.mParent != null && parent.mParent instanceof View) {
13394 parent = (View) parent.mParent;
13395 }
13396
13397 return parent;
13398 }
13399
13400 /**
13401 * <p>Computes the coordinates of this view on the screen. The argument
13402 * must be an array of two integers. After the method returns, the array
13403 * contains the x and y location in that order.</p>
13404 *
13405 * @param location an array of two integers in which to hold the coordinates
13406 */
13407 public void getLocationOnScreen(int[] location) {
13408 getLocationInWindow(location);
13409
13410 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013411 if (info != null) {
13412 location[0] += info.mWindowLeft;
13413 location[1] += info.mWindowTop;
13414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013415 }
13416
13417 /**
13418 * <p>Computes the coordinates of this view in its window. The argument
13419 * must be an array of two integers. After the method returns, the array
13420 * contains the x and y location in that order.</p>
13421 *
13422 * @param location an array of two integers in which to hold the coordinates
13423 */
13424 public void getLocationInWindow(int[] location) {
13425 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013426 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013427 }
13428
Gilles Debunne6583ce52011-12-06 18:09:02 -080013429 if (mAttachInfo == null) {
13430 // When the view is not attached to a window, this method does not make sense
13431 location[0] = location[1] = 0;
13432 return;
13433 }
13434
Gilles Debunnecea45132011-11-24 02:19:27 +010013435 float[] position = mAttachInfo.mTmpTransformLocation;
13436 position[0] = position[1] = 0.0f;
13437
13438 if (!hasIdentityMatrix()) {
13439 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013441
Gilles Debunnecea45132011-11-24 02:19:27 +010013442 position[0] += mLeft;
13443 position[1] += mTop;
13444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013445 ViewParent viewParent = mParent;
13446 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013447 final View view = (View) viewParent;
13448
13449 position[0] -= view.mScrollX;
13450 position[1] -= view.mScrollY;
13451
13452 if (!view.hasIdentityMatrix()) {
13453 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013454 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013455
13456 position[0] += view.mLeft;
13457 position[1] += view.mTop;
13458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013459 viewParent = view.mParent;
13460 }
Romain Guy8506ab42009-06-11 17:35:47 -070013461
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013462 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013463 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010013464 final ViewRootImpl vr = (ViewRootImpl) viewParent;
13465 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013466 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013467
13468 location[0] = (int) (position[0] + 0.5f);
13469 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013470 }
13471
13472 /**
13473 * {@hide}
13474 * @param id the id of the view to be found
13475 * @return the view of the specified id, null if cannot be found
13476 */
13477 protected View findViewTraversal(int id) {
13478 if (id == mID) {
13479 return this;
13480 }
13481 return null;
13482 }
13483
13484 /**
13485 * {@hide}
13486 * @param tag the tag of the view to be found
13487 * @return the view of specified tag, null if cannot be found
13488 */
13489 protected View findViewWithTagTraversal(Object tag) {
13490 if (tag != null && tag.equals(mTag)) {
13491 return this;
13492 }
13493 return null;
13494 }
13495
13496 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013497 * {@hide}
13498 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013499 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013500 * @return The first view that matches the predicate or null.
13501 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013502 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013503 if (predicate.apply(this)) {
13504 return this;
13505 }
13506 return null;
13507 }
13508
13509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 * Look for a child view with the given id. If this view has the given
13511 * id, return this view.
13512 *
13513 * @param id The id to search for.
13514 * @return The view that has the given id in the hierarchy or null
13515 */
13516 public final View findViewById(int id) {
13517 if (id < 0) {
13518 return null;
13519 }
13520 return findViewTraversal(id);
13521 }
13522
13523 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013524 * Finds a view by its unuque and stable accessibility id.
13525 *
13526 * @param accessibilityId The searched accessibility id.
13527 * @return The found view.
13528 */
13529 final View findViewByAccessibilityId(int accessibilityId) {
13530 if (accessibilityId < 0) {
13531 return null;
13532 }
13533 return findViewByAccessibilityIdTraversal(accessibilityId);
13534 }
13535
13536 /**
13537 * Performs the traversal to find a view by its unuque and stable accessibility id.
13538 *
13539 * <strong>Note:</strong>This method does not stop at the root namespace
13540 * boundary since the user can touch the screen at an arbitrary location
13541 * potentially crossing the root namespace bounday which will send an
13542 * accessibility event to accessibility services and they should be able
13543 * to obtain the event source. Also accessibility ids are guaranteed to be
13544 * unique in the window.
13545 *
13546 * @param accessibilityId The accessibility id.
13547 * @return The found view.
13548 */
13549 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13550 if (getAccessibilityViewId() == accessibilityId) {
13551 return this;
13552 }
13553 return null;
13554 }
13555
13556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013557 * Look for a child view with the given tag. If this view has the given
13558 * tag, return this view.
13559 *
13560 * @param tag The tag to search for, using "tag.equals(getTag())".
13561 * @return The View that has the given tag in the hierarchy or null
13562 */
13563 public final View findViewWithTag(Object tag) {
13564 if (tag == null) {
13565 return null;
13566 }
13567 return findViewWithTagTraversal(tag);
13568 }
13569
13570 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013571 * {@hide}
13572 * Look for a child view that matches the specified predicate.
13573 * If this view matches the predicate, return this view.
13574 *
13575 * @param predicate The predicate to evaluate.
13576 * @return The first view that matches the predicate or null.
13577 */
13578 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013579 return findViewByPredicateTraversal(predicate, null);
13580 }
13581
13582 /**
13583 * {@hide}
13584 * Look for a child view that matches the specified predicate,
13585 * starting with the specified view and its descendents and then
13586 * recusively searching the ancestors and siblings of that view
13587 * until this view is reached.
13588 *
13589 * This method is useful in cases where the predicate does not match
13590 * a single unique view (perhaps multiple views use the same id)
13591 * and we are trying to find the view that is "closest" in scope to the
13592 * starting view.
13593 *
13594 * @param start The view to start from.
13595 * @param predicate The predicate to evaluate.
13596 * @return The first view that matches the predicate or null.
13597 */
13598 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13599 View childToSkip = null;
13600 for (;;) {
13601 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13602 if (view != null || start == this) {
13603 return view;
13604 }
13605
13606 ViewParent parent = start.getParent();
13607 if (parent == null || !(parent instanceof View)) {
13608 return null;
13609 }
13610
13611 childToSkip = start;
13612 start = (View) parent;
13613 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013614 }
13615
13616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013617 * Sets the identifier for this view. The identifier does not have to be
13618 * unique in this view's hierarchy. The identifier should be a positive
13619 * number.
13620 *
13621 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070013622 * @see #getId()
13623 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013624 *
13625 * @param id a number used to identify the view
13626 *
13627 * @attr ref android.R.styleable#View_id
13628 */
13629 public void setId(int id) {
13630 mID = id;
13631 }
13632
13633 /**
13634 * {@hide}
13635 *
13636 * @param isRoot true if the view belongs to the root namespace, false
13637 * otherwise
13638 */
13639 public void setIsRootNamespace(boolean isRoot) {
13640 if (isRoot) {
13641 mPrivateFlags |= IS_ROOT_NAMESPACE;
13642 } else {
13643 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13644 }
13645 }
13646
13647 /**
13648 * {@hide}
13649 *
13650 * @return true if the view belongs to the root namespace, false otherwise
13651 */
13652 public boolean isRootNamespace() {
13653 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13654 }
13655
13656 /**
13657 * Returns this view's identifier.
13658 *
13659 * @return a positive integer used to identify the view or {@link #NO_ID}
13660 * if the view has no ID
13661 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013662 * @see #setId(int)
13663 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013664 * @attr ref android.R.styleable#View_id
13665 */
13666 @ViewDebug.CapturedViewProperty
13667 public int getId() {
13668 return mID;
13669 }
13670
13671 /**
13672 * Returns this view's tag.
13673 *
13674 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013675 *
13676 * @see #setTag(Object)
13677 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678 */
13679 @ViewDebug.ExportedProperty
13680 public Object getTag() {
13681 return mTag;
13682 }
13683
13684 /**
13685 * Sets the tag associated with this view. A tag can be used to mark
13686 * a view in its hierarchy and does not have to be unique within the
13687 * hierarchy. Tags can also be used to store data within a view without
13688 * resorting to another data structure.
13689 *
13690 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013691 *
13692 * @see #getTag()
13693 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013694 */
13695 public void setTag(final Object tag) {
13696 mTag = tag;
13697 }
13698
13699 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013700 * Returns the tag associated with this view and the specified key.
13701 *
13702 * @param key The key identifying the tag
13703 *
13704 * @return the Object stored in this view as a tag
13705 *
13706 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013707 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013708 */
13709 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013710 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013711 return null;
13712 }
13713
13714 /**
13715 * Sets a tag associated with this view and a key. A tag can be used
13716 * to mark a view in its hierarchy and does not have to be unique within
13717 * the hierarchy. Tags can also be used to store data within a view
13718 * without resorting to another data structure.
13719 *
13720 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013721 * application to ensure it is unique (see the <a
13722 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13723 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013724 * the Android framework or not associated with any package will cause
13725 * an {@link IllegalArgumentException} to be thrown.
13726 *
13727 * @param key The key identifying the tag
13728 * @param tag An Object to tag the view with
13729 *
13730 * @throws IllegalArgumentException If they specified key is not valid
13731 *
13732 * @see #setTag(Object)
13733 * @see #getTag(int)
13734 */
13735 public void setTag(int key, final Object tag) {
13736 // If the package id is 0x00 or 0x01, it's either an undefined package
13737 // or a framework id
13738 if ((key >>> 24) < 2) {
13739 throw new IllegalArgumentException("The key must be an application-specific "
13740 + "resource id.");
13741 }
13742
Adam Powell2b2f6d62011-09-23 11:15:39 -070013743 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013744 }
13745
13746 /**
13747 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13748 * framework id.
13749 *
13750 * @hide
13751 */
13752 public void setTagInternal(int key, Object tag) {
13753 if ((key >>> 24) != 0x1) {
13754 throw new IllegalArgumentException("The key must be a framework-specific "
13755 + "resource id.");
13756 }
13757
Adam Powell2b2f6d62011-09-23 11:15:39 -070013758 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013759 }
13760
Adam Powell2b2f6d62011-09-23 11:15:39 -070013761 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013762 if (mKeyedTags == null) {
13763 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013764 }
13765
Adam Powell7db82ac2011-09-22 19:44:04 -070013766 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013767 }
13768
13769 /**
Romain Guy13922e02009-05-12 17:56:14 -070013770 * @param consistency The type of consistency. See ViewDebug for more information.
13771 *
13772 * @hide
13773 */
13774 protected boolean dispatchConsistencyCheck(int consistency) {
13775 return onConsistencyCheck(consistency);
13776 }
13777
13778 /**
13779 * Method that subclasses should implement to check their consistency. The type of
13780 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013781 *
Romain Guy13922e02009-05-12 17:56:14 -070013782 * @param consistency The type of consistency. See ViewDebug for more information.
13783 *
13784 * @throws IllegalStateException if the view is in an inconsistent state.
13785 *
13786 * @hide
13787 */
13788 protected boolean onConsistencyCheck(int consistency) {
13789 boolean result = true;
13790
13791 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13792 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13793
13794 if (checkLayout) {
13795 if (getParent() == null) {
13796 result = false;
13797 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13798 "View " + this + " does not have a parent.");
13799 }
13800
13801 if (mAttachInfo == null) {
13802 result = false;
13803 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13804 "View " + this + " is not attached to a window.");
13805 }
13806 }
13807
13808 if (checkDrawing) {
13809 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13810 // from their draw() method
13811
13812 if ((mPrivateFlags & DRAWN) != DRAWN &&
13813 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13814 result = false;
13815 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13816 "View " + this + " was invalidated but its drawing cache is valid.");
13817 }
13818 }
13819
13820 return result;
13821 }
13822
13823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824 * Prints information about this view in the log output, with the tag
13825 * {@link #VIEW_LOG_TAG}.
13826 *
13827 * @hide
13828 */
13829 public void debug() {
13830 debug(0);
13831 }
13832
13833 /**
13834 * Prints information about this view in the log output, with the tag
13835 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13836 * indentation defined by the <code>depth</code>.
13837 *
13838 * @param depth the indentation level
13839 *
13840 * @hide
13841 */
13842 protected void debug(int depth) {
13843 String output = debugIndent(depth - 1);
13844
13845 output += "+ " + this;
13846 int id = getId();
13847 if (id != -1) {
13848 output += " (id=" + id + ")";
13849 }
13850 Object tag = getTag();
13851 if (tag != null) {
13852 output += " (tag=" + tag + ")";
13853 }
13854 Log.d(VIEW_LOG_TAG, output);
13855
13856 if ((mPrivateFlags & FOCUSED) != 0) {
13857 output = debugIndent(depth) + " FOCUSED";
13858 Log.d(VIEW_LOG_TAG, output);
13859 }
13860
13861 output = debugIndent(depth);
13862 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13863 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13864 + "} ";
13865 Log.d(VIEW_LOG_TAG, output);
13866
13867 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13868 || mPaddingBottom != 0) {
13869 output = debugIndent(depth);
13870 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13871 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13872 Log.d(VIEW_LOG_TAG, output);
13873 }
13874
13875 output = debugIndent(depth);
13876 output += "mMeasureWidth=" + mMeasuredWidth +
13877 " mMeasureHeight=" + mMeasuredHeight;
13878 Log.d(VIEW_LOG_TAG, output);
13879
13880 output = debugIndent(depth);
13881 if (mLayoutParams == null) {
13882 output += "BAD! no layout params";
13883 } else {
13884 output = mLayoutParams.debug(output);
13885 }
13886 Log.d(VIEW_LOG_TAG, output);
13887
13888 output = debugIndent(depth);
13889 output += "flags={";
13890 output += View.printFlags(mViewFlags);
13891 output += "}";
13892 Log.d(VIEW_LOG_TAG, output);
13893
13894 output = debugIndent(depth);
13895 output += "privateFlags={";
13896 output += View.printPrivateFlags(mPrivateFlags);
13897 output += "}";
13898 Log.d(VIEW_LOG_TAG, output);
13899 }
13900
13901 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013902 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 *
13904 * @param depth the indentation level
13905 * @return a String containing (depth * 2 + 3) * 2 white spaces
13906 *
13907 * @hide
13908 */
13909 protected static String debugIndent(int depth) {
13910 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13911 for (int i = 0; i < (depth * 2) + 3; i++) {
13912 spaces.append(' ').append(' ');
13913 }
13914 return spaces.toString();
13915 }
13916
13917 /**
13918 * <p>Return the offset of the widget's text baseline from the widget's top
13919 * boundary. If this widget does not support baseline alignment, this
13920 * method returns -1. </p>
13921 *
13922 * @return the offset of the baseline within the widget's bounds or -1
13923 * if baseline alignment is not supported
13924 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013925 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926 public int getBaseline() {
13927 return -1;
13928 }
13929
13930 /**
13931 * Call this when something has changed which has invalidated the
13932 * layout of this view. This will schedule a layout pass of the view
13933 * tree.
13934 */
13935 public void requestLayout() {
13936 if (ViewDebug.TRACE_HIERARCHY) {
13937 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13938 }
13939
13940 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013941 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070013943 if (mLayoutParams != null) {
13944 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
13945 }
13946
13947 if (mParent != null && !mParent.isLayoutRequested()) {
13948 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013949 }
13950 }
13951
13952 /**
13953 * Forces this view to be laid out during the next layout pass.
13954 * This method does not call requestLayout() or forceLayout()
13955 * on the parent.
13956 */
13957 public void forceLayout() {
13958 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013959 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 }
13961
13962 /**
13963 * <p>
13964 * This is called to find out how big a view should be. The parent
13965 * supplies constraint information in the width and height parameters.
13966 * </p>
13967 *
13968 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013969 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013970 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013971 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 * </p>
13973 *
13974 *
13975 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13976 * parent
13977 * @param heightMeasureSpec Vertical space requirements as imposed by the
13978 * parent
13979 *
13980 * @see #onMeasure(int, int)
13981 */
13982 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13983 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13984 widthMeasureSpec != mOldWidthMeasureSpec ||
13985 heightMeasureSpec != mOldHeightMeasureSpec) {
13986
13987 // first clears the measured dimension flag
13988 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13989
13990 if (ViewDebug.TRACE_HIERARCHY) {
13991 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13992 }
13993
13994 // measure ourselves, this should set the measured dimension flag back
13995 onMeasure(widthMeasureSpec, heightMeasureSpec);
13996
13997 // flag not set, setMeasuredDimension() was not invoked, we raise
13998 // an exception to warn the developer
13999 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14000 throw new IllegalStateException("onMeasure() did not set the"
14001 + " measured dimension by calling"
14002 + " setMeasuredDimension()");
14003 }
14004
14005 mPrivateFlags |= LAYOUT_REQUIRED;
14006 }
14007
14008 mOldWidthMeasureSpec = widthMeasureSpec;
14009 mOldHeightMeasureSpec = heightMeasureSpec;
14010 }
14011
14012 /**
14013 * <p>
14014 * Measure the view and its content to determine the measured width and the
14015 * measured height. This method is invoked by {@link #measure(int, int)} and
14016 * should be overriden by subclasses to provide accurate and efficient
14017 * measurement of their contents.
14018 * </p>
14019 *
14020 * <p>
14021 * <strong>CONTRACT:</strong> When overriding this method, you
14022 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14023 * measured width and height of this view. Failure to do so will trigger an
14024 * <code>IllegalStateException</code>, thrown by
14025 * {@link #measure(int, int)}. Calling the superclass'
14026 * {@link #onMeasure(int, int)} is a valid use.
14027 * </p>
14028 *
14029 * <p>
14030 * The base class implementation of measure defaults to the background size,
14031 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14032 * override {@link #onMeasure(int, int)} to provide better measurements of
14033 * their content.
14034 * </p>
14035 *
14036 * <p>
14037 * If this method is overridden, it is the subclass's responsibility to make
14038 * sure the measured height and width are at least the view's minimum height
14039 * and width ({@link #getSuggestedMinimumHeight()} and
14040 * {@link #getSuggestedMinimumWidth()}).
14041 * </p>
14042 *
14043 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14044 * The requirements are encoded with
14045 * {@link android.view.View.MeasureSpec}.
14046 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14047 * The requirements are encoded with
14048 * {@link android.view.View.MeasureSpec}.
14049 *
14050 * @see #getMeasuredWidth()
14051 * @see #getMeasuredHeight()
14052 * @see #setMeasuredDimension(int, int)
14053 * @see #getSuggestedMinimumHeight()
14054 * @see #getSuggestedMinimumWidth()
14055 * @see android.view.View.MeasureSpec#getMode(int)
14056 * @see android.view.View.MeasureSpec#getSize(int)
14057 */
14058 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14059 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14060 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14061 }
14062
14063 /**
14064 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14065 * measured width and measured height. Failing to do so will trigger an
14066 * exception at measurement time.</p>
14067 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014068 * @param measuredWidth The measured width of this view. May be a complex
14069 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14070 * {@link #MEASURED_STATE_TOO_SMALL}.
14071 * @param measuredHeight The measured height of this view. May be a complex
14072 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14073 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 */
14075 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14076 mMeasuredWidth = measuredWidth;
14077 mMeasuredHeight = measuredHeight;
14078
14079 mPrivateFlags |= MEASURED_DIMENSION_SET;
14080 }
14081
14082 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014083 * Merge two states as returned by {@link #getMeasuredState()}.
14084 * @param curState The current state as returned from a view or the result
14085 * of combining multiple views.
14086 * @param newState The new view state to combine.
14087 * @return Returns a new integer reflecting the combination of the two
14088 * states.
14089 */
14090 public static int combineMeasuredStates(int curState, int newState) {
14091 return curState | newState;
14092 }
14093
14094 /**
14095 * Version of {@link #resolveSizeAndState(int, int, int)}
14096 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14097 */
14098 public static int resolveSize(int size, int measureSpec) {
14099 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14100 }
14101
14102 /**
14103 * Utility to reconcile a desired size and state, with constraints imposed
14104 * by a MeasureSpec. Will take the desired size, unless a different size
14105 * is imposed by the constraints. The returned value is a compound integer,
14106 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14107 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14108 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014109 *
14110 * @param size How big the view wants to be
14111 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014112 * @return Size information bit mask as defined by
14113 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014115 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014116 int result = size;
14117 int specMode = MeasureSpec.getMode(measureSpec);
14118 int specSize = MeasureSpec.getSize(measureSpec);
14119 switch (specMode) {
14120 case MeasureSpec.UNSPECIFIED:
14121 result = size;
14122 break;
14123 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014124 if (specSize < size) {
14125 result = specSize | MEASURED_STATE_TOO_SMALL;
14126 } else {
14127 result = size;
14128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014129 break;
14130 case MeasureSpec.EXACTLY:
14131 result = specSize;
14132 break;
14133 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014134 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014135 }
14136
14137 /**
14138 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014139 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014140 * by the MeasureSpec.
14141 *
14142 * @param size Default size for this view
14143 * @param measureSpec Constraints imposed by the parent
14144 * @return The size this view should be.
14145 */
14146 public static int getDefaultSize(int size, int measureSpec) {
14147 int result = size;
14148 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014149 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150
14151 switch (specMode) {
14152 case MeasureSpec.UNSPECIFIED:
14153 result = size;
14154 break;
14155 case MeasureSpec.AT_MOST:
14156 case MeasureSpec.EXACTLY:
14157 result = specSize;
14158 break;
14159 }
14160 return result;
14161 }
14162
14163 /**
14164 * Returns the suggested minimum height that the view should use. This
14165 * returns the maximum of the view's minimum height
14166 * and the background's minimum height
14167 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14168 * <p>
14169 * When being used in {@link #onMeasure(int, int)}, the caller should still
14170 * ensure the returned height is within the requirements of the parent.
14171 *
14172 * @return The suggested minimum height of the view.
14173 */
14174 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014175 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014177 }
14178
14179 /**
14180 * Returns the suggested minimum width that the view should use. This
14181 * returns the maximum of the view's minimum width)
14182 * and the background's minimum width
14183 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14184 * <p>
14185 * When being used in {@link #onMeasure(int, int)}, the caller should still
14186 * ensure the returned width is within the requirements of the parent.
14187 *
14188 * @return The suggested minimum width of the view.
14189 */
14190 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014191 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014193
Philip Milne6c8ea062012-04-03 17:38:43 -070014194 /**
14195 * Returns the minimum height of the view.
14196 *
14197 * @return the minimum height the view will try to be.
14198 *
14199 * @see #setMinimumHeight(int)
14200 *
14201 * @attr ref android.R.styleable#View_minHeight
14202 */
14203 public int getMinimumHeight() {
14204 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014205 }
14206
14207 /**
14208 * Sets the minimum height of the view. It is not guaranteed the view will
14209 * be able to achieve this minimum height (for example, if its parent layout
14210 * constrains it with less available height).
14211 *
14212 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014213 *
14214 * @see #getMinimumHeight()
14215 *
14216 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217 */
14218 public void setMinimumHeight(int minHeight) {
14219 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014220 requestLayout();
14221 }
14222
14223 /**
14224 * Returns the minimum width of the view.
14225 *
14226 * @return the minimum width the view will try to be.
14227 *
14228 * @see #setMinimumWidth(int)
14229 *
14230 * @attr ref android.R.styleable#View_minWidth
14231 */
14232 public int getMinimumWidth() {
14233 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014234 }
14235
14236 /**
14237 * Sets the minimum width of the view. It is not guaranteed the view will
14238 * be able to achieve this minimum width (for example, if its parent layout
14239 * constrains it with less available width).
14240 *
14241 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014242 *
14243 * @see #getMinimumWidth()
14244 *
14245 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 */
14247 public void setMinimumWidth(int minWidth) {
14248 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014249 requestLayout();
14250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014251 }
14252
14253 /**
14254 * Get the animation currently associated with this view.
14255 *
14256 * @return The animation that is currently playing or
14257 * scheduled to play for this view.
14258 */
14259 public Animation getAnimation() {
14260 return mCurrentAnimation;
14261 }
14262
14263 /**
14264 * Start the specified animation now.
14265 *
14266 * @param animation the animation to start now
14267 */
14268 public void startAnimation(Animation animation) {
14269 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14270 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014271 invalidateParentCaches();
14272 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014273 }
14274
14275 /**
14276 * Cancels any animations for this view.
14277 */
14278 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014279 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014280 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014283 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014284 }
14285
14286 /**
14287 * Sets the next animation to play for this view.
14288 * If you want the animation to play immediately, use
14289 * startAnimation. This method provides allows fine-grained
14290 * control over the start time and invalidation, but you
14291 * must make sure that 1) the animation has a start time set, and
14292 * 2) the view will be invalidated when the animation is supposed to
14293 * start.
14294 *
14295 * @param animation The next animation, or null.
14296 */
14297 public void setAnimation(Animation animation) {
14298 mCurrentAnimation = animation;
14299 if (animation != null) {
14300 animation.reset();
14301 }
14302 }
14303
14304 /**
14305 * Invoked by a parent ViewGroup to notify the start of the animation
14306 * currently associated with this view. If you override this method,
14307 * always call super.onAnimationStart();
14308 *
14309 * @see #setAnimation(android.view.animation.Animation)
14310 * @see #getAnimation()
14311 */
14312 protected void onAnimationStart() {
14313 mPrivateFlags |= ANIMATION_STARTED;
14314 }
14315
14316 /**
14317 * Invoked by a parent ViewGroup to notify the end of the animation
14318 * currently associated with this view. If you override this method,
14319 * always call super.onAnimationEnd();
14320 *
14321 * @see #setAnimation(android.view.animation.Animation)
14322 * @see #getAnimation()
14323 */
14324 protected void onAnimationEnd() {
14325 mPrivateFlags &= ~ANIMATION_STARTED;
14326 }
14327
14328 /**
14329 * Invoked if there is a Transform that involves alpha. Subclass that can
14330 * draw themselves with the specified alpha should return true, and then
14331 * respect that alpha when their onDraw() is called. If this returns false
14332 * then the view may be redirected to draw into an offscreen buffer to
14333 * fulfill the request, which will look fine, but may be slower than if the
14334 * subclass handles it internally. The default implementation returns false.
14335 *
14336 * @param alpha The alpha (0..255) to apply to the view's drawing
14337 * @return true if the view can draw with the specified alpha.
14338 */
14339 protected boolean onSetAlpha(int alpha) {
14340 return false;
14341 }
14342
14343 /**
14344 * This is used by the RootView to perform an optimization when
14345 * the view hierarchy contains one or several SurfaceView.
14346 * SurfaceView is always considered transparent, but its children are not,
14347 * therefore all View objects remove themselves from the global transparent
14348 * region (passed as a parameter to this function).
14349 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014350 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014351 *
14352 * @return Returns true if the effective visibility of the view at this
14353 * point is opaque, regardless of the transparent region; returns false
14354 * if it is possible for underlying windows to be seen behind the view.
14355 *
14356 * {@hide}
14357 */
14358 public boolean gatherTransparentRegion(Region region) {
14359 final AttachInfo attachInfo = mAttachInfo;
14360 if (region != null && attachInfo != null) {
14361 final int pflags = mPrivateFlags;
14362 if ((pflags & SKIP_DRAW) == 0) {
14363 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14364 // remove it from the transparent region.
14365 final int[] location = attachInfo.mTransparentLocation;
14366 getLocationInWindow(location);
14367 region.op(location[0], location[1], location[0] + mRight - mLeft,
14368 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014369 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014370 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14371 // exists, so we remove the background drawable's non-transparent
14372 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014373 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014374 }
14375 }
14376 return true;
14377 }
14378
14379 /**
14380 * Play a sound effect for this view.
14381 *
14382 * <p>The framework will play sound effects for some built in actions, such as
14383 * clicking, but you may wish to play these effects in your widget,
14384 * for instance, for internal navigation.
14385 *
14386 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14387 * {@link #isSoundEffectsEnabled()} is true.
14388 *
14389 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14390 */
14391 public void playSoundEffect(int soundConstant) {
14392 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14393 return;
14394 }
14395 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14396 }
14397
14398 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014399 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014400 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014401 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014402 *
14403 * <p>The framework will provide haptic feedback for some built in actions,
14404 * such as long presses, but you may wish to provide feedback for your
14405 * own widget.
14406 *
14407 * <p>The feedback will only be performed if
14408 * {@link #isHapticFeedbackEnabled()} is true.
14409 *
14410 * @param feedbackConstant One of the constants defined in
14411 * {@link HapticFeedbackConstants}
14412 */
14413 public boolean performHapticFeedback(int feedbackConstant) {
14414 return performHapticFeedback(feedbackConstant, 0);
14415 }
14416
14417 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014418 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014419 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014420 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 *
14422 * @param feedbackConstant One of the constants defined in
14423 * {@link HapticFeedbackConstants}
14424 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14425 */
14426 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14427 if (mAttachInfo == null) {
14428 return false;
14429 }
Romain Guyf607bdc2010-09-10 19:20:06 -070014430 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070014431 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432 && !isHapticFeedbackEnabled()) {
14433 return false;
14434 }
Romain Guy812ccbe2010-06-01 14:07:24 -070014435 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
14436 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014437 }
14438
14439 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070014440 * Request that the visibility of the status bar or other screen/window
14441 * decorations be changed.
14442 *
14443 * <p>This method is used to put the over device UI into temporary modes
14444 * where the user's attention is focused more on the application content,
14445 * by dimming or hiding surrounding system affordances. This is typically
14446 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
14447 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
14448 * to be placed behind the action bar (and with these flags other system
14449 * affordances) so that smooth transitions between hiding and showing them
14450 * can be done.
14451 *
14452 * <p>Two representative examples of the use of system UI visibility is
14453 * implementing a content browsing application (like a magazine reader)
14454 * and a video playing application.
14455 *
14456 * <p>The first code shows a typical implementation of a View in a content
14457 * browsing application. In this implementation, the application goes
14458 * into a content-oriented mode by hiding the status bar and action bar,
14459 * and putting the navigation elements into lights out mode. The user can
14460 * then interact with content while in this mode. Such an application should
14461 * provide an easy way for the user to toggle out of the mode (such as to
14462 * check information in the status bar or access notifications). In the
14463 * implementation here, this is done simply by tapping on the content.
14464 *
14465 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
14466 * content}
14467 *
14468 * <p>This second code sample shows a typical implementation of a View
14469 * in a video playing application. In this situation, while the video is
14470 * playing the application would like to go into a complete full-screen mode,
14471 * to use as much of the display as possible for the video. When in this state
14472 * the user can not interact with the application; the system intercepts
14473 * touching on the screen to pop the UI out of full screen mode.
14474 *
14475 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
14476 * content}
14477 *
14478 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
14479 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
14480 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
14481 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080014482 */
14483 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040014484 if (visibility != mSystemUiVisibility) {
14485 mSystemUiVisibility = visibility;
14486 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14487 mParent.recomputeViewAttributes(this);
14488 }
Joe Onorato664644d2011-01-23 17:53:23 -080014489 }
14490 }
14491
14492 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070014493 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
14494 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
14495 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
14496 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
14497 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080014498 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080014499 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080014500 return mSystemUiVisibility;
14501 }
14502
Scott Mainec6331b2011-05-24 16:55:56 -070014503 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070014504 * Returns the current system UI visibility that is currently set for
14505 * the entire window. This is the combination of the
14506 * {@link #setSystemUiVisibility(int)} values supplied by all of the
14507 * views in the window.
14508 */
14509 public int getWindowSystemUiVisibility() {
14510 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
14511 }
14512
14513 /**
14514 * Override to find out when the window's requested system UI visibility
14515 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
14516 * This is different from the callbacks recieved through
14517 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
14518 * in that this is only telling you about the local request of the window,
14519 * not the actual values applied by the system.
14520 */
14521 public void onWindowSystemUiVisibilityChanged(int visible) {
14522 }
14523
14524 /**
14525 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
14526 * the view hierarchy.
14527 */
14528 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
14529 onWindowSystemUiVisibilityChanged(visible);
14530 }
14531
14532 /**
Scott Mainec6331b2011-05-24 16:55:56 -070014533 * Set a listener to receive callbacks when the visibility of the system bar changes.
14534 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
14535 */
Joe Onorato664644d2011-01-23 17:53:23 -080014536 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014537 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080014538 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14539 mParent.recomputeViewAttributes(this);
14540 }
14541 }
14542
14543 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014544 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
14545 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080014546 */
14547 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014548 ListenerInfo li = mListenerInfo;
14549 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
14550 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080014551 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080014552 }
14553 }
14554
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014555 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
14556 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
14557 if (val != mSystemUiVisibility) {
14558 setSystemUiVisibility(val);
14559 }
14560 }
14561
Joe Onorato664644d2011-01-23 17:53:23 -080014562 /**
Joe Malin32736f02011-01-19 16:14:20 -080014563 * Creates an image that the system displays during the drag and drop
14564 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14565 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14566 * appearance as the given View. The default also positions the center of the drag shadow
14567 * directly under the touch point. If no View is provided (the constructor with no parameters
14568 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14569 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14570 * default is an invisible drag shadow.
14571 * <p>
14572 * You are not required to use the View you provide to the constructor as the basis of the
14573 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14574 * anything you want as the drag shadow.
14575 * </p>
14576 * <p>
14577 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14578 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14579 * size and position of the drag shadow. It uses this data to construct a
14580 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14581 * so that your application can draw the shadow image in the Canvas.
14582 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014583 *
14584 * <div class="special reference">
14585 * <h3>Developer Guides</h3>
14586 * <p>For a guide to implementing drag and drop features, read the
14587 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14588 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014589 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014590 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014591 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014592
14593 /**
Joe Malin32736f02011-01-19 16:14:20 -080014594 * Constructs a shadow image builder based on a View. By default, the resulting drag
14595 * shadow will have the same appearance and dimensions as the View, with the touch point
14596 * over the center of the View.
14597 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014598 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014599 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014600 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014601 }
14602
Christopher Tate17ed60c2011-01-18 12:50:26 -080014603 /**
14604 * Construct a shadow builder object with no associated View. This
14605 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14606 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14607 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014608 * reference to any View object. If they are not overridden, then the result is an
14609 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014610 */
14611 public DragShadowBuilder() {
14612 mView = new WeakReference<View>(null);
14613 }
14614
14615 /**
14616 * Returns the View object that had been passed to the
14617 * {@link #View.DragShadowBuilder(View)}
14618 * constructor. If that View parameter was {@code null} or if the
14619 * {@link #View.DragShadowBuilder()}
14620 * constructor was used to instantiate the builder object, this method will return
14621 * null.
14622 *
14623 * @return The View object associate with this builder object.
14624 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014625 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014626 final public View getView() {
14627 return mView.get();
14628 }
14629
Christopher Tate2c095f32010-10-04 14:13:40 -070014630 /**
Joe Malin32736f02011-01-19 16:14:20 -080014631 * Provides the metrics for the shadow image. These include the dimensions of
14632 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014633 * be centered under the touch location while dragging.
14634 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014635 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014636 * same as the dimensions of the View itself and centers the shadow under
14637 * the touch point.
14638 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014639 *
Joe Malin32736f02011-01-19 16:14:20 -080014640 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14641 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14642 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14643 * image.
14644 *
14645 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14646 * shadow image that should be underneath the touch point during the drag and drop
14647 * operation. Your application must set {@link android.graphics.Point#x} to the
14648 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014649 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014650 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014651 final View view = mView.get();
14652 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014653 shadowSize.set(view.getWidth(), view.getHeight());
14654 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014655 } else {
14656 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14657 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014658 }
14659
14660 /**
Joe Malin32736f02011-01-19 16:14:20 -080014661 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14662 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014663 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014664 *
Joe Malin32736f02011-01-19 16:14:20 -080014665 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014666 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014667 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014668 final View view = mView.get();
14669 if (view != null) {
14670 view.draw(canvas);
14671 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014672 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014673 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014674 }
14675 }
14676
14677 /**
Joe Malin32736f02011-01-19 16:14:20 -080014678 * Starts a drag and drop operation. When your application calls this method, it passes a
14679 * {@link android.view.View.DragShadowBuilder} object to the system. The
14680 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14681 * to get metrics for the drag shadow, and then calls the object's
14682 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14683 * <p>
14684 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14685 * drag events to all the View objects in your application that are currently visible. It does
14686 * this either by calling the View object's drag listener (an implementation of
14687 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14688 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14689 * Both are passed a {@link android.view.DragEvent} object that has a
14690 * {@link android.view.DragEvent#getAction()} value of
14691 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14692 * </p>
14693 * <p>
14694 * Your application can invoke startDrag() on any attached View object. The View object does not
14695 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14696 * be related to the View the user selected for dragging.
14697 * </p>
14698 * @param data A {@link android.content.ClipData} object pointing to the data to be
14699 * transferred by the drag and drop operation.
14700 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14701 * drag shadow.
14702 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14703 * drop operation. This Object is put into every DragEvent object sent by the system during the
14704 * current drag.
14705 * <p>
14706 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14707 * to the target Views. For example, it can contain flags that differentiate between a
14708 * a copy operation and a move operation.
14709 * </p>
14710 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14711 * so the parameter should be set to 0.
14712 * @return {@code true} if the method completes successfully, or
14713 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14714 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014715 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014716 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014717 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014718 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014719 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014720 }
14721 boolean okay = false;
14722
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014723 Point shadowSize = new Point();
14724 Point shadowTouchPoint = new Point();
14725 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014726
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014727 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14728 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14729 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014730 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014731
Chris Tatea32dcf72010-10-14 12:13:50 -070014732 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014733 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14734 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014735 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014736 Surface surface = new Surface();
14737 try {
14738 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014739 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014740 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014741 + " surface=" + surface);
14742 if (token != null) {
14743 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014744 try {
Chris Tate6b391282010-10-14 15:48:59 -070014745 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014746 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014747 } finally {
14748 surface.unlockCanvasAndPost(canvas);
14749 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014750
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014751 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014752
14753 // Cache the local state object for delivery with DragEvents
14754 root.setLocalDragState(myLocalState);
14755
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014756 // repurpose 'shadowSize' for the last touch point
14757 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014758
Christopher Tatea53146c2010-09-07 11:57:52 -070014759 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014760 shadowSize.x, shadowSize.y,
14761 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014762 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014763
14764 // Off and running! Release our local surface instance; the drag
14765 // shadow surface is now managed by the system process.
14766 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014767 }
14768 } catch (Exception e) {
14769 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14770 surface.destroy();
14771 }
14772
14773 return okay;
14774 }
14775
Christopher Tatea53146c2010-09-07 11:57:52 -070014776 /**
Joe Malin32736f02011-01-19 16:14:20 -080014777 * Handles drag events sent by the system following a call to
14778 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14779 *<p>
14780 * When the system calls this method, it passes a
14781 * {@link android.view.DragEvent} object. A call to
14782 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14783 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14784 * operation.
14785 * @param event The {@link android.view.DragEvent} sent by the system.
14786 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14787 * in DragEvent, indicating the type of drag event represented by this object.
14788 * @return {@code true} if the method was successful, otherwise {@code false}.
14789 * <p>
14790 * The method should return {@code true} in response to an action type of
14791 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14792 * operation.
14793 * </p>
14794 * <p>
14795 * The method should also return {@code true} in response to an action type of
14796 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14797 * {@code false} if it didn't.
14798 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014799 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014800 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014801 return false;
14802 }
14803
14804 /**
Joe Malin32736f02011-01-19 16:14:20 -080014805 * Detects if this View is enabled and has a drag event listener.
14806 * If both are true, then it calls the drag event listener with the
14807 * {@link android.view.DragEvent} it received. If the drag event listener returns
14808 * {@code true}, then dispatchDragEvent() returns {@code true}.
14809 * <p>
14810 * For all other cases, the method calls the
14811 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14812 * method and returns its result.
14813 * </p>
14814 * <p>
14815 * This ensures that a drag event is always consumed, even if the View does not have a drag
14816 * event listener. However, if the View has a listener and the listener returns true, then
14817 * onDragEvent() is not called.
14818 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014819 */
14820 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014821 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014822 ListenerInfo li = mListenerInfo;
14823 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14824 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014825 return true;
14826 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014827 return onDragEvent(event);
14828 }
14829
Christopher Tate3d4bf172011-03-28 16:16:46 -070014830 boolean canAcceptDrag() {
14831 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14832 }
14833
Christopher Tatea53146c2010-09-07 11:57:52 -070014834 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014835 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14836 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014837 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014838 */
14839 public void onCloseSystemDialogs(String reason) {
14840 }
Joe Malin32736f02011-01-19 16:14:20 -080014841
Dianne Hackbornffa42482009-09-23 22:20:11 -070014842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014844 * update a Region being computed for
14845 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 * that any non-transparent parts of the Drawable are removed from the
14847 * given transparent region.
14848 *
14849 * @param dr The Drawable whose transparency is to be applied to the region.
14850 * @param region A Region holding the current transparency information,
14851 * where any parts of the region that are set are considered to be
14852 * transparent. On return, this region will be modified to have the
14853 * transparency information reduced by the corresponding parts of the
14854 * Drawable that are not transparent.
14855 * {@hide}
14856 */
14857 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14858 if (DBG) {
14859 Log.i("View", "Getting transparent region for: " + this);
14860 }
14861 final Region r = dr.getTransparentRegion();
14862 final Rect db = dr.getBounds();
14863 final AttachInfo attachInfo = mAttachInfo;
14864 if (r != null && attachInfo != null) {
14865 final int w = getRight()-getLeft();
14866 final int h = getBottom()-getTop();
14867 if (db.left > 0) {
14868 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14869 r.op(0, 0, db.left, h, Region.Op.UNION);
14870 }
14871 if (db.right < w) {
14872 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14873 r.op(db.right, 0, w, h, Region.Op.UNION);
14874 }
14875 if (db.top > 0) {
14876 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14877 r.op(0, 0, w, db.top, Region.Op.UNION);
14878 }
14879 if (db.bottom < h) {
14880 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14881 r.op(0, db.bottom, w, h, Region.Op.UNION);
14882 }
14883 final int[] location = attachInfo.mTransparentLocation;
14884 getLocationInWindow(location);
14885 r.translate(location[0], location[1]);
14886 region.op(r, Region.Op.INTERSECT);
14887 } else {
14888 region.op(db, Region.Op.DIFFERENCE);
14889 }
14890 }
14891
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014892 private void checkForLongClick(int delayOffset) {
14893 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14894 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014895
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014896 if (mPendingCheckForLongPress == null) {
14897 mPendingCheckForLongPress = new CheckForLongPress();
14898 }
14899 mPendingCheckForLongPress.rememberWindowAttachCount();
14900 postDelayed(mPendingCheckForLongPress,
14901 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014903 }
14904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014905 /**
14906 * Inflate a view from an XML resource. This convenience method wraps the {@link
14907 * LayoutInflater} class, which provides a full range of options for view inflation.
14908 *
14909 * @param context The Context object for your activity or application.
14910 * @param resource The resource ID to inflate
14911 * @param root A view group that will be the parent. Used to properly inflate the
14912 * layout_* parameters.
14913 * @see LayoutInflater
14914 */
14915 public static View inflate(Context context, int resource, ViewGroup root) {
14916 LayoutInflater factory = LayoutInflater.from(context);
14917 return factory.inflate(resource, root);
14918 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 /**
Adam Powell637d3372010-08-25 14:37:03 -070014921 * Scroll the view with standard behavior for scrolling beyond the normal
14922 * content boundaries. Views that call this method should override
14923 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14924 * results of an over-scroll operation.
14925 *
14926 * Views can use this method to handle any touch or fling-based scrolling.
14927 *
14928 * @param deltaX Change in X in pixels
14929 * @param deltaY Change in Y in pixels
14930 * @param scrollX Current X scroll value in pixels before applying deltaX
14931 * @param scrollY Current Y scroll value in pixels before applying deltaY
14932 * @param scrollRangeX Maximum content scroll range along the X axis
14933 * @param scrollRangeY Maximum content scroll range along the Y axis
14934 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14935 * along the X axis.
14936 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14937 * along the Y axis.
14938 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14939 * @return true if scrolling was clamped to an over-scroll boundary along either
14940 * axis, false otherwise.
14941 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014942 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014943 protected boolean overScrollBy(int deltaX, int deltaY,
14944 int scrollX, int scrollY,
14945 int scrollRangeX, int scrollRangeY,
14946 int maxOverScrollX, int maxOverScrollY,
14947 boolean isTouchEvent) {
14948 final int overScrollMode = mOverScrollMode;
14949 final boolean canScrollHorizontal =
14950 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14951 final boolean canScrollVertical =
14952 computeVerticalScrollRange() > computeVerticalScrollExtent();
14953 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14954 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14955 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14956 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14957
14958 int newScrollX = scrollX + deltaX;
14959 if (!overScrollHorizontal) {
14960 maxOverScrollX = 0;
14961 }
14962
14963 int newScrollY = scrollY + deltaY;
14964 if (!overScrollVertical) {
14965 maxOverScrollY = 0;
14966 }
14967
14968 // Clamp values if at the limits and record
14969 final int left = -maxOverScrollX;
14970 final int right = maxOverScrollX + scrollRangeX;
14971 final int top = -maxOverScrollY;
14972 final int bottom = maxOverScrollY + scrollRangeY;
14973
14974 boolean clampedX = false;
14975 if (newScrollX > right) {
14976 newScrollX = right;
14977 clampedX = true;
14978 } else if (newScrollX < left) {
14979 newScrollX = left;
14980 clampedX = true;
14981 }
14982
14983 boolean clampedY = false;
14984 if (newScrollY > bottom) {
14985 newScrollY = bottom;
14986 clampedY = true;
14987 } else if (newScrollY < top) {
14988 newScrollY = top;
14989 clampedY = true;
14990 }
14991
14992 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14993
14994 return clampedX || clampedY;
14995 }
14996
14997 /**
14998 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14999 * respond to the results of an over-scroll operation.
15000 *
15001 * @param scrollX New X scroll value in pixels
15002 * @param scrollY New Y scroll value in pixels
15003 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15004 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15005 */
15006 protected void onOverScrolled(int scrollX, int scrollY,
15007 boolean clampedX, boolean clampedY) {
15008 // Intentionally empty.
15009 }
15010
15011 /**
15012 * Returns the over-scroll mode for this view. The result will be
15013 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15014 * (allow over-scrolling only if the view content is larger than the container),
15015 * or {@link #OVER_SCROLL_NEVER}.
15016 *
15017 * @return This view's over-scroll mode.
15018 */
15019 public int getOverScrollMode() {
15020 return mOverScrollMode;
15021 }
15022
15023 /**
15024 * Set the over-scroll mode for this view. Valid over-scroll modes are
15025 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15026 * (allow over-scrolling only if the view content is larger than the container),
15027 * or {@link #OVER_SCROLL_NEVER}.
15028 *
15029 * Setting the over-scroll mode of a view will have an effect only if the
15030 * view is capable of scrolling.
15031 *
15032 * @param overScrollMode The new over-scroll mode for this view.
15033 */
15034 public void setOverScrollMode(int overScrollMode) {
15035 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15036 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15037 overScrollMode != OVER_SCROLL_NEVER) {
15038 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15039 }
15040 mOverScrollMode = overScrollMode;
15041 }
15042
15043 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015044 * Gets a scale factor that determines the distance the view should scroll
15045 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15046 * @return The vertical scroll scale factor.
15047 * @hide
15048 */
15049 protected float getVerticalScrollFactor() {
15050 if (mVerticalScrollFactor == 0) {
15051 TypedValue outValue = new TypedValue();
15052 if (!mContext.getTheme().resolveAttribute(
15053 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15054 throw new IllegalStateException(
15055 "Expected theme to define listPreferredItemHeight.");
15056 }
15057 mVerticalScrollFactor = outValue.getDimension(
15058 mContext.getResources().getDisplayMetrics());
15059 }
15060 return mVerticalScrollFactor;
15061 }
15062
15063 /**
15064 * Gets a scale factor that determines the distance the view should scroll
15065 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15066 * @return The horizontal scroll scale factor.
15067 * @hide
15068 */
15069 protected float getHorizontalScrollFactor() {
15070 // TODO: Should use something else.
15071 return getVerticalScrollFactor();
15072 }
15073
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015074 /**
15075 * Return the value specifying the text direction or policy that was set with
15076 * {@link #setTextDirection(int)}.
15077 *
15078 * @return the defined text direction. It can be one of:
15079 *
15080 * {@link #TEXT_DIRECTION_INHERIT},
15081 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15082 * {@link #TEXT_DIRECTION_ANY_RTL},
15083 * {@link #TEXT_DIRECTION_LTR},
15084 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015085 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015086 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015087 @ViewDebug.ExportedProperty(category = "text", mapping = {
15088 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15089 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15090 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15091 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15092 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15093 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15094 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015095 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015096 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015097 }
15098
15099 /**
15100 * Set the text direction.
15101 *
15102 * @param textDirection the direction to set. Should be one of:
15103 *
15104 * {@link #TEXT_DIRECTION_INHERIT},
15105 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15106 * {@link #TEXT_DIRECTION_ANY_RTL},
15107 * {@link #TEXT_DIRECTION_LTR},
15108 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015109 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015110 */
15111 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015112 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015113 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015114 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015115 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015116 // Set the new text direction
15117 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015118 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015119 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015120 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015121 }
15122 }
15123
15124 /**
15125 * Return the resolved text direction.
15126 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015127 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15128 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15129 * up the parent chain of the view. if there is no parent, then it will return the default
15130 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15131 *
15132 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015133 *
Doug Feltcb3791202011-07-07 11:57:48 -070015134 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15135 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015136 * {@link #TEXT_DIRECTION_LTR},
15137 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015138 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015139 */
15140 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015141 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015142 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015143 resolveTextDirection();
15144 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015145 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015146 }
15147
15148 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015149 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15150 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015151 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015152 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015153 // Reset any previous text direction resolution
15154 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15155
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015156 if (hasRtlSupport()) {
15157 // Set resolved text direction flag depending on text direction flag
15158 final int textDirection = getTextDirection();
15159 switch(textDirection) {
15160 case TEXT_DIRECTION_INHERIT:
15161 if (canResolveTextDirection()) {
15162 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015163
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015164 // Set current resolved direction to the same value as the parent's one
15165 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15166 switch (parentResolvedDirection) {
15167 case TEXT_DIRECTION_FIRST_STRONG:
15168 case TEXT_DIRECTION_ANY_RTL:
15169 case TEXT_DIRECTION_LTR:
15170 case TEXT_DIRECTION_RTL:
15171 case TEXT_DIRECTION_LOCALE:
15172 mPrivateFlags2 |=
15173 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15174 break;
15175 default:
15176 // Default resolved direction is "first strong" heuristic
15177 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15178 }
15179 } else {
15180 // We cannot do the resolution if there is no parent, so use the default one
15181 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015182 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015183 break;
15184 case TEXT_DIRECTION_FIRST_STRONG:
15185 case TEXT_DIRECTION_ANY_RTL:
15186 case TEXT_DIRECTION_LTR:
15187 case TEXT_DIRECTION_RTL:
15188 case TEXT_DIRECTION_LOCALE:
15189 // Resolved direction is the same as text direction
15190 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15191 break;
15192 default:
15193 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015194 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015195 }
15196 } else {
15197 // Default resolved direction is "first strong" heuristic
15198 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015199 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015200
15201 // Set to resolved
15202 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015203 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015204 }
15205
15206 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015207 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015208 * resolution should override this method.
15209 *
15210 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015211 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015212 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015213 }
15214
15215 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015216 * Check if text direction resolution can be done.
15217 *
15218 * @return true if text direction resolution can be done otherwise return false.
15219 */
15220 public boolean canResolveTextDirection() {
15221 switch (getTextDirection()) {
15222 case TEXT_DIRECTION_INHERIT:
15223 return (mParent != null) && (mParent instanceof ViewGroup);
15224 default:
15225 return true;
15226 }
15227 }
15228
15229 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015230 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015231 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015232 * reset is done.
15233 */
15234 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015235 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015236 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015237 }
15238
15239 /**
15240 * Called when text direction is reset. Subclasses that care about text direction reset should
15241 * override this method and do a reset of the text direction of their children. The default
15242 * implementation does nothing.
15243 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015244 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015245 }
15246
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015247 /**
15248 * Return the value specifying the text alignment or policy that was set with
15249 * {@link #setTextAlignment(int)}.
15250 *
15251 * @return the defined text alignment. It can be one of:
15252 *
15253 * {@link #TEXT_ALIGNMENT_INHERIT},
15254 * {@link #TEXT_ALIGNMENT_GRAVITY},
15255 * {@link #TEXT_ALIGNMENT_CENTER},
15256 * {@link #TEXT_ALIGNMENT_TEXT_START},
15257 * {@link #TEXT_ALIGNMENT_TEXT_END},
15258 * {@link #TEXT_ALIGNMENT_VIEW_START},
15259 * {@link #TEXT_ALIGNMENT_VIEW_END}
15260 */
15261 @ViewDebug.ExportedProperty(category = "text", mapping = {
15262 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15263 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15264 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15265 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15266 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15267 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15268 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15269 })
15270 public int getTextAlignment() {
15271 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15272 }
15273
15274 /**
15275 * Set the text alignment.
15276 *
15277 * @param textAlignment The text alignment to set. Should be one of
15278 *
15279 * {@link #TEXT_ALIGNMENT_INHERIT},
15280 * {@link #TEXT_ALIGNMENT_GRAVITY},
15281 * {@link #TEXT_ALIGNMENT_CENTER},
15282 * {@link #TEXT_ALIGNMENT_TEXT_START},
15283 * {@link #TEXT_ALIGNMENT_TEXT_END},
15284 * {@link #TEXT_ALIGNMENT_VIEW_START},
15285 * {@link #TEXT_ALIGNMENT_VIEW_END}
15286 *
15287 * @attr ref android.R.styleable#View_textAlignment
15288 */
15289 public void setTextAlignment(int textAlignment) {
15290 if (textAlignment != getTextAlignment()) {
15291 // Reset the current and resolved text alignment
15292 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15293 resetResolvedTextAlignment();
15294 // Set the new text alignment
15295 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15296 // Refresh
15297 requestLayout();
15298 invalidate(true);
15299 }
15300 }
15301
15302 /**
15303 * Return the resolved text alignment.
15304 *
15305 * The resolved text alignment. This needs resolution if the value is
15306 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15307 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15308 *
15309 * @return the resolved text alignment. Returns one of:
15310 *
15311 * {@link #TEXT_ALIGNMENT_GRAVITY},
15312 * {@link #TEXT_ALIGNMENT_CENTER},
15313 * {@link #TEXT_ALIGNMENT_TEXT_START},
15314 * {@link #TEXT_ALIGNMENT_TEXT_END},
15315 * {@link #TEXT_ALIGNMENT_VIEW_START},
15316 * {@link #TEXT_ALIGNMENT_VIEW_END}
15317 */
15318 @ViewDebug.ExportedProperty(category = "text", mapping = {
15319 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15320 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15321 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15322 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15323 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15324 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15325 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15326 })
15327 public int getResolvedTextAlignment() {
15328 // If text alignment is not resolved, then resolve it
15329 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15330 resolveTextAlignment();
15331 }
15332 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15333 }
15334
15335 /**
15336 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15337 * resolution is done.
15338 */
15339 public void resolveTextAlignment() {
15340 // Reset any previous text alignment resolution
15341 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15342
15343 if (hasRtlSupport()) {
15344 // Set resolved text alignment flag depending on text alignment flag
15345 final int textAlignment = getTextAlignment();
15346 switch (textAlignment) {
15347 case TEXT_ALIGNMENT_INHERIT:
15348 // Check if we can resolve the text alignment
15349 if (canResolveLayoutDirection() && mParent instanceof View) {
15350 View view = (View) mParent;
15351
15352 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15353 switch (parentResolvedTextAlignment) {
15354 case TEXT_ALIGNMENT_GRAVITY:
15355 case TEXT_ALIGNMENT_TEXT_START:
15356 case TEXT_ALIGNMENT_TEXT_END:
15357 case TEXT_ALIGNMENT_CENTER:
15358 case TEXT_ALIGNMENT_VIEW_START:
15359 case TEXT_ALIGNMENT_VIEW_END:
15360 // Resolved text alignment is the same as the parent resolved
15361 // text alignment
15362 mPrivateFlags2 |=
15363 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15364 break;
15365 default:
15366 // Use default resolved text alignment
15367 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15368 }
15369 }
15370 else {
15371 // We cannot do the resolution if there is no parent so use the default
15372 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15373 }
15374 break;
15375 case TEXT_ALIGNMENT_GRAVITY:
15376 case TEXT_ALIGNMENT_TEXT_START:
15377 case TEXT_ALIGNMENT_TEXT_END:
15378 case TEXT_ALIGNMENT_CENTER:
15379 case TEXT_ALIGNMENT_VIEW_START:
15380 case TEXT_ALIGNMENT_VIEW_END:
15381 // Resolved text alignment is the same as text alignment
15382 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15383 break;
15384 default:
15385 // Use default resolved text alignment
15386 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15387 }
15388 } else {
15389 // Use default resolved text alignment
15390 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15391 }
15392
15393 // Set the resolved
15394 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
15395 onResolvedTextAlignmentChanged();
15396 }
15397
15398 /**
15399 * Check if text alignment resolution can be done.
15400 *
15401 * @return true if text alignment resolution can be done otherwise return false.
15402 */
15403 public boolean canResolveTextAlignment() {
15404 switch (getTextAlignment()) {
15405 case TEXT_DIRECTION_INHERIT:
15406 return (mParent != null);
15407 default:
15408 return true;
15409 }
15410 }
15411
15412 /**
15413 * Called when text alignment has been resolved. Subclasses that care about text alignment
15414 * resolution should override this method.
15415 *
15416 * The default implementation does nothing.
15417 */
15418 public void onResolvedTextAlignmentChanged() {
15419 }
15420
15421 /**
15422 * Reset resolved text alignment. Text alignment can be resolved with a call to
15423 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
15424 * reset is done.
15425 */
15426 public void resetResolvedTextAlignment() {
15427 // Reset any previous text alignment resolution
15428 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15429 onResolvedTextAlignmentReset();
15430 }
15431
15432 /**
15433 * Called when text alignment is reset. Subclasses that care about text alignment reset should
15434 * override this method and do a reset of the text alignment of their children. The default
15435 * implementation does nothing.
15436 */
15437 public void onResolvedTextAlignmentReset() {
15438 }
15439
Chet Haaseb39f0512011-05-24 14:36:40 -070015440 //
15441 // Properties
15442 //
15443 /**
15444 * A Property wrapper around the <code>alpha</code> functionality handled by the
15445 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
15446 */
Chet Haased47f1532011-12-16 11:18:52 -080015447 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015448 @Override
15449 public void setValue(View object, float value) {
15450 object.setAlpha(value);
15451 }
15452
15453 @Override
15454 public Float get(View object) {
15455 return object.getAlpha();
15456 }
15457 };
15458
15459 /**
15460 * A Property wrapper around the <code>translationX</code> functionality handled by the
15461 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
15462 */
Chet Haased47f1532011-12-16 11:18:52 -080015463 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015464 @Override
15465 public void setValue(View object, float value) {
15466 object.setTranslationX(value);
15467 }
15468
15469 @Override
15470 public Float get(View object) {
15471 return object.getTranslationX();
15472 }
15473 };
15474
15475 /**
15476 * A Property wrapper around the <code>translationY</code> functionality handled by the
15477 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
15478 */
Chet Haased47f1532011-12-16 11:18:52 -080015479 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015480 @Override
15481 public void setValue(View object, float value) {
15482 object.setTranslationY(value);
15483 }
15484
15485 @Override
15486 public Float get(View object) {
15487 return object.getTranslationY();
15488 }
15489 };
15490
15491 /**
15492 * A Property wrapper around the <code>x</code> functionality handled by the
15493 * {@link View#setX(float)} and {@link View#getX()} methods.
15494 */
Chet Haased47f1532011-12-16 11:18:52 -080015495 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015496 @Override
15497 public void setValue(View object, float value) {
15498 object.setX(value);
15499 }
15500
15501 @Override
15502 public Float get(View object) {
15503 return object.getX();
15504 }
15505 };
15506
15507 /**
15508 * A Property wrapper around the <code>y</code> functionality handled by the
15509 * {@link View#setY(float)} and {@link View#getY()} methods.
15510 */
Chet Haased47f1532011-12-16 11:18:52 -080015511 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015512 @Override
15513 public void setValue(View object, float value) {
15514 object.setY(value);
15515 }
15516
15517 @Override
15518 public Float get(View object) {
15519 return object.getY();
15520 }
15521 };
15522
15523 /**
15524 * A Property wrapper around the <code>rotation</code> functionality handled by the
15525 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
15526 */
Chet Haased47f1532011-12-16 11:18:52 -080015527 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015528 @Override
15529 public void setValue(View object, float value) {
15530 object.setRotation(value);
15531 }
15532
15533 @Override
15534 public Float get(View object) {
15535 return object.getRotation();
15536 }
15537 };
15538
15539 /**
15540 * A Property wrapper around the <code>rotationX</code> functionality handled by the
15541 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
15542 */
Chet Haased47f1532011-12-16 11:18:52 -080015543 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015544 @Override
15545 public void setValue(View object, float value) {
15546 object.setRotationX(value);
15547 }
15548
15549 @Override
15550 public Float get(View object) {
15551 return object.getRotationX();
15552 }
15553 };
15554
15555 /**
15556 * A Property wrapper around the <code>rotationY</code> functionality handled by the
15557 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
15558 */
Chet Haased47f1532011-12-16 11:18:52 -080015559 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015560 @Override
15561 public void setValue(View object, float value) {
15562 object.setRotationY(value);
15563 }
15564
15565 @Override
15566 public Float get(View object) {
15567 return object.getRotationY();
15568 }
15569 };
15570
15571 /**
15572 * A Property wrapper around the <code>scaleX</code> functionality handled by the
15573 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
15574 */
Chet Haased47f1532011-12-16 11:18:52 -080015575 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015576 @Override
15577 public void setValue(View object, float value) {
15578 object.setScaleX(value);
15579 }
15580
15581 @Override
15582 public Float get(View object) {
15583 return object.getScaleX();
15584 }
15585 };
15586
15587 /**
15588 * A Property wrapper around the <code>scaleY</code> functionality handled by the
15589 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
15590 */
Chet Haased47f1532011-12-16 11:18:52 -080015591 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015592 @Override
15593 public void setValue(View object, float value) {
15594 object.setScaleY(value);
15595 }
15596
15597 @Override
15598 public Float get(View object) {
15599 return object.getScaleY();
15600 }
15601 };
15602
Jeff Brown33bbfd22011-02-24 20:55:35 -080015603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015604 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
15605 * Each MeasureSpec represents a requirement for either the width or the height.
15606 * A MeasureSpec is comprised of a size and a mode. There are three possible
15607 * modes:
15608 * <dl>
15609 * <dt>UNSPECIFIED</dt>
15610 * <dd>
15611 * The parent has not imposed any constraint on the child. It can be whatever size
15612 * it wants.
15613 * </dd>
15614 *
15615 * <dt>EXACTLY</dt>
15616 * <dd>
15617 * The parent has determined an exact size for the child. The child is going to be
15618 * given those bounds regardless of how big it wants to be.
15619 * </dd>
15620 *
15621 * <dt>AT_MOST</dt>
15622 * <dd>
15623 * The child can be as large as it wants up to the specified size.
15624 * </dd>
15625 * </dl>
15626 *
15627 * MeasureSpecs are implemented as ints to reduce object allocation. This class
15628 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
15629 */
15630 public static class MeasureSpec {
15631 private static final int MODE_SHIFT = 30;
15632 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
15633
15634 /**
15635 * Measure specification mode: The parent has not imposed any constraint
15636 * on the child. It can be whatever size it wants.
15637 */
15638 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
15639
15640 /**
15641 * Measure specification mode: The parent has determined an exact size
15642 * for the child. The child is going to be given those bounds regardless
15643 * of how big it wants to be.
15644 */
15645 public static final int EXACTLY = 1 << MODE_SHIFT;
15646
15647 /**
15648 * Measure specification mode: The child can be as large as it wants up
15649 * to the specified size.
15650 */
15651 public static final int AT_MOST = 2 << MODE_SHIFT;
15652
15653 /**
15654 * Creates a measure specification based on the supplied size and mode.
15655 *
15656 * The mode must always be one of the following:
15657 * <ul>
15658 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
15659 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
15660 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
15661 * </ul>
15662 *
15663 * @param size the size of the measure specification
15664 * @param mode the mode of the measure specification
15665 * @return the measure specification based on size and mode
15666 */
15667 public static int makeMeasureSpec(int size, int mode) {
15668 return size + mode;
15669 }
15670
15671 /**
15672 * Extracts the mode from the supplied measure specification.
15673 *
15674 * @param measureSpec the measure specification to extract the mode from
15675 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
15676 * {@link android.view.View.MeasureSpec#AT_MOST} or
15677 * {@link android.view.View.MeasureSpec#EXACTLY}
15678 */
15679 public static int getMode(int measureSpec) {
15680 return (measureSpec & MODE_MASK);
15681 }
15682
15683 /**
15684 * Extracts the size from the supplied measure specification.
15685 *
15686 * @param measureSpec the measure specification to extract the size from
15687 * @return the size in pixels defined in the supplied measure specification
15688 */
15689 public static int getSize(int measureSpec) {
15690 return (measureSpec & ~MODE_MASK);
15691 }
15692
15693 /**
15694 * Returns a String representation of the specified measure
15695 * specification.
15696 *
15697 * @param measureSpec the measure specification to convert to a String
15698 * @return a String with the following format: "MeasureSpec: MODE SIZE"
15699 */
15700 public static String toString(int measureSpec) {
15701 int mode = getMode(measureSpec);
15702 int size = getSize(measureSpec);
15703
15704 StringBuilder sb = new StringBuilder("MeasureSpec: ");
15705
15706 if (mode == UNSPECIFIED)
15707 sb.append("UNSPECIFIED ");
15708 else if (mode == EXACTLY)
15709 sb.append("EXACTLY ");
15710 else if (mode == AT_MOST)
15711 sb.append("AT_MOST ");
15712 else
15713 sb.append(mode).append(" ");
15714
15715 sb.append(size);
15716 return sb.toString();
15717 }
15718 }
15719
15720 class CheckForLongPress implements Runnable {
15721
15722 private int mOriginalWindowAttachCount;
15723
15724 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070015725 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015726 && mOriginalWindowAttachCount == mWindowAttachCount) {
15727 if (performLongClick()) {
15728 mHasPerformedLongPress = true;
15729 }
15730 }
15731 }
15732
15733 public void rememberWindowAttachCount() {
15734 mOriginalWindowAttachCount = mWindowAttachCount;
15735 }
15736 }
Joe Malin32736f02011-01-19 16:14:20 -080015737
Adam Powelle14579b2009-12-16 18:39:52 -080015738 private final class CheckForTap implements Runnable {
15739 public void run() {
15740 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080015741 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015742 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080015743 }
15744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015745
Adam Powella35d7682010-03-12 14:48:13 -080015746 private final class PerformClick implements Runnable {
15747 public void run() {
15748 performClick();
15749 }
15750 }
15751
Dianne Hackborn63042d62011-01-26 18:56:29 -080015752 /** @hide */
15753 public void hackTurnOffWindowResizeAnim(boolean off) {
15754 mAttachInfo.mTurnOffWindowResizeAnim = off;
15755 }
Joe Malin32736f02011-01-19 16:14:20 -080015756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015757 /**
Chet Haasea00f3862011-02-22 06:34:40 -080015758 * This method returns a ViewPropertyAnimator object, which can be used to animate
15759 * specific properties on this View.
15760 *
15761 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15762 */
15763 public ViewPropertyAnimator animate() {
15764 if (mAnimator == null) {
15765 mAnimator = new ViewPropertyAnimator(this);
15766 }
15767 return mAnimator;
15768 }
15769
15770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015771 * Interface definition for a callback to be invoked when a key event is
15772 * dispatched to this view. The callback will be invoked before the key
15773 * event is given to the view.
15774 */
15775 public interface OnKeyListener {
15776 /**
15777 * Called when a key is dispatched to a view. This allows listeners to
15778 * get a chance to respond before the target view.
15779 *
15780 * @param v The view the key has been dispatched to.
15781 * @param keyCode The code for the physical key that was pressed
15782 * @param event The KeyEvent object containing full information about
15783 * the event.
15784 * @return True if the listener has consumed the event, false otherwise.
15785 */
15786 boolean onKey(View v, int keyCode, KeyEvent event);
15787 }
15788
15789 /**
15790 * Interface definition for a callback to be invoked when a touch event is
15791 * dispatched to this view. The callback will be invoked before the touch
15792 * event is given to the view.
15793 */
15794 public interface OnTouchListener {
15795 /**
15796 * Called when a touch event is dispatched to a view. This allows listeners to
15797 * get a chance to respond before the target view.
15798 *
15799 * @param v The view the touch event has been dispatched to.
15800 * @param event The MotionEvent object containing full information about
15801 * the event.
15802 * @return True if the listener has consumed the event, false otherwise.
15803 */
15804 boolean onTouch(View v, MotionEvent event);
15805 }
15806
15807 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015808 * Interface definition for a callback to be invoked when a hover event is
15809 * dispatched to this view. The callback will be invoked before the hover
15810 * event is given to the view.
15811 */
15812 public interface OnHoverListener {
15813 /**
15814 * Called when a hover event is dispatched to a view. This allows listeners to
15815 * get a chance to respond before the target view.
15816 *
15817 * @param v The view the hover event has been dispatched to.
15818 * @param event The MotionEvent object containing full information about
15819 * the event.
15820 * @return True if the listener has consumed the event, false otherwise.
15821 */
15822 boolean onHover(View v, MotionEvent event);
15823 }
15824
15825 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015826 * Interface definition for a callback to be invoked when a generic motion event is
15827 * dispatched to this view. The callback will be invoked before the generic motion
15828 * event is given to the view.
15829 */
15830 public interface OnGenericMotionListener {
15831 /**
15832 * Called when a generic motion event is dispatched to a view. This allows listeners to
15833 * get a chance to respond before the target view.
15834 *
15835 * @param v The view the generic motion event has been dispatched to.
15836 * @param event The MotionEvent object containing full information about
15837 * the event.
15838 * @return True if the listener has consumed the event, false otherwise.
15839 */
15840 boolean onGenericMotion(View v, MotionEvent event);
15841 }
15842
15843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015844 * Interface definition for a callback to be invoked when a view has been clicked and held.
15845 */
15846 public interface OnLongClickListener {
15847 /**
15848 * Called when a view has been clicked and held.
15849 *
15850 * @param v The view that was clicked and held.
15851 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015852 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015853 */
15854 boolean onLongClick(View v);
15855 }
15856
15857 /**
Chris Tate32affef2010-10-18 15:29:21 -070015858 * Interface definition for a callback to be invoked when a drag is being dispatched
15859 * to this view. The callback will be invoked before the hosting view's own
15860 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15861 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015862 *
15863 * <div class="special reference">
15864 * <h3>Developer Guides</h3>
15865 * <p>For a guide to implementing drag and drop features, read the
15866 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15867 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015868 */
15869 public interface OnDragListener {
15870 /**
15871 * Called when a drag event is dispatched to a view. This allows listeners
15872 * to get a chance to override base View behavior.
15873 *
Joe Malin32736f02011-01-19 16:14:20 -080015874 * @param v The View that received the drag event.
15875 * @param event The {@link android.view.DragEvent} object for the drag event.
15876 * @return {@code true} if the drag event was handled successfully, or {@code false}
15877 * if the drag event was not handled. Note that {@code false} will trigger the View
15878 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015879 */
15880 boolean onDrag(View v, DragEvent event);
15881 }
15882
15883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015884 * Interface definition for a callback to be invoked when the focus state of
15885 * a view changed.
15886 */
15887 public interface OnFocusChangeListener {
15888 /**
15889 * Called when the focus state of a view has changed.
15890 *
15891 * @param v The view whose state has changed.
15892 * @param hasFocus The new focus state of v.
15893 */
15894 void onFocusChange(View v, boolean hasFocus);
15895 }
15896
15897 /**
15898 * Interface definition for a callback to be invoked when a view is clicked.
15899 */
15900 public interface OnClickListener {
15901 /**
15902 * Called when a view has been clicked.
15903 *
15904 * @param v The view that was clicked.
15905 */
15906 void onClick(View v);
15907 }
15908
15909 /**
15910 * Interface definition for a callback to be invoked when the context menu
15911 * for this view is being built.
15912 */
15913 public interface OnCreateContextMenuListener {
15914 /**
15915 * Called when the context menu for this view is being built. It is not
15916 * safe to hold onto the menu after this method returns.
15917 *
15918 * @param menu The context menu that is being built
15919 * @param v The view for which the context menu is being built
15920 * @param menuInfo Extra information about the item for which the
15921 * context menu should be shown. This information will vary
15922 * depending on the class of v.
15923 */
15924 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15925 }
15926
Joe Onorato664644d2011-01-23 17:53:23 -080015927 /**
15928 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015929 * visibility. This reports <strong>global</strong> changes to the system UI
15930 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015931 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015932 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015933 */
15934 public interface OnSystemUiVisibilityChangeListener {
15935 /**
15936 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015937 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015938 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015939 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015940 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15941 * <strong>global</strong> state of the UI visibility flags, not what your
15942 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015943 */
15944 public void onSystemUiVisibilityChange(int visibility);
15945 }
15946
Adam Powell4afd62b2011-02-18 15:02:18 -080015947 /**
15948 * Interface definition for a callback to be invoked when this view is attached
15949 * or detached from its window.
15950 */
15951 public interface OnAttachStateChangeListener {
15952 /**
15953 * Called when the view is attached to a window.
15954 * @param v The view that was attached
15955 */
15956 public void onViewAttachedToWindow(View v);
15957 /**
15958 * Called when the view is detached from a window.
15959 * @param v The view that was detached
15960 */
15961 public void onViewDetachedFromWindow(View v);
15962 }
15963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015964 private final class UnsetPressedState implements Runnable {
15965 public void run() {
15966 setPressed(false);
15967 }
15968 }
15969
15970 /**
15971 * Base class for derived classes that want to save and restore their own
15972 * state in {@link android.view.View#onSaveInstanceState()}.
15973 */
15974 public static class BaseSavedState extends AbsSavedState {
15975 /**
15976 * Constructor used when reading from a parcel. Reads the state of the superclass.
15977 *
15978 * @param source
15979 */
15980 public BaseSavedState(Parcel source) {
15981 super(source);
15982 }
15983
15984 /**
15985 * Constructor called by derived classes when creating their SavedState objects
15986 *
15987 * @param superState The state of the superclass of this view
15988 */
15989 public BaseSavedState(Parcelable superState) {
15990 super(superState);
15991 }
15992
15993 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15994 new Parcelable.Creator<BaseSavedState>() {
15995 public BaseSavedState createFromParcel(Parcel in) {
15996 return new BaseSavedState(in);
15997 }
15998
15999 public BaseSavedState[] newArray(int size) {
16000 return new BaseSavedState[size];
16001 }
16002 };
16003 }
16004
16005 /**
16006 * A set of information given to a view when it is attached to its parent
16007 * window.
16008 */
16009 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016010 interface Callbacks {
16011 void playSoundEffect(int effectId);
16012 boolean performHapticFeedback(int effectId, boolean always);
16013 }
16014
16015 /**
16016 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16017 * to a Handler. This class contains the target (View) to invalidate and
16018 * the coordinates of the dirty rectangle.
16019 *
16020 * For performance purposes, this class also implements a pool of up to
16021 * POOL_LIMIT objects that get reused. This reduces memory allocations
16022 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016023 */
Romain Guyd928d682009-03-31 17:52:16 -070016024 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016025 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016026 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16027 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016028 public InvalidateInfo newInstance() {
16029 return new InvalidateInfo();
16030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016031
Romain Guyd928d682009-03-31 17:52:16 -070016032 public void onAcquired(InvalidateInfo element) {
16033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016034
Romain Guyd928d682009-03-31 17:52:16 -070016035 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016036 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016037 }
16038 }, POOL_LIMIT)
16039 );
16040
16041 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016042 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016043
16044 View target;
16045
16046 int left;
16047 int top;
16048 int right;
16049 int bottom;
16050
Romain Guyd928d682009-03-31 17:52:16 -070016051 public void setNextPoolable(InvalidateInfo element) {
16052 mNext = element;
16053 }
16054
16055 public InvalidateInfo getNextPoolable() {
16056 return mNext;
16057 }
16058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016059 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016060 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016061 }
16062
16063 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016064 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016065 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016066
16067 public boolean isPooled() {
16068 return mIsPooled;
16069 }
16070
16071 public void setPooled(boolean isPooled) {
16072 mIsPooled = isPooled;
16073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016074 }
16075
16076 final IWindowSession mSession;
16077
16078 final IWindow mWindow;
16079
16080 final IBinder mWindowToken;
16081
16082 final Callbacks mRootCallbacks;
16083
Romain Guy59a12ca2011-06-09 17:48:21 -070016084 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016086 /**
16087 * The top view of the hierarchy.
16088 */
16089 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016091 IBinder mPanelParentWindowToken;
16092 Surface mSurface;
16093
Romain Guyb051e892010-09-28 19:09:36 -070016094 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016095 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016096 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016097
Romain Guy7e4e5612012-03-05 14:37:29 -080016098 boolean mScreenOn;
16099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016100 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016101 * Scale factor used by the compatibility mode
16102 */
16103 float mApplicationScale;
16104
16105 /**
16106 * Indicates whether the application is in compatibility mode
16107 */
16108 boolean mScalingRequired;
16109
16110 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016111 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016112 */
16113 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016114
Dianne Hackborn63042d62011-01-26 18:56:29 -080016115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016116 * Left position of this view's window
16117 */
16118 int mWindowLeft;
16119
16120 /**
16121 * Top position of this view's window
16122 */
16123 int mWindowTop;
16124
16125 /**
Adam Powell26153a32010-11-08 15:22:27 -080016126 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016127 */
Adam Powell26153a32010-11-08 15:22:27 -080016128 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016129
16130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016131 * For windows that are full-screen but using insets to layout inside
16132 * of the screen decorations, these are the current insets for the
16133 * content of the window.
16134 */
16135 final Rect mContentInsets = new Rect();
16136
16137 /**
16138 * For windows that are full-screen but using insets to layout inside
16139 * of the screen decorations, these are the current insets for the
16140 * actual visible parts of the window.
16141 */
16142 final Rect mVisibleInsets = new Rect();
16143
16144 /**
16145 * The internal insets given by this window. This value is
16146 * supplied by the client (through
16147 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16148 * be given to the window manager when changed to be used in laying
16149 * out windows behind it.
16150 */
16151 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16152 = new ViewTreeObserver.InternalInsetsInfo();
16153
16154 /**
16155 * All views in the window's hierarchy that serve as scroll containers,
16156 * used to determine if the window can be resized or must be panned
16157 * to adjust for a soft input area.
16158 */
16159 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16160
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016161 final KeyEvent.DispatcherState mKeyDispatchState
16162 = new KeyEvent.DispatcherState();
16163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016164 /**
16165 * Indicates whether the view's window currently has the focus.
16166 */
16167 boolean mHasWindowFocus;
16168
16169 /**
16170 * The current visibility of the window.
16171 */
16172 int mWindowVisibility;
16173
16174 /**
16175 * Indicates the time at which drawing started to occur.
16176 */
16177 long mDrawingTime;
16178
16179 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016180 * Indicates whether or not ignoring the DIRTY_MASK flags.
16181 */
16182 boolean mIgnoreDirtyState;
16183
16184 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016185 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16186 * to avoid clearing that flag prematurely.
16187 */
16188 boolean mSetIgnoreDirtyState = false;
16189
16190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016191 * Indicates whether the view's window is currently in touch mode.
16192 */
16193 boolean mInTouchMode;
16194
16195 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016196 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016197 * the next time it performs a traversal
16198 */
16199 boolean mRecomputeGlobalAttributes;
16200
16201 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016202 * Always report new attributes at next traversal.
16203 */
16204 boolean mForceReportNewAttributes;
16205
16206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016207 * Set during a traveral if any views want to keep the screen on.
16208 */
16209 boolean mKeepScreenOn;
16210
16211 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016212 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16213 */
16214 int mSystemUiVisibility;
16215
16216 /**
16217 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16218 * attached.
16219 */
16220 boolean mHasSystemUiListeners;
16221
16222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016223 * Set if the visibility of any views has changed.
16224 */
16225 boolean mViewVisibilityChanged;
16226
16227 /**
16228 * Set to true if a view has been scrolled.
16229 */
16230 boolean mViewScrollChanged;
16231
16232 /**
16233 * Global to the view hierarchy used as a temporary for dealing with
16234 * x/y points in the transparent region computations.
16235 */
16236 final int[] mTransparentLocation = new int[2];
16237
16238 /**
16239 * Global to the view hierarchy used as a temporary for dealing with
16240 * x/y points in the ViewGroup.invalidateChild implementation.
16241 */
16242 final int[] mInvalidateChildLocation = new int[2];
16243
Chet Haasec3aa3612010-06-17 08:50:37 -070016244
16245 /**
16246 * Global to the view hierarchy used as a temporary for dealing with
16247 * x/y location when view is transformed.
16248 */
16249 final float[] mTmpTransformLocation = new float[2];
16250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016251 /**
16252 * The view tree observer used to dispatch global events like
16253 * layout, pre-draw, touch mode change, etc.
16254 */
16255 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16256
16257 /**
16258 * A Canvas used by the view hierarchy to perform bitmap caching.
16259 */
16260 Canvas mCanvas;
16261
16262 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016263 * The view root impl.
16264 */
16265 final ViewRootImpl mViewRootImpl;
16266
16267 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016268 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016269 * handler can be used to pump events in the UI events queue.
16270 */
16271 final Handler mHandler;
16272
16273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016274 * Temporary for use in computing invalidate rectangles while
16275 * calling up the hierarchy.
16276 */
16277 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016278
16279 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016280 * Temporary for use in computing hit areas with transformed views
16281 */
16282 final RectF mTmpTransformRect = new RectF();
16283
16284 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016285 * Temporary list for use in collecting focusable descendents of a view.
16286 */
16287 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
16288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016289 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016290 * The id of the window for accessibility purposes.
16291 */
16292 int mAccessibilityWindowId = View.NO_ID;
16293
16294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016295 * Creates a new set of attachment information with the specified
16296 * events handler and thread.
16297 *
16298 * @param handler the events handler the view must use
16299 */
16300 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016301 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016302 mSession = session;
16303 mWindow = window;
16304 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016305 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016306 mHandler = handler;
16307 mRootCallbacks = effectPlayer;
16308 }
16309 }
16310
16311 /**
16312 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16313 * is supported. This avoids keeping too many unused fields in most
16314 * instances of View.</p>
16315 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016316 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016317
Mike Cleronf116bf82009-09-27 19:14:12 -070016318 /**
16319 * Scrollbars are not visible
16320 */
16321 public static final int OFF = 0;
16322
16323 /**
16324 * Scrollbars are visible
16325 */
16326 public static final int ON = 1;
16327
16328 /**
16329 * Scrollbars are fading away
16330 */
16331 public static final int FADING = 2;
16332
16333 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016335 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016336 public int scrollBarDefaultDelayBeforeFade;
16337 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016338
16339 public int scrollBarSize;
16340 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016341 public float[] interpolatorValues;
16342 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016343
16344 public final Paint paint;
16345 public final Matrix matrix;
16346 public Shader shader;
16347
Mike Cleronf116bf82009-09-27 19:14:12 -070016348 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16349
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016350 private static final float[] OPAQUE = { 255 };
16351 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016352
Mike Cleronf116bf82009-09-27 19:14:12 -070016353 /**
16354 * When fading should start. This time moves into the future every time
16355 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16356 */
16357 public long fadeStartTime;
16358
16359
16360 /**
16361 * The current state of the scrollbars: ON, OFF, or FADING
16362 */
16363 public int state = OFF;
16364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016365 private int mLastColor;
16366
Mike Cleronf116bf82009-09-27 19:14:12 -070016367 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016368 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16369 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016370 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16371 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016372
16373 paint = new Paint();
16374 matrix = new Matrix();
16375 // use use a height of 1, and then wack the matrix each time we
16376 // actually use it.
16377 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016379 paint.setShader(shader);
16380 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070016381 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016382 }
Romain Guy8506ab42009-06-11 17:35:47 -070016383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016384 public void setFadeColor(int color) {
16385 if (color != 0 && color != mLastColor) {
16386 mLastColor = color;
16387 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070016388
Romain Guye55e1a72009-08-27 10:42:26 -070016389 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
16390 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016392 paint.setShader(shader);
16393 // Restore the default transfer mode (src_over)
16394 paint.setXfermode(null);
16395 }
16396 }
Joe Malin32736f02011-01-19 16:14:20 -080016397
Mike Cleronf116bf82009-09-27 19:14:12 -070016398 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070016399 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070016400 if (now >= fadeStartTime) {
16401
16402 // the animation fades the scrollbars out by changing
16403 // the opacity (alpha) from fully opaque to fully
16404 // transparent
16405 int nextFrame = (int) now;
16406 int framesCount = 0;
16407
16408 Interpolator interpolator = scrollBarInterpolator;
16409
16410 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016411 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070016412
16413 // End transparent
16414 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016415 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070016416
16417 state = FADING;
16418
16419 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080016420 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070016421 }
16422 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070016423 }
Mike Cleronf116bf82009-09-27 19:14:12 -070016424
Svetoslav Ganova0156172011-06-26 17:55:44 -070016425 /**
16426 * Resuable callback for sending
16427 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
16428 */
16429 private class SendViewScrolledAccessibilityEvent implements Runnable {
16430 public volatile boolean mIsPending;
16431
16432 public void run() {
16433 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
16434 mIsPending = false;
16435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016436 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016437
16438 /**
16439 * <p>
16440 * This class represents a delegate that can be registered in a {@link View}
16441 * to enhance accessibility support via composition rather via inheritance.
16442 * It is specifically targeted to widget developers that extend basic View
16443 * classes i.e. classes in package android.view, that would like their
16444 * applications to be backwards compatible.
16445 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080016446 * <div class="special reference">
16447 * <h3>Developer Guides</h3>
16448 * <p>For more information about making applications accessible, read the
16449 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
16450 * developer guide.</p>
16451 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016452 * <p>
16453 * A scenario in which a developer would like to use an accessibility delegate
16454 * is overriding a method introduced in a later API version then the minimal API
16455 * version supported by the application. For example, the method
16456 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
16457 * in API version 4 when the accessibility APIs were first introduced. If a
16458 * developer would like his application to run on API version 4 devices (assuming
16459 * all other APIs used by the application are version 4 or lower) and take advantage
16460 * of this method, instead of overriding the method which would break the application's
16461 * backwards compatibility, he can override the corresponding method in this
16462 * delegate and register the delegate in the target View if the API version of
16463 * the system is high enough i.e. the API version is same or higher to the API
16464 * version that introduced
16465 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
16466 * </p>
16467 * <p>
16468 * Here is an example implementation:
16469 * </p>
16470 * <code><pre><p>
16471 * if (Build.VERSION.SDK_INT >= 14) {
16472 * // If the API version is equal of higher than the version in
16473 * // which onInitializeAccessibilityNodeInfo was introduced we
16474 * // register a delegate with a customized implementation.
16475 * View view = findViewById(R.id.view_id);
16476 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
16477 * public void onInitializeAccessibilityNodeInfo(View host,
16478 * AccessibilityNodeInfo info) {
16479 * // Let the default implementation populate the info.
16480 * super.onInitializeAccessibilityNodeInfo(host, info);
16481 * // Set some other information.
16482 * info.setEnabled(host.isEnabled());
16483 * }
16484 * });
16485 * }
16486 * </code></pre></p>
16487 * <p>
16488 * This delegate contains methods that correspond to the accessibility methods
16489 * in View. If a delegate has been specified the implementation in View hands
16490 * off handling to the corresponding method in this delegate. The default
16491 * implementation the delegate methods behaves exactly as the corresponding
16492 * method in View for the case of no accessibility delegate been set. Hence,
16493 * to customize the behavior of a View method, clients can override only the
16494 * corresponding delegate method without altering the behavior of the rest
16495 * accessibility related methods of the host view.
16496 * </p>
16497 */
16498 public static class AccessibilityDelegate {
16499
16500 /**
16501 * Sends an accessibility event of the given type. If accessibility is not
16502 * enabled this method has no effect.
16503 * <p>
16504 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
16505 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
16506 * been set.
16507 * </p>
16508 *
16509 * @param host The View hosting the delegate.
16510 * @param eventType The type of the event to send.
16511 *
16512 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
16513 */
16514 public void sendAccessibilityEvent(View host, int eventType) {
16515 host.sendAccessibilityEventInternal(eventType);
16516 }
16517
16518 /**
16519 * Sends an accessibility event. This method behaves exactly as
16520 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
16521 * empty {@link AccessibilityEvent} and does not perform a check whether
16522 * accessibility is enabled.
16523 * <p>
16524 * The default implementation behaves as
16525 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16526 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
16527 * the case of no accessibility delegate been set.
16528 * </p>
16529 *
16530 * @param host The View hosting the delegate.
16531 * @param event The event to send.
16532 *
16533 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16534 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16535 */
16536 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
16537 host.sendAccessibilityEventUncheckedInternal(event);
16538 }
16539
16540 /**
16541 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
16542 * to its children for adding their text content to the event.
16543 * <p>
16544 * The default implementation behaves as
16545 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16546 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
16547 * the case of no accessibility delegate been set.
16548 * </p>
16549 *
16550 * @param host The View hosting the delegate.
16551 * @param event The event.
16552 * @return True if the event population was completed.
16553 *
16554 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16555 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16556 */
16557 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16558 return host.dispatchPopulateAccessibilityEventInternal(event);
16559 }
16560
16561 /**
16562 * Gives a chance to the host View to populate the accessibility event with its
16563 * text content.
16564 * <p>
16565 * The default implementation behaves as
16566 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
16567 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
16568 * the case of no accessibility delegate been set.
16569 * </p>
16570 *
16571 * @param host The View hosting the delegate.
16572 * @param event The accessibility event which to populate.
16573 *
16574 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
16575 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
16576 */
16577 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16578 host.onPopulateAccessibilityEventInternal(event);
16579 }
16580
16581 /**
16582 * Initializes an {@link AccessibilityEvent} with information about the
16583 * the host View which is the event source.
16584 * <p>
16585 * The default implementation behaves as
16586 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
16587 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
16588 * the case of no accessibility delegate been set.
16589 * </p>
16590 *
16591 * @param host The View hosting the delegate.
16592 * @param event The event to initialize.
16593 *
16594 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
16595 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
16596 */
16597 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
16598 host.onInitializeAccessibilityEventInternal(event);
16599 }
16600
16601 /**
16602 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
16603 * <p>
16604 * The default implementation behaves as
16605 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16606 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
16607 * the case of no accessibility delegate been set.
16608 * </p>
16609 *
16610 * @param host The View hosting the delegate.
16611 * @param info The instance to initialize.
16612 *
16613 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16614 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16615 */
16616 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
16617 host.onInitializeAccessibilityNodeInfoInternal(info);
16618 }
16619
16620 /**
16621 * Called when a child of the host View has requested sending an
16622 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
16623 * to augment the event.
16624 * <p>
16625 * The default implementation behaves as
16626 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16627 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
16628 * the case of no accessibility delegate been set.
16629 * </p>
16630 *
16631 * @param host The View hosting the delegate.
16632 * @param child The child which requests sending the event.
16633 * @param event The event to be sent.
16634 * @return True if the event should be sent
16635 *
16636 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16637 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16638 */
16639 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
16640 AccessibilityEvent event) {
16641 return host.onRequestSendAccessibilityEventInternal(child, event);
16642 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070016643
16644 /**
16645 * Gets the provider for managing a virtual view hierarchy rooted at this View
16646 * and reported to {@link android.accessibilityservice.AccessibilityService}s
16647 * that explore the window content.
16648 * <p>
16649 * The default implementation behaves as
16650 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
16651 * the case of no accessibility delegate been set.
16652 * </p>
16653 *
16654 * @return The provider.
16655 *
16656 * @see AccessibilityNodeProvider
16657 */
16658 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
16659 return null;
16660 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016662}