blob: a833cbefa621da713f4e55fe92ba058d6e1fd660 [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;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070049import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070065import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070066import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070068import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080069import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
74
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070076import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070077
Doug Feltcb3791202011-07-07 11:57:48 -070078import com.android.internal.R;
79import com.android.internal.util.Predicate;
80import com.android.internal.view.menu.MenuBuilder;
81
Christopher Tatea0374192010-10-05 13:06:41 -070082import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070083import java.lang.reflect.InvocationTargetException;
84import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.ArrayList;
86import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070087import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080088import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90/**
91 * <p>
92 * This class represents the basic building block for user interface components. A View
93 * occupies a rectangular area on the screen and is responsible for drawing and
94 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070095 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
97 * are invisible containers that hold other Views (or other ViewGroups) and define
98 * their layout properties.
99 * </p>
100 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For information about using this class to develop your application's user interface,
104 * 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 -0800105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700132 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700176 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700195 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700202 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700249 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700342 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
343 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
345 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * </p>
347 *
348 * <p>
349 * Even though a view can define a padding, it does not provide any support for
350 * margins. However, view groups provide such a support. Refer to
351 * {@link android.view.ViewGroup} and
352 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
353 * </p>
354 *
355 * <a name="Layout"></a>
356 * <h3>Layout</h3>
357 * <p>
358 * Layout is a two pass process: a measure pass and a layout pass. The measuring
359 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
360 * of the view tree. Each view pushes dimension specifications down the tree
361 * during the recursion. At the end of the measure pass, every view has stored
362 * its measurements. The second pass happens in
363 * {@link #layout(int,int,int,int)} and is also top-down. During
364 * this pass each parent is responsible for positioning all of its children
365 * using the sizes computed in the measure pass.
366 * </p>
367 *
368 * <p>
369 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
370 * {@link #getMeasuredHeight()} values must be set, along with those for all of
371 * that view's descendants. A view's measured width and measured height values
372 * must respect the constraints imposed by the view's parents. This guarantees
373 * that at the end of the measure pass, all parents accept all of their
374 * children's measurements. A parent view may call measure() more than once on
375 * its children. For example, the parent may measure each child once with
376 * unspecified dimensions to find out how big they want to be, then call
377 * measure() on them again with actual numbers if the sum of all the children's
378 * unconstrained sizes is too big or too small.
379 * </p>
380 *
381 * <p>
382 * The measure pass uses two classes to communicate dimensions. The
383 * {@link MeasureSpec} class is used by views to tell their parents how they
384 * want to be measured and positioned. The base LayoutParams class just
385 * describes how big the view wants to be for both width and height. For each
386 * dimension, it can specify one of:
387 * <ul>
388 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800389 * <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 -0800390 * (minus padding)
391 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
392 * enclose its content (plus padding).
393 * </ul>
394 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
395 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
396 * an X and Y value.
397 * </p>
398 *
399 * <p>
400 * MeasureSpecs are used to push requirements down the tree from parent to
401 * child. A MeasureSpec can be in one of three modes:
402 * <ul>
403 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
404 * of a child view. For example, a LinearLayout may call measure() on its child
405 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
406 * tall the child view wants to be given a width of 240 pixels.
407 * <li>EXACTLY: This is used by the parent to impose an exact size on the
408 * child. The child must use this size, and guarantee that all of its
409 * descendants will fit within this size.
410 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
411 * child. The child must gurantee that it and all of its descendants will fit
412 * within this size.
413 * </ul>
414 * </p>
415 *
416 * <p>
417 * To intiate a layout, call {@link #requestLayout}. This method is typically
418 * called by a view on itself when it believes that is can no longer fit within
419 * its current bounds.
420 * </p>
421 *
422 * <a name="Drawing"></a>
423 * <h3>Drawing</h3>
424 * <p>
425 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700426 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * this means that parents will draw before (i.e., behind) their children, with
428 * siblings drawn in the order they appear in the tree.
429 * If you set a background drawable for a View, then the View will draw it for you
430 * before calling back to its <code>onDraw()</code> method.
431 * </p>
432 *
433 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700434 * 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 -0800435 * </p>
436 *
437 * <p>
438 * To force a view to draw, call {@link #invalidate()}.
439 * </p>
440 *
441 * <a name="EventHandlingThreading"></a>
442 * <h3>Event Handling and Threading</h3>
443 * <p>
444 * The basic cycle of a view is as follows:
445 * <ol>
446 * <li>An event comes in and is dispatched to the appropriate view. The view
447 * handles the event and notifies any listeners.</li>
448 * <li>If in the course of processing the event, the view's bounds may need
449 * to be changed, the view will call {@link #requestLayout()}.</li>
450 * <li>Similarly, if in the course of processing the event the view's appearance
451 * may need to be changed, the view will call {@link #invalidate()}.</li>
452 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
453 * the framework will take care of measuring, laying out, and drawing the tree
454 * as appropriate.</li>
455 * </ol>
456 * </p>
457 *
458 * <p><em>Note: The entire view tree is single threaded. You must always be on
459 * the UI thread when calling any method on any view.</em>
460 * If you are doing work on other threads and want to update the state of a view
461 * from that thread, you should use a {@link Handler}.
462 * </p>
463 *
464 * <a name="FocusHandling"></a>
465 * <h3>Focus Handling</h3>
466 * <p>
467 * The framework will handle routine focus movement in response to user input.
468 * This includes changing the focus as views are removed or hidden, or as new
469 * views become available. Views indicate their willingness to take focus
470 * through the {@link #isFocusable} method. To change whether a view can take
471 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
472 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
473 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
474 * </p>
475 * <p>
476 * Focus movement is based on an algorithm which finds the nearest neighbor in a
477 * given direction. In rare cases, the default algorithm may not match the
478 * intended behavior of the developer. In these situations, you can provide
479 * explicit overrides by using these XML attributes in the layout file:
480 * <pre>
481 * nextFocusDown
482 * nextFocusLeft
483 * nextFocusRight
484 * nextFocusUp
485 * </pre>
486 * </p>
487 *
488 *
489 * <p>
490 * To get a particular view to take focus, call {@link #requestFocus()}.
491 * </p>
492 *
493 * <a name="TouchMode"></a>
494 * <h3>Touch Mode</h3>
495 * <p>
496 * When a user is navigating a user interface via directional keys such as a D-pad, it is
497 * necessary to give focus to actionable items such as buttons so the user can see
498 * what will take input. If the device has touch capabilities, however, and the user
499 * begins interacting with the interface by touching it, it is no longer necessary to
500 * always highlight, or give focus to, a particular view. This motivates a mode
501 * for interaction named 'touch mode'.
502 * </p>
503 * <p>
504 * For a touch capable device, once the user touches the screen, the device
505 * will enter touch mode. From this point onward, only views for which
506 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
507 * Other views that are touchable, like buttons, will not take focus when touched; they will
508 * only fire the on click listeners.
509 * </p>
510 * <p>
511 * Any time a user hits a directional key, such as a D-pad direction, the view device will
512 * exit touch mode, and find a view to take focus, so that the user may resume interacting
513 * with the user interface without touching the screen again.
514 * </p>
515 * <p>
516 * The touch mode state is maintained across {@link android.app.Activity}s. Call
517 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
518 * </p>
519 *
520 * <a name="Scrolling"></a>
521 * <h3>Scrolling</h3>
522 * <p>
523 * The framework provides basic support for views that wish to internally
524 * scroll their content. This includes keeping track of the X and Y scroll
525 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800526 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700527 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 * </p>
529 *
530 * <a name="Tags"></a>
531 * <h3>Tags</h3>
532 * <p>
533 * Unlike IDs, tags are not used to identify views. Tags are essentially an
534 * extra piece of information that can be associated with a view. They are most
535 * often used as a convenience to store data related to views in the views
536 * themselves rather than by putting them in a separate structure.
537 * </p>
538 *
539 * <a name="Animation"></a>
540 * <h3>Animation</h3>
541 * <p>
542 * You can attach an {@link Animation} object to a view using
543 * {@link #setAnimation(Animation)} or
544 * {@link #startAnimation(Animation)}. The animation can alter the scale,
545 * rotation, translation and alpha of a view over time. If the animation is
546 * attached to a view that has children, the animation will affect the entire
547 * subtree rooted by that node. When an animation is started, the framework will
548 * take care of redrawing the appropriate views until the animation completes.
549 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800550 * <p>
551 * Starting with Android 3.0, the preferred way of animating views is to use the
552 * {@link android.animation} package APIs.
553 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 *
Jeff Brown85a31762010-09-01 17:01:00 -0700555 * <a name="Security"></a>
556 * <h3>Security</h3>
557 * <p>
558 * Sometimes it is essential that an application be able to verify that an action
559 * is being performed with the full knowledge and consent of the user, such as
560 * granting a permission request, making a purchase or clicking on an advertisement.
561 * Unfortunately, a malicious application could try to spoof the user into
562 * performing these actions, unaware, by concealing the intended purpose of the view.
563 * As a remedy, the framework offers a touch filtering mechanism that can be used to
564 * improve the security of views that provide access to sensitive functionality.
565 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700566 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800567 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700568 * will discard touches that are received whenever the view's window is obscured by
569 * another visible window. As a result, the view will not receive touches whenever a
570 * toast, dialog or other window appears above the view's window.
571 * </p><p>
572 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700573 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
574 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700575 * </p>
576 *
Romain Guy171c5922011-01-06 10:04:23 -0800577 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700578 * @attr ref android.R.styleable#View_background
579 * @attr ref android.R.styleable#View_clickable
580 * @attr ref android.R.styleable#View_contentDescription
581 * @attr ref android.R.styleable#View_drawingCacheQuality
582 * @attr ref android.R.styleable#View_duplicateParentState
583 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700584 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700585 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700586 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700587 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700589 * @attr ref android.R.styleable#View_isScrollContainer
590 * @attr ref android.R.styleable#View_focusable
591 * @attr ref android.R.styleable#View_focusableInTouchMode
592 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
593 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800594 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700595 * @attr ref android.R.styleable#View_longClickable
596 * @attr ref android.R.styleable#View_minHeight
597 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @attr ref android.R.styleable#View_nextFocusDown
599 * @attr ref android.R.styleable#View_nextFocusLeft
600 * @attr ref android.R.styleable#View_nextFocusRight
601 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_onClick
603 * @attr ref android.R.styleable#View_padding
604 * @attr ref android.R.styleable#View_paddingBottom
605 * @attr ref android.R.styleable#View_paddingLeft
606 * @attr ref android.R.styleable#View_paddingRight
607 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800608 * @attr ref android.R.styleable#View_paddingStart
609 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800611 * @attr ref android.R.styleable#View_rotation
612 * @attr ref android.R.styleable#View_rotationX
613 * @attr ref android.R.styleable#View_rotationY
614 * @attr ref android.R.styleable#View_scaleX
615 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_scrollX
617 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_scrollbarSize
619 * @attr ref android.R.styleable#View_scrollbarStyle
620 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700621 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
622 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
624 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarThumbVertical
626 * @attr ref android.R.styleable#View_scrollbarTrackVertical
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_soundEffectsEnabled
630 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700631 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800632 * @attr ref android.R.styleable#View_transformPivotX
633 * @attr ref android.R.styleable#View_transformPivotY
634 * @attr ref android.R.styleable#View_translationX
635 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700636 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 *
638 * @see android.view.ViewGroup
639 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700640public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
641 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private static final boolean DBG = false;
643
644 /**
645 * The logging tag used by this class with android.util.Log.
646 */
647 protected static final String VIEW_LOG_TAG = "View";
648
649 /**
650 * Used to mark a View that has no ID.
651 */
652 public static final int NO_ID = -1;
653
654 /**
655 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
656 * calling setFlags.
657 */
658 private static final int NOT_FOCUSABLE = 0x00000000;
659
660 /**
661 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
662 * setFlags.
663 */
664 private static final int FOCUSABLE = 0x00000001;
665
666 /**
667 * Mask for use with setFlags indicating bits used for focus.
668 */
669 private static final int FOCUSABLE_MASK = 0x00000001;
670
671 /**
672 * This view will adjust its padding to fit sytem windows (e.g. status bar)
673 */
674 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
675
676 /**
Scott Main812634c22011-07-27 13:22:35 -0700677 * This view is visible.
678 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
679 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 */
681 public static final int VISIBLE = 0x00000000;
682
683 /**
684 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700685 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
686 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 */
688 public static final int INVISIBLE = 0x00000004;
689
690 /**
691 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700692 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
693 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 */
695 public static final int GONE = 0x00000008;
696
697 /**
698 * Mask for use with setFlags indicating bits used for visibility.
699 * {@hide}
700 */
701 static final int VISIBILITY_MASK = 0x0000000C;
702
703 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
704
705 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700706 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 * Use with ENABLED_MASK when calling setFlags.
708 * {@hide}
709 */
710 static final int ENABLED = 0x00000000;
711
712 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700713 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * Use with ENABLED_MASK when calling setFlags.
715 * {@hide}
716 */
717 static final int DISABLED = 0x00000020;
718
719 /**
720 * Mask for use with setFlags indicating bits used for indicating whether
721 * this view is enabled
722 * {@hide}
723 */
724 static final int ENABLED_MASK = 0x00000020;
725
726 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700727 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
728 * called and further optimizations will be performed. It is okay to have
729 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 * {@hide}
731 */
732 static final int WILL_NOT_DRAW = 0x00000080;
733
734 /**
735 * Mask for use with setFlags indicating bits used for indicating whether
736 * this view is will draw
737 * {@hide}
738 */
739 static final int DRAW_MASK = 0x00000080;
740
741 /**
742 * <p>This view doesn't show scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_NONE = 0x00000000;
746
747 /**
748 * <p>This view shows horizontal scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
752
753 /**
754 * <p>This view shows vertical scrollbars.</p>
755 * {@hide}
756 */
757 static final int SCROLLBARS_VERTICAL = 0x00000200;
758
759 /**
760 * <p>Mask for use with setFlags indicating bits used for indicating which
761 * scrollbars are enabled.</p>
762 * {@hide}
763 */
764 static final int SCROLLBARS_MASK = 0x00000300;
765
Jeff Brown85a31762010-09-01 17:01:00 -0700766 /**
767 * Indicates that the view should filter touches when its window is obscured.
768 * Refer to the class comments for more information about this security feature.
769 * {@hide}
770 */
771 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
772
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700773 /**
774 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
775 * that they are optional and should be skipped if the window has
776 * requested system UI flags that ignore those insets for layout.
777 */
778 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
780 /**
781 * <p>This view doesn't show fading edges.</p>
782 * {@hide}
783 */
784 static final int FADING_EDGE_NONE = 0x00000000;
785
786 /**
787 * <p>This view shows horizontal fading edges.</p>
788 * {@hide}
789 */
790 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
791
792 /**
793 * <p>This view shows vertical fading edges.</p>
794 * {@hide}
795 */
796 static final int FADING_EDGE_VERTICAL = 0x00002000;
797
798 /**
799 * <p>Mask for use with setFlags indicating bits used for indicating which
800 * fading edges are enabled.</p>
801 * {@hide}
802 */
803 static final int FADING_EDGE_MASK = 0x00003000;
804
805 /**
806 * <p>Indicates this view can be clicked. When clickable, a View reacts
807 * to clicks by notifying the OnClickListener.<p>
808 * {@hide}
809 */
810 static final int CLICKABLE = 0x00004000;
811
812 /**
813 * <p>Indicates this view is caching its drawing into a bitmap.</p>
814 * {@hide}
815 */
816 static final int DRAWING_CACHE_ENABLED = 0x00008000;
817
818 /**
819 * <p>Indicates that no icicle should be saved for this view.<p>
820 * {@hide}
821 */
822 static final int SAVE_DISABLED = 0x000010000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
826 * property.</p>
827 * {@hide}
828 */
829 static final int SAVE_DISABLED_MASK = 0x000010000;
830
831 /**
832 * <p>Indicates that no drawing cache should ever be created for this view.<p>
833 * {@hide}
834 */
835 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
836
837 /**
838 * <p>Indicates this view can take / keep focus when int touch mode.</p>
839 * {@hide}
840 */
841 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
842
843 /**
844 * <p>Enables low quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
847
848 /**
849 * <p>Enables high quality mode for the drawing cache.</p>
850 */
851 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
852
853 /**
854 * <p>Enables automatic quality mode for the drawing cache.</p>
855 */
856 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
857
858 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
859 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
860 };
861
862 /**
863 * <p>Mask for use with setFlags indicating bits used for the cache
864 * quality property.</p>
865 * {@hide}
866 */
867 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
868
869 /**
870 * <p>
871 * Indicates this view can be long clicked. When long clickable, a View
872 * reacts to long clicks by notifying the OnLongClickListener or showing a
873 * context menu.
874 * </p>
875 * {@hide}
876 */
877 static final int LONG_CLICKABLE = 0x00200000;
878
879 /**
880 * <p>Indicates that this view gets its drawable states from its direct parent
881 * and ignores its original internal states.</p>
882 *
883 * @hide
884 */
885 static final int DUPLICATE_PARENT_STATE = 0x00400000;
886
887 /**
888 * The scrollbar style to display the scrollbars inside the content area,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency on the view's content.
891 */
892 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
893
894 /**
895 * The scrollbar style to display the scrollbars inside the padded area,
896 * increasing the padding of the view. The scrollbars will not overlap the
897 * content area of the view.
898 */
899 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
900
901 /**
902 * The scrollbar style to display the scrollbars at the edge of the view,
903 * without increasing the padding. The scrollbars will be overlaid with
904 * translucency.
905 */
906 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
907
908 /**
909 * The scrollbar style to display the scrollbars at the edge of the view,
910 * increasing the padding of the view. The scrollbars will only overlap the
911 * background, if any.
912 */
913 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
914
915 /**
916 * Mask to check if the scrollbar style is overlay or inset.
917 * {@hide}
918 */
919 static final int SCROLLBARS_INSET_MASK = 0x01000000;
920
921 /**
922 * Mask to check if the scrollbar style is inside or outside.
923 * {@hide}
924 */
925 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
926
927 /**
928 * Mask for scrollbar style.
929 * {@hide}
930 */
931 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
932
933 /**
934 * View flag indicating that the screen should remain on while the
935 * window containing this view is visible to the user. This effectively
936 * takes care of automatically setting the WindowManager's
937 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
938 */
939 public static final int KEEP_SCREEN_ON = 0x04000000;
940
941 /**
942 * View flag indicating whether this view should have sound effects enabled
943 * for events such as clicking and touching.
944 */
945 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
946
947 /**
948 * View flag indicating whether this view should have haptic feedback
949 * enabled for events such as long presses.
950 */
951 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
952
953 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700954 * <p>Indicates that the view hierarchy should stop saving state when
955 * it reaches this view. If state saving is initiated immediately at
956 * the view, it will be allowed.
957 * {@hide}
958 */
959 static final int PARENT_SAVE_DISABLED = 0x20000000;
960
961 /**
962 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
963 * {@hide}
964 */
965 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
966
967 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700968 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
969 * should add all focusable Views regardless if they are focusable in touch mode.
970 */
971 public static final int FOCUSABLES_ALL = 0x00000000;
972
973 /**
974 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
975 * should add only Views focusable in touch mode.
976 */
977 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
978
979 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700980 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
981 * should add only accessibility focusable Views.
982 *
983 * @hide
984 */
985 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
986
987 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700988 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 * item.
990 */
991 public static final int FOCUS_BACKWARD = 0x00000001;
992
993 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700994 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * item.
996 */
997 public static final int FOCUS_FORWARD = 0x00000002;
998
999 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001000 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public static final int FOCUS_LEFT = 0x00000011;
1003
1004 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001005 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 */
1007 public static final int FOCUS_UP = 0x00000021;
1008
1009 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001010 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 */
1012 public static final int FOCUS_RIGHT = 0x00000042;
1013
1014 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001015 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 */
1017 public static final int FOCUS_DOWN = 0x00000082;
1018
Svetoslav Ganov42138042012-03-20 11:51:39 -07001019 // Accessibility focus directions.
1020
1021 /**
1022 * The accessibility focus which is the current user position when
1023 * interacting with the accessibility framework.
1024 */
1025 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1026
1027 /**
1028 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1029 */
1030 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1031
1032 /**
1033 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1034 */
1035 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1036
1037 /**
1038 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1039 */
1040 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1041
1042 /**
1043 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1044 */
1045 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1046
1047 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001048 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001049 */
1050 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1051
1052 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001053 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001054 */
1055 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1056
1057 /**
1058 * Use with {@link #focusSearch(int)}. Move acessibility focus in a view.
1059 */
1060 public static final int ACCESSIBILITY_FOCUS_IN = 0x00000004 | FOCUS_ACCESSIBILITY;
1061
1062 /**
1063 * Use with {@link #focusSearch(int)}. Move acessibility focus out of a view.
1064 */
1065 public static final int ACCESSIBILITY_FOCUS_OUT = 0x00000008 | FOCUS_ACCESSIBILITY;
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001068 * Use with {@link #focusSearch(int)}. Move acessibility focus to the next view.
1069 */
1070 public static final int ACCESSIBILITY_FOCUS_NEXT = 0x00000010 | FOCUS_ACCESSIBILITY;
1071
1072 /**
1073 * Use with {@link #focusSearch(int)}. Move acessibility focus to the previous view.
1074 */
1075 public static final int ACCESSIBILITY_FOCUS_PREVIOUS = 0x00000020 | FOCUS_ACCESSIBILITY;
1076
1077 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001078 * Bits of {@link #getMeasuredWidthAndState()} and
1079 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1080 */
1081 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1082
1083 /**
1084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1086 */
1087 public static final int MEASURED_STATE_MASK = 0xff000000;
1088
1089 /**
1090 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1091 * for functions that combine both width and height into a single int,
1092 * such as {@link #getMeasuredState()} and the childState argument of
1093 * {@link #resolveSizeAndState(int, int, int)}.
1094 */
1095 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1096
1097 /**
1098 * Bit of {@link #getMeasuredWidthAndState()} and
1099 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1100 * is smaller that the space the view would like to have.
1101 */
1102 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1103
1104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 * Base View state sets
1106 */
1107 // Singles
1108 /**
1109 * Indicates the view has no states set. States are used with
1110 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1111 * view depending on its state.
1112 *
1113 * @see android.graphics.drawable.Drawable
1114 * @see #getDrawableState()
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is enabled. States are used with
1119 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1120 * view depending on its state.
1121 *
1122 * @see android.graphics.drawable.Drawable
1123 * @see #getDrawableState()
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Indicates the view is focused. States are used with
1128 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1129 * view depending on its state.
1130 *
1131 * @see android.graphics.drawable.Drawable
1132 * @see #getDrawableState()
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is selected. States are used with
1137 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1138 * view depending on its state.
1139 *
1140 * @see android.graphics.drawable.Drawable
1141 * @see #getDrawableState()
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is pressed. States are used with
1146 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1147 * view depending on its state.
1148 *
1149 * @see android.graphics.drawable.Drawable
1150 * @see #getDrawableState()
1151 * @hide
1152 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001153 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Indicates the view's window has focus. States are used with
1156 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1157 * view depending on its state.
1158 *
1159 * @see android.graphics.drawable.Drawable
1160 * @see #getDrawableState()
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 // Doubles
1164 /**
1165 * Indicates the view is enabled and has the focus.
1166 *
1167 * @see #ENABLED_STATE_SET
1168 * @see #FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is enabled and selected.
1173 *
1174 * @see #ENABLED_STATE_SET
1175 * @see #SELECTED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 /**
1179 * Indicates the view is enabled and that its window has focus.
1180 *
1181 * @see #ENABLED_STATE_SET
1182 * @see #WINDOW_FOCUSED_STATE_SET
1183 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001184 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 /**
1186 * Indicates the view is focused and selected.
1187 *
1188 * @see #FOCUSED_STATE_SET
1189 * @see #SELECTED_STATE_SET
1190 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001191 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 /**
1193 * Indicates the view has the focus and that its window has the focus.
1194 *
1195 * @see #FOCUSED_STATE_SET
1196 * @see #WINDOW_FOCUSED_STATE_SET
1197 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001198 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 /**
1200 * Indicates the view is selected and that its window has the focus.
1201 *
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 // Triples
1207 /**
1208 * Indicates the view is enabled, focused and selected.
1209 *
1210 * @see #ENABLED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 * @see #SELECTED_STATE_SET
1213 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001214 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /**
1216 * Indicates the view is enabled, focused and its window has the focus.
1217 *
1218 * @see #ENABLED_STATE_SET
1219 * @see #FOCUSED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is enabled, selected and its window has the focus.
1225 *
1226 * @see #ENABLED_STATE_SET
1227 * @see #SELECTED_STATE_SET
1228 * @see #WINDOW_FOCUSED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is focused, selected and its window has the focus.
1233 *
1234 * @see #FOCUSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 * @see #WINDOW_FOCUSED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is enabled, focused, selected and its window
1241 * has the focus.
1242 *
1243 * @see #ENABLED_STATE_SET
1244 * @see #FOCUSED_STATE_SET
1245 * @see #SELECTED_STATE_SET
1246 * @see #WINDOW_FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed and its window has the focus.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed and selected.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #SELECTED_STATE_SET
1261 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001262 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 /**
1264 * Indicates the view is pressed, selected and its window has the focus.
1265 *
1266 * @see #PRESSED_STATE_SET
1267 * @see #SELECTED_STATE_SET
1268 * @see #WINDOW_FOCUSED_STATE_SET
1269 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001270 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 /**
1272 * Indicates the view is pressed and focused.
1273 *
1274 * @see #PRESSED_STATE_SET
1275 * @see #FOCUSED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed, focused and its window has the focus.
1280 *
1281 * @see #PRESSED_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_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, focused and selected.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #SELECTED_STATE_SET
1291 * @see #FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 /**
1295 * Indicates the view is pressed, focused, selected and its window has the focus.
1296 *
1297 * @see #PRESSED_STATE_SET
1298 * @see #FOCUSED_STATE_SET
1299 * @see #SELECTED_STATE_SET
1300 * @see #WINDOW_FOCUSED_STATE_SET
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 /**
1304 * Indicates the view is pressed and enabled.
1305 *
1306 * @see #PRESSED_STATE_SET
1307 * @see #ENABLED_STATE_SET
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 /**
1311 * Indicates the view is pressed, enabled and its window has the focus.
1312 *
1313 * @see #PRESSED_STATE_SET
1314 * @see #ENABLED_STATE_SET
1315 * @see #WINDOW_FOCUSED_STATE_SET
1316 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 /**
1319 * Indicates the view is pressed, enabled and selected.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #ENABLED_STATE_SET
1323 * @see #SELECTED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled, selected and its window has the
1328 * focus.
1329 *
1330 * @see #PRESSED_STATE_SET
1331 * @see #ENABLED_STATE_SET
1332 * @see #SELECTED_STATE_SET
1333 * @see #WINDOW_FOCUSED_STATE_SET
1334 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 /**
1337 * Indicates the view is pressed, enabled and focused.
1338 *
1339 * @see #PRESSED_STATE_SET
1340 * @see #ENABLED_STATE_SET
1341 * @see #FOCUSED_STATE_SET
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 /**
1345 * Indicates the view is pressed, enabled, focused and its window has the
1346 * focus.
1347 *
1348 * @see #PRESSED_STATE_SET
1349 * @see #ENABLED_STATE_SET
1350 * @see #FOCUSED_STATE_SET
1351 * @see #WINDOW_FOCUSED_STATE_SET
1352 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001353 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 /**
1355 * Indicates the view is pressed, enabled, focused and selected.
1356 *
1357 * @see #PRESSED_STATE_SET
1358 * @see #ENABLED_STATE_SET
1359 * @see #SELECTED_STATE_SET
1360 * @see #FOCUSED_STATE_SET
1361 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001362 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 /**
1364 * Indicates the view is pressed, enabled, focused, selected and its window
1365 * has the focus.
1366 *
1367 * @see #PRESSED_STATE_SET
1368 * @see #ENABLED_STATE_SET
1369 * @see #SELECTED_STATE_SET
1370 * @see #FOCUSED_STATE_SET
1371 * @see #WINDOW_FOCUSED_STATE_SET
1372 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001373 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374
1375 /**
1376 * The order here is very important to {@link #getDrawableState()}
1377 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 private static final int[][] VIEW_STATE_SETS;
1379
Romain Guyb051e892010-09-28 19:09:36 -07001380 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1381 static final int VIEW_STATE_SELECTED = 1 << 1;
1382 static final int VIEW_STATE_FOCUSED = 1 << 2;
1383 static final int VIEW_STATE_ENABLED = 1 << 3;
1384 static final int VIEW_STATE_PRESSED = 1 << 4;
1385 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001386 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001387 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001388 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1389 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001390
1391 static final int[] VIEW_STATE_IDS = new int[] {
1392 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1393 R.attr.state_selected, VIEW_STATE_SELECTED,
1394 R.attr.state_focused, VIEW_STATE_FOCUSED,
1395 R.attr.state_enabled, VIEW_STATE_ENABLED,
1396 R.attr.state_pressed, VIEW_STATE_PRESSED,
1397 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001398 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001399 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001400 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001401 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 };
1403
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001404 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001405 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1406 throw new IllegalStateException(
1407 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1408 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001409 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001410 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001411 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001412 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001413 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001414 orderedIds[i * 2] = viewState;
1415 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001416 }
1417 }
1418 }
Romain Guyb051e892010-09-28 19:09:36 -07001419 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1420 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1421 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 int numBits = Integer.bitCount(i);
1423 int[] set = new int[numBits];
1424 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001425 for (int j = 0; j < orderedIds.length; j += 2) {
1426 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001427 set[pos++] = orderedIds[j];
1428 }
1429 }
1430 VIEW_STATE_SETS[i] = set;
1431 }
1432
1433 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1434 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1435 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1436 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1438 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1439 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1441 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1443 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1445 | VIEW_STATE_FOCUSED];
1446 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1447 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1449 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1451 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1453 | VIEW_STATE_ENABLED];
1454 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1456 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1458 | VIEW_STATE_ENABLED];
1459 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1461 | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1464 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1465
1466 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1467 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1469 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1471 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_PRESSED];
1474 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1476 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1477 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1478 | VIEW_STATE_PRESSED];
1479 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1480 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1481 | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1484 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1485 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1487 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1488 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1489 | VIEW_STATE_PRESSED];
1490 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1491 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1492 | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1495 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1504 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1507 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1508 | VIEW_STATE_PRESSED];
1509 }
1510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001512 * Accessibility event types that are dispatched for text population.
1513 */
1514 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1515 AccessibilityEvent.TYPE_VIEW_CLICKED
1516 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1517 | AccessibilityEvent.TYPE_VIEW_SELECTED
1518 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1519 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1520 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001521 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001522 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001523 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1524 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001525
1526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 * Temporary Rect currently for use in setBackground(). This will probably
1528 * be extended in the future to hold our own class with more than just
1529 * a Rect. :)
1530 */
1531 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001532
1533 /**
1534 * Map used to store views' tags.
1535 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001536 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001539 * The next available accessiiblity id.
1540 */
1541 private static int sNextAccessibilityViewId;
1542
1543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 * The animation currently associated with this view.
1545 * @hide
1546 */
1547 protected Animation mCurrentAnimation = null;
1548
1549 /**
1550 * Width as measured during measure pass.
1551 * {@hide}
1552 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001553 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001554 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555
1556 /**
1557 * Height as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001564 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1565 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1566 * its display list. This flag, used only when hw accelerated, allows us to clear the
1567 * flag while retaining this information until it's needed (at getDisplayList() time and
1568 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1569 *
1570 * {@hide}
1571 */
1572 boolean mRecreateDisplayList = false;
1573
1574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 * The view's identifier.
1576 * {@hide}
1577 *
1578 * @see #setId(int)
1579 * @see #getId()
1580 */
1581 @ViewDebug.ExportedProperty(resolveId = true)
1582 int mID = NO_ID;
1583
1584 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001585 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001586 */
1587 int mAccessibilityViewId = NO_ID;
1588
1589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 * The view's tag.
1591 * {@hide}
1592 *
1593 * @see #setTag(Object)
1594 * @see #getTag()
1595 */
1596 protected Object mTag;
1597
1598 // for mPrivateFlags:
1599 /** {@hide} */
1600 static final int WANTS_FOCUS = 0x00000001;
1601 /** {@hide} */
1602 static final int FOCUSED = 0x00000002;
1603 /** {@hide} */
1604 static final int SELECTED = 0x00000004;
1605 /** {@hide} */
1606 static final int IS_ROOT_NAMESPACE = 0x00000008;
1607 /** {@hide} */
1608 static final int HAS_BOUNDS = 0x00000010;
1609 /** {@hide} */
1610 static final int DRAWN = 0x00000020;
1611 /**
1612 * When this flag is set, this view is running an animation on behalf of its
1613 * children and should therefore not cancel invalidate requests, even if they
1614 * lie outside of this view's bounds.
1615 *
1616 * {@hide}
1617 */
1618 static final int DRAW_ANIMATION = 0x00000040;
1619 /** {@hide} */
1620 static final int SKIP_DRAW = 0x00000080;
1621 /** {@hide} */
1622 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1623 /** {@hide} */
1624 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1625 /** {@hide} */
1626 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1627 /** {@hide} */
1628 static final int MEASURED_DIMENSION_SET = 0x00000800;
1629 /** {@hide} */
1630 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001631 /** {@hide} */
1632 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633
1634 private static final int PRESSED = 0x00004000;
1635
1636 /** {@hide} */
1637 static final int DRAWING_CACHE_VALID = 0x00008000;
1638 /**
1639 * Flag used to indicate that this view should be drawn once more (and only once
1640 * more) after its animation has completed.
1641 * {@hide}
1642 */
1643 static final int ANIMATION_STARTED = 0x00010000;
1644
1645 private static final int SAVE_STATE_CALLED = 0x00020000;
1646
1647 /**
1648 * Indicates that the View returned true when onSetAlpha() was called and that
1649 * the alpha must be restored.
1650 * {@hide}
1651 */
1652 static final int ALPHA_SET = 0x00040000;
1653
1654 /**
1655 * Set by {@link #setScrollContainer(boolean)}.
1656 */
1657 static final int SCROLL_CONTAINER = 0x00080000;
1658
1659 /**
1660 * Set by {@link #setScrollContainer(boolean)}.
1661 */
1662 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1663
1664 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001665 * View flag indicating whether this view was invalidated (fully or partially.)
1666 *
1667 * @hide
1668 */
1669 static final int DIRTY = 0x00200000;
1670
1671 /**
1672 * View flag indicating whether this view was invalidated by an opaque
1673 * invalidate request.
1674 *
1675 * @hide
1676 */
1677 static final int DIRTY_OPAQUE = 0x00400000;
1678
1679 /**
1680 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1681 *
1682 * @hide
1683 */
1684 static final int DIRTY_MASK = 0x00600000;
1685
1686 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001687 * Indicates whether the background is opaque.
1688 *
1689 * @hide
1690 */
1691 static final int OPAQUE_BACKGROUND = 0x00800000;
1692
1693 /**
1694 * Indicates whether the scrollbars are opaque.
1695 *
1696 * @hide
1697 */
1698 static final int OPAQUE_SCROLLBARS = 0x01000000;
1699
1700 /**
1701 * Indicates whether the view is opaque.
1702 *
1703 * @hide
1704 */
1705 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001706
Adam Powelle14579b2009-12-16 18:39:52 -08001707 /**
1708 * Indicates a prepressed state;
1709 * the short time between ACTION_DOWN and recognizing
1710 * a 'real' press. Prepressed is used to recognize quick taps
1711 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001712 *
Adam Powelle14579b2009-12-16 18:39:52 -08001713 * @hide
1714 */
1715 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001716
Adam Powellc9fbaab2010-02-16 17:16:19 -08001717 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001718 * Indicates whether the view is temporarily detached.
1719 *
1720 * @hide
1721 */
1722 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001723
Adam Powell8568c3a2010-04-19 14:26:11 -07001724 /**
1725 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001726 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001727 * @hide
1728 */
1729 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001730
1731 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001732 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1733 * @hide
1734 */
1735 private static final int HOVERED = 0x10000000;
1736
1737 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001738 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1739 * for transform operations
1740 *
1741 * @hide
1742 */
Adam Powellf37df072010-09-17 16:22:49 -07001743 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001744
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001745 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001746 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001747
Chet Haasefd2b0022010-08-06 13:08:56 -07001748 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001749 * Indicates that this view was specifically invalidated, not just dirtied because some
1750 * child view was invalidated. The flag is used to determine when we need to recreate
1751 * a view's display list (as opposed to just returning a reference to its existing
1752 * display list).
1753 *
1754 * @hide
1755 */
1756 static final int INVALIDATED = 0x80000000;
1757
Christopher Tate3d4bf172011-03-28 16:16:46 -07001758 /* Masks for mPrivateFlags2 */
1759
1760 /**
1761 * Indicates that this view has reported that it can accept the current drag's content.
1762 * Cleared when the drag operation concludes.
1763 * @hide
1764 */
1765 static final int DRAG_CAN_ACCEPT = 0x00000001;
1766
1767 /**
1768 * Indicates that this view is currently directly under the drag location in a
1769 * drag-and-drop operation involving content that it can accept. Cleared when
1770 * the drag exits the view, or when the drag operation concludes.
1771 * @hide
1772 */
1773 static final int DRAG_HOVERED = 0x00000002;
1774
Cibu Johny86666632010-02-22 13:01:02 -08001775 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001776 * Horizontal layout direction of this view is from Left to Right.
1777 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001778 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001779 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001780
1781 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001782 * Horizontal layout direction of this view is from Right to Left.
1783 * Use with {@link #setLayoutDirection}.
1784 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001785 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001786
1787 /**
1788 * Horizontal layout direction of this view is inherited from its parent.
1789 * Use with {@link #setLayoutDirection}.
1790 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001791 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001792
1793 /**
1794 * Horizontal layout direction of this view is from deduced from the default language
1795 * script for the locale. Use with {@link #setLayoutDirection}.
1796 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001797 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798
1799 /**
1800 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001801 * @hide
1802 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001803 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1804
1805 /**
1806 * Mask for use with private flags indicating bits used for horizontal layout direction.
1807 * @hide
1808 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001809 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001810
1811 /**
1812 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1813 * right-to-left direction.
1814 * @hide
1815 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001816 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001817
1818 /**
1819 * Indicates whether the view horizontal layout direction has been resolved.
1820 * @hide
1821 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001822 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001823
1824 /**
1825 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1826 * @hide
1827 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001828 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001829
1830 /*
1831 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1832 * flag value.
1833 * @hide
1834 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001835 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1836 LAYOUT_DIRECTION_LTR,
1837 LAYOUT_DIRECTION_RTL,
1838 LAYOUT_DIRECTION_INHERIT,
1839 LAYOUT_DIRECTION_LOCALE
1840 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001841
1842 /**
1843 * Default horizontal layout direction.
1844 * @hide
1845 */
1846 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001847
Adam Powell539ee872012-02-03 19:00:49 -08001848 /**
1849 * Indicates that the view is tracking some sort of transient state
1850 * that the app should not need to be aware of, but that the framework
1851 * should take special care to preserve.
1852 *
1853 * @hide
1854 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001855 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001856
1857
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001858 /**
1859 * Text direction is inherited thru {@link ViewGroup}
1860 */
1861 public static final int TEXT_DIRECTION_INHERIT = 0;
1862
1863 /**
1864 * Text direction is using "first strong algorithm". The first strong directional character
1865 * determines the paragraph direction. If there is no strong directional character, the
1866 * paragraph direction is the view's resolved layout direction.
1867 */
1868 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1869
1870 /**
1871 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1872 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1873 * If there are neither, the paragraph direction is the view's resolved layout direction.
1874 */
1875 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1876
1877 /**
1878 * Text direction is forced to LTR.
1879 */
1880 public static final int TEXT_DIRECTION_LTR = 3;
1881
1882 /**
1883 * Text direction is forced to RTL.
1884 */
1885 public static final int TEXT_DIRECTION_RTL = 4;
1886
1887 /**
1888 * Text direction is coming from the system Locale.
1889 */
1890 public static final int TEXT_DIRECTION_LOCALE = 5;
1891
1892 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001893 * Default text direction is inherited
1894 */
1895 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1896
1897 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001898 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1899 * @hide
1900 */
1901 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1902
1903 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001904 * Mask for use with private flags indicating bits used for text direction.
1905 * @hide
1906 */
1907 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1908
1909 /**
1910 * Array of text direction flags for mapping attribute "textDirection" to correct
1911 * flag value.
1912 * @hide
1913 */
1914 private static final int[] TEXT_DIRECTION_FLAGS = {
1915 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1916 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1917 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1918 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1919 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1920 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1921 };
1922
1923 /**
1924 * Indicates whether the view text direction has been resolved.
1925 * @hide
1926 */
1927 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1928
1929 /**
1930 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1931 * @hide
1932 */
1933 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1934
1935 /**
1936 * Mask for use with private flags indicating bits used for resolved text direction.
1937 * @hide
1938 */
1939 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1940
1941 /**
1942 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1943 * @hide
1944 */
1945 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1946 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1947
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001948 /*
1949 * Default text alignment. The text alignment of this View is inherited from its parent.
1950 * Use with {@link #setTextAlignment(int)}
1951 */
1952 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1953
1954 /**
1955 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1956 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1957 *
1958 * Use with {@link #setTextAlignment(int)}
1959 */
1960 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1961
1962 /**
1963 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1964 *
1965 * Use with {@link #setTextAlignment(int)}
1966 */
1967 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1968
1969 /**
1970 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1971 *
1972 * Use with {@link #setTextAlignment(int)}
1973 */
1974 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1975
1976 /**
1977 * Center the paragraph, e.g. ALIGN_CENTER.
1978 *
1979 * Use with {@link #setTextAlignment(int)}
1980 */
1981 public static final int TEXT_ALIGNMENT_CENTER = 4;
1982
1983 /**
1984 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1985 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1986 *
1987 * Use with {@link #setTextAlignment(int)}
1988 */
1989 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1990
1991 /**
1992 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1993 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1994 *
1995 * Use with {@link #setTextAlignment(int)}
1996 */
1997 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1998
1999 /**
2000 * Default text alignment is inherited
2001 */
2002 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2003
2004 /**
2005 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2006 * @hide
2007 */
2008 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2009
2010 /**
2011 * Mask for use with private flags indicating bits used for text alignment.
2012 * @hide
2013 */
2014 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2015
2016 /**
2017 * Array of text direction flags for mapping attribute "textAlignment" to correct
2018 * flag value.
2019 * @hide
2020 */
2021 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2022 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2023 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2024 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2025 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2026 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2027 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2028 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2029 };
2030
2031 /**
2032 * Indicates whether the view text alignment has been resolved.
2033 * @hide
2034 */
2035 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2036
2037 /**
2038 * Bit shift to get the resolved text alignment.
2039 * @hide
2040 */
2041 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2042
2043 /**
2044 * Mask for use with private flags indicating bits used for text alignment.
2045 * @hide
2046 */
2047 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2048
2049 /**
2050 * Indicates whether if the view text alignment has been resolved to gravity
2051 */
2052 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2053 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2054
Svetoslav Ganov42138042012-03-20 11:51:39 -07002055 // Accessiblity constants for mPrivateFlags2
2056
2057 /**
2058 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2059 */
2060 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2061
2062 /**
2063 * Automatically determine whether a view is important for accessibility.
2064 */
2065 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2066
2067 /**
2068 * The view is important for accessibility.
2069 */
2070 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2071
2072 /**
2073 * The view is not important for accessibility.
2074 */
2075 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2076
2077 /**
2078 * The default whether the view is important for accessiblity.
2079 */
2080 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2081
2082 /**
2083 * Mask for obtainig the bits which specify how to determine
2084 * whether a view is important for accessibility.
2085 */
2086 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2087 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2088 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2089
2090 /**
2091 * Flag indicating whether a view has accessibility focus.
2092 */
2093 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2094
2095 /**
2096 * Flag indicating whether a view state for accessibility has changed.
2097 */
2098 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002099
Christopher Tate3d4bf172011-03-28 16:16:46 -07002100 /* End of masks for mPrivateFlags2 */
2101
2102 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2103
Chet Haasedaf98e92011-01-10 14:10:36 -08002104 /**
Adam Powell637d3372010-08-25 14:37:03 -07002105 * Always allow a user to over-scroll this view, provided it is a
2106 * view that can scroll.
2107 *
2108 * @see #getOverScrollMode()
2109 * @see #setOverScrollMode(int)
2110 */
2111 public static final int OVER_SCROLL_ALWAYS = 0;
2112
2113 /**
2114 * Allow a user to over-scroll this view only if the content is large
2115 * enough to meaningfully scroll, provided it is a view that can scroll.
2116 *
2117 * @see #getOverScrollMode()
2118 * @see #setOverScrollMode(int)
2119 */
2120 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2121
2122 /**
2123 * Never allow a user to over-scroll this view.
2124 *
2125 * @see #getOverScrollMode()
2126 * @see #setOverScrollMode(int)
2127 */
2128 public static final int OVER_SCROLL_NEVER = 2;
2129
2130 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002131 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2132 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002133 *
Joe Malin32736f02011-01-19 16:14:20 -08002134 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002135 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002136 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002137
2138 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002139 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2140 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002141 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002142 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002143 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002144 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002145 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002146 *
Joe Malin32736f02011-01-19 16:14:20 -08002147 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002148 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002149 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2150
2151 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002152 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2153 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002154 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002155 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002156 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2157 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2158 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002159 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002160 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2161 * window flags) for displaying content using every last pixel on the display.
2162 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002163 * <p>There is a limitation: because navigation controls are so important, the least user
2164 * interaction will cause them to reappear immediately. When this happens, both
2165 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2166 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002167 *
2168 * @see #setSystemUiVisibility(int)
2169 */
2170 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2171
2172 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002173 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2174 * into the normal fullscreen mode so that its content can take over the screen
2175 * while still allowing the user to interact with the application.
2176 *
2177 * <p>This has the same visual effect as
2178 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2179 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2180 * meaning that non-critical screen decorations (such as the status bar) will be
2181 * hidden while the user is in the View's window, focusing the experience on
2182 * that content. Unlike the window flag, if you are using ActionBar in
2183 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2184 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2185 * hide the action bar.
2186 *
2187 * <p>This approach to going fullscreen is best used over the window flag when
2188 * it is a transient state -- that is, the application does this at certain
2189 * points in its user interaction where it wants to allow the user to focus
2190 * on content, but not as a continuous state. For situations where the application
2191 * would like to simply stay full screen the entire time (such as a game that
2192 * wants to take over the screen), the
2193 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2194 * is usually a better approach. The state set here will be removed by the system
2195 * in various situations (such as the user moving to another application) like
2196 * the other system UI states.
2197 *
2198 * <p>When using this flag, the application should provide some easy facility
2199 * for the user to go out of it. A common example would be in an e-book
2200 * reader, where tapping on the screen brings back whatever screen and UI
2201 * decorations that had been hidden while the user was immersed in reading
2202 * the book.
2203 *
2204 * @see #setSystemUiVisibility(int)
2205 */
2206 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2207
2208 /**
2209 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2210 * flags, we would like a stable view of the content insets given to
2211 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2212 * will always represent the worst case that the application can expect
2213 * as a continue state. In practice this means with any of system bar,
2214 * nav bar, and status bar shown, but not the space that would be needed
2215 * for an input method.
2216 *
2217 * <p>If you are using ActionBar in
2218 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2219 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2220 * insets it adds to those given to the application.
2221 */
2222 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2223
2224 /**
2225 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2226 * to be layed out as if it has requested
2227 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2228 * allows it to avoid artifacts when switching in and out of that mode, at
2229 * the expense that some of its user interface may be covered by screen
2230 * decorations when they are shown. You can perform layout of your inner
2231 * UI elements to account for the navagation system UI through the
2232 * {@link #fitSystemWindows(Rect)} method.
2233 */
2234 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2235
2236 /**
2237 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2238 * to be layed out as if it has requested
2239 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2240 * allows it to avoid artifacts when switching in and out of that mode, at
2241 * the expense that some of its user interface may be covered by screen
2242 * decorations when they are shown. You can perform layout of your inner
2243 * UI elements to account for non-fullscreen system UI through the
2244 * {@link #fitSystemWindows(Rect)} method.
2245 */
2246 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2247
2248 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002249 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2250 */
2251 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2252
2253 /**
2254 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2255 */
2256 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002257
2258 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002259 * @hide
2260 *
2261 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2262 * out of the public fields to keep the undefined bits out of the developer's way.
2263 *
2264 * Flag to make the status bar not expandable. Unless you also
2265 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2266 */
2267 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2268
2269 /**
2270 * @hide
2271 *
2272 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2273 * out of the public fields to keep the undefined bits out of the developer's way.
2274 *
2275 * Flag to hide notification icons and scrolling ticker text.
2276 */
2277 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2278
2279 /**
2280 * @hide
2281 *
2282 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2283 * out of the public fields to keep the undefined bits out of the developer's way.
2284 *
2285 * Flag to disable incoming notification alerts. This will not block
2286 * icons, but it will block sound, vibrating and other visual or aural notifications.
2287 */
2288 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2289
2290 /**
2291 * @hide
2292 *
2293 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2294 * out of the public fields to keep the undefined bits out of the developer's way.
2295 *
2296 * Flag to hide only the scrolling ticker. Note that
2297 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2298 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2299 */
2300 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2301
2302 /**
2303 * @hide
2304 *
2305 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2306 * out of the public fields to keep the undefined bits out of the developer's way.
2307 *
2308 * Flag to hide the center system info area.
2309 */
2310 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2311
2312 /**
2313 * @hide
2314 *
2315 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2316 * out of the public fields to keep the undefined bits out of the developer's way.
2317 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002318 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002319 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2320 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002321 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002322
2323 /**
2324 * @hide
2325 *
2326 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2327 * out of the public fields to keep the undefined bits out of the developer's way.
2328 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002329 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002330 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2331 */
2332 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2333
2334 /**
2335 * @hide
2336 *
2337 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2338 * out of the public fields to keep the undefined bits out of the developer's way.
2339 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002340 * Flag to hide only the clock. You might use this if your activity has
2341 * its own clock making the status bar's clock redundant.
2342 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002343 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2344
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002345 /**
2346 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002347 *
2348 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2349 * out of the public fields to keep the undefined bits out of the developer's way.
2350 *
2351 * Flag to hide only the recent apps button. Don't use this
2352 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2353 */
2354 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2355
2356 /**
2357 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002358 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002359 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002360
2361 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002362 * These are the system UI flags that can be cleared by events outside
2363 * of an application. Currently this is just the ability to tap on the
2364 * screen while hiding the navigation bar to have it return.
2365 * @hide
2366 */
2367 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002368 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2369 | SYSTEM_UI_FLAG_FULLSCREEN;
2370
2371 /**
2372 * Flags that can impact the layout in relation to system UI.
2373 */
2374 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2375 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2376 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002377
2378 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002379 * Find views that render the specified text.
2380 *
2381 * @see #findViewsWithText(ArrayList, CharSequence, int)
2382 */
2383 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2384
2385 /**
2386 * Find find views that contain the specified content description.
2387 *
2388 * @see #findViewsWithText(ArrayList, CharSequence, int)
2389 */
2390 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2391
2392 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002393 * Find views that contain {@link AccessibilityNodeProvider}. Such
2394 * a View is a root of virtual view hierarchy and may contain the searched
2395 * text. If this flag is set Views with providers are automatically
2396 * added and it is a responsibility of the client to call the APIs of
2397 * the provider to determine whether the virtual tree rooted at this View
2398 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2399 * represeting the virtual views with this text.
2400 *
2401 * @see #findViewsWithText(ArrayList, CharSequence, int)
2402 *
2403 * @hide
2404 */
2405 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2406
2407 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002408 * Indicates that the screen has changed state and is now off.
2409 *
2410 * @see #onScreenStateChanged(int)
2411 */
2412 public static final int SCREEN_STATE_OFF = 0x0;
2413
2414 /**
2415 * Indicates that the screen has changed state and is now on.
2416 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002417 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002418 */
2419 public static final int SCREEN_STATE_ON = 0x1;
2420
2421 /**
Adam Powell637d3372010-08-25 14:37:03 -07002422 * Controls the over-scroll mode for this view.
2423 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2424 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2425 * and {@link #OVER_SCROLL_NEVER}.
2426 */
2427 private int mOverScrollMode;
2428
2429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 * The parent this view is attached to.
2431 * {@hide}
2432 *
2433 * @see #getParent()
2434 */
2435 protected ViewParent mParent;
2436
2437 /**
2438 * {@hide}
2439 */
2440 AttachInfo mAttachInfo;
2441
2442 /**
2443 * {@hide}
2444 */
Romain Guy809a7f62009-05-14 15:44:42 -07002445 @ViewDebug.ExportedProperty(flagMapping = {
2446 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2447 name = "FORCE_LAYOUT"),
2448 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2449 name = "LAYOUT_REQUIRED"),
2450 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002451 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002452 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2453 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2454 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2455 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2456 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002458 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459
2460 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002461 * This view's request for the visibility of the status bar.
2462 * @hide
2463 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002464 @ViewDebug.ExportedProperty(flagMapping = {
2465 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2466 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2467 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2468 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2469 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2470 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2471 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2472 equals = SYSTEM_UI_FLAG_VISIBLE,
2473 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2474 })
Joe Onorato664644d2011-01-23 17:53:23 -08002475 int mSystemUiVisibility;
2476
2477 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002478 * Reference count for transient state.
2479 * @see #setHasTransientState(boolean)
2480 */
2481 int mTransientStateCount = 0;
2482
2483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 * Count of how many windows this view has been attached to.
2485 */
2486 int mWindowAttachCount;
2487
2488 /**
2489 * The layout parameters associated with this view and used by the parent
2490 * {@link android.view.ViewGroup} to determine how this view should be
2491 * laid out.
2492 * {@hide}
2493 */
2494 protected ViewGroup.LayoutParams mLayoutParams;
2495
2496 /**
2497 * The view flags hold various views states.
2498 * {@hide}
2499 */
2500 @ViewDebug.ExportedProperty
2501 int mViewFlags;
2502
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002503 static class TransformationInfo {
2504 /**
2505 * The transform matrix for the View. This transform is calculated internally
2506 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2507 * is used by default. Do *not* use this variable directly; instead call
2508 * getMatrix(), which will automatically recalculate the matrix if necessary
2509 * to get the correct matrix based on the latest rotation and scale properties.
2510 */
2511 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002512
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002513 /**
2514 * The transform matrix for the View. This transform is calculated internally
2515 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2516 * is used by default. Do *not* use this variable directly; instead call
2517 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2518 * to get the correct matrix based on the latest rotation and scale properties.
2519 */
2520 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002521
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002522 /**
2523 * An internal variable that tracks whether we need to recalculate the
2524 * transform matrix, based on whether the rotation or scaleX/Y properties
2525 * have changed since the matrix was last calculated.
2526 */
2527 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002528
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002529 /**
2530 * An internal variable that tracks whether we need to recalculate the
2531 * transform matrix, based on whether the rotation or scaleX/Y properties
2532 * have changed since the matrix was last calculated.
2533 */
2534 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002535
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002536 /**
2537 * A variable that tracks whether we need to recalculate the
2538 * transform matrix, based on whether the rotation or scaleX/Y properties
2539 * have changed since the matrix was last calculated. This variable
2540 * is only valid after a call to updateMatrix() or to a function that
2541 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2542 */
2543 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002544
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002545 /**
2546 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2547 */
2548 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002549
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002550 /**
2551 * This matrix is used when computing the matrix for 3D rotations.
2552 */
2553 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002554
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002555 /**
2556 * These prev values are used to recalculate a centered pivot point when necessary. The
2557 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2558 * set), so thes values are only used then as well.
2559 */
2560 private int mPrevWidth = -1;
2561 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002562
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002563 /**
2564 * The degrees rotation around the vertical axis through the pivot point.
2565 */
2566 @ViewDebug.ExportedProperty
2567 float mRotationY = 0f;
2568
2569 /**
2570 * The degrees rotation around the horizontal axis through the pivot point.
2571 */
2572 @ViewDebug.ExportedProperty
2573 float mRotationX = 0f;
2574
2575 /**
2576 * The degrees rotation around the pivot point.
2577 */
2578 @ViewDebug.ExportedProperty
2579 float mRotation = 0f;
2580
2581 /**
2582 * The amount of translation of the object away from its left property (post-layout).
2583 */
2584 @ViewDebug.ExportedProperty
2585 float mTranslationX = 0f;
2586
2587 /**
2588 * The amount of translation of the object away from its top property (post-layout).
2589 */
2590 @ViewDebug.ExportedProperty
2591 float mTranslationY = 0f;
2592
2593 /**
2594 * The amount of scale in the x direction around the pivot point. A
2595 * value of 1 means no scaling is applied.
2596 */
2597 @ViewDebug.ExportedProperty
2598 float mScaleX = 1f;
2599
2600 /**
2601 * The amount of scale in the y direction around the pivot point. A
2602 * value of 1 means no scaling is applied.
2603 */
2604 @ViewDebug.ExportedProperty
2605 float mScaleY = 1f;
2606
2607 /**
Chet Haasea33de552012-02-03 16:28:24 -08002608 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002609 */
2610 @ViewDebug.ExportedProperty
2611 float mPivotX = 0f;
2612
2613 /**
Chet Haasea33de552012-02-03 16:28:24 -08002614 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002615 */
2616 @ViewDebug.ExportedProperty
2617 float mPivotY = 0f;
2618
2619 /**
2620 * The opacity of the View. This is a value from 0 to 1, where 0 means
2621 * completely transparent and 1 means completely opaque.
2622 */
2623 @ViewDebug.ExportedProperty
2624 float mAlpha = 1f;
2625 }
2626
2627 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002628
Joe Malin32736f02011-01-19 16:14:20 -08002629 private boolean mLastIsOpaque;
2630
Chet Haasefd2b0022010-08-06 13:08:56 -07002631 /**
2632 * Convenience value to check for float values that are close enough to zero to be considered
2633 * zero.
2634 */
Romain Guy2542d192010-08-18 11:47:12 -07002635 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002636
2637 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 * The distance in pixels from the left edge of this view's parent
2639 * to the left edge of this view.
2640 * {@hide}
2641 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002642 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 protected int mLeft;
2644 /**
2645 * The distance in pixels from the left edge of this view's parent
2646 * to the right edge of this view.
2647 * {@hide}
2648 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002649 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 protected int mRight;
2651 /**
2652 * The distance in pixels from the top edge of this view's parent
2653 * to the top edge of this view.
2654 * {@hide}
2655 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002656 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 protected int mTop;
2658 /**
2659 * The distance in pixels from the top edge of this view's parent
2660 * to the bottom edge of this view.
2661 * {@hide}
2662 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002663 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 protected int mBottom;
2665
2666 /**
2667 * The offset, in pixels, by which the content of this view is scrolled
2668 * horizontally.
2669 * {@hide}
2670 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002671 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 protected int mScrollX;
2673 /**
2674 * The offset, in pixels, by which the content of this view is scrolled
2675 * vertically.
2676 * {@hide}
2677 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002678 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 protected int mScrollY;
2680
2681 /**
2682 * The left padding in pixels, that is the distance in pixels between the
2683 * left edge of this view and the left edge of its content.
2684 * {@hide}
2685 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002686 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 protected int mPaddingLeft;
2688 /**
2689 * The right padding in pixels, that is the distance in pixels between the
2690 * right edge of this view and the right edge of its content.
2691 * {@hide}
2692 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002693 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 protected int mPaddingRight;
2695 /**
2696 * The top padding in pixels, that is the distance in pixels between the
2697 * top edge of this view and the top edge of its content.
2698 * {@hide}
2699 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002700 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 protected int mPaddingTop;
2702 /**
2703 * The bottom padding in pixels, that is the distance in pixels between the
2704 * bottom edge of this view and the bottom edge of its content.
2705 * {@hide}
2706 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002707 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 protected int mPaddingBottom;
2709
2710 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002711 * The layout insets in pixels, that is the distance in pixels between the
2712 * visible edges of this view its bounds.
2713 */
2714 private Insets mLayoutInsets;
2715
2716 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002717 * Briefly describes the view and is primarily used for accessibility support.
2718 */
2719 private CharSequence mContentDescription;
2720
2721 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002723 *
2724 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002726 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002727 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728
2729 /**
2730 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002731 *
2732 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002734 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002735 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002737 /**
Adam Powell20232d02010-12-08 21:08:53 -08002738 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002739 *
2740 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002741 */
2742 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002743 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002744
2745 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002746 * Cache if the user padding is relative.
2747 *
2748 */
2749 @ViewDebug.ExportedProperty(category = "padding")
2750 boolean mUserPaddingRelative;
2751
2752 /**
2753 * Cache the paddingStart set by the user to append to the scrollbar's size.
2754 *
2755 */
2756 @ViewDebug.ExportedProperty(category = "padding")
2757 int mUserPaddingStart;
2758
2759 /**
2760 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2761 *
2762 */
2763 @ViewDebug.ExportedProperty(category = "padding")
2764 int mUserPaddingEnd;
2765
2766 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002767 * @hide
2768 */
2769 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2770 /**
2771 * @hide
2772 */
2773 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774
Philip Milne6c8ea062012-04-03 17:38:43 -07002775 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776
2777 private int mBackgroundResource;
2778 private boolean mBackgroundSizeChanged;
2779
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002780 static class ListenerInfo {
2781 /**
2782 * Listener used to dispatch focus change events.
2783 * This field should be made private, so it is hidden from the SDK.
2784 * {@hide}
2785 */
2786 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002788 /**
2789 * Listeners for layout change events.
2790 */
2791 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002792
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002793 /**
2794 * Listeners for attach events.
2795 */
2796 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002797
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002798 /**
2799 * Listener used to dispatch click events.
2800 * This field should be made private, so it is hidden from the SDK.
2801 * {@hide}
2802 */
2803 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002805 /**
2806 * Listener used to dispatch long click events.
2807 * This field should be made private, so it is hidden from the SDK.
2808 * {@hide}
2809 */
2810 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002812 /**
2813 * Listener used to build the context menu.
2814 * This field should be made private, so it is hidden from the SDK.
2815 * {@hide}
2816 */
2817 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002819 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002821 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002823 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002824
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002825 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002826
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002827 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002828
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002829 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2830 }
2831
2832 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 /**
2835 * The application environment this view lives in.
2836 * This field should be made private, so it is hidden from the SDK.
2837 * {@hide}
2838 */
2839 protected Context mContext;
2840
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002841 private final Resources mResources;
2842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 private ScrollabilityCache mScrollCache;
2844
2845 private int[] mDrawableState = null;
2846
Romain Guy0211a0a2011-02-14 16:34:59 -08002847 /**
2848 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002849 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002850 * @hide
2851 */
2852 public boolean mCachingFailed;
2853
Romain Guy02890fd2010-08-06 17:58:44 -07002854 private Bitmap mDrawingCache;
2855 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002856 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002857 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858
2859 /**
2860 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2861 * the user may specify which view to go to next.
2862 */
2863 private int mNextFocusLeftId = View.NO_ID;
2864
2865 /**
2866 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2867 * the user may specify which view to go to next.
2868 */
2869 private int mNextFocusRightId = View.NO_ID;
2870
2871 /**
2872 * When this view has focus and the next focus is {@link #FOCUS_UP},
2873 * the user may specify which view to go to next.
2874 */
2875 private int mNextFocusUpId = View.NO_ID;
2876
2877 /**
2878 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2879 * the user may specify which view to go to next.
2880 */
2881 private int mNextFocusDownId = View.NO_ID;
2882
Jeff Brown4e6319b2010-12-13 10:36:51 -08002883 /**
2884 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2885 * the user may specify which view to go to next.
2886 */
2887 int mNextFocusForwardId = View.NO_ID;
2888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002890 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002891 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002892 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 private UnsetPressedState mUnsetPressedState;
2895
2896 /**
2897 * Whether the long press's action has been invoked. The tap's action is invoked on the
2898 * up event while a long press is invoked as soon as the long press duration is reached, so
2899 * a long press could be performed before the tap is checked, in which case the tap's action
2900 * should not be invoked.
2901 */
2902 private boolean mHasPerformedLongPress;
2903
2904 /**
2905 * The minimum height of the view. We'll try our best to have the height
2906 * of this view to at least this amount.
2907 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002908 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 private int mMinHeight;
2910
2911 /**
2912 * The minimum width of the view. We'll try our best to have the width
2913 * of this view to at least this amount.
2914 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002915 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 private int mMinWidth;
2917
2918 /**
2919 * The delegate to handle touch events that are physically in this view
2920 * but should be handled by another view.
2921 */
2922 private TouchDelegate mTouchDelegate = null;
2923
2924 /**
2925 * Solid color to use as a background when creating the drawing cache. Enables
2926 * the cache to use 16 bit bitmaps instead of 32 bit.
2927 */
2928 private int mDrawingCacheBackgroundColor = 0;
2929
2930 /**
2931 * Special tree observer used when mAttachInfo is null.
2932 */
2933 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002934
Adam Powelle14579b2009-12-16 18:39:52 -08002935 /**
2936 * Cache the touch slop from the context that created the view.
2937 */
2938 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002941 * Object that handles automatic animation of view properties.
2942 */
2943 private ViewPropertyAnimator mAnimator = null;
2944
2945 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002946 * Flag indicating that a drag can cross window boundaries. When
2947 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2948 * with this flag set, all visible applications will be able to participate
2949 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002950 *
2951 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002952 */
2953 public static final int DRAG_FLAG_GLOBAL = 1;
2954
2955 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002956 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2957 */
2958 private float mVerticalScrollFactor;
2959
2960 /**
Adam Powell20232d02010-12-08 21:08:53 -08002961 * Position of the vertical scroll bar.
2962 */
2963 private int mVerticalScrollbarPosition;
2964
2965 /**
2966 * Position the scroll bar at the default position as determined by the system.
2967 */
2968 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2969
2970 /**
2971 * Position the scroll bar along the left edge.
2972 */
2973 public static final int SCROLLBAR_POSITION_LEFT = 1;
2974
2975 /**
2976 * Position the scroll bar along the right edge.
2977 */
2978 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2979
2980 /**
Romain Guy171c5922011-01-06 10:04:23 -08002981 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002982 *
2983 * @see #getLayerType()
2984 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002985 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002986 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002987 */
2988 public static final int LAYER_TYPE_NONE = 0;
2989
2990 /**
2991 * <p>Indicates that the view has a software layer. A software layer is backed
2992 * by a bitmap and causes the view to be rendered using Android's software
2993 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002994 *
Romain Guy171c5922011-01-06 10:04:23 -08002995 * <p>Software layers have various usages:</p>
2996 * <p>When the application is not using hardware acceleration, a software layer
2997 * is useful to apply a specific color filter and/or blending mode and/or
2998 * translucency to a view and all its children.</p>
2999 * <p>When the application is using hardware acceleration, a software layer
3000 * is useful to render drawing primitives not supported by the hardware
3001 * accelerated pipeline. It can also be used to cache a complex view tree
3002 * into a texture and reduce the complexity of drawing operations. For instance,
3003 * when animating a complex view tree with a translation, a software layer can
3004 * be used to render the view tree only once.</p>
3005 * <p>Software layers should be avoided when the affected view tree updates
3006 * often. Every update will require to re-render the software layer, which can
3007 * potentially be slow (particularly when hardware acceleration is turned on
3008 * since the layer will have to be uploaded into a hardware texture after every
3009 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003010 *
3011 * @see #getLayerType()
3012 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003013 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003014 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003015 */
3016 public static final int LAYER_TYPE_SOFTWARE = 1;
3017
3018 /**
3019 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3020 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3021 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3022 * rendering pipeline, but only if hardware acceleration is turned on for the
3023 * view hierarchy. When hardware acceleration is turned off, hardware layers
3024 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003025 *
Romain Guy171c5922011-01-06 10:04:23 -08003026 * <p>A hardware layer is useful to apply a specific color filter and/or
3027 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003028 * <p>A hardware layer can be used to cache a complex view tree into a
3029 * texture and reduce the complexity of drawing operations. For instance,
3030 * when animating a complex view tree with a translation, a hardware layer can
3031 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003032 * <p>A hardware layer can also be used to increase the rendering quality when
3033 * rotation transformations are applied on a view. It can also be used to
3034 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003035 *
3036 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003037 * @see #setLayerType(int, android.graphics.Paint)
3038 * @see #LAYER_TYPE_NONE
3039 * @see #LAYER_TYPE_SOFTWARE
3040 */
3041 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003042
Romain Guy3aaff3a2011-01-12 14:18:47 -08003043 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3044 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3045 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3046 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3047 })
Romain Guy171c5922011-01-06 10:04:23 -08003048 int mLayerType = LAYER_TYPE_NONE;
3049 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003050 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003051
3052 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003053 * Set to true when the view is sending hover accessibility events because it
3054 * is the innermost hovered view.
3055 */
3056 private boolean mSendingHoverAccessibilityEvents;
3057
3058 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 * Simple constructor to use when creating a view from code.
3060 *
3061 * @param context The Context the view is running in, through which it can
3062 * access the current theme, resources, etc.
3063 */
3064 public View(Context context) {
3065 mContext = context;
3066 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003067 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003068 // Set layout and text direction defaults
3069 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003070 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003071 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3072 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003073 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003074 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003075 mUserPaddingStart = -1;
3076 mUserPaddingEnd = -1;
3077 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079
3080 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003081 * Delegate for injecting accessiblity functionality.
3082 */
3083 AccessibilityDelegate mAccessibilityDelegate;
3084
3085 /**
3086 * Consistency verifier for debugging purposes.
3087 * @hide
3088 */
3089 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3090 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3091 new InputEventConsistencyVerifier(this, 0) : null;
3092
3093 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 * Constructor that is called when inflating a view from XML. This is called
3095 * when a view is being constructed from an XML file, supplying attributes
3096 * that were specified in the XML file. This version uses a default style of
3097 * 0, so the only attribute values applied are those in the Context's Theme
3098 * and the given AttributeSet.
3099 *
3100 * <p>
3101 * The method onFinishInflate() will be called after all children have been
3102 * added.
3103 *
3104 * @param context The Context the view is running in, through which it can
3105 * access the current theme, resources, etc.
3106 * @param attrs The attributes of the XML tag that is inflating the view.
3107 * @see #View(Context, AttributeSet, int)
3108 */
3109 public View(Context context, AttributeSet attrs) {
3110 this(context, attrs, 0);
3111 }
3112
3113 /**
3114 * Perform inflation from XML and apply a class-specific base style. This
3115 * constructor of View allows subclasses to use their own base style when
3116 * they are inflating. For example, a Button class's constructor would call
3117 * this version of the super class constructor and supply
3118 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3119 * the theme's button style to modify all of the base view attributes (in
3120 * particular its background) as well as the Button class's attributes.
3121 *
3122 * @param context The Context the view is running in, through which it can
3123 * access the current theme, resources, etc.
3124 * @param attrs The attributes of the XML tag that is inflating the view.
3125 * @param defStyle The default style to apply to this view. If 0, no style
3126 * will be applied (beyond what is included in the theme). This may
3127 * either be an attribute resource, whose value will be retrieved
3128 * from the current theme, or an explicit style resource.
3129 * @see #View(Context, AttributeSet)
3130 */
3131 public View(Context context, AttributeSet attrs, int defStyle) {
3132 this(context);
3133
3134 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3135 defStyle, 0);
3136
3137 Drawable background = null;
3138
3139 int leftPadding = -1;
3140 int topPadding = -1;
3141 int rightPadding = -1;
3142 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003143 int startPadding = -1;
3144 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145
3146 int padding = -1;
3147
3148 int viewFlagValues = 0;
3149 int viewFlagMasks = 0;
3150
3151 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 int x = 0;
3154 int y = 0;
3155
Chet Haase73066682010-11-29 15:55:32 -08003156 float tx = 0;
3157 float ty = 0;
3158 float rotation = 0;
3159 float rotationX = 0;
3160 float rotationY = 0;
3161 float sx = 1f;
3162 float sy = 1f;
3163 boolean transformSet = false;
3164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3166
Adam Powell637d3372010-08-25 14:37:03 -07003167 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 final int N = a.getIndexCount();
3169 for (int i = 0; i < N; i++) {
3170 int attr = a.getIndex(i);
3171 switch (attr) {
3172 case com.android.internal.R.styleable.View_background:
3173 background = a.getDrawable(attr);
3174 break;
3175 case com.android.internal.R.styleable.View_padding:
3176 padding = a.getDimensionPixelSize(attr, -1);
3177 break;
3178 case com.android.internal.R.styleable.View_paddingLeft:
3179 leftPadding = a.getDimensionPixelSize(attr, -1);
3180 break;
3181 case com.android.internal.R.styleable.View_paddingTop:
3182 topPadding = a.getDimensionPixelSize(attr, -1);
3183 break;
3184 case com.android.internal.R.styleable.View_paddingRight:
3185 rightPadding = a.getDimensionPixelSize(attr, -1);
3186 break;
3187 case com.android.internal.R.styleable.View_paddingBottom:
3188 bottomPadding = a.getDimensionPixelSize(attr, -1);
3189 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003190 case com.android.internal.R.styleable.View_paddingStart:
3191 startPadding = a.getDimensionPixelSize(attr, -1);
3192 break;
3193 case com.android.internal.R.styleable.View_paddingEnd:
3194 endPadding = a.getDimensionPixelSize(attr, -1);
3195 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 case com.android.internal.R.styleable.View_scrollX:
3197 x = a.getDimensionPixelOffset(attr, 0);
3198 break;
3199 case com.android.internal.R.styleable.View_scrollY:
3200 y = a.getDimensionPixelOffset(attr, 0);
3201 break;
Chet Haase73066682010-11-29 15:55:32 -08003202 case com.android.internal.R.styleable.View_alpha:
3203 setAlpha(a.getFloat(attr, 1f));
3204 break;
3205 case com.android.internal.R.styleable.View_transformPivotX:
3206 setPivotX(a.getDimensionPixelOffset(attr, 0));
3207 break;
3208 case com.android.internal.R.styleable.View_transformPivotY:
3209 setPivotY(a.getDimensionPixelOffset(attr, 0));
3210 break;
3211 case com.android.internal.R.styleable.View_translationX:
3212 tx = a.getDimensionPixelOffset(attr, 0);
3213 transformSet = true;
3214 break;
3215 case com.android.internal.R.styleable.View_translationY:
3216 ty = a.getDimensionPixelOffset(attr, 0);
3217 transformSet = true;
3218 break;
3219 case com.android.internal.R.styleable.View_rotation:
3220 rotation = a.getFloat(attr, 0);
3221 transformSet = true;
3222 break;
3223 case com.android.internal.R.styleable.View_rotationX:
3224 rotationX = a.getFloat(attr, 0);
3225 transformSet = true;
3226 break;
3227 case com.android.internal.R.styleable.View_rotationY:
3228 rotationY = a.getFloat(attr, 0);
3229 transformSet = true;
3230 break;
3231 case com.android.internal.R.styleable.View_scaleX:
3232 sx = a.getFloat(attr, 1f);
3233 transformSet = true;
3234 break;
3235 case com.android.internal.R.styleable.View_scaleY:
3236 sy = a.getFloat(attr, 1f);
3237 transformSet = true;
3238 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 case com.android.internal.R.styleable.View_id:
3240 mID = a.getResourceId(attr, NO_ID);
3241 break;
3242 case com.android.internal.R.styleable.View_tag:
3243 mTag = a.getText(attr);
3244 break;
3245 case com.android.internal.R.styleable.View_fitsSystemWindows:
3246 if (a.getBoolean(attr, false)) {
3247 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3248 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3249 }
3250 break;
3251 case com.android.internal.R.styleable.View_focusable:
3252 if (a.getBoolean(attr, false)) {
3253 viewFlagValues |= FOCUSABLE;
3254 viewFlagMasks |= FOCUSABLE_MASK;
3255 }
3256 break;
3257 case com.android.internal.R.styleable.View_focusableInTouchMode:
3258 if (a.getBoolean(attr, false)) {
3259 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3260 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3261 }
3262 break;
3263 case com.android.internal.R.styleable.View_clickable:
3264 if (a.getBoolean(attr, false)) {
3265 viewFlagValues |= CLICKABLE;
3266 viewFlagMasks |= CLICKABLE;
3267 }
3268 break;
3269 case com.android.internal.R.styleable.View_longClickable:
3270 if (a.getBoolean(attr, false)) {
3271 viewFlagValues |= LONG_CLICKABLE;
3272 viewFlagMasks |= LONG_CLICKABLE;
3273 }
3274 break;
3275 case com.android.internal.R.styleable.View_saveEnabled:
3276 if (!a.getBoolean(attr, true)) {
3277 viewFlagValues |= SAVE_DISABLED;
3278 viewFlagMasks |= SAVE_DISABLED_MASK;
3279 }
3280 break;
3281 case com.android.internal.R.styleable.View_duplicateParentState:
3282 if (a.getBoolean(attr, false)) {
3283 viewFlagValues |= DUPLICATE_PARENT_STATE;
3284 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3285 }
3286 break;
3287 case com.android.internal.R.styleable.View_visibility:
3288 final int visibility = a.getInt(attr, 0);
3289 if (visibility != 0) {
3290 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3291 viewFlagMasks |= VISIBILITY_MASK;
3292 }
3293 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003294 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003295 // Clear any layout direction flags (included resolved bits) already set
3296 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3297 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003298 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003299 final int value = (layoutDirection != -1) ?
3300 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3301 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003302 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 case com.android.internal.R.styleable.View_drawingCacheQuality:
3304 final int cacheQuality = a.getInt(attr, 0);
3305 if (cacheQuality != 0) {
3306 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3307 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3308 }
3309 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003310 case com.android.internal.R.styleable.View_contentDescription:
3311 mContentDescription = a.getString(attr);
3312 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3314 if (!a.getBoolean(attr, true)) {
3315 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3316 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3317 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003318 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3320 if (!a.getBoolean(attr, true)) {
3321 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3322 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3323 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003324 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 case R.styleable.View_scrollbars:
3326 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3327 if (scrollbars != SCROLLBARS_NONE) {
3328 viewFlagValues |= scrollbars;
3329 viewFlagMasks |= SCROLLBARS_MASK;
3330 initializeScrollbars(a);
3331 }
3332 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003333 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003335 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3336 // Ignore the attribute starting with ICS
3337 break;
3338 }
3339 // With builds < ICS, fall through and apply fading edges
3340 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3342 if (fadingEdge != FADING_EDGE_NONE) {
3343 viewFlagValues |= fadingEdge;
3344 viewFlagMasks |= FADING_EDGE_MASK;
3345 initializeFadingEdge(a);
3346 }
3347 break;
3348 case R.styleable.View_scrollbarStyle:
3349 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3350 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3351 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3352 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3353 }
3354 break;
3355 case R.styleable.View_isScrollContainer:
3356 setScrollContainer = true;
3357 if (a.getBoolean(attr, false)) {
3358 setScrollContainer(true);
3359 }
3360 break;
3361 case com.android.internal.R.styleable.View_keepScreenOn:
3362 if (a.getBoolean(attr, false)) {
3363 viewFlagValues |= KEEP_SCREEN_ON;
3364 viewFlagMasks |= KEEP_SCREEN_ON;
3365 }
3366 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003367 case R.styleable.View_filterTouchesWhenObscured:
3368 if (a.getBoolean(attr, false)) {
3369 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3370 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3371 }
3372 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 case R.styleable.View_nextFocusLeft:
3374 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3375 break;
3376 case R.styleable.View_nextFocusRight:
3377 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3378 break;
3379 case R.styleable.View_nextFocusUp:
3380 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3381 break;
3382 case R.styleable.View_nextFocusDown:
3383 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3384 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003385 case R.styleable.View_nextFocusForward:
3386 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3387 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 case R.styleable.View_minWidth:
3389 mMinWidth = a.getDimensionPixelSize(attr, 0);
3390 break;
3391 case R.styleable.View_minHeight:
3392 mMinHeight = a.getDimensionPixelSize(attr, 0);
3393 break;
Romain Guy9a817362009-05-01 10:57:14 -07003394 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003395 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003396 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003397 + "be used within a restricted context");
3398 }
3399
Romain Guy9a817362009-05-01 10:57:14 -07003400 final String handlerName = a.getString(attr);
3401 if (handlerName != null) {
3402 setOnClickListener(new OnClickListener() {
3403 private Method mHandler;
3404
3405 public void onClick(View v) {
3406 if (mHandler == null) {
3407 try {
3408 mHandler = getContext().getClass().getMethod(handlerName,
3409 View.class);
3410 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003411 int id = getId();
3412 String idText = id == NO_ID ? "" : " with id '"
3413 + getContext().getResources().getResourceEntryName(
3414 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003415 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003416 handlerName + "(View) in the activity "
3417 + getContext().getClass() + " for onClick handler"
3418 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003419 }
3420 }
3421
3422 try {
3423 mHandler.invoke(getContext(), View.this);
3424 } catch (IllegalAccessException e) {
3425 throw new IllegalStateException("Could not execute non "
3426 + "public method of the activity", e);
3427 } catch (InvocationTargetException e) {
3428 throw new IllegalStateException("Could not execute "
3429 + "method of the activity", e);
3430 }
3431 }
3432 });
3433 }
3434 break;
Adam Powell637d3372010-08-25 14:37:03 -07003435 case R.styleable.View_overScrollMode:
3436 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3437 break;
Adam Powell20232d02010-12-08 21:08:53 -08003438 case R.styleable.View_verticalScrollbarPosition:
3439 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3440 break;
Romain Guy171c5922011-01-06 10:04:23 -08003441 case R.styleable.View_layerType:
3442 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3443 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003444 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003445 // Clear any text direction flag already set
3446 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3447 // Set the text direction flags depending on the value of the attribute
3448 final int textDirection = a.getInt(attr, -1);
3449 if (textDirection != -1) {
3450 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3451 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003452 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003453 case R.styleable.View_textAlignment:
3454 // Clear any text alignment flag already set
3455 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3456 // Set the text alignment flag depending on the value of the attribute
3457 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3458 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3459 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003460 case R.styleable.View_importantForAccessibility:
3461 setImportantForAccessibility(a.getInt(attr,
3462 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 }
3464 }
3465
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003466 a.recycle();
3467
Adam Powell637d3372010-08-25 14:37:03 -07003468 setOverScrollMode(overScrollMode);
3469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003471 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 }
3473
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003474 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3475 // layout direction). Those cached values will be used later during padding resolution.
3476 mUserPaddingStart = startPadding;
3477 mUserPaddingEnd = endPadding;
3478
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003479 updateUserPaddingRelative();
3480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 if (padding >= 0) {
3482 leftPadding = padding;
3483 topPadding = padding;
3484 rightPadding = padding;
3485 bottomPadding = padding;
3486 }
3487
3488 // If the user specified the padding (either with android:padding or
3489 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3490 // use the default padding or the padding from the background drawable
3491 // (stored at this point in mPadding*)
3492 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3493 topPadding >= 0 ? topPadding : mPaddingTop,
3494 rightPadding >= 0 ? rightPadding : mPaddingRight,
3495 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3496
3497 if (viewFlagMasks != 0) {
3498 setFlags(viewFlagValues, viewFlagMasks);
3499 }
3500
3501 // Needs to be called after mViewFlags is set
3502 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3503 recomputePadding();
3504 }
3505
3506 if (x != 0 || y != 0) {
3507 scrollTo(x, y);
3508 }
3509
Chet Haase73066682010-11-29 15:55:32 -08003510 if (transformSet) {
3511 setTranslationX(tx);
3512 setTranslationY(ty);
3513 setRotation(rotation);
3514 setRotationX(rotationX);
3515 setRotationY(rotationY);
3516 setScaleX(sx);
3517 setScaleY(sy);
3518 }
3519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3521 setScrollContainer(true);
3522 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003523
3524 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 }
3526
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003527 private void updateUserPaddingRelative() {
3528 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3529 }
3530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 /**
3532 * Non-public constructor for use in testing
3533 */
3534 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003535 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
3537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 /**
3539 * <p>
3540 * Initializes the fading edges from a given set of styled attributes. This
3541 * method should be called by subclasses that need fading edges and when an
3542 * instance of these subclasses is created programmatically rather than
3543 * being inflated from XML. This method is automatically called when the XML
3544 * is inflated.
3545 * </p>
3546 *
3547 * @param a the styled attributes set to initialize the fading edges from
3548 */
3549 protected void initializeFadingEdge(TypedArray a) {
3550 initScrollCache();
3551
3552 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3553 R.styleable.View_fadingEdgeLength,
3554 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3555 }
3556
3557 /**
3558 * Returns the size of the vertical faded edges used to indicate that more
3559 * content in this view is visible.
3560 *
3561 * @return The size in pixels of the vertical faded edge or 0 if vertical
3562 * faded edges are not enabled for this view.
3563 * @attr ref android.R.styleable#View_fadingEdgeLength
3564 */
3565 public int getVerticalFadingEdgeLength() {
3566 if (isVerticalFadingEdgeEnabled()) {
3567 ScrollabilityCache cache = mScrollCache;
3568 if (cache != null) {
3569 return cache.fadingEdgeLength;
3570 }
3571 }
3572 return 0;
3573 }
3574
3575 /**
3576 * Set the size of the faded edge used to indicate that more content in this
3577 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003578 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3579 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3580 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 *
3582 * @param length The size in pixels of the faded edge used to indicate that more
3583 * content in this view is visible.
3584 */
3585 public void setFadingEdgeLength(int length) {
3586 initScrollCache();
3587 mScrollCache.fadingEdgeLength = length;
3588 }
3589
3590 /**
3591 * Returns the size of the horizontal faded edges used to indicate that more
3592 * content in this view is visible.
3593 *
3594 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3595 * faded edges are not enabled for this view.
3596 * @attr ref android.R.styleable#View_fadingEdgeLength
3597 */
3598 public int getHorizontalFadingEdgeLength() {
3599 if (isHorizontalFadingEdgeEnabled()) {
3600 ScrollabilityCache cache = mScrollCache;
3601 if (cache != null) {
3602 return cache.fadingEdgeLength;
3603 }
3604 }
3605 return 0;
3606 }
3607
3608 /**
3609 * Returns the width of the vertical scrollbar.
3610 *
3611 * @return The width in pixels of the vertical scrollbar or 0 if there
3612 * is no vertical scrollbar.
3613 */
3614 public int getVerticalScrollbarWidth() {
3615 ScrollabilityCache cache = mScrollCache;
3616 if (cache != null) {
3617 ScrollBarDrawable scrollBar = cache.scrollBar;
3618 if (scrollBar != null) {
3619 int size = scrollBar.getSize(true);
3620 if (size <= 0) {
3621 size = cache.scrollBarSize;
3622 }
3623 return size;
3624 }
3625 return 0;
3626 }
3627 return 0;
3628 }
3629
3630 /**
3631 * Returns the height of the horizontal scrollbar.
3632 *
3633 * @return The height in pixels of the horizontal scrollbar or 0 if
3634 * there is no horizontal scrollbar.
3635 */
3636 protected int getHorizontalScrollbarHeight() {
3637 ScrollabilityCache cache = mScrollCache;
3638 if (cache != null) {
3639 ScrollBarDrawable scrollBar = cache.scrollBar;
3640 if (scrollBar != null) {
3641 int size = scrollBar.getSize(false);
3642 if (size <= 0) {
3643 size = cache.scrollBarSize;
3644 }
3645 return size;
3646 }
3647 return 0;
3648 }
3649 return 0;
3650 }
3651
3652 /**
3653 * <p>
3654 * Initializes the scrollbars from a given set of styled attributes. This
3655 * method should be called by subclasses that need scrollbars and when an
3656 * instance of these subclasses is created programmatically rather than
3657 * being inflated from XML. This method is automatically called when the XML
3658 * is inflated.
3659 * </p>
3660 *
3661 * @param a the styled attributes set to initialize the scrollbars from
3662 */
3663 protected void initializeScrollbars(TypedArray a) {
3664 initScrollCache();
3665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003667
Mike Cleronf116bf82009-09-27 19:14:12 -07003668 if (scrollabilityCache.scrollBar == null) {
3669 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3670 }
Joe Malin32736f02011-01-19 16:14:20 -08003671
Romain Guy8bda2482010-03-02 11:42:11 -08003672 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673
Mike Cleronf116bf82009-09-27 19:14:12 -07003674 if (!fadeScrollbars) {
3675 scrollabilityCache.state = ScrollabilityCache.ON;
3676 }
3677 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003678
3679
Mike Cleronf116bf82009-09-27 19:14:12 -07003680 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3681 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3682 .getScrollBarFadeDuration());
3683 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3684 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3685 ViewConfiguration.getScrollDefaultDelay());
3686
Joe Malin32736f02011-01-19 16:14:20 -08003687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3689 com.android.internal.R.styleable.View_scrollbarSize,
3690 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3691
3692 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3693 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3694
3695 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3696 if (thumb != null) {
3697 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3698 }
3699
3700 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3701 false);
3702 if (alwaysDraw) {
3703 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3704 }
3705
3706 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3707 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3708
3709 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3710 if (thumb != null) {
3711 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3712 }
3713
3714 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3715 false);
3716 if (alwaysDraw) {
3717 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3718 }
3719
3720 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003721 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723
3724 /**
3725 * <p>
3726 * Initalizes the scrollability cache if necessary.
3727 * </p>
3728 */
3729 private void initScrollCache() {
3730 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003731 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 }
3733 }
3734
Philip Milne6c8ea062012-04-03 17:38:43 -07003735 private ScrollabilityCache getScrollCache() {
3736 initScrollCache();
3737 return mScrollCache;
3738 }
3739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 /**
Adam Powell20232d02010-12-08 21:08:53 -08003741 * Set the position of the vertical scroll bar. Should be one of
3742 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3743 * {@link #SCROLLBAR_POSITION_RIGHT}.
3744 *
3745 * @param position Where the vertical scroll bar should be positioned.
3746 */
3747 public void setVerticalScrollbarPosition(int position) {
3748 if (mVerticalScrollbarPosition != position) {
3749 mVerticalScrollbarPosition = position;
3750 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003751 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003752 }
3753 }
3754
3755 /**
3756 * @return The position where the vertical scroll bar will show, if applicable.
3757 * @see #setVerticalScrollbarPosition(int)
3758 */
3759 public int getVerticalScrollbarPosition() {
3760 return mVerticalScrollbarPosition;
3761 }
3762
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003763 ListenerInfo getListenerInfo() {
3764 if (mListenerInfo != null) {
3765 return mListenerInfo;
3766 }
3767 mListenerInfo = new ListenerInfo();
3768 return mListenerInfo;
3769 }
3770
Adam Powell20232d02010-12-08 21:08:53 -08003771 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 * Register a callback to be invoked when focus of this view changed.
3773 *
3774 * @param l The callback that will run.
3775 */
3776 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003777 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 }
3779
3780 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003781 * Add a listener that will be called when the bounds of the view change due to
3782 * layout processing.
3783 *
3784 * @param listener The listener that will be called when layout bounds change.
3785 */
3786 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003787 ListenerInfo li = getListenerInfo();
3788 if (li.mOnLayoutChangeListeners == null) {
3789 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003790 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003791 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3792 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003793 }
Chet Haase21cd1382010-09-01 17:42:29 -07003794 }
3795
3796 /**
3797 * Remove a listener for layout changes.
3798 *
3799 * @param listener The listener for layout bounds change.
3800 */
3801 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003802 ListenerInfo li = mListenerInfo;
3803 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003804 return;
3805 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003806 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003807 }
3808
3809 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003810 * Add a listener for attach state changes.
3811 *
3812 * This listener will be called whenever this view is attached or detached
3813 * from a window. Remove the listener using
3814 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3815 *
3816 * @param listener Listener to attach
3817 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3818 */
3819 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003820 ListenerInfo li = getListenerInfo();
3821 if (li.mOnAttachStateChangeListeners == null) {
3822 li.mOnAttachStateChangeListeners
3823 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003824 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003825 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003826 }
3827
3828 /**
3829 * Remove a listener for attach state changes. The listener will receive no further
3830 * notification of window attach/detach events.
3831 *
3832 * @param listener Listener to remove
3833 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3834 */
3835 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003836 ListenerInfo li = mListenerInfo;
3837 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003838 return;
3839 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003840 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003841 }
3842
3843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 * Returns the focus-change callback registered for this view.
3845 *
3846 * @return The callback, or null if one is not registered.
3847 */
3848 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003849 ListenerInfo li = mListenerInfo;
3850 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 }
3852
3853 /**
3854 * Register a callback to be invoked when this view is clicked. If this view is not
3855 * clickable, it becomes clickable.
3856 *
3857 * @param l The callback that will run
3858 *
3859 * @see #setClickable(boolean)
3860 */
3861 public void setOnClickListener(OnClickListener l) {
3862 if (!isClickable()) {
3863 setClickable(true);
3864 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003865 getListenerInfo().mOnClickListener = l;
3866 }
3867
3868 /**
3869 * Return whether this view has an attached OnClickListener. Returns
3870 * true if there is a listener, false if there is none.
3871 */
3872 public boolean hasOnClickListeners() {
3873 ListenerInfo li = mListenerInfo;
3874 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 }
3876
3877 /**
3878 * Register a callback to be invoked when this view is clicked and held. If this view is not
3879 * long clickable, it becomes long clickable.
3880 *
3881 * @param l The callback that will run
3882 *
3883 * @see #setLongClickable(boolean)
3884 */
3885 public void setOnLongClickListener(OnLongClickListener l) {
3886 if (!isLongClickable()) {
3887 setLongClickable(true);
3888 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003889 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 }
3891
3892 /**
3893 * Register a callback to be invoked when the context menu for this view is
3894 * being built. If this view is not long clickable, it becomes long clickable.
3895 *
3896 * @param l The callback that will run
3897 *
3898 */
3899 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3900 if (!isLongClickable()) {
3901 setLongClickable(true);
3902 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003903 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905
3906 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003907 * Call this view's OnClickListener, if it is defined. Performs all normal
3908 * actions associated with clicking: reporting accessibility event, playing
3909 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 *
3911 * @return True there was an assigned OnClickListener that was called, false
3912 * otherwise is returned.
3913 */
3914 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003915 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3916
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003917 ListenerInfo li = mListenerInfo;
3918 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003920 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 return true;
3922 }
3923
3924 return false;
3925 }
3926
3927 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003928 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3929 * this only calls the listener, and does not do any associated clicking
3930 * actions like reporting an accessibility event.
3931 *
3932 * @return True there was an assigned OnClickListener that was called, false
3933 * otherwise is returned.
3934 */
3935 public boolean callOnClick() {
3936 ListenerInfo li = mListenerInfo;
3937 if (li != null && li.mOnClickListener != null) {
3938 li.mOnClickListener.onClick(this);
3939 return true;
3940 }
3941 return false;
3942 }
3943
3944 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003945 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3946 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003948 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 */
3950 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003951 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003954 ListenerInfo li = mListenerInfo;
3955 if (li != null && li.mOnLongClickListener != null) {
3956 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 }
3958 if (!handled) {
3959 handled = showContextMenu();
3960 }
3961 if (handled) {
3962 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3963 }
3964 return handled;
3965 }
3966
3967 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003968 * Performs button-related actions during a touch down event.
3969 *
3970 * @param event The event.
3971 * @return True if the down was consumed.
3972 *
3973 * @hide
3974 */
3975 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3976 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3977 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3978 return true;
3979 }
3980 }
3981 return false;
3982 }
3983
3984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 * Bring up the context menu for this view.
3986 *
3987 * @return Whether a context menu was displayed.
3988 */
3989 public boolean showContextMenu() {
3990 return getParent().showContextMenuForChild(this);
3991 }
3992
3993 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003994 * Bring up the context menu for this view, referring to the item under the specified point.
3995 *
3996 * @param x The referenced x coordinate.
3997 * @param y The referenced y coordinate.
3998 * @param metaState The keyboard modifiers that were pressed.
3999 * @return Whether a context menu was displayed.
4000 *
4001 * @hide
4002 */
4003 public boolean showContextMenu(float x, float y, int metaState) {
4004 return showContextMenu();
4005 }
4006
4007 /**
Adam Powell6e346362010-07-23 10:18:23 -07004008 * Start an action mode.
4009 *
4010 * @param callback Callback that will control the lifecycle of the action mode
4011 * @return The new action mode if it is started, null otherwise
4012 *
4013 * @see ActionMode
4014 */
4015 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004016 ViewParent parent = getParent();
4017 if (parent == null) return null;
4018 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004019 }
4020
4021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 * Register a callback to be invoked when a key is pressed in this view.
4023 * @param l the key listener to attach to this view
4024 */
4025 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004026 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
4028
4029 /**
4030 * Register a callback to be invoked when a touch event is sent to this view.
4031 * @param l the touch listener to attach to this view
4032 */
4033 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004034 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 }
4036
4037 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004038 * Register a callback to be invoked when a generic motion event is sent to this view.
4039 * @param l the generic motion listener to attach to this view
4040 */
4041 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004042 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004043 }
4044
4045 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004046 * Register a callback to be invoked when a hover event is sent to this view.
4047 * @param l the hover listener to attach to this view
4048 */
4049 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004050 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004051 }
4052
4053 /**
Joe Malin32736f02011-01-19 16:14:20 -08004054 * Register a drag event listener callback object for this View. The parameter is
4055 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4056 * View, the system calls the
4057 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4058 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004059 */
4060 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004061 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004062 }
4063
4064 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004065 * Give this view focus. This will cause
4066 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 *
4068 * Note: this does not check whether this {@link View} should get focus, it just
4069 * gives it focus no matter what. It should only be called internally by framework
4070 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4071 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004072 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4073 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 * focus moved when requestFocus() is called. It may not always
4075 * apply, in which case use the default View.FOCUS_DOWN.
4076 * @param previouslyFocusedRect The rectangle of the view that had focus
4077 * prior in this View's coordinate system.
4078 */
4079 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4080 if (DBG) {
4081 System.out.println(this + " requestFocus()");
4082 }
4083
4084 if ((mPrivateFlags & FOCUSED) == 0) {
4085 mPrivateFlags |= FOCUSED;
4086
4087 if (mParent != null) {
4088 mParent.requestChildFocus(this, this);
4089 }
4090
4091 onFocusChanged(true, direction, previouslyFocusedRect);
4092 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004093
4094 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4095 notifyAccessibilityStateChanged();
4096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 }
4098 }
4099
4100 /**
4101 * Request that a rectangle of this view be visible on the screen,
4102 * scrolling if necessary just enough.
4103 *
4104 * <p>A View should call this if it maintains some notion of which part
4105 * of its content is interesting. For example, a text editing view
4106 * should call this when its cursor moves.
4107 *
4108 * @param rectangle The rectangle.
4109 * @return Whether any parent scrolled.
4110 */
4111 public boolean requestRectangleOnScreen(Rect rectangle) {
4112 return requestRectangleOnScreen(rectangle, false);
4113 }
4114
4115 /**
4116 * Request that a rectangle of this view be visible on the screen,
4117 * scrolling if necessary just enough.
4118 *
4119 * <p>A View should call this if it maintains some notion of which part
4120 * of its content is interesting. For example, a text editing view
4121 * should call this when its cursor moves.
4122 *
4123 * <p>When <code>immediate</code> is set to true, scrolling will not be
4124 * animated.
4125 *
4126 * @param rectangle The rectangle.
4127 * @param immediate True to forbid animated scrolling, false otherwise
4128 * @return Whether any parent scrolled.
4129 */
4130 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4131 View child = this;
4132 ViewParent parent = mParent;
4133 boolean scrolled = false;
4134 while (parent != null) {
4135 scrolled |= parent.requestChildRectangleOnScreen(child,
4136 rectangle, immediate);
4137
4138 // offset rect so next call has the rectangle in the
4139 // coordinate system of its direct child.
4140 rectangle.offset(child.getLeft(), child.getTop());
4141 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4142
4143 if (!(parent instanceof View)) {
4144 break;
4145 }
Romain Guy8506ab42009-06-11 17:35:47 -07004146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 child = (View) parent;
4148 parent = child.getParent();
4149 }
4150 return scrolled;
4151 }
4152
4153 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004154 * Called when this view wants to give up focus. If focus is cleared
4155 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4156 * <p>
4157 * <strong>Note:</strong> When a View clears focus the framework is trying
4158 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004159 * View is the first from the top that can take focus, then all callbacks
4160 * related to clearing focus will be invoked after wich the framework will
4161 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004162 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 */
4164 public void clearFocus() {
4165 if (DBG) {
4166 System.out.println(this + " clearFocus()");
4167 }
4168
4169 if ((mPrivateFlags & FOCUSED) != 0) {
4170 mPrivateFlags &= ~FOCUSED;
4171
4172 if (mParent != null) {
4173 mParent.clearChildFocus(this);
4174 }
4175
4176 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004179
4180 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004181
4182 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4183 notifyAccessibilityStateChanged();
4184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 }
4186 }
4187
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004188 void ensureInputFocusOnFirstFocusable() {
4189 View root = getRootView();
4190 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004191 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 }
4193 }
4194
4195 /**
4196 * Called internally by the view system when a new view is getting focus.
4197 * This is what clears the old focus.
4198 */
4199 void unFocus() {
4200 if (DBG) {
4201 System.out.println(this + " unFocus()");
4202 }
4203
4204 if ((mPrivateFlags & FOCUSED) != 0) {
4205 mPrivateFlags &= ~FOCUSED;
4206
4207 onFocusChanged(false, 0, null);
4208 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004209
4210 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4211 notifyAccessibilityStateChanged();
4212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 }
4214 }
4215
4216 /**
4217 * Returns true if this view has focus iteself, or is the ancestor of the
4218 * view that has focus.
4219 *
4220 * @return True if this view has or contains focus, false otherwise.
4221 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004222 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 public boolean hasFocus() {
4224 return (mPrivateFlags & FOCUSED) != 0;
4225 }
4226
4227 /**
4228 * Returns true if this view is focusable or if it contains a reachable View
4229 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4230 * is a View whose parents do not block descendants focus.
4231 *
4232 * Only {@link #VISIBLE} views are considered focusable.
4233 *
4234 * @return True if the view is focusable or if the view contains a focusable
4235 * View, false otherwise.
4236 *
4237 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4238 */
4239 public boolean hasFocusable() {
4240 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4241 }
4242
4243 /**
4244 * Called by the view system when the focus state of this view changes.
4245 * When the focus change event is caused by directional navigation, direction
4246 * and previouslyFocusedRect provide insight into where the focus is coming from.
4247 * When overriding, be sure to call up through to the super class so that
4248 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 * @param gainFocus True if the View has focus; false otherwise.
4251 * @param direction The direction focus has moved when requestFocus()
4252 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004253 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4254 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4255 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4257 * system, of the previously focused view. If applicable, this will be
4258 * passed in as finer grained information about where the focus is coming
4259 * from (in addition to direction). Will be <code>null</code> otherwise.
4260 */
4261 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004262 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004263 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4264 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4265 requestAccessibilityFocus();
4266 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004267 }
4268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 InputMethodManager imm = InputMethodManager.peekInstance();
4270 if (!gainFocus) {
4271 if (isPressed()) {
4272 setPressed(false);
4273 }
4274 if (imm != null && mAttachInfo != null
4275 && mAttachInfo.mHasWindowFocus) {
4276 imm.focusOut(this);
4277 }
Romain Guya2431d02009-04-30 16:30:00 -07004278 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 } else if (imm != null && mAttachInfo != null
4280 && mAttachInfo.mHasWindowFocus) {
4281 imm.focusIn(this);
4282 }
Romain Guy8506ab42009-06-11 17:35:47 -07004283
Romain Guy0fd89bf2011-01-26 15:41:30 -08004284 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004285 ListenerInfo li = mListenerInfo;
4286 if (li != null && li.mOnFocusChangeListener != null) {
4287 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
Joe Malin32736f02011-01-19 16:14:20 -08004289
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004290 if (mAttachInfo != null) {
4291 mAttachInfo.mKeyDispatchState.reset(this);
4292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 }
4294
4295 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004296 * Sends an accessibility event of the given type. If accessiiblity is
4297 * not enabled this method has no effect. The default implementation calls
4298 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4299 * to populate information about the event source (this View), then calls
4300 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4301 * populate the text content of the event source including its descendants,
4302 * and last calls
4303 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4304 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004305 * <p>
4306 * If an {@link AccessibilityDelegate} has been specified via calling
4307 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4308 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4309 * responsible for handling this call.
4310 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004311 *
Scott Mainb303d832011-10-12 16:45:18 -07004312 * @param eventType The type of the event to send, as defined by several types from
4313 * {@link android.view.accessibility.AccessibilityEvent}, such as
4314 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4315 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004316 *
4317 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4318 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4319 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004320 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004321 */
4322 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004323 if (mAccessibilityDelegate != null) {
4324 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4325 } else {
4326 sendAccessibilityEventInternal(eventType);
4327 }
4328 }
4329
4330 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004331 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4332 * {@link AccessibilityEvent} to make an announcement which is related to some
4333 * sort of a context change for which none of the events representing UI transitions
4334 * is a good fit. For example, announcing a new page in a book. If accessibility
4335 * is not enabled this method does nothing.
4336 *
4337 * @param text The announcement text.
4338 */
4339 public void announceForAccessibility(CharSequence text) {
4340 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4341 AccessibilityEvent event = AccessibilityEvent.obtain(
4342 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4343 event.getText().add(text);
4344 sendAccessibilityEventUnchecked(event);
4345 }
4346 }
4347
4348 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004349 * @see #sendAccessibilityEvent(int)
4350 *
4351 * Note: Called from the default {@link AccessibilityDelegate}.
4352 */
4353 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004354 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4355 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4356 }
4357 }
4358
4359 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004360 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4361 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004362 * perform a check whether accessibility is enabled.
4363 * <p>
4364 * If an {@link AccessibilityDelegate} has been specified via calling
4365 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4366 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4367 * is responsible for handling this call.
4368 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004369 *
4370 * @param event The event to send.
4371 *
4372 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004373 */
4374 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004375 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004376 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004377 } else {
4378 sendAccessibilityEventUncheckedInternal(event);
4379 }
4380 }
4381
4382 /**
4383 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4384 *
4385 * Note: Called from the default {@link AccessibilityDelegate}.
4386 */
4387 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004388 if (!isShown()) {
4389 return;
4390 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004391 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004392 // Only a subset of accessibility events populates text content.
4393 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4394 dispatchPopulateAccessibilityEvent(event);
4395 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004396 // Intercept accessibility focus events fired by virtual nodes to keep
4397 // track of accessibility focus position in such nodes.
4398 final int eventType = event.getEventType();
4399 switch (eventType) {
4400 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4401 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4402 event.getSourceNodeId());
4403 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4404 ViewRootImpl viewRootImpl = getViewRootImpl();
4405 if (viewRootImpl != null) {
4406 viewRootImpl.setAccessibilityFocusedHost(this);
4407 }
4408 }
4409 } break;
4410 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4411 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4412 event.getSourceNodeId());
4413 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4414 ViewRootImpl viewRootImpl = getViewRootImpl();
4415 if (viewRootImpl != null) {
4416 viewRootImpl.setAccessibilityFocusedHost(null);
4417 }
4418 }
4419 } break;
4420 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004421 // In the beginning we called #isShown(), so we know that getParent() is not null.
4422 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004423 }
4424
4425 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004426 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4427 * to its children for adding their text content to the event. Note that the
4428 * event text is populated in a separate dispatch path since we add to the
4429 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004430 * A typical implementation will call
4431 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4432 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4433 * on each child. Override this method if custom population of the event text
4434 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004435 * <p>
4436 * If an {@link AccessibilityDelegate} has been specified via calling
4437 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4438 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4439 * is responsible for handling this call.
4440 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004441 * <p>
4442 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4443 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4444 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004445 *
4446 * @param event The event.
4447 *
4448 * @return True if the event population was completed.
4449 */
4450 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004451 if (mAccessibilityDelegate != null) {
4452 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4453 } else {
4454 return dispatchPopulateAccessibilityEventInternal(event);
4455 }
4456 }
4457
4458 /**
4459 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4460 *
4461 * Note: Called from the default {@link AccessibilityDelegate}.
4462 */
4463 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004464 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004465 return false;
4466 }
4467
4468 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004469 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004470 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004471 * text content. While this method is free to modify event
4472 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004473 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4474 * <p>
4475 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004476 * to the text added by the super implementation:
4477 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004478 * super.onPopulateAccessibilityEvent(event);
4479 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4480 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4481 * mCurrentDate.getTimeInMillis(), flags);
4482 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004483 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004484 * <p>
4485 * If an {@link AccessibilityDelegate} has been specified via calling
4486 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4487 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4488 * is responsible for handling this call.
4489 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004490 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4491 * information to the event, in case the default implementation has basic information to add.
4492 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004493 *
4494 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004495 *
4496 * @see #sendAccessibilityEvent(int)
4497 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004498 */
4499 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004500 if (mAccessibilityDelegate != null) {
4501 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4502 } else {
4503 onPopulateAccessibilityEventInternal(event);
4504 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004505 }
4506
4507 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004508 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4509 *
4510 * Note: Called from the default {@link AccessibilityDelegate}.
4511 */
4512 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4513
4514 }
4515
4516 /**
4517 * Initializes an {@link AccessibilityEvent} with information about
4518 * this View which is the event source. In other words, the source of
4519 * an accessibility event is the view whose state change triggered firing
4520 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004521 * <p>
4522 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004523 * to properties set by the super implementation:
4524 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4525 * super.onInitializeAccessibilityEvent(event);
4526 * event.setPassword(true);
4527 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004528 * <p>
4529 * If an {@link AccessibilityDelegate} has been specified via calling
4530 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4531 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4532 * is responsible for handling this call.
4533 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004534 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4535 * information to the event, in case the default implementation has basic information to add.
4536 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004537 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004538 *
4539 * @see #sendAccessibilityEvent(int)
4540 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4541 */
4542 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004543 if (mAccessibilityDelegate != null) {
4544 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4545 } else {
4546 onInitializeAccessibilityEventInternal(event);
4547 }
4548 }
4549
4550 /**
4551 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4552 *
4553 * Note: Called from the default {@link AccessibilityDelegate}.
4554 */
4555 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004556 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004557 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004558 event.setPackageName(getContext().getPackageName());
4559 event.setEnabled(isEnabled());
4560 event.setContentDescription(mContentDescription);
4561
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004562 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004563 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004564 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4565 FOCUSABLES_ALL);
4566 event.setItemCount(focusablesTempList.size());
4567 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4568 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004569 }
4570 }
4571
4572 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004573 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4574 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4575 * This method is responsible for obtaining an accessibility node info from a
4576 * pool of reusable instances and calling
4577 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4578 * initialize the former.
4579 * <p>
4580 * Note: The client is responsible for recycling the obtained instance by calling
4581 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4582 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004583 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004584 * @return A populated {@link AccessibilityNodeInfo}.
4585 *
4586 * @see AccessibilityNodeInfo
4587 */
4588 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004589 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4590 if (provider != null) {
4591 return provider.createAccessibilityNodeInfo(View.NO_ID);
4592 } else {
4593 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4594 onInitializeAccessibilityNodeInfo(info);
4595 return info;
4596 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004597 }
4598
4599 /**
4600 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4601 * The base implementation sets:
4602 * <ul>
4603 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004604 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4605 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004606 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4607 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4608 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4609 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4610 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4611 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4612 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4613 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4614 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4615 * </ul>
4616 * <p>
4617 * Subclasses should override this method, call the super implementation,
4618 * and set additional attributes.
4619 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004620 * <p>
4621 * If an {@link AccessibilityDelegate} has been specified via calling
4622 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4623 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4624 * is responsible for handling this call.
4625 * </p>
4626 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004627 * @param info The instance to initialize.
4628 */
4629 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004630 if (mAccessibilityDelegate != null) {
4631 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4632 } else {
4633 onInitializeAccessibilityNodeInfoInternal(info);
4634 }
4635 }
4636
4637 /**
4638 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4639 *
4640 * Note: Called from the default {@link AccessibilityDelegate}.
4641 */
4642 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004643 Rect bounds = mAttachInfo.mTmpInvalRect;
4644 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004645 info.setBoundsInParent(bounds);
4646
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004647 getGlobalVisibleRect(bounds);
4648 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004649 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004650
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004651 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004652 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004653 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004654 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004655 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004656 }
4657
4658 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004659 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004660 info.setContentDescription(getContentDescription());
4661
4662 info.setEnabled(isEnabled());
4663 info.setClickable(isClickable());
4664 info.setFocusable(isFocusable());
4665 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004666 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004667 info.setSelected(isSelected());
4668 info.setLongClickable(isLongClickable());
4669
4670 // TODO: These make sense only if we are in an AdapterView but all
4671 // views can be selected. Maybe from accessiiblity perspective
4672 // we should report as selectable view in an AdapterView.
4673 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4674 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4675
4676 if (isFocusable()) {
4677 if (isFocused()) {
4678 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4679 } else {
4680 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4681 }
4682 }
4683 }
4684
4685 /**
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004686 * Computes whether this view is visible on the screen.
4687 *
4688 * @return Whether the view is visible on the screen.
4689 */
4690 boolean isDisplayedOnScreen() {
4691 // The first two checks are made also made by isShown() which
4692 // however traverses the tree up to the parent to catch that.
4693 // Therefore, we do some fail fast check to minimize the up
4694 // tree traversal.
4695 return (mAttachInfo != null
4696 && mAttachInfo.mWindowVisibility == View.VISIBLE
4697 && getAlpha() > 0
4698 && isShown()
4699 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4700 }
4701
4702 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004703 * Sets a delegate for implementing accessibility support via compositon as
4704 * opposed to inheritance. The delegate's primary use is for implementing
4705 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4706 *
4707 * @param delegate The delegate instance.
4708 *
4709 * @see AccessibilityDelegate
4710 */
4711 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4712 mAccessibilityDelegate = delegate;
4713 }
4714
4715 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004716 * Gets the provider for managing a virtual view hierarchy rooted at this View
4717 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4718 * that explore the window content.
4719 * <p>
4720 * If this method returns an instance, this instance is responsible for managing
4721 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4722 * View including the one representing the View itself. Similarly the returned
4723 * instance is responsible for performing accessibility actions on any virtual
4724 * view or the root view itself.
4725 * </p>
4726 * <p>
4727 * If an {@link AccessibilityDelegate} has been specified via calling
4728 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4729 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4730 * is responsible for handling this call.
4731 * </p>
4732 *
4733 * @return The provider.
4734 *
4735 * @see AccessibilityNodeProvider
4736 */
4737 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4738 if (mAccessibilityDelegate != null) {
4739 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4740 } else {
4741 return null;
4742 }
4743 }
4744
4745 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004746 * Gets the unique identifier of this view on the screen for accessibility purposes.
4747 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4748 *
4749 * @return The view accessibility id.
4750 *
4751 * @hide
4752 */
4753 public int getAccessibilityViewId() {
4754 if (mAccessibilityViewId == NO_ID) {
4755 mAccessibilityViewId = sNextAccessibilityViewId++;
4756 }
4757 return mAccessibilityViewId;
4758 }
4759
4760 /**
4761 * Gets the unique identifier of the window in which this View reseides.
4762 *
4763 * @return The window accessibility id.
4764 *
4765 * @hide
4766 */
4767 public int getAccessibilityWindowId() {
4768 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4769 }
4770
4771 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004772 * Gets the {@link View} description. It briefly describes the view and is
4773 * primarily used for accessibility support. Set this property to enable
4774 * better accessibility support for your application. This is especially
4775 * true for views that do not have textual representation (For example,
4776 * ImageButton).
4777 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004778 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004779 *
4780 * @attr ref android.R.styleable#View_contentDescription
4781 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004782 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004783 public CharSequence getContentDescription() {
4784 return mContentDescription;
4785 }
4786
4787 /**
4788 * Sets the {@link View} description. It briefly describes the view and is
4789 * primarily used for accessibility support. Set this property to enable
4790 * better accessibility support for your application. This is especially
4791 * true for views that do not have textual representation (For example,
4792 * ImageButton).
4793 *
4794 * @param contentDescription The content description.
4795 *
4796 * @attr ref android.R.styleable#View_contentDescription
4797 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004798 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004799 public void setContentDescription(CharSequence contentDescription) {
4800 mContentDescription = contentDescription;
4801 }
4802
4803 /**
Romain Guya2431d02009-04-30 16:30:00 -07004804 * Invoked whenever this view loses focus, either by losing window focus or by losing
4805 * focus within its window. This method can be used to clear any state tied to the
4806 * focus. For instance, if a button is held pressed with the trackball and the window
4807 * loses focus, this method can be used to cancel the press.
4808 *
4809 * Subclasses of View overriding this method should always call super.onFocusLost().
4810 *
4811 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004812 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004813 *
4814 * @hide pending API council approval
4815 */
4816 protected void onFocusLost() {
4817 resetPressedState();
4818 }
4819
4820 private void resetPressedState() {
4821 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4822 return;
4823 }
4824
4825 if (isPressed()) {
4826 setPressed(false);
4827
4828 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004829 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004830 }
4831 }
4832 }
4833
4834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 * Returns true if this view has focus
4836 *
4837 * @return True if this view has focus, false otherwise.
4838 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004839 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 public boolean isFocused() {
4841 return (mPrivateFlags & FOCUSED) != 0;
4842 }
4843
4844 /**
4845 * Find the view in the hierarchy rooted at this view that currently has
4846 * focus.
4847 *
4848 * @return The view that currently has focus, or null if no focused view can
4849 * be found.
4850 */
4851 public View findFocus() {
4852 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4853 }
4854
4855 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004856 * Indicates whether this view is one of the set of scrollable containers in
4857 * its window.
4858 *
4859 * @return whether this view is one of the set of scrollable containers in
4860 * its window
4861 *
4862 * @attr ref android.R.styleable#View_isScrollContainer
4863 */
4864 public boolean isScrollContainer() {
4865 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4866 }
4867
4868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 * Change whether this view is one of the set of scrollable containers in
4870 * its window. This will be used to determine whether the window can
4871 * resize or must pan when a soft input area is open -- scrollable
4872 * containers allow the window to use resize mode since the container
4873 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004874 *
4875 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 */
4877 public void setScrollContainer(boolean isScrollContainer) {
4878 if (isScrollContainer) {
4879 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4880 mAttachInfo.mScrollContainers.add(this);
4881 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4882 }
4883 mPrivateFlags |= SCROLL_CONTAINER;
4884 } else {
4885 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4886 mAttachInfo.mScrollContainers.remove(this);
4887 }
4888 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4889 }
4890 }
4891
4892 /**
4893 * Returns the quality of the drawing cache.
4894 *
4895 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4896 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4897 *
4898 * @see #setDrawingCacheQuality(int)
4899 * @see #setDrawingCacheEnabled(boolean)
4900 * @see #isDrawingCacheEnabled()
4901 *
4902 * @attr ref android.R.styleable#View_drawingCacheQuality
4903 */
4904 public int getDrawingCacheQuality() {
4905 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4906 }
4907
4908 /**
4909 * Set the drawing cache quality of this view. This value is used only when the
4910 * drawing cache is enabled
4911 *
4912 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4913 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4914 *
4915 * @see #getDrawingCacheQuality()
4916 * @see #setDrawingCacheEnabled(boolean)
4917 * @see #isDrawingCacheEnabled()
4918 *
4919 * @attr ref android.R.styleable#View_drawingCacheQuality
4920 */
4921 public void setDrawingCacheQuality(int quality) {
4922 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4923 }
4924
4925 /**
4926 * Returns whether the screen should remain on, corresponding to the current
4927 * value of {@link #KEEP_SCREEN_ON}.
4928 *
4929 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4930 *
4931 * @see #setKeepScreenOn(boolean)
4932 *
4933 * @attr ref android.R.styleable#View_keepScreenOn
4934 */
4935 public boolean getKeepScreenOn() {
4936 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4937 }
4938
4939 /**
4940 * Controls whether the screen should remain on, modifying the
4941 * value of {@link #KEEP_SCREEN_ON}.
4942 *
4943 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4944 *
4945 * @see #getKeepScreenOn()
4946 *
4947 * @attr ref android.R.styleable#View_keepScreenOn
4948 */
4949 public void setKeepScreenOn(boolean keepScreenOn) {
4950 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4951 }
4952
4953 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004954 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4955 * @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 -08004956 *
4957 * @attr ref android.R.styleable#View_nextFocusLeft
4958 */
4959 public int getNextFocusLeftId() {
4960 return mNextFocusLeftId;
4961 }
4962
4963 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004964 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4965 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4966 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 *
4968 * @attr ref android.R.styleable#View_nextFocusLeft
4969 */
4970 public void setNextFocusLeftId(int nextFocusLeftId) {
4971 mNextFocusLeftId = nextFocusLeftId;
4972 }
4973
4974 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004975 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4976 * @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 -08004977 *
4978 * @attr ref android.R.styleable#View_nextFocusRight
4979 */
4980 public int getNextFocusRightId() {
4981 return mNextFocusRightId;
4982 }
4983
4984 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004985 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4986 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4987 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 *
4989 * @attr ref android.R.styleable#View_nextFocusRight
4990 */
4991 public void setNextFocusRightId(int nextFocusRightId) {
4992 mNextFocusRightId = nextFocusRightId;
4993 }
4994
4995 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004996 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4997 * @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 -08004998 *
4999 * @attr ref android.R.styleable#View_nextFocusUp
5000 */
5001 public int getNextFocusUpId() {
5002 return mNextFocusUpId;
5003 }
5004
5005 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005006 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5007 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5008 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 *
5010 * @attr ref android.R.styleable#View_nextFocusUp
5011 */
5012 public void setNextFocusUpId(int nextFocusUpId) {
5013 mNextFocusUpId = nextFocusUpId;
5014 }
5015
5016 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005017 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5018 * @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 -08005019 *
5020 * @attr ref android.R.styleable#View_nextFocusDown
5021 */
5022 public int getNextFocusDownId() {
5023 return mNextFocusDownId;
5024 }
5025
5026 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005027 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5028 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5029 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 *
5031 * @attr ref android.R.styleable#View_nextFocusDown
5032 */
5033 public void setNextFocusDownId(int nextFocusDownId) {
5034 mNextFocusDownId = nextFocusDownId;
5035 }
5036
5037 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005038 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5039 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5040 *
5041 * @attr ref android.R.styleable#View_nextFocusForward
5042 */
5043 public int getNextFocusForwardId() {
5044 return mNextFocusForwardId;
5045 }
5046
5047 /**
5048 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5049 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5050 * decide automatically.
5051 *
5052 * @attr ref android.R.styleable#View_nextFocusForward
5053 */
5054 public void setNextFocusForwardId(int nextFocusForwardId) {
5055 mNextFocusForwardId = nextFocusForwardId;
5056 }
5057
5058 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 * Returns the visibility of this view and all of its ancestors
5060 *
5061 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5062 */
5063 public boolean isShown() {
5064 View current = this;
5065 //noinspection ConstantConditions
5066 do {
5067 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5068 return false;
5069 }
5070 ViewParent parent = current.mParent;
5071 if (parent == null) {
5072 return false; // We are not attached to the view root
5073 }
5074 if (!(parent instanceof View)) {
5075 return true;
5076 }
5077 current = (View) parent;
5078 } while (current != null);
5079
5080 return false;
5081 }
5082
5083 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005084 * Called by the view hierarchy when the content insets for a window have
5085 * changed, to allow it to adjust its content to fit within those windows.
5086 * The content insets tell you the space that the status bar, input method,
5087 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005089 * <p>You do not normally need to deal with this function, since the default
5090 * window decoration given to applications takes care of applying it to the
5091 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5092 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5093 * and your content can be placed under those system elements. You can then
5094 * use this method within your view hierarchy if you have parts of your UI
5095 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005097 * <p>The default implementation of this method simply applies the content
5098 * inset's to the view's padding. This can be enabled through
5099 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5100 * the method and handle the insets however you would like. Note that the
5101 * insets provided by the framework are always relative to the far edges
5102 * of the window, not accounting for the location of the called view within
5103 * that window. (In fact when this method is called you do not yet know
5104 * where the layout will place the view, as it is done before layout happens.)
5105 *
5106 * <p>Note: unlike many View methods, there is no dispatch phase to this
5107 * call. If you are overriding it in a ViewGroup and want to allow the
5108 * call to continue to your children, you must be sure to call the super
5109 * implementation.
5110 *
5111 * @param insets Current content insets of the window. Prior to
5112 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5113 * the insets or else you and Android will be unhappy.
5114 *
5115 * @return Return true if this view applied the insets and it should not
5116 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 */
5118 protected boolean fitSystemWindows(Rect insets) {
5119 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005120 mUserPaddingStart = -1;
5121 mUserPaddingEnd = -1;
5122 mUserPaddingRelative = false;
5123 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5124 || mAttachInfo == null
5125 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5126 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5127 return true;
5128 } else {
5129 internalSetPadding(0, 0, 0, 0);
5130 return false;
5131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 }
5133 return false;
5134 }
5135
5136 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005137 * Set whether or not this view should account for system screen decorations
5138 * such as the status bar and inset its content. This allows this view to be
5139 * positioned in absolute screen coordinates and remain visible to the user.
5140 *
5141 * <p>This should only be used by top-level window decor views.
5142 *
5143 * @param fitSystemWindows true to inset content for system screen decorations, false for
5144 * default behavior.
5145 *
5146 * @attr ref android.R.styleable#View_fitsSystemWindows
5147 */
5148 public void setFitsSystemWindows(boolean fitSystemWindows) {
5149 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5150 }
5151
5152 /**
5153 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5154 * will account for system screen decorations such as the status bar and inset its
5155 * content. This allows the view to be positioned in absolute screen coordinates
5156 * and remain visible to the user.
5157 *
5158 * @return true if this view will adjust its content bounds for system screen decorations.
5159 *
5160 * @attr ref android.R.styleable#View_fitsSystemWindows
5161 */
5162 public boolean fitsSystemWindows() {
5163 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5164 }
5165
5166 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005167 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5168 */
5169 public void requestFitSystemWindows() {
5170 if (mParent != null) {
5171 mParent.requestFitSystemWindows();
5172 }
5173 }
5174
5175 /**
5176 * For use by PhoneWindow to make its own system window fitting optional.
5177 * @hide
5178 */
5179 public void makeOptionalFitsSystemWindows() {
5180 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5181 }
5182
5183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 * Returns the visibility status for this view.
5185 *
5186 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5187 * @attr ref android.R.styleable#View_visibility
5188 */
5189 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005190 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5191 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5192 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 })
5194 public int getVisibility() {
5195 return mViewFlags & VISIBILITY_MASK;
5196 }
5197
5198 /**
5199 * Set the enabled state of this view.
5200 *
5201 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5202 * @attr ref android.R.styleable#View_visibility
5203 */
5204 @RemotableViewMethod
5205 public void setVisibility(int visibility) {
5206 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005207 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 }
5209
5210 /**
5211 * Returns the enabled status for this view. The interpretation of the
5212 * enabled state varies by subclass.
5213 *
5214 * @return True if this view is enabled, false otherwise.
5215 */
5216 @ViewDebug.ExportedProperty
5217 public boolean isEnabled() {
5218 return (mViewFlags & ENABLED_MASK) == ENABLED;
5219 }
5220
5221 /**
5222 * Set the enabled state of this view. The interpretation of the enabled
5223 * state varies by subclass.
5224 *
5225 * @param enabled True if this view is enabled, false otherwise.
5226 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005227 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005229 if (enabled == isEnabled()) return;
5230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5232
5233 /*
5234 * The View most likely has to change its appearance, so refresh
5235 * the drawable state.
5236 */
5237 refreshDrawableState();
5238
5239 // Invalidate too, since the default behavior for views is to be
5240 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005241 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 }
5243
5244 /**
5245 * Set whether this view can receive the focus.
5246 *
5247 * Setting this to false will also ensure that this view is not focusable
5248 * in touch mode.
5249 *
5250 * @param focusable If true, this view can receive the focus.
5251 *
5252 * @see #setFocusableInTouchMode(boolean)
5253 * @attr ref android.R.styleable#View_focusable
5254 */
5255 public void setFocusable(boolean focusable) {
5256 if (!focusable) {
5257 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5258 }
5259 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5260 }
5261
5262 /**
5263 * Set whether this view can receive focus while in touch mode.
5264 *
5265 * Setting this to true will also ensure that this view is focusable.
5266 *
5267 * @param focusableInTouchMode If true, this view can receive the focus while
5268 * in touch mode.
5269 *
5270 * @see #setFocusable(boolean)
5271 * @attr ref android.R.styleable#View_focusableInTouchMode
5272 */
5273 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5274 // Focusable in touch mode should always be set before the focusable flag
5275 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5276 // which, in touch mode, will not successfully request focus on this view
5277 // because the focusable in touch mode flag is not set
5278 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5279 if (focusableInTouchMode) {
5280 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5281 }
5282 }
5283
5284 /**
5285 * Set whether this view should have sound effects enabled for events such as
5286 * clicking and touching.
5287 *
5288 * <p>You may wish to disable sound effects for a view if you already play sounds,
5289 * for instance, a dial key that plays dtmf tones.
5290 *
5291 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5292 * @see #isSoundEffectsEnabled()
5293 * @see #playSoundEffect(int)
5294 * @attr ref android.R.styleable#View_soundEffectsEnabled
5295 */
5296 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5297 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5298 }
5299
5300 /**
5301 * @return whether this view should have sound effects enabled for events such as
5302 * clicking and touching.
5303 *
5304 * @see #setSoundEffectsEnabled(boolean)
5305 * @see #playSoundEffect(int)
5306 * @attr ref android.R.styleable#View_soundEffectsEnabled
5307 */
5308 @ViewDebug.ExportedProperty
5309 public boolean isSoundEffectsEnabled() {
5310 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5311 }
5312
5313 /**
5314 * Set whether this view should have haptic feedback for events such as
5315 * long presses.
5316 *
5317 * <p>You may wish to disable haptic feedback if your view already controls
5318 * its own haptic feedback.
5319 *
5320 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5321 * @see #isHapticFeedbackEnabled()
5322 * @see #performHapticFeedback(int)
5323 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5324 */
5325 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5326 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5327 }
5328
5329 /**
5330 * @return whether this view should have haptic feedback enabled for events
5331 * long presses.
5332 *
5333 * @see #setHapticFeedbackEnabled(boolean)
5334 * @see #performHapticFeedback(int)
5335 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5336 */
5337 @ViewDebug.ExportedProperty
5338 public boolean isHapticFeedbackEnabled() {
5339 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5340 }
5341
5342 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005343 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005344 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005345 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5346 * {@link #LAYOUT_DIRECTION_RTL},
5347 * {@link #LAYOUT_DIRECTION_INHERIT} or
5348 * {@link #LAYOUT_DIRECTION_LOCALE}.
5349 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005350 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005351 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005352 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5353 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5354 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5355 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005356 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005357 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005358 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005359 }
5360
5361 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005362 * Set the layout direction for this view. This will propagate a reset of layout direction
5363 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005364 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005365 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5366 * {@link #LAYOUT_DIRECTION_RTL},
5367 * {@link #LAYOUT_DIRECTION_INHERIT} or
5368 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005369 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005370 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005371 */
5372 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005373 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005374 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005375 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005376 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005377 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005378 // Set the new layout direction (filtered) and ask for a layout pass
5379 mPrivateFlags2 |=
5380 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5381 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005382 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005383 }
5384
5385 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005386 * Returns the resolved layout direction for this view.
5387 *
5388 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005389 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005390 */
5391 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005392 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5393 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005394 })
5395 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005396 // The layout diretion will be resolved only if needed
5397 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5398 resolveLayoutDirection();
5399 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005400 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005401 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5402 }
5403
5404 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005405 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5406 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005407 *
5408 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005409 */
5410 @ViewDebug.ExportedProperty(category = "layout")
5411 public boolean isLayoutRtl() {
5412 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5413 }
5414
5415 /**
Adam Powell539ee872012-02-03 19:00:49 -08005416 * Indicates whether the view is currently tracking transient state that the
5417 * app should not need to concern itself with saving and restoring, but that
5418 * the framework should take special note to preserve when possible.
5419 *
5420 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005421 */
5422 @ViewDebug.ExportedProperty(category = "layout")
5423 public boolean hasTransientState() {
5424 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5425 }
5426
5427 /**
5428 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005429 * framework should attempt to preserve when possible. This flag is reference counted,
5430 * so every call to setHasTransientState(true) should be paired with a later call
5431 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005432 *
5433 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005434 */
5435 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005436 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5437 mTransientStateCount - 1;
5438 if (mTransientStateCount < 0) {
5439 mTransientStateCount = 0;
5440 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5441 "unmatched pair of setHasTransientState calls");
5442 }
5443 if ((hasTransientState && mTransientStateCount == 1) ||
5444 (hasTransientState && mTransientStateCount == 0)) {
5445 // update flag if we've just incremented up from 0 or decremented down to 0
5446 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5447 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5448 if (mParent != null) {
5449 try {
5450 mParent.childHasTransientStateChanged(this, hasTransientState);
5451 } catch (AbstractMethodError e) {
5452 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5453 " does not fully implement ViewParent", e);
5454 }
Adam Powell539ee872012-02-03 19:00:49 -08005455 }
5456 }
5457 }
5458
5459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 * If this view doesn't do any drawing on its own, set this flag to
5461 * allow further optimizations. By default, this flag is not set on
5462 * View, but could be set on some View subclasses such as ViewGroup.
5463 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005464 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5465 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 *
5467 * @param willNotDraw whether or not this View draw on its own
5468 */
5469 public void setWillNotDraw(boolean willNotDraw) {
5470 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5471 }
5472
5473 /**
5474 * Returns whether or not this View draws on its own.
5475 *
5476 * @return true if this view has nothing to draw, false otherwise
5477 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005478 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 public boolean willNotDraw() {
5480 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5481 }
5482
5483 /**
5484 * When a View's drawing cache is enabled, drawing is redirected to an
5485 * offscreen bitmap. Some views, like an ImageView, must be able to
5486 * bypass this mechanism if they already draw a single bitmap, to avoid
5487 * unnecessary usage of the memory.
5488 *
5489 * @param willNotCacheDrawing true if this view does not cache its
5490 * drawing, false otherwise
5491 */
5492 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5493 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5494 }
5495
5496 /**
5497 * Returns whether or not this View can cache its drawing or not.
5498 *
5499 * @return true if this view does not cache its drawing, false otherwise
5500 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005501 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 public boolean willNotCacheDrawing() {
5503 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5504 }
5505
5506 /**
5507 * Indicates whether this view reacts to click events or not.
5508 *
5509 * @return true if the view is clickable, false otherwise
5510 *
5511 * @see #setClickable(boolean)
5512 * @attr ref android.R.styleable#View_clickable
5513 */
5514 @ViewDebug.ExportedProperty
5515 public boolean isClickable() {
5516 return (mViewFlags & CLICKABLE) == CLICKABLE;
5517 }
5518
5519 /**
5520 * Enables or disables click events for this view. When a view
5521 * is clickable it will change its state to "pressed" on every click.
5522 * Subclasses should set the view clickable to visually react to
5523 * user's clicks.
5524 *
5525 * @param clickable true to make the view clickable, false otherwise
5526 *
5527 * @see #isClickable()
5528 * @attr ref android.R.styleable#View_clickable
5529 */
5530 public void setClickable(boolean clickable) {
5531 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5532 }
5533
5534 /**
5535 * Indicates whether this view reacts to long click events or not.
5536 *
5537 * @return true if the view is long clickable, false otherwise
5538 *
5539 * @see #setLongClickable(boolean)
5540 * @attr ref android.R.styleable#View_longClickable
5541 */
5542 public boolean isLongClickable() {
5543 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5544 }
5545
5546 /**
5547 * Enables or disables long click events for this view. When a view is long
5548 * clickable it reacts to the user holding down the button for a longer
5549 * duration than a tap. This event can either launch the listener or a
5550 * context menu.
5551 *
5552 * @param longClickable true to make the view long clickable, false otherwise
5553 * @see #isLongClickable()
5554 * @attr ref android.R.styleable#View_longClickable
5555 */
5556 public void setLongClickable(boolean longClickable) {
5557 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5558 }
5559
5560 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005561 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 *
5563 * @see #isClickable()
5564 * @see #setClickable(boolean)
5565 *
5566 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5567 * the View's internal state from a previously set "pressed" state.
5568 */
5569 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005570 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 if (pressed) {
5573 mPrivateFlags |= PRESSED;
5574 } else {
5575 mPrivateFlags &= ~PRESSED;
5576 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005577
5578 if (needsRefresh) {
5579 refreshDrawableState();
5580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 dispatchSetPressed(pressed);
5582 }
5583
5584 /**
5585 * Dispatch setPressed to all of this View's children.
5586 *
5587 * @see #setPressed(boolean)
5588 *
5589 * @param pressed The new pressed state
5590 */
5591 protected void dispatchSetPressed(boolean pressed) {
5592 }
5593
5594 /**
5595 * Indicates whether the view is currently in pressed state. Unless
5596 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5597 * the pressed state.
5598 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005599 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 * @see #isClickable()
5601 * @see #setClickable(boolean)
5602 *
5603 * @return true if the view is currently pressed, false otherwise
5604 */
5605 public boolean isPressed() {
5606 return (mPrivateFlags & PRESSED) == PRESSED;
5607 }
5608
5609 /**
5610 * Indicates whether this view will save its state (that is,
5611 * whether its {@link #onSaveInstanceState} method will be called).
5612 *
5613 * @return Returns true if the view state saving is enabled, else false.
5614 *
5615 * @see #setSaveEnabled(boolean)
5616 * @attr ref android.R.styleable#View_saveEnabled
5617 */
5618 public boolean isSaveEnabled() {
5619 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5620 }
5621
5622 /**
5623 * Controls whether the saving of this view's state is
5624 * enabled (that is, whether its {@link #onSaveInstanceState} method
5625 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005626 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 * for its state to be saved. This flag can only disable the
5628 * saving of this view; any child views may still have their state saved.
5629 *
5630 * @param enabled Set to false to <em>disable</em> state saving, or true
5631 * (the default) to allow it.
5632 *
5633 * @see #isSaveEnabled()
5634 * @see #setId(int)
5635 * @see #onSaveInstanceState()
5636 * @attr ref android.R.styleable#View_saveEnabled
5637 */
5638 public void setSaveEnabled(boolean enabled) {
5639 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5640 }
5641
Jeff Brown85a31762010-09-01 17:01:00 -07005642 /**
5643 * Gets whether the framework should discard touches when the view's
5644 * window is obscured by another visible window.
5645 * Refer to the {@link View} security documentation for more details.
5646 *
5647 * @return True if touch filtering is enabled.
5648 *
5649 * @see #setFilterTouchesWhenObscured(boolean)
5650 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5651 */
5652 @ViewDebug.ExportedProperty
5653 public boolean getFilterTouchesWhenObscured() {
5654 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5655 }
5656
5657 /**
5658 * Sets whether the framework should discard touches when the view's
5659 * window is obscured by another visible window.
5660 * Refer to the {@link View} security documentation for more details.
5661 *
5662 * @param enabled True if touch filtering should be enabled.
5663 *
5664 * @see #getFilterTouchesWhenObscured
5665 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5666 */
5667 public void setFilterTouchesWhenObscured(boolean enabled) {
5668 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5669 FILTER_TOUCHES_WHEN_OBSCURED);
5670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671
5672 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005673 * Indicates whether the entire hierarchy under this view will save its
5674 * state when a state saving traversal occurs from its parent. The default
5675 * is true; if false, these views will not be saved unless
5676 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5677 *
5678 * @return Returns true if the view state saving from parent is enabled, else false.
5679 *
5680 * @see #setSaveFromParentEnabled(boolean)
5681 */
5682 public boolean isSaveFromParentEnabled() {
5683 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5684 }
5685
5686 /**
5687 * Controls whether the entire hierarchy under this view will save its
5688 * state when a state saving traversal occurs from its parent. The default
5689 * is true; if false, these views will not be saved unless
5690 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5691 *
5692 * @param enabled Set to false to <em>disable</em> state saving, or true
5693 * (the default) to allow it.
5694 *
5695 * @see #isSaveFromParentEnabled()
5696 * @see #setId(int)
5697 * @see #onSaveInstanceState()
5698 */
5699 public void setSaveFromParentEnabled(boolean enabled) {
5700 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5701 }
5702
5703
5704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 * Returns whether this View is able to take focus.
5706 *
5707 * @return True if this view can take focus, or false otherwise.
5708 * @attr ref android.R.styleable#View_focusable
5709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005710 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005711 public final boolean isFocusable() {
5712 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5713 }
5714
5715 /**
5716 * When a view is focusable, it may not want to take focus when in touch mode.
5717 * For example, a button would like focus when the user is navigating via a D-pad
5718 * so that the user can click on it, but once the user starts touching the screen,
5719 * the button shouldn't take focus
5720 * @return Whether the view is focusable in touch mode.
5721 * @attr ref android.R.styleable#View_focusableInTouchMode
5722 */
5723 @ViewDebug.ExportedProperty
5724 public final boolean isFocusableInTouchMode() {
5725 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5726 }
5727
5728 /**
5729 * Find the nearest view in the specified direction that can take focus.
5730 * This does not actually give focus to that view.
5731 *
5732 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5733 *
5734 * @return The nearest focusable in the specified direction, or null if none
5735 * can be found.
5736 */
5737 public View focusSearch(int direction) {
5738 if (mParent != null) {
5739 return mParent.focusSearch(this, direction);
5740 } else {
5741 return null;
5742 }
5743 }
5744
5745 /**
5746 * This method is the last chance for the focused view and its ancestors to
5747 * respond to an arrow key. This is called when the focused view did not
5748 * consume the key internally, nor could the view system find a new view in
5749 * the requested direction to give focus to.
5750 *
5751 * @param focused The currently focused view.
5752 * @param direction The direction focus wants to move. One of FOCUS_UP,
5753 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5754 * @return True if the this view consumed this unhandled move.
5755 */
5756 public boolean dispatchUnhandledMove(View focused, int direction) {
5757 return false;
5758 }
5759
5760 /**
5761 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005762 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005764 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5765 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 * @return The user specified next view, or null if there is none.
5767 */
5768 View findUserSetNextFocus(View root, int direction) {
5769 switch (direction) {
5770 case FOCUS_LEFT:
5771 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005772 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 case FOCUS_RIGHT:
5774 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005775 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 case FOCUS_UP:
5777 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005778 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 case FOCUS_DOWN:
5780 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005781 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005782 case FOCUS_FORWARD:
5783 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005784 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005785 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005786 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005787 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005788 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005789 @Override
5790 public boolean apply(View t) {
5791 return t.mNextFocusForwardId == id;
5792 }
5793 });
5794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 }
5796 return null;
5797 }
5798
Jeff Brown4dfbec22011-08-15 14:55:37 -07005799 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5800 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5801 @Override
5802 public boolean apply(View t) {
5803 return t.mID == childViewId;
5804 }
5805 });
5806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 if (result == null) {
5808 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5809 + "by user for id " + childViewId);
5810 }
5811 return result;
5812 }
5813
5814 /**
5815 * Find and return all focusable views that are descendants of this view,
5816 * possibly including this view if it is focusable itself.
5817 *
5818 * @param direction The direction of the focus
5819 * @return A list of focusable views
5820 */
5821 public ArrayList<View> getFocusables(int direction) {
5822 ArrayList<View> result = new ArrayList<View>(24);
5823 addFocusables(result, direction);
5824 return result;
5825 }
5826
5827 /**
5828 * Add any focusable views that are descendants of this view (possibly
5829 * including this view if it is focusable itself) to views. If we are in touch mode,
5830 * only add views that are also focusable in touch mode.
5831 *
5832 * @param views Focusable views found so far
5833 * @param direction The direction of the focus
5834 */
5835 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005836 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838
svetoslavganov75986cf2009-05-14 22:28:01 -07005839 /**
5840 * Adds any focusable views that are descendants of this view (possibly
5841 * including this view if it is focusable itself) to views. This method
5842 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005843 * only views focusable in touch mode if we are in touch mode or
5844 * only views that can take accessibility focus if accessibility is enabeld
5845 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005846 *
5847 * @param views Focusable views found so far or null if all we are interested is
5848 * the number of focusables.
5849 * @param direction The direction of the focus.
5850 * @param focusableMode The type of focusables to be added.
5851 *
5852 * @see #FOCUSABLES_ALL
5853 * @see #FOCUSABLES_TOUCH_MODE
5854 */
5855 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005856 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005857 return;
5858 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005859 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5860 if (AccessibilityManager.getInstance(mContext).isEnabled()
5861 && includeForAccessibility()) {
5862 views.add(this);
5863 return;
5864 }
5865 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005866 if (!isFocusable()) {
5867 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005868 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005869 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5870 && isInTouchMode() && !isFocusableInTouchMode()) {
5871 return;
5872 }
5873 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 }
5875
5876 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005877 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005878 * The search is performed by either the text that the View renders or the content
5879 * description that describes the view for accessibility purposes and the view does
5880 * not render or both. Clients can specify how the search is to be performed via
5881 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5882 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005883 *
5884 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005885 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005886 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005887 * @see #FIND_VIEWS_WITH_TEXT
5888 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5889 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005890 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005891 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005892 if (getAccessibilityNodeProvider() != null) {
5893 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5894 outViews.add(this);
5895 }
5896 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5897 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005898 String searchedLowerCase = searched.toString().toLowerCase();
5899 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5900 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5901 outViews.add(this);
5902 }
5903 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005904 }
5905
5906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 * Find and return all touchable views that are descendants of this view,
5908 * possibly including this view if it is touchable itself.
5909 *
5910 * @return A list of touchable views
5911 */
5912 public ArrayList<View> getTouchables() {
5913 ArrayList<View> result = new ArrayList<View>();
5914 addTouchables(result);
5915 return result;
5916 }
5917
5918 /**
5919 * Add any touchable views that are descendants of this view (possibly
5920 * including this view if it is touchable itself) to views.
5921 *
5922 * @param views Touchable views found so far
5923 */
5924 public void addTouchables(ArrayList<View> views) {
5925 final int viewFlags = mViewFlags;
5926
5927 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5928 && (viewFlags & ENABLED_MASK) == ENABLED) {
5929 views.add(this);
5930 }
5931 }
5932
5933 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005934 * Returns whether this View is accessibility focused.
5935 *
5936 * @return True if this View is accessibility focused.
5937 */
5938 boolean isAccessibilityFocused() {
5939 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5940 }
5941
5942 /**
5943 * Call this to try to give accessibility focus to this view.
5944 *
5945 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5946 * returns false or the view is no visible or the view already has accessibility
5947 * focus.
5948 *
5949 * See also {@link #focusSearch(int)}, which is what you call to say that you
5950 * have focus, and you want your parent to look for the next one.
5951 *
5952 * @return Whether this view actually took accessibility focus.
5953 *
5954 * @hide
5955 */
5956 public boolean requestAccessibilityFocus() {
5957 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5958 return false;
5959 }
5960 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5961 return false;
5962 }
5963 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5964 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5965 ViewRootImpl viewRootImpl = getViewRootImpl();
5966 if (viewRootImpl != null) {
5967 viewRootImpl.setAccessibilityFocusedHost(this);
5968 }
5969 invalidate();
5970 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5971 notifyAccessibilityStateChanged();
5972 // Try to give input focus to this view - not a descendant.
5973 requestFocusNoSearch(View.FOCUS_DOWN, null);
5974 return true;
5975 }
5976 return false;
5977 }
5978
5979 /**
5980 * Call this to try to clear accessibility focus of this view.
5981 *
5982 * See also {@link #focusSearch(int)}, which is what you call to say that you
5983 * have focus, and you want your parent to look for the next one.
5984 *
5985 * @hide
5986 */
5987 public void clearAccessibilityFocus() {
5988 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5989 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5990 ViewRootImpl viewRootImpl = getViewRootImpl();
5991 if (viewRootImpl != null) {
5992 viewRootImpl.setAccessibilityFocusedHost(null);
5993 }
5994 invalidate();
5995 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5996 notifyAccessibilityStateChanged();
5997 // Try to move accessibility focus to the input focus.
5998 View rootView = getRootView();
5999 if (rootView != null) {
6000 View inputFocus = rootView.findFocus();
6001 if (inputFocus != null) {
6002 inputFocus.requestAccessibilityFocus();
6003 }
6004 }
6005 }
6006 }
6007
6008 /**
6009 * Find the best view to take accessibility focus from a hover.
6010 * This function finds the deepest actionable view and if that
6011 * fails ask the parent to take accessibility focus from hover.
6012 *
6013 * @param x The X hovered location in this view coorditantes.
6014 * @param y The Y hovered location in this view coorditantes.
6015 * @return Whether the request was handled.
6016 *
6017 * @hide
6018 */
6019 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6020 if (onRequestAccessibilityFocusFromHover(x, y)) {
6021 return true;
6022 }
6023 ViewParent parent = mParent;
6024 if (parent instanceof View) {
6025 View parentView = (View) parent;
6026
6027 float[] position = mAttachInfo.mTmpTransformLocation;
6028 position[0] = x;
6029 position[1] = y;
6030
6031 // Compensate for the transformation of the current matrix.
6032 if (!hasIdentityMatrix()) {
6033 getMatrix().mapPoints(position);
6034 }
6035
6036 // Compensate for the parent scroll and the offset
6037 // of this view stop from the parent top.
6038 position[0] += mLeft - parentView.mScrollX;
6039 position[1] += mTop - parentView.mScrollY;
6040
6041 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6042 }
6043 return false;
6044 }
6045
6046 /**
6047 * Requests to give this View focus from hover.
6048 *
6049 * @param x The X hovered location in this view coorditantes.
6050 * @param y The Y hovered location in this view coorditantes.
6051 * @return Whether the request was handled.
6052 *
6053 * @hide
6054 */
6055 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6056 if (includeForAccessibility()
6057 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6058 return requestAccessibilityFocus();
6059 }
6060 return false;
6061 }
6062
6063 /**
6064 * Clears accessibility focus without calling any callback methods
6065 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6066 * is used for clearing accessibility focus when giving this focus to
6067 * another view.
6068 */
6069 void clearAccessibilityFocusNoCallbacks() {
6070 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6071 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6072 invalidate();
6073 }
6074 }
6075
6076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 * Call this to try to give focus to a specific view or to one of its
6078 * descendants.
6079 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006080 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6081 * false), or if it is focusable and it is not focusable in touch mode
6082 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006084 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 * have focus, and you want your parent to look for the next one.
6086 *
6087 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6088 * {@link #FOCUS_DOWN} and <code>null</code>.
6089 *
6090 * @return Whether this view or one of its descendants actually took focus.
6091 */
6092 public final boolean requestFocus() {
6093 return requestFocus(View.FOCUS_DOWN);
6094 }
6095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 /**
6097 * Call this to try to give focus to a specific view or to one of its
6098 * descendants and give it a hint about what direction focus is heading.
6099 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006100 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6101 * false), or if it is focusable and it is not focusable in touch mode
6102 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006103 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006104 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 * have focus, and you want your parent to look for the next one.
6106 *
6107 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6108 * <code>null</code> set for the previously focused rectangle.
6109 *
6110 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6111 * @return Whether this view or one of its descendants actually took focus.
6112 */
6113 public final boolean requestFocus(int direction) {
6114 return requestFocus(direction, null);
6115 }
6116
6117 /**
6118 * Call this to try to give focus to a specific view or to one of its descendants
6119 * and give it hints about the direction and a specific rectangle that the focus
6120 * is coming from. The rectangle can help give larger views a finer grained hint
6121 * about where focus is coming from, and therefore, where to show selection, or
6122 * forward focus change internally.
6123 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006124 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6125 * false), or if it is focusable and it is not focusable in touch mode
6126 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 *
6128 * A View will not take focus if it is not visible.
6129 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006130 * A View will not take focus if one of its parents has
6131 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6132 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006134 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 * have focus, and you want your parent to look for the next one.
6136 *
6137 * You may wish to override this method if your custom {@link View} has an internal
6138 * {@link View} that it wishes to forward the request to.
6139 *
6140 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6141 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6142 * to give a finer grained hint about where focus is coming from. May be null
6143 * if there is no hint.
6144 * @return Whether this view or one of its descendants actually took focus.
6145 */
6146 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006147 return requestFocusNoSearch(direction, previouslyFocusedRect);
6148 }
6149
6150 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 // need to be focusable
6152 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6153 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6154 return false;
6155 }
6156
6157 // need to be focusable in touch mode if in touch mode
6158 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006159 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6160 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161 }
6162
6163 // need to not have any parents blocking us
6164 if (hasAncestorThatBlocksDescendantFocus()) {
6165 return false;
6166 }
6167
6168 handleFocusGainInternal(direction, previouslyFocusedRect);
6169 return true;
6170 }
6171
6172 /**
6173 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6174 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6175 * touch mode to request focus when they are touched.
6176 *
6177 * @return Whether this view or one of its descendants actually took focus.
6178 *
6179 * @see #isInTouchMode()
6180 *
6181 */
6182 public final boolean requestFocusFromTouch() {
6183 // Leave touch mode if we need to
6184 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006185 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006186 if (viewRoot != null) {
6187 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 }
6189 }
6190 return requestFocus(View.FOCUS_DOWN);
6191 }
6192
6193 /**
6194 * @return Whether any ancestor of this view blocks descendant focus.
6195 */
6196 private boolean hasAncestorThatBlocksDescendantFocus() {
6197 ViewParent ancestor = mParent;
6198 while (ancestor instanceof ViewGroup) {
6199 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6200 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6201 return true;
6202 } else {
6203 ancestor = vgAncestor.getParent();
6204 }
6205 }
6206 return false;
6207 }
6208
6209 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006210 * Gets the mode for determining whether this View is important for accessibility
6211 * which is if it fires accessibility events and if it is reported to
6212 * accessibility services that query the screen.
6213 *
6214 * @return The mode for determining whether a View is important for accessibility.
6215 *
6216 * @attr ref android.R.styleable#View_importantForAccessibility
6217 *
6218 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6219 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6220 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6221 */
6222 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6223 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6224 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6225 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6226 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6227 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6228 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6229 })
6230 public int getImportantForAccessibility() {
6231 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6232 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6233 }
6234
6235 /**
6236 * Sets how to determine whether this view is important for accessibility
6237 * which is if it fires accessibility events and if it is reported to
6238 * accessibility services that query the screen.
6239 *
6240 * @param mode How to determine whether this view is important for accessibility.
6241 *
6242 * @attr ref android.R.styleable#View_importantForAccessibility
6243 *
6244 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6245 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6246 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6247 */
6248 public void setImportantForAccessibility(int mode) {
6249 if (mode != getImportantForAccessibility()) {
6250 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6251 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6252 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6253 notifyAccessibilityStateChanged();
6254 }
6255 }
6256
6257 /**
6258 * Gets whether this view should be exposed for accessibility.
6259 *
6260 * @return Whether the view is exposed for accessibility.
6261 *
6262 * @hide
6263 */
6264 public boolean isImportantForAccessibility() {
6265 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6266 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6267 switch (mode) {
6268 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6269 return true;
6270 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6271 return false;
6272 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6273 return isActionableForAccessibility() || hasListenersForAccessibility();
6274 default:
6275 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6276 + mode);
6277 }
6278 }
6279
6280 /**
6281 * Gets the parent for accessibility purposes. Note that the parent for
6282 * accessibility is not necessary the immediate parent. It is the first
6283 * predecessor that is important for accessibility.
6284 *
6285 * @return The parent for accessibility purposes.
6286 */
6287 public ViewParent getParentForAccessibility() {
6288 if (mParent instanceof View) {
6289 View parentView = (View) mParent;
6290 if (parentView.includeForAccessibility()) {
6291 return mParent;
6292 } else {
6293 return mParent.getParentForAccessibility();
6294 }
6295 }
6296 return null;
6297 }
6298
6299 /**
6300 * Adds the children of a given View for accessibility. Since some Views are
6301 * not important for accessibility the children for accessibility are not
6302 * necessarily direct children of the riew, rather they are the first level of
6303 * descendants important for accessibility.
6304 *
6305 * @param children The list of children for accessibility.
6306 */
6307 public void addChildrenForAccessibility(ArrayList<View> children) {
6308 if (includeForAccessibility()) {
6309 children.add(this);
6310 }
6311 }
6312
6313 /**
6314 * Whether to regard this view for accessibility. A view is regarded for
6315 * accessibility if it is important for accessibility or the querying
6316 * accessibility service has explicitly requested that view not
6317 * important for accessibility are regarded.
6318 *
6319 * @return Whether to regard the view for accessibility.
6320 */
6321 boolean includeForAccessibility() {
6322 if (mAttachInfo != null) {
6323 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006324 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006325 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006326 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006327 }
6328 }
6329 return false;
6330 }
6331
6332 /**
6333 * Returns whether the View is considered actionable from
6334 * accessibility perspective. Such view are important for
6335 * accessiiblity.
6336 *
6337 * @return True if the view is actionable for accessibility.
6338 */
6339 private boolean isActionableForAccessibility() {
6340 return (isClickable() || isLongClickable() || isFocusable());
6341 }
6342
6343 /**
6344 * Returns whether the View has registered callbacks wich makes it
6345 * important for accessiiblity.
6346 *
6347 * @return True if the view is actionable for accessibility.
6348 */
6349 private boolean hasListenersForAccessibility() {
6350 ListenerInfo info = getListenerInfo();
6351 return mTouchDelegate != null || info.mOnKeyListener != null
6352 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6353 || info.mOnHoverListener != null || info.mOnDragListener != null;
6354 }
6355
6356 /**
6357 * Notifies accessibility services that some view's important for
6358 * accessibility state has changed. Note that such notifications
6359 * are made at most once every
6360 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6361 * to avoid unnecessary load to the system. Also once a view has
6362 * made a notifucation this method is a NOP until the notification has
6363 * been sent to clients.
6364 *
6365 * @hide
6366 *
6367 * TODO: Makse sure this method is called for any view state change
6368 * that is interesting for accessilility purposes.
6369 */
6370 public void notifyAccessibilityStateChanged() {
6371 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6372 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6373 if (mParent != null) {
6374 mParent.childAccessibilityStateChanged(this);
6375 }
6376 }
6377 }
6378
6379 /**
6380 * Reset the state indicating the this view has requested clients
6381 * interested in its accessiblity state to be notified.
6382 *
6383 * @hide
6384 */
6385 public void resetAccessibilityStateChanged() {
6386 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6387 }
6388
6389 /**
6390 * Performs the specified accessibility action on the view. For
6391 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6392 *
6393 * @param action The action to perform.
6394 * @return Whether the action was performed.
6395 */
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07006396 public boolean performAccessibilityAction(int action, Bundle args) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006397 switch (action) {
6398 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006399 if (isClickable()) {
6400 performClick();
6401 }
6402 } break;
6403 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6404 if (isLongClickable()) {
6405 performLongClick();
6406 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006407 } break;
6408 case AccessibilityNodeInfo.ACTION_FOCUS: {
6409 if (!hasFocus()) {
6410 // Get out of touch mode since accessibility
6411 // wants to move focus around.
6412 getViewRootImpl().ensureTouchMode(false);
6413 return requestFocus();
6414 }
6415 } break;
6416 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6417 if (hasFocus()) {
6418 clearFocus();
6419 return !isFocused();
6420 }
6421 } break;
6422 case AccessibilityNodeInfo.ACTION_SELECT: {
6423 if (!isSelected()) {
6424 setSelected(true);
6425 return isSelected();
6426 }
6427 } break;
6428 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6429 if (isSelected()) {
6430 setSelected(false);
6431 return !isSelected();
6432 }
6433 } break;
6434 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6435 if (!isAccessibilityFocused()) {
6436 return requestAccessibilityFocus();
6437 }
6438 } break;
6439 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6440 if (isAccessibilityFocused()) {
6441 clearAccessibilityFocus();
6442 return true;
6443 }
6444 } break;
6445 }
6446 return false;
6447 }
6448
6449 /**
Romain Guya440b002010-02-24 15:57:54 -08006450 * @hide
6451 */
6452 public void dispatchStartTemporaryDetach() {
6453 onStartTemporaryDetach();
6454 }
6455
6456 /**
6457 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6459 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006460 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 */
6462 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006463 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006464 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006465 }
6466
6467 /**
6468 * @hide
6469 */
6470 public void dispatchFinishTemporaryDetach() {
6471 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 }
Romain Guy8506ab42009-06-11 17:35:47 -07006473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 /**
6475 * Called after {@link #onStartTemporaryDetach} when the container is done
6476 * changing the view.
6477 */
6478 public void onFinishTemporaryDetach() {
6479 }
Romain Guy8506ab42009-06-11 17:35:47 -07006480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006482 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6483 * for this view's window. Returns null if the view is not currently attached
6484 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006485 * just use the standard high-level event callbacks like
6486 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006487 */
6488 public KeyEvent.DispatcherState getKeyDispatcherState() {
6489 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6490 }
Joe Malin32736f02011-01-19 16:14:20 -08006491
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 * Dispatch a key event before it is processed by any input method
6494 * associated with the view hierarchy. This can be used to intercept
6495 * key events in special situations before the IME consumes them; a
6496 * typical example would be handling the BACK key to update the application's
6497 * UI instead of allowing the IME to see it and close itself.
6498 *
6499 * @param event The key event to be dispatched.
6500 * @return True if the event was handled, false otherwise.
6501 */
6502 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6503 return onKeyPreIme(event.getKeyCode(), event);
6504 }
6505
6506 /**
6507 * Dispatch a key event to the next view on the focus path. This path runs
6508 * from the top of the view tree down to the currently focused view. If this
6509 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6510 * the next node down the focus path. This method also fires any key
6511 * listeners.
6512 *
6513 * @param event The key event to be dispatched.
6514 * @return True if the event was handled, false otherwise.
6515 */
6516 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006517 if (mInputEventConsistencyVerifier != null) {
6518 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520
Jeff Brown21bc5c92011-02-28 18:27:14 -08006521 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006522 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006523 ListenerInfo li = mListenerInfo;
6524 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6525 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 return true;
6527 }
6528
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006529 if (event.dispatch(this, mAttachInfo != null
6530 ? mAttachInfo.mKeyDispatchState : null, this)) {
6531 return true;
6532 }
6533
6534 if (mInputEventConsistencyVerifier != null) {
6535 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6536 }
6537 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 }
6539
6540 /**
6541 * Dispatches a key shortcut event.
6542 *
6543 * @param event The key event to be dispatched.
6544 * @return True if the event was handled by the view, false otherwise.
6545 */
6546 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6547 return onKeyShortcut(event.getKeyCode(), event);
6548 }
6549
6550 /**
6551 * Pass the touch screen motion event down to the target view, or this
6552 * view if it is the target.
6553 *
6554 * @param event The motion event to be dispatched.
6555 * @return True if the event was handled by the view, false otherwise.
6556 */
6557 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006558 if (mInputEventConsistencyVerifier != null) {
6559 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6560 }
6561
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006562 if (onFilterTouchEventForSecurity(event)) {
6563 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006564 ListenerInfo li = mListenerInfo;
6565 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6566 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006567 return true;
6568 }
6569
6570 if (onTouchEvent(event)) {
6571 return true;
6572 }
Jeff Brown85a31762010-09-01 17:01:00 -07006573 }
6574
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006575 if (mInputEventConsistencyVerifier != null) {
6576 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006578 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 }
6580
6581 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006582 * Filter the touch event to apply security policies.
6583 *
6584 * @param event The motion event to be filtered.
6585 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006586 *
Jeff Brown85a31762010-09-01 17:01:00 -07006587 * @see #getFilterTouchesWhenObscured
6588 */
6589 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006590 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006591 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6592 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6593 // Window is obscured, drop this touch.
6594 return false;
6595 }
6596 return true;
6597 }
6598
6599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006600 * Pass a trackball motion event down to the focused view.
6601 *
6602 * @param event The motion event to be dispatched.
6603 * @return True if the event was handled by the view, false otherwise.
6604 */
6605 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006606 if (mInputEventConsistencyVerifier != null) {
6607 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6608 }
6609
Romain Guy02ccac62011-06-24 13:20:23 -07006610 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 }
6612
6613 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006614 * Dispatch a generic motion event.
6615 * <p>
6616 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6617 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006618 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006619 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006620 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006621 *
6622 * @param event The motion event to be dispatched.
6623 * @return True if the event was handled by the view, false otherwise.
6624 */
6625 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006626 if (mInputEventConsistencyVerifier != null) {
6627 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6628 }
6629
Jeff Browna032cc02011-03-07 16:56:21 -08006630 final int source = event.getSource();
6631 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6632 final int action = event.getAction();
6633 if (action == MotionEvent.ACTION_HOVER_ENTER
6634 || action == MotionEvent.ACTION_HOVER_MOVE
6635 || action == MotionEvent.ACTION_HOVER_EXIT) {
6636 if (dispatchHoverEvent(event)) {
6637 return true;
6638 }
6639 } else if (dispatchGenericPointerEvent(event)) {
6640 return true;
6641 }
6642 } else if (dispatchGenericFocusedEvent(event)) {
6643 return true;
6644 }
6645
Jeff Brown10b62902011-06-20 16:40:37 -07006646 if (dispatchGenericMotionEventInternal(event)) {
6647 return true;
6648 }
6649
6650 if (mInputEventConsistencyVerifier != null) {
6651 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6652 }
6653 return false;
6654 }
6655
6656 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006657 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006658 ListenerInfo li = mListenerInfo;
6659 if (li != null && li.mOnGenericMotionListener != null
6660 && (mViewFlags & ENABLED_MASK) == ENABLED
6661 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006662 return true;
6663 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006664
6665 if (onGenericMotionEvent(event)) {
6666 return true;
6667 }
6668
6669 if (mInputEventConsistencyVerifier != null) {
6670 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6671 }
6672 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006673 }
6674
6675 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006676 * Dispatch a hover event.
6677 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006678 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006679 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006680 * </p>
6681 *
6682 * @param event The motion event to be dispatched.
6683 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006684 */
6685 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006686 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006687 ListenerInfo li = mListenerInfo;
6688 if (li != null && li.mOnHoverListener != null
6689 && (mViewFlags & ENABLED_MASK) == ENABLED
6690 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006691 return true;
6692 }
6693
Jeff Browna032cc02011-03-07 16:56:21 -08006694 return onHoverEvent(event);
6695 }
6696
6697 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006698 * Returns true if the view has a child to which it has recently sent
6699 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6700 * it does not have a hovered child, then it must be the innermost hovered view.
6701 * @hide
6702 */
6703 protected boolean hasHoveredChild() {
6704 return false;
6705 }
6706
6707 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006708 * Dispatch a generic motion event to the view under the first pointer.
6709 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006710 * Do not call this method directly.
6711 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006712 * </p>
6713 *
6714 * @param event The motion event to be dispatched.
6715 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006716 */
6717 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6718 return false;
6719 }
6720
6721 /**
6722 * Dispatch a generic motion event to the currently focused view.
6723 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006724 * Do not call this method directly.
6725 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006726 * </p>
6727 *
6728 * @param event The motion event to be dispatched.
6729 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006730 */
6731 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6732 return false;
6733 }
6734
6735 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006736 * Dispatch a pointer event.
6737 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006738 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6739 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6740 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006741 * and should not be expected to handle other pointing device features.
6742 * </p>
6743 *
6744 * @param event The motion event to be dispatched.
6745 * @return True if the event was handled by the view, false otherwise.
6746 * @hide
6747 */
6748 public final boolean dispatchPointerEvent(MotionEvent event) {
6749 if (event.isTouchEvent()) {
6750 return dispatchTouchEvent(event);
6751 } else {
6752 return dispatchGenericMotionEvent(event);
6753 }
6754 }
6755
6756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 * Called when the window containing this view gains or loses window focus.
6758 * ViewGroups should override to route to their children.
6759 *
6760 * @param hasFocus True if the window containing this view now has focus,
6761 * false otherwise.
6762 */
6763 public void dispatchWindowFocusChanged(boolean hasFocus) {
6764 onWindowFocusChanged(hasFocus);
6765 }
6766
6767 /**
6768 * Called when the window containing this view gains or loses focus. Note
6769 * that this is separate from view focus: to receive key events, both
6770 * your view and its window must have focus. If a window is displayed
6771 * on top of yours that takes input focus, then your own window will lose
6772 * focus but the view focus will remain unchanged.
6773 *
6774 * @param hasWindowFocus True if the window containing this view now has
6775 * focus, false otherwise.
6776 */
6777 public void onWindowFocusChanged(boolean hasWindowFocus) {
6778 InputMethodManager imm = InputMethodManager.peekInstance();
6779 if (!hasWindowFocus) {
6780 if (isPressed()) {
6781 setPressed(false);
6782 }
6783 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6784 imm.focusOut(this);
6785 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006786 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006787 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006788 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6790 imm.focusIn(this);
6791 }
6792 refreshDrawableState();
6793 }
6794
6795 /**
6796 * Returns true if this view is in a window that currently has window focus.
6797 * Note that this is not the same as the view itself having focus.
6798 *
6799 * @return True if this view is in a window that currently has window focus.
6800 */
6801 public boolean hasWindowFocus() {
6802 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6803 }
6804
6805 /**
Adam Powell326d8082009-12-09 15:10:07 -08006806 * Dispatch a view visibility change down the view hierarchy.
6807 * ViewGroups should override to route to their children.
6808 * @param changedView The view whose visibility changed. Could be 'this' or
6809 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006810 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6811 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006812 */
6813 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6814 onVisibilityChanged(changedView, visibility);
6815 }
6816
6817 /**
6818 * Called when the visibility of the view or an ancestor of the view is changed.
6819 * @param changedView The view whose visibility changed. Could be 'this' or
6820 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006821 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6822 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006823 */
6824 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006825 if (visibility == VISIBLE) {
6826 if (mAttachInfo != null) {
6827 initialAwakenScrollBars();
6828 } else {
6829 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6830 }
6831 }
Adam Powell326d8082009-12-09 15:10:07 -08006832 }
6833
6834 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006835 * Dispatch a hint about whether this view is displayed. For instance, when
6836 * a View moves out of the screen, it might receives a display hint indicating
6837 * the view is not displayed. Applications should not <em>rely</em> on this hint
6838 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006839 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006840 * @param hint A hint about whether or not this view is displayed:
6841 * {@link #VISIBLE} or {@link #INVISIBLE}.
6842 */
6843 public void dispatchDisplayHint(int hint) {
6844 onDisplayHint(hint);
6845 }
6846
6847 /**
6848 * Gives this view a hint about whether is displayed or not. For instance, when
6849 * a View moves out of the screen, it might receives a display hint indicating
6850 * the view is not displayed. Applications should not <em>rely</em> on this hint
6851 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006852 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006853 * @param hint A hint about whether or not this view is displayed:
6854 * {@link #VISIBLE} or {@link #INVISIBLE}.
6855 */
6856 protected void onDisplayHint(int hint) {
6857 }
6858
6859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 * Dispatch a window visibility change down the view hierarchy.
6861 * ViewGroups should override to route to their children.
6862 *
6863 * @param visibility The new visibility of the window.
6864 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006865 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 */
6867 public void dispatchWindowVisibilityChanged(int visibility) {
6868 onWindowVisibilityChanged(visibility);
6869 }
6870
6871 /**
6872 * Called when the window containing has change its visibility
6873 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6874 * that this tells you whether or not your window is being made visible
6875 * to the window manager; this does <em>not</em> tell you whether or not
6876 * your window is obscured by other windows on the screen, even if it
6877 * is itself visible.
6878 *
6879 * @param visibility The new visibility of the window.
6880 */
6881 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006882 if (visibility == VISIBLE) {
6883 initialAwakenScrollBars();
6884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885 }
6886
6887 /**
6888 * Returns the current visibility of the window this view is attached to
6889 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6890 *
6891 * @return Returns the current visibility of the view's window.
6892 */
6893 public int getWindowVisibility() {
6894 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6895 }
6896
6897 /**
6898 * Retrieve the overall visible display size in which the window this view is
6899 * attached to has been positioned in. This takes into account screen
6900 * decorations above the window, for both cases where the window itself
6901 * is being position inside of them or the window is being placed under
6902 * then and covered insets are used for the window to position its content
6903 * inside. In effect, this tells you the available area where content can
6904 * be placed and remain visible to users.
6905 *
6906 * <p>This function requires an IPC back to the window manager to retrieve
6907 * the requested information, so should not be used in performance critical
6908 * code like drawing.
6909 *
6910 * @param outRect Filled in with the visible display frame. If the view
6911 * is not attached to a window, this is simply the raw display size.
6912 */
6913 public void getWindowVisibleDisplayFrame(Rect outRect) {
6914 if (mAttachInfo != null) {
6915 try {
6916 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6917 } catch (RemoteException e) {
6918 return;
6919 }
6920 // XXX This is really broken, and probably all needs to be done
6921 // in the window manager, and we need to know more about whether
6922 // we want the area behind or in front of the IME.
6923 final Rect insets = mAttachInfo.mVisibleInsets;
6924 outRect.left += insets.left;
6925 outRect.top += insets.top;
6926 outRect.right -= insets.right;
6927 outRect.bottom -= insets.bottom;
6928 return;
6929 }
6930 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006931 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 }
6933
6934 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006935 * Dispatch a notification about a resource configuration change down
6936 * the view hierarchy.
6937 * ViewGroups should override to route to their children.
6938 *
6939 * @param newConfig The new resource configuration.
6940 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006941 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006942 */
6943 public void dispatchConfigurationChanged(Configuration newConfig) {
6944 onConfigurationChanged(newConfig);
6945 }
6946
6947 /**
6948 * Called when the current configuration of the resources being used
6949 * by the application have changed. You can use this to decide when
6950 * to reload resources that can changed based on orientation and other
6951 * configuration characterstics. You only need to use this if you are
6952 * not relying on the normal {@link android.app.Activity} mechanism of
6953 * recreating the activity instance upon a configuration change.
6954 *
6955 * @param newConfig The new resource configuration.
6956 */
6957 protected void onConfigurationChanged(Configuration newConfig) {
6958 }
6959
6960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 * Private function to aggregate all per-view attributes in to the view
6962 * root.
6963 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006964 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6965 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 }
6967
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006968 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6969 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006970 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006971 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006972 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006973 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006974 ListenerInfo li = mListenerInfo;
6975 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006976 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 }
6979 }
6980
6981 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006982 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006984 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6985 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 ai.mRecomputeGlobalAttributes = true;
6987 }
6988 }
6989 }
6990
6991 /**
6992 * Returns whether the device is currently in touch mode. Touch mode is entered
6993 * once the user begins interacting with the device by touch, and affects various
6994 * things like whether focus is always visible to the user.
6995 *
6996 * @return Whether the device is in touch mode.
6997 */
6998 @ViewDebug.ExportedProperty
6999 public boolean isInTouchMode() {
7000 if (mAttachInfo != null) {
7001 return mAttachInfo.mInTouchMode;
7002 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007003 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 }
7005 }
7006
7007 /**
7008 * Returns the context the view is running in, through which it can
7009 * access the current theme, resources, etc.
7010 *
7011 * @return The view's Context.
7012 */
7013 @ViewDebug.CapturedViewProperty
7014 public final Context getContext() {
7015 return mContext;
7016 }
7017
7018 /**
7019 * Handle a key event before it is processed by any input method
7020 * associated with the view hierarchy. This can be used to intercept
7021 * key events in special situations before the IME consumes them; a
7022 * typical example would be handling the BACK key to update the application's
7023 * UI instead of allowing the IME to see it and close itself.
7024 *
7025 * @param keyCode The value in event.getKeyCode().
7026 * @param event Description of the key event.
7027 * @return If you handled the event, return true. If you want to allow the
7028 * event to be handled by the next receiver, return false.
7029 */
7030 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7031 return false;
7032 }
7033
7034 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007035 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7036 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7038 * is released, if the view is enabled and clickable.
7039 *
7040 * @param keyCode A key code that represents the button pressed, from
7041 * {@link android.view.KeyEvent}.
7042 * @param event The KeyEvent object that defines the button action.
7043 */
7044 public boolean onKeyDown(int keyCode, KeyEvent event) {
7045 boolean result = false;
7046
7047 switch (keyCode) {
7048 case KeyEvent.KEYCODE_DPAD_CENTER:
7049 case KeyEvent.KEYCODE_ENTER: {
7050 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7051 return true;
7052 }
7053 // Long clickable items don't necessarily have to be clickable
7054 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7055 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7056 (event.getRepeatCount() == 0)) {
7057 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007058 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 return true;
7060 }
7061 break;
7062 }
7063 }
7064 return result;
7065 }
7066
7067 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007068 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7069 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7070 * the event).
7071 */
7072 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7073 return false;
7074 }
7075
7076 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007077 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7078 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7080 * {@link KeyEvent#KEYCODE_ENTER} is released.
7081 *
7082 * @param keyCode A key code that represents the button pressed, from
7083 * {@link android.view.KeyEvent}.
7084 * @param event The KeyEvent object that defines the button action.
7085 */
7086 public boolean onKeyUp(int keyCode, KeyEvent event) {
7087 boolean result = false;
7088
7089 switch (keyCode) {
7090 case KeyEvent.KEYCODE_DPAD_CENTER:
7091 case KeyEvent.KEYCODE_ENTER: {
7092 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7093 return true;
7094 }
7095 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7096 setPressed(false);
7097
7098 if (!mHasPerformedLongPress) {
7099 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007100 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101
7102 result = performClick();
7103 }
7104 }
7105 break;
7106 }
7107 }
7108 return result;
7109 }
7110
7111 /**
7112 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7113 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7114 * the event).
7115 *
7116 * @param keyCode A key code that represents the button pressed, from
7117 * {@link android.view.KeyEvent}.
7118 * @param repeatCount The number of times the action was made.
7119 * @param event The KeyEvent object that defines the button action.
7120 */
7121 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7122 return false;
7123 }
7124
7125 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007126 * Called on the focused view when a key shortcut event is not handled.
7127 * Override this method to implement local key shortcuts for the View.
7128 * Key shortcuts can also be implemented by setting the
7129 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 *
7131 * @param keyCode The value in event.getKeyCode().
7132 * @param event Description of the key event.
7133 * @return If you handled the event, return true. If you want to allow the
7134 * event to be handled by the next receiver, return false.
7135 */
7136 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7137 return false;
7138 }
7139
7140 /**
7141 * Check whether the called view is a text editor, in which case it
7142 * would make sense to automatically display a soft input window for
7143 * it. Subclasses should override this if they implement
7144 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007145 * a call on that method would return a non-null InputConnection, and
7146 * they are really a first-class editor that the user would normally
7147 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007148 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007149 * <p>The default implementation always returns false. This does
7150 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7151 * will not be called or the user can not otherwise perform edits on your
7152 * view; it is just a hint to the system that this is not the primary
7153 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 * @return Returns true if this view is a text editor, else false.
7156 */
7157 public boolean onCheckIsTextEditor() {
7158 return false;
7159 }
Romain Guy8506ab42009-06-11 17:35:47 -07007160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 /**
7162 * Create a new InputConnection for an InputMethod to interact
7163 * with the view. The default implementation returns null, since it doesn't
7164 * support input methods. You can override this to implement such support.
7165 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 * <p>When implementing this, you probably also want to implement
7168 * {@link #onCheckIsTextEditor()} to indicate you will return a
7169 * non-null InputConnection.
7170 *
7171 * @param outAttrs Fill in with attribute information about the connection.
7172 */
7173 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7174 return null;
7175 }
7176
7177 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007178 * Called by the {@link android.view.inputmethod.InputMethodManager}
7179 * when a view who is not the current
7180 * input connection target is trying to make a call on the manager. The
7181 * default implementation returns false; you can override this to return
7182 * true for certain views if you are performing InputConnection proxying
7183 * to them.
7184 * @param view The View that is making the InputMethodManager call.
7185 * @return Return true to allow the call, false to reject.
7186 */
7187 public boolean checkInputConnectionProxy(View view) {
7188 return false;
7189 }
Romain Guy8506ab42009-06-11 17:35:47 -07007190
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 * Show the context menu for this view. It is not safe to hold on to the
7193 * menu after returning from this method.
7194 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007195 * You should normally not overload this method. Overload
7196 * {@link #onCreateContextMenu(ContextMenu)} or define an
7197 * {@link OnCreateContextMenuListener} to add items to the context menu.
7198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 * @param menu The context menu to populate
7200 */
7201 public void createContextMenu(ContextMenu menu) {
7202 ContextMenuInfo menuInfo = getContextMenuInfo();
7203
7204 // Sets the current menu info so all items added to menu will have
7205 // my extra info set.
7206 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7207
7208 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007209 ListenerInfo li = mListenerInfo;
7210 if (li != null && li.mOnCreateContextMenuListener != null) {
7211 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 }
7213
7214 // Clear the extra information so subsequent items that aren't mine don't
7215 // have my extra info.
7216 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7217
7218 if (mParent != null) {
7219 mParent.createContextMenu(menu);
7220 }
7221 }
7222
7223 /**
7224 * Views should implement this if they have extra information to associate
7225 * with the context menu. The return result is supplied as a parameter to
7226 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7227 * callback.
7228 *
7229 * @return Extra information about the item for which the context menu
7230 * should be shown. This information will vary across different
7231 * subclasses of View.
7232 */
7233 protected ContextMenuInfo getContextMenuInfo() {
7234 return null;
7235 }
7236
7237 /**
7238 * Views should implement this if the view itself is going to add items to
7239 * the context menu.
7240 *
7241 * @param menu the context menu to populate
7242 */
7243 protected void onCreateContextMenu(ContextMenu menu) {
7244 }
7245
7246 /**
7247 * Implement this method to handle trackball motion events. The
7248 * <em>relative</em> movement of the trackball since the last event
7249 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7250 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7251 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7252 * they will often be fractional values, representing the more fine-grained
7253 * movement information available from a trackball).
7254 *
7255 * @param event The motion event.
7256 * @return True if the event was handled, false otherwise.
7257 */
7258 public boolean onTrackballEvent(MotionEvent event) {
7259 return false;
7260 }
7261
7262 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007263 * Implement this method to handle generic motion events.
7264 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007265 * Generic motion events describe joystick movements, mouse hovers, track pad
7266 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007267 * {@link MotionEvent#getSource() source} of the motion event specifies
7268 * the class of input that was received. Implementations of this method
7269 * must examine the bits in the source before processing the event.
7270 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007271 * </p><p>
7272 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7273 * are delivered to the view under the pointer. All other generic motion events are
7274 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007275 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007276 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007277 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007278 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7279 * // process the joystick movement...
7280 * return true;
7281 * }
7282 * }
7283 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7284 * switch (event.getAction()) {
7285 * case MotionEvent.ACTION_HOVER_MOVE:
7286 * // process the mouse hover movement...
7287 * return true;
7288 * case MotionEvent.ACTION_SCROLL:
7289 * // process the scroll wheel movement...
7290 * return true;
7291 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007292 * }
7293 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007294 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007295 *
7296 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007297 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007298 */
7299 public boolean onGenericMotionEvent(MotionEvent event) {
7300 return false;
7301 }
7302
7303 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007304 * Implement this method to handle hover events.
7305 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007306 * This method is called whenever a pointer is hovering into, over, or out of the
7307 * bounds of a view and the view is not currently being touched.
7308 * Hover events are represented as pointer events with action
7309 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7310 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7311 * </p>
7312 * <ul>
7313 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7314 * when the pointer enters the bounds of the view.</li>
7315 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7316 * when the pointer has already entered the bounds of the view and has moved.</li>
7317 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7318 * when the pointer has exited the bounds of the view or when the pointer is
7319 * about to go down due to a button click, tap, or similar user action that
7320 * causes the view to be touched.</li>
7321 * </ul>
7322 * <p>
7323 * The view should implement this method to return true to indicate that it is
7324 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007325 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007326 * The default implementation calls {@link #setHovered} to update the hovered state
7327 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007328 * is enabled and is clickable. The default implementation also sends hover
7329 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007330 * </p>
7331 *
7332 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007333 * @return True if the view handled the hover event.
7334 *
7335 * @see #isHovered
7336 * @see #setHovered
7337 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007338 */
7339 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007340 // The root view may receive hover (or touch) events that are outside the bounds of
7341 // the window. This code ensures that we only send accessibility events for
7342 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007343 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007344 if (!mSendingHoverAccessibilityEvents) {
7345 if ((action == MotionEvent.ACTION_HOVER_ENTER
7346 || action == MotionEvent.ACTION_HOVER_MOVE)
7347 && !hasHoveredChild()
7348 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007349 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007350 mSendingHoverAccessibilityEvents = true;
7351 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007352 }
7353 } else {
7354 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007355 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007356 && !pointInView(event.getX(), event.getY()))) {
7357 mSendingHoverAccessibilityEvents = false;
7358 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007359 // If the window does not have input focus we take away accessibility
7360 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007361 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007362 getViewRootImpl().setAccessibilityFocusedHost(null);
7363 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007364 }
Jeff Browna1b24182011-07-28 13:38:24 -07007365 }
7366
Jeff Brown87b7f802011-06-21 18:35:45 -07007367 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007368 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007369 case MotionEvent.ACTION_HOVER_ENTER:
7370 setHovered(true);
7371 break;
7372 case MotionEvent.ACTION_HOVER_EXIT:
7373 setHovered(false);
7374 break;
7375 }
Jeff Browna1b24182011-07-28 13:38:24 -07007376
7377 // Dispatch the event to onGenericMotionEvent before returning true.
7378 // This is to provide compatibility with existing applications that
7379 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7380 // break because of the new default handling for hoverable views
7381 // in onHoverEvent.
7382 // Note that onGenericMotionEvent will be called by default when
7383 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7384 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007385 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007386 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007387
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007388 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007389 }
7390
7391 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007392 * Returns true if the view should handle {@link #onHoverEvent}
7393 * by calling {@link #setHovered} to change its hovered state.
7394 *
7395 * @return True if the view is hoverable.
7396 */
7397 private boolean isHoverable() {
7398 final int viewFlags = mViewFlags;
7399 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7400 return false;
7401 }
7402
7403 return (viewFlags & CLICKABLE) == CLICKABLE
7404 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7405 }
7406
7407 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007408 * Returns true if the view is currently hovered.
7409 *
7410 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007411 *
7412 * @see #setHovered
7413 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007414 */
Jeff Brown10b62902011-06-20 16:40:37 -07007415 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007416 public boolean isHovered() {
7417 return (mPrivateFlags & HOVERED) != 0;
7418 }
7419
7420 /**
7421 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007422 * <p>
7423 * Calling this method also changes the drawable state of the view. This
7424 * enables the view to react to hover by using different drawable resources
7425 * to change its appearance.
7426 * </p><p>
7427 * The {@link #onHoverChanged} method is called when the hovered state changes.
7428 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007429 *
7430 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007431 *
7432 * @see #isHovered
7433 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007434 */
7435 public void setHovered(boolean hovered) {
7436 if (hovered) {
7437 if ((mPrivateFlags & HOVERED) == 0) {
7438 mPrivateFlags |= HOVERED;
7439 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007440 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007441 }
7442 } else {
7443 if ((mPrivateFlags & HOVERED) != 0) {
7444 mPrivateFlags &= ~HOVERED;
7445 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007446 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007447 }
7448 }
7449 }
7450
7451 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007452 * Implement this method to handle hover state changes.
7453 * <p>
7454 * This method is called whenever the hover state changes as a result of a
7455 * call to {@link #setHovered}.
7456 * </p>
7457 *
7458 * @param hovered The current hover state, as returned by {@link #isHovered}.
7459 *
7460 * @see #isHovered
7461 * @see #setHovered
7462 */
7463 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007464 }
7465
7466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 * Implement this method to handle touch screen motion events.
7468 *
7469 * @param event The motion event.
7470 * @return True if the event was handled, false otherwise.
7471 */
7472 public boolean onTouchEvent(MotionEvent event) {
7473 final int viewFlags = mViewFlags;
7474
7475 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007476 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007477 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 // A disabled view that is clickable still consumes the touch
7480 // events, it just doesn't respond to them.
7481 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7482 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7483 }
7484
7485 if (mTouchDelegate != null) {
7486 if (mTouchDelegate.onTouchEvent(event)) {
7487 return true;
7488 }
7489 }
7490
7491 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7492 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7493 switch (event.getAction()) {
7494 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007495 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7496 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 // take focus if we don't have it already and we should in
7498 // touch mode.
7499 boolean focusTaken = false;
7500 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7501 focusTaken = requestFocus();
7502 }
7503
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007504 if (prepressed) {
7505 // The button is being released before we actually
7506 // showed it as pressed. Make it show the pressed
7507 // state now (before scheduling the click) to ensure
7508 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007509 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007510 }
Joe Malin32736f02011-01-19 16:14:20 -08007511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 if (!mHasPerformedLongPress) {
7513 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007514 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515
7516 // Only perform take click actions if we were in the pressed state
7517 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007518 // Use a Runnable and post this rather than calling
7519 // performClick directly. This lets other visual state
7520 // of the view update before click actions start.
7521 if (mPerformClick == null) {
7522 mPerformClick = new PerformClick();
7523 }
7524 if (!post(mPerformClick)) {
7525 performClick();
7526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 }
7528 }
7529
7530 if (mUnsetPressedState == null) {
7531 mUnsetPressedState = new UnsetPressedState();
7532 }
7533
Adam Powelle14579b2009-12-16 18:39:52 -08007534 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007535 postDelayed(mUnsetPressedState,
7536 ViewConfiguration.getPressedStateDuration());
7537 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 // If the post failed, unpress right now
7539 mUnsetPressedState.run();
7540 }
Adam Powelle14579b2009-12-16 18:39:52 -08007541 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 }
7543 break;
7544
7545 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007546 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007547
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007548 if (performButtonActionOnTouchDown(event)) {
7549 break;
7550 }
7551
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007552 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007553 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007554
7555 // For views inside a scrolling container, delay the pressed feedback for
7556 // a short period in case this is a scroll.
7557 if (isInScrollingContainer) {
7558 mPrivateFlags |= PREPRESSED;
7559 if (mPendingCheckForTap == null) {
7560 mPendingCheckForTap = new CheckForTap();
7561 }
7562 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7563 } else {
7564 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007565 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007566 checkForLongClick(0);
7567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 break;
7569
7570 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007571 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007572 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 break;
7574
7575 case MotionEvent.ACTION_MOVE:
7576 final int x = (int) event.getX();
7577 final int y = (int) event.getY();
7578
7579 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007580 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007582 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007584 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007585 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586
Adam Powell4d6f0662012-02-21 15:11:11 -08007587 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 }
7590 break;
7591 }
7592 return true;
7593 }
7594
7595 return false;
7596 }
7597
7598 /**
Adam Powell10298662011-08-14 18:26:30 -07007599 * @hide
7600 */
7601 public boolean isInScrollingContainer() {
7602 ViewParent p = getParent();
7603 while (p != null && p instanceof ViewGroup) {
7604 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7605 return true;
7606 }
7607 p = p.getParent();
7608 }
7609 return false;
7610 }
7611
7612 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007613 * Remove the longpress detection timer.
7614 */
7615 private void removeLongPressCallback() {
7616 if (mPendingCheckForLongPress != null) {
7617 removeCallbacks(mPendingCheckForLongPress);
7618 }
7619 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007620
7621 /**
7622 * Remove the pending click action
7623 */
7624 private void removePerformClickCallback() {
7625 if (mPerformClick != null) {
7626 removeCallbacks(mPerformClick);
7627 }
7628 }
7629
Adam Powelle14579b2009-12-16 18:39:52 -08007630 /**
Romain Guya440b002010-02-24 15:57:54 -08007631 * Remove the prepress detection timer.
7632 */
7633 private void removeUnsetPressCallback() {
7634 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7635 setPressed(false);
7636 removeCallbacks(mUnsetPressedState);
7637 }
7638 }
7639
7640 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007641 * Remove the tap detection timer.
7642 */
7643 private void removeTapCallback() {
7644 if (mPendingCheckForTap != null) {
7645 mPrivateFlags &= ~PREPRESSED;
7646 removeCallbacks(mPendingCheckForTap);
7647 }
7648 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007649
7650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 * Cancels a pending long press. Your subclass can use this if you
7652 * want the context menu to come up if the user presses and holds
7653 * at the same place, but you don't want it to come up if they press
7654 * and then move around enough to cause scrolling.
7655 */
7656 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007657 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007658
7659 /*
7660 * The prepressed state handled by the tap callback is a display
7661 * construct, but the tap callback will post a long press callback
7662 * less its own timeout. Remove it here.
7663 */
7664 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
7666
7667 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007668 * Remove the pending callback for sending a
7669 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7670 */
7671 private void removeSendViewScrolledAccessibilityEventCallback() {
7672 if (mSendViewScrolledAccessibilityEvent != null) {
7673 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7674 }
7675 }
7676
7677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 * Sets the TouchDelegate for this View.
7679 */
7680 public void setTouchDelegate(TouchDelegate delegate) {
7681 mTouchDelegate = delegate;
7682 }
7683
7684 /**
7685 * Gets the TouchDelegate for this View.
7686 */
7687 public TouchDelegate getTouchDelegate() {
7688 return mTouchDelegate;
7689 }
7690
7691 /**
7692 * Set flags controlling behavior of this view.
7693 *
7694 * @param flags Constant indicating the value which should be set
7695 * @param mask Constant indicating the bit range that should be changed
7696 */
7697 void setFlags(int flags, int mask) {
7698 int old = mViewFlags;
7699 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7700
7701 int changed = mViewFlags ^ old;
7702 if (changed == 0) {
7703 return;
7704 }
7705 int privateFlags = mPrivateFlags;
7706
7707 /* Check if the FOCUSABLE bit has changed */
7708 if (((changed & FOCUSABLE_MASK) != 0) &&
7709 ((privateFlags & HAS_BOUNDS) !=0)) {
7710 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7711 && ((privateFlags & FOCUSED) != 0)) {
7712 /* Give up focus if we are no longer focusable */
7713 clearFocus();
7714 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7715 && ((privateFlags & FOCUSED) == 0)) {
7716 /*
7717 * Tell the view system that we are now available to take focus
7718 * if no one else already has it.
7719 */
7720 if (mParent != null) mParent.focusableViewAvailable(this);
7721 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007722 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7723 notifyAccessibilityStateChanged();
7724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 }
7726
7727 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7728 if ((changed & VISIBILITY_MASK) != 0) {
7729 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007730 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007731 * it was not visible. Marking it drawn ensures that the invalidation will
7732 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007734 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007735 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736
7737 needGlobalAttributesUpdate(true);
7738
7739 // a view becoming visible is worth notifying the parent
7740 // about in case nothing has focus. even if this specific view
7741 // isn't focusable, it may contain something that is, so let
7742 // the root view try to give this focus if nothing else does.
7743 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7744 mParent.focusableViewAvailable(this);
7745 }
7746 }
7747 }
7748
7749 /* Check if the GONE bit has changed */
7750 if ((changed & GONE) != 0) {
7751 needGlobalAttributesUpdate(false);
7752 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753
Romain Guyecd80ee2009-12-03 17:13:02 -08007754 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7755 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007756 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007757 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007758 if (mParent instanceof View) {
7759 // GONE views noop invalidation, so invalidate the parent
7760 ((View) mParent).invalidate(true);
7761 }
7762 // Mark the view drawn to ensure that it gets invalidated properly the next
7763 // time it is visible and gets invalidated
7764 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 }
7766 if (mAttachInfo != null) {
7767 mAttachInfo.mViewVisibilityChanged = true;
7768 }
7769 }
7770
7771 /* Check if the VISIBLE bit has changed */
7772 if ((changed & INVISIBLE) != 0) {
7773 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007774 /*
7775 * If this view is becoming invisible, set the DRAWN flag so that
7776 * the next invalidate() will not be skipped.
7777 */
7778 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779
7780 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007781 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 if (getRootView() != this) {
7783 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007784 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 }
7786 }
7787 if (mAttachInfo != null) {
7788 mAttachInfo.mViewVisibilityChanged = true;
7789 }
7790 }
7791
Adam Powell326d8082009-12-09 15:10:07 -08007792 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007793 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007794 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7795 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007796 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007797 } else if (mParent != null) {
7798 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007799 }
Adam Powell326d8082009-12-09 15:10:07 -08007800 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7801 }
7802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7804 destroyDrawingCache();
7805 }
7806
7807 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7808 destroyDrawingCache();
7809 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007810 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
7812
7813 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7814 destroyDrawingCache();
7815 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7816 }
7817
7818 if ((changed & DRAW_MASK) != 0) {
7819 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007820 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 mPrivateFlags &= ~SKIP_DRAW;
7822 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7823 } else {
7824 mPrivateFlags |= SKIP_DRAW;
7825 }
7826 } else {
7827 mPrivateFlags &= ~SKIP_DRAW;
7828 }
7829 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007830 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 }
7832
7833 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007834 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 mParent.recomputeViewAttributes(this);
7836 }
7837 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007838
7839 if (AccessibilityManager.getInstance(mContext).isEnabled()
7840 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7841 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7842 notifyAccessibilityStateChanged();
7843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 }
7845
7846 /**
7847 * Change the view's z order in the tree, so it's on top of other sibling
7848 * views
7849 */
7850 public void bringToFront() {
7851 if (mParent != null) {
7852 mParent.bringChildToFront(this);
7853 }
7854 }
7855
7856 /**
7857 * This is called in response to an internal scroll in this view (i.e., the
7858 * view scrolled its own contents). This is typically as a result of
7859 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7860 * called.
7861 *
7862 * @param l Current horizontal scroll origin.
7863 * @param t Current vertical scroll origin.
7864 * @param oldl Previous horizontal scroll origin.
7865 * @param oldt Previous vertical scroll origin.
7866 */
7867 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007868 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7869 postSendViewScrolledAccessibilityEventCallback();
7870 }
7871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 mBackgroundSizeChanged = true;
7873
7874 final AttachInfo ai = mAttachInfo;
7875 if (ai != null) {
7876 ai.mViewScrollChanged = true;
7877 }
7878 }
7879
7880 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007881 * Interface definition for a callback to be invoked when the layout bounds of a view
7882 * changes due to layout processing.
7883 */
7884 public interface OnLayoutChangeListener {
7885 /**
7886 * Called when the focus state of a view has changed.
7887 *
7888 * @param v The view whose state has changed.
7889 * @param left The new value of the view's left property.
7890 * @param top The new value of the view's top property.
7891 * @param right The new value of the view's right property.
7892 * @param bottom The new value of the view's bottom property.
7893 * @param oldLeft The previous value of the view's left property.
7894 * @param oldTop The previous value of the view's top property.
7895 * @param oldRight The previous value of the view's right property.
7896 * @param oldBottom The previous value of the view's bottom property.
7897 */
7898 void onLayoutChange(View v, int left, int top, int right, int bottom,
7899 int oldLeft, int oldTop, int oldRight, int oldBottom);
7900 }
7901
7902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 * This is called during layout when the size of this view has changed. If
7904 * you were just added to the view hierarchy, you're called with the old
7905 * values of 0.
7906 *
7907 * @param w Current width of this view.
7908 * @param h Current height of this view.
7909 * @param oldw Old width of this view.
7910 * @param oldh Old height of this view.
7911 */
7912 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7913 }
7914
7915 /**
7916 * Called by draw to draw the child views. This may be overridden
7917 * by derived classes to gain control just before its children are drawn
7918 * (but after its own view has been drawn).
7919 * @param canvas the canvas on which to draw the view
7920 */
7921 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 }
7924
7925 /**
7926 * Gets the parent of this view. Note that the parent is a
7927 * ViewParent and not necessarily a View.
7928 *
7929 * @return Parent of this view.
7930 */
7931 public final ViewParent getParent() {
7932 return mParent;
7933 }
7934
7935 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007936 * Set the horizontal scrolled position of your view. This will cause a call to
7937 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7938 * invalidated.
7939 * @param value the x position to scroll to
7940 */
7941 public void setScrollX(int value) {
7942 scrollTo(value, mScrollY);
7943 }
7944
7945 /**
7946 * Set the vertical scrolled position of your view. This will cause a call to
7947 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7948 * invalidated.
7949 * @param value the y position to scroll to
7950 */
7951 public void setScrollY(int value) {
7952 scrollTo(mScrollX, value);
7953 }
7954
7955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 * Return the scrolled left position of this view. This is the left edge of
7957 * the displayed part of your view. You do not need to draw any pixels
7958 * farther left, since those are outside of the frame of your view on
7959 * screen.
7960 *
7961 * @return The left edge of the displayed part of your view, in pixels.
7962 */
7963 public final int getScrollX() {
7964 return mScrollX;
7965 }
7966
7967 /**
7968 * Return the scrolled top position of this view. This is the top edge of
7969 * the displayed part of your view. You do not need to draw any pixels above
7970 * it, since those are outside of the frame of your view on screen.
7971 *
7972 * @return The top edge of the displayed part of your view, in pixels.
7973 */
7974 public final int getScrollY() {
7975 return mScrollY;
7976 }
7977
7978 /**
7979 * Return the width of the your view.
7980 *
7981 * @return The width of your view, in pixels.
7982 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007983 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 public final int getWidth() {
7985 return mRight - mLeft;
7986 }
7987
7988 /**
7989 * Return the height of your view.
7990 *
7991 * @return The height of your view, in pixels.
7992 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007993 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007994 public final int getHeight() {
7995 return mBottom - mTop;
7996 }
7997
7998 /**
7999 * Return the visible drawing bounds of your view. Fills in the output
8000 * rectangle with the values from getScrollX(), getScrollY(),
8001 * getWidth(), and getHeight().
8002 *
8003 * @param outRect The (scrolled) drawing bounds of the view.
8004 */
8005 public void getDrawingRect(Rect outRect) {
8006 outRect.left = mScrollX;
8007 outRect.top = mScrollY;
8008 outRect.right = mScrollX + (mRight - mLeft);
8009 outRect.bottom = mScrollY + (mBottom - mTop);
8010 }
8011
8012 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008013 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8014 * raw width component (that is the result is masked by
8015 * {@link #MEASURED_SIZE_MASK}).
8016 *
8017 * @return The raw measured width of this view.
8018 */
8019 public final int getMeasuredWidth() {
8020 return mMeasuredWidth & MEASURED_SIZE_MASK;
8021 }
8022
8023 /**
8024 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008025 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008026 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 * This should be used during measurement and layout calculations only. Use
8028 * {@link #getWidth()} to see how wide a view is after layout.
8029 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008030 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008032 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 return mMeasuredWidth;
8034 }
8035
8036 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008037 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8038 * raw width component (that is the result is masked by
8039 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008041 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 */
8043 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008044 return mMeasuredHeight & MEASURED_SIZE_MASK;
8045 }
8046
8047 /**
8048 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008049 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008050 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8051 * This should be used during measurement and layout calculations only. Use
8052 * {@link #getHeight()} to see how wide a view is after layout.
8053 *
8054 * @return The measured width of this view as a bit mask.
8055 */
8056 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 return mMeasuredHeight;
8058 }
8059
8060 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008061 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8062 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8063 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8064 * and the height component is at the shifted bits
8065 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8066 */
8067 public final int getMeasuredState() {
8068 return (mMeasuredWidth&MEASURED_STATE_MASK)
8069 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8070 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8071 }
8072
8073 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008074 * The transform matrix of this view, which is calculated based on the current
8075 * roation, scale, and pivot properties.
8076 *
8077 * @see #getRotation()
8078 * @see #getScaleX()
8079 * @see #getScaleY()
8080 * @see #getPivotX()
8081 * @see #getPivotY()
8082 * @return The current transform matrix for the view
8083 */
8084 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008085 if (mTransformationInfo != null) {
8086 updateMatrix();
8087 return mTransformationInfo.mMatrix;
8088 }
8089 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008090 }
8091
8092 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008093 * Utility function to determine if the value is far enough away from zero to be
8094 * considered non-zero.
8095 * @param value A floating point value to check for zero-ness
8096 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8097 */
8098 private static boolean nonzero(float value) {
8099 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8100 }
8101
8102 /**
Jeff Brown86671742010-09-30 20:00:15 -07008103 * Returns true if the transform matrix is the identity matrix.
8104 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008105 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008106 * @return True if the transform matrix is the identity matrix, false otherwise.
8107 */
Jeff Brown86671742010-09-30 20:00:15 -07008108 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008109 if (mTransformationInfo != null) {
8110 updateMatrix();
8111 return mTransformationInfo.mMatrixIsIdentity;
8112 }
8113 return true;
8114 }
8115
8116 void ensureTransformationInfo() {
8117 if (mTransformationInfo == null) {
8118 mTransformationInfo = new TransformationInfo();
8119 }
Jeff Brown86671742010-09-30 20:00:15 -07008120 }
8121
8122 /**
8123 * Recomputes the transform matrix if necessary.
8124 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008125 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008126 final TransformationInfo info = mTransformationInfo;
8127 if (info == null) {
8128 return;
8129 }
8130 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008131 // transform-related properties have changed since the last time someone
8132 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008133
8134 // Figure out if we need to update the pivot point
8135 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008136 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8137 info.mPrevWidth = mRight - mLeft;
8138 info.mPrevHeight = mBottom - mTop;
8139 info.mPivotX = info.mPrevWidth / 2f;
8140 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008141 }
8142 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008143 info.mMatrix.reset();
8144 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8145 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8146 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8147 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008148 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008149 if (info.mCamera == null) {
8150 info.mCamera = new Camera();
8151 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008152 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008153 info.mCamera.save();
8154 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8155 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8156 info.mCamera.getMatrix(info.matrix3D);
8157 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8158 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8159 info.mPivotY + info.mTranslationY);
8160 info.mMatrix.postConcat(info.matrix3D);
8161 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008162 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008163 info.mMatrixDirty = false;
8164 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8165 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008166 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008167 }
8168
8169 /**
8170 * Utility method to retrieve the inverse of the current mMatrix property.
8171 * We cache the matrix to avoid recalculating it when transform properties
8172 * have not changed.
8173 *
8174 * @return The inverse of the current matrix of this view.
8175 */
Jeff Brown86671742010-09-30 20:00:15 -07008176 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008177 final TransformationInfo info = mTransformationInfo;
8178 if (info != null) {
8179 updateMatrix();
8180 if (info.mInverseMatrixDirty) {
8181 if (info.mInverseMatrix == null) {
8182 info.mInverseMatrix = new Matrix();
8183 }
8184 info.mMatrix.invert(info.mInverseMatrix);
8185 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008186 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008187 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008188 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008189 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008190 }
8191
8192 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008193 * Gets the distance along the Z axis from the camera to this view.
8194 *
8195 * @see #setCameraDistance(float)
8196 *
8197 * @return The distance along the Z axis.
8198 */
8199 public float getCameraDistance() {
8200 ensureTransformationInfo();
8201 final float dpi = mResources.getDisplayMetrics().densityDpi;
8202 final TransformationInfo info = mTransformationInfo;
8203 if (info.mCamera == null) {
8204 info.mCamera = new Camera();
8205 info.matrix3D = new Matrix();
8206 }
8207 return -(info.mCamera.getLocationZ() * dpi);
8208 }
8209
8210 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008211 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8212 * views are drawn) from the camera to this view. The camera's distance
8213 * affects 3D transformations, for instance rotations around the X and Y
8214 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008215 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008216 * use a camera distance that's greater than the height (X axis rotation) or
8217 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008218 *
Romain Guya5364ee2011-02-24 14:46:04 -08008219 * <p>The distance of the camera from the view plane can have an affect on the
8220 * perspective distortion of the view when it is rotated around the x or y axis.
8221 * For example, a large distance will result in a large viewing angle, and there
8222 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008223 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008224 * also result in some drawing artifacts if the rotated view ends up partially
8225 * behind the camera (which is why the recommendation is to use a distance at
8226 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008227 *
Romain Guya5364ee2011-02-24 14:46:04 -08008228 * <p>The distance is expressed in "depth pixels." The default distance depends
8229 * on the screen density. For instance, on a medium density display, the
8230 * default distance is 1280. On a high density display, the default distance
8231 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008232 *
Romain Guya5364ee2011-02-24 14:46:04 -08008233 * <p>If you want to specify a distance that leads to visually consistent
8234 * results across various densities, use the following formula:</p>
8235 * <pre>
8236 * float scale = context.getResources().getDisplayMetrics().density;
8237 * view.setCameraDistance(distance * scale);
8238 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008239 *
Romain Guya5364ee2011-02-24 14:46:04 -08008240 * <p>The density scale factor of a high density display is 1.5,
8241 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008242 *
Romain Guya5364ee2011-02-24 14:46:04 -08008243 * @param distance The distance in "depth pixels", if negative the opposite
8244 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008245 *
8246 * @see #setRotationX(float)
8247 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008248 */
8249 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008250 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008251
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008252 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008253 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008254 final TransformationInfo info = mTransformationInfo;
8255 if (info.mCamera == null) {
8256 info.mCamera = new Camera();
8257 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008258 }
8259
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008260 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8261 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008262
Chet Haase9d1992d2012-03-13 11:03:25 -07008263 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008264 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008265 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008266 }
Romain Guya5364ee2011-02-24 14:46:04 -08008267 }
8268
8269 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008270 * The degrees that the view is rotated around the pivot point.
8271 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008272 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008273 * @see #getPivotX()
8274 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008275 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008276 * @return The degrees of rotation.
8277 */
Chet Haasea5531132012-02-02 13:41:44 -08008278 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008279 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008280 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008281 }
8282
8283 /**
Chet Haase897247b2010-09-09 14:54:47 -07008284 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8285 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008286 *
8287 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008288 *
8289 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008290 * @see #getPivotX()
8291 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008292 * @see #setRotationX(float)
8293 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008294 *
8295 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008296 */
8297 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008298 ensureTransformationInfo();
8299 final TransformationInfo info = mTransformationInfo;
8300 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008301 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008302 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008303 info.mRotation = rotation;
8304 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008305 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008306 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008307 mDisplayList.setRotation(rotation);
8308 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008309 }
8310 }
8311
8312 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008313 * The degrees that the view is rotated around the vertical axis through the pivot point.
8314 *
8315 * @see #getPivotX()
8316 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008317 * @see #setRotationY(float)
8318 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008319 * @return The degrees of Y rotation.
8320 */
Chet Haasea5531132012-02-02 13:41:44 -08008321 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008322 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008323 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008324 }
8325
8326 /**
Chet Haase897247b2010-09-09 14:54:47 -07008327 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8328 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8329 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008330 *
Romain Guya5364ee2011-02-24 14:46:04 -08008331 * When rotating large views, it is recommended to adjust the camera distance
8332 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008333 *
8334 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008335 *
8336 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008337 * @see #getPivotX()
8338 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008339 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008340 * @see #setRotationX(float)
8341 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008342 *
8343 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008344 */
8345 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008346 ensureTransformationInfo();
8347 final TransformationInfo info = mTransformationInfo;
8348 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008349 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008350 info.mRotationY = rotationY;
8351 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008352 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008353 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008354 mDisplayList.setRotationY(rotationY);
8355 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008356 }
8357 }
8358
8359 /**
8360 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8361 *
8362 * @see #getPivotX()
8363 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008364 * @see #setRotationX(float)
8365 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008366 * @return The degrees of X rotation.
8367 */
Chet Haasea5531132012-02-02 13:41:44 -08008368 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008369 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008370 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008371 }
8372
8373 /**
Chet Haase897247b2010-09-09 14:54:47 -07008374 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8375 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8376 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008377 *
Romain Guya5364ee2011-02-24 14:46:04 -08008378 * When rotating large views, it is recommended to adjust the camera distance
8379 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008380 *
8381 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008382 *
8383 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008384 * @see #getPivotX()
8385 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008386 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008387 * @see #setRotationY(float)
8388 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008389 *
8390 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008391 */
8392 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008393 ensureTransformationInfo();
8394 final TransformationInfo info = mTransformationInfo;
8395 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008396 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008397 info.mRotationX = rotationX;
8398 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008399 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008400 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008401 mDisplayList.setRotationX(rotationX);
8402 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008403 }
8404 }
8405
8406 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008407 * The amount that the view is scaled in x around the pivot point, as a proportion of
8408 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8409 *
Joe Onorato93162322010-09-16 15:42:01 -04008410 * <p>By default, this is 1.0f.
8411 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008412 * @see #getPivotX()
8413 * @see #getPivotY()
8414 * @return The scaling factor.
8415 */
Chet Haasea5531132012-02-02 13:41:44 -08008416 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008417 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008418 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008419 }
8420
8421 /**
8422 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8423 * the view's unscaled width. A value of 1 means that no scaling is applied.
8424 *
8425 * @param scaleX The scaling factor.
8426 * @see #getPivotX()
8427 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008428 *
8429 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008430 */
8431 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008432 ensureTransformationInfo();
8433 final TransformationInfo info = mTransformationInfo;
8434 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008435 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008436 info.mScaleX = scaleX;
8437 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008438 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008439 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008440 mDisplayList.setScaleX(scaleX);
8441 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008442 }
8443 }
8444
8445 /**
8446 * The amount that the view is scaled in y around the pivot point, as a proportion of
8447 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8448 *
Joe Onorato93162322010-09-16 15:42:01 -04008449 * <p>By default, this is 1.0f.
8450 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008451 * @see #getPivotX()
8452 * @see #getPivotY()
8453 * @return The scaling factor.
8454 */
Chet Haasea5531132012-02-02 13:41:44 -08008455 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008456 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008457 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008458 }
8459
8460 /**
8461 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8462 * the view's unscaled width. A value of 1 means that no scaling is applied.
8463 *
8464 * @param scaleY The scaling factor.
8465 * @see #getPivotX()
8466 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008467 *
8468 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008469 */
8470 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008471 ensureTransformationInfo();
8472 final TransformationInfo info = mTransformationInfo;
8473 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008474 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008475 info.mScaleY = scaleY;
8476 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008477 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008478 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008479 mDisplayList.setScaleY(scaleY);
8480 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008481 }
8482 }
8483
8484 /**
8485 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8486 * and {@link #setScaleX(float) scaled}.
8487 *
8488 * @see #getRotation()
8489 * @see #getScaleX()
8490 * @see #getScaleY()
8491 * @see #getPivotY()
8492 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008493 *
8494 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008495 */
Chet Haasea5531132012-02-02 13:41:44 -08008496 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008497 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008498 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008499 }
8500
8501 /**
8502 * Sets the x location of the point around which the view is
8503 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008504 * By default, the pivot point is centered on the object.
8505 * Setting this property disables this behavior and causes the view to use only the
8506 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008507 *
8508 * @param pivotX The x location of the pivot point.
8509 * @see #getRotation()
8510 * @see #getScaleX()
8511 * @see #getScaleY()
8512 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008513 *
8514 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008515 */
8516 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008517 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008518 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008519 final TransformationInfo info = mTransformationInfo;
8520 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008521 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008522 info.mPivotX = pivotX;
8523 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008524 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008525 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008526 mDisplayList.setPivotX(pivotX);
8527 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008528 }
8529 }
8530
8531 /**
8532 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8533 * and {@link #setScaleY(float) scaled}.
8534 *
8535 * @see #getRotation()
8536 * @see #getScaleX()
8537 * @see #getScaleY()
8538 * @see #getPivotY()
8539 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008540 *
8541 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008542 */
Chet Haasea5531132012-02-02 13:41:44 -08008543 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008544 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008545 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008546 }
8547
8548 /**
8549 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008550 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8551 * Setting this property disables this behavior and causes the view to use only the
8552 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008553 *
8554 * @param pivotY The y location of the pivot point.
8555 * @see #getRotation()
8556 * @see #getScaleX()
8557 * @see #getScaleY()
8558 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008559 *
8560 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008561 */
8562 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008563 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008564 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008565 final TransformationInfo info = mTransformationInfo;
8566 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008567 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008568 info.mPivotY = pivotY;
8569 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008570 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008571 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008572 mDisplayList.setPivotY(pivotY);
8573 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008574 }
8575 }
8576
8577 /**
8578 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8579 * completely transparent and 1 means the view is completely opaque.
8580 *
Joe Onorato93162322010-09-16 15:42:01 -04008581 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008582 * @return The opacity of the view.
8583 */
Chet Haasea5531132012-02-02 13:41:44 -08008584 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008585 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008586 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008587 }
8588
8589 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008590 * Returns whether this View has content which overlaps. This function, intended to be
8591 * overridden by specific View types, is an optimization when alpha is set on a view. If
8592 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8593 * and then composited it into place, which can be expensive. If the view has no overlapping
8594 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8595 * An example of overlapping rendering is a TextView with a background image, such as a
8596 * Button. An example of non-overlapping rendering is a TextView with no background, or
8597 * an ImageView with only the foreground image. The default implementation returns true;
8598 * subclasses should override if they have cases which can be optimized.
8599 *
8600 * @return true if the content in this view might overlap, false otherwise.
8601 */
8602 public boolean hasOverlappingRendering() {
8603 return true;
8604 }
8605
8606 /**
Romain Guy171c5922011-01-06 10:04:23 -08008607 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8608 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008609 *
Romain Guy171c5922011-01-06 10:04:23 -08008610 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8611 * responsible for applying the opacity itself. Otherwise, calling this method is
8612 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008613 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008614 *
Chet Haasea5531132012-02-02 13:41:44 -08008615 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8616 * performance implications. It is generally best to use the alpha property sparingly and
8617 * transiently, as in the case of fading animations.</p>
8618 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008619 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008620 *
Joe Malin32736f02011-01-19 16:14:20 -08008621 * @see #setLayerType(int, android.graphics.Paint)
8622 *
Chet Haase73066682010-11-29 15:55:32 -08008623 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008624 */
8625 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008626 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008627 if (mTransformationInfo.mAlpha != alpha) {
8628 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008629 if (onSetAlpha((int) (alpha * 255))) {
8630 mPrivateFlags |= ALPHA_SET;
8631 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008632 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008633 invalidate(true);
8634 } else {
8635 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008636 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008637 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008638 mDisplayList.setAlpha(alpha);
8639 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008640 }
Chet Haaseed032702010-10-01 14:05:54 -07008641 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008642 }
8643
8644 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008645 * Faster version of setAlpha() which performs the same steps except there are
8646 * no calls to invalidate(). The caller of this function should perform proper invalidation
8647 * on the parent and this object. The return value indicates whether the subclass handles
8648 * alpha (the return value for onSetAlpha()).
8649 *
8650 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008651 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8652 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008653 */
8654 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008655 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008656 if (mTransformationInfo.mAlpha != alpha) {
8657 mTransformationInfo.mAlpha = alpha;
8658 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8659 if (subclassHandlesAlpha) {
8660 mPrivateFlags |= ALPHA_SET;
8661 return true;
8662 } else {
8663 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008664 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008665 mDisplayList.setAlpha(alpha);
8666 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008667 }
Chet Haasea00f3862011-02-22 06:34:40 -08008668 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008669 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008670 }
8671
8672 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 * Top position of this view relative to its parent.
8674 *
8675 * @return The top of this view, in pixels.
8676 */
8677 @ViewDebug.CapturedViewProperty
8678 public final int getTop() {
8679 return mTop;
8680 }
8681
8682 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008683 * Sets the top position of this view relative to its parent. This method is meant to be called
8684 * by the layout system and should not generally be called otherwise, because the property
8685 * may be changed at any time by the layout.
8686 *
8687 * @param top The top of this view, in pixels.
8688 */
8689 public final void setTop(int top) {
8690 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008691 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008692 final boolean matrixIsIdentity = mTransformationInfo == null
8693 || mTransformationInfo.mMatrixIsIdentity;
8694 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008695 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008696 int minTop;
8697 int yLoc;
8698 if (top < mTop) {
8699 minTop = top;
8700 yLoc = top - mTop;
8701 } else {
8702 minTop = mTop;
8703 yLoc = 0;
8704 }
Chet Haasee9140a72011-02-16 16:23:29 -08008705 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008706 }
8707 } else {
8708 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008709 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008710 }
8711
Chet Haaseed032702010-10-01 14:05:54 -07008712 int width = mRight - mLeft;
8713 int oldHeight = mBottom - mTop;
8714
Chet Haase21cd1382010-09-01 17:42:29 -07008715 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07008716 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008717 mDisplayList.setTop(mTop);
8718 }
Chet Haase21cd1382010-09-01 17:42:29 -07008719
Chet Haaseed032702010-10-01 14:05:54 -07008720 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8721
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008722 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008723 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8724 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008725 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008726 }
Chet Haase21cd1382010-09-01 17:42:29 -07008727 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008728 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008729 }
Chet Haase55dbb652010-12-21 20:15:08 -08008730 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008731 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008732 }
8733 }
8734
8735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008736 * Bottom position of this view relative to its parent.
8737 *
8738 * @return The bottom of this view, in pixels.
8739 */
8740 @ViewDebug.CapturedViewProperty
8741 public final int getBottom() {
8742 return mBottom;
8743 }
8744
8745 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008746 * True if this view has changed since the last time being drawn.
8747 *
8748 * @return The dirty state of this view.
8749 */
8750 public boolean isDirty() {
8751 return (mPrivateFlags & DIRTY_MASK) != 0;
8752 }
8753
8754 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008755 * Sets the bottom position of this view relative to its parent. This method is meant to be
8756 * called by the layout system and should not generally be called otherwise, because the
8757 * property may be changed at any time by the layout.
8758 *
8759 * @param bottom The bottom of this view, in pixels.
8760 */
8761 public final void setBottom(int bottom) {
8762 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008763 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008764 final boolean matrixIsIdentity = mTransformationInfo == null
8765 || mTransformationInfo.mMatrixIsIdentity;
8766 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008767 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008768 int maxBottom;
8769 if (bottom < mBottom) {
8770 maxBottom = mBottom;
8771 } else {
8772 maxBottom = bottom;
8773 }
Chet Haasee9140a72011-02-16 16:23:29 -08008774 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008775 }
8776 } else {
8777 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008778 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008779 }
8780
Chet Haaseed032702010-10-01 14:05:54 -07008781 int width = mRight - mLeft;
8782 int oldHeight = mBottom - mTop;
8783
Chet Haase21cd1382010-09-01 17:42:29 -07008784 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07008785 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008786 mDisplayList.setBottom(mBottom);
8787 }
Chet Haase21cd1382010-09-01 17:42:29 -07008788
Chet Haaseed032702010-10-01 14:05:54 -07008789 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8790
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008791 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008792 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8793 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008794 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008795 }
Chet Haase21cd1382010-09-01 17:42:29 -07008796 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008797 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008798 }
Chet Haase55dbb652010-12-21 20:15:08 -08008799 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008800 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008801 }
8802 }
8803
8804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008805 * Left position of this view relative to its parent.
8806 *
8807 * @return The left edge of this view, in pixels.
8808 */
8809 @ViewDebug.CapturedViewProperty
8810 public final int getLeft() {
8811 return mLeft;
8812 }
8813
8814 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008815 * Sets the left position of this view relative to its parent. This method is meant to be called
8816 * by the layout system and should not generally be called otherwise, because the property
8817 * may be changed at any time by the layout.
8818 *
8819 * @param left The bottom of this view, in pixels.
8820 */
8821 public final void setLeft(int left) {
8822 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008823 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008824 final boolean matrixIsIdentity = mTransformationInfo == null
8825 || mTransformationInfo.mMatrixIsIdentity;
8826 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008827 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008828 int minLeft;
8829 int xLoc;
8830 if (left < mLeft) {
8831 minLeft = left;
8832 xLoc = left - mLeft;
8833 } else {
8834 minLeft = mLeft;
8835 xLoc = 0;
8836 }
Chet Haasee9140a72011-02-16 16:23:29 -08008837 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008838 }
8839 } else {
8840 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008841 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008842 }
8843
Chet Haaseed032702010-10-01 14:05:54 -07008844 int oldWidth = mRight - mLeft;
8845 int height = mBottom - mTop;
8846
Chet Haase21cd1382010-09-01 17:42:29 -07008847 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07008848 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008849 mDisplayList.setLeft(left);
8850 }
Chet Haase21cd1382010-09-01 17:42:29 -07008851
Chet Haaseed032702010-10-01 14:05:54 -07008852 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8853
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008854 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008855 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8856 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008857 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008858 }
Chet Haase21cd1382010-09-01 17:42:29 -07008859 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008860 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008861 }
Chet Haase55dbb652010-12-21 20:15:08 -08008862 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008863 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008864 }
8865 }
8866
8867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008868 * Right position of this view relative to its parent.
8869 *
8870 * @return The right edge of this view, in pixels.
8871 */
8872 @ViewDebug.CapturedViewProperty
8873 public final int getRight() {
8874 return mRight;
8875 }
8876
8877 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008878 * Sets the right position of this view relative to its parent. This method is meant to be called
8879 * by the layout system and should not generally be called otherwise, because the property
8880 * may be changed at any time by the layout.
8881 *
8882 * @param right The bottom of this view, in pixels.
8883 */
8884 public final void setRight(int right) {
8885 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008886 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008887 final boolean matrixIsIdentity = mTransformationInfo == null
8888 || mTransformationInfo.mMatrixIsIdentity;
8889 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008890 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008891 int maxRight;
8892 if (right < mRight) {
8893 maxRight = mRight;
8894 } else {
8895 maxRight = right;
8896 }
Chet Haasee9140a72011-02-16 16:23:29 -08008897 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008898 }
8899 } else {
8900 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008901 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008902 }
8903
Chet Haaseed032702010-10-01 14:05:54 -07008904 int oldWidth = mRight - mLeft;
8905 int height = mBottom - mTop;
8906
Chet Haase21cd1382010-09-01 17:42:29 -07008907 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07008908 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008909 mDisplayList.setRight(mRight);
8910 }
Chet Haase21cd1382010-09-01 17:42:29 -07008911
Chet Haaseed032702010-10-01 14:05:54 -07008912 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8913
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008914 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008915 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8916 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008917 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008918 }
Chet Haase21cd1382010-09-01 17:42:29 -07008919 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008920 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008921 }
Chet Haase55dbb652010-12-21 20:15:08 -08008922 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008923 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008924 }
8925 }
8926
8927 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008928 * The visual x position of this view, in pixels. This is equivalent to the
8929 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008930 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008931 *
Chet Haasedf030d22010-07-30 17:22:38 -07008932 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 */
Chet Haasea5531132012-02-02 13:41:44 -08008934 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008935 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008936 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008937 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008938
Chet Haasedf030d22010-07-30 17:22:38 -07008939 /**
8940 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8941 * {@link #setTranslationX(float) translationX} property to be the difference between
8942 * the x value passed in and the current {@link #getLeft() left} property.
8943 *
8944 * @param x The visual x position of this view, in pixels.
8945 */
8946 public void setX(float x) {
8947 setTranslationX(x - mLeft);
8948 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008949
Chet Haasedf030d22010-07-30 17:22:38 -07008950 /**
8951 * The visual y position of this view, in pixels. This is equivalent to the
8952 * {@link #setTranslationY(float) translationY} property plus the current
8953 * {@link #getTop() top} property.
8954 *
8955 * @return The visual y position of this view, in pixels.
8956 */
Chet Haasea5531132012-02-02 13:41:44 -08008957 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008958 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008959 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008960 }
8961
8962 /**
8963 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8964 * {@link #setTranslationY(float) translationY} property to be the difference between
8965 * the y value passed in and the current {@link #getTop() top} property.
8966 *
8967 * @param y The visual y position of this view, in pixels.
8968 */
8969 public void setY(float y) {
8970 setTranslationY(y - mTop);
8971 }
8972
8973
8974 /**
8975 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8976 * This position is post-layout, in addition to wherever the object's
8977 * layout placed it.
8978 *
8979 * @return The horizontal position of this view relative to its left position, in pixels.
8980 */
Chet Haasea5531132012-02-02 13:41:44 -08008981 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008982 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008983 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008984 }
8985
8986 /**
8987 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8988 * This effectively positions the object post-layout, in addition to wherever the object's
8989 * layout placed it.
8990 *
8991 * @param translationX The horizontal position of this view relative to its left position,
8992 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008993 *
8994 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008995 */
8996 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008997 ensureTransformationInfo();
8998 final TransformationInfo info = mTransformationInfo;
8999 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009000 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009001 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009002 info.mTranslationX = translationX;
9003 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009004 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009005 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009006 mDisplayList.setTranslationX(translationX);
9007 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 }
9009 }
9010
9011 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009012 * The horizontal location of this view relative to its {@link #getTop() top} position.
9013 * This position is post-layout, in addition to wherever the object's
9014 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009015 *
Chet Haasedf030d22010-07-30 17:22:38 -07009016 * @return The vertical position of this view relative to its top position,
9017 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009018 */
Chet Haasea5531132012-02-02 13:41:44 -08009019 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009020 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009021 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009022 }
9023
9024 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009025 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9026 * This effectively positions the object post-layout, in addition to wherever the object's
9027 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009028 *
Chet Haasedf030d22010-07-30 17:22:38 -07009029 * @param translationY The vertical position of this view relative to its top position,
9030 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009031 *
9032 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009033 */
Chet Haasedf030d22010-07-30 17:22:38 -07009034 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009035 ensureTransformationInfo();
9036 final TransformationInfo info = mTransformationInfo;
9037 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009038 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009039 info.mTranslationY = translationY;
9040 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009041 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009042 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009043 mDisplayList.setTranslationY(translationY);
9044 }
Chet Haasedf030d22010-07-30 17:22:38 -07009045 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 }
9047
9048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009049 * Hit rectangle in parent's coordinates
9050 *
9051 * @param outRect The hit rectangle of the view.
9052 */
9053 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009054 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009055 final TransformationInfo info = mTransformationInfo;
9056 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009057 outRect.set(mLeft, mTop, mRight, mBottom);
9058 } else {
9059 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009060 tmpRect.set(-info.mPivotX, -info.mPivotY,
9061 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9062 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009063 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9064 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009065 }
9066 }
9067
9068 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009069 * Determines whether the given point, in local coordinates is inside the view.
9070 */
9071 /*package*/ final boolean pointInView(float localX, float localY) {
9072 return localX >= 0 && localX < (mRight - mLeft)
9073 && localY >= 0 && localY < (mBottom - mTop);
9074 }
9075
9076 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009077 * Utility method to determine whether the given point, in local coordinates,
9078 * is inside the view, where the area of the view is expanded by the slop factor.
9079 * This method is called while processing touch-move events to determine if the event
9080 * is still within the view.
9081 */
9082 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009083 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009084 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 }
9086
9087 /**
9088 * When a view has focus and the user navigates away from it, the next view is searched for
9089 * starting from the rectangle filled in by this method.
9090 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009091 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9092 * of the view. However, if your view maintains some idea of internal selection,
9093 * such as a cursor, or a selected row or column, you should override this method and
9094 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 *
9096 * @param r The rectangle to fill in, in this view's coordinates.
9097 */
9098 public void getFocusedRect(Rect r) {
9099 getDrawingRect(r);
9100 }
9101
9102 /**
9103 * If some part of this view is not clipped by any of its parents, then
9104 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009105 * coordinates (without taking possible View rotations into account), offset
9106 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9107 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 *
9109 * @param r If true is returned, r holds the global coordinates of the
9110 * visible portion of this view.
9111 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9112 * between this view and its root. globalOffet may be null.
9113 * @return true if r is non-empty (i.e. part of the view is visible at the
9114 * root level.
9115 */
9116 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9117 int width = mRight - mLeft;
9118 int height = mBottom - mTop;
9119 if (width > 0 && height > 0) {
9120 r.set(0, 0, width, height);
9121 if (globalOffset != null) {
9122 globalOffset.set(-mScrollX, -mScrollY);
9123 }
9124 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9125 }
9126 return false;
9127 }
9128
9129 public final boolean getGlobalVisibleRect(Rect r) {
9130 return getGlobalVisibleRect(r, null);
9131 }
9132
9133 public final boolean getLocalVisibleRect(Rect r) {
9134 Point offset = new Point();
9135 if (getGlobalVisibleRect(r, offset)) {
9136 r.offset(-offset.x, -offset.y); // make r local
9137 return true;
9138 }
9139 return false;
9140 }
9141
9142 /**
9143 * Offset this view's vertical location by the specified number of pixels.
9144 *
9145 * @param offset the number of pixels to offset the view by
9146 */
9147 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009148 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009149 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 final boolean matrixIsIdentity = mTransformationInfo == null
9151 || mTransformationInfo.mMatrixIsIdentity;
9152 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009153 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009154 invalidateViewProperty(false, false);
9155 } else {
9156 final ViewParent p = mParent;
9157 if (p != null && mAttachInfo != null) {
9158 final Rect r = mAttachInfo.mTmpInvalRect;
9159 int minTop;
9160 int maxBottom;
9161 int yLoc;
9162 if (offset < 0) {
9163 minTop = mTop + offset;
9164 maxBottom = mBottom;
9165 yLoc = offset;
9166 } else {
9167 minTop = mTop;
9168 maxBottom = mBottom + offset;
9169 yLoc = 0;
9170 }
9171 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9172 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009173 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 }
9175 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009176 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009177 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009178
Chet Haasec3aa3612010-06-17 08:50:37 -07009179 mTop += offset;
9180 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009181 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009182 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009183 invalidateViewProperty(false, false);
9184 } else {
9185 if (!matrixIsIdentity) {
9186 invalidateViewProperty(false, true);
9187 }
9188 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009189 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 }
9192
9193 /**
9194 * Offset this view's horizontal location by the specified amount of pixels.
9195 *
9196 * @param offset the numer of pixels to offset the view by
9197 */
9198 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009200 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009201 final boolean matrixIsIdentity = mTransformationInfo == null
9202 || mTransformationInfo.mMatrixIsIdentity;
9203 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009204 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009205 invalidateViewProperty(false, false);
9206 } else {
9207 final ViewParent p = mParent;
9208 if (p != null && mAttachInfo != null) {
9209 final Rect r = mAttachInfo.mTmpInvalRect;
9210 int minLeft;
9211 int maxRight;
9212 if (offset < 0) {
9213 minLeft = mLeft + offset;
9214 maxRight = mRight;
9215 } else {
9216 minLeft = mLeft;
9217 maxRight = mRight + offset;
9218 }
9219 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9220 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009221 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009222 }
9223 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009224 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009225 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009226
Chet Haasec3aa3612010-06-17 08:50:37 -07009227 mLeft += offset;
9228 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009229 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009230 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009231 invalidateViewProperty(false, false);
9232 } else {
9233 if (!matrixIsIdentity) {
9234 invalidateViewProperty(false, true);
9235 }
9236 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009237 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 }
9240
9241 /**
9242 * Get the LayoutParams associated with this view. All views should have
9243 * layout parameters. These supply parameters to the <i>parent</i> of this
9244 * view specifying how it should be arranged. There are many subclasses of
9245 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9246 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009247 *
9248 * This method may return null if this View is not attached to a parent
9249 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9250 * was not invoked successfully. When a View is attached to a parent
9251 * ViewGroup, this method must not return null.
9252 *
9253 * @return The LayoutParams associated with this view, or null if no
9254 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009256 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 public ViewGroup.LayoutParams getLayoutParams() {
9258 return mLayoutParams;
9259 }
9260
9261 /**
9262 * Set the layout parameters associated with this view. These supply
9263 * parameters to the <i>parent</i> of this view specifying how it should be
9264 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9265 * correspond to the different subclasses of ViewGroup that are responsible
9266 * for arranging their children.
9267 *
Romain Guy01c174b2011-02-22 11:51:06 -08009268 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009269 */
9270 public void setLayoutParams(ViewGroup.LayoutParams params) {
9271 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009272 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 }
9274 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009275 if (mParent instanceof ViewGroup) {
9276 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009278 requestLayout();
9279 }
9280
9281 /**
9282 * Set the scrolled position of your view. This will cause a call to
9283 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9284 * invalidated.
9285 * @param x the x position to scroll to
9286 * @param y the y position to scroll to
9287 */
9288 public void scrollTo(int x, int y) {
9289 if (mScrollX != x || mScrollY != y) {
9290 int oldX = mScrollX;
9291 int oldY = mScrollY;
9292 mScrollX = x;
9293 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009294 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009296 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009297 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 }
9300 }
9301
9302 /**
9303 * Move the scrolled position of your view. This will cause a call to
9304 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9305 * invalidated.
9306 * @param x the amount of pixels to scroll by horizontally
9307 * @param y the amount of pixels to scroll by vertically
9308 */
9309 public void scrollBy(int x, int y) {
9310 scrollTo(mScrollX + x, mScrollY + y);
9311 }
9312
9313 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009314 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9315 * animation to fade the scrollbars out after a default delay. If a subclass
9316 * provides animated scrolling, the start delay should equal the duration
9317 * of the scrolling animation.</p>
9318 *
9319 * <p>The animation starts only if at least one of the scrollbars is
9320 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9321 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9322 * this method returns true, and false otherwise. If the animation is
9323 * started, this method calls {@link #invalidate()}; in that case the
9324 * caller should not call {@link #invalidate()}.</p>
9325 *
9326 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009327 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009328 *
9329 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9330 * and {@link #scrollTo(int, int)}.</p>
9331 *
9332 * @return true if the animation is played, false otherwise
9333 *
9334 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009335 * @see #scrollBy(int, int)
9336 * @see #scrollTo(int, int)
9337 * @see #isHorizontalScrollBarEnabled()
9338 * @see #isVerticalScrollBarEnabled()
9339 * @see #setHorizontalScrollBarEnabled(boolean)
9340 * @see #setVerticalScrollBarEnabled(boolean)
9341 */
9342 protected boolean awakenScrollBars() {
9343 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009344 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009345 }
9346
9347 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009348 * Trigger the scrollbars to draw.
9349 * This method differs from awakenScrollBars() only in its default duration.
9350 * initialAwakenScrollBars() will show the scroll bars for longer than
9351 * usual to give the user more of a chance to notice them.
9352 *
9353 * @return true if the animation is played, false otherwise.
9354 */
9355 private boolean initialAwakenScrollBars() {
9356 return mScrollCache != null &&
9357 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9358 }
9359
9360 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009361 * <p>
9362 * Trigger the scrollbars to draw. When invoked this method starts an
9363 * animation to fade the scrollbars out after a fixed delay. If a subclass
9364 * provides animated scrolling, the start delay should equal the duration of
9365 * the scrolling animation.
9366 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009367 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009368 * <p>
9369 * The animation starts only if at least one of the scrollbars is enabled,
9370 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9371 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9372 * this method returns true, and false otherwise. If the animation is
9373 * started, this method calls {@link #invalidate()}; in that case the caller
9374 * should not call {@link #invalidate()}.
9375 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009376 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009377 * <p>
9378 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009379 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009380 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009381 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009382 * @param startDelay the delay, in milliseconds, after which the animation
9383 * should start; when the delay is 0, the animation starts
9384 * immediately
9385 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009386 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009387 * @see #scrollBy(int, int)
9388 * @see #scrollTo(int, int)
9389 * @see #isHorizontalScrollBarEnabled()
9390 * @see #isVerticalScrollBarEnabled()
9391 * @see #setHorizontalScrollBarEnabled(boolean)
9392 * @see #setVerticalScrollBarEnabled(boolean)
9393 */
9394 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009395 return awakenScrollBars(startDelay, true);
9396 }
Joe Malin32736f02011-01-19 16:14:20 -08009397
Mike Cleron290947b2009-09-29 18:34:32 -07009398 /**
9399 * <p>
9400 * Trigger the scrollbars to draw. When invoked this method starts an
9401 * animation to fade the scrollbars out after a fixed delay. If a subclass
9402 * provides animated scrolling, the start delay should equal the duration of
9403 * the scrolling animation.
9404 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009405 *
Mike Cleron290947b2009-09-29 18:34:32 -07009406 * <p>
9407 * The animation starts only if at least one of the scrollbars is enabled,
9408 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9409 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9410 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009411 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009412 * is set to true; in that case the caller
9413 * should not call {@link #invalidate()}.
9414 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009415 *
Mike Cleron290947b2009-09-29 18:34:32 -07009416 * <p>
9417 * This method should be invoked everytime a subclass directly updates the
9418 * scroll parameters.
9419 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009420 *
Mike Cleron290947b2009-09-29 18:34:32 -07009421 * @param startDelay the delay, in milliseconds, after which the animation
9422 * should start; when the delay is 0, the animation starts
9423 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009424 *
Mike Cleron290947b2009-09-29 18:34:32 -07009425 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009426 *
Mike Cleron290947b2009-09-29 18:34:32 -07009427 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009428 *
Mike Cleron290947b2009-09-29 18:34:32 -07009429 * @see #scrollBy(int, int)
9430 * @see #scrollTo(int, int)
9431 * @see #isHorizontalScrollBarEnabled()
9432 * @see #isVerticalScrollBarEnabled()
9433 * @see #setHorizontalScrollBarEnabled(boolean)
9434 * @see #setVerticalScrollBarEnabled(boolean)
9435 */
9436 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009437 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009438
Mike Cleronf116bf82009-09-27 19:14:12 -07009439 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9440 return false;
9441 }
9442
9443 if (scrollCache.scrollBar == null) {
9444 scrollCache.scrollBar = new ScrollBarDrawable();
9445 }
9446
9447 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9448
Mike Cleron290947b2009-09-29 18:34:32 -07009449 if (invalidate) {
9450 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009451 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009452 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009453
9454 if (scrollCache.state == ScrollabilityCache.OFF) {
9455 // FIXME: this is copied from WindowManagerService.
9456 // We should get this value from the system when it
9457 // is possible to do so.
9458 final int KEY_REPEAT_FIRST_DELAY = 750;
9459 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9460 }
9461
9462 // Tell mScrollCache when we should start fading. This may
9463 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009464 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009465 scrollCache.fadeStartTime = fadeStartTime;
9466 scrollCache.state = ScrollabilityCache.ON;
9467
9468 // Schedule our fader to run, unscheduling any old ones first
9469 if (mAttachInfo != null) {
9470 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9471 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9472 }
9473
9474 return true;
9475 }
9476
9477 return false;
9478 }
9479
9480 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009481 * Do not invalidate views which are not visible and which are not running an animation. They
9482 * will not get drawn and they should not set dirty flags as if they will be drawn
9483 */
9484 private boolean skipInvalidate() {
9485 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9486 (!(mParent instanceof ViewGroup) ||
9487 !((ViewGroup) mParent).isViewTransitioning(this));
9488 }
9489 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009490 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009491 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9492 * in the future. This must be called from a UI thread. To call from a non-UI
9493 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 *
9495 * WARNING: This method is destructive to dirty.
9496 * @param dirty the rectangle representing the bounds of the dirty region
9497 */
9498 public void invalidate(Rect dirty) {
9499 if (ViewDebug.TRACE_HIERARCHY) {
9500 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9501 }
9502
Chet Haaseaceafe62011-08-26 15:44:33 -07009503 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009504 return;
9505 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009506 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009507 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9508 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009510 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009511 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 final ViewParent p = mParent;
9513 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009514 //noinspection PointlessBooleanExpression,ConstantConditions
9515 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9516 if (p != null && ai != null && ai.mHardwareAccelerated) {
9517 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009518 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009519 p.invalidateChild(this, null);
9520 return;
9521 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009523 if (p != null && ai != null) {
9524 final int scrollX = mScrollX;
9525 final int scrollY = mScrollY;
9526 final Rect r = ai.mTmpInvalRect;
9527 r.set(dirty.left - scrollX, dirty.top - scrollY,
9528 dirty.right - scrollX, dirty.bottom - scrollY);
9529 mParent.invalidateChild(this, r);
9530 }
9531 }
9532 }
9533
9534 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009535 * 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 -08009536 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009537 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9538 * will be called at some point in the future. This must be called from
9539 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 * @param l the left position of the dirty region
9541 * @param t the top position of the dirty region
9542 * @param r the right position of the dirty region
9543 * @param b the bottom position of the dirty region
9544 */
9545 public void invalidate(int l, int t, int r, int b) {
9546 if (ViewDebug.TRACE_HIERARCHY) {
9547 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9548 }
9549
Chet Haaseaceafe62011-08-26 15:44:33 -07009550 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009551 return;
9552 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009553 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009554 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9555 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009557 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009558 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 final ViewParent p = mParent;
9560 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009561 //noinspection PointlessBooleanExpression,ConstantConditions
9562 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9563 if (p != null && ai != null && ai.mHardwareAccelerated) {
9564 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009565 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009566 p.invalidateChild(this, null);
9567 return;
9568 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570 if (p != null && ai != null && l < r && t < b) {
9571 final int scrollX = mScrollX;
9572 final int scrollY = mScrollY;
9573 final Rect tmpr = ai.mTmpInvalRect;
9574 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9575 p.invalidateChild(this, tmpr);
9576 }
9577 }
9578 }
9579
9580 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009581 * Invalidate the whole view. If the view is visible,
9582 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9583 * the future. This must be called from a UI thread. To call from a non-UI thread,
9584 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 */
9586 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009587 invalidate(true);
9588 }
Joe Malin32736f02011-01-19 16:14:20 -08009589
Chet Haaseed032702010-10-01 14:05:54 -07009590 /**
9591 * This is where the invalidate() work actually happens. A full invalidate()
9592 * causes the drawing cache to be invalidated, but this function can be called with
9593 * invalidateCache set to false to skip that invalidation step for cases that do not
9594 * need it (for example, a component that remains at the same dimensions with the same
9595 * content).
9596 *
9597 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9598 * well. This is usually true for a full invalidate, but may be set to false if the
9599 * View's contents or dimensions have not changed.
9600 */
Romain Guy849d0a32011-02-01 17:20:48 -08009601 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 if (ViewDebug.TRACE_HIERARCHY) {
9603 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9604 }
9605
Chet Haaseaceafe62011-08-26 15:44:33 -07009606 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009607 return;
9608 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009609 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009610 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009611 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9612 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009613 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009614 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009615 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009616 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009617 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009620 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009621 //noinspection PointlessBooleanExpression,ConstantConditions
9622 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9623 if (p != null && ai != null && ai.mHardwareAccelerated) {
9624 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009625 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009626 p.invalidateChild(this, null);
9627 return;
9628 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009629 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631 if (p != null && ai != null) {
9632 final Rect r = ai.mTmpInvalRect;
9633 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9634 // Don't call invalidate -- we don't want to internally scroll
9635 // our own bounds
9636 p.invalidateChild(this, r);
9637 }
9638 }
9639 }
9640
9641 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009642 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9643 * set any flags or handle all of the cases handled by the default invalidation methods.
9644 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9645 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9646 * walk up the hierarchy, transforming the dirty rect as necessary.
9647 *
9648 * The method also handles normal invalidation logic if display list properties are not
9649 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9650 * backup approach, to handle these cases used in the various property-setting methods.
9651 *
9652 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9653 * are not being used in this view
9654 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9655 * list properties are not being used in this view
9656 */
9657 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009658 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009659 if (invalidateParent) {
9660 invalidateParentCaches();
9661 }
9662 if (forceRedraw) {
9663 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9664 }
9665 invalidate(false);
9666 } else {
9667 final AttachInfo ai = mAttachInfo;
9668 final ViewParent p = mParent;
9669 if (p != null && ai != null) {
9670 final Rect r = ai.mTmpInvalRect;
9671 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9672 if (mParent instanceof ViewGroup) {
9673 ((ViewGroup) mParent).invalidateChildFast(this, r);
9674 } else {
9675 mParent.invalidateChild(this, r);
9676 }
9677 }
9678 }
9679 }
9680
9681 /**
9682 * Utility method to transform a given Rect by the current matrix of this view.
9683 */
9684 void transformRect(final Rect rect) {
9685 if (!getMatrix().isIdentity()) {
9686 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9687 boundingRect.set(rect);
9688 getMatrix().mapRect(boundingRect);
9689 rect.set((int) (boundingRect.left - 0.5f),
9690 (int) (boundingRect.top - 0.5f),
9691 (int) (boundingRect.right + 0.5f),
9692 (int) (boundingRect.bottom + 0.5f));
9693 }
9694 }
9695
9696 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009697 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009698 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9699 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009700 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9701 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009702 *
9703 * @hide
9704 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009705 protected void invalidateParentCaches() {
9706 if (mParent instanceof View) {
9707 ((View) mParent).mPrivateFlags |= INVALIDATED;
9708 }
9709 }
Joe Malin32736f02011-01-19 16:14:20 -08009710
Romain Guy0fd89bf2011-01-26 15:41:30 -08009711 /**
9712 * Used to indicate that the parent of this view should be invalidated. This functionality
9713 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9714 * which is necessary when various parent-managed properties of the view change, such as
9715 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9716 * an invalidation event to the parent.
9717 *
9718 * @hide
9719 */
9720 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009721 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009722 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009723 }
9724 }
9725
9726 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009727 * Indicates whether this View is opaque. An opaque View guarantees that it will
9728 * draw all the pixels overlapping its bounds using a fully opaque color.
9729 *
9730 * Subclasses of View should override this method whenever possible to indicate
9731 * whether an instance is opaque. Opaque Views are treated in a special way by
9732 * the View hierarchy, possibly allowing it to perform optimizations during
9733 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009734 *
Romain Guy24443ea2009-05-11 11:56:30 -07009735 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009736 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009737 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009738 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009739 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009740 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9741 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009742 }
9743
Adam Powell20232d02010-12-08 21:08:53 -08009744 /**
9745 * @hide
9746 */
9747 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009748 // Opaque if:
9749 // - Has a background
9750 // - Background is opaque
9751 // - Doesn't have scrollbars or scrollbars are inside overlay
9752
Philip Milne6c8ea062012-04-03 17:38:43 -07009753 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009754 mPrivateFlags |= OPAQUE_BACKGROUND;
9755 } else {
9756 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9757 }
9758
9759 final int flags = mViewFlags;
9760 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9761 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9762 mPrivateFlags |= OPAQUE_SCROLLBARS;
9763 } else {
9764 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9765 }
9766 }
9767
9768 /**
9769 * @hide
9770 */
9771 protected boolean hasOpaqueScrollbars() {
9772 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009773 }
9774
9775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009776 * @return A handler associated with the thread running the View. This
9777 * handler can be used to pump events in the UI events queue.
9778 */
9779 public Handler getHandler() {
9780 if (mAttachInfo != null) {
9781 return mAttachInfo.mHandler;
9782 }
9783 return null;
9784 }
9785
9786 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009787 * Gets the view root associated with the View.
9788 * @return The view root, or null if none.
9789 * @hide
9790 */
9791 public ViewRootImpl getViewRootImpl() {
9792 if (mAttachInfo != null) {
9793 return mAttachInfo.mViewRootImpl;
9794 }
9795 return null;
9796 }
9797
9798 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009799 * <p>Causes the Runnable to be added to the message queue.
9800 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009801 *
Romain Guye63a4f32011-08-11 11:33:31 -07009802 * <p>This method can be invoked from outside of the UI thread
9803 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 *
9805 * @param action The Runnable that will be executed.
9806 *
9807 * @return Returns true if the Runnable was successfully placed in to the
9808 * message queue. Returns false on failure, usually because the
9809 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009810 *
9811 * @see #postDelayed
9812 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 */
9814 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009815 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009816 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009817 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009819 // Assume that post will succeed later
9820 ViewRootImpl.getRunQueue().post(action);
9821 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 }
9823
9824 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009825 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009826 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009827 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009828 *
Romain Guye63a4f32011-08-11 11:33:31 -07009829 * <p>This method can be invoked from outside of the UI thread
9830 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 *
9832 * @param action The Runnable that will be executed.
9833 * @param delayMillis The delay (in milliseconds) until the Runnable
9834 * will be executed.
9835 *
9836 * @return true if the Runnable was successfully placed in to the
9837 * message queue. Returns false on failure, usually because the
9838 * looper processing the message queue is exiting. Note that a
9839 * result of true does not mean the Runnable will be processed --
9840 * if the looper is quit before the delivery time of the message
9841 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009842 *
9843 * @see #post
9844 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 */
9846 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009847 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009848 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009849 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009851 // Assume that post will succeed later
9852 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9853 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009854 }
9855
9856 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009857 * <p>Causes the Runnable to execute on the next animation time step.
9858 * The runnable will be run on the user interface thread.</p>
9859 *
9860 * <p>This method can be invoked from outside of the UI thread
9861 * only when this View is attached to a window.</p>
9862 *
9863 * @param action The Runnable that will be executed.
9864 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009865 * @see #postOnAnimationDelayed
9866 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009867 */
9868 public void postOnAnimation(Runnable action) {
9869 final AttachInfo attachInfo = mAttachInfo;
9870 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009871 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9872 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009873 } else {
9874 // Assume that post will succeed later
9875 ViewRootImpl.getRunQueue().post(action);
9876 }
9877 }
9878
9879 /**
9880 * <p>Causes the Runnable to execute on the next animation time step,
9881 * after the specified amount of time elapses.
9882 * The runnable will be run on the user interface thread.</p>
9883 *
9884 * <p>This method can be invoked from outside of the UI thread
9885 * only when this View is attached to a window.</p>
9886 *
9887 * @param action The Runnable that will be executed.
9888 * @param delayMillis The delay (in milliseconds) until the Runnable
9889 * will be executed.
9890 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009891 * @see #postOnAnimation
9892 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009893 */
9894 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9895 final AttachInfo attachInfo = mAttachInfo;
9896 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009897 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9898 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009899 } else {
9900 // Assume that post will succeed later
9901 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9902 }
9903 }
9904
9905 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009906 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009907 *
Romain Guye63a4f32011-08-11 11:33:31 -07009908 * <p>This method can be invoked from outside of the UI thread
9909 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 *
9911 * @param action The Runnable to remove from the message handling queue
9912 *
9913 * @return true if this view could ask the Handler to remove the Runnable,
9914 * false otherwise. When the returned value is true, the Runnable
9915 * may or may not have been actually removed from the message queue
9916 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009917 *
9918 * @see #post
9919 * @see #postDelayed
9920 * @see #postOnAnimation
9921 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 */
9923 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009924 if (action != null) {
9925 final AttachInfo attachInfo = mAttachInfo;
9926 if (attachInfo != null) {
9927 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009928 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9929 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009930 } else {
9931 // Assume that post will succeed later
9932 ViewRootImpl.getRunQueue().removeCallbacks(action);
9933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 return true;
9936 }
9937
9938 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009939 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9940 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009941 *
Romain Guye63a4f32011-08-11 11:33:31 -07009942 * <p>This method can be invoked from outside of the UI thread
9943 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009946 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 */
9948 public void postInvalidate() {
9949 postInvalidateDelayed(0);
9950 }
9951
9952 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009953 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9954 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009955 *
Romain Guye63a4f32011-08-11 11:33:31 -07009956 * <p>This method can be invoked from outside of the UI thread
9957 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 *
9959 * @param left The left coordinate of the rectangle to invalidate.
9960 * @param top The top coordinate of the rectangle to invalidate.
9961 * @param right The right coordinate of the rectangle to invalidate.
9962 * @param bottom The bottom coordinate of the rectangle to invalidate.
9963 *
9964 * @see #invalidate(int, int, int, int)
9965 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009966 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 */
9968 public void postInvalidate(int left, int top, int right, int bottom) {
9969 postInvalidateDelayed(0, left, top, right, bottom);
9970 }
9971
9972 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009973 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9974 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009975 *
Romain Guye63a4f32011-08-11 11:33:31 -07009976 * <p>This method can be invoked from outside of the UI thread
9977 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009978 *
9979 * @param delayMilliseconds the duration in milliseconds to delay the
9980 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009981 *
9982 * @see #invalidate()
9983 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 */
9985 public void postInvalidateDelayed(long delayMilliseconds) {
9986 // We try only with the AttachInfo because there's no point in invalidating
9987 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009988 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009989 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009990 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991 }
9992 }
9993
9994 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009995 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9996 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009997 *
Romain Guye63a4f32011-08-11 11:33:31 -07009998 * <p>This method can be invoked from outside of the UI thread
9999 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 *
10001 * @param delayMilliseconds the duration in milliseconds to delay the
10002 * invalidation by
10003 * @param left The left coordinate of the rectangle to invalidate.
10004 * @param top The top coordinate of the rectangle to invalidate.
10005 * @param right The right coordinate of the rectangle to invalidate.
10006 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010007 *
10008 * @see #invalidate(int, int, int, int)
10009 * @see #invalidate(Rect)
10010 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 */
10012 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10013 int right, int bottom) {
10014
10015 // We try only with the AttachInfo because there's no point in invalidating
10016 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010017 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010018 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10020 info.target = this;
10021 info.left = left;
10022 info.top = top;
10023 info.right = right;
10024 info.bottom = bottom;
10025
Jeff Browna175a5b2012-02-15 19:18:31 -080010026 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 }
10028 }
10029
10030 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010031 * <p>Cause an invalidate to happen on the next animation time step, typically the
10032 * next display frame.</p>
10033 *
10034 * <p>This method can be invoked from outside of the UI thread
10035 * only when this View is attached to a window.</p>
10036 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010037 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010038 */
10039 public void postInvalidateOnAnimation() {
10040 // We try only with the AttachInfo because there's no point in invalidating
10041 // if we are not attached to our window
10042 final AttachInfo attachInfo = mAttachInfo;
10043 if (attachInfo != null) {
10044 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10045 }
10046 }
10047
10048 /**
10049 * <p>Cause an invalidate of the specified area to happen on the next animation
10050 * time step, typically the next display frame.</p>
10051 *
10052 * <p>This method can be invoked from outside of the UI thread
10053 * only when this View is attached to a window.</p>
10054 *
10055 * @param left The left coordinate of the rectangle to invalidate.
10056 * @param top The top coordinate of the rectangle to invalidate.
10057 * @param right The right coordinate of the rectangle to invalidate.
10058 * @param bottom The bottom coordinate of the rectangle to invalidate.
10059 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010060 * @see #invalidate(int, int, int, int)
10061 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010062 */
10063 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10064 // We try only with the AttachInfo because there's no point in invalidating
10065 // if we are not attached to our window
10066 final AttachInfo attachInfo = mAttachInfo;
10067 if (attachInfo != null) {
10068 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10069 info.target = this;
10070 info.left = left;
10071 info.top = top;
10072 info.right = right;
10073 info.bottom = bottom;
10074
10075 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10076 }
10077 }
10078
10079 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010080 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10081 * This event is sent at most once every
10082 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10083 */
10084 private void postSendViewScrolledAccessibilityEventCallback() {
10085 if (mSendViewScrolledAccessibilityEvent == null) {
10086 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10087 }
10088 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10089 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10090 postDelayed(mSendViewScrolledAccessibilityEvent,
10091 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10092 }
10093 }
10094
10095 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096 * Called by a parent to request that a child update its values for mScrollX
10097 * and mScrollY if necessary. This will typically be done if the child is
10098 * animating a scroll using a {@link android.widget.Scroller Scroller}
10099 * object.
10100 */
10101 public void computeScroll() {
10102 }
10103
10104 /**
10105 * <p>Indicate whether the horizontal edges are faded when the view is
10106 * scrolled horizontally.</p>
10107 *
10108 * @return true if the horizontal edges should are faded on scroll, false
10109 * otherwise
10110 *
10111 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010112 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010113 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 */
10115 public boolean isHorizontalFadingEdgeEnabled() {
10116 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10117 }
10118
10119 /**
10120 * <p>Define whether the horizontal edges should be faded when this view
10121 * is scrolled horizontally.</p>
10122 *
10123 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10124 * be faded when the view is scrolled
10125 * horizontally
10126 *
10127 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010128 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010129 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 */
10131 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10132 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10133 if (horizontalFadingEdgeEnabled) {
10134 initScrollCache();
10135 }
10136
10137 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10138 }
10139 }
10140
10141 /**
10142 * <p>Indicate whether the vertical edges are faded when the view is
10143 * scrolled horizontally.</p>
10144 *
10145 * @return true if the vertical edges should are faded on scroll, false
10146 * otherwise
10147 *
10148 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010149 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010150 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 */
10152 public boolean isVerticalFadingEdgeEnabled() {
10153 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10154 }
10155
10156 /**
10157 * <p>Define whether the vertical edges should be faded when this view
10158 * is scrolled vertically.</p>
10159 *
10160 * @param verticalFadingEdgeEnabled true if the vertical edges should
10161 * be faded when the view is scrolled
10162 * vertically
10163 *
10164 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010165 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010166 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 */
10168 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10169 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10170 if (verticalFadingEdgeEnabled) {
10171 initScrollCache();
10172 }
10173
10174 mViewFlags ^= FADING_EDGE_VERTICAL;
10175 }
10176 }
10177
10178 /**
10179 * Returns the strength, or intensity, of the top faded edge. The strength is
10180 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10181 * returns 0.0 or 1.0 but no value in between.
10182 *
10183 * Subclasses should override this method to provide a smoother fade transition
10184 * when scrolling occurs.
10185 *
10186 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10187 */
10188 protected float getTopFadingEdgeStrength() {
10189 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10190 }
10191
10192 /**
10193 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10194 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10195 * returns 0.0 or 1.0 but no value in between.
10196 *
10197 * Subclasses should override this method to provide a smoother fade transition
10198 * when scrolling occurs.
10199 *
10200 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10201 */
10202 protected float getBottomFadingEdgeStrength() {
10203 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10204 computeVerticalScrollRange() ? 1.0f : 0.0f;
10205 }
10206
10207 /**
10208 * Returns the strength, or intensity, of the left faded edge. The strength is
10209 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10210 * returns 0.0 or 1.0 but no value in between.
10211 *
10212 * Subclasses should override this method to provide a smoother fade transition
10213 * when scrolling occurs.
10214 *
10215 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10216 */
10217 protected float getLeftFadingEdgeStrength() {
10218 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10219 }
10220
10221 /**
10222 * Returns the strength, or intensity, of the right faded edge. The strength is
10223 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10224 * returns 0.0 or 1.0 but no value in between.
10225 *
10226 * Subclasses should override this method to provide a smoother fade transition
10227 * when scrolling occurs.
10228 *
10229 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10230 */
10231 protected float getRightFadingEdgeStrength() {
10232 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10233 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10234 }
10235
10236 /**
10237 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10238 * scrollbar is not drawn by default.</p>
10239 *
10240 * @return true if the horizontal scrollbar should be painted, false
10241 * otherwise
10242 *
10243 * @see #setHorizontalScrollBarEnabled(boolean)
10244 */
10245 public boolean isHorizontalScrollBarEnabled() {
10246 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10247 }
10248
10249 /**
10250 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10251 * scrollbar is not drawn by default.</p>
10252 *
10253 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10254 * be painted
10255 *
10256 * @see #isHorizontalScrollBarEnabled()
10257 */
10258 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10259 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10260 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010261 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010262 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 }
10264 }
10265
10266 /**
10267 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10268 * scrollbar is not drawn by default.</p>
10269 *
10270 * @return true if the vertical scrollbar should be painted, false
10271 * otherwise
10272 *
10273 * @see #setVerticalScrollBarEnabled(boolean)
10274 */
10275 public boolean isVerticalScrollBarEnabled() {
10276 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10277 }
10278
10279 /**
10280 * <p>Define whether the vertical scrollbar should be drawn or not. The
10281 * scrollbar is not drawn by default.</p>
10282 *
10283 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10284 * be painted
10285 *
10286 * @see #isVerticalScrollBarEnabled()
10287 */
10288 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10289 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10290 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010291 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010292 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010293 }
10294 }
10295
Adam Powell20232d02010-12-08 21:08:53 -080010296 /**
10297 * @hide
10298 */
10299 protected void recomputePadding() {
10300 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 }
Joe Malin32736f02011-01-19 16:14:20 -080010302
Mike Cleronfe81d382009-09-28 14:22:16 -070010303 /**
10304 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010305 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010306 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010307 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010308 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010309 */
10310 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10311 initScrollCache();
10312 final ScrollabilityCache scrollabilityCache = mScrollCache;
10313 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010314 if (fadeScrollbars) {
10315 scrollabilityCache.state = ScrollabilityCache.OFF;
10316 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010317 scrollabilityCache.state = ScrollabilityCache.ON;
10318 }
10319 }
Joe Malin32736f02011-01-19 16:14:20 -080010320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 /**
Joe Malin32736f02011-01-19 16:14:20 -080010322 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010323 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010324 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010325 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010326 *
10327 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010328 */
10329 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010330 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010331 }
Joe Malin32736f02011-01-19 16:14:20 -080010332
Mike Cleron52f0a642009-09-28 18:21:37 -070010333 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010334 *
10335 * Returns the delay before scrollbars fade.
10336 *
10337 * @return the delay before scrollbars fade
10338 *
10339 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10340 */
10341 public int getScrollBarDefaultDelayBeforeFade() {
10342 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10343 mScrollCache.scrollBarDefaultDelayBeforeFade;
10344 }
10345
10346 /**
10347 * Define the delay before scrollbars fade.
10348 *
10349 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10350 *
10351 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10352 */
10353 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10354 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10355 }
10356
10357 /**
10358 *
10359 * Returns the scrollbar fade duration.
10360 *
10361 * @return the scrollbar fade duration
10362 *
10363 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10364 */
10365 public int getScrollBarFadeDuration() {
10366 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10367 mScrollCache.scrollBarFadeDuration;
10368 }
10369
10370 /**
10371 * Define the scrollbar fade duration.
10372 *
10373 * @param scrollBarFadeDuration - the scrollbar fade duration
10374 *
10375 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10376 */
10377 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10378 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10379 }
10380
10381 /**
10382 *
10383 * Returns the scrollbar size.
10384 *
10385 * @return the scrollbar size
10386 *
10387 * @attr ref android.R.styleable#View_scrollbarSize
10388 */
10389 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010390 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010391 mScrollCache.scrollBarSize;
10392 }
10393
10394 /**
10395 * Define the scrollbar size.
10396 *
10397 * @param scrollBarSize - the scrollbar size
10398 *
10399 * @attr ref android.R.styleable#View_scrollbarSize
10400 */
10401 public void setScrollBarSize(int scrollBarSize) {
10402 getScrollCache().scrollBarSize = scrollBarSize;
10403 }
10404
10405 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010406 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10407 * inset. When inset, they add to the padding of the view. And the scrollbars
10408 * can be drawn inside the padding area or on the edge of the view. For example,
10409 * if a view has a background drawable and you want to draw the scrollbars
10410 * inside the padding specified by the drawable, you can use
10411 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10412 * appear at the edge of the view, ignoring the padding, then you can use
10413 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10414 * @param style the style of the scrollbars. Should be one of
10415 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10416 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10417 * @see #SCROLLBARS_INSIDE_OVERLAY
10418 * @see #SCROLLBARS_INSIDE_INSET
10419 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10420 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010421 *
10422 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 */
10424 public void setScrollBarStyle(int style) {
10425 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10426 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010427 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010428 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 }
10430 }
10431
10432 /**
10433 * <p>Returns the current scrollbar style.</p>
10434 * @return the current scrollbar style
10435 * @see #SCROLLBARS_INSIDE_OVERLAY
10436 * @see #SCROLLBARS_INSIDE_INSET
10437 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10438 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010439 *
10440 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010442 @ViewDebug.ExportedProperty(mapping = {
10443 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10444 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10445 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10446 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10447 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 public int getScrollBarStyle() {
10449 return mViewFlags & SCROLLBARS_STYLE_MASK;
10450 }
10451
10452 /**
10453 * <p>Compute the horizontal range that the horizontal scrollbar
10454 * represents.</p>
10455 *
10456 * <p>The range is expressed in arbitrary units that must be the same as the
10457 * units used by {@link #computeHorizontalScrollExtent()} and
10458 * {@link #computeHorizontalScrollOffset()}.</p>
10459 *
10460 * <p>The default range is the drawing width of this view.</p>
10461 *
10462 * @return the total horizontal range represented by the horizontal
10463 * scrollbar
10464 *
10465 * @see #computeHorizontalScrollExtent()
10466 * @see #computeHorizontalScrollOffset()
10467 * @see android.widget.ScrollBarDrawable
10468 */
10469 protected int computeHorizontalScrollRange() {
10470 return getWidth();
10471 }
10472
10473 /**
10474 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10475 * within the horizontal range. This value is used to compute the position
10476 * of the thumb within the scrollbar's track.</p>
10477 *
10478 * <p>The range is expressed in arbitrary units that must be the same as the
10479 * units used by {@link #computeHorizontalScrollRange()} and
10480 * {@link #computeHorizontalScrollExtent()}.</p>
10481 *
10482 * <p>The default offset is the scroll offset of this view.</p>
10483 *
10484 * @return the horizontal offset of the scrollbar's thumb
10485 *
10486 * @see #computeHorizontalScrollRange()
10487 * @see #computeHorizontalScrollExtent()
10488 * @see android.widget.ScrollBarDrawable
10489 */
10490 protected int computeHorizontalScrollOffset() {
10491 return mScrollX;
10492 }
10493
10494 /**
10495 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10496 * within the horizontal range. This value is used to compute the length
10497 * of the thumb within the scrollbar's track.</p>
10498 *
10499 * <p>The range is expressed in arbitrary units that must be the same as the
10500 * units used by {@link #computeHorizontalScrollRange()} and
10501 * {@link #computeHorizontalScrollOffset()}.</p>
10502 *
10503 * <p>The default extent is the drawing width of this view.</p>
10504 *
10505 * @return the horizontal extent of the scrollbar's thumb
10506 *
10507 * @see #computeHorizontalScrollRange()
10508 * @see #computeHorizontalScrollOffset()
10509 * @see android.widget.ScrollBarDrawable
10510 */
10511 protected int computeHorizontalScrollExtent() {
10512 return getWidth();
10513 }
10514
10515 /**
10516 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10517 *
10518 * <p>The range is expressed in arbitrary units that must be the same as the
10519 * units used by {@link #computeVerticalScrollExtent()} and
10520 * {@link #computeVerticalScrollOffset()}.</p>
10521 *
10522 * @return the total vertical range represented by the vertical scrollbar
10523 *
10524 * <p>The default range is the drawing height of this view.</p>
10525 *
10526 * @see #computeVerticalScrollExtent()
10527 * @see #computeVerticalScrollOffset()
10528 * @see android.widget.ScrollBarDrawable
10529 */
10530 protected int computeVerticalScrollRange() {
10531 return getHeight();
10532 }
10533
10534 /**
10535 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10536 * within the horizontal range. This value is used to compute the position
10537 * of the thumb within the scrollbar's track.</p>
10538 *
10539 * <p>The range is expressed in arbitrary units that must be the same as the
10540 * units used by {@link #computeVerticalScrollRange()} and
10541 * {@link #computeVerticalScrollExtent()}.</p>
10542 *
10543 * <p>The default offset is the scroll offset of this view.</p>
10544 *
10545 * @return the vertical offset of the scrollbar's thumb
10546 *
10547 * @see #computeVerticalScrollRange()
10548 * @see #computeVerticalScrollExtent()
10549 * @see android.widget.ScrollBarDrawable
10550 */
10551 protected int computeVerticalScrollOffset() {
10552 return mScrollY;
10553 }
10554
10555 /**
10556 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10557 * within the vertical range. This value is used to compute the length
10558 * of the thumb within the scrollbar's track.</p>
10559 *
10560 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010561 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 * {@link #computeVerticalScrollOffset()}.</p>
10563 *
10564 * <p>The default extent is the drawing height of this view.</p>
10565 *
10566 * @return the vertical extent of the scrollbar's thumb
10567 *
10568 * @see #computeVerticalScrollRange()
10569 * @see #computeVerticalScrollOffset()
10570 * @see android.widget.ScrollBarDrawable
10571 */
10572 protected int computeVerticalScrollExtent() {
10573 return getHeight();
10574 }
10575
10576 /**
Adam Powell69159442011-06-13 17:53:06 -070010577 * Check if this view can be scrolled horizontally in a certain direction.
10578 *
10579 * @param direction Negative to check scrolling left, positive to check scrolling right.
10580 * @return true if this view can be scrolled in the specified direction, false otherwise.
10581 */
10582 public boolean canScrollHorizontally(int direction) {
10583 final int offset = computeHorizontalScrollOffset();
10584 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10585 if (range == 0) return false;
10586 if (direction < 0) {
10587 return offset > 0;
10588 } else {
10589 return offset < range - 1;
10590 }
10591 }
10592
10593 /**
10594 * Check if this view can be scrolled vertically in a certain direction.
10595 *
10596 * @param direction Negative to check scrolling up, positive to check scrolling down.
10597 * @return true if this view can be scrolled in the specified direction, false otherwise.
10598 */
10599 public boolean canScrollVertically(int direction) {
10600 final int offset = computeVerticalScrollOffset();
10601 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10602 if (range == 0) return false;
10603 if (direction < 0) {
10604 return offset > 0;
10605 } else {
10606 return offset < range - 1;
10607 }
10608 }
10609
10610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10612 * scrollbars are painted only if they have been awakened first.</p>
10613 *
10614 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010615 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010616 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010617 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010618 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 // scrollbars are drawn only when the animation is running
10620 final ScrollabilityCache cache = mScrollCache;
10621 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010622
Mike Cleronf116bf82009-09-27 19:14:12 -070010623 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010624
Mike Cleronf116bf82009-09-27 19:14:12 -070010625 if (state == ScrollabilityCache.OFF) {
10626 return;
10627 }
Joe Malin32736f02011-01-19 16:14:20 -080010628
Mike Cleronf116bf82009-09-27 19:14:12 -070010629 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010630
Mike Cleronf116bf82009-09-27 19:14:12 -070010631 if (state == ScrollabilityCache.FADING) {
10632 // We're fading -- get our fade interpolation
10633 if (cache.interpolatorValues == null) {
10634 cache.interpolatorValues = new float[1];
10635 }
Joe Malin32736f02011-01-19 16:14:20 -080010636
Mike Cleronf116bf82009-09-27 19:14:12 -070010637 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010638
Mike Cleronf116bf82009-09-27 19:14:12 -070010639 // Stops the animation if we're done
10640 if (cache.scrollBarInterpolator.timeToValues(values) ==
10641 Interpolator.Result.FREEZE_END) {
10642 cache.state = ScrollabilityCache.OFF;
10643 } else {
10644 cache.scrollBar.setAlpha(Math.round(values[0]));
10645 }
Joe Malin32736f02011-01-19 16:14:20 -080010646
10647 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010648 // drawing. We only want this when we're fading so that
10649 // we prevent excessive redraws
10650 invalidate = true;
10651 } else {
10652 // We're just on -- but we may have been fading before so
10653 // reset alpha
10654 cache.scrollBar.setAlpha(255);
10655 }
10656
Joe Malin32736f02011-01-19 16:14:20 -080010657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 final int viewFlags = mViewFlags;
10659
10660 final boolean drawHorizontalScrollBar =
10661 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10662 final boolean drawVerticalScrollBar =
10663 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10664 && !isVerticalScrollBarHidden();
10665
10666 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10667 final int width = mRight - mLeft;
10668 final int height = mBottom - mTop;
10669
10670 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671
Mike Reede8853fc2009-09-04 14:01:48 -040010672 final int scrollX = mScrollX;
10673 final int scrollY = mScrollY;
10674 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10675
Mike Cleronf116bf82009-09-27 19:14:12 -070010676 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010678 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010679 int size = scrollBar.getSize(false);
10680 if (size <= 0) {
10681 size = cache.scrollBarSize;
10682 }
10683
Mike Cleronf116bf82009-09-27 19:14:12 -070010684 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010685 computeHorizontalScrollOffset(),
10686 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010687 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010688 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010689 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010690 left = scrollX + (mPaddingLeft & inside);
10691 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10692 bottom = top + size;
10693 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10694 if (invalidate) {
10695 invalidate(left, top, right, bottom);
10696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697 }
10698
10699 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010700 int size = scrollBar.getSize(true);
10701 if (size <= 0) {
10702 size = cache.scrollBarSize;
10703 }
10704
Mike Reede8853fc2009-09-04 14:01:48 -040010705 scrollBar.setParameters(computeVerticalScrollRange(),
10706 computeVerticalScrollOffset(),
10707 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010708 switch (mVerticalScrollbarPosition) {
10709 default:
10710 case SCROLLBAR_POSITION_DEFAULT:
10711 case SCROLLBAR_POSITION_RIGHT:
10712 left = scrollX + width - size - (mUserPaddingRight & inside);
10713 break;
10714 case SCROLLBAR_POSITION_LEFT:
10715 left = scrollX + (mUserPaddingLeft & inside);
10716 break;
10717 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010718 top = scrollY + (mPaddingTop & inside);
10719 right = left + size;
10720 bottom = scrollY + height - (mUserPaddingBottom & inside);
10721 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10722 if (invalidate) {
10723 invalidate(left, top, right, bottom);
10724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725 }
10726 }
10727 }
10728 }
Romain Guy8506ab42009-06-11 17:35:47 -070010729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010731 * 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 -080010732 * FastScroller is visible.
10733 * @return whether to temporarily hide the vertical scrollbar
10734 * @hide
10735 */
10736 protected boolean isVerticalScrollBarHidden() {
10737 return false;
10738 }
10739
10740 /**
10741 * <p>Draw the horizontal scrollbar if
10742 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10743 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 * @param canvas the canvas on which to draw the scrollbar
10745 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 *
10747 * @see #isHorizontalScrollBarEnabled()
10748 * @see #computeHorizontalScrollRange()
10749 * @see #computeHorizontalScrollExtent()
10750 * @see #computeHorizontalScrollOffset()
10751 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010752 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010753 */
Romain Guy8fb95422010-08-17 18:38:51 -070010754 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10755 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010756 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010757 scrollBar.draw(canvas);
10758 }
Mike Reede8853fc2009-09-04 14:01:48 -040010759
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10762 * returns true.</p>
10763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010764 * @param canvas the canvas on which to draw the scrollbar
10765 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 *
10767 * @see #isVerticalScrollBarEnabled()
10768 * @see #computeVerticalScrollRange()
10769 * @see #computeVerticalScrollExtent()
10770 * @see #computeVerticalScrollOffset()
10771 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010772 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 */
Romain Guy8fb95422010-08-17 18:38:51 -070010774 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10775 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010776 scrollBar.setBounds(l, t, r, b);
10777 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 }
10779
10780 /**
10781 * Implement this to do your drawing.
10782 *
10783 * @param canvas the canvas on which the background will be drawn
10784 */
10785 protected void onDraw(Canvas canvas) {
10786 }
10787
10788 /*
10789 * Caller is responsible for calling requestLayout if necessary.
10790 * (This allows addViewInLayout to not request a new layout.)
10791 */
10792 void assignParent(ViewParent parent) {
10793 if (mParent == null) {
10794 mParent = parent;
10795 } else if (parent == null) {
10796 mParent = null;
10797 } else {
10798 throw new RuntimeException("view " + this + " being added, but"
10799 + " it already has a parent");
10800 }
10801 }
10802
10803 /**
10804 * This is called when the view is attached to a window. At this point it
10805 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010806 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10807 * however it may be called any time before the first onDraw -- including
10808 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 *
10810 * @see #onDetachedFromWindow()
10811 */
10812 protected void onAttachedToWindow() {
10813 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10814 mParent.requestTransparentRegion(this);
10815 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010816 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10817 initialAwakenScrollBars();
10818 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10819 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010820 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010821 // Order is important here: LayoutDirection MUST be resolved before Padding
10822 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010823 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010824 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010825 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010826 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010827 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010828 if (isFocused()) {
10829 InputMethodManager imm = InputMethodManager.peekInstance();
10830 imm.focusIn(this);
10831 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010832 }
Cibu Johny86666632010-02-22 13:01:02 -080010833
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010834 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010835 * @see #onScreenStateChanged(int)
10836 */
10837 void dispatchScreenStateChanged(int screenState) {
10838 onScreenStateChanged(screenState);
10839 }
10840
10841 /**
10842 * This method is called whenever the state of the screen this view is
10843 * attached to changes. A state change will usually occurs when the screen
10844 * turns on or off (whether it happens automatically or the user does it
10845 * manually.)
10846 *
10847 * @param screenState The new state of the screen. Can be either
10848 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10849 */
10850 public void onScreenStateChanged(int screenState) {
10851 }
10852
10853 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010854 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10855 */
10856 private boolean hasRtlSupport() {
10857 return mContext.getApplicationInfo().hasRtlSupport();
10858 }
10859
10860 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010861 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10862 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010863 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010864 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010865 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010866 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010867 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010868
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010869 if (hasRtlSupport()) {
10870 // Set resolved depending on layout direction
10871 switch (getLayoutDirection()) {
10872 case LAYOUT_DIRECTION_INHERIT:
10873 // If this is root view, no need to look at parent's layout dir.
10874 if (canResolveLayoutDirection()) {
10875 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010876
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010877 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10878 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10879 }
10880 } else {
10881 // Nothing to do, LTR by default
10882 }
10883 break;
10884 case LAYOUT_DIRECTION_RTL:
10885 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10886 break;
10887 case LAYOUT_DIRECTION_LOCALE:
10888 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010889 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10890 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010891 break;
10892 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010893 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010894 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010895 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010896
10897 // Set to resolved
10898 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010899 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010900 // Resolve padding
10901 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010902 }
10903
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010904 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010905 * Called when layout direction has been resolved.
10906 *
10907 * The default implementation does nothing.
10908 */
10909 public void onResolvedLayoutDirectionChanged() {
10910 }
10911
10912 /**
10913 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010914 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010915 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010916 // If the user specified the absolute padding (either with android:padding or
10917 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10918 // use the default padding or the padding from the background drawable
10919 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010920 int resolvedLayoutDirection = getResolvedLayoutDirection();
10921 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010922 case LAYOUT_DIRECTION_RTL:
10923 // Start user padding override Right user padding. Otherwise, if Right user
10924 // padding is not defined, use the default Right padding. If Right user padding
10925 // is defined, just use it.
10926 if (mUserPaddingStart >= 0) {
10927 mUserPaddingRight = mUserPaddingStart;
10928 } else if (mUserPaddingRight < 0) {
10929 mUserPaddingRight = mPaddingRight;
10930 }
10931 if (mUserPaddingEnd >= 0) {
10932 mUserPaddingLeft = mUserPaddingEnd;
10933 } else if (mUserPaddingLeft < 0) {
10934 mUserPaddingLeft = mPaddingLeft;
10935 }
10936 break;
10937 case LAYOUT_DIRECTION_LTR:
10938 default:
10939 // Start user padding override Left user padding. Otherwise, if Left user
10940 // padding is not defined, use the default left padding. If Left user padding
10941 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010942 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010943 mUserPaddingLeft = mUserPaddingStart;
10944 } else if (mUserPaddingLeft < 0) {
10945 mUserPaddingLeft = mPaddingLeft;
10946 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010947 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010948 mUserPaddingRight = mUserPaddingEnd;
10949 } else if (mUserPaddingRight < 0) {
10950 mUserPaddingRight = mPaddingRight;
10951 }
10952 }
10953
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010954 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10955
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010956 if(isPaddingRelative()) {
10957 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10958 } else {
10959 recomputePadding();
10960 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010961 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010962 }
10963
10964 /**
10965 * Resolve padding depending on the layout direction. Subclasses that care about
10966 * padding resolution should override this method. The default implementation does
10967 * nothing.
10968 *
10969 * @param layoutDirection the direction of the layout
10970 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010971 * @see {@link #LAYOUT_DIRECTION_LTR}
10972 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010973 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010974 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010975 }
10976
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010977 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010978 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010979 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010980 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010981 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010982 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010983 switch (getLayoutDirection()) {
10984 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010985 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010986 default:
10987 return true;
10988 }
10989 }
10990
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010991 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010992 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10993 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010994 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010995 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010996 // Reset the current resolved bits
10997 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010998 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010999 // Reset also the text direction
11000 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011001 }
11002
11003 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011004 * Called during reset of resolved layout direction.
11005 *
11006 * Subclasses need to override this method to clear cached information that depends on the
11007 * resolved layout direction, or to inform child views that inherit their layout direction.
11008 *
11009 * The default implementation does nothing.
11010 */
11011 public void onResolvedLayoutDirectionReset() {
11012 }
11013
11014 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011015 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011016 *
11017 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011018 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011019 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011020 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011021 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 }
11023
11024 /**
11025 * This is called when the view is detached from a window. At this point it
11026 * no longer has a surface for drawing.
11027 *
11028 * @see #onAttachedToWindow()
11029 */
11030 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011031 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011032
Romain Guya440b002010-02-24 15:57:54 -080011033 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011034 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011035 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011036 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011038 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011039
Romain Guya998dff2012-03-23 18:58:36 -070011040 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011041
11042 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011043 if (mDisplayList != null) {
11044 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11045 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011046 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011047 } else {
11048 if (mDisplayList != null) {
11049 // Should never happen
11050 mDisplayList.invalidate();
11051 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011052 }
11053
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011054 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011055
11056 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011057 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011058 resetAccessibilityStateChanged();
11059 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 }
11061
11062 /**
11063 * @return The number of times this view has been attached to a window
11064 */
11065 protected int getWindowAttachCount() {
11066 return mWindowAttachCount;
11067 }
11068
11069 /**
11070 * Retrieve a unique token identifying the window this view is attached to.
11071 * @return Return the window's token for use in
11072 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11073 */
11074 public IBinder getWindowToken() {
11075 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11076 }
11077
11078 /**
11079 * Retrieve a unique token identifying the top-level "real" window of
11080 * the window that this view is attached to. That is, this is like
11081 * {@link #getWindowToken}, except if the window this view in is a panel
11082 * window (attached to another containing window), then the token of
11083 * the containing window is returned instead.
11084 *
11085 * @return Returns the associated window token, either
11086 * {@link #getWindowToken()} or the containing window's token.
11087 */
11088 public IBinder getApplicationWindowToken() {
11089 AttachInfo ai = mAttachInfo;
11090 if (ai != null) {
11091 IBinder appWindowToken = ai.mPanelParentWindowToken;
11092 if (appWindowToken == null) {
11093 appWindowToken = ai.mWindowToken;
11094 }
11095 return appWindowToken;
11096 }
11097 return null;
11098 }
11099
11100 /**
11101 * Retrieve private session object this view hierarchy is using to
11102 * communicate with the window manager.
11103 * @return the session object to communicate with the window manager
11104 */
11105 /*package*/ IWindowSession getWindowSession() {
11106 return mAttachInfo != null ? mAttachInfo.mSession : null;
11107 }
11108
11109 /**
11110 * @param info the {@link android.view.View.AttachInfo} to associated with
11111 * this view
11112 */
11113 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11114 //System.out.println("Attached! " + this);
11115 mAttachInfo = info;
11116 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011117 // We will need to evaluate the drawable state at least once.
11118 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 if (mFloatingTreeObserver != null) {
11120 info.mTreeObserver.merge(mFloatingTreeObserver);
11121 mFloatingTreeObserver = null;
11122 }
11123 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11124 mAttachInfo.mScrollContainers.add(this);
11125 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11126 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011127 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011129
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011130 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011131 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011132 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011133 if (listeners != null && listeners.size() > 0) {
11134 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11135 // perform the dispatching. The iterator is a safe guard against listeners that
11136 // could mutate the list by calling the various add/remove methods. This prevents
11137 // the array from being modified while we iterate it.
11138 for (OnAttachStateChangeListener listener : listeners) {
11139 listener.onViewAttachedToWindow(this);
11140 }
11141 }
11142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 int vis = info.mWindowVisibility;
11144 if (vis != GONE) {
11145 onWindowVisibilityChanged(vis);
11146 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011147 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11148 // If nobody has evaluated the drawable state yet, then do it now.
11149 refreshDrawableState();
11150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 }
11152
11153 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011154 AttachInfo info = mAttachInfo;
11155 if (info != null) {
11156 int vis = info.mWindowVisibility;
11157 if (vis != GONE) {
11158 onWindowVisibilityChanged(GONE);
11159 }
11160 }
11161
11162 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011163
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011164 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011165 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011166 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011167 if (listeners != null && listeners.size() > 0) {
11168 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11169 // perform the dispatching. The iterator is a safe guard against listeners that
11170 // could mutate the list by calling the various add/remove methods. This prevents
11171 // the array from being modified while we iterate it.
11172 for (OnAttachStateChangeListener listener : listeners) {
11173 listener.onViewDetachedFromWindow(this);
11174 }
11175 }
11176
Romain Guy01d5edc2011-01-28 11:28:53 -080011177 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 mAttachInfo.mScrollContainers.remove(this);
11179 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11180 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011182 mAttachInfo = null;
11183 }
11184
11185 /**
11186 * Store this view hierarchy's frozen state into the given container.
11187 *
11188 * @param container The SparseArray in which to save the view's state.
11189 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011190 * @see #restoreHierarchyState(android.util.SparseArray)
11191 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11192 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 */
11194 public void saveHierarchyState(SparseArray<Parcelable> container) {
11195 dispatchSaveInstanceState(container);
11196 }
11197
11198 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011199 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11200 * this view and its children. May be overridden to modify how freezing happens to a
11201 * 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 -080011202 *
11203 * @param container The SparseArray in which to save the view's state.
11204 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011205 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11206 * @see #saveHierarchyState(android.util.SparseArray)
11207 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011208 */
11209 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11210 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11211 mPrivateFlags &= ~SAVE_STATE_CALLED;
11212 Parcelable state = onSaveInstanceState();
11213 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11214 throw new IllegalStateException(
11215 "Derived class did not call super.onSaveInstanceState()");
11216 }
11217 if (state != null) {
11218 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11219 // + ": " + state);
11220 container.put(mID, state);
11221 }
11222 }
11223 }
11224
11225 /**
11226 * Hook allowing a view to generate a representation of its internal state
11227 * that can later be used to create a new instance with that same state.
11228 * This state should only contain information that is not persistent or can
11229 * not be reconstructed later. For example, you will never store your
11230 * current position on screen because that will be computed again when a
11231 * new instance of the view is placed in its view hierarchy.
11232 * <p>
11233 * Some examples of things you may store here: the current cursor position
11234 * in a text view (but usually not the text itself since that is stored in a
11235 * content provider or other persistent storage), the currently selected
11236 * item in a list view.
11237 *
11238 * @return Returns a Parcelable object containing the view's current dynamic
11239 * state, or null if there is nothing interesting to save. The
11240 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011241 * @see #onRestoreInstanceState(android.os.Parcelable)
11242 * @see #saveHierarchyState(android.util.SparseArray)
11243 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011244 * @see #setSaveEnabled(boolean)
11245 */
11246 protected Parcelable onSaveInstanceState() {
11247 mPrivateFlags |= SAVE_STATE_CALLED;
11248 return BaseSavedState.EMPTY_STATE;
11249 }
11250
11251 /**
11252 * Restore this view hierarchy's frozen state from the given container.
11253 *
11254 * @param container The SparseArray which holds previously frozen states.
11255 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011256 * @see #saveHierarchyState(android.util.SparseArray)
11257 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11258 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 */
11260 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11261 dispatchRestoreInstanceState(container);
11262 }
11263
11264 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011265 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11266 * state for this view and its children. May be overridden to modify how restoring
11267 * happens to a view's children; for example, some views may want to not store state
11268 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 *
11270 * @param container The SparseArray which holds previously saved state.
11271 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011272 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11273 * @see #restoreHierarchyState(android.util.SparseArray)
11274 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011275 */
11276 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11277 if (mID != NO_ID) {
11278 Parcelable state = container.get(mID);
11279 if (state != null) {
11280 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11281 // + ": " + state);
11282 mPrivateFlags &= ~SAVE_STATE_CALLED;
11283 onRestoreInstanceState(state);
11284 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11285 throw new IllegalStateException(
11286 "Derived class did not call super.onRestoreInstanceState()");
11287 }
11288 }
11289 }
11290 }
11291
11292 /**
11293 * Hook allowing a view to re-apply a representation of its internal state that had previously
11294 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11295 * null state.
11296 *
11297 * @param state The frozen state that had previously been returned by
11298 * {@link #onSaveInstanceState}.
11299 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011300 * @see #onSaveInstanceState()
11301 * @see #restoreHierarchyState(android.util.SparseArray)
11302 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 */
11304 protected void onRestoreInstanceState(Parcelable state) {
11305 mPrivateFlags |= SAVE_STATE_CALLED;
11306 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011307 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11308 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011309 + "when two views of different type have the same id in the same hierarchy. "
11310 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011311 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011312 }
11313 }
11314
11315 /**
11316 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11317 *
11318 * @return the drawing start time in milliseconds
11319 */
11320 public long getDrawingTime() {
11321 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11322 }
11323
11324 /**
11325 * <p>Enables or disables the duplication of the parent's state into this view. When
11326 * duplication is enabled, this view gets its drawable state from its parent rather
11327 * than from its own internal properties.</p>
11328 *
11329 * <p>Note: in the current implementation, setting this property to true after the
11330 * view was added to a ViewGroup might have no effect at all. This property should
11331 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11332 *
11333 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11334 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011335 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011336 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11337 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011338 *
11339 * @param enabled True to enable duplication of the parent's drawable state, false
11340 * to disable it.
11341 *
11342 * @see #getDrawableState()
11343 * @see #isDuplicateParentStateEnabled()
11344 */
11345 public void setDuplicateParentStateEnabled(boolean enabled) {
11346 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11347 }
11348
11349 /**
11350 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11351 *
11352 * @return True if this view's drawable state is duplicated from the parent,
11353 * false otherwise
11354 *
11355 * @see #getDrawableState()
11356 * @see #setDuplicateParentStateEnabled(boolean)
11357 */
11358 public boolean isDuplicateParentStateEnabled() {
11359 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11360 }
11361
11362 /**
Romain Guy171c5922011-01-06 10:04:23 -080011363 * <p>Specifies the type of layer backing this view. The layer can be
11364 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11365 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011366 *
Romain Guy171c5922011-01-06 10:04:23 -080011367 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11368 * instance that controls how the layer is composed on screen. The following
11369 * properties of the paint are taken into account when composing the layer:</p>
11370 * <ul>
11371 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11372 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11373 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11374 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011375 *
Romain Guy171c5922011-01-06 10:04:23 -080011376 * <p>If this view has an alpha value set to < 1.0 by calling
11377 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11378 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11379 * equivalent to setting a hardware layer on this view and providing a paint with
11380 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011381 *
Romain Guy171c5922011-01-06 10:04:23 -080011382 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11383 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11384 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011385 *
Romain Guy171c5922011-01-06 10:04:23 -080011386 * @param layerType The ype of layer to use with this view, must be one of
11387 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11388 * {@link #LAYER_TYPE_HARDWARE}
11389 * @param paint The paint used to compose the layer. This argument is optional
11390 * and can be null. It is ignored when the layer type is
11391 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011392 *
11393 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011394 * @see #LAYER_TYPE_NONE
11395 * @see #LAYER_TYPE_SOFTWARE
11396 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011397 * @see #setAlpha(float)
11398 *
Romain Guy171c5922011-01-06 10:04:23 -080011399 * @attr ref android.R.styleable#View_layerType
11400 */
11401 public void setLayerType(int layerType, Paint paint) {
11402 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011403 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011404 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11405 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011406
Romain Guyd6cd5722011-01-17 14:42:41 -080011407 if (layerType == mLayerType) {
11408 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11409 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011410 invalidateParentCaches();
11411 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011412 }
11413 return;
11414 }
Romain Guy171c5922011-01-06 10:04:23 -080011415
11416 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011417 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011418 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011419 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011420 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011421 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011422 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011423 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011424 default:
11425 break;
Romain Guy171c5922011-01-06 10:04:23 -080011426 }
11427
11428 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011429 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11430 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11431 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011432
Romain Guy0fd89bf2011-01-26 15:41:30 -080011433 invalidateParentCaches();
11434 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011435 }
11436
11437 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011438 * Indicates whether this view has a static layer. A view with layer type
11439 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11440 * dynamic.
11441 */
11442 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011443 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011444 }
11445
11446 /**
Romain Guy171c5922011-01-06 10:04:23 -080011447 * Indicates what type of layer is currently associated with this view. By default
11448 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11449 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11450 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011451 *
Romain Guy171c5922011-01-06 10:04:23 -080011452 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11453 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011454 *
11455 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011456 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011457 * @see #LAYER_TYPE_NONE
11458 * @see #LAYER_TYPE_SOFTWARE
11459 * @see #LAYER_TYPE_HARDWARE
11460 */
11461 public int getLayerType() {
11462 return mLayerType;
11463 }
Joe Malin32736f02011-01-19 16:14:20 -080011464
Romain Guy6c319ca2011-01-11 14:29:25 -080011465 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011466 * Forces this view's layer to be created and this view to be rendered
11467 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11468 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011469 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011470 * This method can for instance be used to render a view into its layer before
11471 * starting an animation. If this view is complex, rendering into the layer
11472 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011473 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011474 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011475 *
11476 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011477 */
11478 public void buildLayer() {
11479 if (mLayerType == LAYER_TYPE_NONE) return;
11480
11481 if (mAttachInfo == null) {
11482 throw new IllegalStateException("This view must be attached to a window first");
11483 }
11484
11485 switch (mLayerType) {
11486 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011487 if (mAttachInfo.mHardwareRenderer != null &&
11488 mAttachInfo.mHardwareRenderer.isEnabled() &&
11489 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011490 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011491 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011492 break;
11493 case LAYER_TYPE_SOFTWARE:
11494 buildDrawingCache(true);
11495 break;
11496 }
11497 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011498
Romain Guy9c4b79a2011-11-10 19:23:58 -080011499 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11500 void flushLayer() {
11501 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11502 mHardwareLayer.flush();
11503 }
11504 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011505
11506 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011507 * <p>Returns a hardware layer that can be used to draw this view again
11508 * without executing its draw method.</p>
11509 *
11510 * @return A HardwareLayer ready to render, or null if an error occurred.
11511 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011512 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011513 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11514 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011515 return null;
11516 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011517
Romain Guy9c4b79a2011-11-10 19:23:58 -080011518 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011519
11520 final int width = mRight - mLeft;
11521 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011522
Romain Guy6c319ca2011-01-11 14:29:25 -080011523 if (width == 0 || height == 0) {
11524 return null;
11525 }
11526
11527 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11528 if (mHardwareLayer == null) {
11529 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11530 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011531 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011532 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11533 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011534 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011535 }
11536
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011537 // The layer is not valid if the underlying GPU resources cannot be allocated
11538 if (!mHardwareLayer.isValid()) {
11539 return null;
11540 }
11541
Chet Haasea1cff502012-02-21 13:43:44 -080011542 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011543 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011544 }
11545
11546 return mHardwareLayer;
11547 }
Romain Guy171c5922011-01-06 10:04:23 -080011548
Romain Guy589b0bb2011-10-10 13:57:47 -070011549 /**
11550 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011551 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011552 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011553 *
11554 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011555 * @see #LAYER_TYPE_HARDWARE
11556 */
Romain Guya998dff2012-03-23 18:58:36 -070011557 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011558 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011559 AttachInfo info = mAttachInfo;
11560 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011561 info.mHardwareRenderer.isEnabled() &&
11562 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011563 mHardwareLayer.destroy();
11564 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011565
Romain Guy9c4b79a2011-11-10 19:23:58 -080011566 invalidate(true);
11567 invalidateParentCaches();
11568 }
Romain Guy65b345f2011-07-27 18:51:50 -070011569 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011570 }
Romain Guy65b345f2011-07-27 18:51:50 -070011571 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011572 }
11573
Romain Guy171c5922011-01-06 10:04:23 -080011574 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011575 * Destroys all hardware rendering resources. This method is invoked
11576 * when the system needs to reclaim resources. Upon execution of this
11577 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011578 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011579 * Note: you <strong>must</strong> call
11580 * <code>super.destroyHardwareResources()</code> when overriding
11581 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011582 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011583 * @hide
11584 */
11585 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011586 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011587 }
11588
11589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011590 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11591 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11592 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11593 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11594 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11595 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011596 *
Romain Guy171c5922011-01-06 10:04:23 -080011597 * <p>Enabling the drawing cache is similar to
11598 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011599 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11600 * drawing cache has no effect on rendering because the system uses a different mechanism
11601 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11602 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11603 * for information on how to enable software and hardware layers.</p>
11604 *
11605 * <p>This API can be used to manually generate
11606 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11607 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011608 *
11609 * @param enabled true to enable the drawing cache, false otherwise
11610 *
11611 * @see #isDrawingCacheEnabled()
11612 * @see #getDrawingCache()
11613 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011614 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011615 */
11616 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011617 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11619 }
11620
11621 /**
11622 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11623 *
11624 * @return true if the drawing cache is enabled
11625 *
11626 * @see #setDrawingCacheEnabled(boolean)
11627 * @see #getDrawingCache()
11628 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011629 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011630 public boolean isDrawingCacheEnabled() {
11631 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11632 }
11633
11634 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011635 * Debugging utility which recursively outputs the dirty state of a view and its
11636 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011637 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011638 * @hide
11639 */
Romain Guy676b1732011-02-14 14:45:33 -080011640 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011641 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11642 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11643 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11644 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11645 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11646 if (clear) {
11647 mPrivateFlags &= clearMask;
11648 }
11649 if (this instanceof ViewGroup) {
11650 ViewGroup parent = (ViewGroup) this;
11651 final int count = parent.getChildCount();
11652 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011653 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011654 child.outputDirtyFlags(indent + " ", clear, clearMask);
11655 }
11656 }
11657 }
11658
11659 /**
11660 * This method is used by ViewGroup to cause its children to restore or recreate their
11661 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11662 * to recreate its own display list, which would happen if it went through the normal
11663 * draw/dispatchDraw mechanisms.
11664 *
11665 * @hide
11666 */
11667 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011668
11669 /**
11670 * A view that is not attached or hardware accelerated cannot create a display list.
11671 * This method checks these conditions and returns the appropriate result.
11672 *
11673 * @return true if view has the ability to create a display list, false otherwise.
11674 *
11675 * @hide
11676 */
11677 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011678 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011679 }
Joe Malin32736f02011-01-19 16:14:20 -080011680
Chet Haasedaf98e92011-01-10 14:10:36 -080011681 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011682 * @return The HardwareRenderer associated with that view or null if hardware rendering
11683 * is not supported or this this has not been attached to a window.
11684 *
11685 * @hide
11686 */
11687 public HardwareRenderer getHardwareRenderer() {
11688 if (mAttachInfo != null) {
11689 return mAttachInfo.mHardwareRenderer;
11690 }
11691 return null;
11692 }
11693
11694 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011695 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11696 * Otherwise, the same display list will be returned (after having been rendered into
11697 * along the way, depending on the invalidation state of the view).
11698 *
11699 * @param displayList The previous version of this displayList, could be null.
11700 * @param isLayer Whether the requester of the display list is a layer. If so,
11701 * the view will avoid creating a layer inside the resulting display list.
11702 * @return A new or reused DisplayList object.
11703 */
11704 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11705 if (!canHaveDisplayList()) {
11706 return null;
11707 }
11708
11709 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11710 displayList == null || !displayList.isValid() ||
11711 (!isLayer && mRecreateDisplayList))) {
11712 // Don't need to recreate the display list, just need to tell our
11713 // children to restore/recreate theirs
11714 if (displayList != null && displayList.isValid() &&
11715 !isLayer && !mRecreateDisplayList) {
11716 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11717 mPrivateFlags &= ~DIRTY_MASK;
11718 dispatchGetDisplayList();
11719
11720 return displayList;
11721 }
11722
11723 if (!isLayer) {
11724 // If we got here, we're recreating it. Mark it as such to ensure that
11725 // we copy in child display lists into ours in drawChild()
11726 mRecreateDisplayList = true;
11727 }
11728 if (displayList == null) {
11729 final String name = getClass().getSimpleName();
11730 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11731 // If we're creating a new display list, make sure our parent gets invalidated
11732 // since they will need to recreate their display list to account for this
11733 // new child display list.
11734 invalidateParentCaches();
11735 }
11736
11737 boolean caching = false;
11738 final HardwareCanvas canvas = displayList.start();
11739 int restoreCount = 0;
11740 int width = mRight - mLeft;
11741 int height = mBottom - mTop;
11742
11743 try {
11744 canvas.setViewport(width, height);
11745 // The dirty rect should always be null for a display list
11746 canvas.onPreDraw(null);
11747 int layerType = (
11748 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11749 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070011750 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070011751 if (layerType == LAYER_TYPE_HARDWARE) {
11752 final HardwareLayer layer = getHardwareLayer();
11753 if (layer != null && layer.isValid()) {
11754 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11755 } else {
11756 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11757 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11758 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11759 }
11760 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011761 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011762 buildDrawingCache(true);
11763 Bitmap cache = getDrawingCache(true);
11764 if (cache != null) {
11765 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11766 caching = true;
11767 }
Chet Haasea1cff502012-02-21 13:43:44 -080011768 }
Chet Haasea1cff502012-02-21 13:43:44 -080011769 } else {
11770
11771 computeScroll();
11772
Chet Haasea1cff502012-02-21 13:43:44 -080011773 canvas.translate(-mScrollX, -mScrollY);
11774 if (!isLayer) {
11775 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11776 mPrivateFlags &= ~DIRTY_MASK;
11777 }
11778
11779 // Fast path for layouts with no backgrounds
11780 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11781 dispatchDraw(canvas);
11782 } else {
11783 draw(canvas);
11784 }
11785 }
11786 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080011787 canvas.onPostDraw();
11788
11789 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070011790 displayList.setCaching(caching);
11791 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080011792 displayList.setLeftTopRightBottom(0, 0, width, height);
11793 } else {
11794 setDisplayListProperties(displayList);
11795 }
11796 }
11797 } else if (!isLayer) {
11798 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11799 mPrivateFlags &= ~DIRTY_MASK;
11800 }
11801
11802 return displayList;
11803 }
11804
11805 /**
11806 * Get the DisplayList for the HardwareLayer
11807 *
11808 * @param layer The HardwareLayer whose DisplayList we want
11809 * @return A DisplayList fopr the specified HardwareLayer
11810 */
11811 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11812 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11813 layer.setDisplayList(displayList);
11814 return displayList;
11815 }
11816
11817
11818 /**
Romain Guyb051e892010-09-28 19:09:36 -070011819 * <p>Returns a display list that can be used to draw this view again
11820 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011821 *
Romain Guyb051e892010-09-28 19:09:36 -070011822 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011823 *
11824 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011825 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011826 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011827 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011828 return mDisplayList;
11829 }
11830
11831 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011832 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011833 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011834 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011835 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011836 * @see #getDrawingCache(boolean)
11837 */
11838 public Bitmap getDrawingCache() {
11839 return getDrawingCache(false);
11840 }
11841
11842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011843 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11844 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11845 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11846 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11847 * request the drawing cache by calling this method and draw it on screen if the
11848 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011849 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011850 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11851 * this method will create a bitmap of the same size as this view. Because this bitmap
11852 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11853 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11854 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11855 * size than the view. This implies that your application must be able to handle this
11856 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011857 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011858 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11859 * the current density of the screen when the application is in compatibility
11860 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011861 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011862 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011864 * @see #setDrawingCacheEnabled(boolean)
11865 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011866 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011867 * @see #destroyDrawingCache()
11868 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011869 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11871 return null;
11872 }
11873 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011874 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011875 }
Romain Guy02890fd2010-08-06 17:58:44 -070011876 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877 }
11878
11879 /**
11880 * <p>Frees the resources used by the drawing cache. If you call
11881 * {@link #buildDrawingCache()} manually without calling
11882 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11883 * should cleanup the cache with this method afterwards.</p>
11884 *
11885 * @see #setDrawingCacheEnabled(boolean)
11886 * @see #buildDrawingCache()
11887 * @see #getDrawingCache()
11888 */
11889 public void destroyDrawingCache() {
11890 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011891 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 mDrawingCache = null;
11893 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011894 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011895 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011896 mUnscaledDrawingCache = null;
11897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898 }
11899
11900 /**
11901 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011902 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011903 * view will always be drawn on top of a solid color.
11904 *
11905 * @param color The background color to use for the drawing cache's bitmap
11906 *
11907 * @see #setDrawingCacheEnabled(boolean)
11908 * @see #buildDrawingCache()
11909 * @see #getDrawingCache()
11910 */
11911 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011912 if (color != mDrawingCacheBackgroundColor) {
11913 mDrawingCacheBackgroundColor = color;
11914 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011916 }
11917
11918 /**
11919 * @see #setDrawingCacheBackgroundColor(int)
11920 *
11921 * @return The background color to used for the drawing cache's bitmap
11922 */
11923 public int getDrawingCacheBackgroundColor() {
11924 return mDrawingCacheBackgroundColor;
11925 }
11926
11927 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011928 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011929 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011930 * @see #buildDrawingCache(boolean)
11931 */
11932 public void buildDrawingCache() {
11933 buildDrawingCache(false);
11934 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011935
Romain Guyfbd8f692009-06-26 14:51:58 -070011936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11938 *
11939 * <p>If you call {@link #buildDrawingCache()} manually without calling
11940 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11941 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011942 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011943 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11944 * this method will create a bitmap of the same size as this view. Because this bitmap
11945 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11946 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11947 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11948 * size than the view. This implies that your application must be able to handle this
11949 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011950 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011951 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11952 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011953 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011954 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955 *
11956 * @see #getDrawingCache()
11957 * @see #destroyDrawingCache()
11958 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011959 public void buildDrawingCache(boolean autoScale) {
11960 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011961 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011962 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011963
11964 if (ViewDebug.TRACE_HIERARCHY) {
11965 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011967
Romain Guy8506ab42009-06-11 17:35:47 -070011968 int width = mRight - mLeft;
11969 int height = mBottom - mTop;
11970
11971 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011972 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011973
Romain Guye1123222009-06-29 14:24:56 -070011974 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011975 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11976 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978
11979 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011980 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011981 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011982
11983 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011984 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011985 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11987 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011988 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 return;
11990 }
11991
11992 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011993 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011994
11995 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 Bitmap.Config quality;
11997 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011998 // Never pick ARGB_4444 because it looks awful
11999 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12001 case DRAWING_CACHE_QUALITY_AUTO:
12002 quality = Bitmap.Config.ARGB_8888;
12003 break;
12004 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012005 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012006 break;
12007 case DRAWING_CACHE_QUALITY_HIGH:
12008 quality = Bitmap.Config.ARGB_8888;
12009 break;
12010 default:
12011 quality = Bitmap.Config.ARGB_8888;
12012 break;
12013 }
12014 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012015 // Optimization for translucent windows
12016 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012017 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 }
12019
12020 // Try to cleanup memory
12021 if (bitmap != null) bitmap.recycle();
12022
12023 try {
12024 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012025 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012026 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012027 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012028 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012029 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012030 }
Adam Powell26153a32010-11-08 15:22:27 -080012031 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 } catch (OutOfMemoryError e) {
12033 // If there is not enough memory to create the bitmap cache, just
12034 // ignore the issue as bitmap caches are not required to draw the
12035 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012036 if (autoScale) {
12037 mDrawingCache = null;
12038 } else {
12039 mUnscaledDrawingCache = null;
12040 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012041 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 return;
12043 }
12044
12045 clear = drawingCacheBackgroundColor != 0;
12046 }
12047
12048 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 if (attachInfo != null) {
12050 canvas = attachInfo.mCanvas;
12051 if (canvas == null) {
12052 canvas = new Canvas();
12053 }
12054 canvas.setBitmap(bitmap);
12055 // Temporarily clobber the cached Canvas in case one of our children
12056 // is also using a drawing cache. Without this, the children would
12057 // steal the canvas by attaching their own bitmap to it and bad, bad
12058 // thing would happen (invisible views, corrupted drawings, etc.)
12059 attachInfo.mCanvas = null;
12060 } else {
12061 // This case should hopefully never or seldom happen
12062 canvas = new Canvas(bitmap);
12063 }
12064
12065 if (clear) {
12066 bitmap.eraseColor(drawingCacheBackgroundColor);
12067 }
12068
12069 computeScroll();
12070 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012071
Romain Guye1123222009-06-29 14:24:56 -070012072 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012073 final float scale = attachInfo.mApplicationScale;
12074 canvas.scale(scale, scale);
12075 }
Joe Malin32736f02011-01-19 16:14:20 -080012076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012077 canvas.translate(-mScrollX, -mScrollY);
12078
Romain Guy5bcdff42009-05-14 21:27:18 -070012079 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012080 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12081 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012082 mPrivateFlags |= DRAWING_CACHE_VALID;
12083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012084
12085 // Fast path for layouts with no backgrounds
12086 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12087 if (ViewDebug.TRACE_HIERARCHY) {
12088 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12089 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012090 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 dispatchDraw(canvas);
12092 } else {
12093 draw(canvas);
12094 }
12095
12096 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012097 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012098
12099 if (attachInfo != null) {
12100 // Restore the cached Canvas for our siblings
12101 attachInfo.mCanvas = canvas;
12102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012103 }
12104 }
12105
12106 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012107 * Create a snapshot of the view into a bitmap. We should probably make
12108 * some form of this public, but should think about the API.
12109 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012110 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012111 int width = mRight - mLeft;
12112 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012113
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012114 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012115 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012116 width = (int) ((width * scale) + 0.5f);
12117 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012118
Romain Guy8c11e312009-09-14 15:15:30 -070012119 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012120 if (bitmap == null) {
12121 throw new OutOfMemoryError();
12122 }
12123
Romain Guyc529d8d2011-09-06 15:01:39 -070012124 Resources resources = getResources();
12125 if (resources != null) {
12126 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12127 }
Joe Malin32736f02011-01-19 16:14:20 -080012128
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012129 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012130 if (attachInfo != null) {
12131 canvas = attachInfo.mCanvas;
12132 if (canvas == null) {
12133 canvas = new Canvas();
12134 }
12135 canvas.setBitmap(bitmap);
12136 // Temporarily clobber the cached Canvas in case one of our children
12137 // is also using a drawing cache. Without this, the children would
12138 // steal the canvas by attaching their own bitmap to it and bad, bad
12139 // things would happen (invisible views, corrupted drawings, etc.)
12140 attachInfo.mCanvas = null;
12141 } else {
12142 // This case should hopefully never or seldom happen
12143 canvas = new Canvas(bitmap);
12144 }
12145
Romain Guy5bcdff42009-05-14 21:27:18 -070012146 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012147 bitmap.eraseColor(backgroundColor);
12148 }
12149
12150 computeScroll();
12151 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012152 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012153 canvas.translate(-mScrollX, -mScrollY);
12154
Romain Guy5bcdff42009-05-14 21:27:18 -070012155 // Temporarily remove the dirty mask
12156 int flags = mPrivateFlags;
12157 mPrivateFlags &= ~DIRTY_MASK;
12158
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012159 // Fast path for layouts with no backgrounds
12160 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12161 dispatchDraw(canvas);
12162 } else {
12163 draw(canvas);
12164 }
12165
Romain Guy5bcdff42009-05-14 21:27:18 -070012166 mPrivateFlags = flags;
12167
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012168 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012169 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012170
12171 if (attachInfo != null) {
12172 // Restore the cached Canvas for our siblings
12173 attachInfo.mCanvas = canvas;
12174 }
Romain Guy8506ab42009-06-11 17:35:47 -070012175
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012176 return bitmap;
12177 }
12178
12179 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180 * Indicates whether this View is currently in edit mode. A View is usually
12181 * in edit mode when displayed within a developer tool. For instance, if
12182 * this View is being drawn by a visual user interface builder, this method
12183 * should return true.
12184 *
12185 * Subclasses should check the return value of this method to provide
12186 * different behaviors if their normal behavior might interfere with the
12187 * host environment. For instance: the class spawns a thread in its
12188 * constructor, the drawing code relies on device-specific features, etc.
12189 *
12190 * This method is usually checked in the drawing code of custom widgets.
12191 *
12192 * @return True if this View is in edit mode, false otherwise.
12193 */
12194 public boolean isInEditMode() {
12195 return false;
12196 }
12197
12198 /**
12199 * If the View draws content inside its padding and enables fading edges,
12200 * it needs to support padding offsets. Padding offsets are added to the
12201 * fading edges to extend the length of the fade so that it covers pixels
12202 * drawn inside the padding.
12203 *
12204 * Subclasses of this class should override this method if they need
12205 * to draw content inside the padding.
12206 *
12207 * @return True if padding offset must be applied, false otherwise.
12208 *
12209 * @see #getLeftPaddingOffset()
12210 * @see #getRightPaddingOffset()
12211 * @see #getTopPaddingOffset()
12212 * @see #getBottomPaddingOffset()
12213 *
12214 * @since CURRENT
12215 */
12216 protected boolean isPaddingOffsetRequired() {
12217 return false;
12218 }
12219
12220 /**
12221 * Amount by which to extend the left fading region. Called only when
12222 * {@link #isPaddingOffsetRequired()} returns true.
12223 *
12224 * @return The left padding offset in pixels.
12225 *
12226 * @see #isPaddingOffsetRequired()
12227 *
12228 * @since CURRENT
12229 */
12230 protected int getLeftPaddingOffset() {
12231 return 0;
12232 }
12233
12234 /**
12235 * Amount by which to extend the right fading region. Called only when
12236 * {@link #isPaddingOffsetRequired()} returns true.
12237 *
12238 * @return The right padding offset in pixels.
12239 *
12240 * @see #isPaddingOffsetRequired()
12241 *
12242 * @since CURRENT
12243 */
12244 protected int getRightPaddingOffset() {
12245 return 0;
12246 }
12247
12248 /**
12249 * Amount by which to extend the top fading region. Called only when
12250 * {@link #isPaddingOffsetRequired()} returns true.
12251 *
12252 * @return The top padding offset in pixels.
12253 *
12254 * @see #isPaddingOffsetRequired()
12255 *
12256 * @since CURRENT
12257 */
12258 protected int getTopPaddingOffset() {
12259 return 0;
12260 }
12261
12262 /**
12263 * Amount by which to extend the bottom fading region. Called only when
12264 * {@link #isPaddingOffsetRequired()} returns true.
12265 *
12266 * @return The bottom padding offset in pixels.
12267 *
12268 * @see #isPaddingOffsetRequired()
12269 *
12270 * @since CURRENT
12271 */
12272 protected int getBottomPaddingOffset() {
12273 return 0;
12274 }
12275
12276 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012277 * @hide
12278 * @param offsetRequired
12279 */
12280 protected int getFadeTop(boolean offsetRequired) {
12281 int top = mPaddingTop;
12282 if (offsetRequired) top += getTopPaddingOffset();
12283 return top;
12284 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012285
Romain Guyf2fc4602011-07-19 15:20:03 -070012286 /**
12287 * @hide
12288 * @param offsetRequired
12289 */
12290 protected int getFadeHeight(boolean offsetRequired) {
12291 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012292 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012293 return mBottom - mTop - mPaddingBottom - padding;
12294 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012295
Romain Guyf2fc4602011-07-19 15:20:03 -070012296 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012297 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012298 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012299 *
Romain Guy2bffd262010-09-12 17:40:02 -070012300 * <p>Even if this method returns true, it does not mean that every call
12301 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12302 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012303 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012304 * window is hardware accelerated,
12305 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12306 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012307 *
Romain Guy2bffd262010-09-12 17:40:02 -070012308 * @return True if the view is attached to a window and the window is
12309 * hardware accelerated; false in any other case.
12310 */
12311 public boolean isHardwareAccelerated() {
12312 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12313 }
Joe Malin32736f02011-01-19 16:14:20 -080012314
Romain Guy2bffd262010-09-12 17:40:02 -070012315 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012316 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12317 * case of an active Animation being run on the view.
12318 */
12319 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12320 Animation a, boolean scalingRequired) {
12321 Transformation invalidationTransform;
12322 final int flags = parent.mGroupFlags;
12323 final boolean initialized = a.isInitialized();
12324 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012325 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012326 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12327 onAnimationStart();
12328 }
12329
12330 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12331 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12332 if (parent.mInvalidationTransformation == null) {
12333 parent.mInvalidationTransformation = new Transformation();
12334 }
12335 invalidationTransform = parent.mInvalidationTransformation;
12336 a.getTransformation(drawingTime, invalidationTransform, 1f);
12337 } else {
12338 invalidationTransform = parent.mChildTransformation;
12339 }
12340 if (more) {
12341 if (!a.willChangeBounds()) {
12342 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12343 parent.FLAG_OPTIMIZE_INVALIDATE) {
12344 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12345 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12346 // The child need to draw an animation, potentially offscreen, so
12347 // make sure we do not cancel invalidate requests
12348 parent.mPrivateFlags |= DRAW_ANIMATION;
12349 parent.invalidate(mLeft, mTop, mRight, mBottom);
12350 }
12351 } else {
12352 if (parent.mInvalidateRegion == null) {
12353 parent.mInvalidateRegion = new RectF();
12354 }
12355 final RectF region = parent.mInvalidateRegion;
12356 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12357 invalidationTransform);
12358
12359 // The child need to draw an animation, potentially offscreen, so
12360 // make sure we do not cancel invalidate requests
12361 parent.mPrivateFlags |= DRAW_ANIMATION;
12362
12363 final int left = mLeft + (int) region.left;
12364 final int top = mTop + (int) region.top;
12365 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12366 top + (int) (region.height() + .5f));
12367 }
12368 }
12369 return more;
12370 }
12371
Chet Haasea1cff502012-02-21 13:43:44 -080012372 void setDisplayListProperties() {
12373 setDisplayListProperties(mDisplayList);
12374 }
12375
12376 /**
12377 * This method is called by getDisplayList() when a display list is created or re-rendered.
12378 * It sets or resets the current value of all properties on that display list (resetting is
12379 * necessary when a display list is being re-created, because we need to make sure that
12380 * previously-set transform values
12381 */
12382 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012383 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012384 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012385 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012386 if (mParent instanceof ViewGroup) {
12387 displayList.setClipChildren(
12388 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12389 }
Chet Haase9420abd2012-03-29 16:28:32 -070012390 float alpha = 1;
12391 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12392 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12393 ViewGroup parentVG = (ViewGroup) mParent;
12394 final boolean hasTransform =
12395 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12396 if (hasTransform) {
12397 Transformation transform = parentVG.mChildTransformation;
12398 final int transformType = parentVG.mChildTransformation.getTransformationType();
12399 if (transformType != Transformation.TYPE_IDENTITY) {
12400 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12401 alpha = transform.getAlpha();
12402 }
12403 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12404 displayList.setStaticMatrix(transform.getMatrix());
12405 }
12406 }
12407 }
Chet Haasea1cff502012-02-21 13:43:44 -080012408 }
12409 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012410 alpha *= mTransformationInfo.mAlpha;
12411 if (alpha < 1) {
12412 final int multipliedAlpha = (int) (255 * alpha);
12413 if (onSetAlpha(multipliedAlpha)) {
12414 alpha = 1;
12415 }
12416 }
12417 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012418 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12419 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12420 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12421 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012422 if (mTransformationInfo.mCamera == null) {
12423 mTransformationInfo.mCamera = new Camera();
12424 mTransformationInfo.matrix3D = new Matrix();
12425 }
12426 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012427 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12428 displayList.setPivotX(getPivotX());
12429 displayList.setPivotY(getPivotY());
12430 }
Chet Haase9420abd2012-03-29 16:28:32 -070012431 } else if (alpha < 1) {
12432 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012433 }
12434 }
12435 }
12436
Chet Haasebcca79a2012-02-14 08:45:14 -080012437 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012438 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12439 * This draw() method is an implementation detail and is not intended to be overridden or
12440 * to be called from anywhere else other than ViewGroup.drawChild().
12441 */
12442 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012443 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012444 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012445 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012446 final int flags = parent.mGroupFlags;
12447
Chet Haasea1cff502012-02-21 13:43:44 -080012448 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012449 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012450 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012451 }
12452
12453 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012454 boolean concatMatrix = false;
12455
12456 boolean scalingRequired = false;
12457 boolean caching;
12458 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12459
12460 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012461 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12462 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012463 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012464 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012465 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12466 } else {
12467 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12468 }
12469
Chet Haasebcca79a2012-02-14 08:45:14 -080012470 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012471 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012472 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012473 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012474 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012475 } else if (!useDisplayListProperties &&
12476 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012477 final boolean hasTransform =
12478 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012479 if (hasTransform) {
12480 final int transformType = parent.mChildTransformation.getTransformationType();
12481 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12482 parent.mChildTransformation : null;
12483 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12484 }
12485 }
12486
12487 concatMatrix |= !childHasIdentityMatrix;
12488
12489 // Sets the flag as early as possible to allow draw() implementations
12490 // to call invalidate() successfully when doing animations
12491 mPrivateFlags |= DRAWN;
12492
Chet Haasebcca79a2012-02-14 08:45:14 -080012493 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012494 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12495 return more;
12496 }
12497
12498 if (hardwareAccelerated) {
12499 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12500 // retain the flag's value temporarily in the mRecreateDisplayList flag
12501 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12502 mPrivateFlags &= ~INVALIDATED;
12503 }
12504
12505 computeScroll();
12506
12507 final int sx = mScrollX;
12508 final int sy = mScrollY;
12509
12510 DisplayList displayList = null;
12511 Bitmap cache = null;
12512 boolean hasDisplayList = false;
12513 if (caching) {
12514 if (!hardwareAccelerated) {
12515 if (layerType != LAYER_TYPE_NONE) {
12516 layerType = LAYER_TYPE_SOFTWARE;
12517 buildDrawingCache(true);
12518 }
12519 cache = getDrawingCache(true);
12520 } else {
12521 switch (layerType) {
12522 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012523 if (useDisplayListProperties) {
12524 hasDisplayList = canHaveDisplayList();
12525 } else {
12526 buildDrawingCache(true);
12527 cache = getDrawingCache(true);
12528 }
Chet Haase64a48c12012-02-13 16:33:29 -080012529 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012530 case LAYER_TYPE_HARDWARE:
12531 if (useDisplayListProperties) {
12532 hasDisplayList = canHaveDisplayList();
12533 }
12534 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012535 case LAYER_TYPE_NONE:
12536 // Delay getting the display list until animation-driven alpha values are
12537 // set up and possibly passed on to the view
12538 hasDisplayList = canHaveDisplayList();
12539 break;
12540 }
12541 }
12542 }
Chet Haasea1cff502012-02-21 13:43:44 -080012543 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012544 if (useDisplayListProperties) {
12545 displayList = getDisplayList();
12546 if (!displayList.isValid()) {
12547 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12548 // to getDisplayList(), the display list will be marked invalid and we should not
12549 // try to use it again.
12550 displayList = null;
12551 hasDisplayList = false;
12552 useDisplayListProperties = false;
12553 }
12554 }
Chet Haase64a48c12012-02-13 16:33:29 -080012555
12556 final boolean hasNoCache = cache == null || hasDisplayList;
12557 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12558 layerType != LAYER_TYPE_HARDWARE;
12559
Chet Haasea1cff502012-02-21 13:43:44 -080012560 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012561 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012562 restoreTo = canvas.save();
12563 }
Chet Haase64a48c12012-02-13 16:33:29 -080012564 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012565 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012566 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012567 if (!useDisplayListProperties) {
12568 canvas.translate(mLeft, mTop);
12569 }
Chet Haase64a48c12012-02-13 16:33:29 -080012570 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012571 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012572 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012573 restoreTo = canvas.save();
12574 }
Chet Haase64a48c12012-02-13 16:33:29 -080012575 // mAttachInfo cannot be null, otherwise scalingRequired == false
12576 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12577 canvas.scale(scale, scale);
12578 }
12579 }
12580
Chet Haasea1cff502012-02-21 13:43:44 -080012581 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012582 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012583 if (transformToApply != null || !childHasIdentityMatrix) {
12584 int transX = 0;
12585 int transY = 0;
12586
12587 if (offsetForScroll) {
12588 transX = -sx;
12589 transY = -sy;
12590 }
12591
12592 if (transformToApply != null) {
12593 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012594 if (useDisplayListProperties) {
12595 displayList.setAnimationMatrix(transformToApply.getMatrix());
12596 } else {
12597 // Undo the scroll translation, apply the transformation matrix,
12598 // then redo the scroll translate to get the correct result.
12599 canvas.translate(-transX, -transY);
12600 canvas.concat(transformToApply.getMatrix());
12601 canvas.translate(transX, transY);
12602 }
Chet Haasea1cff502012-02-21 13:43:44 -080012603 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012604 }
12605
12606 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012607 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012608 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012609 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012610 }
12611 }
12612
Chet Haasea1cff502012-02-21 13:43:44 -080012613 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012614 canvas.translate(-transX, -transY);
12615 canvas.concat(getMatrix());
12616 canvas.translate(transX, transY);
12617 }
12618 }
12619
Chet Haase9420abd2012-03-29 16:28:32 -070012620 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012621 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012622 if (hasNoCache) {
12623 final int multipliedAlpha = (int) (255 * alpha);
12624 if (!onSetAlpha(multipliedAlpha)) {
12625 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012626 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012627 layerType != LAYER_TYPE_NONE) {
12628 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12629 }
Chet Haase9420abd2012-03-29 16:28:32 -070012630 if (useDisplayListProperties) {
12631 displayList.setAlpha(alpha * getAlpha());
12632 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012633 final int scrollX = hasDisplayList ? 0 : sx;
12634 final int scrollY = hasDisplayList ? 0 : sy;
12635 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12636 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012637 }
12638 } else {
12639 // Alpha is handled by the child directly, clobber the layer's alpha
12640 mPrivateFlags |= ALPHA_SET;
12641 }
12642 }
12643 }
12644 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12645 onSetAlpha(255);
12646 mPrivateFlags &= ~ALPHA_SET;
12647 }
12648
Chet Haasea1cff502012-02-21 13:43:44 -080012649 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12650 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012651 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012652 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012653 } else {
12654 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012655 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012656 } else {
12657 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12658 }
12659 }
12660 }
12661
Chet Haase9420abd2012-03-29 16:28:32 -070012662 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012663 displayList = getDisplayList();
12664 if (!displayList.isValid()) {
12665 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12666 // to getDisplayList(), the display list will be marked invalid and we should not
12667 // try to use it again.
12668 displayList = null;
12669 hasDisplayList = false;
12670 }
12671 }
12672
12673 if (hasNoCache) {
12674 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012675 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012676 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012677 if (layer != null && layer.isValid()) {
12678 mLayerPaint.setAlpha((int) (alpha * 255));
12679 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12680 layerRendered = true;
12681 } else {
12682 final int scrollX = hasDisplayList ? 0 : sx;
12683 final int scrollY = hasDisplayList ? 0 : sy;
12684 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012685 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012686 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12687 }
12688 }
12689
12690 if (!layerRendered) {
12691 if (!hasDisplayList) {
12692 // Fast path for layouts with no backgrounds
12693 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12694 if (ViewDebug.TRACE_HIERARCHY) {
12695 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12696 }
12697 mPrivateFlags &= ~DIRTY_MASK;
12698 dispatchDraw(canvas);
12699 } else {
12700 draw(canvas);
12701 }
12702 } else {
12703 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070012704 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012705 }
12706 }
12707 } else if (cache != null) {
12708 mPrivateFlags &= ~DIRTY_MASK;
12709 Paint cachePaint;
12710
12711 if (layerType == LAYER_TYPE_NONE) {
12712 cachePaint = parent.mCachePaint;
12713 if (cachePaint == null) {
12714 cachePaint = new Paint();
12715 cachePaint.setDither(false);
12716 parent.mCachePaint = cachePaint;
12717 }
Chet Haase9420abd2012-03-29 16:28:32 -070012718 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012719 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012720 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12721 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012722 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012723 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012724 }
12725 } else {
12726 cachePaint = mLayerPaint;
12727 cachePaint.setAlpha((int) (alpha * 255));
12728 }
12729 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12730 }
12731
Chet Haasea1cff502012-02-21 13:43:44 -080012732 if (restoreTo >= 0) {
12733 canvas.restoreToCount(restoreTo);
12734 }
Chet Haase64a48c12012-02-13 16:33:29 -080012735
12736 if (a != null && !more) {
12737 if (!hardwareAccelerated && !a.getFillAfter()) {
12738 onSetAlpha(255);
12739 }
12740 parent.finishAnimatingView(this, a);
12741 }
12742
12743 if (more && hardwareAccelerated) {
12744 // invalidation is the trigger to recreate display lists, so if we're using
12745 // display lists to render, force an invalidate to allow the animation to
12746 // continue drawing another frame
12747 parent.invalidate(true);
12748 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12749 // alpha animations should cause the child to recreate its display list
12750 invalidate(true);
12751 }
12752 }
12753
12754 mRecreateDisplayList = false;
12755
12756 return more;
12757 }
12758
12759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012760 * Manually render this view (and all of its children) to the given Canvas.
12761 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012762 * called. When implementing a view, implement
12763 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12764 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012765 *
12766 * @param canvas The Canvas to which the View is rendered.
12767 */
12768 public void draw(Canvas canvas) {
12769 if (ViewDebug.TRACE_HIERARCHY) {
12770 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12771 }
12772
Romain Guy5bcdff42009-05-14 21:27:18 -070012773 final int privateFlags = mPrivateFlags;
12774 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12775 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12776 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012778 /*
12779 * Draw traversal performs several drawing steps which must be executed
12780 * in the appropriate order:
12781 *
12782 * 1. Draw the background
12783 * 2. If necessary, save the canvas' layers to prepare for fading
12784 * 3. Draw view's content
12785 * 4. Draw children
12786 * 5. If necessary, draw the fading edges and restore layers
12787 * 6. Draw decorations (scrollbars for instance)
12788 */
12789
12790 // Step 1, draw the background, if needed
12791 int saveCount;
12792
Romain Guy24443ea2009-05-11 11:56:30 -070012793 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012794 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012795 if (background != null) {
12796 final int scrollX = mScrollX;
12797 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012798
Romain Guy24443ea2009-05-11 11:56:30 -070012799 if (mBackgroundSizeChanged) {
12800 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12801 mBackgroundSizeChanged = false;
12802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012803
Romain Guy24443ea2009-05-11 11:56:30 -070012804 if ((scrollX | scrollY) == 0) {
12805 background.draw(canvas);
12806 } else {
12807 canvas.translate(scrollX, scrollY);
12808 background.draw(canvas);
12809 canvas.translate(-scrollX, -scrollY);
12810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012811 }
12812 }
12813
12814 // skip step 2 & 5 if possible (common case)
12815 final int viewFlags = mViewFlags;
12816 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12817 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12818 if (!verticalEdges && !horizontalEdges) {
12819 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012820 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012821
12822 // Step 4, draw the children
12823 dispatchDraw(canvas);
12824
12825 // Step 6, draw decorations (scrollbars)
12826 onDrawScrollBars(canvas);
12827
12828 // we're done...
12829 return;
12830 }
12831
12832 /*
12833 * Here we do the full fledged routine...
12834 * (this is an uncommon case where speed matters less,
12835 * this is why we repeat some of the tests that have been
12836 * done above)
12837 */
12838
12839 boolean drawTop = false;
12840 boolean drawBottom = false;
12841 boolean drawLeft = false;
12842 boolean drawRight = false;
12843
12844 float topFadeStrength = 0.0f;
12845 float bottomFadeStrength = 0.0f;
12846 float leftFadeStrength = 0.0f;
12847 float rightFadeStrength = 0.0f;
12848
12849 // Step 2, save the canvas' layers
12850 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012851
12852 final boolean offsetRequired = isPaddingOffsetRequired();
12853 if (offsetRequired) {
12854 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855 }
12856
12857 int left = mScrollX + paddingLeft;
12858 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012859 int top = mScrollY + getFadeTop(offsetRequired);
12860 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012861
12862 if (offsetRequired) {
12863 right += getRightPaddingOffset();
12864 bottom += getBottomPaddingOffset();
12865 }
12866
12867 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012868 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012869 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012870
12871 // clip the fade length if top and bottom fades overlap
12872 // overlapping fades produce odd-looking artifacts
12873 if (verticalEdges && (top + length > bottom - length)) {
12874 length = (bottom - top) / 2;
12875 }
12876
12877 // also clip horizontal fades if necessary
12878 if (horizontalEdges && (left + length > right - length)) {
12879 length = (right - left) / 2;
12880 }
12881
12882 if (verticalEdges) {
12883 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012884 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012885 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012886 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012887 }
12888
12889 if (horizontalEdges) {
12890 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012891 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012892 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012893 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012894 }
12895
12896 saveCount = canvas.getSaveCount();
12897
12898 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012899 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12901
12902 if (drawTop) {
12903 canvas.saveLayer(left, top, right, top + length, null, flags);
12904 }
12905
12906 if (drawBottom) {
12907 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12908 }
12909
12910 if (drawLeft) {
12911 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12912 }
12913
12914 if (drawRight) {
12915 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12916 }
12917 } else {
12918 scrollabilityCache.setFadeColor(solidColor);
12919 }
12920
12921 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012922 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012923
12924 // Step 4, draw the children
12925 dispatchDraw(canvas);
12926
12927 // Step 5, draw the fade effect and restore layers
12928 final Paint p = scrollabilityCache.paint;
12929 final Matrix matrix = scrollabilityCache.matrix;
12930 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012931
12932 if (drawTop) {
12933 matrix.setScale(1, fadeHeight * topFadeStrength);
12934 matrix.postTranslate(left, top);
12935 fade.setLocalMatrix(matrix);
12936 canvas.drawRect(left, top, right, top + length, p);
12937 }
12938
12939 if (drawBottom) {
12940 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12941 matrix.postRotate(180);
12942 matrix.postTranslate(left, bottom);
12943 fade.setLocalMatrix(matrix);
12944 canvas.drawRect(left, bottom - length, right, bottom, p);
12945 }
12946
12947 if (drawLeft) {
12948 matrix.setScale(1, fadeHeight * leftFadeStrength);
12949 matrix.postRotate(-90);
12950 matrix.postTranslate(left, top);
12951 fade.setLocalMatrix(matrix);
12952 canvas.drawRect(left, top, left + length, bottom, p);
12953 }
12954
12955 if (drawRight) {
12956 matrix.setScale(1, fadeHeight * rightFadeStrength);
12957 matrix.postRotate(90);
12958 matrix.postTranslate(right, top);
12959 fade.setLocalMatrix(matrix);
12960 canvas.drawRect(right - length, top, right, bottom, p);
12961 }
12962
12963 canvas.restoreToCount(saveCount);
12964
12965 // Step 6, draw decorations (scrollbars)
12966 onDrawScrollBars(canvas);
12967 }
12968
12969 /**
12970 * Override this if your view is known to always be drawn on top of a solid color background,
12971 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12972 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12973 * should be set to 0xFF.
12974 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012975 * @see #setVerticalFadingEdgeEnabled(boolean)
12976 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012977 *
12978 * @return The known solid color background for this view, or 0 if the color may vary
12979 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012980 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012981 public int getSolidColor() {
12982 return 0;
12983 }
12984
12985 /**
12986 * Build a human readable string representation of the specified view flags.
12987 *
12988 * @param flags the view flags to convert to a string
12989 * @return a String representing the supplied flags
12990 */
12991 private static String printFlags(int flags) {
12992 String output = "";
12993 int numFlags = 0;
12994 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12995 output += "TAKES_FOCUS";
12996 numFlags++;
12997 }
12998
12999 switch (flags & VISIBILITY_MASK) {
13000 case INVISIBLE:
13001 if (numFlags > 0) {
13002 output += " ";
13003 }
13004 output += "INVISIBLE";
13005 // USELESS HERE numFlags++;
13006 break;
13007 case GONE:
13008 if (numFlags > 0) {
13009 output += " ";
13010 }
13011 output += "GONE";
13012 // USELESS HERE numFlags++;
13013 break;
13014 default:
13015 break;
13016 }
13017 return output;
13018 }
13019
13020 /**
13021 * Build a human readable string representation of the specified private
13022 * view flags.
13023 *
13024 * @param privateFlags the private view flags to convert to a string
13025 * @return a String representing the supplied flags
13026 */
13027 private static String printPrivateFlags(int privateFlags) {
13028 String output = "";
13029 int numFlags = 0;
13030
13031 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13032 output += "WANTS_FOCUS";
13033 numFlags++;
13034 }
13035
13036 if ((privateFlags & FOCUSED) == FOCUSED) {
13037 if (numFlags > 0) {
13038 output += " ";
13039 }
13040 output += "FOCUSED";
13041 numFlags++;
13042 }
13043
13044 if ((privateFlags & SELECTED) == SELECTED) {
13045 if (numFlags > 0) {
13046 output += " ";
13047 }
13048 output += "SELECTED";
13049 numFlags++;
13050 }
13051
13052 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13053 if (numFlags > 0) {
13054 output += " ";
13055 }
13056 output += "IS_ROOT_NAMESPACE";
13057 numFlags++;
13058 }
13059
13060 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13061 if (numFlags > 0) {
13062 output += " ";
13063 }
13064 output += "HAS_BOUNDS";
13065 numFlags++;
13066 }
13067
13068 if ((privateFlags & DRAWN) == DRAWN) {
13069 if (numFlags > 0) {
13070 output += " ";
13071 }
13072 output += "DRAWN";
13073 // USELESS HERE numFlags++;
13074 }
13075 return output;
13076 }
13077
13078 /**
13079 * <p>Indicates whether or not this view's layout will be requested during
13080 * the next hierarchy layout pass.</p>
13081 *
13082 * @return true if the layout will be forced during next layout pass
13083 */
13084 public boolean isLayoutRequested() {
13085 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13086 }
13087
13088 /**
13089 * Assign a size and position to a view and all of its
13090 * descendants
13091 *
13092 * <p>This is the second phase of the layout mechanism.
13093 * (The first is measuring). In this phase, each parent calls
13094 * layout on all of its children to position them.
13095 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013096 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013097 *
Chet Haase9c087442011-01-12 16:20:16 -080013098 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013099 * Derived classes with children should override
13100 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013101 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013102 *
13103 * @param l Left position, relative to parent
13104 * @param t Top position, relative to parent
13105 * @param r Right position, relative to parent
13106 * @param b Bottom position, relative to parent
13107 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013108 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013109 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013110 int oldL = mLeft;
13111 int oldT = mTop;
13112 int oldB = mBottom;
13113 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013114 boolean changed = setFrame(l, t, r, b);
13115 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13116 if (ViewDebug.TRACE_HIERARCHY) {
13117 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13118 }
13119
13120 onLayout(changed, l, t, r, b);
13121 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013122
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013123 ListenerInfo li = mListenerInfo;
13124 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013125 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013126 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013127 int numListeners = listenersCopy.size();
13128 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013129 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013130 }
13131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013132 }
13133 mPrivateFlags &= ~FORCE_LAYOUT;
13134 }
13135
13136 /**
13137 * Called from layout when this view should
13138 * assign a size and position to each of its children.
13139 *
13140 * Derived classes with children should override
13141 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013142 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013143 * @param changed This is a new size or position for this view
13144 * @param left Left position, relative to parent
13145 * @param top Top position, relative to parent
13146 * @param right Right position, relative to parent
13147 * @param bottom Bottom position, relative to parent
13148 */
13149 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13150 }
13151
13152 /**
13153 * Assign a size and position to this view.
13154 *
13155 * This is called from layout.
13156 *
13157 * @param left Left position, relative to parent
13158 * @param top Top position, relative to parent
13159 * @param right Right position, relative to parent
13160 * @param bottom Bottom position, relative to parent
13161 * @return true if the new size and position are different than the
13162 * previous ones
13163 * {@hide}
13164 */
13165 protected boolean setFrame(int left, int top, int right, int bottom) {
13166 boolean changed = false;
13167
13168 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013169 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013170 + right + "," + bottom + ")");
13171 }
13172
13173 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13174 changed = true;
13175
13176 // Remember our drawn bit
13177 int drawn = mPrivateFlags & DRAWN;
13178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013179 int oldWidth = mRight - mLeft;
13180 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013181 int newWidth = right - left;
13182 int newHeight = bottom - top;
13183 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13184
13185 // Invalidate our old position
13186 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187
13188 mLeft = left;
13189 mTop = top;
13190 mRight = right;
13191 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013192 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013193 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013195
13196 mPrivateFlags |= HAS_BOUNDS;
13197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198
Chet Haase75755e22011-07-18 17:48:25 -070013199 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013200 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13201 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013202 if (mTransformationInfo != null) {
13203 mTransformationInfo.mMatrixDirty = true;
13204 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013206 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13207 }
13208
13209 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13210 // If we are visible, force the DRAWN bit to on so that
13211 // this invalidate will go through (at least to our parent).
13212 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013213 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013214 // the DRAWN bit.
13215 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013216 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013217 // parent display list may need to be recreated based on a change in the bounds
13218 // of any child
13219 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013220 }
13221
13222 // Reset drawn bit to original value (invalidate turns it off)
13223 mPrivateFlags |= drawn;
13224
13225 mBackgroundSizeChanged = true;
13226 }
13227 return changed;
13228 }
13229
13230 /**
13231 * Finalize inflating a view from XML. This is called as the last phase
13232 * of inflation, after all child views have been added.
13233 *
13234 * <p>Even if the subclass overrides onFinishInflate, they should always be
13235 * sure to call the super method, so that we get called.
13236 */
13237 protected void onFinishInflate() {
13238 }
13239
13240 /**
13241 * Returns the resources associated with this view.
13242 *
13243 * @return Resources object.
13244 */
13245 public Resources getResources() {
13246 return mResources;
13247 }
13248
13249 /**
13250 * Invalidates the specified Drawable.
13251 *
13252 * @param drawable the drawable to invalidate
13253 */
13254 public void invalidateDrawable(Drawable drawable) {
13255 if (verifyDrawable(drawable)) {
13256 final Rect dirty = drawable.getBounds();
13257 final int scrollX = mScrollX;
13258 final int scrollY = mScrollY;
13259
13260 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13261 dirty.right + scrollX, dirty.bottom + scrollY);
13262 }
13263 }
13264
13265 /**
13266 * Schedules an action on a drawable to occur at a specified time.
13267 *
13268 * @param who the recipient of the action
13269 * @param what the action to run on the drawable
13270 * @param when the time at which the action must occur. Uses the
13271 * {@link SystemClock#uptimeMillis} timebase.
13272 */
13273 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013274 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013275 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013276 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013277 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13278 Choreographer.CALLBACK_ANIMATION, what, who,
13279 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013280 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013281 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013283 }
13284 }
13285
13286 /**
13287 * Cancels a scheduled action on a drawable.
13288 *
13289 * @param who the recipient of the action
13290 * @param what the action to cancel
13291 */
13292 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013293 if (verifyDrawable(who) && what != null) {
13294 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013295 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13296 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013297 } else {
13298 ViewRootImpl.getRunQueue().removeCallbacks(what);
13299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013300 }
13301 }
13302
13303 /**
13304 * Unschedule any events associated with the given Drawable. This can be
13305 * used when selecting a new Drawable into a view, so that the previous
13306 * one is completely unscheduled.
13307 *
13308 * @param who The Drawable to unschedule.
13309 *
13310 * @see #drawableStateChanged
13311 */
13312 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013313 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013314 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13315 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013316 }
13317 }
13318
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013319 /**
13320 * Return the layout direction of a given Drawable.
13321 *
13322 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013323 */
13324 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013325 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013326 }
13327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013328 /**
13329 * If your view subclass is displaying its own Drawable objects, it should
13330 * override this function and return true for any Drawable it is
13331 * displaying. This allows animations for those drawables to be
13332 * scheduled.
13333 *
13334 * <p>Be sure to call through to the super class when overriding this
13335 * function.
13336 *
13337 * @param who The Drawable to verify. Return true if it is one you are
13338 * displaying, else return the result of calling through to the
13339 * super class.
13340 *
13341 * @return boolean If true than the Drawable is being displayed in the
13342 * view; else false and it is not allowed to animate.
13343 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013344 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13345 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013346 */
13347 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013348 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013349 }
13350
13351 /**
13352 * This function is called whenever the state of the view changes in such
13353 * a way that it impacts the state of drawables being shown.
13354 *
13355 * <p>Be sure to call through to the superclass when overriding this
13356 * function.
13357 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013358 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013359 */
13360 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013361 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013362 if (d != null && d.isStateful()) {
13363 d.setState(getDrawableState());
13364 }
13365 }
13366
13367 /**
13368 * Call this to force a view to update its drawable state. This will cause
13369 * drawableStateChanged to be called on this view. Views that are interested
13370 * in the new state should call getDrawableState.
13371 *
13372 * @see #drawableStateChanged
13373 * @see #getDrawableState
13374 */
13375 public void refreshDrawableState() {
13376 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13377 drawableStateChanged();
13378
13379 ViewParent parent = mParent;
13380 if (parent != null) {
13381 parent.childDrawableStateChanged(this);
13382 }
13383 }
13384
13385 /**
13386 * Return an array of resource IDs of the drawable states representing the
13387 * current state of the view.
13388 *
13389 * @return The current drawable state
13390 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013391 * @see Drawable#setState(int[])
13392 * @see #drawableStateChanged()
13393 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013394 */
13395 public final int[] getDrawableState() {
13396 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13397 return mDrawableState;
13398 } else {
13399 mDrawableState = onCreateDrawableState(0);
13400 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13401 return mDrawableState;
13402 }
13403 }
13404
13405 /**
13406 * Generate the new {@link android.graphics.drawable.Drawable} state for
13407 * this view. This is called by the view
13408 * system when the cached Drawable state is determined to be invalid. To
13409 * retrieve the current state, you should use {@link #getDrawableState}.
13410 *
13411 * @param extraSpace if non-zero, this is the number of extra entries you
13412 * would like in the returned array in which you can place your own
13413 * states.
13414 *
13415 * @return Returns an array holding the current {@link Drawable} state of
13416 * the view.
13417 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013418 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013419 */
13420 protected int[] onCreateDrawableState(int extraSpace) {
13421 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13422 mParent instanceof View) {
13423 return ((View) mParent).onCreateDrawableState(extraSpace);
13424 }
13425
13426 int[] drawableState;
13427
13428 int privateFlags = mPrivateFlags;
13429
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013430 int viewStateIndex = 0;
13431 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13432 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13433 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013434 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013435 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13436 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013437 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13438 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013439 // This is set if HW acceleration is requested, even if the current
13440 // process doesn't allow it. This is just to allow app preview
13441 // windows to better match their app.
13442 viewStateIndex |= VIEW_STATE_ACCELERATED;
13443 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013444 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013445
Christopher Tate3d4bf172011-03-28 16:16:46 -070013446 final int privateFlags2 = mPrivateFlags2;
13447 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13448 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013450 drawableState = VIEW_STATE_SETS[viewStateIndex];
13451
13452 //noinspection ConstantIfStatement
13453 if (false) {
13454 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13455 Log.i("View", toString()
13456 + " pressed=" + ((privateFlags & PRESSED) != 0)
13457 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13458 + " fo=" + hasFocus()
13459 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013460 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013461 + ": " + Arrays.toString(drawableState));
13462 }
13463
13464 if (extraSpace == 0) {
13465 return drawableState;
13466 }
13467
13468 final int[] fullState;
13469 if (drawableState != null) {
13470 fullState = new int[drawableState.length + extraSpace];
13471 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13472 } else {
13473 fullState = new int[extraSpace];
13474 }
13475
13476 return fullState;
13477 }
13478
13479 /**
13480 * Merge your own state values in <var>additionalState</var> into the base
13481 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013482 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013483 *
13484 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013485 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013486 * own additional state values.
13487 *
13488 * @param additionalState The additional state values you would like
13489 * added to <var>baseState</var>; this array is not modified.
13490 *
13491 * @return As a convenience, the <var>baseState</var> array you originally
13492 * passed into the function is returned.
13493 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013494 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013495 */
13496 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13497 final int N = baseState.length;
13498 int i = N - 1;
13499 while (i >= 0 && baseState[i] == 0) {
13500 i--;
13501 }
13502 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13503 return baseState;
13504 }
13505
13506 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013507 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13508 * on all Drawable objects associated with this view.
13509 */
13510 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013511 if (mBackground != null) {
13512 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013513 }
13514 }
13515
13516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013517 * Sets the background color for this view.
13518 * @param color the color of the background
13519 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013520 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013521 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013522 if (mBackground instanceof ColorDrawable) {
13523 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013524 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013525 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527 }
13528
13529 /**
13530 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013531 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013534 * @attr ref android.R.styleable#View_background
13535 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013536 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013537 public void setBackgroundResource(int resid) {
13538 if (resid != 0 && resid == mBackgroundResource) {
13539 return;
13540 }
13541
13542 Drawable d= null;
13543 if (resid != 0) {
13544 d = mResources.getDrawable(resid);
13545 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013546 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013547
13548 mBackgroundResource = resid;
13549 }
13550
13551 /**
13552 * Set the background to a given Drawable, or remove the background. If the
13553 * background has padding, this View's padding is set to the background's
13554 * padding. However, when a background is removed, this View's padding isn't
13555 * touched. If setting the padding is desired, please use
13556 * {@link #setPadding(int, int, int, int)}.
13557 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013558 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013559 * background
13560 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013561 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013562 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013563 setBackgroundDrawable(background);
13564 }
13565
13566 /**
13567 * @deprecated use {@link #setBackground(Drawable)} instead
13568 */
13569 @Deprecated
13570 public void setBackgroundDrawable(Drawable background) {
13571 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013572 return;
13573 }
13574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013575 boolean requestLayout = false;
13576
13577 mBackgroundResource = 0;
13578
13579 /*
13580 * Regardless of whether we're setting a new background or not, we want
13581 * to clear the previous drawable.
13582 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013583 if (mBackground != null) {
13584 mBackground.setCallback(null);
13585 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 }
13587
Philip Milne6c8ea062012-04-03 17:38:43 -070013588 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013589 Rect padding = sThreadLocal.get();
13590 if (padding == null) {
13591 padding = new Rect();
13592 sThreadLocal.set(padding);
13593 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013594 if (background.getPadding(padding)) {
13595 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013596 case LAYOUT_DIRECTION_RTL:
13597 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13598 break;
13599 case LAYOUT_DIRECTION_LTR:
13600 default:
13601 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 }
13604
13605 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13606 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013607 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13608 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013609 requestLayout = true;
13610 }
13611
Philip Milne6c8ea062012-04-03 17:38:43 -070013612 background.setCallback(this);
13613 if (background.isStateful()) {
13614 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013615 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013616 background.setVisible(getVisibility() == VISIBLE, false);
13617 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013618
13619 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13620 mPrivateFlags &= ~SKIP_DRAW;
13621 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13622 requestLayout = true;
13623 }
13624 } else {
13625 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013626 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013627
13628 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13629 /*
13630 * This view ONLY drew the background before and we're removing
13631 * the background, so now it won't draw anything
13632 * (hence we SKIP_DRAW)
13633 */
13634 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13635 mPrivateFlags |= SKIP_DRAW;
13636 }
13637
13638 /*
13639 * When the background is set, we try to apply its padding to this
13640 * View. When the background is removed, we don't touch this View's
13641 * padding. This is noted in the Javadocs. Hence, we don't need to
13642 * requestLayout(), the invalidate() below is sufficient.
13643 */
13644
13645 // The old background's minimum size could have affected this
13646 // View's layout, so let's requestLayout
13647 requestLayout = true;
13648 }
13649
Romain Guy8f1344f52009-05-15 16:03:59 -070013650 computeOpaqueFlags();
13651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652 if (requestLayout) {
13653 requestLayout();
13654 }
13655
13656 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013657 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013658 }
13659
13660 /**
13661 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013664 *
13665 * @see #setBackground(Drawable)
13666 *
13667 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013668 */
13669 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013670 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 }
13672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013673 /**
13674 * Sets the padding. The view may add on the space required to display
13675 * the scrollbars, depending on the style and visibility of the scrollbars.
13676 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13677 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13678 * from the values set in this call.
13679 *
13680 * @attr ref android.R.styleable#View_padding
13681 * @attr ref android.R.styleable#View_paddingBottom
13682 * @attr ref android.R.styleable#View_paddingLeft
13683 * @attr ref android.R.styleable#View_paddingRight
13684 * @attr ref android.R.styleable#View_paddingTop
13685 * @param left the left padding in pixels
13686 * @param top the top padding in pixels
13687 * @param right the right padding in pixels
13688 * @param bottom the bottom padding in pixels
13689 */
13690 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013691 mUserPaddingStart = -1;
13692 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013693 mUserPaddingRelative = false;
13694
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013695 internalSetPadding(left, top, right, bottom);
13696 }
13697
13698 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013699 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013700 mUserPaddingRight = right;
13701 mUserPaddingBottom = bottom;
13702
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013703 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013704 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013705
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013706 // Common case is there are no scroll bars.
13707 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013708 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013709 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013710 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013711 switch (mVerticalScrollbarPosition) {
13712 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013713 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13714 left += offset;
13715 } else {
13716 right += offset;
13717 }
13718 break;
Adam Powell20232d02010-12-08 21:08:53 -080013719 case SCROLLBAR_POSITION_RIGHT:
13720 right += offset;
13721 break;
13722 case SCROLLBAR_POSITION_LEFT:
13723 left += offset;
13724 break;
13725 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013726 }
Adam Powell20232d02010-12-08 21:08:53 -080013727 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013728 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13729 ? 0 : getHorizontalScrollbarHeight();
13730 }
13731 }
Romain Guy8506ab42009-06-11 17:35:47 -070013732
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013733 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013734 changed = true;
13735 mPaddingLeft = left;
13736 }
13737 if (mPaddingTop != top) {
13738 changed = true;
13739 mPaddingTop = top;
13740 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013741 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013742 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013743 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013744 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013745 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013746 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013747 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013748 }
13749
13750 if (changed) {
13751 requestLayout();
13752 }
13753 }
13754
13755 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013756 * Sets the relative padding. The view may add on the space required to display
13757 * the scrollbars, depending on the style and visibility of the scrollbars.
13758 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13759 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13760 * from the values set in this call.
13761 *
13762 * @attr ref android.R.styleable#View_padding
13763 * @attr ref android.R.styleable#View_paddingBottom
13764 * @attr ref android.R.styleable#View_paddingStart
13765 * @attr ref android.R.styleable#View_paddingEnd
13766 * @attr ref android.R.styleable#View_paddingTop
13767 * @param start the start padding in pixels
13768 * @param top the top padding in pixels
13769 * @param end the end padding in pixels
13770 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013771 */
13772 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013773 mUserPaddingStart = start;
13774 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013775 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013776
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013777 switch(getResolvedLayoutDirection()) {
13778 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013779 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013780 break;
13781 case LAYOUT_DIRECTION_LTR:
13782 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013783 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013784 }
13785 }
13786
13787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013788 * Returns the top padding of this view.
13789 *
13790 * @return the top padding in pixels
13791 */
13792 public int getPaddingTop() {
13793 return mPaddingTop;
13794 }
13795
13796 /**
13797 * Returns the bottom padding of this view. If there are inset and enabled
13798 * scrollbars, this value may include the space required to display the
13799 * scrollbars as well.
13800 *
13801 * @return the bottom padding in pixels
13802 */
13803 public int getPaddingBottom() {
13804 return mPaddingBottom;
13805 }
13806
13807 /**
13808 * Returns the left padding of this view. If there are inset and enabled
13809 * scrollbars, this value may include the space required to display the
13810 * scrollbars as well.
13811 *
13812 * @return the left padding in pixels
13813 */
13814 public int getPaddingLeft() {
13815 return mPaddingLeft;
13816 }
13817
13818 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013819 * Returns the start padding of this view depending on its resolved layout direction.
13820 * If there are inset and enabled scrollbars, this value may include the space
13821 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013822 *
13823 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013824 */
13825 public int getPaddingStart() {
13826 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13827 mPaddingRight : mPaddingLeft;
13828 }
13829
13830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013831 * Returns the right padding of this view. If there are inset and enabled
13832 * scrollbars, this value may include the space required to display the
13833 * scrollbars as well.
13834 *
13835 * @return the right padding in pixels
13836 */
13837 public int getPaddingRight() {
13838 return mPaddingRight;
13839 }
13840
13841 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013842 * Returns the end padding of this view depending on its resolved layout direction.
13843 * If there are inset and enabled scrollbars, this value may include the space
13844 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013845 *
13846 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013847 */
13848 public int getPaddingEnd() {
13849 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13850 mPaddingLeft : mPaddingRight;
13851 }
13852
13853 /**
13854 * Return if the padding as been set thru relative values
13855 * {@link #setPaddingRelative(int, int, int, int)} or thru
13856 * @attr ref android.R.styleable#View_paddingStart or
13857 * @attr ref android.R.styleable#View_paddingEnd
13858 *
13859 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013860 */
13861 public boolean isPaddingRelative() {
13862 return mUserPaddingRelative;
13863 }
13864
13865 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013866 * @hide
13867 */
13868 public Insets getLayoutInsets() {
13869 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013870 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013871 }
13872 return mLayoutInsets;
13873 }
13874
13875 /**
13876 * @hide
13877 */
13878 public void setLayoutInsets(Insets layoutInsets) {
13879 mLayoutInsets = layoutInsets;
13880 }
13881
13882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013883 * Changes the selection state of this view. A view can be selected or not.
13884 * Note that selection is not the same as focus. Views are typically
13885 * selected in the context of an AdapterView like ListView or GridView;
13886 * the selected view is the view that is highlighted.
13887 *
13888 * @param selected true if the view must be selected, false otherwise
13889 */
13890 public void setSelected(boolean selected) {
13891 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13892 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013893 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013894 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 refreshDrawableState();
13896 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013897 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13898 notifyAccessibilityStateChanged();
13899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 }
13901 }
13902
13903 /**
13904 * Dispatch setSelected to all of this View's children.
13905 *
13906 * @see #setSelected(boolean)
13907 *
13908 * @param selected The new selected state
13909 */
13910 protected void dispatchSetSelected(boolean selected) {
13911 }
13912
13913 /**
13914 * Indicates the selection state of this view.
13915 *
13916 * @return true if the view is selected, false otherwise
13917 */
13918 @ViewDebug.ExportedProperty
13919 public boolean isSelected() {
13920 return (mPrivateFlags & SELECTED) != 0;
13921 }
13922
13923 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013924 * Changes the activated state of this view. A view can be activated or not.
13925 * Note that activation is not the same as selection. Selection is
13926 * a transient property, representing the view (hierarchy) the user is
13927 * currently interacting with. Activation is a longer-term state that the
13928 * user can move views in and out of. For example, in a list view with
13929 * single or multiple selection enabled, the views in the current selection
13930 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13931 * here.) The activated state is propagated down to children of the view it
13932 * is set on.
13933 *
13934 * @param activated true if the view must be activated, false otherwise
13935 */
13936 public void setActivated(boolean activated) {
13937 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13938 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013939 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013940 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013941 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013942 }
13943 }
13944
13945 /**
13946 * Dispatch setActivated to all of this View's children.
13947 *
13948 * @see #setActivated(boolean)
13949 *
13950 * @param activated The new activated state
13951 */
13952 protected void dispatchSetActivated(boolean activated) {
13953 }
13954
13955 /**
13956 * Indicates the activation state of this view.
13957 *
13958 * @return true if the view is activated, false otherwise
13959 */
13960 @ViewDebug.ExportedProperty
13961 public boolean isActivated() {
13962 return (mPrivateFlags & ACTIVATED) != 0;
13963 }
13964
13965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013966 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13967 * observer can be used to get notifications when global events, like
13968 * layout, happen.
13969 *
13970 * The returned ViewTreeObserver observer is not guaranteed to remain
13971 * valid for the lifetime of this View. If the caller of this method keeps
13972 * a long-lived reference to ViewTreeObserver, it should always check for
13973 * the return value of {@link ViewTreeObserver#isAlive()}.
13974 *
13975 * @return The ViewTreeObserver for this view's hierarchy.
13976 */
13977 public ViewTreeObserver getViewTreeObserver() {
13978 if (mAttachInfo != null) {
13979 return mAttachInfo.mTreeObserver;
13980 }
13981 if (mFloatingTreeObserver == null) {
13982 mFloatingTreeObserver = new ViewTreeObserver();
13983 }
13984 return mFloatingTreeObserver;
13985 }
13986
13987 /**
13988 * <p>Finds the topmost view in the current view hierarchy.</p>
13989 *
13990 * @return the topmost view containing this view
13991 */
13992 public View getRootView() {
13993 if (mAttachInfo != null) {
13994 final View v = mAttachInfo.mRootView;
13995 if (v != null) {
13996 return v;
13997 }
13998 }
Romain Guy8506ab42009-06-11 17:35:47 -070013999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 View parent = this;
14001
14002 while (parent.mParent != null && parent.mParent instanceof View) {
14003 parent = (View) parent.mParent;
14004 }
14005
14006 return parent;
14007 }
14008
14009 /**
14010 * <p>Computes the coordinates of this view on the screen. The argument
14011 * must be an array of two integers. After the method returns, the array
14012 * contains the x and y location in that order.</p>
14013 *
14014 * @param location an array of two integers in which to hold the coordinates
14015 */
14016 public void getLocationOnScreen(int[] location) {
14017 getLocationInWindow(location);
14018
14019 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014020 if (info != null) {
14021 location[0] += info.mWindowLeft;
14022 location[1] += info.mWindowTop;
14023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014024 }
14025
14026 /**
14027 * <p>Computes the coordinates of this view in its window. The argument
14028 * must be an array of two integers. After the method returns, the array
14029 * contains the x and y location in that order.</p>
14030 *
14031 * @param location an array of two integers in which to hold the coordinates
14032 */
14033 public void getLocationInWindow(int[] location) {
14034 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014035 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014036 }
14037
Gilles Debunne6583ce52011-12-06 18:09:02 -080014038 if (mAttachInfo == null) {
14039 // When the view is not attached to a window, this method does not make sense
14040 location[0] = location[1] = 0;
14041 return;
14042 }
14043
Gilles Debunnecea45132011-11-24 02:19:27 +010014044 float[] position = mAttachInfo.mTmpTransformLocation;
14045 position[0] = position[1] = 0.0f;
14046
14047 if (!hasIdentityMatrix()) {
14048 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014050
Gilles Debunnecea45132011-11-24 02:19:27 +010014051 position[0] += mLeft;
14052 position[1] += mTop;
14053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014054 ViewParent viewParent = mParent;
14055 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014056 final View view = (View) viewParent;
14057
14058 position[0] -= view.mScrollX;
14059 position[1] -= view.mScrollY;
14060
14061 if (!view.hasIdentityMatrix()) {
14062 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014063 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014064
14065 position[0] += view.mLeft;
14066 position[1] += view.mTop;
14067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014068 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014069 }
Romain Guy8506ab42009-06-11 17:35:47 -070014070
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014071 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014073 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14074 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014075 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014076
14077 location[0] = (int) (position[0] + 0.5f);
14078 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014079 }
14080
14081 /**
14082 * {@hide}
14083 * @param id the id of the view to be found
14084 * @return the view of the specified id, null if cannot be found
14085 */
14086 protected View findViewTraversal(int id) {
14087 if (id == mID) {
14088 return this;
14089 }
14090 return null;
14091 }
14092
14093 /**
14094 * {@hide}
14095 * @param tag the tag of the view to be found
14096 * @return the view of specified tag, null if cannot be found
14097 */
14098 protected View findViewWithTagTraversal(Object tag) {
14099 if (tag != null && tag.equals(mTag)) {
14100 return this;
14101 }
14102 return null;
14103 }
14104
14105 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014106 * {@hide}
14107 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014108 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014109 * @return The first view that matches the predicate or null.
14110 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014111 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014112 if (predicate.apply(this)) {
14113 return this;
14114 }
14115 return null;
14116 }
14117
14118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 * Look for a child view with the given id. If this view has the given
14120 * id, return this view.
14121 *
14122 * @param id The id to search for.
14123 * @return The view that has the given id in the hierarchy or null
14124 */
14125 public final View findViewById(int id) {
14126 if (id < 0) {
14127 return null;
14128 }
14129 return findViewTraversal(id);
14130 }
14131
14132 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014133 * Finds a view by its unuque and stable accessibility id.
14134 *
14135 * @param accessibilityId The searched accessibility id.
14136 * @return The found view.
14137 */
14138 final View findViewByAccessibilityId(int accessibilityId) {
14139 if (accessibilityId < 0) {
14140 return null;
14141 }
14142 return findViewByAccessibilityIdTraversal(accessibilityId);
14143 }
14144
14145 /**
14146 * Performs the traversal to find a view by its unuque and stable accessibility id.
14147 *
14148 * <strong>Note:</strong>This method does not stop at the root namespace
14149 * boundary since the user can touch the screen at an arbitrary location
14150 * potentially crossing the root namespace bounday which will send an
14151 * accessibility event to accessibility services and they should be able
14152 * to obtain the event source. Also accessibility ids are guaranteed to be
14153 * unique in the window.
14154 *
14155 * @param accessibilityId The accessibility id.
14156 * @return The found view.
14157 */
14158 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14159 if (getAccessibilityViewId() == accessibilityId) {
14160 return this;
14161 }
14162 return null;
14163 }
14164
14165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014166 * Look for a child view with the given tag. If this view has the given
14167 * tag, return this view.
14168 *
14169 * @param tag The tag to search for, using "tag.equals(getTag())".
14170 * @return The View that has the given tag in the hierarchy or null
14171 */
14172 public final View findViewWithTag(Object tag) {
14173 if (tag == null) {
14174 return null;
14175 }
14176 return findViewWithTagTraversal(tag);
14177 }
14178
14179 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014180 * {@hide}
14181 * Look for a child view that matches the specified predicate.
14182 * If this view matches the predicate, return this view.
14183 *
14184 * @param predicate The predicate to evaluate.
14185 * @return The first view that matches the predicate or null.
14186 */
14187 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014188 return findViewByPredicateTraversal(predicate, null);
14189 }
14190
14191 /**
14192 * {@hide}
14193 * Look for a child view that matches the specified predicate,
14194 * starting with the specified view and its descendents and then
14195 * recusively searching the ancestors and siblings of that view
14196 * until this view is reached.
14197 *
14198 * This method is useful in cases where the predicate does not match
14199 * a single unique view (perhaps multiple views use the same id)
14200 * and we are trying to find the view that is "closest" in scope to the
14201 * starting view.
14202 *
14203 * @param start The view to start from.
14204 * @param predicate The predicate to evaluate.
14205 * @return The first view that matches the predicate or null.
14206 */
14207 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14208 View childToSkip = null;
14209 for (;;) {
14210 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14211 if (view != null || start == this) {
14212 return view;
14213 }
14214
14215 ViewParent parent = start.getParent();
14216 if (parent == null || !(parent instanceof View)) {
14217 return null;
14218 }
14219
14220 childToSkip = start;
14221 start = (View) parent;
14222 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014223 }
14224
14225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014226 * Sets the identifier for this view. The identifier does not have to be
14227 * unique in this view's hierarchy. The identifier should be a positive
14228 * number.
14229 *
14230 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014231 * @see #getId()
14232 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014233 *
14234 * @param id a number used to identify the view
14235 *
14236 * @attr ref android.R.styleable#View_id
14237 */
14238 public void setId(int id) {
14239 mID = id;
14240 }
14241
14242 /**
14243 * {@hide}
14244 *
14245 * @param isRoot true if the view belongs to the root namespace, false
14246 * otherwise
14247 */
14248 public void setIsRootNamespace(boolean isRoot) {
14249 if (isRoot) {
14250 mPrivateFlags |= IS_ROOT_NAMESPACE;
14251 } else {
14252 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14253 }
14254 }
14255
14256 /**
14257 * {@hide}
14258 *
14259 * @return true if the view belongs to the root namespace, false otherwise
14260 */
14261 public boolean isRootNamespace() {
14262 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14263 }
14264
14265 /**
14266 * Returns this view's identifier.
14267 *
14268 * @return a positive integer used to identify the view or {@link #NO_ID}
14269 * if the view has no ID
14270 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014271 * @see #setId(int)
14272 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014273 * @attr ref android.R.styleable#View_id
14274 */
14275 @ViewDebug.CapturedViewProperty
14276 public int getId() {
14277 return mID;
14278 }
14279
14280 /**
14281 * Returns this view's tag.
14282 *
14283 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014284 *
14285 * @see #setTag(Object)
14286 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014287 */
14288 @ViewDebug.ExportedProperty
14289 public Object getTag() {
14290 return mTag;
14291 }
14292
14293 /**
14294 * Sets the tag associated with this view. A tag can be used to mark
14295 * a view in its hierarchy and does not have to be unique within the
14296 * hierarchy. Tags can also be used to store data within a view without
14297 * resorting to another data structure.
14298 *
14299 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014300 *
14301 * @see #getTag()
14302 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014303 */
14304 public void setTag(final Object tag) {
14305 mTag = tag;
14306 }
14307
14308 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014309 * Returns the tag associated with this view and the specified key.
14310 *
14311 * @param key The key identifying the tag
14312 *
14313 * @return the Object stored in this view as a tag
14314 *
14315 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014316 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014317 */
14318 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014319 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014320 return null;
14321 }
14322
14323 /**
14324 * Sets a tag associated with this view and a key. A tag can be used
14325 * to mark a view in its hierarchy and does not have to be unique within
14326 * the hierarchy. Tags can also be used to store data within a view
14327 * without resorting to another data structure.
14328 *
14329 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014330 * application to ensure it is unique (see the <a
14331 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14332 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014333 * the Android framework or not associated with any package will cause
14334 * an {@link IllegalArgumentException} to be thrown.
14335 *
14336 * @param key The key identifying the tag
14337 * @param tag An Object to tag the view with
14338 *
14339 * @throws IllegalArgumentException If they specified key is not valid
14340 *
14341 * @see #setTag(Object)
14342 * @see #getTag(int)
14343 */
14344 public void setTag(int key, final Object tag) {
14345 // If the package id is 0x00 or 0x01, it's either an undefined package
14346 // or a framework id
14347 if ((key >>> 24) < 2) {
14348 throw new IllegalArgumentException("The key must be an application-specific "
14349 + "resource id.");
14350 }
14351
Adam Powell2b2f6d62011-09-23 11:15:39 -070014352 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014353 }
14354
14355 /**
14356 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14357 * framework id.
14358 *
14359 * @hide
14360 */
14361 public void setTagInternal(int key, Object tag) {
14362 if ((key >>> 24) != 0x1) {
14363 throw new IllegalArgumentException("The key must be a framework-specific "
14364 + "resource id.");
14365 }
14366
Adam Powell2b2f6d62011-09-23 11:15:39 -070014367 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014368 }
14369
Adam Powell2b2f6d62011-09-23 11:15:39 -070014370 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014371 if (mKeyedTags == null) {
14372 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014373 }
14374
Adam Powell7db82ac2011-09-22 19:44:04 -070014375 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014376 }
14377
14378 /**
Romain Guy13922e02009-05-12 17:56:14 -070014379 * @param consistency The type of consistency. See ViewDebug for more information.
14380 *
14381 * @hide
14382 */
14383 protected boolean dispatchConsistencyCheck(int consistency) {
14384 return onConsistencyCheck(consistency);
14385 }
14386
14387 /**
14388 * Method that subclasses should implement to check their consistency. The type of
14389 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014390 *
Romain Guy13922e02009-05-12 17:56:14 -070014391 * @param consistency The type of consistency. See ViewDebug for more information.
14392 *
14393 * @throws IllegalStateException if the view is in an inconsistent state.
14394 *
14395 * @hide
14396 */
14397 protected boolean onConsistencyCheck(int consistency) {
14398 boolean result = true;
14399
14400 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14401 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14402
14403 if (checkLayout) {
14404 if (getParent() == null) {
14405 result = false;
14406 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14407 "View " + this + " does not have a parent.");
14408 }
14409
14410 if (mAttachInfo == null) {
14411 result = false;
14412 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14413 "View " + this + " is not attached to a window.");
14414 }
14415 }
14416
14417 if (checkDrawing) {
14418 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14419 // from their draw() method
14420
14421 if ((mPrivateFlags & DRAWN) != DRAWN &&
14422 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14423 result = false;
14424 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14425 "View " + this + " was invalidated but its drawing cache is valid.");
14426 }
14427 }
14428
14429 return result;
14430 }
14431
14432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014433 * Prints information about this view in the log output, with the tag
14434 * {@link #VIEW_LOG_TAG}.
14435 *
14436 * @hide
14437 */
14438 public void debug() {
14439 debug(0);
14440 }
14441
14442 /**
14443 * Prints information about this view in the log output, with the tag
14444 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14445 * indentation defined by the <code>depth</code>.
14446 *
14447 * @param depth the indentation level
14448 *
14449 * @hide
14450 */
14451 protected void debug(int depth) {
14452 String output = debugIndent(depth - 1);
14453
14454 output += "+ " + this;
14455 int id = getId();
14456 if (id != -1) {
14457 output += " (id=" + id + ")";
14458 }
14459 Object tag = getTag();
14460 if (tag != null) {
14461 output += " (tag=" + tag + ")";
14462 }
14463 Log.d(VIEW_LOG_TAG, output);
14464
14465 if ((mPrivateFlags & FOCUSED) != 0) {
14466 output = debugIndent(depth) + " FOCUSED";
14467 Log.d(VIEW_LOG_TAG, output);
14468 }
14469
14470 output = debugIndent(depth);
14471 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14472 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14473 + "} ";
14474 Log.d(VIEW_LOG_TAG, output);
14475
14476 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14477 || mPaddingBottom != 0) {
14478 output = debugIndent(depth);
14479 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14480 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14481 Log.d(VIEW_LOG_TAG, output);
14482 }
14483
14484 output = debugIndent(depth);
14485 output += "mMeasureWidth=" + mMeasuredWidth +
14486 " mMeasureHeight=" + mMeasuredHeight;
14487 Log.d(VIEW_LOG_TAG, output);
14488
14489 output = debugIndent(depth);
14490 if (mLayoutParams == null) {
14491 output += "BAD! no layout params";
14492 } else {
14493 output = mLayoutParams.debug(output);
14494 }
14495 Log.d(VIEW_LOG_TAG, output);
14496
14497 output = debugIndent(depth);
14498 output += "flags={";
14499 output += View.printFlags(mViewFlags);
14500 output += "}";
14501 Log.d(VIEW_LOG_TAG, output);
14502
14503 output = debugIndent(depth);
14504 output += "privateFlags={";
14505 output += View.printPrivateFlags(mPrivateFlags);
14506 output += "}";
14507 Log.d(VIEW_LOG_TAG, output);
14508 }
14509
14510 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014511 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014512 *
14513 * @param depth the indentation level
14514 * @return a String containing (depth * 2 + 3) * 2 white spaces
14515 *
14516 * @hide
14517 */
14518 protected static String debugIndent(int depth) {
14519 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14520 for (int i = 0; i < (depth * 2) + 3; i++) {
14521 spaces.append(' ').append(' ');
14522 }
14523 return spaces.toString();
14524 }
14525
14526 /**
14527 * <p>Return the offset of the widget's text baseline from the widget's top
14528 * boundary. If this widget does not support baseline alignment, this
14529 * method returns -1. </p>
14530 *
14531 * @return the offset of the baseline within the widget's bounds or -1
14532 * if baseline alignment is not supported
14533 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014534 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014535 public int getBaseline() {
14536 return -1;
14537 }
14538
14539 /**
14540 * Call this when something has changed which has invalidated the
14541 * layout of this view. This will schedule a layout pass of the view
14542 * tree.
14543 */
14544 public void requestLayout() {
14545 if (ViewDebug.TRACE_HIERARCHY) {
14546 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14547 }
14548
14549 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014550 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014551
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014552 if (mLayoutParams != null) {
14553 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14554 }
14555
14556 if (mParent != null && !mParent.isLayoutRequested()) {
14557 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014558 }
14559 }
14560
14561 /**
14562 * Forces this view to be laid out during the next layout pass.
14563 * This method does not call requestLayout() or forceLayout()
14564 * on the parent.
14565 */
14566 public void forceLayout() {
14567 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014568 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 }
14570
14571 /**
14572 * <p>
14573 * This is called to find out how big a view should be. The parent
14574 * supplies constraint information in the width and height parameters.
14575 * </p>
14576 *
14577 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014578 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014579 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014580 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014581 * </p>
14582 *
14583 *
14584 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14585 * parent
14586 * @param heightMeasureSpec Vertical space requirements as imposed by the
14587 * parent
14588 *
14589 * @see #onMeasure(int, int)
14590 */
14591 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14592 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14593 widthMeasureSpec != mOldWidthMeasureSpec ||
14594 heightMeasureSpec != mOldHeightMeasureSpec) {
14595
14596 // first clears the measured dimension flag
14597 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14598
14599 if (ViewDebug.TRACE_HIERARCHY) {
14600 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14601 }
14602
14603 // measure ourselves, this should set the measured dimension flag back
14604 onMeasure(widthMeasureSpec, heightMeasureSpec);
14605
14606 // flag not set, setMeasuredDimension() was not invoked, we raise
14607 // an exception to warn the developer
14608 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14609 throw new IllegalStateException("onMeasure() did not set the"
14610 + " measured dimension by calling"
14611 + " setMeasuredDimension()");
14612 }
14613
14614 mPrivateFlags |= LAYOUT_REQUIRED;
14615 }
14616
14617 mOldWidthMeasureSpec = widthMeasureSpec;
14618 mOldHeightMeasureSpec = heightMeasureSpec;
14619 }
14620
14621 /**
14622 * <p>
14623 * Measure the view and its content to determine the measured width and the
14624 * measured height. This method is invoked by {@link #measure(int, int)} and
14625 * should be overriden by subclasses to provide accurate and efficient
14626 * measurement of their contents.
14627 * </p>
14628 *
14629 * <p>
14630 * <strong>CONTRACT:</strong> When overriding this method, you
14631 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14632 * measured width and height of this view. Failure to do so will trigger an
14633 * <code>IllegalStateException</code>, thrown by
14634 * {@link #measure(int, int)}. Calling the superclass'
14635 * {@link #onMeasure(int, int)} is a valid use.
14636 * </p>
14637 *
14638 * <p>
14639 * The base class implementation of measure defaults to the background size,
14640 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14641 * override {@link #onMeasure(int, int)} to provide better measurements of
14642 * their content.
14643 * </p>
14644 *
14645 * <p>
14646 * If this method is overridden, it is the subclass's responsibility to make
14647 * sure the measured height and width are at least the view's minimum height
14648 * and width ({@link #getSuggestedMinimumHeight()} and
14649 * {@link #getSuggestedMinimumWidth()}).
14650 * </p>
14651 *
14652 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14653 * The requirements are encoded with
14654 * {@link android.view.View.MeasureSpec}.
14655 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14656 * The requirements are encoded with
14657 * {@link android.view.View.MeasureSpec}.
14658 *
14659 * @see #getMeasuredWidth()
14660 * @see #getMeasuredHeight()
14661 * @see #setMeasuredDimension(int, int)
14662 * @see #getSuggestedMinimumHeight()
14663 * @see #getSuggestedMinimumWidth()
14664 * @see android.view.View.MeasureSpec#getMode(int)
14665 * @see android.view.View.MeasureSpec#getSize(int)
14666 */
14667 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14668 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14669 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14670 }
14671
14672 /**
14673 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14674 * measured width and measured height. Failing to do so will trigger an
14675 * exception at measurement time.</p>
14676 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014677 * @param measuredWidth The measured width of this view. May be a complex
14678 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14679 * {@link #MEASURED_STATE_TOO_SMALL}.
14680 * @param measuredHeight The measured height of this view. May be a complex
14681 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14682 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014683 */
14684 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14685 mMeasuredWidth = measuredWidth;
14686 mMeasuredHeight = measuredHeight;
14687
14688 mPrivateFlags |= MEASURED_DIMENSION_SET;
14689 }
14690
14691 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014692 * Merge two states as returned by {@link #getMeasuredState()}.
14693 * @param curState The current state as returned from a view or the result
14694 * of combining multiple views.
14695 * @param newState The new view state to combine.
14696 * @return Returns a new integer reflecting the combination of the two
14697 * states.
14698 */
14699 public static int combineMeasuredStates(int curState, int newState) {
14700 return curState | newState;
14701 }
14702
14703 /**
14704 * Version of {@link #resolveSizeAndState(int, int, int)}
14705 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14706 */
14707 public static int resolveSize(int size, int measureSpec) {
14708 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14709 }
14710
14711 /**
14712 * Utility to reconcile a desired size and state, with constraints imposed
14713 * by a MeasureSpec. Will take the desired size, unless a different size
14714 * is imposed by the constraints. The returned value is a compound integer,
14715 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14716 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14717 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014718 *
14719 * @param size How big the view wants to be
14720 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014721 * @return Size information bit mask as defined by
14722 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014723 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014724 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014725 int result = size;
14726 int specMode = MeasureSpec.getMode(measureSpec);
14727 int specSize = MeasureSpec.getSize(measureSpec);
14728 switch (specMode) {
14729 case MeasureSpec.UNSPECIFIED:
14730 result = size;
14731 break;
14732 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014733 if (specSize < size) {
14734 result = specSize | MEASURED_STATE_TOO_SMALL;
14735 } else {
14736 result = size;
14737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 break;
14739 case MeasureSpec.EXACTLY:
14740 result = specSize;
14741 break;
14742 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014743 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014744 }
14745
14746 /**
14747 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014748 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014749 * by the MeasureSpec.
14750 *
14751 * @param size Default size for this view
14752 * @param measureSpec Constraints imposed by the parent
14753 * @return The size this view should be.
14754 */
14755 public static int getDefaultSize(int size, int measureSpec) {
14756 int result = size;
14757 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014758 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014759
14760 switch (specMode) {
14761 case MeasureSpec.UNSPECIFIED:
14762 result = size;
14763 break;
14764 case MeasureSpec.AT_MOST:
14765 case MeasureSpec.EXACTLY:
14766 result = specSize;
14767 break;
14768 }
14769 return result;
14770 }
14771
14772 /**
14773 * Returns the suggested minimum height that the view should use. This
14774 * returns the maximum of the view's minimum height
14775 * and the background's minimum height
14776 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14777 * <p>
14778 * When being used in {@link #onMeasure(int, int)}, the caller should still
14779 * ensure the returned height is within the requirements of the parent.
14780 *
14781 * @return The suggested minimum height of the view.
14782 */
14783 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014784 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 }
14787
14788 /**
14789 * Returns the suggested minimum width that the view should use. This
14790 * returns the maximum of the view's minimum width)
14791 * and the background's minimum width
14792 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14793 * <p>
14794 * When being used in {@link #onMeasure(int, int)}, the caller should still
14795 * ensure the returned width is within the requirements of the parent.
14796 *
14797 * @return The suggested minimum width of the view.
14798 */
14799 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014800 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014802
Philip Milne6c8ea062012-04-03 17:38:43 -070014803 /**
14804 * Returns the minimum height of the view.
14805 *
14806 * @return the minimum height the view will try to be.
14807 *
14808 * @see #setMinimumHeight(int)
14809 *
14810 * @attr ref android.R.styleable#View_minHeight
14811 */
14812 public int getMinimumHeight() {
14813 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014814 }
14815
14816 /**
14817 * Sets the minimum height of the view. It is not guaranteed the view will
14818 * be able to achieve this minimum height (for example, if its parent layout
14819 * constrains it with less available height).
14820 *
14821 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014822 *
14823 * @see #getMinimumHeight()
14824 *
14825 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014826 */
14827 public void setMinimumHeight(int minHeight) {
14828 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014829 requestLayout();
14830 }
14831
14832 /**
14833 * Returns the minimum width of the view.
14834 *
14835 * @return the minimum width the view will try to be.
14836 *
14837 * @see #setMinimumWidth(int)
14838 *
14839 * @attr ref android.R.styleable#View_minWidth
14840 */
14841 public int getMinimumWidth() {
14842 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843 }
14844
14845 /**
14846 * Sets the minimum width of the view. It is not guaranteed the view will
14847 * be able to achieve this minimum width (for example, if its parent layout
14848 * constrains it with less available width).
14849 *
14850 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014851 *
14852 * @see #getMinimumWidth()
14853 *
14854 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014855 */
14856 public void setMinimumWidth(int minWidth) {
14857 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014858 requestLayout();
14859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 }
14861
14862 /**
14863 * Get the animation currently associated with this view.
14864 *
14865 * @return The animation that is currently playing or
14866 * scheduled to play for this view.
14867 */
14868 public Animation getAnimation() {
14869 return mCurrentAnimation;
14870 }
14871
14872 /**
14873 * Start the specified animation now.
14874 *
14875 * @param animation the animation to start now
14876 */
14877 public void startAnimation(Animation animation) {
14878 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14879 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014880 invalidateParentCaches();
14881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014882 }
14883
14884 /**
14885 * Cancels any animations for this view.
14886 */
14887 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014888 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014889 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014891 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014892 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014893 }
14894
14895 /**
14896 * Sets the next animation to play for this view.
14897 * If you want the animation to play immediately, use
14898 * startAnimation. This method provides allows fine-grained
14899 * control over the start time and invalidation, but you
14900 * must make sure that 1) the animation has a start time set, and
14901 * 2) the view will be invalidated when the animation is supposed to
14902 * start.
14903 *
14904 * @param animation The next animation, or null.
14905 */
14906 public void setAnimation(Animation animation) {
14907 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014909 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014910 // If the screen is off assume the animation start time is now instead of
14911 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14912 // would cause the animation to start when the screen turns back on
14913 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14914 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14915 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014917 animation.reset();
14918 }
14919 }
14920
14921 /**
14922 * Invoked by a parent ViewGroup to notify the start of the animation
14923 * currently associated with this view. If you override this method,
14924 * always call super.onAnimationStart();
14925 *
14926 * @see #setAnimation(android.view.animation.Animation)
14927 * @see #getAnimation()
14928 */
14929 protected void onAnimationStart() {
14930 mPrivateFlags |= ANIMATION_STARTED;
14931 }
14932
14933 /**
14934 * Invoked by a parent ViewGroup to notify the end of the animation
14935 * currently associated with this view. If you override this method,
14936 * always call super.onAnimationEnd();
14937 *
14938 * @see #setAnimation(android.view.animation.Animation)
14939 * @see #getAnimation()
14940 */
14941 protected void onAnimationEnd() {
14942 mPrivateFlags &= ~ANIMATION_STARTED;
14943 }
14944
14945 /**
14946 * Invoked if there is a Transform that involves alpha. Subclass that can
14947 * draw themselves with the specified alpha should return true, and then
14948 * respect that alpha when their onDraw() is called. If this returns false
14949 * then the view may be redirected to draw into an offscreen buffer to
14950 * fulfill the request, which will look fine, but may be slower than if the
14951 * subclass handles it internally. The default implementation returns false.
14952 *
14953 * @param alpha The alpha (0..255) to apply to the view's drawing
14954 * @return true if the view can draw with the specified alpha.
14955 */
14956 protected boolean onSetAlpha(int alpha) {
14957 return false;
14958 }
14959
14960 /**
14961 * This is used by the RootView to perform an optimization when
14962 * the view hierarchy contains one or several SurfaceView.
14963 * SurfaceView is always considered transparent, but its children are not,
14964 * therefore all View objects remove themselves from the global transparent
14965 * region (passed as a parameter to this function).
14966 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014967 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014968 *
14969 * @return Returns true if the effective visibility of the view at this
14970 * point is opaque, regardless of the transparent region; returns false
14971 * if it is possible for underlying windows to be seen behind the view.
14972 *
14973 * {@hide}
14974 */
14975 public boolean gatherTransparentRegion(Region region) {
14976 final AttachInfo attachInfo = mAttachInfo;
14977 if (region != null && attachInfo != null) {
14978 final int pflags = mPrivateFlags;
14979 if ((pflags & SKIP_DRAW) == 0) {
14980 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14981 // remove it from the transparent region.
14982 final int[] location = attachInfo.mTransparentLocation;
14983 getLocationInWindow(location);
14984 region.op(location[0], location[1], location[0] + mRight - mLeft,
14985 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014986 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014987 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14988 // exists, so we remove the background drawable's non-transparent
14989 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014990 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014991 }
14992 }
14993 return true;
14994 }
14995
14996 /**
14997 * Play a sound effect for this view.
14998 *
14999 * <p>The framework will play sound effects for some built in actions, such as
15000 * clicking, but you may wish to play these effects in your widget,
15001 * for instance, for internal navigation.
15002 *
15003 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15004 * {@link #isSoundEffectsEnabled()} is true.
15005 *
15006 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15007 */
15008 public void playSoundEffect(int soundConstant) {
15009 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15010 return;
15011 }
15012 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15013 }
15014
15015 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015016 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015017 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015018 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015019 *
15020 * <p>The framework will provide haptic feedback for some built in actions,
15021 * such as long presses, but you may wish to provide feedback for your
15022 * own widget.
15023 *
15024 * <p>The feedback will only be performed if
15025 * {@link #isHapticFeedbackEnabled()} is true.
15026 *
15027 * @param feedbackConstant One of the constants defined in
15028 * {@link HapticFeedbackConstants}
15029 */
15030 public boolean performHapticFeedback(int feedbackConstant) {
15031 return performHapticFeedback(feedbackConstant, 0);
15032 }
15033
15034 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015035 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015036 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015037 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015038 *
15039 * @param feedbackConstant One of the constants defined in
15040 * {@link HapticFeedbackConstants}
15041 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15042 */
15043 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15044 if (mAttachInfo == null) {
15045 return false;
15046 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015047 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015048 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015049 && !isHapticFeedbackEnabled()) {
15050 return false;
15051 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015052 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15053 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015054 }
15055
15056 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015057 * Request that the visibility of the status bar or other screen/window
15058 * decorations be changed.
15059 *
15060 * <p>This method is used to put the over device UI into temporary modes
15061 * where the user's attention is focused more on the application content,
15062 * by dimming or hiding surrounding system affordances. This is typically
15063 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15064 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15065 * to be placed behind the action bar (and with these flags other system
15066 * affordances) so that smooth transitions between hiding and showing them
15067 * can be done.
15068 *
15069 * <p>Two representative examples of the use of system UI visibility is
15070 * implementing a content browsing application (like a magazine reader)
15071 * and a video playing application.
15072 *
15073 * <p>The first code shows a typical implementation of a View in a content
15074 * browsing application. In this implementation, the application goes
15075 * into a content-oriented mode by hiding the status bar and action bar,
15076 * and putting the navigation elements into lights out mode. The user can
15077 * then interact with content while in this mode. Such an application should
15078 * provide an easy way for the user to toggle out of the mode (such as to
15079 * check information in the status bar or access notifications). In the
15080 * implementation here, this is done simply by tapping on the content.
15081 *
15082 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15083 * content}
15084 *
15085 * <p>This second code sample shows a typical implementation of a View
15086 * in a video playing application. In this situation, while the video is
15087 * playing the application would like to go into a complete full-screen mode,
15088 * to use as much of the display as possible for the video. When in this state
15089 * the user can not interact with the application; the system intercepts
15090 * touching on the screen to pop the UI out of full screen mode.
15091 *
15092 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15093 * content}
15094 *
15095 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15096 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15097 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15098 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015099 */
15100 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015101 if (visibility != mSystemUiVisibility) {
15102 mSystemUiVisibility = visibility;
15103 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15104 mParent.recomputeViewAttributes(this);
15105 }
Joe Onorato664644d2011-01-23 17:53:23 -080015106 }
15107 }
15108
15109 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015110 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15111 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15112 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15113 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15114 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015115 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015116 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015117 return mSystemUiVisibility;
15118 }
15119
Scott Mainec6331b2011-05-24 16:55:56 -070015120 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015121 * Returns the current system UI visibility that is currently set for
15122 * the entire window. This is the combination of the
15123 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15124 * views in the window.
15125 */
15126 public int getWindowSystemUiVisibility() {
15127 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15128 }
15129
15130 /**
15131 * Override to find out when the window's requested system UI visibility
15132 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15133 * This is different from the callbacks recieved through
15134 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15135 * in that this is only telling you about the local request of the window,
15136 * not the actual values applied by the system.
15137 */
15138 public void onWindowSystemUiVisibilityChanged(int visible) {
15139 }
15140
15141 /**
15142 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15143 * the view hierarchy.
15144 */
15145 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15146 onWindowSystemUiVisibilityChanged(visible);
15147 }
15148
15149 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015150 * Set a listener to receive callbacks when the visibility of the system bar changes.
15151 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15152 */
Joe Onorato664644d2011-01-23 17:53:23 -080015153 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015154 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015155 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15156 mParent.recomputeViewAttributes(this);
15157 }
15158 }
15159
15160 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015161 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15162 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015163 */
15164 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015165 ListenerInfo li = mListenerInfo;
15166 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15167 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015168 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015169 }
15170 }
15171
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015172 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15173 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15174 if (val != mSystemUiVisibility) {
15175 setSystemUiVisibility(val);
15176 }
15177 }
15178
Joe Onorato664644d2011-01-23 17:53:23 -080015179 /**
Joe Malin32736f02011-01-19 16:14:20 -080015180 * Creates an image that the system displays during the drag and drop
15181 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15182 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15183 * appearance as the given View. The default also positions the center of the drag shadow
15184 * directly under the touch point. If no View is provided (the constructor with no parameters
15185 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15186 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15187 * default is an invisible drag shadow.
15188 * <p>
15189 * You are not required to use the View you provide to the constructor as the basis of the
15190 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15191 * anything you want as the drag shadow.
15192 * </p>
15193 * <p>
15194 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15195 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15196 * size and position of the drag shadow. It uses this data to construct a
15197 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15198 * so that your application can draw the shadow image in the Canvas.
15199 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015200 *
15201 * <div class="special reference">
15202 * <h3>Developer Guides</h3>
15203 * <p>For a guide to implementing drag and drop features, read the
15204 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15205 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015206 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015207 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015208 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015209
15210 /**
Joe Malin32736f02011-01-19 16:14:20 -080015211 * Constructs a shadow image builder based on a View. By default, the resulting drag
15212 * shadow will have the same appearance and dimensions as the View, with the touch point
15213 * over the center of the View.
15214 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015215 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015216 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015217 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015218 }
15219
Christopher Tate17ed60c2011-01-18 12:50:26 -080015220 /**
15221 * Construct a shadow builder object with no associated View. This
15222 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15223 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15224 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015225 * reference to any View object. If they are not overridden, then the result is an
15226 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015227 */
15228 public DragShadowBuilder() {
15229 mView = new WeakReference<View>(null);
15230 }
15231
15232 /**
15233 * Returns the View object that had been passed to the
15234 * {@link #View.DragShadowBuilder(View)}
15235 * constructor. If that View parameter was {@code null} or if the
15236 * {@link #View.DragShadowBuilder()}
15237 * constructor was used to instantiate the builder object, this method will return
15238 * null.
15239 *
15240 * @return The View object associate with this builder object.
15241 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015242 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015243 final public View getView() {
15244 return mView.get();
15245 }
15246
Christopher Tate2c095f32010-10-04 14:13:40 -070015247 /**
Joe Malin32736f02011-01-19 16:14:20 -080015248 * Provides the metrics for the shadow image. These include the dimensions of
15249 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015250 * be centered under the touch location while dragging.
15251 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015252 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015253 * same as the dimensions of the View itself and centers the shadow under
15254 * the touch point.
15255 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015256 *
Joe Malin32736f02011-01-19 16:14:20 -080015257 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15258 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15259 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15260 * image.
15261 *
15262 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15263 * shadow image that should be underneath the touch point during the drag and drop
15264 * operation. Your application must set {@link android.graphics.Point#x} to the
15265 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015266 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015267 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015268 final View view = mView.get();
15269 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015270 shadowSize.set(view.getWidth(), view.getHeight());
15271 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015272 } else {
15273 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15274 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015275 }
15276
15277 /**
Joe Malin32736f02011-01-19 16:14:20 -080015278 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15279 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015280 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015281 *
Joe Malin32736f02011-01-19 16:14:20 -080015282 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015283 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015284 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015285 final View view = mView.get();
15286 if (view != null) {
15287 view.draw(canvas);
15288 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015289 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015290 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015291 }
15292 }
15293
15294 /**
Joe Malin32736f02011-01-19 16:14:20 -080015295 * Starts a drag and drop operation. When your application calls this method, it passes a
15296 * {@link android.view.View.DragShadowBuilder} object to the system. The
15297 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15298 * to get metrics for the drag shadow, and then calls the object's
15299 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15300 * <p>
15301 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15302 * drag events to all the View objects in your application that are currently visible. It does
15303 * this either by calling the View object's drag listener (an implementation of
15304 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15305 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15306 * Both are passed a {@link android.view.DragEvent} object that has a
15307 * {@link android.view.DragEvent#getAction()} value of
15308 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15309 * </p>
15310 * <p>
15311 * Your application can invoke startDrag() on any attached View object. The View object does not
15312 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15313 * be related to the View the user selected for dragging.
15314 * </p>
15315 * @param data A {@link android.content.ClipData} object pointing to the data to be
15316 * transferred by the drag and drop operation.
15317 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15318 * drag shadow.
15319 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15320 * drop operation. This Object is put into every DragEvent object sent by the system during the
15321 * current drag.
15322 * <p>
15323 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15324 * to the target Views. For example, it can contain flags that differentiate between a
15325 * a copy operation and a move operation.
15326 * </p>
15327 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15328 * so the parameter should be set to 0.
15329 * @return {@code true} if the method completes successfully, or
15330 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15331 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015332 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015333 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015334 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015335 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015336 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015337 }
15338 boolean okay = false;
15339
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015340 Point shadowSize = new Point();
15341 Point shadowTouchPoint = new Point();
15342 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015343
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015344 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15345 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15346 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015347 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015348
Chris Tatea32dcf72010-10-14 12:13:50 -070015349 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015350 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15351 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015352 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015353 Surface surface = new Surface();
15354 try {
15355 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015356 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015357 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015358 + " surface=" + surface);
15359 if (token != null) {
15360 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015361 try {
Chris Tate6b391282010-10-14 15:48:59 -070015362 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015363 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015364 } finally {
15365 surface.unlockCanvasAndPost(canvas);
15366 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015367
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015368 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015369
15370 // Cache the local state object for delivery with DragEvents
15371 root.setLocalDragState(myLocalState);
15372
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015373 // repurpose 'shadowSize' for the last touch point
15374 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015375
Christopher Tatea53146c2010-09-07 11:57:52 -070015376 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015377 shadowSize.x, shadowSize.y,
15378 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015379 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015380
15381 // Off and running! Release our local surface instance; the drag
15382 // shadow surface is now managed by the system process.
15383 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015384 }
15385 } catch (Exception e) {
15386 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15387 surface.destroy();
15388 }
15389
15390 return okay;
15391 }
15392
Christopher Tatea53146c2010-09-07 11:57:52 -070015393 /**
Joe Malin32736f02011-01-19 16:14:20 -080015394 * Handles drag events sent by the system following a call to
15395 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15396 *<p>
15397 * When the system calls this method, it passes a
15398 * {@link android.view.DragEvent} object. A call to
15399 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15400 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15401 * operation.
15402 * @param event The {@link android.view.DragEvent} sent by the system.
15403 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15404 * in DragEvent, indicating the type of drag event represented by this object.
15405 * @return {@code true} if the method was successful, otherwise {@code false}.
15406 * <p>
15407 * The method should return {@code true} in response to an action type of
15408 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15409 * operation.
15410 * </p>
15411 * <p>
15412 * The method should also return {@code true} in response to an action type of
15413 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15414 * {@code false} if it didn't.
15415 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015416 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015417 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015418 return false;
15419 }
15420
15421 /**
Joe Malin32736f02011-01-19 16:14:20 -080015422 * Detects if this View is enabled and has a drag event listener.
15423 * If both are true, then it calls the drag event listener with the
15424 * {@link android.view.DragEvent} it received. If the drag event listener returns
15425 * {@code true}, then dispatchDragEvent() returns {@code true}.
15426 * <p>
15427 * For all other cases, the method calls the
15428 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15429 * method and returns its result.
15430 * </p>
15431 * <p>
15432 * This ensures that a drag event is always consumed, even if the View does not have a drag
15433 * event listener. However, if the View has a listener and the listener returns true, then
15434 * onDragEvent() is not called.
15435 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015436 */
15437 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015438 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015439 ListenerInfo li = mListenerInfo;
15440 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15441 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015442 return true;
15443 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015444 return onDragEvent(event);
15445 }
15446
Christopher Tate3d4bf172011-03-28 16:16:46 -070015447 boolean canAcceptDrag() {
15448 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15449 }
15450
Christopher Tatea53146c2010-09-07 11:57:52 -070015451 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015452 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15453 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015454 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015455 */
15456 public void onCloseSystemDialogs(String reason) {
15457 }
Joe Malin32736f02011-01-19 16:14:20 -080015458
Dianne Hackbornffa42482009-09-23 22:20:11 -070015459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015460 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015461 * update a Region being computed for
15462 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015463 * that any non-transparent parts of the Drawable are removed from the
15464 * given transparent region.
15465 *
15466 * @param dr The Drawable whose transparency is to be applied to the region.
15467 * @param region A Region holding the current transparency information,
15468 * where any parts of the region that are set are considered to be
15469 * transparent. On return, this region will be modified to have the
15470 * transparency information reduced by the corresponding parts of the
15471 * Drawable that are not transparent.
15472 * {@hide}
15473 */
15474 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15475 if (DBG) {
15476 Log.i("View", "Getting transparent region for: " + this);
15477 }
15478 final Region r = dr.getTransparentRegion();
15479 final Rect db = dr.getBounds();
15480 final AttachInfo attachInfo = mAttachInfo;
15481 if (r != null && attachInfo != null) {
15482 final int w = getRight()-getLeft();
15483 final int h = getBottom()-getTop();
15484 if (db.left > 0) {
15485 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15486 r.op(0, 0, db.left, h, Region.Op.UNION);
15487 }
15488 if (db.right < w) {
15489 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15490 r.op(db.right, 0, w, h, Region.Op.UNION);
15491 }
15492 if (db.top > 0) {
15493 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15494 r.op(0, 0, w, db.top, Region.Op.UNION);
15495 }
15496 if (db.bottom < h) {
15497 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15498 r.op(0, db.bottom, w, h, Region.Op.UNION);
15499 }
15500 final int[] location = attachInfo.mTransparentLocation;
15501 getLocationInWindow(location);
15502 r.translate(location[0], location[1]);
15503 region.op(r, Region.Op.INTERSECT);
15504 } else {
15505 region.op(db, Region.Op.DIFFERENCE);
15506 }
15507 }
15508
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015509 private void checkForLongClick(int delayOffset) {
15510 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15511 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015512
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015513 if (mPendingCheckForLongPress == null) {
15514 mPendingCheckForLongPress = new CheckForLongPress();
15515 }
15516 mPendingCheckForLongPress.rememberWindowAttachCount();
15517 postDelayed(mPendingCheckForLongPress,
15518 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015520 }
15521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015522 /**
15523 * Inflate a view from an XML resource. This convenience method wraps the {@link
15524 * LayoutInflater} class, which provides a full range of options for view inflation.
15525 *
15526 * @param context The Context object for your activity or application.
15527 * @param resource The resource ID to inflate
15528 * @param root A view group that will be the parent. Used to properly inflate the
15529 * layout_* parameters.
15530 * @see LayoutInflater
15531 */
15532 public static View inflate(Context context, int resource, ViewGroup root) {
15533 LayoutInflater factory = LayoutInflater.from(context);
15534 return factory.inflate(resource, root);
15535 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015537 /**
Adam Powell637d3372010-08-25 14:37:03 -070015538 * Scroll the view with standard behavior for scrolling beyond the normal
15539 * content boundaries. Views that call this method should override
15540 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15541 * results of an over-scroll operation.
15542 *
15543 * Views can use this method to handle any touch or fling-based scrolling.
15544 *
15545 * @param deltaX Change in X in pixels
15546 * @param deltaY Change in Y in pixels
15547 * @param scrollX Current X scroll value in pixels before applying deltaX
15548 * @param scrollY Current Y scroll value in pixels before applying deltaY
15549 * @param scrollRangeX Maximum content scroll range along the X axis
15550 * @param scrollRangeY Maximum content scroll range along the Y axis
15551 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15552 * along the X axis.
15553 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15554 * along the Y axis.
15555 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15556 * @return true if scrolling was clamped to an over-scroll boundary along either
15557 * axis, false otherwise.
15558 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015559 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015560 protected boolean overScrollBy(int deltaX, int deltaY,
15561 int scrollX, int scrollY,
15562 int scrollRangeX, int scrollRangeY,
15563 int maxOverScrollX, int maxOverScrollY,
15564 boolean isTouchEvent) {
15565 final int overScrollMode = mOverScrollMode;
15566 final boolean canScrollHorizontal =
15567 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15568 final boolean canScrollVertical =
15569 computeVerticalScrollRange() > computeVerticalScrollExtent();
15570 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15571 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15572 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15573 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15574
15575 int newScrollX = scrollX + deltaX;
15576 if (!overScrollHorizontal) {
15577 maxOverScrollX = 0;
15578 }
15579
15580 int newScrollY = scrollY + deltaY;
15581 if (!overScrollVertical) {
15582 maxOverScrollY = 0;
15583 }
15584
15585 // Clamp values if at the limits and record
15586 final int left = -maxOverScrollX;
15587 final int right = maxOverScrollX + scrollRangeX;
15588 final int top = -maxOverScrollY;
15589 final int bottom = maxOverScrollY + scrollRangeY;
15590
15591 boolean clampedX = false;
15592 if (newScrollX > right) {
15593 newScrollX = right;
15594 clampedX = true;
15595 } else if (newScrollX < left) {
15596 newScrollX = left;
15597 clampedX = true;
15598 }
15599
15600 boolean clampedY = false;
15601 if (newScrollY > bottom) {
15602 newScrollY = bottom;
15603 clampedY = true;
15604 } else if (newScrollY < top) {
15605 newScrollY = top;
15606 clampedY = true;
15607 }
15608
15609 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15610
15611 return clampedX || clampedY;
15612 }
15613
15614 /**
15615 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15616 * respond to the results of an over-scroll operation.
15617 *
15618 * @param scrollX New X scroll value in pixels
15619 * @param scrollY New Y scroll value in pixels
15620 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15621 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15622 */
15623 protected void onOverScrolled(int scrollX, int scrollY,
15624 boolean clampedX, boolean clampedY) {
15625 // Intentionally empty.
15626 }
15627
15628 /**
15629 * Returns the over-scroll mode for this view. The result will be
15630 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15631 * (allow over-scrolling only if the view content is larger than the container),
15632 * or {@link #OVER_SCROLL_NEVER}.
15633 *
15634 * @return This view's over-scroll mode.
15635 */
15636 public int getOverScrollMode() {
15637 return mOverScrollMode;
15638 }
15639
15640 /**
15641 * Set the over-scroll mode for this view. Valid over-scroll modes are
15642 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15643 * (allow over-scrolling only if the view content is larger than the container),
15644 * or {@link #OVER_SCROLL_NEVER}.
15645 *
15646 * Setting the over-scroll mode of a view will have an effect only if the
15647 * view is capable of scrolling.
15648 *
15649 * @param overScrollMode The new over-scroll mode for this view.
15650 */
15651 public void setOverScrollMode(int overScrollMode) {
15652 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15653 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15654 overScrollMode != OVER_SCROLL_NEVER) {
15655 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15656 }
15657 mOverScrollMode = overScrollMode;
15658 }
15659
15660 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015661 * Gets a scale factor that determines the distance the view should scroll
15662 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15663 * @return The vertical scroll scale factor.
15664 * @hide
15665 */
15666 protected float getVerticalScrollFactor() {
15667 if (mVerticalScrollFactor == 0) {
15668 TypedValue outValue = new TypedValue();
15669 if (!mContext.getTheme().resolveAttribute(
15670 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15671 throw new IllegalStateException(
15672 "Expected theme to define listPreferredItemHeight.");
15673 }
15674 mVerticalScrollFactor = outValue.getDimension(
15675 mContext.getResources().getDisplayMetrics());
15676 }
15677 return mVerticalScrollFactor;
15678 }
15679
15680 /**
15681 * Gets a scale factor that determines the distance the view should scroll
15682 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15683 * @return The horizontal scroll scale factor.
15684 * @hide
15685 */
15686 protected float getHorizontalScrollFactor() {
15687 // TODO: Should use something else.
15688 return getVerticalScrollFactor();
15689 }
15690
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015691 /**
15692 * Return the value specifying the text direction or policy that was set with
15693 * {@link #setTextDirection(int)}.
15694 *
15695 * @return the defined text direction. It can be one of:
15696 *
15697 * {@link #TEXT_DIRECTION_INHERIT},
15698 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15699 * {@link #TEXT_DIRECTION_ANY_RTL},
15700 * {@link #TEXT_DIRECTION_LTR},
15701 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015702 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015703 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015704 @ViewDebug.ExportedProperty(category = "text", mapping = {
15705 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15706 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15707 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15708 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15709 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15710 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15711 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015712 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015713 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015714 }
15715
15716 /**
15717 * Set the text direction.
15718 *
15719 * @param textDirection the direction to set. Should be one of:
15720 *
15721 * {@link #TEXT_DIRECTION_INHERIT},
15722 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15723 * {@link #TEXT_DIRECTION_ANY_RTL},
15724 * {@link #TEXT_DIRECTION_LTR},
15725 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015726 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015727 */
15728 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015729 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015730 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015731 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015732 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015733 // Set the new text direction
15734 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015735 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015736 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015737 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015738 }
15739 }
15740
15741 /**
15742 * Return the resolved text direction.
15743 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015744 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15745 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15746 * up the parent chain of the view. if there is no parent, then it will return the default
15747 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15748 *
15749 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015750 *
Doug Feltcb3791202011-07-07 11:57:48 -070015751 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15752 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015753 * {@link #TEXT_DIRECTION_LTR},
15754 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015755 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015756 */
15757 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015758 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015759 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015760 resolveTextDirection();
15761 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015762 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015763 }
15764
15765 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015766 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15767 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015768 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015769 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015770 // Reset any previous text direction resolution
15771 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15772
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015773 if (hasRtlSupport()) {
15774 // Set resolved text direction flag depending on text direction flag
15775 final int textDirection = getTextDirection();
15776 switch(textDirection) {
15777 case TEXT_DIRECTION_INHERIT:
15778 if (canResolveTextDirection()) {
15779 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015780
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015781 // Set current resolved direction to the same value as the parent's one
15782 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15783 switch (parentResolvedDirection) {
15784 case TEXT_DIRECTION_FIRST_STRONG:
15785 case TEXT_DIRECTION_ANY_RTL:
15786 case TEXT_DIRECTION_LTR:
15787 case TEXT_DIRECTION_RTL:
15788 case TEXT_DIRECTION_LOCALE:
15789 mPrivateFlags2 |=
15790 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15791 break;
15792 default:
15793 // Default resolved direction is "first strong" heuristic
15794 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15795 }
15796 } else {
15797 // We cannot do the resolution if there is no parent, so use the default one
15798 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015799 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015800 break;
15801 case TEXT_DIRECTION_FIRST_STRONG:
15802 case TEXT_DIRECTION_ANY_RTL:
15803 case TEXT_DIRECTION_LTR:
15804 case TEXT_DIRECTION_RTL:
15805 case TEXT_DIRECTION_LOCALE:
15806 // Resolved direction is the same as text direction
15807 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15808 break;
15809 default:
15810 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015811 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015812 }
15813 } else {
15814 // Default resolved direction is "first strong" heuristic
15815 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015816 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015817
15818 // Set to resolved
15819 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015820 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015821 }
15822
15823 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015824 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015825 * resolution should override this method.
15826 *
15827 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015828 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015829 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015830 }
15831
15832 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015833 * Check if text direction resolution can be done.
15834 *
15835 * @return true if text direction resolution can be done otherwise return false.
15836 */
15837 public boolean canResolveTextDirection() {
15838 switch (getTextDirection()) {
15839 case TEXT_DIRECTION_INHERIT:
15840 return (mParent != null) && (mParent instanceof ViewGroup);
15841 default:
15842 return true;
15843 }
15844 }
15845
15846 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015847 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015848 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015849 * reset is done.
15850 */
15851 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015852 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015853 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015854 }
15855
15856 /**
15857 * Called when text direction is reset. Subclasses that care about text direction reset should
15858 * override this method and do a reset of the text direction of their children. The default
15859 * implementation does nothing.
15860 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015861 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015862 }
15863
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015864 /**
15865 * Return the value specifying the text alignment or policy that was set with
15866 * {@link #setTextAlignment(int)}.
15867 *
15868 * @return the defined text alignment. It can be one of:
15869 *
15870 * {@link #TEXT_ALIGNMENT_INHERIT},
15871 * {@link #TEXT_ALIGNMENT_GRAVITY},
15872 * {@link #TEXT_ALIGNMENT_CENTER},
15873 * {@link #TEXT_ALIGNMENT_TEXT_START},
15874 * {@link #TEXT_ALIGNMENT_TEXT_END},
15875 * {@link #TEXT_ALIGNMENT_VIEW_START},
15876 * {@link #TEXT_ALIGNMENT_VIEW_END}
15877 */
15878 @ViewDebug.ExportedProperty(category = "text", mapping = {
15879 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15880 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15881 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15882 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15883 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15884 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15885 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15886 })
15887 public int getTextAlignment() {
15888 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15889 }
15890
15891 /**
15892 * Set the text alignment.
15893 *
15894 * @param textAlignment The text alignment to set. Should be one of
15895 *
15896 * {@link #TEXT_ALIGNMENT_INHERIT},
15897 * {@link #TEXT_ALIGNMENT_GRAVITY},
15898 * {@link #TEXT_ALIGNMENT_CENTER},
15899 * {@link #TEXT_ALIGNMENT_TEXT_START},
15900 * {@link #TEXT_ALIGNMENT_TEXT_END},
15901 * {@link #TEXT_ALIGNMENT_VIEW_START},
15902 * {@link #TEXT_ALIGNMENT_VIEW_END}
15903 *
15904 * @attr ref android.R.styleable#View_textAlignment
15905 */
15906 public void setTextAlignment(int textAlignment) {
15907 if (textAlignment != getTextAlignment()) {
15908 // Reset the current and resolved text alignment
15909 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15910 resetResolvedTextAlignment();
15911 // Set the new text alignment
15912 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15913 // Refresh
15914 requestLayout();
15915 invalidate(true);
15916 }
15917 }
15918
15919 /**
15920 * Return the resolved text alignment.
15921 *
15922 * The resolved text alignment. This needs resolution if the value is
15923 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15924 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15925 *
15926 * @return the resolved text alignment. Returns one of:
15927 *
15928 * {@link #TEXT_ALIGNMENT_GRAVITY},
15929 * {@link #TEXT_ALIGNMENT_CENTER},
15930 * {@link #TEXT_ALIGNMENT_TEXT_START},
15931 * {@link #TEXT_ALIGNMENT_TEXT_END},
15932 * {@link #TEXT_ALIGNMENT_VIEW_START},
15933 * {@link #TEXT_ALIGNMENT_VIEW_END}
15934 */
15935 @ViewDebug.ExportedProperty(category = "text", mapping = {
15936 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15937 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15938 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15939 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15940 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15941 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15942 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15943 })
15944 public int getResolvedTextAlignment() {
15945 // If text alignment is not resolved, then resolve it
15946 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15947 resolveTextAlignment();
15948 }
15949 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15950 }
15951
15952 /**
15953 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15954 * resolution is done.
15955 */
15956 public void resolveTextAlignment() {
15957 // Reset any previous text alignment resolution
15958 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15959
15960 if (hasRtlSupport()) {
15961 // Set resolved text alignment flag depending on text alignment flag
15962 final int textAlignment = getTextAlignment();
15963 switch (textAlignment) {
15964 case TEXT_ALIGNMENT_INHERIT:
15965 // Check if we can resolve the text alignment
15966 if (canResolveLayoutDirection() && mParent instanceof View) {
15967 View view = (View) mParent;
15968
15969 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15970 switch (parentResolvedTextAlignment) {
15971 case TEXT_ALIGNMENT_GRAVITY:
15972 case TEXT_ALIGNMENT_TEXT_START:
15973 case TEXT_ALIGNMENT_TEXT_END:
15974 case TEXT_ALIGNMENT_CENTER:
15975 case TEXT_ALIGNMENT_VIEW_START:
15976 case TEXT_ALIGNMENT_VIEW_END:
15977 // Resolved text alignment is the same as the parent resolved
15978 // text alignment
15979 mPrivateFlags2 |=
15980 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15981 break;
15982 default:
15983 // Use default resolved text alignment
15984 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15985 }
15986 }
15987 else {
15988 // We cannot do the resolution if there is no parent so use the default
15989 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15990 }
15991 break;
15992 case TEXT_ALIGNMENT_GRAVITY:
15993 case TEXT_ALIGNMENT_TEXT_START:
15994 case TEXT_ALIGNMENT_TEXT_END:
15995 case TEXT_ALIGNMENT_CENTER:
15996 case TEXT_ALIGNMENT_VIEW_START:
15997 case TEXT_ALIGNMENT_VIEW_END:
15998 // Resolved text alignment is the same as text alignment
15999 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16000 break;
16001 default:
16002 // Use default resolved text alignment
16003 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16004 }
16005 } else {
16006 // Use default resolved text alignment
16007 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16008 }
16009
16010 // Set the resolved
16011 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16012 onResolvedTextAlignmentChanged();
16013 }
16014
16015 /**
16016 * Check if text alignment resolution can be done.
16017 *
16018 * @return true if text alignment resolution can be done otherwise return false.
16019 */
16020 public boolean canResolveTextAlignment() {
16021 switch (getTextAlignment()) {
16022 case TEXT_DIRECTION_INHERIT:
16023 return (mParent != null);
16024 default:
16025 return true;
16026 }
16027 }
16028
16029 /**
16030 * Called when text alignment has been resolved. Subclasses that care about text alignment
16031 * resolution should override this method.
16032 *
16033 * The default implementation does nothing.
16034 */
16035 public void onResolvedTextAlignmentChanged() {
16036 }
16037
16038 /**
16039 * Reset resolved text alignment. Text alignment can be resolved with a call to
16040 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16041 * reset is done.
16042 */
16043 public void resetResolvedTextAlignment() {
16044 // Reset any previous text alignment resolution
16045 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16046 onResolvedTextAlignmentReset();
16047 }
16048
16049 /**
16050 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16051 * override this method and do a reset of the text alignment of their children. The default
16052 * implementation does nothing.
16053 */
16054 public void onResolvedTextAlignmentReset() {
16055 }
16056
Chet Haaseb39f0512011-05-24 14:36:40 -070016057 //
16058 // Properties
16059 //
16060 /**
16061 * A Property wrapper around the <code>alpha</code> functionality handled by the
16062 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16063 */
Chet Haased47f1532011-12-16 11:18:52 -080016064 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016065 @Override
16066 public void setValue(View object, float value) {
16067 object.setAlpha(value);
16068 }
16069
16070 @Override
16071 public Float get(View object) {
16072 return object.getAlpha();
16073 }
16074 };
16075
16076 /**
16077 * A Property wrapper around the <code>translationX</code> functionality handled by the
16078 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16079 */
Chet Haased47f1532011-12-16 11:18:52 -080016080 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016081 @Override
16082 public void setValue(View object, float value) {
16083 object.setTranslationX(value);
16084 }
16085
16086 @Override
16087 public Float get(View object) {
16088 return object.getTranslationX();
16089 }
16090 };
16091
16092 /**
16093 * A Property wrapper around the <code>translationY</code> functionality handled by the
16094 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16095 */
Chet Haased47f1532011-12-16 11:18:52 -080016096 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016097 @Override
16098 public void setValue(View object, float value) {
16099 object.setTranslationY(value);
16100 }
16101
16102 @Override
16103 public Float get(View object) {
16104 return object.getTranslationY();
16105 }
16106 };
16107
16108 /**
16109 * A Property wrapper around the <code>x</code> functionality handled by the
16110 * {@link View#setX(float)} and {@link View#getX()} methods.
16111 */
Chet Haased47f1532011-12-16 11:18:52 -080016112 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016113 @Override
16114 public void setValue(View object, float value) {
16115 object.setX(value);
16116 }
16117
16118 @Override
16119 public Float get(View object) {
16120 return object.getX();
16121 }
16122 };
16123
16124 /**
16125 * A Property wrapper around the <code>y</code> functionality handled by the
16126 * {@link View#setY(float)} and {@link View#getY()} methods.
16127 */
Chet Haased47f1532011-12-16 11:18:52 -080016128 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016129 @Override
16130 public void setValue(View object, float value) {
16131 object.setY(value);
16132 }
16133
16134 @Override
16135 public Float get(View object) {
16136 return object.getY();
16137 }
16138 };
16139
16140 /**
16141 * A Property wrapper around the <code>rotation</code> functionality handled by the
16142 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16143 */
Chet Haased47f1532011-12-16 11:18:52 -080016144 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016145 @Override
16146 public void setValue(View object, float value) {
16147 object.setRotation(value);
16148 }
16149
16150 @Override
16151 public Float get(View object) {
16152 return object.getRotation();
16153 }
16154 };
16155
16156 /**
16157 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16158 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16159 */
Chet Haased47f1532011-12-16 11:18:52 -080016160 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016161 @Override
16162 public void setValue(View object, float value) {
16163 object.setRotationX(value);
16164 }
16165
16166 @Override
16167 public Float get(View object) {
16168 return object.getRotationX();
16169 }
16170 };
16171
16172 /**
16173 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16174 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16175 */
Chet Haased47f1532011-12-16 11:18:52 -080016176 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016177 @Override
16178 public void setValue(View object, float value) {
16179 object.setRotationY(value);
16180 }
16181
16182 @Override
16183 public Float get(View object) {
16184 return object.getRotationY();
16185 }
16186 };
16187
16188 /**
16189 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16190 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16191 */
Chet Haased47f1532011-12-16 11:18:52 -080016192 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016193 @Override
16194 public void setValue(View object, float value) {
16195 object.setScaleX(value);
16196 }
16197
16198 @Override
16199 public Float get(View object) {
16200 return object.getScaleX();
16201 }
16202 };
16203
16204 /**
16205 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16206 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16207 */
Chet Haased47f1532011-12-16 11:18:52 -080016208 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016209 @Override
16210 public void setValue(View object, float value) {
16211 object.setScaleY(value);
16212 }
16213
16214 @Override
16215 public Float get(View object) {
16216 return object.getScaleY();
16217 }
16218 };
16219
Jeff Brown33bbfd22011-02-24 20:55:35 -080016220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016221 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16222 * Each MeasureSpec represents a requirement for either the width or the height.
16223 * A MeasureSpec is comprised of a size and a mode. There are three possible
16224 * modes:
16225 * <dl>
16226 * <dt>UNSPECIFIED</dt>
16227 * <dd>
16228 * The parent has not imposed any constraint on the child. It can be whatever size
16229 * it wants.
16230 * </dd>
16231 *
16232 * <dt>EXACTLY</dt>
16233 * <dd>
16234 * The parent has determined an exact size for the child. The child is going to be
16235 * given those bounds regardless of how big it wants to be.
16236 * </dd>
16237 *
16238 * <dt>AT_MOST</dt>
16239 * <dd>
16240 * The child can be as large as it wants up to the specified size.
16241 * </dd>
16242 * </dl>
16243 *
16244 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16245 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16246 */
16247 public static class MeasureSpec {
16248 private static final int MODE_SHIFT = 30;
16249 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16250
16251 /**
16252 * Measure specification mode: The parent has not imposed any constraint
16253 * on the child. It can be whatever size it wants.
16254 */
16255 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16256
16257 /**
16258 * Measure specification mode: The parent has determined an exact size
16259 * for the child. The child is going to be given those bounds regardless
16260 * of how big it wants to be.
16261 */
16262 public static final int EXACTLY = 1 << MODE_SHIFT;
16263
16264 /**
16265 * Measure specification mode: The child can be as large as it wants up
16266 * to the specified size.
16267 */
16268 public static final int AT_MOST = 2 << MODE_SHIFT;
16269
16270 /**
16271 * Creates a measure specification based on the supplied size and mode.
16272 *
16273 * The mode must always be one of the following:
16274 * <ul>
16275 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16276 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16277 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16278 * </ul>
16279 *
16280 * @param size the size of the measure specification
16281 * @param mode the mode of the measure specification
16282 * @return the measure specification based on size and mode
16283 */
16284 public static int makeMeasureSpec(int size, int mode) {
16285 return size + mode;
16286 }
16287
16288 /**
16289 * Extracts the mode from the supplied measure specification.
16290 *
16291 * @param measureSpec the measure specification to extract the mode from
16292 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16293 * {@link android.view.View.MeasureSpec#AT_MOST} or
16294 * {@link android.view.View.MeasureSpec#EXACTLY}
16295 */
16296 public static int getMode(int measureSpec) {
16297 return (measureSpec & MODE_MASK);
16298 }
16299
16300 /**
16301 * Extracts the size from the supplied measure specification.
16302 *
16303 * @param measureSpec the measure specification to extract the size from
16304 * @return the size in pixels defined in the supplied measure specification
16305 */
16306 public static int getSize(int measureSpec) {
16307 return (measureSpec & ~MODE_MASK);
16308 }
16309
16310 /**
16311 * Returns a String representation of the specified measure
16312 * specification.
16313 *
16314 * @param measureSpec the measure specification to convert to a String
16315 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16316 */
16317 public static String toString(int measureSpec) {
16318 int mode = getMode(measureSpec);
16319 int size = getSize(measureSpec);
16320
16321 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16322
16323 if (mode == UNSPECIFIED)
16324 sb.append("UNSPECIFIED ");
16325 else if (mode == EXACTLY)
16326 sb.append("EXACTLY ");
16327 else if (mode == AT_MOST)
16328 sb.append("AT_MOST ");
16329 else
16330 sb.append(mode).append(" ");
16331
16332 sb.append(size);
16333 return sb.toString();
16334 }
16335 }
16336
16337 class CheckForLongPress implements Runnable {
16338
16339 private int mOriginalWindowAttachCount;
16340
16341 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016342 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016343 && mOriginalWindowAttachCount == mWindowAttachCount) {
16344 if (performLongClick()) {
16345 mHasPerformedLongPress = true;
16346 }
16347 }
16348 }
16349
16350 public void rememberWindowAttachCount() {
16351 mOriginalWindowAttachCount = mWindowAttachCount;
16352 }
16353 }
Joe Malin32736f02011-01-19 16:14:20 -080016354
Adam Powelle14579b2009-12-16 18:39:52 -080016355 private final class CheckForTap implements Runnable {
16356 public void run() {
16357 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016358 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016359 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016360 }
16361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016362
Adam Powella35d7682010-03-12 14:48:13 -080016363 private final class PerformClick implements Runnable {
16364 public void run() {
16365 performClick();
16366 }
16367 }
16368
Dianne Hackborn63042d62011-01-26 18:56:29 -080016369 /** @hide */
16370 public void hackTurnOffWindowResizeAnim(boolean off) {
16371 mAttachInfo.mTurnOffWindowResizeAnim = off;
16372 }
Joe Malin32736f02011-01-19 16:14:20 -080016373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016374 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016375 * This method returns a ViewPropertyAnimator object, which can be used to animate
16376 * specific properties on this View.
16377 *
16378 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16379 */
16380 public ViewPropertyAnimator animate() {
16381 if (mAnimator == null) {
16382 mAnimator = new ViewPropertyAnimator(this);
16383 }
16384 return mAnimator;
16385 }
16386
16387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016388 * Interface definition for a callback to be invoked when a key event is
16389 * dispatched to this view. The callback will be invoked before the key
16390 * event is given to the view.
16391 */
16392 public interface OnKeyListener {
16393 /**
16394 * Called when a key is dispatched to a view. This allows listeners to
16395 * get a chance to respond before the target view.
16396 *
16397 * @param v The view the key has been dispatched to.
16398 * @param keyCode The code for the physical key that was pressed
16399 * @param event The KeyEvent object containing full information about
16400 * the event.
16401 * @return True if the listener has consumed the event, false otherwise.
16402 */
16403 boolean onKey(View v, int keyCode, KeyEvent event);
16404 }
16405
16406 /**
16407 * Interface definition for a callback to be invoked when a touch event is
16408 * dispatched to this view. The callback will be invoked before the touch
16409 * event is given to the view.
16410 */
16411 public interface OnTouchListener {
16412 /**
16413 * Called when a touch event is dispatched to a view. This allows listeners to
16414 * get a chance to respond before the target view.
16415 *
16416 * @param v The view the touch event has been dispatched to.
16417 * @param event The MotionEvent object containing full information about
16418 * the event.
16419 * @return True if the listener has consumed the event, false otherwise.
16420 */
16421 boolean onTouch(View v, MotionEvent event);
16422 }
16423
16424 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016425 * Interface definition for a callback to be invoked when a hover event is
16426 * dispatched to this view. The callback will be invoked before the hover
16427 * event is given to the view.
16428 */
16429 public interface OnHoverListener {
16430 /**
16431 * Called when a hover event is dispatched to a view. This allows listeners to
16432 * get a chance to respond before the target view.
16433 *
16434 * @param v The view the hover event has been dispatched to.
16435 * @param event The MotionEvent object containing full information about
16436 * the event.
16437 * @return True if the listener has consumed the event, false otherwise.
16438 */
16439 boolean onHover(View v, MotionEvent event);
16440 }
16441
16442 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016443 * Interface definition for a callback to be invoked when a generic motion event is
16444 * dispatched to this view. The callback will be invoked before the generic motion
16445 * event is given to the view.
16446 */
16447 public interface OnGenericMotionListener {
16448 /**
16449 * Called when a generic motion event is dispatched to a view. This allows listeners to
16450 * get a chance to respond before the target view.
16451 *
16452 * @param v The view the generic motion event has been dispatched to.
16453 * @param event The MotionEvent object containing full information about
16454 * the event.
16455 * @return True if the listener has consumed the event, false otherwise.
16456 */
16457 boolean onGenericMotion(View v, MotionEvent event);
16458 }
16459
16460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016461 * Interface definition for a callback to be invoked when a view has been clicked and held.
16462 */
16463 public interface OnLongClickListener {
16464 /**
16465 * Called when a view has been clicked and held.
16466 *
16467 * @param v The view that was clicked and held.
16468 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016469 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016470 */
16471 boolean onLongClick(View v);
16472 }
16473
16474 /**
Chris Tate32affef2010-10-18 15:29:21 -070016475 * Interface definition for a callback to be invoked when a drag is being dispatched
16476 * to this view. The callback will be invoked before the hosting view's own
16477 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16478 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016479 *
16480 * <div class="special reference">
16481 * <h3>Developer Guides</h3>
16482 * <p>For a guide to implementing drag and drop features, read the
16483 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16484 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016485 */
16486 public interface OnDragListener {
16487 /**
16488 * Called when a drag event is dispatched to a view. This allows listeners
16489 * to get a chance to override base View behavior.
16490 *
Joe Malin32736f02011-01-19 16:14:20 -080016491 * @param v The View that received the drag event.
16492 * @param event The {@link android.view.DragEvent} object for the drag event.
16493 * @return {@code true} if the drag event was handled successfully, or {@code false}
16494 * if the drag event was not handled. Note that {@code false} will trigger the View
16495 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016496 */
16497 boolean onDrag(View v, DragEvent event);
16498 }
16499
16500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016501 * Interface definition for a callback to be invoked when the focus state of
16502 * a view changed.
16503 */
16504 public interface OnFocusChangeListener {
16505 /**
16506 * Called when the focus state of a view has changed.
16507 *
16508 * @param v The view whose state has changed.
16509 * @param hasFocus The new focus state of v.
16510 */
16511 void onFocusChange(View v, boolean hasFocus);
16512 }
16513
16514 /**
16515 * Interface definition for a callback to be invoked when a view is clicked.
16516 */
16517 public interface OnClickListener {
16518 /**
16519 * Called when a view has been clicked.
16520 *
16521 * @param v The view that was clicked.
16522 */
16523 void onClick(View v);
16524 }
16525
16526 /**
16527 * Interface definition for a callback to be invoked when the context menu
16528 * for this view is being built.
16529 */
16530 public interface OnCreateContextMenuListener {
16531 /**
16532 * Called when the context menu for this view is being built. It is not
16533 * safe to hold onto the menu after this method returns.
16534 *
16535 * @param menu The context menu that is being built
16536 * @param v The view for which the context menu is being built
16537 * @param menuInfo Extra information about the item for which the
16538 * context menu should be shown. This information will vary
16539 * depending on the class of v.
16540 */
16541 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16542 }
16543
Joe Onorato664644d2011-01-23 17:53:23 -080016544 /**
16545 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016546 * visibility. This reports <strong>global</strong> changes to the system UI
16547 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016548 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016549 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016550 */
16551 public interface OnSystemUiVisibilityChangeListener {
16552 /**
16553 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016554 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016555 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016556 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016557 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16558 * <strong>global</strong> state of the UI visibility flags, not what your
16559 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016560 */
16561 public void onSystemUiVisibilityChange(int visibility);
16562 }
16563
Adam Powell4afd62b2011-02-18 15:02:18 -080016564 /**
16565 * Interface definition for a callback to be invoked when this view is attached
16566 * or detached from its window.
16567 */
16568 public interface OnAttachStateChangeListener {
16569 /**
16570 * Called when the view is attached to a window.
16571 * @param v The view that was attached
16572 */
16573 public void onViewAttachedToWindow(View v);
16574 /**
16575 * Called when the view is detached from a window.
16576 * @param v The view that was detached
16577 */
16578 public void onViewDetachedFromWindow(View v);
16579 }
16580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016581 private final class UnsetPressedState implements Runnable {
16582 public void run() {
16583 setPressed(false);
16584 }
16585 }
16586
16587 /**
16588 * Base class for derived classes that want to save and restore their own
16589 * state in {@link android.view.View#onSaveInstanceState()}.
16590 */
16591 public static class BaseSavedState extends AbsSavedState {
16592 /**
16593 * Constructor used when reading from a parcel. Reads the state of the superclass.
16594 *
16595 * @param source
16596 */
16597 public BaseSavedState(Parcel source) {
16598 super(source);
16599 }
16600
16601 /**
16602 * Constructor called by derived classes when creating their SavedState objects
16603 *
16604 * @param superState The state of the superclass of this view
16605 */
16606 public BaseSavedState(Parcelable superState) {
16607 super(superState);
16608 }
16609
16610 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16611 new Parcelable.Creator<BaseSavedState>() {
16612 public BaseSavedState createFromParcel(Parcel in) {
16613 return new BaseSavedState(in);
16614 }
16615
16616 public BaseSavedState[] newArray(int size) {
16617 return new BaseSavedState[size];
16618 }
16619 };
16620 }
16621
16622 /**
16623 * A set of information given to a view when it is attached to its parent
16624 * window.
16625 */
16626 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016627 interface Callbacks {
16628 void playSoundEffect(int effectId);
16629 boolean performHapticFeedback(int effectId, boolean always);
16630 }
16631
16632 /**
16633 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16634 * to a Handler. This class contains the target (View) to invalidate and
16635 * the coordinates of the dirty rectangle.
16636 *
16637 * For performance purposes, this class also implements a pool of up to
16638 * POOL_LIMIT objects that get reused. This reduces memory allocations
16639 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016640 */
Romain Guyd928d682009-03-31 17:52:16 -070016641 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016642 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016643 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16644 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016645 public InvalidateInfo newInstance() {
16646 return new InvalidateInfo();
16647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016648
Romain Guyd928d682009-03-31 17:52:16 -070016649 public void onAcquired(InvalidateInfo element) {
16650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016651
Romain Guyd928d682009-03-31 17:52:16 -070016652 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016653 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016654 }
16655 }, POOL_LIMIT)
16656 );
16657
16658 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016659 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016660
16661 View target;
16662
16663 int left;
16664 int top;
16665 int right;
16666 int bottom;
16667
Romain Guyd928d682009-03-31 17:52:16 -070016668 public void setNextPoolable(InvalidateInfo element) {
16669 mNext = element;
16670 }
16671
16672 public InvalidateInfo getNextPoolable() {
16673 return mNext;
16674 }
16675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016676 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016677 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016678 }
16679
16680 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016681 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016682 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016683
16684 public boolean isPooled() {
16685 return mIsPooled;
16686 }
16687
16688 public void setPooled(boolean isPooled) {
16689 mIsPooled = isPooled;
16690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016691 }
16692
16693 final IWindowSession mSession;
16694
16695 final IWindow mWindow;
16696
16697 final IBinder mWindowToken;
16698
16699 final Callbacks mRootCallbacks;
16700
Romain Guy59a12ca2011-06-09 17:48:21 -070016701 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016703 /**
16704 * The top view of the hierarchy.
16705 */
16706 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016708 IBinder mPanelParentWindowToken;
16709 Surface mSurface;
16710
Romain Guyb051e892010-09-28 19:09:36 -070016711 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016712 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016713 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016714
Romain Guy7e4e5612012-03-05 14:37:29 -080016715 boolean mScreenOn;
16716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016717 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016718 * Scale factor used by the compatibility mode
16719 */
16720 float mApplicationScale;
16721
16722 /**
16723 * Indicates whether the application is in compatibility mode
16724 */
16725 boolean mScalingRequired;
16726
16727 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016728 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016729 */
16730 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016731
Dianne Hackborn63042d62011-01-26 18:56:29 -080016732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016733 * Left position of this view's window
16734 */
16735 int mWindowLeft;
16736
16737 /**
16738 * Top position of this view's window
16739 */
16740 int mWindowTop;
16741
16742 /**
Adam Powell26153a32010-11-08 15:22:27 -080016743 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016744 */
Adam Powell26153a32010-11-08 15:22:27 -080016745 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016746
16747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016748 * For windows that are full-screen but using insets to layout inside
16749 * of the screen decorations, these are the current insets for the
16750 * content of the window.
16751 */
16752 final Rect mContentInsets = new Rect();
16753
16754 /**
16755 * For windows that are full-screen but using insets to layout inside
16756 * of the screen decorations, these are the current insets for the
16757 * actual visible parts of the window.
16758 */
16759 final Rect mVisibleInsets = new Rect();
16760
16761 /**
16762 * The internal insets given by this window. This value is
16763 * supplied by the client (through
16764 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16765 * be given to the window manager when changed to be used in laying
16766 * out windows behind it.
16767 */
16768 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16769 = new ViewTreeObserver.InternalInsetsInfo();
16770
16771 /**
16772 * All views in the window's hierarchy that serve as scroll containers,
16773 * used to determine if the window can be resized or must be panned
16774 * to adjust for a soft input area.
16775 */
16776 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16777
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016778 final KeyEvent.DispatcherState mKeyDispatchState
16779 = new KeyEvent.DispatcherState();
16780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016781 /**
16782 * Indicates whether the view's window currently has the focus.
16783 */
16784 boolean mHasWindowFocus;
16785
16786 /**
16787 * The current visibility of the window.
16788 */
16789 int mWindowVisibility;
16790
16791 /**
16792 * Indicates the time at which drawing started to occur.
16793 */
16794 long mDrawingTime;
16795
16796 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016797 * Indicates whether or not ignoring the DIRTY_MASK flags.
16798 */
16799 boolean mIgnoreDirtyState;
16800
16801 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016802 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16803 * to avoid clearing that flag prematurely.
16804 */
16805 boolean mSetIgnoreDirtyState = false;
16806
16807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016808 * Indicates whether the view's window is currently in touch mode.
16809 */
16810 boolean mInTouchMode;
16811
16812 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016813 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016814 * the next time it performs a traversal
16815 */
16816 boolean mRecomputeGlobalAttributes;
16817
16818 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016819 * Always report new attributes at next traversal.
16820 */
16821 boolean mForceReportNewAttributes;
16822
16823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016824 * Set during a traveral if any views want to keep the screen on.
16825 */
16826 boolean mKeepScreenOn;
16827
16828 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016829 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16830 */
16831 int mSystemUiVisibility;
16832
16833 /**
16834 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16835 * attached.
16836 */
16837 boolean mHasSystemUiListeners;
16838
16839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016840 * Set if the visibility of any views has changed.
16841 */
16842 boolean mViewVisibilityChanged;
16843
16844 /**
16845 * Set to true if a view has been scrolled.
16846 */
16847 boolean mViewScrollChanged;
16848
16849 /**
16850 * Global to the view hierarchy used as a temporary for dealing with
16851 * x/y points in the transparent region computations.
16852 */
16853 final int[] mTransparentLocation = new int[2];
16854
16855 /**
16856 * Global to the view hierarchy used as a temporary for dealing with
16857 * x/y points in the ViewGroup.invalidateChild implementation.
16858 */
16859 final int[] mInvalidateChildLocation = new int[2];
16860
Chet Haasec3aa3612010-06-17 08:50:37 -070016861
16862 /**
16863 * Global to the view hierarchy used as a temporary for dealing with
16864 * x/y location when view is transformed.
16865 */
16866 final float[] mTmpTransformLocation = new float[2];
16867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016868 /**
16869 * The view tree observer used to dispatch global events like
16870 * layout, pre-draw, touch mode change, etc.
16871 */
16872 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16873
16874 /**
16875 * A Canvas used by the view hierarchy to perform bitmap caching.
16876 */
16877 Canvas mCanvas;
16878
16879 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016880 * The view root impl.
16881 */
16882 final ViewRootImpl mViewRootImpl;
16883
16884 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016885 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016886 * handler can be used to pump events in the UI events queue.
16887 */
16888 final Handler mHandler;
16889
16890 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016891 * Temporary for use in computing invalidate rectangles while
16892 * calling up the hierarchy.
16893 */
16894 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016895
16896 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016897 * Temporary for use in computing hit areas with transformed views
16898 */
16899 final RectF mTmpTransformRect = new RectF();
16900
16901 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016902 * Temporary list for use in collecting focusable descendents of a view.
16903 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016904 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016906 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016907 * The id of the window for accessibility purposes.
16908 */
16909 int mAccessibilityWindowId = View.NO_ID;
16910
16911 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016912 * Whether to ingore not exposed for accessibility Views when
16913 * reporting the view tree to accessibility services.
16914 */
16915 boolean mIncludeNotImportantViews;
16916
16917 /**
16918 * The drawable for highlighting accessibility focus.
16919 */
16920 Drawable mAccessibilityFocusDrawable;
16921
16922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 * Creates a new set of attachment information with the specified
16924 * events handler and thread.
16925 *
16926 * @param handler the events handler the view must use
16927 */
16928 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016929 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016930 mSession = session;
16931 mWindow = window;
16932 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016933 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016934 mHandler = handler;
16935 mRootCallbacks = effectPlayer;
16936 }
16937 }
16938
16939 /**
16940 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16941 * is supported. This avoids keeping too many unused fields in most
16942 * instances of View.</p>
16943 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016944 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016945
Mike Cleronf116bf82009-09-27 19:14:12 -070016946 /**
16947 * Scrollbars are not visible
16948 */
16949 public static final int OFF = 0;
16950
16951 /**
16952 * Scrollbars are visible
16953 */
16954 public static final int ON = 1;
16955
16956 /**
16957 * Scrollbars are fading away
16958 */
16959 public static final int FADING = 2;
16960
16961 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016963 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016964 public int scrollBarDefaultDelayBeforeFade;
16965 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016966
16967 public int scrollBarSize;
16968 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016969 public float[] interpolatorValues;
16970 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016971
16972 public final Paint paint;
16973 public final Matrix matrix;
16974 public Shader shader;
16975
Mike Cleronf116bf82009-09-27 19:14:12 -070016976 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16977
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016978 private static final float[] OPAQUE = { 255 };
16979 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016980
Mike Cleronf116bf82009-09-27 19:14:12 -070016981 /**
16982 * When fading should start. This time moves into the future every time
16983 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16984 */
16985 public long fadeStartTime;
16986
16987
16988 /**
16989 * The current state of the scrollbars: ON, OFF, or FADING
16990 */
16991 public int state = OFF;
16992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016993 private int mLastColor;
16994
Mike Cleronf116bf82009-09-27 19:14:12 -070016995 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016996 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16997 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016998 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16999 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017000
17001 paint = new Paint();
17002 matrix = new Matrix();
17003 // use use a height of 1, and then wack the matrix each time we
17004 // actually use it.
17005 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017007 paint.setShader(shader);
17008 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017009 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017010 }
Romain Guy8506ab42009-06-11 17:35:47 -070017011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017012 public void setFadeColor(int color) {
17013 if (color != 0 && color != mLastColor) {
17014 mLastColor = color;
17015 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017016
Romain Guye55e1a72009-08-27 10:42:26 -070017017 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17018 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017020 paint.setShader(shader);
17021 // Restore the default transfer mode (src_over)
17022 paint.setXfermode(null);
17023 }
17024 }
Joe Malin32736f02011-01-19 16:14:20 -080017025
Mike Cleronf116bf82009-09-27 19:14:12 -070017026 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017027 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017028 if (now >= fadeStartTime) {
17029
17030 // the animation fades the scrollbars out by changing
17031 // the opacity (alpha) from fully opaque to fully
17032 // transparent
17033 int nextFrame = (int) now;
17034 int framesCount = 0;
17035
17036 Interpolator interpolator = scrollBarInterpolator;
17037
17038 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017039 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017040
17041 // End transparent
17042 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017043 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017044
17045 state = FADING;
17046
17047 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017048 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017049 }
17050 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017051 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017052
Svetoslav Ganova0156172011-06-26 17:55:44 -070017053 /**
17054 * Resuable callback for sending
17055 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17056 */
17057 private class SendViewScrolledAccessibilityEvent implements Runnable {
17058 public volatile boolean mIsPending;
17059
17060 public void run() {
17061 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17062 mIsPending = false;
17063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017065
17066 /**
17067 * <p>
17068 * This class represents a delegate that can be registered in a {@link View}
17069 * to enhance accessibility support via composition rather via inheritance.
17070 * It is specifically targeted to widget developers that extend basic View
17071 * classes i.e. classes in package android.view, that would like their
17072 * applications to be backwards compatible.
17073 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017074 * <div class="special reference">
17075 * <h3>Developer Guides</h3>
17076 * <p>For more information about making applications accessible, read the
17077 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17078 * developer guide.</p>
17079 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017080 * <p>
17081 * A scenario in which a developer would like to use an accessibility delegate
17082 * is overriding a method introduced in a later API version then the minimal API
17083 * version supported by the application. For example, the method
17084 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17085 * in API version 4 when the accessibility APIs were first introduced. If a
17086 * developer would like his application to run on API version 4 devices (assuming
17087 * all other APIs used by the application are version 4 or lower) and take advantage
17088 * of this method, instead of overriding the method which would break the application's
17089 * backwards compatibility, he can override the corresponding method in this
17090 * delegate and register the delegate in the target View if the API version of
17091 * the system is high enough i.e. the API version is same or higher to the API
17092 * version that introduced
17093 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17094 * </p>
17095 * <p>
17096 * Here is an example implementation:
17097 * </p>
17098 * <code><pre><p>
17099 * if (Build.VERSION.SDK_INT >= 14) {
17100 * // If the API version is equal of higher than the version in
17101 * // which onInitializeAccessibilityNodeInfo was introduced we
17102 * // register a delegate with a customized implementation.
17103 * View view = findViewById(R.id.view_id);
17104 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17105 * public void onInitializeAccessibilityNodeInfo(View host,
17106 * AccessibilityNodeInfo info) {
17107 * // Let the default implementation populate the info.
17108 * super.onInitializeAccessibilityNodeInfo(host, info);
17109 * // Set some other information.
17110 * info.setEnabled(host.isEnabled());
17111 * }
17112 * });
17113 * }
17114 * </code></pre></p>
17115 * <p>
17116 * This delegate contains methods that correspond to the accessibility methods
17117 * in View. If a delegate has been specified the implementation in View hands
17118 * off handling to the corresponding method in this delegate. The default
17119 * implementation the delegate methods behaves exactly as the corresponding
17120 * method in View for the case of no accessibility delegate been set. Hence,
17121 * to customize the behavior of a View method, clients can override only the
17122 * corresponding delegate method without altering the behavior of the rest
17123 * accessibility related methods of the host view.
17124 * </p>
17125 */
17126 public static class AccessibilityDelegate {
17127
17128 /**
17129 * Sends an accessibility event of the given type. If accessibility is not
17130 * enabled this method has no effect.
17131 * <p>
17132 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17133 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17134 * been set.
17135 * </p>
17136 *
17137 * @param host The View hosting the delegate.
17138 * @param eventType The type of the event to send.
17139 *
17140 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17141 */
17142 public void sendAccessibilityEvent(View host, int eventType) {
17143 host.sendAccessibilityEventInternal(eventType);
17144 }
17145
17146 /**
17147 * Sends an accessibility event. This method behaves exactly as
17148 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17149 * empty {@link AccessibilityEvent} and does not perform a check whether
17150 * accessibility is enabled.
17151 * <p>
17152 * The default implementation behaves as
17153 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17154 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17155 * the case of no accessibility delegate been set.
17156 * </p>
17157 *
17158 * @param host The View hosting the delegate.
17159 * @param event The event to send.
17160 *
17161 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17162 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17163 */
17164 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17165 host.sendAccessibilityEventUncheckedInternal(event);
17166 }
17167
17168 /**
17169 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17170 * to its children for adding their text content to the event.
17171 * <p>
17172 * The default implementation behaves as
17173 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17174 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17175 * the case of no accessibility delegate been set.
17176 * </p>
17177 *
17178 * @param host The View hosting the delegate.
17179 * @param event The event.
17180 * @return True if the event population was completed.
17181 *
17182 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17183 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17184 */
17185 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17186 return host.dispatchPopulateAccessibilityEventInternal(event);
17187 }
17188
17189 /**
17190 * Gives a chance to the host View to populate the accessibility event with its
17191 * text content.
17192 * <p>
17193 * The default implementation behaves as
17194 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17195 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17196 * the case of no accessibility delegate been set.
17197 * </p>
17198 *
17199 * @param host The View hosting the delegate.
17200 * @param event The accessibility event which to populate.
17201 *
17202 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17203 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17204 */
17205 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17206 host.onPopulateAccessibilityEventInternal(event);
17207 }
17208
17209 /**
17210 * Initializes an {@link AccessibilityEvent} with information about the
17211 * the host View which is the event source.
17212 * <p>
17213 * The default implementation behaves as
17214 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17215 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17216 * the case of no accessibility delegate been set.
17217 * </p>
17218 *
17219 * @param host The View hosting the delegate.
17220 * @param event The event to initialize.
17221 *
17222 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17223 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17224 */
17225 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17226 host.onInitializeAccessibilityEventInternal(event);
17227 }
17228
17229 /**
17230 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17231 * <p>
17232 * The default implementation behaves as
17233 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17234 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17235 * the case of no accessibility delegate been set.
17236 * </p>
17237 *
17238 * @param host The View hosting the delegate.
17239 * @param info The instance to initialize.
17240 *
17241 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17242 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17243 */
17244 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17245 host.onInitializeAccessibilityNodeInfoInternal(info);
17246 }
17247
17248 /**
17249 * Called when a child of the host View has requested sending an
17250 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17251 * to augment the event.
17252 * <p>
17253 * The default implementation behaves as
17254 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17255 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17256 * the case of no accessibility delegate been set.
17257 * </p>
17258 *
17259 * @param host The View hosting the delegate.
17260 * @param child The child which requests sending the event.
17261 * @param event The event to be sent.
17262 * @return True if the event should be sent
17263 *
17264 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17265 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17266 */
17267 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17268 AccessibilityEvent event) {
17269 return host.onRequestSendAccessibilityEventInternal(child, event);
17270 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017271
17272 /**
17273 * Gets the provider for managing a virtual view hierarchy rooted at this View
17274 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17275 * that explore the window content.
17276 * <p>
17277 * The default implementation behaves as
17278 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17279 * the case of no accessibility delegate been set.
17280 * </p>
17281 *
17282 * @return The provider.
17283 *
17284 * @see AccessibilityNodeProvider
17285 */
17286 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17287 return null;
17288 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017290}