blob: 461e7f15b16816501d792cf040da3285ac77039f [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;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070050import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070052import android.util.FloatProperty;
53import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070059import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityEventSource;
65import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070066import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070067import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080070import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070071import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.inputmethod.InputConnection;
73import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.ScrollBarDrawable;
75
Romain Guy1ef3fdb2011-09-09 15:30:30 -070076import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070077import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070078
Doug Feltcb3791202011-07-07 11:57:48 -070079import com.android.internal.R;
80import com.android.internal.util.Predicate;
81import com.android.internal.view.menu.MenuBuilder;
82
Christopher Tatea0374192010-10-05 13:06:41 -070083import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070084import java.lang.reflect.InvocationTargetException;
85import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.ArrayList;
87import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070088import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080089import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * <p>
93 * This class represents the basic building block for user interface components. A View
94 * occupies a rectangular area on the screen and is responsible for drawing and
95 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070096 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
98 * are invisible containers that hold other Views (or other ViewGroups) and define
99 * their layout properties.
100 * </p>
101 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700102 * <div class="special reference">
103 * <h3>Developer Guides</h3>
104 * <p>For information about using this class to develop your application's user interface,
105 * 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 -0800106 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * <a name="Using"></a>
109 * <h3>Using Views</h3>
110 * <p>
111 * All of the views in a window are arranged in a single tree. You can add views
112 * either from code or by specifying a tree of views in one or more XML layout
113 * files. There are many specialized subclasses of views that act as controls or
114 * are capable of displaying text, images, or other content.
115 * </p>
116 * <p>
117 * Once you have created a tree of views, there are typically a few types of
118 * common operations you may wish to perform:
119 * <ul>
120 * <li><strong>Set properties:</strong> for example setting the text of a
121 * {@link android.widget.TextView}. The available properties and the methods
122 * that set them will vary among the different subclasses of views. Note that
123 * properties that are known at build time can be set in the XML layout
124 * files.</li>
125 * <li><strong>Set focus:</strong> The framework will handled moving focus in
126 * response to user input. To force focus to a specific view, call
127 * {@link #requestFocus}.</li>
128 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
129 * that will be notified when something interesting happens to the view. For
130 * example, all views will let you set a listener to be notified when the view
131 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700133 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700136 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * </ul>
138 * </p>
139 * <p><em>
140 * Note: The Android framework is responsible for measuring, laying out and
141 * drawing views. You should not call methods that perform these actions on
142 * views yourself unless you are actually implementing a
143 * {@link android.view.ViewGroup}.
144 * </em></p>
145 *
146 * <a name="Lifecycle"></a>
147 * <h3>Implementing a Custom View</h3>
148 *
149 * <p>
150 * To implement a custom view, you will usually begin by providing overrides for
151 * some of the standard methods that the framework calls on all views. You do
152 * not need to override all of these methods. In fact, you can start by just
153 * overriding {@link #onDraw(android.graphics.Canvas)}.
154 * <table border="2" width="85%" align="center" cellpadding="5">
155 * <thead>
156 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
157 * </thead>
158 *
159 * <tbody>
160 * <tr>
161 * <td rowspan="2">Creation</td>
162 * <td>Constructors</td>
163 * <td>There is a form of the constructor that are called when the view
164 * is created from code and a form that is called when the view is
165 * inflated from a layout file. The second form should parse and apply
166 * any attributes defined in the layout file.
167 * </td>
168 * </tr>
169 * <tr>
170 * <td><code>{@link #onFinishInflate()}</code></td>
171 * <td>Called after a view and all of its children has been inflated
172 * from XML.</td>
173 * </tr>
174 *
175 * <tr>
176 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700177 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * <td>Called to determine the size requirements for this view and all
179 * of its children.
180 * </td>
181 * </tr>
182 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700183 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <td>Called when this view should assign a size and position to all
185 * of its children.
186 * </td>
187 * </tr>
188 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700189 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * <td>Called when the size of this view has changed.
191 * </td>
192 * </tr>
193 *
194 * <tr>
195 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700196 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * <td>Called when the view should render its content.
198 * </td>
199 * </tr>
200 *
201 * <tr>
202 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700203 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <td>Called when a new key event occurs.
205 * </td>
206 * </tr>
207 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a key up event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a trackball motion event occurs.
215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a touch screen motion event occurs.
220 * </td>
221 * </tr>
222 *
223 * <tr>
224 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700225 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * <td>Called when the view gains or loses focus.
227 * </td>
228 * </tr>
229 *
230 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700231 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * <td>Called when the window containing the view gains or loses focus.
233 * </td>
234 * </tr>
235 *
236 * <tr>
237 * <td rowspan="3">Attaching</td>
238 * <td><code>{@link #onAttachedToWindow()}</code></td>
239 * <td>Called when the view is attached to a window.
240 * </td>
241 * </tr>
242 *
243 * <tr>
244 * <td><code>{@link #onDetachedFromWindow}</code></td>
245 * <td>Called when the view is detached from its window.
246 * </td>
247 * </tr>
248 *
249 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700250 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * <td>Called when the visibility of the window containing the view
252 * has changed.
253 * </td>
254 * </tr>
255 * </tbody>
256 *
257 * </table>
258 * </p>
259 *
260 * <a name="IDs"></a>
261 * <h3>IDs</h3>
262 * Views may have an integer id associated with them. These ids are typically
263 * assigned in the layout XML files, and are used to find specific views within
264 * the view tree. A common pattern is to:
265 * <ul>
266 * <li>Define a Button in the layout file and assign it a unique ID.
267 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700268 * &lt;Button
269 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * android:layout_width="wrap_content"
271 * android:layout_height="wrap_content"
272 * android:text="@string/my_button_text"/&gt;
273 * </pre></li>
274 * <li>From the onCreate method of an Activity, find the Button
275 * <pre class="prettyprint">
276 * Button myButton = (Button) findViewById(R.id.my_button);
277 * </pre></li>
278 * </ul>
279 * <p>
280 * View IDs need not be unique throughout the tree, but it is good practice to
281 * ensure that they are at least unique within the part of the tree you are
282 * searching.
283 * </p>
284 *
285 * <a name="Position"></a>
286 * <h3>Position</h3>
287 * <p>
288 * The geometry of a view is that of a rectangle. A view has a location,
289 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
290 * two dimensions, expressed as a width and a height. The unit for location
291 * and dimensions is the pixel.
292 * </p>
293 *
294 * <p>
295 * It is possible to retrieve the location of a view by invoking the methods
296 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
297 * coordinate of the rectangle representing the view. The latter returns the
298 * top, or Y, coordinate of the rectangle representing the view. These methods
299 * both return the location of the view relative to its parent. For instance,
300 * when getLeft() returns 20, that means the view is located 20 pixels to the
301 * right of the left edge of its direct parent.
302 * </p>
303 *
304 * <p>
305 * In addition, several convenience methods are offered to avoid unnecessary
306 * computations, namely {@link #getRight()} and {@link #getBottom()}.
307 * These methods return the coordinates of the right and bottom edges of the
308 * rectangle representing the view. For instance, calling {@link #getRight()}
309 * is similar to the following computation: <code>getLeft() + getWidth()</code>
310 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
311 * </p>
312 *
313 * <a name="SizePaddingMargins"></a>
314 * <h3>Size, padding and margins</h3>
315 * <p>
316 * The size of a view is expressed with a width and a height. A view actually
317 * possess two pairs of width and height values.
318 * </p>
319 *
320 * <p>
321 * The first pair is known as <em>measured width</em> and
322 * <em>measured height</em>. These dimensions define how big a view wants to be
323 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
324 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
325 * and {@link #getMeasuredHeight()}.
326 * </p>
327 *
328 * <p>
329 * The second pair is simply known as <em>width</em> and <em>height</em>, or
330 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
331 * dimensions define the actual size of the view on screen, at drawing time and
332 * after layout. These values may, but do not have to, be different from the
333 * measured width and height. The width and height can be obtained by calling
334 * {@link #getWidth()} and {@link #getHeight()}.
335 * </p>
336 *
337 * <p>
338 * To measure its dimensions, a view takes into account its padding. The padding
339 * is expressed in pixels for the left, top, right and bottom parts of the view.
340 * Padding can be used to offset the content of the view by a specific amount of
341 * pixels. For instance, a left padding of 2 will push the view's content by
342 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700343 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
344 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
345 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
346 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * </p>
348 *
349 * <p>
350 * Even though a view can define a padding, it does not provide any support for
351 * margins. However, view groups provide such a support. Refer to
352 * {@link android.view.ViewGroup} and
353 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
354 * </p>
355 *
356 * <a name="Layout"></a>
357 * <h3>Layout</h3>
358 * <p>
359 * Layout is a two pass process: a measure pass and a layout pass. The measuring
360 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
361 * of the view tree. Each view pushes dimension specifications down the tree
362 * during the recursion. At the end of the measure pass, every view has stored
363 * its measurements. The second pass happens in
364 * {@link #layout(int,int,int,int)} and is also top-down. During
365 * this pass each parent is responsible for positioning all of its children
366 * using the sizes computed in the measure pass.
367 * </p>
368 *
369 * <p>
370 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
371 * {@link #getMeasuredHeight()} values must be set, along with those for all of
372 * that view's descendants. A view's measured width and measured height values
373 * must respect the constraints imposed by the view's parents. This guarantees
374 * that at the end of the measure pass, all parents accept all of their
375 * children's measurements. A parent view may call measure() more than once on
376 * its children. For example, the parent may measure each child once with
377 * unspecified dimensions to find out how big they want to be, then call
378 * measure() on them again with actual numbers if the sum of all the children's
379 * unconstrained sizes is too big or too small.
380 * </p>
381 *
382 * <p>
383 * The measure pass uses two classes to communicate dimensions. The
384 * {@link MeasureSpec} class is used by views to tell their parents how they
385 * want to be measured and positioned. The base LayoutParams class just
386 * describes how big the view wants to be for both width and height. For each
387 * dimension, it can specify one of:
388 * <ul>
389 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800390 * <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 -0800391 * (minus padding)
392 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
393 * enclose its content (plus padding).
394 * </ul>
395 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
396 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
397 * an X and Y value.
398 * </p>
399 *
400 * <p>
401 * MeasureSpecs are used to push requirements down the tree from parent to
402 * child. A MeasureSpec can be in one of three modes:
403 * <ul>
404 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
405 * of a child view. For example, a LinearLayout may call measure() on its child
406 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
407 * tall the child view wants to be given a width of 240 pixels.
408 * <li>EXACTLY: This is used by the parent to impose an exact size on the
409 * child. The child must use this size, and guarantee that all of its
410 * descendants will fit within this size.
411 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
412 * child. The child must gurantee that it and all of its descendants will fit
413 * within this size.
414 * </ul>
415 * </p>
416 *
417 * <p>
418 * To intiate a layout, call {@link #requestLayout}. This method is typically
419 * called by a view on itself when it believes that is can no longer fit within
420 * its current bounds.
421 * </p>
422 *
423 * <a name="Drawing"></a>
424 * <h3>Drawing</h3>
425 * <p>
426 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700427 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * this means that parents will draw before (i.e., behind) their children, with
429 * siblings drawn in the order they appear in the tree.
430 * If you set a background drawable for a View, then the View will draw it for you
431 * before calling back to its <code>onDraw()</code> method.
432 * </p>
433 *
434 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700435 * 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 -0800436 * </p>
437 *
438 * <p>
439 * To force a view to draw, call {@link #invalidate()}.
440 * </p>
441 *
442 * <a name="EventHandlingThreading"></a>
443 * <h3>Event Handling and Threading</h3>
444 * <p>
445 * The basic cycle of a view is as follows:
446 * <ol>
447 * <li>An event comes in and is dispatched to the appropriate view. The view
448 * handles the event and notifies any listeners.</li>
449 * <li>If in the course of processing the event, the view's bounds may need
450 * to be changed, the view will call {@link #requestLayout()}.</li>
451 * <li>Similarly, if in the course of processing the event the view's appearance
452 * may need to be changed, the view will call {@link #invalidate()}.</li>
453 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
454 * the framework will take care of measuring, laying out, and drawing the tree
455 * as appropriate.</li>
456 * </ol>
457 * </p>
458 *
459 * <p><em>Note: The entire view tree is single threaded. You must always be on
460 * the UI thread when calling any method on any view.</em>
461 * If you are doing work on other threads and want to update the state of a view
462 * from that thread, you should use a {@link Handler}.
463 * </p>
464 *
465 * <a name="FocusHandling"></a>
466 * <h3>Focus Handling</h3>
467 * <p>
468 * The framework will handle routine focus movement in response to user input.
469 * This includes changing the focus as views are removed or hidden, or as new
470 * views become available. Views indicate their willingness to take focus
471 * through the {@link #isFocusable} method. To change whether a view can take
472 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
473 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
474 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
475 * </p>
476 * <p>
477 * Focus movement is based on an algorithm which finds the nearest neighbor in a
478 * given direction. In rare cases, the default algorithm may not match the
479 * intended behavior of the developer. In these situations, you can provide
480 * explicit overrides by using these XML attributes in the layout file:
481 * <pre>
482 * nextFocusDown
483 * nextFocusLeft
484 * nextFocusRight
485 * nextFocusUp
486 * </pre>
487 * </p>
488 *
489 *
490 * <p>
491 * To get a particular view to take focus, call {@link #requestFocus()}.
492 * </p>
493 *
494 * <a name="TouchMode"></a>
495 * <h3>Touch Mode</h3>
496 * <p>
497 * When a user is navigating a user interface via directional keys such as a D-pad, it is
498 * necessary to give focus to actionable items such as buttons so the user can see
499 * what will take input. If the device has touch capabilities, however, and the user
500 * begins interacting with the interface by touching it, it is no longer necessary to
501 * always highlight, or give focus to, a particular view. This motivates a mode
502 * for interaction named 'touch mode'.
503 * </p>
504 * <p>
505 * For a touch capable device, once the user touches the screen, the device
506 * will enter touch mode. From this point onward, only views for which
507 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
508 * Other views that are touchable, like buttons, will not take focus when touched; they will
509 * only fire the on click listeners.
510 * </p>
511 * <p>
512 * Any time a user hits a directional key, such as a D-pad direction, the view device will
513 * exit touch mode, and find a view to take focus, so that the user may resume interacting
514 * with the user interface without touching the screen again.
515 * </p>
516 * <p>
517 * The touch mode state is maintained across {@link android.app.Activity}s. Call
518 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
519 * </p>
520 *
521 * <a name="Scrolling"></a>
522 * <h3>Scrolling</h3>
523 * <p>
524 * The framework provides basic support for views that wish to internally
525 * scroll their content. This includes keeping track of the X and Y scroll
526 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800527 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700528 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 * </p>
530 *
531 * <a name="Tags"></a>
532 * <h3>Tags</h3>
533 * <p>
534 * Unlike IDs, tags are not used to identify views. Tags are essentially an
535 * extra piece of information that can be associated with a view. They are most
536 * often used as a convenience to store data related to views in the views
537 * themselves rather than by putting them in a separate structure.
538 * </p>
539 *
540 * <a name="Animation"></a>
541 * <h3>Animation</h3>
542 * <p>
543 * You can attach an {@link Animation} object to a view using
544 * {@link #setAnimation(Animation)} or
545 * {@link #startAnimation(Animation)}. The animation can alter the scale,
546 * rotation, translation and alpha of a view over time. If the animation is
547 * attached to a view that has children, the animation will affect the entire
548 * subtree rooted by that node. When an animation is started, the framework will
549 * take care of redrawing the appropriate views until the animation completes.
550 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800551 * <p>
552 * Starting with Android 3.0, the preferred way of animating views is to use the
553 * {@link android.animation} package APIs.
554 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 *
Jeff Brown85a31762010-09-01 17:01:00 -0700556 * <a name="Security"></a>
557 * <h3>Security</h3>
558 * <p>
559 * Sometimes it is essential that an application be able to verify that an action
560 * is being performed with the full knowledge and consent of the user, such as
561 * granting a permission request, making a purchase or clicking on an advertisement.
562 * Unfortunately, a malicious application could try to spoof the user into
563 * performing these actions, unaware, by concealing the intended purpose of the view.
564 * As a remedy, the framework offers a touch filtering mechanism that can be used to
565 * improve the security of views that provide access to sensitive functionality.
566 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700567 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800568 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700569 * will discard touches that are received whenever the view's window is obscured by
570 * another visible window. As a result, the view will not receive touches whenever a
571 * toast, dialog or other window appears above the view's window.
572 * </p><p>
573 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700574 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
575 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700576 * </p>
577 *
Romain Guy171c5922011-01-06 10:04:23 -0800578 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700579 * @attr ref android.R.styleable#View_background
580 * @attr ref android.R.styleable#View_clickable
581 * @attr ref android.R.styleable#View_contentDescription
582 * @attr ref android.R.styleable#View_drawingCacheQuality
583 * @attr ref android.R.styleable#View_duplicateParentState
584 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700585 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700586 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700588 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_isScrollContainer
591 * @attr ref android.R.styleable#View_focusable
592 * @attr ref android.R.styleable#View_focusableInTouchMode
593 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
594 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800595 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700596 * @attr ref android.R.styleable#View_longClickable
597 * @attr ref android.R.styleable#View_minHeight
598 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * @attr ref android.R.styleable#View_nextFocusDown
600 * @attr ref android.R.styleable#View_nextFocusLeft
601 * @attr ref android.R.styleable#View_nextFocusRight
602 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700603 * @attr ref android.R.styleable#View_onClick
604 * @attr ref android.R.styleable#View_padding
605 * @attr ref android.R.styleable#View_paddingBottom
606 * @attr ref android.R.styleable#View_paddingLeft
607 * @attr ref android.R.styleable#View_paddingRight
608 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800609 * @attr ref android.R.styleable#View_paddingStart
610 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800612 * @attr ref android.R.styleable#View_rotation
613 * @attr ref android.R.styleable#View_rotationX
614 * @attr ref android.R.styleable#View_rotationY
615 * @attr ref android.R.styleable#View_scaleX
616 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_scrollX
618 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_scrollbarSize
620 * @attr ref android.R.styleable#View_scrollbarStyle
621 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700622 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
623 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
625 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 * @attr ref android.R.styleable#View_scrollbarThumbVertical
627 * @attr ref android.R.styleable#View_scrollbarTrackVertical
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700630 * @attr ref android.R.styleable#View_soundEffectsEnabled
631 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700632 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700641public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
642 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 private static final boolean DBG = false;
644
645 /**
646 * The logging tag used by this class with android.util.Log.
647 */
648 protected static final String VIEW_LOG_TAG = "View";
649
650 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700651 * When set to true, apps will draw debugging information about their layouts.
652 *
653 * @hide
654 */
655 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
656
657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 * Used to mark a View that has no ID.
659 */
660 public static final int NO_ID = -1;
661
662 /**
663 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
664 * calling setFlags.
665 */
666 private static final int NOT_FOCUSABLE = 0x00000000;
667
668 /**
669 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
670 * setFlags.
671 */
672 private static final int FOCUSABLE = 0x00000001;
673
674 /**
675 * Mask for use with setFlags indicating bits used for focus.
676 */
677 private static final int FOCUSABLE_MASK = 0x00000001;
678
679 /**
680 * This view will adjust its padding to fit sytem windows (e.g. status bar)
681 */
682 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
683
684 /**
Scott Main812634c22011-07-27 13:22:35 -0700685 * This view is visible.
686 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int VISIBLE = 0x00000000;
690
691 /**
692 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700693 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
694 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
696 public static final int INVISIBLE = 0x00000004;
697
698 /**
699 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700700 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
701 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 */
703 public static final int GONE = 0x00000008;
704
705 /**
706 * Mask for use with setFlags indicating bits used for visibility.
707 * {@hide}
708 */
709 static final int VISIBILITY_MASK = 0x0000000C;
710
711 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
712
713 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700714 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 * Use with ENABLED_MASK when calling setFlags.
716 * {@hide}
717 */
718 static final int ENABLED = 0x00000000;
719
720 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700721 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 * Use with ENABLED_MASK when calling setFlags.
723 * {@hide}
724 */
725 static final int DISABLED = 0x00000020;
726
727 /**
728 * Mask for use with setFlags indicating bits used for indicating whether
729 * this view is enabled
730 * {@hide}
731 */
732 static final int ENABLED_MASK = 0x00000020;
733
734 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700735 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
736 * called and further optimizations will be performed. It is okay to have
737 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * {@hide}
739 */
740 static final int WILL_NOT_DRAW = 0x00000080;
741
742 /**
743 * Mask for use with setFlags indicating bits used for indicating whether
744 * this view is will draw
745 * {@hide}
746 */
747 static final int DRAW_MASK = 0x00000080;
748
749 /**
750 * <p>This view doesn't show scrollbars.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_NONE = 0x00000000;
754
755 /**
756 * <p>This view shows horizontal scrollbars.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
760
761 /**
762 * <p>This view shows vertical scrollbars.</p>
763 * {@hide}
764 */
765 static final int SCROLLBARS_VERTICAL = 0x00000200;
766
767 /**
768 * <p>Mask for use with setFlags indicating bits used for indicating which
769 * scrollbars are enabled.</p>
770 * {@hide}
771 */
772 static final int SCROLLBARS_MASK = 0x00000300;
773
Jeff Brown85a31762010-09-01 17:01:00 -0700774 /**
775 * Indicates that the view should filter touches when its window is obscured.
776 * Refer to the class comments for more information about this security feature.
777 * {@hide}
778 */
779 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
780
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700781 /**
782 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
783 * that they are optional and should be skipped if the window has
784 * requested system UI flags that ignore those insets for layout.
785 */
786 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
788 /**
789 * <p>This view doesn't show fading edges.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_NONE = 0x00000000;
793
794 /**
795 * <p>This view shows horizontal fading edges.</p>
796 * {@hide}
797 */
798 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
799
800 /**
801 * <p>This view shows vertical fading edges.</p>
802 * {@hide}
803 */
804 static final int FADING_EDGE_VERTICAL = 0x00002000;
805
806 /**
807 * <p>Mask for use with setFlags indicating bits used for indicating which
808 * fading edges are enabled.</p>
809 * {@hide}
810 */
811 static final int FADING_EDGE_MASK = 0x00003000;
812
813 /**
814 * <p>Indicates this view can be clicked. When clickable, a View reacts
815 * to clicks by notifying the OnClickListener.<p>
816 * {@hide}
817 */
818 static final int CLICKABLE = 0x00004000;
819
820 /**
821 * <p>Indicates this view is caching its drawing into a bitmap.</p>
822 * {@hide}
823 */
824 static final int DRAWING_CACHE_ENABLED = 0x00008000;
825
826 /**
827 * <p>Indicates that no icicle should be saved for this view.<p>
828 * {@hide}
829 */
830 static final int SAVE_DISABLED = 0x000010000;
831
832 /**
833 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
834 * property.</p>
835 * {@hide}
836 */
837 static final int SAVE_DISABLED_MASK = 0x000010000;
838
839 /**
840 * <p>Indicates that no drawing cache should ever be created for this view.<p>
841 * {@hide}
842 */
843 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
844
845 /**
846 * <p>Indicates this view can take / keep focus when int touch mode.</p>
847 * {@hide}
848 */
849 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
850
851 /**
852 * <p>Enables low quality mode for the drawing cache.</p>
853 */
854 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
855
856 /**
857 * <p>Enables high quality mode for the drawing cache.</p>
858 */
859 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
860
861 /**
862 * <p>Enables automatic quality mode for the drawing cache.</p>
863 */
864 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
865
866 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
867 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
868 };
869
870 /**
871 * <p>Mask for use with setFlags indicating bits used for the cache
872 * quality property.</p>
873 * {@hide}
874 */
875 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
876
877 /**
878 * <p>
879 * Indicates this view can be long clicked. When long clickable, a View
880 * reacts to long clicks by notifying the OnLongClickListener or showing a
881 * context menu.
882 * </p>
883 * {@hide}
884 */
885 static final int LONG_CLICKABLE = 0x00200000;
886
887 /**
888 * <p>Indicates that this view gets its drawable states from its direct parent
889 * and ignores its original internal states.</p>
890 *
891 * @hide
892 */
893 static final int DUPLICATE_PARENT_STATE = 0x00400000;
894
895 /**
896 * The scrollbar style to display the scrollbars inside the content area,
897 * without increasing the padding. The scrollbars will be overlaid with
898 * translucency on the view's content.
899 */
900 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
901
902 /**
903 * The scrollbar style to display the scrollbars inside the padded area,
904 * increasing the padding of the view. The scrollbars will not overlap the
905 * content area of the view.
906 */
907 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
908
909 /**
910 * The scrollbar style to display the scrollbars at the edge of the view,
911 * without increasing the padding. The scrollbars will be overlaid with
912 * translucency.
913 */
914 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
915
916 /**
917 * The scrollbar style to display the scrollbars at the edge of the view,
918 * increasing the padding of the view. The scrollbars will only overlap the
919 * background, if any.
920 */
921 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
922
923 /**
924 * Mask to check if the scrollbar style is overlay or inset.
925 * {@hide}
926 */
927 static final int SCROLLBARS_INSET_MASK = 0x01000000;
928
929 /**
930 * Mask to check if the scrollbar style is inside or outside.
931 * {@hide}
932 */
933 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
934
935 /**
936 * Mask for scrollbar style.
937 * {@hide}
938 */
939 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
940
941 /**
942 * View flag indicating that the screen should remain on while the
943 * window containing this view is visible to the user. This effectively
944 * takes care of automatically setting the WindowManager's
945 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
946 */
947 public static final int KEEP_SCREEN_ON = 0x04000000;
948
949 /**
950 * View flag indicating whether this view should have sound effects enabled
951 * for events such as clicking and touching.
952 */
953 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
954
955 /**
956 * View flag indicating whether this view should have haptic feedback
957 * enabled for events such as long presses.
958 */
959 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
960
961 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700962 * <p>Indicates that the view hierarchy should stop saving state when
963 * it reaches this view. If state saving is initiated immediately at
964 * the view, it will be allowed.
965 * {@hide}
966 */
967 static final int PARENT_SAVE_DISABLED = 0x20000000;
968
969 /**
970 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
971 * {@hide}
972 */
973 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
974
975 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700976 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
977 * should add all focusable Views regardless if they are focusable in touch mode.
978 */
979 public static final int FOCUSABLES_ALL = 0x00000000;
980
981 /**
982 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
983 * should add only Views focusable in touch mode.
984 */
985 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
986
987 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700988 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
989 * should add only accessibility focusable Views.
990 *
991 * @hide
992 */
993 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
994
995 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700996 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 * item.
998 */
999 public static final int FOCUS_BACKWARD = 0x00000001;
1000
1001 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001002 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 * item.
1004 */
1005 public static final int FOCUS_FORWARD = 0x00000002;
1006
1007 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001008 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 */
1010 public static final int FOCUS_LEFT = 0x00000011;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 */
1015 public static final int FOCUS_UP = 0x00000021;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 */
1020 public static final int FOCUS_RIGHT = 0x00000042;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_DOWN = 0x00000082;
1026
Svetoslav Ganov42138042012-03-20 11:51:39 -07001027 // Accessibility focus directions.
1028
1029 /**
1030 * The accessibility focus which is the current user position when
1031 * interacting with the accessibility framework.
1032 */
1033 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1034
1035 /**
1036 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1037 */
1038 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1039
1040 /**
1041 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1042 */
1043 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1044
1045 /**
1046 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1047 */
1048 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1049
1050 /**
1051 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1052 */
1053 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1054
1055 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001056 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001057 */
1058 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1059
1060 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001061 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001062 */
1063 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1064
1065 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001066 * Bits of {@link #getMeasuredWidthAndState()} and
1067 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1068 */
1069 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1070
1071 /**
1072 * Bits of {@link #getMeasuredWidthAndState()} and
1073 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1074 */
1075 public static final int MEASURED_STATE_MASK = 0xff000000;
1076
1077 /**
1078 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1079 * for functions that combine both width and height into a single int,
1080 * such as {@link #getMeasuredState()} and the childState argument of
1081 * {@link #resolveSizeAndState(int, int, int)}.
1082 */
1083 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1084
1085 /**
1086 * Bit of {@link #getMeasuredWidthAndState()} and
1087 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1088 * is smaller that the space the view would like to have.
1089 */
1090 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1091
1092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 * Base View state sets
1094 */
1095 // Singles
1096 /**
1097 * Indicates the view has no states set. States are used with
1098 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1099 * view depending on its state.
1100 *
1101 * @see android.graphics.drawable.Drawable
1102 * @see #getDrawableState()
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is enabled. States are used with
1107 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1108 * view depending on its state.
1109 *
1110 * @see android.graphics.drawable.Drawable
1111 * @see #getDrawableState()
1112 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001113 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
1115 * Indicates the view is focused. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is selected. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is pressed. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 * @hide
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view's window has focus. States are used with
1144 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1145 * view depending on its state.
1146 *
1147 * @see android.graphics.drawable.Drawable
1148 * @see #getDrawableState()
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Doubles
1152 /**
1153 * Indicates the view is enabled and has the focus.
1154 *
1155 * @see #ENABLED_STATE_SET
1156 * @see #FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled and selected.
1161 *
1162 * @see #ENABLED_STATE_SET
1163 * @see #SELECTED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is enabled and that its window has focus.
1168 *
1169 * @see #ENABLED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 /**
1174 * Indicates the view is focused and selected.
1175 *
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view has the focus and that its window has the focus.
1182 *
1183 * @see #FOCUSED_STATE_SET
1184 * @see #WINDOW_FOCUSED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is selected and that its window has the focus.
1189 *
1190 * @see #SELECTED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 // Triples
1195 /**
1196 * Indicates the view is enabled, focused and selected.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is enabled, focused and its window has the focus.
1205 *
1206 * @see #ENABLED_STATE_SET
1207 * @see #FOCUSED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, selected and its window has the focus.
1213 *
1214 * @see #ENABLED_STATE_SET
1215 * @see #SELECTED_STATE_SET
1216 * @see #WINDOW_FOCUSED_STATE_SET
1217 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001218 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 /**
1220 * Indicates the view is focused, selected and its window has the focus.
1221 *
1222 * @see #FOCUSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is enabled, focused, selected and its window
1229 * has the focus.
1230 *
1231 * @see #ENABLED_STATE_SET
1232 * @see #FOCUSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed and its window has the focus.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed and selected.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #SELECTED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, selected and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed and focused.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #SELECTED_STATE_SET
1279 * @see #FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, focused, selected and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #FOCUSED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed and enabled.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled and its window has the focus.
1300 *
1301 * @see #PRESSED_STATE_SET
1302 * @see #ENABLED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 /**
1307 * Indicates the view is pressed, enabled and selected.
1308 *
1309 * @see #PRESSED_STATE_SET
1310 * @see #ENABLED_STATE_SET
1311 * @see #SELECTED_STATE_SET
1312 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001313 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 /**
1315 * Indicates the view is pressed, enabled, selected and its window has the
1316 * focus.
1317 *
1318 * @see #PRESSED_STATE_SET
1319 * @see #ENABLED_STATE_SET
1320 * @see #SELECTED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and focused.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled, focused and selected.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #SELECTED_STATE_SET
1348 * @see #FOCUSED_STATE_SET
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 /**
1352 * Indicates the view is pressed, enabled, focused, selected and its window
1353 * has the focus.
1354 *
1355 * @see #PRESSED_STATE_SET
1356 * @see #ENABLED_STATE_SET
1357 * @see #SELECTED_STATE_SET
1358 * @see #FOCUSED_STATE_SET
1359 * @see #WINDOW_FOCUSED_STATE_SET
1360 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362
1363 /**
1364 * The order here is very important to {@link #getDrawableState()}
1365 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001366 private static final int[][] VIEW_STATE_SETS;
1367
Romain Guyb051e892010-09-28 19:09:36 -07001368 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1369 static final int VIEW_STATE_SELECTED = 1 << 1;
1370 static final int VIEW_STATE_FOCUSED = 1 << 2;
1371 static final int VIEW_STATE_ENABLED = 1 << 3;
1372 static final int VIEW_STATE_PRESSED = 1 << 4;
1373 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001374 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001375 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001376 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1377 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378
1379 static final int[] VIEW_STATE_IDS = new int[] {
1380 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1381 R.attr.state_selected, VIEW_STATE_SELECTED,
1382 R.attr.state_focused, VIEW_STATE_FOCUSED,
1383 R.attr.state_enabled, VIEW_STATE_ENABLED,
1384 R.attr.state_pressed, VIEW_STATE_PRESSED,
1385 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001386 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001387 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001388 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001389 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 };
1391
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001393 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1394 throw new IllegalStateException(
1395 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1396 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001398 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001401 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001402 orderedIds[i * 2] = viewState;
1403 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001404 }
1405 }
1406 }
Romain Guyb051e892010-09-28 19:09:36 -07001407 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1408 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1409 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 int numBits = Integer.bitCount(i);
1411 int[] set = new int[numBits];
1412 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001413 for (int j = 0; j < orderedIds.length; j += 2) {
1414 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 set[pos++] = orderedIds[j];
1416 }
1417 }
1418 VIEW_STATE_SETS[i] = set;
1419 }
1420
1421 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1422 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1423 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1424 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1426 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1427 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1429 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1431 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1433 | VIEW_STATE_FOCUSED];
1434 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1435 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1437 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1439 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_ENABLED];
1442 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1444 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_ENABLED];
1447 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_ENABLED];
1450 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1453
1454 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1455 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1459 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1464 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_PRESSED];
1470 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1472 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1477 | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1482 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1483 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1484 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1485 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1486 | VIEW_STATE_PRESSED];
1487 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1488 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1489 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1490 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1491 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1492 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1495 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1496 | VIEW_STATE_PRESSED];
1497 }
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001500 * Accessibility event types that are dispatched for text population.
1501 */
1502 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1503 AccessibilityEvent.TYPE_VIEW_CLICKED
1504 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1505 | AccessibilityEvent.TYPE_VIEW_SELECTED
1506 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1507 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1508 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001509 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001510 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001511 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1512 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * Temporary Rect currently for use in setBackground(). This will probably
1516 * be extended in the future to hold our own class with more than just
1517 * a Rect. :)
1518 */
1519 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001520
1521 /**
1522 * Map used to store views' tags.
1523 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001524 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001527 * The next available accessiiblity id.
1528 */
1529 private static int sNextAccessibilityViewId;
1530
1531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 * The animation currently associated with this view.
1533 * @hide
1534 */
1535 protected Animation mCurrentAnimation = null;
1536
1537 /**
1538 * Width as measured during measure pass.
1539 * {@hide}
1540 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001541 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001542 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543
1544 /**
1545 * Height as measured during measure pass.
1546 * {@hide}
1547 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001548 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001549 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
1551 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001552 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1553 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1554 * its display list. This flag, used only when hw accelerated, allows us to clear the
1555 * flag while retaining this information until it's needed (at getDisplayList() time and
1556 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1557 *
1558 * {@hide}
1559 */
1560 boolean mRecreateDisplayList = false;
1561
1562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 * The view's identifier.
1564 * {@hide}
1565 *
1566 * @see #setId(int)
1567 * @see #getId()
1568 */
1569 @ViewDebug.ExportedProperty(resolveId = true)
1570 int mID = NO_ID;
1571
1572 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001573 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001574 */
1575 int mAccessibilityViewId = NO_ID;
1576
1577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 * The view's tag.
1579 * {@hide}
1580 *
1581 * @see #setTag(Object)
1582 * @see #getTag()
1583 */
1584 protected Object mTag;
1585
1586 // for mPrivateFlags:
1587 /** {@hide} */
1588 static final int WANTS_FOCUS = 0x00000001;
1589 /** {@hide} */
1590 static final int FOCUSED = 0x00000002;
1591 /** {@hide} */
1592 static final int SELECTED = 0x00000004;
1593 /** {@hide} */
1594 static final int IS_ROOT_NAMESPACE = 0x00000008;
1595 /** {@hide} */
1596 static final int HAS_BOUNDS = 0x00000010;
1597 /** {@hide} */
1598 static final int DRAWN = 0x00000020;
1599 /**
1600 * When this flag is set, this view is running an animation on behalf of its
1601 * children and should therefore not cancel invalidate requests, even if they
1602 * lie outside of this view's bounds.
1603 *
1604 * {@hide}
1605 */
1606 static final int DRAW_ANIMATION = 0x00000040;
1607 /** {@hide} */
1608 static final int SKIP_DRAW = 0x00000080;
1609 /** {@hide} */
1610 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1611 /** {@hide} */
1612 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1613 /** {@hide} */
1614 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1615 /** {@hide} */
1616 static final int MEASURED_DIMENSION_SET = 0x00000800;
1617 /** {@hide} */
1618 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001619 /** {@hide} */
1620 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621
1622 private static final int PRESSED = 0x00004000;
1623
1624 /** {@hide} */
1625 static final int DRAWING_CACHE_VALID = 0x00008000;
1626 /**
1627 * Flag used to indicate that this view should be drawn once more (and only once
1628 * more) after its animation has completed.
1629 * {@hide}
1630 */
1631 static final int ANIMATION_STARTED = 0x00010000;
1632
1633 private static final int SAVE_STATE_CALLED = 0x00020000;
1634
1635 /**
1636 * Indicates that the View returned true when onSetAlpha() was called and that
1637 * the alpha must be restored.
1638 * {@hide}
1639 */
1640 static final int ALPHA_SET = 0x00040000;
1641
1642 /**
1643 * Set by {@link #setScrollContainer(boolean)}.
1644 */
1645 static final int SCROLL_CONTAINER = 0x00080000;
1646
1647 /**
1648 * Set by {@link #setScrollContainer(boolean)}.
1649 */
1650 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1651
1652 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001653 * View flag indicating whether this view was invalidated (fully or partially.)
1654 *
1655 * @hide
1656 */
1657 static final int DIRTY = 0x00200000;
1658
1659 /**
1660 * View flag indicating whether this view was invalidated by an opaque
1661 * invalidate request.
1662 *
1663 * @hide
1664 */
1665 static final int DIRTY_OPAQUE = 0x00400000;
1666
1667 /**
1668 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1669 *
1670 * @hide
1671 */
1672 static final int DIRTY_MASK = 0x00600000;
1673
1674 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001675 * Indicates whether the background is opaque.
1676 *
1677 * @hide
1678 */
1679 static final int OPAQUE_BACKGROUND = 0x00800000;
1680
1681 /**
1682 * Indicates whether the scrollbars are opaque.
1683 *
1684 * @hide
1685 */
1686 static final int OPAQUE_SCROLLBARS = 0x01000000;
1687
1688 /**
1689 * Indicates whether the view is opaque.
1690 *
1691 * @hide
1692 */
1693 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powelle14579b2009-12-16 18:39:52 -08001695 /**
1696 * Indicates a prepressed state;
1697 * the short time between ACTION_DOWN and recognizing
1698 * a 'real' press. Prepressed is used to recognize quick taps
1699 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001700 *
Adam Powelle14579b2009-12-16 18:39:52 -08001701 * @hide
1702 */
1703 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001704
Adam Powellc9fbaab2010-02-16 17:16:19 -08001705 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001706 * Indicates whether the view is temporarily detached.
1707 *
1708 * @hide
1709 */
1710 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001711
Adam Powell8568c3a2010-04-19 14:26:11 -07001712 /**
1713 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001714 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001715 * @hide
1716 */
1717 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001718
1719 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001720 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1721 * @hide
1722 */
1723 private static final int HOVERED = 0x10000000;
1724
1725 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001726 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1727 * for transform operations
1728 *
1729 * @hide
1730 */
Adam Powellf37df072010-09-17 16:22:49 -07001731 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001732
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001733 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001734 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001735
Chet Haasefd2b0022010-08-06 13:08:56 -07001736 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001737 * Indicates that this view was specifically invalidated, not just dirtied because some
1738 * child view was invalidated. The flag is used to determine when we need to recreate
1739 * a view's display list (as opposed to just returning a reference to its existing
1740 * display list).
1741 *
1742 * @hide
1743 */
1744 static final int INVALIDATED = 0x80000000;
1745
Christopher Tate3d4bf172011-03-28 16:16:46 -07001746 /* Masks for mPrivateFlags2 */
1747
1748 /**
1749 * Indicates that this view has reported that it can accept the current drag's content.
1750 * Cleared when the drag operation concludes.
1751 * @hide
1752 */
1753 static final int DRAG_CAN_ACCEPT = 0x00000001;
1754
1755 /**
1756 * Indicates that this view is currently directly under the drag location in a
1757 * drag-and-drop operation involving content that it can accept. Cleared when
1758 * the drag exits the view, or when the drag operation concludes.
1759 * @hide
1760 */
1761 static final int DRAG_HOVERED = 0x00000002;
1762
Cibu Johny86666632010-02-22 13:01:02 -08001763 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001764 * Horizontal layout direction of this view is from Left to Right.
1765 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001766 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001767 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001768
1769 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001770 * Horizontal layout direction of this view is from Right to Left.
1771 * Use with {@link #setLayoutDirection}.
1772 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001773 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001774
1775 /**
1776 * Horizontal layout direction of this view is inherited from its parent.
1777 * Use with {@link #setLayoutDirection}.
1778 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001779 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780
1781 /**
1782 * Horizontal layout direction of this view is from deduced from the default language
1783 * script for the locale. Use with {@link #setLayoutDirection}.
1784 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001785 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001786
1787 /**
1788 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001789 * @hide
1790 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001791 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1792
1793 /**
1794 * Mask for use with private flags indicating bits used for horizontal layout direction.
1795 * @hide
1796 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001797 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798
1799 /**
1800 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1801 * right-to-left direction.
1802 * @hide
1803 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001804 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805
1806 /**
1807 * Indicates whether the view horizontal layout direction has been resolved.
1808 * @hide
1809 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001810 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001811
1812 /**
1813 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1814 * @hide
1815 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001816 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001817
1818 /*
1819 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1820 * flag value.
1821 * @hide
1822 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001823 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1824 LAYOUT_DIRECTION_LTR,
1825 LAYOUT_DIRECTION_RTL,
1826 LAYOUT_DIRECTION_INHERIT,
1827 LAYOUT_DIRECTION_LOCALE
1828 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001829
1830 /**
1831 * Default horizontal layout direction.
1832 * @hide
1833 */
1834 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001835
Adam Powell539ee872012-02-03 19:00:49 -08001836 /**
1837 * Indicates that the view is tracking some sort of transient state
1838 * that the app should not need to be aware of, but that the framework
1839 * should take special care to preserve.
1840 *
1841 * @hide
1842 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001843 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001844
1845
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001846 /**
1847 * Text direction is inherited thru {@link ViewGroup}
1848 */
1849 public static final int TEXT_DIRECTION_INHERIT = 0;
1850
1851 /**
1852 * Text direction is using "first strong algorithm". The first strong directional character
1853 * determines the paragraph direction. If there is no strong directional character, the
1854 * paragraph direction is the view's resolved layout direction.
1855 */
1856 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1857
1858 /**
1859 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1860 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1861 * If there are neither, the paragraph direction is the view's resolved layout direction.
1862 */
1863 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1864
1865 /**
1866 * Text direction is forced to LTR.
1867 */
1868 public static final int TEXT_DIRECTION_LTR = 3;
1869
1870 /**
1871 * Text direction is forced to RTL.
1872 */
1873 public static final int TEXT_DIRECTION_RTL = 4;
1874
1875 /**
1876 * Text direction is coming from the system Locale.
1877 */
1878 public static final int TEXT_DIRECTION_LOCALE = 5;
1879
1880 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001881 * Default text direction is inherited
1882 */
1883 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1884
1885 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001886 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1887 * @hide
1888 */
1889 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1890
1891 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001892 * Mask for use with private flags indicating bits used for text direction.
1893 * @hide
1894 */
1895 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1896
1897 /**
1898 * Array of text direction flags for mapping attribute "textDirection" to correct
1899 * flag value.
1900 * @hide
1901 */
1902 private static final int[] TEXT_DIRECTION_FLAGS = {
1903 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1904 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1905 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1906 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1907 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1908 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1909 };
1910
1911 /**
1912 * Indicates whether the view text direction has been resolved.
1913 * @hide
1914 */
1915 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1916
1917 /**
1918 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1919 * @hide
1920 */
1921 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1922
1923 /**
1924 * Mask for use with private flags indicating bits used for resolved text direction.
1925 * @hide
1926 */
1927 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1928
1929 /**
1930 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1931 * @hide
1932 */
1933 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1934 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1935
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001936 /*
1937 * Default text alignment. The text alignment of this View is inherited from its parent.
1938 * Use with {@link #setTextAlignment(int)}
1939 */
1940 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1941
1942 /**
1943 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1944 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1945 *
1946 * Use with {@link #setTextAlignment(int)}
1947 */
1948 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1949
1950 /**
1951 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1952 *
1953 * Use with {@link #setTextAlignment(int)}
1954 */
1955 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1956
1957 /**
1958 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1959 *
1960 * Use with {@link #setTextAlignment(int)}
1961 */
1962 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1963
1964 /**
1965 * Center the paragraph, e.g. ALIGN_CENTER.
1966 *
1967 * Use with {@link #setTextAlignment(int)}
1968 */
1969 public static final int TEXT_ALIGNMENT_CENTER = 4;
1970
1971 /**
1972 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1973 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1974 *
1975 * Use with {@link #setTextAlignment(int)}
1976 */
1977 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1978
1979 /**
1980 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1981 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1982 *
1983 * Use with {@link #setTextAlignment(int)}
1984 */
1985 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1986
1987 /**
1988 * Default text alignment is inherited
1989 */
1990 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1991
1992 /**
1993 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1994 * @hide
1995 */
1996 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1997
1998 /**
1999 * Mask for use with private flags indicating bits used for text alignment.
2000 * @hide
2001 */
2002 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2003
2004 /**
2005 * Array of text direction flags for mapping attribute "textAlignment" to correct
2006 * flag value.
2007 * @hide
2008 */
2009 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2010 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2011 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2012 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2013 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2014 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2015 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2016 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2017 };
2018
2019 /**
2020 * Indicates whether the view text alignment has been resolved.
2021 * @hide
2022 */
2023 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2024
2025 /**
2026 * Bit shift to get the resolved text alignment.
2027 * @hide
2028 */
2029 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2030
2031 /**
2032 * Mask for use with private flags indicating bits used for text alignment.
2033 * @hide
2034 */
2035 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2036
2037 /**
2038 * Indicates whether if the view text alignment has been resolved to gravity
2039 */
2040 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2041 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2042
Svetoslav Ganov42138042012-03-20 11:51:39 -07002043 // Accessiblity constants for mPrivateFlags2
2044
2045 /**
2046 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2047 */
2048 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2049
2050 /**
2051 * Automatically determine whether a view is important for accessibility.
2052 */
2053 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2054
2055 /**
2056 * The view is important for accessibility.
2057 */
2058 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2059
2060 /**
2061 * The view is not important for accessibility.
2062 */
2063 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2064
2065 /**
2066 * The default whether the view is important for accessiblity.
2067 */
2068 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2069
2070 /**
2071 * Mask for obtainig the bits which specify how to determine
2072 * whether a view is important for accessibility.
2073 */
2074 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2075 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2076 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2077
2078 /**
2079 * Flag indicating whether a view has accessibility focus.
2080 */
2081 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2082
2083 /**
2084 * Flag indicating whether a view state for accessibility has changed.
2085 */
2086 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002087
Christopher Tate3d4bf172011-03-28 16:16:46 -07002088 /* End of masks for mPrivateFlags2 */
2089
2090 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2091
Chet Haasedaf98e92011-01-10 14:10:36 -08002092 /**
Adam Powell637d3372010-08-25 14:37:03 -07002093 * Always allow a user to over-scroll this view, provided it is a
2094 * view that can scroll.
2095 *
2096 * @see #getOverScrollMode()
2097 * @see #setOverScrollMode(int)
2098 */
2099 public static final int OVER_SCROLL_ALWAYS = 0;
2100
2101 /**
2102 * Allow a user to over-scroll this view only if the content is large
2103 * enough to meaningfully scroll, provided it is a view that can scroll.
2104 *
2105 * @see #getOverScrollMode()
2106 * @see #setOverScrollMode(int)
2107 */
2108 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2109
2110 /**
2111 * Never allow a user to over-scroll this view.
2112 *
2113 * @see #getOverScrollMode()
2114 * @see #setOverScrollMode(int)
2115 */
2116 public static final int OVER_SCROLL_NEVER = 2;
2117
2118 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002119 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2120 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002121 *
Joe Malin32736f02011-01-19 16:14:20 -08002122 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002123 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002124 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002125
2126 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002127 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2128 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002129 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002130 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002131 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002132 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002133 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002134 *
Joe Malin32736f02011-01-19 16:14:20 -08002135 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002136 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002137 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2138
2139 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002140 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2141 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002142 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002143 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002144 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2145 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2146 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002147 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002148 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2149 * window flags) for displaying content using every last pixel on the display.
2150 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002151 * <p>There is a limitation: because navigation controls are so important, the least user
2152 * interaction will cause them to reappear immediately. When this happens, both
2153 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2154 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002155 *
2156 * @see #setSystemUiVisibility(int)
2157 */
2158 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2159
2160 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2162 * into the normal fullscreen mode so that its content can take over the screen
2163 * while still allowing the user to interact with the application.
2164 *
2165 * <p>This has the same visual effect as
2166 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2167 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2168 * meaning that non-critical screen decorations (such as the status bar) will be
2169 * hidden while the user is in the View's window, focusing the experience on
2170 * that content. Unlike the window flag, if you are using ActionBar in
2171 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2172 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2173 * hide the action bar.
2174 *
2175 * <p>This approach to going fullscreen is best used over the window flag when
2176 * it is a transient state -- that is, the application does this at certain
2177 * points in its user interaction where it wants to allow the user to focus
2178 * on content, but not as a continuous state. For situations where the application
2179 * would like to simply stay full screen the entire time (such as a game that
2180 * wants to take over the screen), the
2181 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2182 * is usually a better approach. The state set here will be removed by the system
2183 * in various situations (such as the user moving to another application) like
2184 * the other system UI states.
2185 *
2186 * <p>When using this flag, the application should provide some easy facility
2187 * for the user to go out of it. A common example would be in an e-book
2188 * reader, where tapping on the screen brings back whatever screen and UI
2189 * decorations that had been hidden while the user was immersed in reading
2190 * the book.
2191 *
2192 * @see #setSystemUiVisibility(int)
2193 */
2194 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2195
2196 /**
2197 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2198 * flags, we would like a stable view of the content insets given to
2199 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2200 * will always represent the worst case that the application can expect
2201 * as a continue state. In practice this means with any of system bar,
2202 * nav bar, and status bar shown, but not the space that would be needed
2203 * for an input method.
2204 *
2205 * <p>If you are using ActionBar in
2206 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2207 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2208 * insets it adds to those given to the application.
2209 */
2210 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2211
2212 /**
2213 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2214 * to be layed out as if it has requested
2215 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2216 * allows it to avoid artifacts when switching in and out of that mode, at
2217 * the expense that some of its user interface may be covered by screen
2218 * decorations when they are shown. You can perform layout of your inner
2219 * UI elements to account for the navagation system UI through the
2220 * {@link #fitSystemWindows(Rect)} method.
2221 */
2222 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
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_FULLSCREEN}, 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 non-fullscreen system UI through the
2232 * {@link #fitSystemWindows(Rect)} method.
2233 */
2234 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2235
2236 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002237 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2238 */
2239 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2240
2241 /**
2242 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2243 */
2244 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002245
2246 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002247 * @hide
2248 *
2249 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2250 * out of the public fields to keep the undefined bits out of the developer's way.
2251 *
2252 * Flag to make the status bar not expandable. Unless you also
2253 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2254 */
2255 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2256
2257 /**
2258 * @hide
2259 *
2260 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2261 * out of the public fields to keep the undefined bits out of the developer's way.
2262 *
2263 * Flag to hide notification icons and scrolling ticker text.
2264 */
2265 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2266
2267 /**
2268 * @hide
2269 *
2270 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2271 * out of the public fields to keep the undefined bits out of the developer's way.
2272 *
2273 * Flag to disable incoming notification alerts. This will not block
2274 * icons, but it will block sound, vibrating and other visual or aural notifications.
2275 */
2276 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2277
2278 /**
2279 * @hide
2280 *
2281 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2282 * out of the public fields to keep the undefined bits out of the developer's way.
2283 *
2284 * Flag to hide only the scrolling ticker. Note that
2285 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2286 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2287 */
2288 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
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 the center system info area.
2297 */
2298 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2299
2300 /**
2301 * @hide
2302 *
2303 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2304 * out of the public fields to keep the undefined bits out of the developer's way.
2305 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002306 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002307 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2308 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002309 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002310
2311 /**
2312 * @hide
2313 *
2314 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2315 * out of the public fields to keep the undefined bits out of the developer's way.
2316 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002317 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002318 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2319 */
2320 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2321
2322 /**
2323 * @hide
2324 *
2325 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2326 * out of the public fields to keep the undefined bits out of the developer's way.
2327 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002328 * Flag to hide only the clock. You might use this if your activity has
2329 * its own clock making the status bar's clock redundant.
2330 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002331 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2332
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002333 /**
2334 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002335 *
2336 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2337 * out of the public fields to keep the undefined bits out of the developer's way.
2338 *
2339 * Flag to hide only the recent apps button. Don't use this
2340 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2341 */
2342 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2343
2344 /**
2345 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002346 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002347 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002348
2349 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002350 * These are the system UI flags that can be cleared by events outside
2351 * of an application. Currently this is just the ability to tap on the
2352 * screen while hiding the navigation bar to have it return.
2353 * @hide
2354 */
2355 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002356 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2357 | SYSTEM_UI_FLAG_FULLSCREEN;
2358
2359 /**
2360 * Flags that can impact the layout in relation to system UI.
2361 */
2362 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2363 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2364 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002365
2366 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002367 * Find views that render the specified text.
2368 *
2369 * @see #findViewsWithText(ArrayList, CharSequence, int)
2370 */
2371 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2372
2373 /**
2374 * Find find views that contain the specified content description.
2375 *
2376 * @see #findViewsWithText(ArrayList, CharSequence, int)
2377 */
2378 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2379
2380 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002381 * Find views that contain {@link AccessibilityNodeProvider}. Such
2382 * a View is a root of virtual view hierarchy and may contain the searched
2383 * text. If this flag is set Views with providers are automatically
2384 * added and it is a responsibility of the client to call the APIs of
2385 * the provider to determine whether the virtual tree rooted at this View
2386 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2387 * represeting the virtual views with this text.
2388 *
2389 * @see #findViewsWithText(ArrayList, CharSequence, int)
2390 *
2391 * @hide
2392 */
2393 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2394
2395 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002396 * Indicates that the screen has changed state and is now off.
2397 *
2398 * @see #onScreenStateChanged(int)
2399 */
2400 public static final int SCREEN_STATE_OFF = 0x0;
2401
2402 /**
2403 * Indicates that the screen has changed state and is now on.
2404 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002405 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002406 */
2407 public static final int SCREEN_STATE_ON = 0x1;
2408
2409 /**
Adam Powell637d3372010-08-25 14:37:03 -07002410 * Controls the over-scroll mode for this view.
2411 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2412 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2413 * and {@link #OVER_SCROLL_NEVER}.
2414 */
2415 private int mOverScrollMode;
2416
2417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 * The parent this view is attached to.
2419 * {@hide}
2420 *
2421 * @see #getParent()
2422 */
2423 protected ViewParent mParent;
2424
2425 /**
2426 * {@hide}
2427 */
2428 AttachInfo mAttachInfo;
2429
2430 /**
2431 * {@hide}
2432 */
Romain Guy809a7f62009-05-14 15:44:42 -07002433 @ViewDebug.ExportedProperty(flagMapping = {
2434 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2435 name = "FORCE_LAYOUT"),
2436 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2437 name = "LAYOUT_REQUIRED"),
2438 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002439 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002440 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2441 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2442 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2443 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2444 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002446 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447
2448 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002449 * This view's request for the visibility of the status bar.
2450 * @hide
2451 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002452 @ViewDebug.ExportedProperty(flagMapping = {
2453 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2454 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2455 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2456 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2457 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2458 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2459 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2460 equals = SYSTEM_UI_FLAG_VISIBLE,
2461 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2462 })
Joe Onorato664644d2011-01-23 17:53:23 -08002463 int mSystemUiVisibility;
2464
2465 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002466 * Reference count for transient state.
2467 * @see #setHasTransientState(boolean)
2468 */
2469 int mTransientStateCount = 0;
2470
2471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 * Count of how many windows this view has been attached to.
2473 */
2474 int mWindowAttachCount;
2475
2476 /**
2477 * The layout parameters associated with this view and used by the parent
2478 * {@link android.view.ViewGroup} to determine how this view should be
2479 * laid out.
2480 * {@hide}
2481 */
2482 protected ViewGroup.LayoutParams mLayoutParams;
2483
2484 /**
2485 * The view flags hold various views states.
2486 * {@hide}
2487 */
2488 @ViewDebug.ExportedProperty
2489 int mViewFlags;
2490
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002491 static class TransformationInfo {
2492 /**
2493 * The transform matrix for the View. This transform is calculated internally
2494 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2495 * is used by default. Do *not* use this variable directly; instead call
2496 * getMatrix(), which will automatically recalculate the matrix if necessary
2497 * to get the correct matrix based on the latest rotation and scale properties.
2498 */
2499 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002500
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002501 /**
2502 * The transform matrix for the View. This transform is calculated internally
2503 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2504 * is used by default. Do *not* use this variable directly; instead call
2505 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2506 * to get the correct matrix based on the latest rotation and scale properties.
2507 */
2508 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002509
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002510 /**
2511 * An internal variable that tracks whether we need to recalculate the
2512 * transform matrix, based on whether the rotation or scaleX/Y properties
2513 * have changed since the matrix was last calculated.
2514 */
2515 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002516
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002517 /**
2518 * An internal variable that tracks whether we need to recalculate the
2519 * transform matrix, based on whether the rotation or scaleX/Y properties
2520 * have changed since the matrix was last calculated.
2521 */
2522 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002523
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002524 /**
2525 * A variable that tracks whether we need to recalculate the
2526 * transform matrix, based on whether the rotation or scaleX/Y properties
2527 * have changed since the matrix was last calculated. This variable
2528 * is only valid after a call to updateMatrix() or to a function that
2529 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2530 */
2531 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002532
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002533 /**
2534 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2535 */
2536 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002537
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002538 /**
2539 * This matrix is used when computing the matrix for 3D rotations.
2540 */
2541 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002542
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002543 /**
2544 * These prev values are used to recalculate a centered pivot point when necessary. The
2545 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2546 * set), so thes values are only used then as well.
2547 */
2548 private int mPrevWidth = -1;
2549 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002550
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002551 /**
2552 * The degrees rotation around the vertical axis through the pivot point.
2553 */
2554 @ViewDebug.ExportedProperty
2555 float mRotationY = 0f;
2556
2557 /**
2558 * The degrees rotation around the horizontal axis through the pivot point.
2559 */
2560 @ViewDebug.ExportedProperty
2561 float mRotationX = 0f;
2562
2563 /**
2564 * The degrees rotation around the pivot point.
2565 */
2566 @ViewDebug.ExportedProperty
2567 float mRotation = 0f;
2568
2569 /**
2570 * The amount of translation of the object away from its left property (post-layout).
2571 */
2572 @ViewDebug.ExportedProperty
2573 float mTranslationX = 0f;
2574
2575 /**
2576 * The amount of translation of the object away from its top property (post-layout).
2577 */
2578 @ViewDebug.ExportedProperty
2579 float mTranslationY = 0f;
2580
2581 /**
2582 * The amount of scale in the x direction around the pivot point. A
2583 * value of 1 means no scaling is applied.
2584 */
2585 @ViewDebug.ExportedProperty
2586 float mScaleX = 1f;
2587
2588 /**
2589 * The amount of scale in the y direction around the pivot point. A
2590 * value of 1 means no scaling is applied.
2591 */
2592 @ViewDebug.ExportedProperty
2593 float mScaleY = 1f;
2594
2595 /**
Chet Haasea33de552012-02-03 16:28:24 -08002596 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002597 */
2598 @ViewDebug.ExportedProperty
2599 float mPivotX = 0f;
2600
2601 /**
Chet Haasea33de552012-02-03 16:28:24 -08002602 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002603 */
2604 @ViewDebug.ExportedProperty
2605 float mPivotY = 0f;
2606
2607 /**
2608 * The opacity of the View. This is a value from 0 to 1, where 0 means
2609 * completely transparent and 1 means completely opaque.
2610 */
2611 @ViewDebug.ExportedProperty
2612 float mAlpha = 1f;
2613 }
2614
2615 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002616
Joe Malin32736f02011-01-19 16:14:20 -08002617 private boolean mLastIsOpaque;
2618
Chet Haasefd2b0022010-08-06 13:08:56 -07002619 /**
2620 * Convenience value to check for float values that are close enough to zero to be considered
2621 * zero.
2622 */
Romain Guy2542d192010-08-18 11:47:12 -07002623 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002624
2625 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 * The distance in pixels from the left edge of this view's parent
2627 * to the left edge of this view.
2628 * {@hide}
2629 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002630 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 protected int mLeft;
2632 /**
2633 * The distance in pixels from the left edge of this view's parent
2634 * to the right edge of this view.
2635 * {@hide}
2636 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002637 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 protected int mRight;
2639 /**
2640 * The distance in pixels from the top edge of this view's parent
2641 * to the top edge of this view.
2642 * {@hide}
2643 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002644 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 protected int mTop;
2646 /**
2647 * The distance in pixels from the top edge of this view's parent
2648 * to the bottom edge of this view.
2649 * {@hide}
2650 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002651 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 protected int mBottom;
2653
2654 /**
2655 * The offset, in pixels, by which the content of this view is scrolled
2656 * horizontally.
2657 * {@hide}
2658 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002659 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 protected int mScrollX;
2661 /**
2662 * The offset, in pixels, by which the content of this view is scrolled
2663 * vertically.
2664 * {@hide}
2665 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002666 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 protected int mScrollY;
2668
2669 /**
2670 * The left padding in pixels, that is the distance in pixels between the
2671 * left edge of this view and the left edge of its content.
2672 * {@hide}
2673 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002674 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 protected int mPaddingLeft;
2676 /**
2677 * The right padding in pixels, that is the distance in pixels between the
2678 * right edge of this view and the right edge of its content.
2679 * {@hide}
2680 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002681 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 protected int mPaddingRight;
2683 /**
2684 * The top padding in pixels, that is the distance in pixels between the
2685 * top edge of this view and the top edge of its content.
2686 * {@hide}
2687 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002688 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 protected int mPaddingTop;
2690 /**
2691 * The bottom padding in pixels, that is the distance in pixels between the
2692 * bottom edge of this view and the bottom edge of its content.
2693 * {@hide}
2694 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002695 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 protected int mPaddingBottom;
2697
2698 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002699 * The layout insets in pixels, that is the distance in pixels between the
2700 * visible edges of this view its bounds.
2701 */
2702 private Insets mLayoutInsets;
2703
2704 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002705 * Briefly describes the view and is primarily used for accessibility support.
2706 */
2707 private CharSequence mContentDescription;
2708
2709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002711 *
2712 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002714 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002715 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716
2717 /**
2718 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002719 *
2720 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002722 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002723 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002725 /**
Adam Powell20232d02010-12-08 21:08:53 -08002726 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002727 *
2728 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002729 */
2730 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002731 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002732
2733 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002734 * Cache if the user padding is relative.
2735 *
2736 */
2737 @ViewDebug.ExportedProperty(category = "padding")
2738 boolean mUserPaddingRelative;
2739
2740 /**
2741 * Cache the paddingStart set by the user to append to the scrollbar's size.
2742 *
2743 */
2744 @ViewDebug.ExportedProperty(category = "padding")
2745 int mUserPaddingStart;
2746
2747 /**
2748 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2749 *
2750 */
2751 @ViewDebug.ExportedProperty(category = "padding")
2752 int mUserPaddingEnd;
2753
2754 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002755 * @hide
2756 */
2757 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2758 /**
2759 * @hide
2760 */
2761 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762
Philip Milne6c8ea062012-04-03 17:38:43 -07002763 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
2765 private int mBackgroundResource;
2766 private boolean mBackgroundSizeChanged;
2767
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002768 static class ListenerInfo {
2769 /**
2770 * Listener used to dispatch focus change events.
2771 * This field should be made private, so it is hidden from the SDK.
2772 * {@hide}
2773 */
2774 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002776 /**
2777 * Listeners for layout change events.
2778 */
2779 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002780
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002781 /**
2782 * Listeners for attach events.
2783 */
2784 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002785
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002786 /**
2787 * Listener used to dispatch click events.
2788 * This field should be made private, so it is hidden from the SDK.
2789 * {@hide}
2790 */
2791 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002793 /**
2794 * Listener used to dispatch long click events.
2795 * This field should be made private, so it is hidden from the SDK.
2796 * {@hide}
2797 */
2798 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002800 /**
2801 * Listener used to build the context menu.
2802 * This field should be made private, so it is hidden from the SDK.
2803 * {@hide}
2804 */
2805 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002807 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002809 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002812
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002813 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002814
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002815 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002816
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002817 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2818 }
2819
2820 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 /**
2823 * The application environment this view lives in.
2824 * This field should be made private, so it is hidden from the SDK.
2825 * {@hide}
2826 */
2827 protected Context mContext;
2828
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002829 private final Resources mResources;
2830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 private ScrollabilityCache mScrollCache;
2832
2833 private int[] mDrawableState = null;
2834
Romain Guy0211a0a2011-02-14 16:34:59 -08002835 /**
2836 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002837 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002838 * @hide
2839 */
2840 public boolean mCachingFailed;
2841
Romain Guy02890fd2010-08-06 17:58:44 -07002842 private Bitmap mDrawingCache;
2843 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002844 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002845 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846
2847 /**
2848 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2849 * the user may specify which view to go to next.
2850 */
2851 private int mNextFocusLeftId = View.NO_ID;
2852
2853 /**
2854 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2855 * the user may specify which view to go to next.
2856 */
2857 private int mNextFocusRightId = View.NO_ID;
2858
2859 /**
2860 * When this view has focus and the next focus is {@link #FOCUS_UP},
2861 * the user may specify which view to go to next.
2862 */
2863 private int mNextFocusUpId = View.NO_ID;
2864
2865 /**
2866 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2867 * the user may specify which view to go to next.
2868 */
2869 private int mNextFocusDownId = View.NO_ID;
2870
Jeff Brown4e6319b2010-12-13 10:36:51 -08002871 /**
2872 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2873 * the user may specify which view to go to next.
2874 */
2875 int mNextFocusForwardId = View.NO_ID;
2876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002878 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002879 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002880 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 private UnsetPressedState mUnsetPressedState;
2883
2884 /**
2885 * Whether the long press's action has been invoked. The tap's action is invoked on the
2886 * up event while a long press is invoked as soon as the long press duration is reached, so
2887 * a long press could be performed before the tap is checked, in which case the tap's action
2888 * should not be invoked.
2889 */
2890 private boolean mHasPerformedLongPress;
2891
2892 /**
2893 * The minimum height of the view. We'll try our best to have the height
2894 * of this view to at least this amount.
2895 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002896 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 private int mMinHeight;
2898
2899 /**
2900 * The minimum width of the view. We'll try our best to have the width
2901 * of this view to at least this amount.
2902 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002903 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 private int mMinWidth;
2905
2906 /**
2907 * The delegate to handle touch events that are physically in this view
2908 * but should be handled by another view.
2909 */
2910 private TouchDelegate mTouchDelegate = null;
2911
2912 /**
2913 * Solid color to use as a background when creating the drawing cache. Enables
2914 * the cache to use 16 bit bitmaps instead of 32 bit.
2915 */
2916 private int mDrawingCacheBackgroundColor = 0;
2917
2918 /**
2919 * Special tree observer used when mAttachInfo is null.
2920 */
2921 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002922
Adam Powelle14579b2009-12-16 18:39:52 -08002923 /**
2924 * Cache the touch slop from the context that created the view.
2925 */
2926 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002929 * Object that handles automatic animation of view properties.
2930 */
2931 private ViewPropertyAnimator mAnimator = null;
2932
2933 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002934 * Flag indicating that a drag can cross window boundaries. When
2935 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2936 * with this flag set, all visible applications will be able to participate
2937 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002938 *
2939 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002940 */
2941 public static final int DRAG_FLAG_GLOBAL = 1;
2942
2943 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002944 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2945 */
2946 private float mVerticalScrollFactor;
2947
2948 /**
Adam Powell20232d02010-12-08 21:08:53 -08002949 * Position of the vertical scroll bar.
2950 */
2951 private int mVerticalScrollbarPosition;
2952
2953 /**
2954 * Position the scroll bar at the default position as determined by the system.
2955 */
2956 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2957
2958 /**
2959 * Position the scroll bar along the left edge.
2960 */
2961 public static final int SCROLLBAR_POSITION_LEFT = 1;
2962
2963 /**
2964 * Position the scroll bar along the right edge.
2965 */
2966 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2967
2968 /**
Romain Guy171c5922011-01-06 10:04:23 -08002969 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002970 *
2971 * @see #getLayerType()
2972 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002973 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002974 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002975 */
2976 public static final int LAYER_TYPE_NONE = 0;
2977
2978 /**
2979 * <p>Indicates that the view has a software layer. A software layer is backed
2980 * by a bitmap and causes the view to be rendered using Android's software
2981 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002982 *
Romain Guy171c5922011-01-06 10:04:23 -08002983 * <p>Software layers have various usages:</p>
2984 * <p>When the application is not using hardware acceleration, a software layer
2985 * is useful to apply a specific color filter and/or blending mode and/or
2986 * translucency to a view and all its children.</p>
2987 * <p>When the application is using hardware acceleration, a software layer
2988 * is useful to render drawing primitives not supported by the hardware
2989 * accelerated pipeline. It can also be used to cache a complex view tree
2990 * into a texture and reduce the complexity of drawing operations. For instance,
2991 * when animating a complex view tree with a translation, a software layer can
2992 * be used to render the view tree only once.</p>
2993 * <p>Software layers should be avoided when the affected view tree updates
2994 * often. Every update will require to re-render the software layer, which can
2995 * potentially be slow (particularly when hardware acceleration is turned on
2996 * since the layer will have to be uploaded into a hardware texture after every
2997 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002998 *
2999 * @see #getLayerType()
3000 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003001 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003002 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003003 */
3004 public static final int LAYER_TYPE_SOFTWARE = 1;
3005
3006 /**
3007 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3008 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3009 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3010 * rendering pipeline, but only if hardware acceleration is turned on for the
3011 * view hierarchy. When hardware acceleration is turned off, hardware layers
3012 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003013 *
Romain Guy171c5922011-01-06 10:04:23 -08003014 * <p>A hardware layer is useful to apply a specific color filter and/or
3015 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003016 * <p>A hardware layer can be used to cache a complex view tree into a
3017 * texture and reduce the complexity of drawing operations. For instance,
3018 * when animating a complex view tree with a translation, a hardware layer can
3019 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003020 * <p>A hardware layer can also be used to increase the rendering quality when
3021 * rotation transformations are applied on a view. It can also be used to
3022 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003023 *
3024 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003025 * @see #setLayerType(int, android.graphics.Paint)
3026 * @see #LAYER_TYPE_NONE
3027 * @see #LAYER_TYPE_SOFTWARE
3028 */
3029 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003030
Romain Guy3aaff3a2011-01-12 14:18:47 -08003031 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3032 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3033 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3034 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3035 })
Romain Guy171c5922011-01-06 10:04:23 -08003036 int mLayerType = LAYER_TYPE_NONE;
3037 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003038 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003039
3040 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003041 * Set to true when the view is sending hover accessibility events because it
3042 * is the innermost hovered view.
3043 */
3044 private boolean mSendingHoverAccessibilityEvents;
3045
3046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 * Simple constructor to use when creating a view from code.
3048 *
3049 * @param context The Context the view is running in, through which it can
3050 * access the current theme, resources, etc.
3051 */
3052 public View(Context context) {
3053 mContext = context;
3054 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003055 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003056 // Set layout and text direction defaults
3057 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003058 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003059 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3060 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003061 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003062 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003063 mUserPaddingStart = -1;
3064 mUserPaddingEnd = -1;
3065 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 }
3067
3068 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003069 * Delegate for injecting accessiblity functionality.
3070 */
3071 AccessibilityDelegate mAccessibilityDelegate;
3072
3073 /**
3074 * Consistency verifier for debugging purposes.
3075 * @hide
3076 */
3077 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3078 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3079 new InputEventConsistencyVerifier(this, 0) : null;
3080
3081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * Constructor that is called when inflating a view from XML. This is called
3083 * when a view is being constructed from an XML file, supplying attributes
3084 * that were specified in the XML file. This version uses a default style of
3085 * 0, so the only attribute values applied are those in the Context's Theme
3086 * and the given AttributeSet.
3087 *
3088 * <p>
3089 * The method onFinishInflate() will be called after all children have been
3090 * added.
3091 *
3092 * @param context The Context the view is running in, through which it can
3093 * access the current theme, resources, etc.
3094 * @param attrs The attributes of the XML tag that is inflating the view.
3095 * @see #View(Context, AttributeSet, int)
3096 */
3097 public View(Context context, AttributeSet attrs) {
3098 this(context, attrs, 0);
3099 }
3100
3101 /**
3102 * Perform inflation from XML and apply a class-specific base style. This
3103 * constructor of View allows subclasses to use their own base style when
3104 * they are inflating. For example, a Button class's constructor would call
3105 * this version of the super class constructor and supply
3106 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3107 * the theme's button style to modify all of the base view attributes (in
3108 * particular its background) as well as the Button class's attributes.
3109 *
3110 * @param context The Context the view is running in, through which it can
3111 * access the current theme, resources, etc.
3112 * @param attrs The attributes of the XML tag that is inflating the view.
3113 * @param defStyle The default style to apply to this view. If 0, no style
3114 * will be applied (beyond what is included in the theme). This may
3115 * either be an attribute resource, whose value will be retrieved
3116 * from the current theme, or an explicit style resource.
3117 * @see #View(Context, AttributeSet)
3118 */
3119 public View(Context context, AttributeSet attrs, int defStyle) {
3120 this(context);
3121
3122 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3123 defStyle, 0);
3124
3125 Drawable background = null;
3126
3127 int leftPadding = -1;
3128 int topPadding = -1;
3129 int rightPadding = -1;
3130 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003131 int startPadding = -1;
3132 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133
3134 int padding = -1;
3135
3136 int viewFlagValues = 0;
3137 int viewFlagMasks = 0;
3138
3139 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 int x = 0;
3142 int y = 0;
3143
Chet Haase73066682010-11-29 15:55:32 -08003144 float tx = 0;
3145 float ty = 0;
3146 float rotation = 0;
3147 float rotationX = 0;
3148 float rotationY = 0;
3149 float sx = 1f;
3150 float sy = 1f;
3151 boolean transformSet = false;
3152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3154
Adam Powell637d3372010-08-25 14:37:03 -07003155 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 final int N = a.getIndexCount();
3157 for (int i = 0; i < N; i++) {
3158 int attr = a.getIndex(i);
3159 switch (attr) {
3160 case com.android.internal.R.styleable.View_background:
3161 background = a.getDrawable(attr);
3162 break;
3163 case com.android.internal.R.styleable.View_padding:
3164 padding = a.getDimensionPixelSize(attr, -1);
3165 break;
3166 case com.android.internal.R.styleable.View_paddingLeft:
3167 leftPadding = a.getDimensionPixelSize(attr, -1);
3168 break;
3169 case com.android.internal.R.styleable.View_paddingTop:
3170 topPadding = a.getDimensionPixelSize(attr, -1);
3171 break;
3172 case com.android.internal.R.styleable.View_paddingRight:
3173 rightPadding = a.getDimensionPixelSize(attr, -1);
3174 break;
3175 case com.android.internal.R.styleable.View_paddingBottom:
3176 bottomPadding = a.getDimensionPixelSize(attr, -1);
3177 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003178 case com.android.internal.R.styleable.View_paddingStart:
3179 startPadding = a.getDimensionPixelSize(attr, -1);
3180 break;
3181 case com.android.internal.R.styleable.View_paddingEnd:
3182 endPadding = a.getDimensionPixelSize(attr, -1);
3183 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 case com.android.internal.R.styleable.View_scrollX:
3185 x = a.getDimensionPixelOffset(attr, 0);
3186 break;
3187 case com.android.internal.R.styleable.View_scrollY:
3188 y = a.getDimensionPixelOffset(attr, 0);
3189 break;
Chet Haase73066682010-11-29 15:55:32 -08003190 case com.android.internal.R.styleable.View_alpha:
3191 setAlpha(a.getFloat(attr, 1f));
3192 break;
3193 case com.android.internal.R.styleable.View_transformPivotX:
3194 setPivotX(a.getDimensionPixelOffset(attr, 0));
3195 break;
3196 case com.android.internal.R.styleable.View_transformPivotY:
3197 setPivotY(a.getDimensionPixelOffset(attr, 0));
3198 break;
3199 case com.android.internal.R.styleable.View_translationX:
3200 tx = a.getDimensionPixelOffset(attr, 0);
3201 transformSet = true;
3202 break;
3203 case com.android.internal.R.styleable.View_translationY:
3204 ty = a.getDimensionPixelOffset(attr, 0);
3205 transformSet = true;
3206 break;
3207 case com.android.internal.R.styleable.View_rotation:
3208 rotation = a.getFloat(attr, 0);
3209 transformSet = true;
3210 break;
3211 case com.android.internal.R.styleable.View_rotationX:
3212 rotationX = a.getFloat(attr, 0);
3213 transformSet = true;
3214 break;
3215 case com.android.internal.R.styleable.View_rotationY:
3216 rotationY = a.getFloat(attr, 0);
3217 transformSet = true;
3218 break;
3219 case com.android.internal.R.styleable.View_scaleX:
3220 sx = a.getFloat(attr, 1f);
3221 transformSet = true;
3222 break;
3223 case com.android.internal.R.styleable.View_scaleY:
3224 sy = a.getFloat(attr, 1f);
3225 transformSet = true;
3226 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 case com.android.internal.R.styleable.View_id:
3228 mID = a.getResourceId(attr, NO_ID);
3229 break;
3230 case com.android.internal.R.styleable.View_tag:
3231 mTag = a.getText(attr);
3232 break;
3233 case com.android.internal.R.styleable.View_fitsSystemWindows:
3234 if (a.getBoolean(attr, false)) {
3235 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3236 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3237 }
3238 break;
3239 case com.android.internal.R.styleable.View_focusable:
3240 if (a.getBoolean(attr, false)) {
3241 viewFlagValues |= FOCUSABLE;
3242 viewFlagMasks |= FOCUSABLE_MASK;
3243 }
3244 break;
3245 case com.android.internal.R.styleable.View_focusableInTouchMode:
3246 if (a.getBoolean(attr, false)) {
3247 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3248 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3249 }
3250 break;
3251 case com.android.internal.R.styleable.View_clickable:
3252 if (a.getBoolean(attr, false)) {
3253 viewFlagValues |= CLICKABLE;
3254 viewFlagMasks |= CLICKABLE;
3255 }
3256 break;
3257 case com.android.internal.R.styleable.View_longClickable:
3258 if (a.getBoolean(attr, false)) {
3259 viewFlagValues |= LONG_CLICKABLE;
3260 viewFlagMasks |= LONG_CLICKABLE;
3261 }
3262 break;
3263 case com.android.internal.R.styleable.View_saveEnabled:
3264 if (!a.getBoolean(attr, true)) {
3265 viewFlagValues |= SAVE_DISABLED;
3266 viewFlagMasks |= SAVE_DISABLED_MASK;
3267 }
3268 break;
3269 case com.android.internal.R.styleable.View_duplicateParentState:
3270 if (a.getBoolean(attr, false)) {
3271 viewFlagValues |= DUPLICATE_PARENT_STATE;
3272 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3273 }
3274 break;
3275 case com.android.internal.R.styleable.View_visibility:
3276 final int visibility = a.getInt(attr, 0);
3277 if (visibility != 0) {
3278 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3279 viewFlagMasks |= VISIBILITY_MASK;
3280 }
3281 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003282 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003283 // Clear any layout direction flags (included resolved bits) already set
3284 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3285 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003286 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003287 final int value = (layoutDirection != -1) ?
3288 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3289 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003290 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 case com.android.internal.R.styleable.View_drawingCacheQuality:
3292 final int cacheQuality = a.getInt(attr, 0);
3293 if (cacheQuality != 0) {
3294 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3295 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3296 }
3297 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003298 case com.android.internal.R.styleable.View_contentDescription:
3299 mContentDescription = a.getString(attr);
3300 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3302 if (!a.getBoolean(attr, true)) {
3303 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3304 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3305 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003306 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3308 if (!a.getBoolean(attr, true)) {
3309 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3310 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3311 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003312 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 case R.styleable.View_scrollbars:
3314 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3315 if (scrollbars != SCROLLBARS_NONE) {
3316 viewFlagValues |= scrollbars;
3317 viewFlagMasks |= SCROLLBARS_MASK;
3318 initializeScrollbars(a);
3319 }
3320 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003321 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003323 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3324 // Ignore the attribute starting with ICS
3325 break;
3326 }
3327 // With builds < ICS, fall through and apply fading edges
3328 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3330 if (fadingEdge != FADING_EDGE_NONE) {
3331 viewFlagValues |= fadingEdge;
3332 viewFlagMasks |= FADING_EDGE_MASK;
3333 initializeFadingEdge(a);
3334 }
3335 break;
3336 case R.styleable.View_scrollbarStyle:
3337 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3338 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3339 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3340 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3341 }
3342 break;
3343 case R.styleable.View_isScrollContainer:
3344 setScrollContainer = true;
3345 if (a.getBoolean(attr, false)) {
3346 setScrollContainer(true);
3347 }
3348 break;
3349 case com.android.internal.R.styleable.View_keepScreenOn:
3350 if (a.getBoolean(attr, false)) {
3351 viewFlagValues |= KEEP_SCREEN_ON;
3352 viewFlagMasks |= KEEP_SCREEN_ON;
3353 }
3354 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003355 case R.styleable.View_filterTouchesWhenObscured:
3356 if (a.getBoolean(attr, false)) {
3357 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3358 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3359 }
3360 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 case R.styleable.View_nextFocusLeft:
3362 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3363 break;
3364 case R.styleable.View_nextFocusRight:
3365 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3366 break;
3367 case R.styleable.View_nextFocusUp:
3368 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3369 break;
3370 case R.styleable.View_nextFocusDown:
3371 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3372 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003373 case R.styleable.View_nextFocusForward:
3374 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3375 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 case R.styleable.View_minWidth:
3377 mMinWidth = a.getDimensionPixelSize(attr, 0);
3378 break;
3379 case R.styleable.View_minHeight:
3380 mMinHeight = a.getDimensionPixelSize(attr, 0);
3381 break;
Romain Guy9a817362009-05-01 10:57:14 -07003382 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003383 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003384 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003385 + "be used within a restricted context");
3386 }
3387
Romain Guy9a817362009-05-01 10:57:14 -07003388 final String handlerName = a.getString(attr);
3389 if (handlerName != null) {
3390 setOnClickListener(new OnClickListener() {
3391 private Method mHandler;
3392
3393 public void onClick(View v) {
3394 if (mHandler == null) {
3395 try {
3396 mHandler = getContext().getClass().getMethod(handlerName,
3397 View.class);
3398 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003399 int id = getId();
3400 String idText = id == NO_ID ? "" : " with id '"
3401 + getContext().getResources().getResourceEntryName(
3402 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003403 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003404 handlerName + "(View) in the activity "
3405 + getContext().getClass() + " for onClick handler"
3406 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003407 }
3408 }
3409
3410 try {
3411 mHandler.invoke(getContext(), View.this);
3412 } catch (IllegalAccessException e) {
3413 throw new IllegalStateException("Could not execute non "
3414 + "public method of the activity", e);
3415 } catch (InvocationTargetException e) {
3416 throw new IllegalStateException("Could not execute "
3417 + "method of the activity", e);
3418 }
3419 }
3420 });
3421 }
3422 break;
Adam Powell637d3372010-08-25 14:37:03 -07003423 case R.styleable.View_overScrollMode:
3424 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3425 break;
Adam Powell20232d02010-12-08 21:08:53 -08003426 case R.styleable.View_verticalScrollbarPosition:
3427 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3428 break;
Romain Guy171c5922011-01-06 10:04:23 -08003429 case R.styleable.View_layerType:
3430 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3431 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003432 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003433 // Clear any text direction flag already set
3434 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3435 // Set the text direction flags depending on the value of the attribute
3436 final int textDirection = a.getInt(attr, -1);
3437 if (textDirection != -1) {
3438 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3439 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003440 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003441 case R.styleable.View_textAlignment:
3442 // Clear any text alignment flag already set
3443 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3444 // Set the text alignment flag depending on the value of the attribute
3445 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3446 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3447 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003448 case R.styleable.View_importantForAccessibility:
3449 setImportantForAccessibility(a.getInt(attr,
3450 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 }
3452 }
3453
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003454 a.recycle();
3455
Adam Powell637d3372010-08-25 14:37:03 -07003456 setOverScrollMode(overScrollMode);
3457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003459 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 }
3461
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003462 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3463 // layout direction). Those cached values will be used later during padding resolution.
3464 mUserPaddingStart = startPadding;
3465 mUserPaddingEnd = endPadding;
3466
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003467 updateUserPaddingRelative();
3468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 if (padding >= 0) {
3470 leftPadding = padding;
3471 topPadding = padding;
3472 rightPadding = padding;
3473 bottomPadding = padding;
3474 }
3475
3476 // If the user specified the padding (either with android:padding or
3477 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3478 // use the default padding or the padding from the background drawable
3479 // (stored at this point in mPadding*)
3480 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3481 topPadding >= 0 ? topPadding : mPaddingTop,
3482 rightPadding >= 0 ? rightPadding : mPaddingRight,
3483 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3484
3485 if (viewFlagMasks != 0) {
3486 setFlags(viewFlagValues, viewFlagMasks);
3487 }
3488
3489 // Needs to be called after mViewFlags is set
3490 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3491 recomputePadding();
3492 }
3493
3494 if (x != 0 || y != 0) {
3495 scrollTo(x, y);
3496 }
3497
Chet Haase73066682010-11-29 15:55:32 -08003498 if (transformSet) {
3499 setTranslationX(tx);
3500 setTranslationY(ty);
3501 setRotation(rotation);
3502 setRotationX(rotationX);
3503 setRotationY(rotationY);
3504 setScaleX(sx);
3505 setScaleY(sy);
3506 }
3507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3509 setScrollContainer(true);
3510 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003511
3512 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 }
3514
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003515 private void updateUserPaddingRelative() {
3516 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3517 }
3518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 /**
3520 * Non-public constructor for use in testing
3521 */
3522 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003523 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 }
3525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 /**
3527 * <p>
3528 * Initializes the fading edges from a given set of styled attributes. This
3529 * method should be called by subclasses that need fading edges and when an
3530 * instance of these subclasses is created programmatically rather than
3531 * being inflated from XML. This method is automatically called when the XML
3532 * is inflated.
3533 * </p>
3534 *
3535 * @param a the styled attributes set to initialize the fading edges from
3536 */
3537 protected void initializeFadingEdge(TypedArray a) {
3538 initScrollCache();
3539
3540 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3541 R.styleable.View_fadingEdgeLength,
3542 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3543 }
3544
3545 /**
3546 * Returns the size of the vertical faded edges used to indicate that more
3547 * content in this view is visible.
3548 *
3549 * @return The size in pixels of the vertical faded edge or 0 if vertical
3550 * faded edges are not enabled for this view.
3551 * @attr ref android.R.styleable#View_fadingEdgeLength
3552 */
3553 public int getVerticalFadingEdgeLength() {
3554 if (isVerticalFadingEdgeEnabled()) {
3555 ScrollabilityCache cache = mScrollCache;
3556 if (cache != null) {
3557 return cache.fadingEdgeLength;
3558 }
3559 }
3560 return 0;
3561 }
3562
3563 /**
3564 * Set the size of the faded edge used to indicate that more content in this
3565 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003566 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3567 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3568 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 *
3570 * @param length The size in pixels of the faded edge used to indicate that more
3571 * content in this view is visible.
3572 */
3573 public void setFadingEdgeLength(int length) {
3574 initScrollCache();
3575 mScrollCache.fadingEdgeLength = length;
3576 }
3577
3578 /**
3579 * Returns the size of the horizontal faded edges used to indicate that more
3580 * content in this view is visible.
3581 *
3582 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3583 * faded edges are not enabled for this view.
3584 * @attr ref android.R.styleable#View_fadingEdgeLength
3585 */
3586 public int getHorizontalFadingEdgeLength() {
3587 if (isHorizontalFadingEdgeEnabled()) {
3588 ScrollabilityCache cache = mScrollCache;
3589 if (cache != null) {
3590 return cache.fadingEdgeLength;
3591 }
3592 }
3593 return 0;
3594 }
3595
3596 /**
3597 * Returns the width of the vertical scrollbar.
3598 *
3599 * @return The width in pixels of the vertical scrollbar or 0 if there
3600 * is no vertical scrollbar.
3601 */
3602 public int getVerticalScrollbarWidth() {
3603 ScrollabilityCache cache = mScrollCache;
3604 if (cache != null) {
3605 ScrollBarDrawable scrollBar = cache.scrollBar;
3606 if (scrollBar != null) {
3607 int size = scrollBar.getSize(true);
3608 if (size <= 0) {
3609 size = cache.scrollBarSize;
3610 }
3611 return size;
3612 }
3613 return 0;
3614 }
3615 return 0;
3616 }
3617
3618 /**
3619 * Returns the height of the horizontal scrollbar.
3620 *
3621 * @return The height in pixels of the horizontal scrollbar or 0 if
3622 * there is no horizontal scrollbar.
3623 */
3624 protected int getHorizontalScrollbarHeight() {
3625 ScrollabilityCache cache = mScrollCache;
3626 if (cache != null) {
3627 ScrollBarDrawable scrollBar = cache.scrollBar;
3628 if (scrollBar != null) {
3629 int size = scrollBar.getSize(false);
3630 if (size <= 0) {
3631 size = cache.scrollBarSize;
3632 }
3633 return size;
3634 }
3635 return 0;
3636 }
3637 return 0;
3638 }
3639
3640 /**
3641 * <p>
3642 * Initializes the scrollbars from a given set of styled attributes. This
3643 * method should be called by subclasses that need scrollbars and when an
3644 * instance of these subclasses is created programmatically rather than
3645 * being inflated from XML. This method is automatically called when the XML
3646 * is inflated.
3647 * </p>
3648 *
3649 * @param a the styled attributes set to initialize the scrollbars from
3650 */
3651 protected void initializeScrollbars(TypedArray a) {
3652 initScrollCache();
3653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003655
Mike Cleronf116bf82009-09-27 19:14:12 -07003656 if (scrollabilityCache.scrollBar == null) {
3657 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3658 }
Joe Malin32736f02011-01-19 16:14:20 -08003659
Romain Guy8bda2482010-03-02 11:42:11 -08003660 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661
Mike Cleronf116bf82009-09-27 19:14:12 -07003662 if (!fadeScrollbars) {
3663 scrollabilityCache.state = ScrollabilityCache.ON;
3664 }
3665 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003666
3667
Mike Cleronf116bf82009-09-27 19:14:12 -07003668 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3669 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3670 .getScrollBarFadeDuration());
3671 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3672 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3673 ViewConfiguration.getScrollDefaultDelay());
3674
Joe Malin32736f02011-01-19 16:14:20 -08003675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3677 com.android.internal.R.styleable.View_scrollbarSize,
3678 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3679
3680 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3681 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3682
3683 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3684 if (thumb != null) {
3685 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3686 }
3687
3688 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3689 false);
3690 if (alwaysDraw) {
3691 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3692 }
3693
3694 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3695 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3696
3697 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3698 if (thumb != null) {
3699 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3700 }
3701
3702 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3703 false);
3704 if (alwaysDraw) {
3705 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3706 }
3707
3708 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003709 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 }
3711
3712 /**
3713 * <p>
3714 * Initalizes the scrollability cache if necessary.
3715 * </p>
3716 */
3717 private void initScrollCache() {
3718 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003719 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
3721 }
3722
Philip Milne6c8ea062012-04-03 17:38:43 -07003723 private ScrollabilityCache getScrollCache() {
3724 initScrollCache();
3725 return mScrollCache;
3726 }
3727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 /**
Adam Powell20232d02010-12-08 21:08:53 -08003729 * Set the position of the vertical scroll bar. Should be one of
3730 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3731 * {@link #SCROLLBAR_POSITION_RIGHT}.
3732 *
3733 * @param position Where the vertical scroll bar should be positioned.
3734 */
3735 public void setVerticalScrollbarPosition(int position) {
3736 if (mVerticalScrollbarPosition != position) {
3737 mVerticalScrollbarPosition = position;
3738 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003739 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003740 }
3741 }
3742
3743 /**
3744 * @return The position where the vertical scroll bar will show, if applicable.
3745 * @see #setVerticalScrollbarPosition(int)
3746 */
3747 public int getVerticalScrollbarPosition() {
3748 return mVerticalScrollbarPosition;
3749 }
3750
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003751 ListenerInfo getListenerInfo() {
3752 if (mListenerInfo != null) {
3753 return mListenerInfo;
3754 }
3755 mListenerInfo = new ListenerInfo();
3756 return mListenerInfo;
3757 }
3758
Adam Powell20232d02010-12-08 21:08:53 -08003759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 * Register a callback to be invoked when focus of this view changed.
3761 *
3762 * @param l The callback that will run.
3763 */
3764 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003765 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 }
3767
3768 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003769 * Add a listener that will be called when the bounds of the view change due to
3770 * layout processing.
3771 *
3772 * @param listener The listener that will be called when layout bounds change.
3773 */
3774 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003775 ListenerInfo li = getListenerInfo();
3776 if (li.mOnLayoutChangeListeners == null) {
3777 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003778 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003779 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3780 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003781 }
Chet Haase21cd1382010-09-01 17:42:29 -07003782 }
3783
3784 /**
3785 * Remove a listener for layout changes.
3786 *
3787 * @param listener The listener for layout bounds change.
3788 */
3789 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 ListenerInfo li = mListenerInfo;
3791 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003792 return;
3793 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003794 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003795 }
3796
3797 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003798 * Add a listener for attach state changes.
3799 *
3800 * This listener will be called whenever this view is attached or detached
3801 * from a window. Remove the listener using
3802 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3803 *
3804 * @param listener Listener to attach
3805 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3806 */
3807 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 ListenerInfo li = getListenerInfo();
3809 if (li.mOnAttachStateChangeListeners == null) {
3810 li.mOnAttachStateChangeListeners
3811 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003812 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003813 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003814 }
3815
3816 /**
3817 * Remove a listener for attach state changes. The listener will receive no further
3818 * notification of window attach/detach events.
3819 *
3820 * @param listener Listener to remove
3821 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3822 */
3823 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003824 ListenerInfo li = mListenerInfo;
3825 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003826 return;
3827 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003828 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003829 }
3830
3831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 * Returns the focus-change callback registered for this view.
3833 *
3834 * @return The callback, or null if one is not registered.
3835 */
3836 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003837 ListenerInfo li = mListenerInfo;
3838 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 }
3840
3841 /**
3842 * Register a callback to be invoked when this view is clicked. If this view is not
3843 * clickable, it becomes clickable.
3844 *
3845 * @param l The callback that will run
3846 *
3847 * @see #setClickable(boolean)
3848 */
3849 public void setOnClickListener(OnClickListener l) {
3850 if (!isClickable()) {
3851 setClickable(true);
3852 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003853 getListenerInfo().mOnClickListener = l;
3854 }
3855
3856 /**
3857 * Return whether this view has an attached OnClickListener. Returns
3858 * true if there is a listener, false if there is none.
3859 */
3860 public boolean hasOnClickListeners() {
3861 ListenerInfo li = mListenerInfo;
3862 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
3864
3865 /**
3866 * Register a callback to be invoked when this view is clicked and held. If this view is not
3867 * long clickable, it becomes long clickable.
3868 *
3869 * @param l The callback that will run
3870 *
3871 * @see #setLongClickable(boolean)
3872 */
3873 public void setOnLongClickListener(OnLongClickListener l) {
3874 if (!isLongClickable()) {
3875 setLongClickable(true);
3876 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003877 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879
3880 /**
3881 * Register a callback to be invoked when the context menu for this view is
3882 * being built. If this view is not long clickable, it becomes long clickable.
3883 *
3884 * @param l The callback that will run
3885 *
3886 */
3887 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3888 if (!isLongClickable()) {
3889 setLongClickable(true);
3890 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003891 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 }
3893
3894 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003895 * Call this view's OnClickListener, if it is defined. Performs all normal
3896 * actions associated with clicking: reporting accessibility event, playing
3897 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 *
3899 * @return True there was an assigned OnClickListener that was called, false
3900 * otherwise is returned.
3901 */
3902 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003903 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3904
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003905 ListenerInfo li = mListenerInfo;
3906 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003908 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 return true;
3910 }
3911
3912 return false;
3913 }
3914
3915 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003916 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3917 * this only calls the listener, and does not do any associated clicking
3918 * actions like reporting an accessibility event.
3919 *
3920 * @return True there was an assigned OnClickListener that was called, false
3921 * otherwise is returned.
3922 */
3923 public boolean callOnClick() {
3924 ListenerInfo li = mListenerInfo;
3925 if (li != null && li.mOnClickListener != null) {
3926 li.mOnClickListener.onClick(this);
3927 return true;
3928 }
3929 return false;
3930 }
3931
3932 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003933 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3934 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003936 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 */
3938 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003939 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003942 ListenerInfo li = mListenerInfo;
3943 if (li != null && li.mOnLongClickListener != null) {
3944 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 }
3946 if (!handled) {
3947 handled = showContextMenu();
3948 }
3949 if (handled) {
3950 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3951 }
3952 return handled;
3953 }
3954
3955 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003956 * Performs button-related actions during a touch down event.
3957 *
3958 * @param event The event.
3959 * @return True if the down was consumed.
3960 *
3961 * @hide
3962 */
3963 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3964 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3965 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3966 return true;
3967 }
3968 }
3969 return false;
3970 }
3971
3972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 * Bring up the context menu for this view.
3974 *
3975 * @return Whether a context menu was displayed.
3976 */
3977 public boolean showContextMenu() {
3978 return getParent().showContextMenuForChild(this);
3979 }
3980
3981 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003982 * Bring up the context menu for this view, referring to the item under the specified point.
3983 *
3984 * @param x The referenced x coordinate.
3985 * @param y The referenced y coordinate.
3986 * @param metaState The keyboard modifiers that were pressed.
3987 * @return Whether a context menu was displayed.
3988 *
3989 * @hide
3990 */
3991 public boolean showContextMenu(float x, float y, int metaState) {
3992 return showContextMenu();
3993 }
3994
3995 /**
Adam Powell6e346362010-07-23 10:18:23 -07003996 * Start an action mode.
3997 *
3998 * @param callback Callback that will control the lifecycle of the action mode
3999 * @return The new action mode if it is started, null otherwise
4000 *
4001 * @see ActionMode
4002 */
4003 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004004 ViewParent parent = getParent();
4005 if (parent == null) return null;
4006 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004007 }
4008
4009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 * Register a callback to be invoked when a key is pressed in this view.
4011 * @param l the key listener to attach to this view
4012 */
4013 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004014 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 }
4016
4017 /**
4018 * Register a callback to be invoked when a touch event is sent to this view.
4019 * @param l the touch listener to attach to this view
4020 */
4021 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004022 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024
4025 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004026 * Register a callback to be invoked when a generic motion event is sent to this view.
4027 * @param l the generic motion listener to attach to this view
4028 */
4029 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004030 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004031 }
4032
4033 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004034 * Register a callback to be invoked when a hover event is sent to this view.
4035 * @param l the hover listener to attach to this view
4036 */
4037 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004039 }
4040
4041 /**
Joe Malin32736f02011-01-19 16:14:20 -08004042 * Register a drag event listener callback object for this View. The parameter is
4043 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4044 * View, the system calls the
4045 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4046 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004047 */
4048 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004049 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004050 }
4051
4052 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004053 * Give this view focus. This will cause
4054 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 *
4056 * Note: this does not check whether this {@link View} should get focus, it just
4057 * gives it focus no matter what. It should only be called internally by framework
4058 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4059 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004060 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4061 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 * focus moved when requestFocus() is called. It may not always
4063 * apply, in which case use the default View.FOCUS_DOWN.
4064 * @param previouslyFocusedRect The rectangle of the view that had focus
4065 * prior in this View's coordinate system.
4066 */
4067 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4068 if (DBG) {
4069 System.out.println(this + " requestFocus()");
4070 }
4071
4072 if ((mPrivateFlags & FOCUSED) == 0) {
4073 mPrivateFlags |= FOCUSED;
4074
4075 if (mParent != null) {
4076 mParent.requestChildFocus(this, this);
4077 }
4078
4079 onFocusChanged(true, direction, previouslyFocusedRect);
4080 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004081
4082 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4083 notifyAccessibilityStateChanged();
4084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 }
4086 }
4087
4088 /**
4089 * Request that a rectangle of this view be visible on the screen,
4090 * scrolling if necessary just enough.
4091 *
4092 * <p>A View should call this if it maintains some notion of which part
4093 * of its content is interesting. For example, a text editing view
4094 * should call this when its cursor moves.
4095 *
4096 * @param rectangle The rectangle.
4097 * @return Whether any parent scrolled.
4098 */
4099 public boolean requestRectangleOnScreen(Rect rectangle) {
4100 return requestRectangleOnScreen(rectangle, false);
4101 }
4102
4103 /**
4104 * Request that a rectangle of this view be visible on the screen,
4105 * scrolling if necessary just enough.
4106 *
4107 * <p>A View should call this if it maintains some notion of which part
4108 * of its content is interesting. For example, a text editing view
4109 * should call this when its cursor moves.
4110 *
4111 * <p>When <code>immediate</code> is set to true, scrolling will not be
4112 * animated.
4113 *
4114 * @param rectangle The rectangle.
4115 * @param immediate True to forbid animated scrolling, false otherwise
4116 * @return Whether any parent scrolled.
4117 */
4118 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4119 View child = this;
4120 ViewParent parent = mParent;
4121 boolean scrolled = false;
4122 while (parent != null) {
4123 scrolled |= parent.requestChildRectangleOnScreen(child,
4124 rectangle, immediate);
4125
4126 // offset rect so next call has the rectangle in the
4127 // coordinate system of its direct child.
4128 rectangle.offset(child.getLeft(), child.getTop());
4129 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4130
4131 if (!(parent instanceof View)) {
4132 break;
4133 }
Romain Guy8506ab42009-06-11 17:35:47 -07004134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 child = (View) parent;
4136 parent = child.getParent();
4137 }
4138 return scrolled;
4139 }
4140
4141 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004142 * Called when this view wants to give up focus. If focus is cleared
4143 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4144 * <p>
4145 * <strong>Note:</strong> When a View clears focus the framework is trying
4146 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004147 * View is the first from the top that can take focus, then all callbacks
4148 * related to clearing focus will be invoked after wich the framework will
4149 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004150 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 */
4152 public void clearFocus() {
4153 if (DBG) {
4154 System.out.println(this + " clearFocus()");
4155 }
4156
4157 if ((mPrivateFlags & FOCUSED) != 0) {
4158 mPrivateFlags &= ~FOCUSED;
4159
4160 if (mParent != null) {
4161 mParent.clearChildFocus(this);
4162 }
4163
4164 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004167
4168 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004169
4170 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4171 notifyAccessibilityStateChanged();
4172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174 }
4175
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004176 void ensureInputFocusOnFirstFocusable() {
4177 View root = getRootView();
4178 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004179 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
4181 }
4182
4183 /**
4184 * Called internally by the view system when a new view is getting focus.
4185 * This is what clears the old focus.
4186 */
4187 void unFocus() {
4188 if (DBG) {
4189 System.out.println(this + " unFocus()");
4190 }
4191
4192 if ((mPrivateFlags & FOCUSED) != 0) {
4193 mPrivateFlags &= ~FOCUSED;
4194
4195 onFocusChanged(false, 0, null);
4196 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004197
4198 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4199 notifyAccessibilityStateChanged();
4200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 }
4202 }
4203
4204 /**
4205 * Returns true if this view has focus iteself, or is the ancestor of the
4206 * view that has focus.
4207 *
4208 * @return True if this view has or contains focus, false otherwise.
4209 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004210 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 public boolean hasFocus() {
4212 return (mPrivateFlags & FOCUSED) != 0;
4213 }
4214
4215 /**
4216 * Returns true if this view is focusable or if it contains a reachable View
4217 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4218 * is a View whose parents do not block descendants focus.
4219 *
4220 * Only {@link #VISIBLE} views are considered focusable.
4221 *
4222 * @return True if the view is focusable or if the view contains a focusable
4223 * View, false otherwise.
4224 *
4225 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4226 */
4227 public boolean hasFocusable() {
4228 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4229 }
4230
4231 /**
4232 * Called by the view system when the focus state of this view changes.
4233 * When the focus change event is caused by directional navigation, direction
4234 * and previouslyFocusedRect provide insight into where the focus is coming from.
4235 * When overriding, be sure to call up through to the super class so that
4236 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 * @param gainFocus True if the View has focus; false otherwise.
4239 * @param direction The direction focus has moved when requestFocus()
4240 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004241 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4242 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4243 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4245 * system, of the previously focused view. If applicable, this will be
4246 * passed in as finer grained information about where the focus is coming
4247 * from (in addition to direction). Will be <code>null</code> otherwise.
4248 */
4249 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004250 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004251 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4252 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4253 requestAccessibilityFocus();
4254 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004255 }
4256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 InputMethodManager imm = InputMethodManager.peekInstance();
4258 if (!gainFocus) {
4259 if (isPressed()) {
4260 setPressed(false);
4261 }
4262 if (imm != null && mAttachInfo != null
4263 && mAttachInfo.mHasWindowFocus) {
4264 imm.focusOut(this);
4265 }
Romain Guya2431d02009-04-30 16:30:00 -07004266 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 } else if (imm != null && mAttachInfo != null
4268 && mAttachInfo.mHasWindowFocus) {
4269 imm.focusIn(this);
4270 }
Romain Guy8506ab42009-06-11 17:35:47 -07004271
Romain Guy0fd89bf2011-01-26 15:41:30 -08004272 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004273 ListenerInfo li = mListenerInfo;
4274 if (li != null && li.mOnFocusChangeListener != null) {
4275 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
Joe Malin32736f02011-01-19 16:14:20 -08004277
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004278 if (mAttachInfo != null) {
4279 mAttachInfo.mKeyDispatchState.reset(this);
4280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 }
4282
4283 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004284 * Sends an accessibility event of the given type. If accessiiblity is
4285 * not enabled this method has no effect. The default implementation calls
4286 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4287 * to populate information about the event source (this View), then calls
4288 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4289 * populate the text content of the event source including its descendants,
4290 * and last calls
4291 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4292 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004293 * <p>
4294 * If an {@link AccessibilityDelegate} has been specified via calling
4295 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4296 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4297 * responsible for handling this call.
4298 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004299 *
Scott Mainb303d832011-10-12 16:45:18 -07004300 * @param eventType The type of the event to send, as defined by several types from
4301 * {@link android.view.accessibility.AccessibilityEvent}, such as
4302 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4303 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004304 *
4305 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4306 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4307 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004308 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004309 */
4310 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004311 if (mAccessibilityDelegate != null) {
4312 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4313 } else {
4314 sendAccessibilityEventInternal(eventType);
4315 }
4316 }
4317
4318 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004319 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4320 * {@link AccessibilityEvent} to make an announcement which is related to some
4321 * sort of a context change for which none of the events representing UI transitions
4322 * is a good fit. For example, announcing a new page in a book. If accessibility
4323 * is not enabled this method does nothing.
4324 *
4325 * @param text The announcement text.
4326 */
4327 public void announceForAccessibility(CharSequence text) {
4328 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4329 AccessibilityEvent event = AccessibilityEvent.obtain(
4330 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4331 event.getText().add(text);
4332 sendAccessibilityEventUnchecked(event);
4333 }
4334 }
4335
4336 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004337 * @see #sendAccessibilityEvent(int)
4338 *
4339 * Note: Called from the default {@link AccessibilityDelegate}.
4340 */
4341 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004342 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4343 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4344 }
4345 }
4346
4347 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004348 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4349 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004350 * perform a check whether accessibility is enabled.
4351 * <p>
4352 * If an {@link AccessibilityDelegate} has been specified via calling
4353 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4354 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4355 * is responsible for handling this call.
4356 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004357 *
4358 * @param event The event to send.
4359 *
4360 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004361 */
4362 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004363 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004364 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004365 } else {
4366 sendAccessibilityEventUncheckedInternal(event);
4367 }
4368 }
4369
4370 /**
4371 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4372 *
4373 * Note: Called from the default {@link AccessibilityDelegate}.
4374 */
4375 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004376 if (!isShown()) {
4377 return;
4378 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004379 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004380 // Only a subset of accessibility events populates text content.
4381 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4382 dispatchPopulateAccessibilityEvent(event);
4383 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004384 // Intercept accessibility focus events fired by virtual nodes to keep
4385 // track of accessibility focus position in such nodes.
4386 final int eventType = event.getEventType();
4387 switch (eventType) {
4388 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4389 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4390 event.getSourceNodeId());
4391 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4392 ViewRootImpl viewRootImpl = getViewRootImpl();
4393 if (viewRootImpl != null) {
4394 viewRootImpl.setAccessibilityFocusedHost(this);
4395 }
4396 }
4397 } break;
4398 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4399 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4400 event.getSourceNodeId());
4401 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4402 ViewRootImpl viewRootImpl = getViewRootImpl();
4403 if (viewRootImpl != null) {
4404 viewRootImpl.setAccessibilityFocusedHost(null);
4405 }
4406 }
4407 } break;
4408 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004409 // In the beginning we called #isShown(), so we know that getParent() is not null.
4410 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004411 }
4412
4413 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004414 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4415 * to its children for adding their text content to the event. Note that the
4416 * event text is populated in a separate dispatch path since we add to the
4417 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004418 * A typical implementation will call
4419 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4420 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4421 * on each child. Override this method if custom population of the event text
4422 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004423 * <p>
4424 * If an {@link AccessibilityDelegate} has been specified via calling
4425 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4426 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4427 * is responsible for handling this call.
4428 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004429 * <p>
4430 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4431 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4432 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004433 *
4434 * @param event The event.
4435 *
4436 * @return True if the event population was completed.
4437 */
4438 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004439 if (mAccessibilityDelegate != null) {
4440 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4441 } else {
4442 return dispatchPopulateAccessibilityEventInternal(event);
4443 }
4444 }
4445
4446 /**
4447 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4448 *
4449 * Note: Called from the default {@link AccessibilityDelegate}.
4450 */
4451 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004452 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004453 return false;
4454 }
4455
4456 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004457 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004458 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004459 * text content. While this method is free to modify event
4460 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4462 * <p>
4463 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004464 * to the text added by the super implementation:
4465 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004466 * super.onPopulateAccessibilityEvent(event);
4467 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4468 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4469 * mCurrentDate.getTimeInMillis(), flags);
4470 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004471 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004472 * <p>
4473 * If an {@link AccessibilityDelegate} has been specified via calling
4474 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4475 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4476 * is responsible for handling this call.
4477 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004478 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4479 * information to the event, in case the default implementation has basic information to add.
4480 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004481 *
4482 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004483 *
4484 * @see #sendAccessibilityEvent(int)
4485 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004486 */
4487 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004488 if (mAccessibilityDelegate != null) {
4489 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4490 } else {
4491 onPopulateAccessibilityEventInternal(event);
4492 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004493 }
4494
4495 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004496 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4497 *
4498 * Note: Called from the default {@link AccessibilityDelegate}.
4499 */
4500 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4501
4502 }
4503
4504 /**
4505 * Initializes an {@link AccessibilityEvent} with information about
4506 * this View which is the event source. In other words, the source of
4507 * an accessibility event is the view whose state change triggered firing
4508 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004509 * <p>
4510 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004511 * to properties set by the super implementation:
4512 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4513 * super.onInitializeAccessibilityEvent(event);
4514 * event.setPassword(true);
4515 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004516 * <p>
4517 * If an {@link AccessibilityDelegate} has been specified via calling
4518 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4519 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4520 * is responsible for handling this call.
4521 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004522 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4523 * information to the event, in case the default implementation has basic information to add.
4524 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004525 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004526 *
4527 * @see #sendAccessibilityEvent(int)
4528 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4529 */
4530 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004531 if (mAccessibilityDelegate != null) {
4532 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4533 } else {
4534 onInitializeAccessibilityEventInternal(event);
4535 }
4536 }
4537
4538 /**
4539 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4540 *
4541 * Note: Called from the default {@link AccessibilityDelegate}.
4542 */
4543 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004544 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004545 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004546 event.setPackageName(getContext().getPackageName());
4547 event.setEnabled(isEnabled());
4548 event.setContentDescription(mContentDescription);
4549
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004550 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004551 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004552 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4553 FOCUSABLES_ALL);
4554 event.setItemCount(focusablesTempList.size());
4555 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4556 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004557 }
4558 }
4559
4560 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004561 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4562 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4563 * This method is responsible for obtaining an accessibility node info from a
4564 * pool of reusable instances and calling
4565 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4566 * initialize the former.
4567 * <p>
4568 * Note: The client is responsible for recycling the obtained instance by calling
4569 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4570 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004571 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004572 * @return A populated {@link AccessibilityNodeInfo}.
4573 *
4574 * @see AccessibilityNodeInfo
4575 */
4576 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004577 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4578 if (provider != null) {
4579 return provider.createAccessibilityNodeInfo(View.NO_ID);
4580 } else {
4581 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4582 onInitializeAccessibilityNodeInfo(info);
4583 return info;
4584 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004585 }
4586
4587 /**
4588 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4589 * The base implementation sets:
4590 * <ul>
4591 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004592 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4593 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004594 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4595 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4596 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4597 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4598 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4599 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4600 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4601 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4602 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4603 * </ul>
4604 * <p>
4605 * Subclasses should override this method, call the super implementation,
4606 * and set additional attributes.
4607 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004608 * <p>
4609 * If an {@link AccessibilityDelegate} has been specified via calling
4610 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4611 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4612 * is responsible for handling this call.
4613 * </p>
4614 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004615 * @param info The instance to initialize.
4616 */
4617 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004618 if (mAccessibilityDelegate != null) {
4619 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4620 } else {
4621 onInitializeAccessibilityNodeInfoInternal(info);
4622 }
4623 }
4624
4625 /**
4626 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4627 *
4628 * Note: Called from the default {@link AccessibilityDelegate}.
4629 */
4630 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004631 Rect bounds = mAttachInfo.mTmpInvalRect;
4632 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004633 info.setBoundsInParent(bounds);
4634
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004635 getGlobalVisibleRect(bounds);
4636 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004637 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004638
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004639 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004640 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004641 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004642 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004643 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004644 }
4645
4646 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004647 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004648 info.setContentDescription(getContentDescription());
4649
4650 info.setEnabled(isEnabled());
4651 info.setClickable(isClickable());
4652 info.setFocusable(isFocusable());
4653 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004654 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004655 info.setSelected(isSelected());
4656 info.setLongClickable(isLongClickable());
4657
4658 // TODO: These make sense only if we are in an AdapterView but all
4659 // views can be selected. Maybe from accessiiblity perspective
4660 // we should report as selectable view in an AdapterView.
4661 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4662 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4663
4664 if (isFocusable()) {
4665 if (isFocused()) {
4666 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4667 } else {
4668 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4669 }
4670 }
4671 }
4672
4673 /**
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004674 * Computes whether this view is visible on the screen.
4675 *
4676 * @return Whether the view is visible on the screen.
4677 */
4678 boolean isDisplayedOnScreen() {
4679 // The first two checks are made also made by isShown() which
4680 // however traverses the tree up to the parent to catch that.
4681 // Therefore, we do some fail fast check to minimize the up
4682 // tree traversal.
4683 return (mAttachInfo != null
4684 && mAttachInfo.mWindowVisibility == View.VISIBLE
4685 && getAlpha() > 0
4686 && isShown()
4687 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4688 }
4689
4690 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004691 * Sets a delegate for implementing accessibility support via compositon as
4692 * opposed to inheritance. The delegate's primary use is for implementing
4693 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4694 *
4695 * @param delegate The delegate instance.
4696 *
4697 * @see AccessibilityDelegate
4698 */
4699 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4700 mAccessibilityDelegate = delegate;
4701 }
4702
4703 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004704 * Gets the provider for managing a virtual view hierarchy rooted at this View
4705 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4706 * that explore the window content.
4707 * <p>
4708 * If this method returns an instance, this instance is responsible for managing
4709 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4710 * View including the one representing the View itself. Similarly the returned
4711 * instance is responsible for performing accessibility actions on any virtual
4712 * view or the root view itself.
4713 * </p>
4714 * <p>
4715 * If an {@link AccessibilityDelegate} has been specified via calling
4716 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4717 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4718 * is responsible for handling this call.
4719 * </p>
4720 *
4721 * @return The provider.
4722 *
4723 * @see AccessibilityNodeProvider
4724 */
4725 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4726 if (mAccessibilityDelegate != null) {
4727 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4728 } else {
4729 return null;
4730 }
4731 }
4732
4733 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004734 * Gets the unique identifier of this view on the screen for accessibility purposes.
4735 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4736 *
4737 * @return The view accessibility id.
4738 *
4739 * @hide
4740 */
4741 public int getAccessibilityViewId() {
4742 if (mAccessibilityViewId == NO_ID) {
4743 mAccessibilityViewId = sNextAccessibilityViewId++;
4744 }
4745 return mAccessibilityViewId;
4746 }
4747
4748 /**
4749 * Gets the unique identifier of the window in which this View reseides.
4750 *
4751 * @return The window accessibility id.
4752 *
4753 * @hide
4754 */
4755 public int getAccessibilityWindowId() {
4756 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4757 }
4758
4759 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004760 * Gets the {@link View} description. It briefly describes the view and is
4761 * primarily used for accessibility support. Set this property to enable
4762 * better accessibility support for your application. This is especially
4763 * true for views that do not have textual representation (For example,
4764 * ImageButton).
4765 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004766 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004767 *
4768 * @attr ref android.R.styleable#View_contentDescription
4769 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004770 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004771 public CharSequence getContentDescription() {
4772 return mContentDescription;
4773 }
4774
4775 /**
4776 * Sets the {@link View} description. It briefly describes the view and is
4777 * primarily used for accessibility support. Set this property to enable
4778 * better accessibility support for your application. This is especially
4779 * true for views that do not have textual representation (For example,
4780 * ImageButton).
4781 *
4782 * @param contentDescription The content description.
4783 *
4784 * @attr ref android.R.styleable#View_contentDescription
4785 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004786 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004787 public void setContentDescription(CharSequence contentDescription) {
4788 mContentDescription = contentDescription;
4789 }
4790
4791 /**
Romain Guya2431d02009-04-30 16:30:00 -07004792 * Invoked whenever this view loses focus, either by losing window focus or by losing
4793 * focus within its window. This method can be used to clear any state tied to the
4794 * focus. For instance, if a button is held pressed with the trackball and the window
4795 * loses focus, this method can be used to cancel the press.
4796 *
4797 * Subclasses of View overriding this method should always call super.onFocusLost().
4798 *
4799 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004800 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004801 *
4802 * @hide pending API council approval
4803 */
4804 protected void onFocusLost() {
4805 resetPressedState();
4806 }
4807
4808 private void resetPressedState() {
4809 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4810 return;
4811 }
4812
4813 if (isPressed()) {
4814 setPressed(false);
4815
4816 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004817 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004818 }
4819 }
4820 }
4821
4822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 * Returns true if this view has focus
4824 *
4825 * @return True if this view has focus, false otherwise.
4826 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004827 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 public boolean isFocused() {
4829 return (mPrivateFlags & FOCUSED) != 0;
4830 }
4831
4832 /**
4833 * Find the view in the hierarchy rooted at this view that currently has
4834 * focus.
4835 *
4836 * @return The view that currently has focus, or null if no focused view can
4837 * be found.
4838 */
4839 public View findFocus() {
4840 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4841 }
4842
4843 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004844 * Indicates whether this view is one of the set of scrollable containers in
4845 * its window.
4846 *
4847 * @return whether this view is one of the set of scrollable containers in
4848 * its window
4849 *
4850 * @attr ref android.R.styleable#View_isScrollContainer
4851 */
4852 public boolean isScrollContainer() {
4853 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4854 }
4855
4856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 * Change whether this view is one of the set of scrollable containers in
4858 * its window. This will be used to determine whether the window can
4859 * resize or must pan when a soft input area is open -- scrollable
4860 * containers allow the window to use resize mode since the container
4861 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004862 *
4863 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 */
4865 public void setScrollContainer(boolean isScrollContainer) {
4866 if (isScrollContainer) {
4867 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4868 mAttachInfo.mScrollContainers.add(this);
4869 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4870 }
4871 mPrivateFlags |= SCROLL_CONTAINER;
4872 } else {
4873 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4874 mAttachInfo.mScrollContainers.remove(this);
4875 }
4876 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4877 }
4878 }
4879
4880 /**
4881 * Returns the quality of the drawing cache.
4882 *
4883 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4884 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4885 *
4886 * @see #setDrawingCacheQuality(int)
4887 * @see #setDrawingCacheEnabled(boolean)
4888 * @see #isDrawingCacheEnabled()
4889 *
4890 * @attr ref android.R.styleable#View_drawingCacheQuality
4891 */
4892 public int getDrawingCacheQuality() {
4893 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4894 }
4895
4896 /**
4897 * Set the drawing cache quality of this view. This value is used only when the
4898 * drawing cache is enabled
4899 *
4900 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4901 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4902 *
4903 * @see #getDrawingCacheQuality()
4904 * @see #setDrawingCacheEnabled(boolean)
4905 * @see #isDrawingCacheEnabled()
4906 *
4907 * @attr ref android.R.styleable#View_drawingCacheQuality
4908 */
4909 public void setDrawingCacheQuality(int quality) {
4910 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4911 }
4912
4913 /**
4914 * Returns whether the screen should remain on, corresponding to the current
4915 * value of {@link #KEEP_SCREEN_ON}.
4916 *
4917 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4918 *
4919 * @see #setKeepScreenOn(boolean)
4920 *
4921 * @attr ref android.R.styleable#View_keepScreenOn
4922 */
4923 public boolean getKeepScreenOn() {
4924 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4925 }
4926
4927 /**
4928 * Controls whether the screen should remain on, modifying the
4929 * value of {@link #KEEP_SCREEN_ON}.
4930 *
4931 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4932 *
4933 * @see #getKeepScreenOn()
4934 *
4935 * @attr ref android.R.styleable#View_keepScreenOn
4936 */
4937 public void setKeepScreenOn(boolean keepScreenOn) {
4938 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4939 }
4940
4941 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004942 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4943 * @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 -08004944 *
4945 * @attr ref android.R.styleable#View_nextFocusLeft
4946 */
4947 public int getNextFocusLeftId() {
4948 return mNextFocusLeftId;
4949 }
4950
4951 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004952 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4953 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4954 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 *
4956 * @attr ref android.R.styleable#View_nextFocusLeft
4957 */
4958 public void setNextFocusLeftId(int nextFocusLeftId) {
4959 mNextFocusLeftId = nextFocusLeftId;
4960 }
4961
4962 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004963 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4964 * @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 -08004965 *
4966 * @attr ref android.R.styleable#View_nextFocusRight
4967 */
4968 public int getNextFocusRightId() {
4969 return mNextFocusRightId;
4970 }
4971
4972 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004973 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4974 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4975 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 *
4977 * @attr ref android.R.styleable#View_nextFocusRight
4978 */
4979 public void setNextFocusRightId(int nextFocusRightId) {
4980 mNextFocusRightId = nextFocusRightId;
4981 }
4982
4983 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004984 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4985 * @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 -08004986 *
4987 * @attr ref android.R.styleable#View_nextFocusUp
4988 */
4989 public int getNextFocusUpId() {
4990 return mNextFocusUpId;
4991 }
4992
4993 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004994 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4995 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4996 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 *
4998 * @attr ref android.R.styleable#View_nextFocusUp
4999 */
5000 public void setNextFocusUpId(int nextFocusUpId) {
5001 mNextFocusUpId = nextFocusUpId;
5002 }
5003
5004 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005005 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5006 * @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 -08005007 *
5008 * @attr ref android.R.styleable#View_nextFocusDown
5009 */
5010 public int getNextFocusDownId() {
5011 return mNextFocusDownId;
5012 }
5013
5014 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005015 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5016 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5017 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 *
5019 * @attr ref android.R.styleable#View_nextFocusDown
5020 */
5021 public void setNextFocusDownId(int nextFocusDownId) {
5022 mNextFocusDownId = nextFocusDownId;
5023 }
5024
5025 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005026 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5027 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5028 *
5029 * @attr ref android.R.styleable#View_nextFocusForward
5030 */
5031 public int getNextFocusForwardId() {
5032 return mNextFocusForwardId;
5033 }
5034
5035 /**
5036 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5037 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5038 * decide automatically.
5039 *
5040 * @attr ref android.R.styleable#View_nextFocusForward
5041 */
5042 public void setNextFocusForwardId(int nextFocusForwardId) {
5043 mNextFocusForwardId = nextFocusForwardId;
5044 }
5045
5046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 * Returns the visibility of this view and all of its ancestors
5048 *
5049 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5050 */
5051 public boolean isShown() {
5052 View current = this;
5053 //noinspection ConstantConditions
5054 do {
5055 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5056 return false;
5057 }
5058 ViewParent parent = current.mParent;
5059 if (parent == null) {
5060 return false; // We are not attached to the view root
5061 }
5062 if (!(parent instanceof View)) {
5063 return true;
5064 }
5065 current = (View) parent;
5066 } while (current != null);
5067
5068 return false;
5069 }
5070
5071 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005072 * Called by the view hierarchy when the content insets for a window have
5073 * changed, to allow it to adjust its content to fit within those windows.
5074 * The content insets tell you the space that the status bar, input method,
5075 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005077 * <p>You do not normally need to deal with this function, since the default
5078 * window decoration given to applications takes care of applying it to the
5079 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5080 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5081 * and your content can be placed under those system elements. You can then
5082 * use this method within your view hierarchy if you have parts of your UI
5083 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005085 * <p>The default implementation of this method simply applies the content
5086 * inset's to the view's padding. This can be enabled through
5087 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5088 * the method and handle the insets however you would like. Note that the
5089 * insets provided by the framework are always relative to the far edges
5090 * of the window, not accounting for the location of the called view within
5091 * that window. (In fact when this method is called you do not yet know
5092 * where the layout will place the view, as it is done before layout happens.)
5093 *
5094 * <p>Note: unlike many View methods, there is no dispatch phase to this
5095 * call. If you are overriding it in a ViewGroup and want to allow the
5096 * call to continue to your children, you must be sure to call the super
5097 * implementation.
5098 *
5099 * @param insets Current content insets of the window. Prior to
5100 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5101 * the insets or else you and Android will be unhappy.
5102 *
5103 * @return Return true if this view applied the insets and it should not
5104 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 */
5106 protected boolean fitSystemWindows(Rect insets) {
5107 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005108 mUserPaddingStart = -1;
5109 mUserPaddingEnd = -1;
5110 mUserPaddingRelative = false;
5111 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5112 || mAttachInfo == null
5113 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5114 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5115 return true;
5116 } else {
5117 internalSetPadding(0, 0, 0, 0);
5118 return false;
5119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 }
5121 return false;
5122 }
5123
5124 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005125 * Set whether or not this view should account for system screen decorations
5126 * such as the status bar and inset its content. This allows this view to be
5127 * positioned in absolute screen coordinates and remain visible to the user.
5128 *
5129 * <p>This should only be used by top-level window decor views.
5130 *
5131 * @param fitSystemWindows true to inset content for system screen decorations, false for
5132 * default behavior.
5133 *
5134 * @attr ref android.R.styleable#View_fitsSystemWindows
5135 */
5136 public void setFitsSystemWindows(boolean fitSystemWindows) {
5137 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5138 }
5139
5140 /**
5141 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5142 * will account for system screen decorations such as the status bar and inset its
5143 * content. This allows the view to be positioned in absolute screen coordinates
5144 * and remain visible to the user.
5145 *
5146 * @return true if this view will adjust its content bounds for system screen decorations.
5147 *
5148 * @attr ref android.R.styleable#View_fitsSystemWindows
5149 */
5150 public boolean fitsSystemWindows() {
5151 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5152 }
5153
5154 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005155 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5156 */
5157 public void requestFitSystemWindows() {
5158 if (mParent != null) {
5159 mParent.requestFitSystemWindows();
5160 }
5161 }
5162
5163 /**
5164 * For use by PhoneWindow to make its own system window fitting optional.
5165 * @hide
5166 */
5167 public void makeOptionalFitsSystemWindows() {
5168 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5169 }
5170
5171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 * Returns the visibility status for this view.
5173 *
5174 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5175 * @attr ref android.R.styleable#View_visibility
5176 */
5177 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005178 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5179 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5180 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 })
5182 public int getVisibility() {
5183 return mViewFlags & VISIBILITY_MASK;
5184 }
5185
5186 /**
5187 * Set the enabled state of this view.
5188 *
5189 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5190 * @attr ref android.R.styleable#View_visibility
5191 */
5192 @RemotableViewMethod
5193 public void setVisibility(int visibility) {
5194 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005195 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 }
5197
5198 /**
5199 * Returns the enabled status for this view. The interpretation of the
5200 * enabled state varies by subclass.
5201 *
5202 * @return True if this view is enabled, false otherwise.
5203 */
5204 @ViewDebug.ExportedProperty
5205 public boolean isEnabled() {
5206 return (mViewFlags & ENABLED_MASK) == ENABLED;
5207 }
5208
5209 /**
5210 * Set the enabled state of this view. The interpretation of the enabled
5211 * state varies by subclass.
5212 *
5213 * @param enabled True if this view is enabled, false otherwise.
5214 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005215 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005217 if (enabled == isEnabled()) return;
5218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5220
5221 /*
5222 * The View most likely has to change its appearance, so refresh
5223 * the drawable state.
5224 */
5225 refreshDrawableState();
5226
5227 // Invalidate too, since the default behavior for views is to be
5228 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005229 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 }
5231
5232 /**
5233 * Set whether this view can receive the focus.
5234 *
5235 * Setting this to false will also ensure that this view is not focusable
5236 * in touch mode.
5237 *
5238 * @param focusable If true, this view can receive the focus.
5239 *
5240 * @see #setFocusableInTouchMode(boolean)
5241 * @attr ref android.R.styleable#View_focusable
5242 */
5243 public void setFocusable(boolean focusable) {
5244 if (!focusable) {
5245 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5246 }
5247 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5248 }
5249
5250 /**
5251 * Set whether this view can receive focus while in touch mode.
5252 *
5253 * Setting this to true will also ensure that this view is focusable.
5254 *
5255 * @param focusableInTouchMode If true, this view can receive the focus while
5256 * in touch mode.
5257 *
5258 * @see #setFocusable(boolean)
5259 * @attr ref android.R.styleable#View_focusableInTouchMode
5260 */
5261 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5262 // Focusable in touch mode should always be set before the focusable flag
5263 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5264 // which, in touch mode, will not successfully request focus on this view
5265 // because the focusable in touch mode flag is not set
5266 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5267 if (focusableInTouchMode) {
5268 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5269 }
5270 }
5271
5272 /**
5273 * Set whether this view should have sound effects enabled for events such as
5274 * clicking and touching.
5275 *
5276 * <p>You may wish to disable sound effects for a view if you already play sounds,
5277 * for instance, a dial key that plays dtmf tones.
5278 *
5279 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5280 * @see #isSoundEffectsEnabled()
5281 * @see #playSoundEffect(int)
5282 * @attr ref android.R.styleable#View_soundEffectsEnabled
5283 */
5284 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5285 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5286 }
5287
5288 /**
5289 * @return whether this view should have sound effects enabled for events such as
5290 * clicking and touching.
5291 *
5292 * @see #setSoundEffectsEnabled(boolean)
5293 * @see #playSoundEffect(int)
5294 * @attr ref android.R.styleable#View_soundEffectsEnabled
5295 */
5296 @ViewDebug.ExportedProperty
5297 public boolean isSoundEffectsEnabled() {
5298 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5299 }
5300
5301 /**
5302 * Set whether this view should have haptic feedback for events such as
5303 * long presses.
5304 *
5305 * <p>You may wish to disable haptic feedback if your view already controls
5306 * its own haptic feedback.
5307 *
5308 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5309 * @see #isHapticFeedbackEnabled()
5310 * @see #performHapticFeedback(int)
5311 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5312 */
5313 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5314 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5315 }
5316
5317 /**
5318 * @return whether this view should have haptic feedback enabled for events
5319 * long presses.
5320 *
5321 * @see #setHapticFeedbackEnabled(boolean)
5322 * @see #performHapticFeedback(int)
5323 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5324 */
5325 @ViewDebug.ExportedProperty
5326 public boolean isHapticFeedbackEnabled() {
5327 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5328 }
5329
5330 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005331 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005332 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005333 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5334 * {@link #LAYOUT_DIRECTION_RTL},
5335 * {@link #LAYOUT_DIRECTION_INHERIT} or
5336 * {@link #LAYOUT_DIRECTION_LOCALE}.
5337 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005338 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005339 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005340 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5341 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5342 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5343 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005344 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005345 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005346 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005347 }
5348
5349 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005350 * Set the layout direction for this view. This will propagate a reset of layout direction
5351 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005352 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005353 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5354 * {@link #LAYOUT_DIRECTION_RTL},
5355 * {@link #LAYOUT_DIRECTION_INHERIT} or
5356 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005357 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005358 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005359 */
5360 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005361 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005362 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005363 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005364 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005365 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005366 // Set the new layout direction (filtered) and ask for a layout pass
5367 mPrivateFlags2 |=
5368 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5369 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005370 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005371 }
5372
5373 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005374 * Returns the resolved layout direction for this view.
5375 *
5376 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005377 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005378 */
5379 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005380 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5381 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005382 })
5383 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005384 // The layout diretion will be resolved only if needed
5385 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5386 resolveLayoutDirection();
5387 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005388 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005389 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5390 }
5391
5392 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005393 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5394 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005395 *
5396 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005397 */
5398 @ViewDebug.ExportedProperty(category = "layout")
5399 public boolean isLayoutRtl() {
5400 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5401 }
5402
5403 /**
Adam Powell539ee872012-02-03 19:00:49 -08005404 * Indicates whether the view is currently tracking transient state that the
5405 * app should not need to concern itself with saving and restoring, but that
5406 * the framework should take special note to preserve when possible.
5407 *
5408 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005409 */
5410 @ViewDebug.ExportedProperty(category = "layout")
5411 public boolean hasTransientState() {
5412 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5413 }
5414
5415 /**
5416 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005417 * framework should attempt to preserve when possible. This flag is reference counted,
5418 * so every call to setHasTransientState(true) should be paired with a later call
5419 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005420 *
5421 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005422 */
5423 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005424 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5425 mTransientStateCount - 1;
5426 if (mTransientStateCount < 0) {
5427 mTransientStateCount = 0;
5428 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5429 "unmatched pair of setHasTransientState calls");
5430 }
5431 if ((hasTransientState && mTransientStateCount == 1) ||
5432 (hasTransientState && mTransientStateCount == 0)) {
5433 // update flag if we've just incremented up from 0 or decremented down to 0
5434 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5435 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5436 if (mParent != null) {
5437 try {
5438 mParent.childHasTransientStateChanged(this, hasTransientState);
5439 } catch (AbstractMethodError e) {
5440 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5441 " does not fully implement ViewParent", e);
5442 }
Adam Powell539ee872012-02-03 19:00:49 -08005443 }
5444 }
5445 }
5446
5447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 * If this view doesn't do any drawing on its own, set this flag to
5449 * allow further optimizations. By default, this flag is not set on
5450 * View, but could be set on some View subclasses such as ViewGroup.
5451 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005452 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5453 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 *
5455 * @param willNotDraw whether or not this View draw on its own
5456 */
5457 public void setWillNotDraw(boolean willNotDraw) {
5458 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5459 }
5460
5461 /**
5462 * Returns whether or not this View draws on its own.
5463 *
5464 * @return true if this view has nothing to draw, false otherwise
5465 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005466 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 public boolean willNotDraw() {
5468 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5469 }
5470
5471 /**
5472 * When a View's drawing cache is enabled, drawing is redirected to an
5473 * offscreen bitmap. Some views, like an ImageView, must be able to
5474 * bypass this mechanism if they already draw a single bitmap, to avoid
5475 * unnecessary usage of the memory.
5476 *
5477 * @param willNotCacheDrawing true if this view does not cache its
5478 * drawing, false otherwise
5479 */
5480 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5481 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5482 }
5483
5484 /**
5485 * Returns whether or not this View can cache its drawing or not.
5486 *
5487 * @return true if this view does not cache its drawing, false otherwise
5488 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005489 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 public boolean willNotCacheDrawing() {
5491 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5492 }
5493
5494 /**
5495 * Indicates whether this view reacts to click events or not.
5496 *
5497 * @return true if the view is clickable, false otherwise
5498 *
5499 * @see #setClickable(boolean)
5500 * @attr ref android.R.styleable#View_clickable
5501 */
5502 @ViewDebug.ExportedProperty
5503 public boolean isClickable() {
5504 return (mViewFlags & CLICKABLE) == CLICKABLE;
5505 }
5506
5507 /**
5508 * Enables or disables click events for this view. When a view
5509 * is clickable it will change its state to "pressed" on every click.
5510 * Subclasses should set the view clickable to visually react to
5511 * user's clicks.
5512 *
5513 * @param clickable true to make the view clickable, false otherwise
5514 *
5515 * @see #isClickable()
5516 * @attr ref android.R.styleable#View_clickable
5517 */
5518 public void setClickable(boolean clickable) {
5519 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5520 }
5521
5522 /**
5523 * Indicates whether this view reacts to long click events or not.
5524 *
5525 * @return true if the view is long clickable, false otherwise
5526 *
5527 * @see #setLongClickable(boolean)
5528 * @attr ref android.R.styleable#View_longClickable
5529 */
5530 public boolean isLongClickable() {
5531 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5532 }
5533
5534 /**
5535 * Enables or disables long click events for this view. When a view is long
5536 * clickable it reacts to the user holding down the button for a longer
5537 * duration than a tap. This event can either launch the listener or a
5538 * context menu.
5539 *
5540 * @param longClickable true to make the view long clickable, false otherwise
5541 * @see #isLongClickable()
5542 * @attr ref android.R.styleable#View_longClickable
5543 */
5544 public void setLongClickable(boolean longClickable) {
5545 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5546 }
5547
5548 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005549 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 *
5551 * @see #isClickable()
5552 * @see #setClickable(boolean)
5553 *
5554 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5555 * the View's internal state from a previously set "pressed" state.
5556 */
5557 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005558 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 if (pressed) {
5561 mPrivateFlags |= PRESSED;
5562 } else {
5563 mPrivateFlags &= ~PRESSED;
5564 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005565
5566 if (needsRefresh) {
5567 refreshDrawableState();
5568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 dispatchSetPressed(pressed);
5570 }
5571
5572 /**
5573 * Dispatch setPressed to all of this View's children.
5574 *
5575 * @see #setPressed(boolean)
5576 *
5577 * @param pressed The new pressed state
5578 */
5579 protected void dispatchSetPressed(boolean pressed) {
5580 }
5581
5582 /**
5583 * Indicates whether the view is currently in pressed state. Unless
5584 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5585 * the pressed state.
5586 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005587 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 * @see #isClickable()
5589 * @see #setClickable(boolean)
5590 *
5591 * @return true if the view is currently pressed, false otherwise
5592 */
5593 public boolean isPressed() {
5594 return (mPrivateFlags & PRESSED) == PRESSED;
5595 }
5596
5597 /**
5598 * Indicates whether this view will save its state (that is,
5599 * whether its {@link #onSaveInstanceState} method will be called).
5600 *
5601 * @return Returns true if the view state saving is enabled, else false.
5602 *
5603 * @see #setSaveEnabled(boolean)
5604 * @attr ref android.R.styleable#View_saveEnabled
5605 */
5606 public boolean isSaveEnabled() {
5607 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5608 }
5609
5610 /**
5611 * Controls whether the saving of this view's state is
5612 * enabled (that is, whether its {@link #onSaveInstanceState} method
5613 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005614 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 * for its state to be saved. This flag can only disable the
5616 * saving of this view; any child views may still have their state saved.
5617 *
5618 * @param enabled Set to false to <em>disable</em> state saving, or true
5619 * (the default) to allow it.
5620 *
5621 * @see #isSaveEnabled()
5622 * @see #setId(int)
5623 * @see #onSaveInstanceState()
5624 * @attr ref android.R.styleable#View_saveEnabled
5625 */
5626 public void setSaveEnabled(boolean enabled) {
5627 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5628 }
5629
Jeff Brown85a31762010-09-01 17:01:00 -07005630 /**
5631 * Gets whether the framework should discard touches when the view's
5632 * window is obscured by another visible window.
5633 * Refer to the {@link View} security documentation for more details.
5634 *
5635 * @return True if touch filtering is enabled.
5636 *
5637 * @see #setFilterTouchesWhenObscured(boolean)
5638 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5639 */
5640 @ViewDebug.ExportedProperty
5641 public boolean getFilterTouchesWhenObscured() {
5642 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5643 }
5644
5645 /**
5646 * Sets whether the framework should discard touches when the view's
5647 * window is obscured by another visible window.
5648 * Refer to the {@link View} security documentation for more details.
5649 *
5650 * @param enabled True if touch filtering should be enabled.
5651 *
5652 * @see #getFilterTouchesWhenObscured
5653 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5654 */
5655 public void setFilterTouchesWhenObscured(boolean enabled) {
5656 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5657 FILTER_TOUCHES_WHEN_OBSCURED);
5658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659
5660 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005661 * Indicates whether the entire hierarchy under this view will save its
5662 * state when a state saving traversal occurs from its parent. The default
5663 * is true; if false, these views will not be saved unless
5664 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5665 *
5666 * @return Returns true if the view state saving from parent is enabled, else false.
5667 *
5668 * @see #setSaveFromParentEnabled(boolean)
5669 */
5670 public boolean isSaveFromParentEnabled() {
5671 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5672 }
5673
5674 /**
5675 * Controls whether the entire hierarchy under this view will save its
5676 * state when a state saving traversal occurs from its parent. The default
5677 * is true; if false, these views will not be saved unless
5678 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5679 *
5680 * @param enabled Set to false to <em>disable</em> state saving, or true
5681 * (the default) to allow it.
5682 *
5683 * @see #isSaveFromParentEnabled()
5684 * @see #setId(int)
5685 * @see #onSaveInstanceState()
5686 */
5687 public void setSaveFromParentEnabled(boolean enabled) {
5688 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5689 }
5690
5691
5692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 * Returns whether this View is able to take focus.
5694 *
5695 * @return True if this view can take focus, or false otherwise.
5696 * @attr ref android.R.styleable#View_focusable
5697 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005698 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 public final boolean isFocusable() {
5700 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5701 }
5702
5703 /**
5704 * When a view is focusable, it may not want to take focus when in touch mode.
5705 * For example, a button would like focus when the user is navigating via a D-pad
5706 * so that the user can click on it, but once the user starts touching the screen,
5707 * the button shouldn't take focus
5708 * @return Whether the view is focusable in touch mode.
5709 * @attr ref android.R.styleable#View_focusableInTouchMode
5710 */
5711 @ViewDebug.ExportedProperty
5712 public final boolean isFocusableInTouchMode() {
5713 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5714 }
5715
5716 /**
5717 * Find the nearest view in the specified direction that can take focus.
5718 * This does not actually give focus to that view.
5719 *
5720 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5721 *
5722 * @return The nearest focusable in the specified direction, or null if none
5723 * can be found.
5724 */
5725 public View focusSearch(int direction) {
5726 if (mParent != null) {
5727 return mParent.focusSearch(this, direction);
5728 } else {
5729 return null;
5730 }
5731 }
5732
5733 /**
5734 * This method is the last chance for the focused view and its ancestors to
5735 * respond to an arrow key. This is called when the focused view did not
5736 * consume the key internally, nor could the view system find a new view in
5737 * the requested direction to give focus to.
5738 *
5739 * @param focused The currently focused view.
5740 * @param direction The direction focus wants to move. One of FOCUS_UP,
5741 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5742 * @return True if the this view consumed this unhandled move.
5743 */
5744 public boolean dispatchUnhandledMove(View focused, int direction) {
5745 return false;
5746 }
5747
5748 /**
5749 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005750 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005752 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5753 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 * @return The user specified next view, or null if there is none.
5755 */
5756 View findUserSetNextFocus(View root, int direction) {
5757 switch (direction) {
5758 case FOCUS_LEFT:
5759 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005760 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 case FOCUS_RIGHT:
5762 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005763 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 case FOCUS_UP:
5765 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005766 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 case FOCUS_DOWN:
5768 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005769 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005770 case FOCUS_FORWARD:
5771 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005772 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005773 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005774 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005775 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005776 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005777 @Override
5778 public boolean apply(View t) {
5779 return t.mNextFocusForwardId == id;
5780 }
5781 });
5782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 }
5784 return null;
5785 }
5786
Jeff Brown4dfbec22011-08-15 14:55:37 -07005787 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5788 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5789 @Override
5790 public boolean apply(View t) {
5791 return t.mID == childViewId;
5792 }
5793 });
5794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 if (result == null) {
5796 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5797 + "by user for id " + childViewId);
5798 }
5799 return result;
5800 }
5801
5802 /**
5803 * Find and return all focusable views that are descendants of this view,
5804 * possibly including this view if it is focusable itself.
5805 *
5806 * @param direction The direction of the focus
5807 * @return A list of focusable views
5808 */
5809 public ArrayList<View> getFocusables(int direction) {
5810 ArrayList<View> result = new ArrayList<View>(24);
5811 addFocusables(result, direction);
5812 return result;
5813 }
5814
5815 /**
5816 * Add any focusable views that are descendants of this view (possibly
5817 * including this view if it is focusable itself) to views. If we are in touch mode,
5818 * only add views that are also focusable in touch mode.
5819 *
5820 * @param views Focusable views found so far
5821 * @param direction The direction of the focus
5822 */
5823 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005824 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826
svetoslavganov75986cf2009-05-14 22:28:01 -07005827 /**
5828 * Adds any focusable views that are descendants of this view (possibly
5829 * including this view if it is focusable itself) to views. This method
5830 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005831 * only views focusable in touch mode if we are in touch mode or
5832 * only views that can take accessibility focus if accessibility is enabeld
5833 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005834 *
5835 * @param views Focusable views found so far or null if all we are interested is
5836 * the number of focusables.
5837 * @param direction The direction of the focus.
5838 * @param focusableMode The type of focusables to be added.
5839 *
5840 * @see #FOCUSABLES_ALL
5841 * @see #FOCUSABLES_TOUCH_MODE
5842 */
5843 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005844 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005845 return;
5846 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005847 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5848 if (AccessibilityManager.getInstance(mContext).isEnabled()
5849 && includeForAccessibility()) {
5850 views.add(this);
5851 return;
5852 }
5853 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005854 if (!isFocusable()) {
5855 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005856 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005857 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5858 && isInTouchMode() && !isFocusableInTouchMode()) {
5859 return;
5860 }
5861 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 }
5863
5864 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005865 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005866 * The search is performed by either the text that the View renders or the content
5867 * description that describes the view for accessibility purposes and the view does
5868 * not render or both. Clients can specify how the search is to be performed via
5869 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5870 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005871 *
5872 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005873 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005874 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005875 * @see #FIND_VIEWS_WITH_TEXT
5876 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5877 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005878 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005879 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005880 if (getAccessibilityNodeProvider() != null) {
5881 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5882 outViews.add(this);
5883 }
5884 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5885 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005886 String searchedLowerCase = searched.toString().toLowerCase();
5887 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5888 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5889 outViews.add(this);
5890 }
5891 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005892 }
5893
5894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 * Find and return all touchable views that are descendants of this view,
5896 * possibly including this view if it is touchable itself.
5897 *
5898 * @return A list of touchable views
5899 */
5900 public ArrayList<View> getTouchables() {
5901 ArrayList<View> result = new ArrayList<View>();
5902 addTouchables(result);
5903 return result;
5904 }
5905
5906 /**
5907 * Add any touchable views that are descendants of this view (possibly
5908 * including this view if it is touchable itself) to views.
5909 *
5910 * @param views Touchable views found so far
5911 */
5912 public void addTouchables(ArrayList<View> views) {
5913 final int viewFlags = mViewFlags;
5914
5915 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5916 && (viewFlags & ENABLED_MASK) == ENABLED) {
5917 views.add(this);
5918 }
5919 }
5920
5921 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005922 * Returns whether this View is accessibility focused.
5923 *
5924 * @return True if this View is accessibility focused.
5925 */
5926 boolean isAccessibilityFocused() {
5927 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5928 }
5929
5930 /**
5931 * Call this to try to give accessibility focus to this view.
5932 *
5933 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5934 * returns false or the view is no visible or the view already has accessibility
5935 * focus.
5936 *
5937 * See also {@link #focusSearch(int)}, which is what you call to say that you
5938 * have focus, and you want your parent to look for the next one.
5939 *
5940 * @return Whether this view actually took accessibility focus.
5941 *
5942 * @hide
5943 */
5944 public boolean requestAccessibilityFocus() {
5945 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5946 return false;
5947 }
5948 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5949 return false;
5950 }
5951 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5952 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5953 ViewRootImpl viewRootImpl = getViewRootImpl();
5954 if (viewRootImpl != null) {
5955 viewRootImpl.setAccessibilityFocusedHost(this);
5956 }
5957 invalidate();
5958 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5959 notifyAccessibilityStateChanged();
5960 // Try to give input focus to this view - not a descendant.
5961 requestFocusNoSearch(View.FOCUS_DOWN, null);
5962 return true;
5963 }
5964 return false;
5965 }
5966
5967 /**
5968 * Call this to try to clear accessibility focus of this view.
5969 *
5970 * See also {@link #focusSearch(int)}, which is what you call to say that you
5971 * have focus, and you want your parent to look for the next one.
5972 *
5973 * @hide
5974 */
5975 public void clearAccessibilityFocus() {
5976 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5977 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5978 ViewRootImpl viewRootImpl = getViewRootImpl();
5979 if (viewRootImpl != null) {
5980 viewRootImpl.setAccessibilityFocusedHost(null);
5981 }
5982 invalidate();
5983 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5984 notifyAccessibilityStateChanged();
5985 // Try to move accessibility focus to the input focus.
5986 View rootView = getRootView();
5987 if (rootView != null) {
5988 View inputFocus = rootView.findFocus();
5989 if (inputFocus != null) {
5990 inputFocus.requestAccessibilityFocus();
5991 }
5992 }
5993 }
5994 }
5995
5996 /**
5997 * Find the best view to take accessibility focus from a hover.
5998 * This function finds the deepest actionable view and if that
5999 * fails ask the parent to take accessibility focus from hover.
6000 *
6001 * @param x The X hovered location in this view coorditantes.
6002 * @param y The Y hovered location in this view coorditantes.
6003 * @return Whether the request was handled.
6004 *
6005 * @hide
6006 */
6007 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6008 if (onRequestAccessibilityFocusFromHover(x, y)) {
6009 return true;
6010 }
6011 ViewParent parent = mParent;
6012 if (parent instanceof View) {
6013 View parentView = (View) parent;
6014
6015 float[] position = mAttachInfo.mTmpTransformLocation;
6016 position[0] = x;
6017 position[1] = y;
6018
6019 // Compensate for the transformation of the current matrix.
6020 if (!hasIdentityMatrix()) {
6021 getMatrix().mapPoints(position);
6022 }
6023
6024 // Compensate for the parent scroll and the offset
6025 // of this view stop from the parent top.
6026 position[0] += mLeft - parentView.mScrollX;
6027 position[1] += mTop - parentView.mScrollY;
6028
6029 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6030 }
6031 return false;
6032 }
6033
6034 /**
6035 * Requests to give this View focus from hover.
6036 *
6037 * @param x The X hovered location in this view coorditantes.
6038 * @param y The Y hovered location in this view coorditantes.
6039 * @return Whether the request was handled.
6040 *
6041 * @hide
6042 */
6043 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6044 if (includeForAccessibility()
6045 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6046 return requestAccessibilityFocus();
6047 }
6048 return false;
6049 }
6050
6051 /**
6052 * Clears accessibility focus without calling any callback methods
6053 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6054 * is used for clearing accessibility focus when giving this focus to
6055 * another view.
6056 */
6057 void clearAccessibilityFocusNoCallbacks() {
6058 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6059 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6060 invalidate();
6061 }
6062 }
6063
6064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 * Call this to try to give focus to a specific view or to one of its
6066 * descendants.
6067 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006068 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6069 * false), or if it is focusable and it is not focusable in touch mode
6070 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006072 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 * have focus, and you want your parent to look for the next one.
6074 *
6075 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6076 * {@link #FOCUS_DOWN} and <code>null</code>.
6077 *
6078 * @return Whether this view or one of its descendants actually took focus.
6079 */
6080 public final boolean requestFocus() {
6081 return requestFocus(View.FOCUS_DOWN);
6082 }
6083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 /**
6085 * Call this to try to give focus to a specific view or to one of its
6086 * descendants and give it a hint about what direction focus is heading.
6087 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006088 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6089 * false), or if it is focusable and it is not focusable in touch mode
6090 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006092 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 * have focus, and you want your parent to look for the next one.
6094 *
6095 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6096 * <code>null</code> set for the previously focused rectangle.
6097 *
6098 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6099 * @return Whether this view or one of its descendants actually took focus.
6100 */
6101 public final boolean requestFocus(int direction) {
6102 return requestFocus(direction, null);
6103 }
6104
6105 /**
6106 * Call this to try to give focus to a specific view or to one of its descendants
6107 * and give it hints about the direction and a specific rectangle that the focus
6108 * is coming from. The rectangle can help give larger views a finer grained hint
6109 * about where focus is coming from, and therefore, where to show selection, or
6110 * forward focus change internally.
6111 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006112 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6113 * false), or if it is focusable and it is not focusable in touch mode
6114 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 *
6116 * A View will not take focus if it is not visible.
6117 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006118 * A View will not take focus if one of its parents has
6119 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6120 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006122 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006123 * have focus, and you want your parent to look for the next one.
6124 *
6125 * You may wish to override this method if your custom {@link View} has an internal
6126 * {@link View} that it wishes to forward the request to.
6127 *
6128 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6129 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6130 * to give a finer grained hint about where focus is coming from. May be null
6131 * if there is no hint.
6132 * @return Whether this view or one of its descendants actually took focus.
6133 */
6134 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006135 return requestFocusNoSearch(direction, previouslyFocusedRect);
6136 }
6137
6138 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 // need to be focusable
6140 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6141 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6142 return false;
6143 }
6144
6145 // need to be focusable in touch mode if in touch mode
6146 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006147 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6148 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 }
6150
6151 // need to not have any parents blocking us
6152 if (hasAncestorThatBlocksDescendantFocus()) {
6153 return false;
6154 }
6155
6156 handleFocusGainInternal(direction, previouslyFocusedRect);
6157 return true;
6158 }
6159
6160 /**
6161 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6162 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6163 * touch mode to request focus when they are touched.
6164 *
6165 * @return Whether this view or one of its descendants actually took focus.
6166 *
6167 * @see #isInTouchMode()
6168 *
6169 */
6170 public final boolean requestFocusFromTouch() {
6171 // Leave touch mode if we need to
6172 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006173 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006174 if (viewRoot != null) {
6175 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 }
6177 }
6178 return requestFocus(View.FOCUS_DOWN);
6179 }
6180
6181 /**
6182 * @return Whether any ancestor of this view blocks descendant focus.
6183 */
6184 private boolean hasAncestorThatBlocksDescendantFocus() {
6185 ViewParent ancestor = mParent;
6186 while (ancestor instanceof ViewGroup) {
6187 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6188 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6189 return true;
6190 } else {
6191 ancestor = vgAncestor.getParent();
6192 }
6193 }
6194 return false;
6195 }
6196
6197 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006198 * Gets the mode for determining whether this View is important for accessibility
6199 * which is if it fires accessibility events and if it is reported to
6200 * accessibility services that query the screen.
6201 *
6202 * @return The mode for determining whether a View is important for accessibility.
6203 *
6204 * @attr ref android.R.styleable#View_importantForAccessibility
6205 *
6206 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6207 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6208 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6209 */
6210 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6211 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6212 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6213 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6214 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6215 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6216 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6217 })
6218 public int getImportantForAccessibility() {
6219 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6220 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6221 }
6222
6223 /**
6224 * Sets how to determine whether this view is important for accessibility
6225 * which is if it fires accessibility events and if it is reported to
6226 * accessibility services that query the screen.
6227 *
6228 * @param mode How to determine whether this view is important for accessibility.
6229 *
6230 * @attr ref android.R.styleable#View_importantForAccessibility
6231 *
6232 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6233 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6234 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6235 */
6236 public void setImportantForAccessibility(int mode) {
6237 if (mode != getImportantForAccessibility()) {
6238 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6239 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6240 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6241 notifyAccessibilityStateChanged();
6242 }
6243 }
6244
6245 /**
6246 * Gets whether this view should be exposed for accessibility.
6247 *
6248 * @return Whether the view is exposed for accessibility.
6249 *
6250 * @hide
6251 */
6252 public boolean isImportantForAccessibility() {
6253 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6254 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6255 switch (mode) {
6256 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6257 return true;
6258 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6259 return false;
6260 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6261 return isActionableForAccessibility() || hasListenersForAccessibility();
6262 default:
6263 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6264 + mode);
6265 }
6266 }
6267
6268 /**
6269 * Gets the parent for accessibility purposes. Note that the parent for
6270 * accessibility is not necessary the immediate parent. It is the first
6271 * predecessor that is important for accessibility.
6272 *
6273 * @return The parent for accessibility purposes.
6274 */
6275 public ViewParent getParentForAccessibility() {
6276 if (mParent instanceof View) {
6277 View parentView = (View) mParent;
6278 if (parentView.includeForAccessibility()) {
6279 return mParent;
6280 } else {
6281 return mParent.getParentForAccessibility();
6282 }
6283 }
6284 return null;
6285 }
6286
6287 /**
6288 * Adds the children of a given View for accessibility. Since some Views are
6289 * not important for accessibility the children for accessibility are not
6290 * necessarily direct children of the riew, rather they are the first level of
6291 * descendants important for accessibility.
6292 *
6293 * @param children The list of children for accessibility.
6294 */
6295 public void addChildrenForAccessibility(ArrayList<View> children) {
6296 if (includeForAccessibility()) {
6297 children.add(this);
6298 }
6299 }
6300
6301 /**
6302 * Whether to regard this view for accessibility. A view is regarded for
6303 * accessibility if it is important for accessibility or the querying
6304 * accessibility service has explicitly requested that view not
6305 * important for accessibility are regarded.
6306 *
6307 * @return Whether to regard the view for accessibility.
6308 */
6309 boolean includeForAccessibility() {
6310 if (mAttachInfo != null) {
6311 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006312 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006313 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006314 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006315 }
6316 }
6317 return false;
6318 }
6319
6320 /**
6321 * Returns whether the View is considered actionable from
6322 * accessibility perspective. Such view are important for
6323 * accessiiblity.
6324 *
6325 * @return True if the view is actionable for accessibility.
6326 */
6327 private boolean isActionableForAccessibility() {
6328 return (isClickable() || isLongClickable() || isFocusable());
6329 }
6330
6331 /**
6332 * Returns whether the View has registered callbacks wich makes it
6333 * important for accessiiblity.
6334 *
6335 * @return True if the view is actionable for accessibility.
6336 */
6337 private boolean hasListenersForAccessibility() {
6338 ListenerInfo info = getListenerInfo();
6339 return mTouchDelegate != null || info.mOnKeyListener != null
6340 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6341 || info.mOnHoverListener != null || info.mOnDragListener != null;
6342 }
6343
6344 /**
6345 * Notifies accessibility services that some view's important for
6346 * accessibility state has changed. Note that such notifications
6347 * are made at most once every
6348 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6349 * to avoid unnecessary load to the system. Also once a view has
6350 * made a notifucation this method is a NOP until the notification has
6351 * been sent to clients.
6352 *
6353 * @hide
6354 *
6355 * TODO: Makse sure this method is called for any view state change
6356 * that is interesting for accessilility purposes.
6357 */
6358 public void notifyAccessibilityStateChanged() {
6359 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6360 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6361 if (mParent != null) {
6362 mParent.childAccessibilityStateChanged(this);
6363 }
6364 }
6365 }
6366
6367 /**
6368 * Reset the state indicating the this view has requested clients
6369 * interested in its accessiblity state to be notified.
6370 *
6371 * @hide
6372 */
6373 public void resetAccessibilityStateChanged() {
6374 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6375 }
6376
6377 /**
6378 * Performs the specified accessibility action on the view. For
6379 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6380 *
6381 * @param action The action to perform.
6382 * @return Whether the action was performed.
6383 */
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07006384 public boolean performAccessibilityAction(int action, Bundle args) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006385 switch (action) {
6386 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006387 if (isClickable()) {
6388 performClick();
6389 }
6390 } break;
6391 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6392 if (isLongClickable()) {
6393 performLongClick();
6394 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006395 } break;
6396 case AccessibilityNodeInfo.ACTION_FOCUS: {
6397 if (!hasFocus()) {
6398 // Get out of touch mode since accessibility
6399 // wants to move focus around.
6400 getViewRootImpl().ensureTouchMode(false);
6401 return requestFocus();
6402 }
6403 } break;
6404 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6405 if (hasFocus()) {
6406 clearFocus();
6407 return !isFocused();
6408 }
6409 } break;
6410 case AccessibilityNodeInfo.ACTION_SELECT: {
6411 if (!isSelected()) {
6412 setSelected(true);
6413 return isSelected();
6414 }
6415 } break;
6416 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6417 if (isSelected()) {
6418 setSelected(false);
6419 return !isSelected();
6420 }
6421 } break;
6422 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6423 if (!isAccessibilityFocused()) {
6424 return requestAccessibilityFocus();
6425 }
6426 } break;
6427 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6428 if (isAccessibilityFocused()) {
6429 clearAccessibilityFocus();
6430 return true;
6431 }
6432 } break;
6433 }
6434 return false;
6435 }
6436
6437 /**
Romain Guya440b002010-02-24 15:57:54 -08006438 * @hide
6439 */
6440 public void dispatchStartTemporaryDetach() {
6441 onStartTemporaryDetach();
6442 }
6443
6444 /**
6445 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6447 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006448 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006449 */
6450 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006451 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006452 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006453 }
6454
6455 /**
6456 * @hide
6457 */
6458 public void dispatchFinishTemporaryDetach() {
6459 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460 }
Romain Guy8506ab42009-06-11 17:35:47 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 /**
6463 * Called after {@link #onStartTemporaryDetach} when the container is done
6464 * changing the view.
6465 */
6466 public void onFinishTemporaryDetach() {
6467 }
Romain Guy8506ab42009-06-11 17:35:47 -07006468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006470 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6471 * for this view's window. Returns null if the view is not currently attached
6472 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006473 * just use the standard high-level event callbacks like
6474 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006475 */
6476 public KeyEvent.DispatcherState getKeyDispatcherState() {
6477 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6478 }
Joe Malin32736f02011-01-19 16:14:20 -08006479
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481 * Dispatch a key event before it is processed by any input method
6482 * associated with the view hierarchy. This can be used to intercept
6483 * key events in special situations before the IME consumes them; a
6484 * typical example would be handling the BACK key to update the application's
6485 * UI instead of allowing the IME to see it and close itself.
6486 *
6487 * @param event The key event to be dispatched.
6488 * @return True if the event was handled, false otherwise.
6489 */
6490 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6491 return onKeyPreIme(event.getKeyCode(), event);
6492 }
6493
6494 /**
6495 * Dispatch a key event to the next view on the focus path. This path runs
6496 * from the top of the view tree down to the currently focused view. If this
6497 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6498 * the next node down the focus path. This method also fires any key
6499 * listeners.
6500 *
6501 * @param event The key event to be dispatched.
6502 * @return True if the event was handled, false otherwise.
6503 */
6504 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006505 if (mInputEventConsistencyVerifier != null) {
6506 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508
Jeff Brown21bc5c92011-02-28 18:27:14 -08006509 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006510 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006511 ListenerInfo li = mListenerInfo;
6512 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6513 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 return true;
6515 }
6516
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006517 if (event.dispatch(this, mAttachInfo != null
6518 ? mAttachInfo.mKeyDispatchState : null, this)) {
6519 return true;
6520 }
6521
6522 if (mInputEventConsistencyVerifier != null) {
6523 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6524 }
6525 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 }
6527
6528 /**
6529 * Dispatches a key shortcut event.
6530 *
6531 * @param event The key event to be dispatched.
6532 * @return True if the event was handled by the view, false otherwise.
6533 */
6534 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6535 return onKeyShortcut(event.getKeyCode(), event);
6536 }
6537
6538 /**
6539 * Pass the touch screen motion event down to the target view, or this
6540 * view if it is the target.
6541 *
6542 * @param event The motion event to be dispatched.
6543 * @return True if the event was handled by the view, false otherwise.
6544 */
6545 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006546 if (mInputEventConsistencyVerifier != null) {
6547 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6548 }
6549
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006550 if (onFilterTouchEventForSecurity(event)) {
6551 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006552 ListenerInfo li = mListenerInfo;
6553 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6554 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006555 return true;
6556 }
6557
6558 if (onTouchEvent(event)) {
6559 return true;
6560 }
Jeff Brown85a31762010-09-01 17:01:00 -07006561 }
6562
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006563 if (mInputEventConsistencyVerifier != null) {
6564 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006566 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006567 }
6568
6569 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006570 * Filter the touch event to apply security policies.
6571 *
6572 * @param event The motion event to be filtered.
6573 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006574 *
Jeff Brown85a31762010-09-01 17:01:00 -07006575 * @see #getFilterTouchesWhenObscured
6576 */
6577 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006578 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006579 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6580 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6581 // Window is obscured, drop this touch.
6582 return false;
6583 }
6584 return true;
6585 }
6586
6587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 * Pass a trackball motion event down to the focused view.
6589 *
6590 * @param event The motion event to be dispatched.
6591 * @return True if the event was handled by the view, false otherwise.
6592 */
6593 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006594 if (mInputEventConsistencyVerifier != null) {
6595 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6596 }
6597
Romain Guy02ccac62011-06-24 13:20:23 -07006598 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 }
6600
6601 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006602 * Dispatch a generic motion event.
6603 * <p>
6604 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6605 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006606 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006607 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006608 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006609 *
6610 * @param event The motion event to be dispatched.
6611 * @return True if the event was handled by the view, false otherwise.
6612 */
6613 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006614 if (mInputEventConsistencyVerifier != null) {
6615 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6616 }
6617
Jeff Browna032cc02011-03-07 16:56:21 -08006618 final int source = event.getSource();
6619 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6620 final int action = event.getAction();
6621 if (action == MotionEvent.ACTION_HOVER_ENTER
6622 || action == MotionEvent.ACTION_HOVER_MOVE
6623 || action == MotionEvent.ACTION_HOVER_EXIT) {
6624 if (dispatchHoverEvent(event)) {
6625 return true;
6626 }
6627 } else if (dispatchGenericPointerEvent(event)) {
6628 return true;
6629 }
6630 } else if (dispatchGenericFocusedEvent(event)) {
6631 return true;
6632 }
6633
Jeff Brown10b62902011-06-20 16:40:37 -07006634 if (dispatchGenericMotionEventInternal(event)) {
6635 return true;
6636 }
6637
6638 if (mInputEventConsistencyVerifier != null) {
6639 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6640 }
6641 return false;
6642 }
6643
6644 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006645 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006646 ListenerInfo li = mListenerInfo;
6647 if (li != null && li.mOnGenericMotionListener != null
6648 && (mViewFlags & ENABLED_MASK) == ENABLED
6649 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006650 return true;
6651 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006652
6653 if (onGenericMotionEvent(event)) {
6654 return true;
6655 }
6656
6657 if (mInputEventConsistencyVerifier != null) {
6658 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6659 }
6660 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006661 }
6662
6663 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006664 * Dispatch a hover event.
6665 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006666 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006667 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006668 * </p>
6669 *
6670 * @param event The motion event to be dispatched.
6671 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006672 */
6673 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006674 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006675 ListenerInfo li = mListenerInfo;
6676 if (li != null && li.mOnHoverListener != null
6677 && (mViewFlags & ENABLED_MASK) == ENABLED
6678 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006679 return true;
6680 }
6681
Jeff Browna032cc02011-03-07 16:56:21 -08006682 return onHoverEvent(event);
6683 }
6684
6685 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006686 * Returns true if the view has a child to which it has recently sent
6687 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6688 * it does not have a hovered child, then it must be the innermost hovered view.
6689 * @hide
6690 */
6691 protected boolean hasHoveredChild() {
6692 return false;
6693 }
6694
6695 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006696 * Dispatch a generic motion event to the view under the first pointer.
6697 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006698 * Do not call this method directly.
6699 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006700 * </p>
6701 *
6702 * @param event The motion event to be dispatched.
6703 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006704 */
6705 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6706 return false;
6707 }
6708
6709 /**
6710 * Dispatch a generic motion event to the currently focused view.
6711 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006712 * Do not call this method directly.
6713 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006714 * </p>
6715 *
6716 * @param event The motion event to be dispatched.
6717 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006718 */
6719 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6720 return false;
6721 }
6722
6723 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006724 * Dispatch a pointer event.
6725 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006726 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6727 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6728 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006729 * and should not be expected to handle other pointing device features.
6730 * </p>
6731 *
6732 * @param event The motion event to be dispatched.
6733 * @return True if the event was handled by the view, false otherwise.
6734 * @hide
6735 */
6736 public final boolean dispatchPointerEvent(MotionEvent event) {
6737 if (event.isTouchEvent()) {
6738 return dispatchTouchEvent(event);
6739 } else {
6740 return dispatchGenericMotionEvent(event);
6741 }
6742 }
6743
6744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 * Called when the window containing this view gains or loses window focus.
6746 * ViewGroups should override to route to their children.
6747 *
6748 * @param hasFocus True if the window containing this view now has focus,
6749 * false otherwise.
6750 */
6751 public void dispatchWindowFocusChanged(boolean hasFocus) {
6752 onWindowFocusChanged(hasFocus);
6753 }
6754
6755 /**
6756 * Called when the window containing this view gains or loses focus. Note
6757 * that this is separate from view focus: to receive key events, both
6758 * your view and its window must have focus. If a window is displayed
6759 * on top of yours that takes input focus, then your own window will lose
6760 * focus but the view focus will remain unchanged.
6761 *
6762 * @param hasWindowFocus True if the window containing this view now has
6763 * focus, false otherwise.
6764 */
6765 public void onWindowFocusChanged(boolean hasWindowFocus) {
6766 InputMethodManager imm = InputMethodManager.peekInstance();
6767 if (!hasWindowFocus) {
6768 if (isPressed()) {
6769 setPressed(false);
6770 }
6771 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6772 imm.focusOut(this);
6773 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006774 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006775 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006776 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6778 imm.focusIn(this);
6779 }
6780 refreshDrawableState();
6781 }
6782
6783 /**
6784 * Returns true if this view is in a window that currently has window focus.
6785 * Note that this is not the same as the view itself having focus.
6786 *
6787 * @return True if this view is in a window that currently has window focus.
6788 */
6789 public boolean hasWindowFocus() {
6790 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6791 }
6792
6793 /**
Adam Powell326d8082009-12-09 15:10:07 -08006794 * Dispatch a view visibility change down the view hierarchy.
6795 * ViewGroups should override to route to their children.
6796 * @param changedView The view whose visibility changed. Could be 'this' or
6797 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006798 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6799 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006800 */
6801 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6802 onVisibilityChanged(changedView, visibility);
6803 }
6804
6805 /**
6806 * Called when the visibility of the view or an ancestor of the view is changed.
6807 * @param changedView The view whose visibility changed. Could be 'this' or
6808 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006809 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6810 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006811 */
6812 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006813 if (visibility == VISIBLE) {
6814 if (mAttachInfo != null) {
6815 initialAwakenScrollBars();
6816 } else {
6817 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6818 }
6819 }
Adam Powell326d8082009-12-09 15:10:07 -08006820 }
6821
6822 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006823 * Dispatch a hint about whether this view is displayed. For instance, when
6824 * a View moves out of the screen, it might receives a display hint indicating
6825 * the view is not displayed. Applications should not <em>rely</em> on this hint
6826 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006827 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006828 * @param hint A hint about whether or not this view is displayed:
6829 * {@link #VISIBLE} or {@link #INVISIBLE}.
6830 */
6831 public void dispatchDisplayHint(int hint) {
6832 onDisplayHint(hint);
6833 }
6834
6835 /**
6836 * Gives this view a hint about whether is displayed or not. For instance, when
6837 * a View moves out of the screen, it might receives a display hint indicating
6838 * the view is not displayed. Applications should not <em>rely</em> on this hint
6839 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006840 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006841 * @param hint A hint about whether or not this view is displayed:
6842 * {@link #VISIBLE} or {@link #INVISIBLE}.
6843 */
6844 protected void onDisplayHint(int hint) {
6845 }
6846
6847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 * Dispatch a window visibility change down the view hierarchy.
6849 * ViewGroups should override to route to their children.
6850 *
6851 * @param visibility The new visibility of the window.
6852 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006853 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006854 */
6855 public void dispatchWindowVisibilityChanged(int visibility) {
6856 onWindowVisibilityChanged(visibility);
6857 }
6858
6859 /**
6860 * Called when the window containing has change its visibility
6861 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6862 * that this tells you whether or not your window is being made visible
6863 * to the window manager; this does <em>not</em> tell you whether or not
6864 * your window is obscured by other windows on the screen, even if it
6865 * is itself visible.
6866 *
6867 * @param visibility The new visibility of the window.
6868 */
6869 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006870 if (visibility == VISIBLE) {
6871 initialAwakenScrollBars();
6872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 }
6874
6875 /**
6876 * Returns the current visibility of the window this view is attached to
6877 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6878 *
6879 * @return Returns the current visibility of the view's window.
6880 */
6881 public int getWindowVisibility() {
6882 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6883 }
6884
6885 /**
6886 * Retrieve the overall visible display size in which the window this view is
6887 * attached to has been positioned in. This takes into account screen
6888 * decorations above the window, for both cases where the window itself
6889 * is being position inside of them or the window is being placed under
6890 * then and covered insets are used for the window to position its content
6891 * inside. In effect, this tells you the available area where content can
6892 * be placed and remain visible to users.
6893 *
6894 * <p>This function requires an IPC back to the window manager to retrieve
6895 * the requested information, so should not be used in performance critical
6896 * code like drawing.
6897 *
6898 * @param outRect Filled in with the visible display frame. If the view
6899 * is not attached to a window, this is simply the raw display size.
6900 */
6901 public void getWindowVisibleDisplayFrame(Rect outRect) {
6902 if (mAttachInfo != null) {
6903 try {
6904 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6905 } catch (RemoteException e) {
6906 return;
6907 }
6908 // XXX This is really broken, and probably all needs to be done
6909 // in the window manager, and we need to know more about whether
6910 // we want the area behind or in front of the IME.
6911 final Rect insets = mAttachInfo.mVisibleInsets;
6912 outRect.left += insets.left;
6913 outRect.top += insets.top;
6914 outRect.right -= insets.right;
6915 outRect.bottom -= insets.bottom;
6916 return;
6917 }
6918 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006919 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 }
6921
6922 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006923 * Dispatch a notification about a resource configuration change down
6924 * the view hierarchy.
6925 * ViewGroups should override to route to their children.
6926 *
6927 * @param newConfig The new resource configuration.
6928 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006929 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006930 */
6931 public void dispatchConfigurationChanged(Configuration newConfig) {
6932 onConfigurationChanged(newConfig);
6933 }
6934
6935 /**
6936 * Called when the current configuration of the resources being used
6937 * by the application have changed. You can use this to decide when
6938 * to reload resources that can changed based on orientation and other
6939 * configuration characterstics. You only need to use this if you are
6940 * not relying on the normal {@link android.app.Activity} mechanism of
6941 * recreating the activity instance upon a configuration change.
6942 *
6943 * @param newConfig The new resource configuration.
6944 */
6945 protected void onConfigurationChanged(Configuration newConfig) {
6946 }
6947
6948 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 * Private function to aggregate all per-view attributes in to the view
6950 * root.
6951 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006952 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6953 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 }
6955
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006956 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6957 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006958 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006959 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006960 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006961 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006962 ListenerInfo li = mListenerInfo;
6963 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006964 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 }
6967 }
6968
6969 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006970 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006971 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006972 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6973 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006974 ai.mRecomputeGlobalAttributes = true;
6975 }
6976 }
6977 }
6978
6979 /**
6980 * Returns whether the device is currently in touch mode. Touch mode is entered
6981 * once the user begins interacting with the device by touch, and affects various
6982 * things like whether focus is always visible to the user.
6983 *
6984 * @return Whether the device is in touch mode.
6985 */
6986 @ViewDebug.ExportedProperty
6987 public boolean isInTouchMode() {
6988 if (mAttachInfo != null) {
6989 return mAttachInfo.mInTouchMode;
6990 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006991 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 }
6993 }
6994
6995 /**
6996 * Returns the context the view is running in, through which it can
6997 * access the current theme, resources, etc.
6998 *
6999 * @return The view's Context.
7000 */
7001 @ViewDebug.CapturedViewProperty
7002 public final Context getContext() {
7003 return mContext;
7004 }
7005
7006 /**
7007 * Handle a key event before it is processed by any input method
7008 * associated with the view hierarchy. This can be used to intercept
7009 * key events in special situations before the IME consumes them; a
7010 * typical example would be handling the BACK key to update the application's
7011 * UI instead of allowing the IME to see it and close itself.
7012 *
7013 * @param keyCode The value in event.getKeyCode().
7014 * @param event Description of the key event.
7015 * @return If you handled the event, return true. If you want to allow the
7016 * event to be handled by the next receiver, return false.
7017 */
7018 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7019 return false;
7020 }
7021
7022 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007023 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7024 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7026 * is released, if the view is enabled and clickable.
7027 *
7028 * @param keyCode A key code that represents the button pressed, from
7029 * {@link android.view.KeyEvent}.
7030 * @param event The KeyEvent object that defines the button action.
7031 */
7032 public boolean onKeyDown(int keyCode, KeyEvent event) {
7033 boolean result = false;
7034
7035 switch (keyCode) {
7036 case KeyEvent.KEYCODE_DPAD_CENTER:
7037 case KeyEvent.KEYCODE_ENTER: {
7038 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7039 return true;
7040 }
7041 // Long clickable items don't necessarily have to be clickable
7042 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7043 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7044 (event.getRepeatCount() == 0)) {
7045 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007046 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 return true;
7048 }
7049 break;
7050 }
7051 }
7052 return result;
7053 }
7054
7055 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007056 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7057 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7058 * the event).
7059 */
7060 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7061 return false;
7062 }
7063
7064 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007065 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7066 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7068 * {@link KeyEvent#KEYCODE_ENTER} is released.
7069 *
7070 * @param keyCode A key code that represents the button pressed, from
7071 * {@link android.view.KeyEvent}.
7072 * @param event The KeyEvent object that defines the button action.
7073 */
7074 public boolean onKeyUp(int keyCode, KeyEvent event) {
7075 boolean result = false;
7076
7077 switch (keyCode) {
7078 case KeyEvent.KEYCODE_DPAD_CENTER:
7079 case KeyEvent.KEYCODE_ENTER: {
7080 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7081 return true;
7082 }
7083 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7084 setPressed(false);
7085
7086 if (!mHasPerformedLongPress) {
7087 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007088 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089
7090 result = performClick();
7091 }
7092 }
7093 break;
7094 }
7095 }
7096 return result;
7097 }
7098
7099 /**
7100 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7101 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7102 * the event).
7103 *
7104 * @param keyCode A key code that represents the button pressed, from
7105 * {@link android.view.KeyEvent}.
7106 * @param repeatCount The number of times the action was made.
7107 * @param event The KeyEvent object that defines the button action.
7108 */
7109 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7110 return false;
7111 }
7112
7113 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007114 * Called on the focused view when a key shortcut event is not handled.
7115 * Override this method to implement local key shortcuts for the View.
7116 * Key shortcuts can also be implemented by setting the
7117 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 *
7119 * @param keyCode The value in event.getKeyCode().
7120 * @param event Description of the key event.
7121 * @return If you handled the event, return true. If you want to allow the
7122 * event to be handled by the next receiver, return false.
7123 */
7124 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7125 return false;
7126 }
7127
7128 /**
7129 * Check whether the called view is a text editor, in which case it
7130 * would make sense to automatically display a soft input window for
7131 * it. Subclasses should override this if they implement
7132 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007133 * a call on that method would return a non-null InputConnection, and
7134 * they are really a first-class editor that the user would normally
7135 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007136 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007137 * <p>The default implementation always returns false. This does
7138 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7139 * will not be called or the user can not otherwise perform edits on your
7140 * view; it is just a hint to the system that this is not the primary
7141 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 * @return Returns true if this view is a text editor, else false.
7144 */
7145 public boolean onCheckIsTextEditor() {
7146 return false;
7147 }
Romain Guy8506ab42009-06-11 17:35:47 -07007148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 /**
7150 * Create a new InputConnection for an InputMethod to interact
7151 * with the view. The default implementation returns null, since it doesn't
7152 * support input methods. You can override this to implement such support.
7153 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 * <p>When implementing this, you probably also want to implement
7156 * {@link #onCheckIsTextEditor()} to indicate you will return a
7157 * non-null InputConnection.
7158 *
7159 * @param outAttrs Fill in with attribute information about the connection.
7160 */
7161 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7162 return null;
7163 }
7164
7165 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007166 * Called by the {@link android.view.inputmethod.InputMethodManager}
7167 * when a view who is not the current
7168 * input connection target is trying to make a call on the manager. The
7169 * default implementation returns false; you can override this to return
7170 * true for certain views if you are performing InputConnection proxying
7171 * to them.
7172 * @param view The View that is making the InputMethodManager call.
7173 * @return Return true to allow the call, false to reject.
7174 */
7175 public boolean checkInputConnectionProxy(View view) {
7176 return false;
7177 }
Romain Guy8506ab42009-06-11 17:35:47 -07007178
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007179 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 * Show the context menu for this view. It is not safe to hold on to the
7181 * menu after returning from this method.
7182 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007183 * You should normally not overload this method. Overload
7184 * {@link #onCreateContextMenu(ContextMenu)} or define an
7185 * {@link OnCreateContextMenuListener} to add items to the context menu.
7186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187 * @param menu The context menu to populate
7188 */
7189 public void createContextMenu(ContextMenu menu) {
7190 ContextMenuInfo menuInfo = getContextMenuInfo();
7191
7192 // Sets the current menu info so all items added to menu will have
7193 // my extra info set.
7194 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7195
7196 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007197 ListenerInfo li = mListenerInfo;
7198 if (li != null && li.mOnCreateContextMenuListener != null) {
7199 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 }
7201
7202 // Clear the extra information so subsequent items that aren't mine don't
7203 // have my extra info.
7204 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7205
7206 if (mParent != null) {
7207 mParent.createContextMenu(menu);
7208 }
7209 }
7210
7211 /**
7212 * Views should implement this if they have extra information to associate
7213 * with the context menu. The return result is supplied as a parameter to
7214 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7215 * callback.
7216 *
7217 * @return Extra information about the item for which the context menu
7218 * should be shown. This information will vary across different
7219 * subclasses of View.
7220 */
7221 protected ContextMenuInfo getContextMenuInfo() {
7222 return null;
7223 }
7224
7225 /**
7226 * Views should implement this if the view itself is going to add items to
7227 * the context menu.
7228 *
7229 * @param menu the context menu to populate
7230 */
7231 protected void onCreateContextMenu(ContextMenu menu) {
7232 }
7233
7234 /**
7235 * Implement this method to handle trackball motion events. The
7236 * <em>relative</em> movement of the trackball since the last event
7237 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7238 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7239 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7240 * they will often be fractional values, representing the more fine-grained
7241 * movement information available from a trackball).
7242 *
7243 * @param event The motion event.
7244 * @return True if the event was handled, false otherwise.
7245 */
7246 public boolean onTrackballEvent(MotionEvent event) {
7247 return false;
7248 }
7249
7250 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007251 * Implement this method to handle generic motion events.
7252 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007253 * Generic motion events describe joystick movements, mouse hovers, track pad
7254 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007255 * {@link MotionEvent#getSource() source} of the motion event specifies
7256 * the class of input that was received. Implementations of this method
7257 * must examine the bits in the source before processing the event.
7258 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007259 * </p><p>
7260 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7261 * are delivered to the view under the pointer. All other generic motion events are
7262 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007263 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007264 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007265 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007266 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7267 * // process the joystick movement...
7268 * return true;
7269 * }
7270 * }
7271 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7272 * switch (event.getAction()) {
7273 * case MotionEvent.ACTION_HOVER_MOVE:
7274 * // process the mouse hover movement...
7275 * return true;
7276 * case MotionEvent.ACTION_SCROLL:
7277 * // process the scroll wheel movement...
7278 * return true;
7279 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007280 * }
7281 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007282 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007283 *
7284 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007285 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007286 */
7287 public boolean onGenericMotionEvent(MotionEvent event) {
7288 return false;
7289 }
7290
7291 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007292 * Implement this method to handle hover events.
7293 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007294 * This method is called whenever a pointer is hovering into, over, or out of the
7295 * bounds of a view and the view is not currently being touched.
7296 * Hover events are represented as pointer events with action
7297 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7298 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7299 * </p>
7300 * <ul>
7301 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7302 * when the pointer enters the bounds of the view.</li>
7303 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7304 * when the pointer has already entered the bounds of the view and has moved.</li>
7305 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7306 * when the pointer has exited the bounds of the view or when the pointer is
7307 * about to go down due to a button click, tap, or similar user action that
7308 * causes the view to be touched.</li>
7309 * </ul>
7310 * <p>
7311 * The view should implement this method to return true to indicate that it is
7312 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007313 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007314 * The default implementation calls {@link #setHovered} to update the hovered state
7315 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007316 * is enabled and is clickable. The default implementation also sends hover
7317 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007318 * </p>
7319 *
7320 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007321 * @return True if the view handled the hover event.
7322 *
7323 * @see #isHovered
7324 * @see #setHovered
7325 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007326 */
7327 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007328 // The root view may receive hover (or touch) events that are outside the bounds of
7329 // the window. This code ensures that we only send accessibility events for
7330 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007331 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007332 if (!mSendingHoverAccessibilityEvents) {
7333 if ((action == MotionEvent.ACTION_HOVER_ENTER
7334 || action == MotionEvent.ACTION_HOVER_MOVE)
7335 && !hasHoveredChild()
7336 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007337 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007338 mSendingHoverAccessibilityEvents = true;
7339 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007340 }
7341 } else {
7342 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007343 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007344 && !pointInView(event.getX(), event.getY()))) {
7345 mSendingHoverAccessibilityEvents = false;
7346 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007347 // If the window does not have input focus we take away accessibility
7348 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007349 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007350 getViewRootImpl().setAccessibilityFocusedHost(null);
7351 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007352 }
Jeff Browna1b24182011-07-28 13:38:24 -07007353 }
7354
Jeff Brown87b7f802011-06-21 18:35:45 -07007355 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007356 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007357 case MotionEvent.ACTION_HOVER_ENTER:
7358 setHovered(true);
7359 break;
7360 case MotionEvent.ACTION_HOVER_EXIT:
7361 setHovered(false);
7362 break;
7363 }
Jeff Browna1b24182011-07-28 13:38:24 -07007364
7365 // Dispatch the event to onGenericMotionEvent before returning true.
7366 // This is to provide compatibility with existing applications that
7367 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7368 // break because of the new default handling for hoverable views
7369 // in onHoverEvent.
7370 // Note that onGenericMotionEvent will be called by default when
7371 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7372 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007373 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007374 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007375
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007376 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007377 }
7378
7379 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007380 * Returns true if the view should handle {@link #onHoverEvent}
7381 * by calling {@link #setHovered} to change its hovered state.
7382 *
7383 * @return True if the view is hoverable.
7384 */
7385 private boolean isHoverable() {
7386 final int viewFlags = mViewFlags;
7387 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7388 return false;
7389 }
7390
7391 return (viewFlags & CLICKABLE) == CLICKABLE
7392 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7393 }
7394
7395 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007396 * Returns true if the view is currently hovered.
7397 *
7398 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007399 *
7400 * @see #setHovered
7401 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007402 */
Jeff Brown10b62902011-06-20 16:40:37 -07007403 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007404 public boolean isHovered() {
7405 return (mPrivateFlags & HOVERED) != 0;
7406 }
7407
7408 /**
7409 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007410 * <p>
7411 * Calling this method also changes the drawable state of the view. This
7412 * enables the view to react to hover by using different drawable resources
7413 * to change its appearance.
7414 * </p><p>
7415 * The {@link #onHoverChanged} method is called when the hovered state changes.
7416 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007417 *
7418 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007419 *
7420 * @see #isHovered
7421 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007422 */
7423 public void setHovered(boolean hovered) {
7424 if (hovered) {
7425 if ((mPrivateFlags & HOVERED) == 0) {
7426 mPrivateFlags |= HOVERED;
7427 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007428 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007429 }
7430 } else {
7431 if ((mPrivateFlags & HOVERED) != 0) {
7432 mPrivateFlags &= ~HOVERED;
7433 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007434 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007435 }
7436 }
7437 }
7438
7439 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007440 * Implement this method to handle hover state changes.
7441 * <p>
7442 * This method is called whenever the hover state changes as a result of a
7443 * call to {@link #setHovered}.
7444 * </p>
7445 *
7446 * @param hovered The current hover state, as returned by {@link #isHovered}.
7447 *
7448 * @see #isHovered
7449 * @see #setHovered
7450 */
7451 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007452 }
7453
7454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 * Implement this method to handle touch screen motion events.
7456 *
7457 * @param event The motion event.
7458 * @return True if the event was handled, false otherwise.
7459 */
7460 public boolean onTouchEvent(MotionEvent event) {
7461 final int viewFlags = mViewFlags;
7462
7463 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007464 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007465 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 // A disabled view that is clickable still consumes the touch
7468 // events, it just doesn't respond to them.
7469 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7470 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7471 }
7472
7473 if (mTouchDelegate != null) {
7474 if (mTouchDelegate.onTouchEvent(event)) {
7475 return true;
7476 }
7477 }
7478
7479 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7480 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7481 switch (event.getAction()) {
7482 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007483 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7484 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 // take focus if we don't have it already and we should in
7486 // touch mode.
7487 boolean focusTaken = false;
7488 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7489 focusTaken = requestFocus();
7490 }
7491
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007492 if (prepressed) {
7493 // The button is being released before we actually
7494 // showed it as pressed. Make it show the pressed
7495 // state now (before scheduling the click) to ensure
7496 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007497 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007498 }
Joe Malin32736f02011-01-19 16:14:20 -08007499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 if (!mHasPerformedLongPress) {
7501 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007502 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503
7504 // Only perform take click actions if we were in the pressed state
7505 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007506 // Use a Runnable and post this rather than calling
7507 // performClick directly. This lets other visual state
7508 // of the view update before click actions start.
7509 if (mPerformClick == null) {
7510 mPerformClick = new PerformClick();
7511 }
7512 if (!post(mPerformClick)) {
7513 performClick();
7514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 }
7516 }
7517
7518 if (mUnsetPressedState == null) {
7519 mUnsetPressedState = new UnsetPressedState();
7520 }
7521
Adam Powelle14579b2009-12-16 18:39:52 -08007522 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007523 postDelayed(mUnsetPressedState,
7524 ViewConfiguration.getPressedStateDuration());
7525 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 // If the post failed, unpress right now
7527 mUnsetPressedState.run();
7528 }
Adam Powelle14579b2009-12-16 18:39:52 -08007529 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530 }
7531 break;
7532
7533 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007534 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007535
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007536 if (performButtonActionOnTouchDown(event)) {
7537 break;
7538 }
7539
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007540 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007541 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007542
7543 // For views inside a scrolling container, delay the pressed feedback for
7544 // a short period in case this is a scroll.
7545 if (isInScrollingContainer) {
7546 mPrivateFlags |= PREPRESSED;
7547 if (mPendingCheckForTap == null) {
7548 mPendingCheckForTap = new CheckForTap();
7549 }
7550 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7551 } else {
7552 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007553 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007554 checkForLongClick(0);
7555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 break;
7557
7558 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007559 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007560 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 break;
7562
7563 case MotionEvent.ACTION_MOVE:
7564 final int x = (int) event.getX();
7565 final int y = (int) event.getY();
7566
7567 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007568 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007569 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007570 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007572 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007573 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574
Adam Powell4d6f0662012-02-21 15:11:11 -08007575 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
7578 break;
7579 }
7580 return true;
7581 }
7582
7583 return false;
7584 }
7585
7586 /**
Adam Powell10298662011-08-14 18:26:30 -07007587 * @hide
7588 */
7589 public boolean isInScrollingContainer() {
7590 ViewParent p = getParent();
7591 while (p != null && p instanceof ViewGroup) {
7592 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7593 return true;
7594 }
7595 p = p.getParent();
7596 }
7597 return false;
7598 }
7599
7600 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007601 * Remove the longpress detection timer.
7602 */
7603 private void removeLongPressCallback() {
7604 if (mPendingCheckForLongPress != null) {
7605 removeCallbacks(mPendingCheckForLongPress);
7606 }
7607 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007608
7609 /**
7610 * Remove the pending click action
7611 */
7612 private void removePerformClickCallback() {
7613 if (mPerformClick != null) {
7614 removeCallbacks(mPerformClick);
7615 }
7616 }
7617
Adam Powelle14579b2009-12-16 18:39:52 -08007618 /**
Romain Guya440b002010-02-24 15:57:54 -08007619 * Remove the prepress detection timer.
7620 */
7621 private void removeUnsetPressCallback() {
7622 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7623 setPressed(false);
7624 removeCallbacks(mUnsetPressedState);
7625 }
7626 }
7627
7628 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007629 * Remove the tap detection timer.
7630 */
7631 private void removeTapCallback() {
7632 if (mPendingCheckForTap != null) {
7633 mPrivateFlags &= ~PREPRESSED;
7634 removeCallbacks(mPendingCheckForTap);
7635 }
7636 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007637
7638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 * Cancels a pending long press. Your subclass can use this if you
7640 * want the context menu to come up if the user presses and holds
7641 * at the same place, but you don't want it to come up if they press
7642 * and then move around enough to cause scrolling.
7643 */
7644 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007645 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007646
7647 /*
7648 * The prepressed state handled by the tap callback is a display
7649 * construct, but the tap callback will post a long press callback
7650 * less its own timeout. Remove it here.
7651 */
7652 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 }
7654
7655 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007656 * Remove the pending callback for sending a
7657 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7658 */
7659 private void removeSendViewScrolledAccessibilityEventCallback() {
7660 if (mSendViewScrolledAccessibilityEvent != null) {
7661 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7662 }
7663 }
7664
7665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 * Sets the TouchDelegate for this View.
7667 */
7668 public void setTouchDelegate(TouchDelegate delegate) {
7669 mTouchDelegate = delegate;
7670 }
7671
7672 /**
7673 * Gets the TouchDelegate for this View.
7674 */
7675 public TouchDelegate getTouchDelegate() {
7676 return mTouchDelegate;
7677 }
7678
7679 /**
7680 * Set flags controlling behavior of this view.
7681 *
7682 * @param flags Constant indicating the value which should be set
7683 * @param mask Constant indicating the bit range that should be changed
7684 */
7685 void setFlags(int flags, int mask) {
7686 int old = mViewFlags;
7687 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7688
7689 int changed = mViewFlags ^ old;
7690 if (changed == 0) {
7691 return;
7692 }
7693 int privateFlags = mPrivateFlags;
7694
7695 /* Check if the FOCUSABLE bit has changed */
7696 if (((changed & FOCUSABLE_MASK) != 0) &&
7697 ((privateFlags & HAS_BOUNDS) !=0)) {
7698 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7699 && ((privateFlags & FOCUSED) != 0)) {
7700 /* Give up focus if we are no longer focusable */
7701 clearFocus();
7702 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7703 && ((privateFlags & FOCUSED) == 0)) {
7704 /*
7705 * Tell the view system that we are now available to take focus
7706 * if no one else already has it.
7707 */
7708 if (mParent != null) mParent.focusableViewAvailable(this);
7709 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007710 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7711 notifyAccessibilityStateChanged();
7712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 }
7714
7715 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7716 if ((changed & VISIBILITY_MASK) != 0) {
7717 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007718 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007719 * it was not visible. Marking it drawn ensures that the invalidation will
7720 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007722 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007723 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724
7725 needGlobalAttributesUpdate(true);
7726
7727 // a view becoming visible is worth notifying the parent
7728 // about in case nothing has focus. even if this specific view
7729 // isn't focusable, it may contain something that is, so let
7730 // the root view try to give this focus if nothing else does.
7731 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7732 mParent.focusableViewAvailable(this);
7733 }
7734 }
7735 }
7736
7737 /* Check if the GONE bit has changed */
7738 if ((changed & GONE) != 0) {
7739 needGlobalAttributesUpdate(false);
7740 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741
Romain Guyecd80ee2009-12-03 17:13:02 -08007742 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7743 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007744 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007745 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007746 if (mParent instanceof View) {
7747 // GONE views noop invalidation, so invalidate the parent
7748 ((View) mParent).invalidate(true);
7749 }
7750 // Mark the view drawn to ensure that it gets invalidated properly the next
7751 // time it is visible and gets invalidated
7752 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 }
7754 if (mAttachInfo != null) {
7755 mAttachInfo.mViewVisibilityChanged = true;
7756 }
7757 }
7758
7759 /* Check if the VISIBLE bit has changed */
7760 if ((changed & INVISIBLE) != 0) {
7761 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007762 /*
7763 * If this view is becoming invisible, set the DRAWN flag so that
7764 * the next invalidate() will not be skipped.
7765 */
7766 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767
7768 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007769 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 if (getRootView() != this) {
7771 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007772 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 }
7774 }
7775 if (mAttachInfo != null) {
7776 mAttachInfo.mViewVisibilityChanged = true;
7777 }
7778 }
7779
Adam Powell326d8082009-12-09 15:10:07 -08007780 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007781 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007782 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7783 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007784 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007785 } else if (mParent != null) {
7786 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007787 }
Adam Powell326d8082009-12-09 15:10:07 -08007788 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7789 }
7790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7792 destroyDrawingCache();
7793 }
7794
7795 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7796 destroyDrawingCache();
7797 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007798 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 }
7800
7801 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7802 destroyDrawingCache();
7803 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7804 }
7805
7806 if ((changed & DRAW_MASK) != 0) {
7807 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007808 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 mPrivateFlags &= ~SKIP_DRAW;
7810 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7811 } else {
7812 mPrivateFlags |= SKIP_DRAW;
7813 }
7814 } else {
7815 mPrivateFlags &= ~SKIP_DRAW;
7816 }
7817 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007818 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 }
7820
7821 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007822 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 mParent.recomputeViewAttributes(this);
7824 }
7825 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007826
7827 if (AccessibilityManager.getInstance(mContext).isEnabled()
7828 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7829 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7830 notifyAccessibilityStateChanged();
7831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 }
7833
7834 /**
7835 * Change the view's z order in the tree, so it's on top of other sibling
7836 * views
7837 */
7838 public void bringToFront() {
7839 if (mParent != null) {
7840 mParent.bringChildToFront(this);
7841 }
7842 }
7843
7844 /**
7845 * This is called in response to an internal scroll in this view (i.e., the
7846 * view scrolled its own contents). This is typically as a result of
7847 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7848 * called.
7849 *
7850 * @param l Current horizontal scroll origin.
7851 * @param t Current vertical scroll origin.
7852 * @param oldl Previous horizontal scroll origin.
7853 * @param oldt Previous vertical scroll origin.
7854 */
7855 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007856 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7857 postSendViewScrolledAccessibilityEventCallback();
7858 }
7859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 mBackgroundSizeChanged = true;
7861
7862 final AttachInfo ai = mAttachInfo;
7863 if (ai != null) {
7864 ai.mViewScrollChanged = true;
7865 }
7866 }
7867
7868 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007869 * Interface definition for a callback to be invoked when the layout bounds of a view
7870 * changes due to layout processing.
7871 */
7872 public interface OnLayoutChangeListener {
7873 /**
7874 * Called when the focus state of a view has changed.
7875 *
7876 * @param v The view whose state has changed.
7877 * @param left The new value of the view's left property.
7878 * @param top The new value of the view's top property.
7879 * @param right The new value of the view's right property.
7880 * @param bottom The new value of the view's bottom property.
7881 * @param oldLeft The previous value of the view's left property.
7882 * @param oldTop The previous value of the view's top property.
7883 * @param oldRight The previous value of the view's right property.
7884 * @param oldBottom The previous value of the view's bottom property.
7885 */
7886 void onLayoutChange(View v, int left, int top, int right, int bottom,
7887 int oldLeft, int oldTop, int oldRight, int oldBottom);
7888 }
7889
7890 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 * This is called during layout when the size of this view has changed. If
7892 * you were just added to the view hierarchy, you're called with the old
7893 * values of 0.
7894 *
7895 * @param w Current width of this view.
7896 * @param h Current height of this view.
7897 * @param oldw Old width of this view.
7898 * @param oldh Old height of this view.
7899 */
7900 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7901 }
7902
7903 /**
7904 * Called by draw to draw the child views. This may be overridden
7905 * by derived classes to gain control just before its children are drawn
7906 * (but after its own view has been drawn).
7907 * @param canvas the canvas on which to draw the view
7908 */
7909 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007911 }
7912
7913 /**
7914 * Gets the parent of this view. Note that the parent is a
7915 * ViewParent and not necessarily a View.
7916 *
7917 * @return Parent of this view.
7918 */
7919 public final ViewParent getParent() {
7920 return mParent;
7921 }
7922
7923 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007924 * Set the horizontal scrolled position of your view. This will cause a call to
7925 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7926 * invalidated.
7927 * @param value the x position to scroll to
7928 */
7929 public void setScrollX(int value) {
7930 scrollTo(value, mScrollY);
7931 }
7932
7933 /**
7934 * Set the vertical scrolled position of your view. This will cause a call to
7935 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7936 * invalidated.
7937 * @param value the y position to scroll to
7938 */
7939 public void setScrollY(int value) {
7940 scrollTo(mScrollX, value);
7941 }
7942
7943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 * Return the scrolled left position of this view. This is the left edge of
7945 * the displayed part of your view. You do not need to draw any pixels
7946 * farther left, since those are outside of the frame of your view on
7947 * screen.
7948 *
7949 * @return The left edge of the displayed part of your view, in pixels.
7950 */
7951 public final int getScrollX() {
7952 return mScrollX;
7953 }
7954
7955 /**
7956 * Return the scrolled top position of this view. This is the top edge of
7957 * the displayed part of your view. You do not need to draw any pixels above
7958 * it, since those are outside of the frame of your view on screen.
7959 *
7960 * @return The top edge of the displayed part of your view, in pixels.
7961 */
7962 public final int getScrollY() {
7963 return mScrollY;
7964 }
7965
7966 /**
7967 * Return the width of the your view.
7968 *
7969 * @return The width of your view, in pixels.
7970 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007971 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007972 public final int getWidth() {
7973 return mRight - mLeft;
7974 }
7975
7976 /**
7977 * Return the height of your view.
7978 *
7979 * @return The height of your view, in pixels.
7980 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007981 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 public final int getHeight() {
7983 return mBottom - mTop;
7984 }
7985
7986 /**
7987 * Return the visible drawing bounds of your view. Fills in the output
7988 * rectangle with the values from getScrollX(), getScrollY(),
7989 * getWidth(), and getHeight().
7990 *
7991 * @param outRect The (scrolled) drawing bounds of the view.
7992 */
7993 public void getDrawingRect(Rect outRect) {
7994 outRect.left = mScrollX;
7995 outRect.top = mScrollY;
7996 outRect.right = mScrollX + (mRight - mLeft);
7997 outRect.bottom = mScrollY + (mBottom - mTop);
7998 }
7999
8000 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008001 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8002 * raw width component (that is the result is masked by
8003 * {@link #MEASURED_SIZE_MASK}).
8004 *
8005 * @return The raw measured width of this view.
8006 */
8007 public final int getMeasuredWidth() {
8008 return mMeasuredWidth & MEASURED_SIZE_MASK;
8009 }
8010
8011 /**
8012 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008013 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008014 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 * This should be used during measurement and layout calculations only. Use
8016 * {@link #getWidth()} to see how wide a view is after layout.
8017 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008018 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008020 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 return mMeasuredWidth;
8022 }
8023
8024 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008025 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8026 * raw width component (that is the result is masked by
8027 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008029 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 */
8031 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008032 return mMeasuredHeight & MEASURED_SIZE_MASK;
8033 }
8034
8035 /**
8036 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008037 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008038 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8039 * This should be used during measurement and layout calculations only. Use
8040 * {@link #getHeight()} to see how wide a view is after layout.
8041 *
8042 * @return The measured width of this view as a bit mask.
8043 */
8044 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 return mMeasuredHeight;
8046 }
8047
8048 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008049 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8050 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8051 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8052 * and the height component is at the shifted bits
8053 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8054 */
8055 public final int getMeasuredState() {
8056 return (mMeasuredWidth&MEASURED_STATE_MASK)
8057 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8058 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8059 }
8060
8061 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008062 * The transform matrix of this view, which is calculated based on the current
8063 * roation, scale, and pivot properties.
8064 *
8065 * @see #getRotation()
8066 * @see #getScaleX()
8067 * @see #getScaleY()
8068 * @see #getPivotX()
8069 * @see #getPivotY()
8070 * @return The current transform matrix for the view
8071 */
8072 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008073 if (mTransformationInfo != null) {
8074 updateMatrix();
8075 return mTransformationInfo.mMatrix;
8076 }
8077 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008078 }
8079
8080 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008081 * Utility function to determine if the value is far enough away from zero to be
8082 * considered non-zero.
8083 * @param value A floating point value to check for zero-ness
8084 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8085 */
8086 private static boolean nonzero(float value) {
8087 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8088 }
8089
8090 /**
Jeff Brown86671742010-09-30 20:00:15 -07008091 * Returns true if the transform matrix is the identity matrix.
8092 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008093 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008094 * @return True if the transform matrix is the identity matrix, false otherwise.
8095 */
Jeff Brown86671742010-09-30 20:00:15 -07008096 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008097 if (mTransformationInfo != null) {
8098 updateMatrix();
8099 return mTransformationInfo.mMatrixIsIdentity;
8100 }
8101 return true;
8102 }
8103
8104 void ensureTransformationInfo() {
8105 if (mTransformationInfo == null) {
8106 mTransformationInfo = new TransformationInfo();
8107 }
Jeff Brown86671742010-09-30 20:00:15 -07008108 }
8109
8110 /**
8111 * Recomputes the transform matrix if necessary.
8112 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008113 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008114 final TransformationInfo info = mTransformationInfo;
8115 if (info == null) {
8116 return;
8117 }
8118 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008119 // transform-related properties have changed since the last time someone
8120 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008121
8122 // Figure out if we need to update the pivot point
8123 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008124 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8125 info.mPrevWidth = mRight - mLeft;
8126 info.mPrevHeight = mBottom - mTop;
8127 info.mPivotX = info.mPrevWidth / 2f;
8128 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008129 }
8130 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008131 info.mMatrix.reset();
8132 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8133 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8134 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8135 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008136 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008137 if (info.mCamera == null) {
8138 info.mCamera = new Camera();
8139 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008140 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008141 info.mCamera.save();
8142 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8143 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8144 info.mCamera.getMatrix(info.matrix3D);
8145 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8146 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8147 info.mPivotY + info.mTranslationY);
8148 info.mMatrix.postConcat(info.matrix3D);
8149 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008150 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008151 info.mMatrixDirty = false;
8152 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8153 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008154 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008155 }
8156
8157 /**
8158 * Utility method to retrieve the inverse of the current mMatrix property.
8159 * We cache the matrix to avoid recalculating it when transform properties
8160 * have not changed.
8161 *
8162 * @return The inverse of the current matrix of this view.
8163 */
Jeff Brown86671742010-09-30 20:00:15 -07008164 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008165 final TransformationInfo info = mTransformationInfo;
8166 if (info != null) {
8167 updateMatrix();
8168 if (info.mInverseMatrixDirty) {
8169 if (info.mInverseMatrix == null) {
8170 info.mInverseMatrix = new Matrix();
8171 }
8172 info.mMatrix.invert(info.mInverseMatrix);
8173 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008174 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008175 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008176 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008177 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 }
8179
8180 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008181 * Gets the distance along the Z axis from the camera to this view.
8182 *
8183 * @see #setCameraDistance(float)
8184 *
8185 * @return The distance along the Z axis.
8186 */
8187 public float getCameraDistance() {
8188 ensureTransformationInfo();
8189 final float dpi = mResources.getDisplayMetrics().densityDpi;
8190 final TransformationInfo info = mTransformationInfo;
8191 if (info.mCamera == null) {
8192 info.mCamera = new Camera();
8193 info.matrix3D = new Matrix();
8194 }
8195 return -(info.mCamera.getLocationZ() * dpi);
8196 }
8197
8198 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008199 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8200 * views are drawn) from the camera to this view. The camera's distance
8201 * affects 3D transformations, for instance rotations around the X and Y
8202 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008203 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008204 * use a camera distance that's greater than the height (X axis rotation) or
8205 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008206 *
Romain Guya5364ee2011-02-24 14:46:04 -08008207 * <p>The distance of the camera from the view plane can have an affect on the
8208 * perspective distortion of the view when it is rotated around the x or y axis.
8209 * For example, a large distance will result in a large viewing angle, and there
8210 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008211 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008212 * also result in some drawing artifacts if the rotated view ends up partially
8213 * behind the camera (which is why the recommendation is to use a distance at
8214 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008215 *
Romain Guya5364ee2011-02-24 14:46:04 -08008216 * <p>The distance is expressed in "depth pixels." The default distance depends
8217 * on the screen density. For instance, on a medium density display, the
8218 * default distance is 1280. On a high density display, the default distance
8219 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008220 *
Romain Guya5364ee2011-02-24 14:46:04 -08008221 * <p>If you want to specify a distance that leads to visually consistent
8222 * results across various densities, use the following formula:</p>
8223 * <pre>
8224 * float scale = context.getResources().getDisplayMetrics().density;
8225 * view.setCameraDistance(distance * scale);
8226 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008227 *
Romain Guya5364ee2011-02-24 14:46:04 -08008228 * <p>The density scale factor of a high density display is 1.5,
8229 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008230 *
Romain Guya5364ee2011-02-24 14:46:04 -08008231 * @param distance The distance in "depth pixels", if negative the opposite
8232 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008233 *
8234 * @see #setRotationX(float)
8235 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008236 */
8237 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008238 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008239
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008240 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008241 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008242 final TransformationInfo info = mTransformationInfo;
8243 if (info.mCamera == null) {
8244 info.mCamera = new Camera();
8245 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008246 }
8247
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008248 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8249 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008250
Chet Haase9d1992d2012-03-13 11:03:25 -07008251 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008252 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008253 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008254 }
Romain Guya5364ee2011-02-24 14:46:04 -08008255 }
8256
8257 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008258 * The degrees that the view is rotated around the pivot point.
8259 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008260 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008261 * @see #getPivotX()
8262 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008263 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008264 * @return The degrees of rotation.
8265 */
Chet Haasea5531132012-02-02 13:41:44 -08008266 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008267 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008268 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008269 }
8270
8271 /**
Chet Haase897247b2010-09-09 14:54:47 -07008272 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8273 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008274 *
8275 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008276 *
8277 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008278 * @see #getPivotX()
8279 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008280 * @see #setRotationX(float)
8281 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008282 *
8283 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008284 */
8285 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008286 ensureTransformationInfo();
8287 final TransformationInfo info = mTransformationInfo;
8288 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008289 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008290 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008291 info.mRotation = rotation;
8292 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008293 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008294 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008295 mDisplayList.setRotation(rotation);
8296 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008297 }
8298 }
8299
8300 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008301 * The degrees that the view is rotated around the vertical axis through the pivot point.
8302 *
8303 * @see #getPivotX()
8304 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008305 * @see #setRotationY(float)
8306 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008307 * @return The degrees of Y rotation.
8308 */
Chet Haasea5531132012-02-02 13:41:44 -08008309 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008310 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008311 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008312 }
8313
8314 /**
Chet Haase897247b2010-09-09 14:54:47 -07008315 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8316 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8317 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008318 *
Romain Guya5364ee2011-02-24 14:46:04 -08008319 * When rotating large views, it is recommended to adjust the camera distance
8320 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008321 *
8322 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008323 *
8324 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008325 * @see #getPivotX()
8326 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008327 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008328 * @see #setRotationX(float)
8329 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008330 *
8331 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008332 */
8333 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008334 ensureTransformationInfo();
8335 final TransformationInfo info = mTransformationInfo;
8336 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008337 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008338 info.mRotationY = rotationY;
8339 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008340 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008341 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008342 mDisplayList.setRotationY(rotationY);
8343 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008344 }
8345 }
8346
8347 /**
8348 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8349 *
8350 * @see #getPivotX()
8351 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008352 * @see #setRotationX(float)
8353 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008354 * @return The degrees of X rotation.
8355 */
Chet Haasea5531132012-02-02 13:41:44 -08008356 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008357 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008358 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008359 }
8360
8361 /**
Chet Haase897247b2010-09-09 14:54:47 -07008362 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8363 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8364 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008365 *
Romain Guya5364ee2011-02-24 14:46:04 -08008366 * When rotating large views, it is recommended to adjust the camera distance
8367 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008368 *
8369 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008370 *
8371 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008372 * @see #getPivotX()
8373 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008374 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008375 * @see #setRotationY(float)
8376 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008377 *
8378 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008379 */
8380 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008381 ensureTransformationInfo();
8382 final TransformationInfo info = mTransformationInfo;
8383 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008384 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008385 info.mRotationX = rotationX;
8386 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008387 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008388 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008389 mDisplayList.setRotationX(rotationX);
8390 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008391 }
8392 }
8393
8394 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008395 * The amount that the view is scaled in x around the pivot point, as a proportion of
8396 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8397 *
Joe Onorato93162322010-09-16 15:42:01 -04008398 * <p>By default, this is 1.0f.
8399 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008400 * @see #getPivotX()
8401 * @see #getPivotY()
8402 * @return The scaling factor.
8403 */
Chet Haasea5531132012-02-02 13:41:44 -08008404 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008405 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008406 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008407 }
8408
8409 /**
8410 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8411 * the view's unscaled width. A value of 1 means that no scaling is applied.
8412 *
8413 * @param scaleX The scaling factor.
8414 * @see #getPivotX()
8415 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008416 *
8417 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008418 */
8419 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008420 ensureTransformationInfo();
8421 final TransformationInfo info = mTransformationInfo;
8422 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008423 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008424 info.mScaleX = scaleX;
8425 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008426 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008427 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008428 mDisplayList.setScaleX(scaleX);
8429 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008430 }
8431 }
8432
8433 /**
8434 * The amount that the view is scaled in y around the pivot point, as a proportion of
8435 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8436 *
Joe Onorato93162322010-09-16 15:42:01 -04008437 * <p>By default, this is 1.0f.
8438 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 * @see #getPivotX()
8440 * @see #getPivotY()
8441 * @return The scaling factor.
8442 */
Chet Haasea5531132012-02-02 13:41:44 -08008443 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008444 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008445 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008446 }
8447
8448 /**
8449 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8450 * the view's unscaled width. A value of 1 means that no scaling is applied.
8451 *
8452 * @param scaleY The scaling factor.
8453 * @see #getPivotX()
8454 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008455 *
8456 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008457 */
8458 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008459 ensureTransformationInfo();
8460 final TransformationInfo info = mTransformationInfo;
8461 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008462 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008463 info.mScaleY = scaleY;
8464 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008465 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008466 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008467 mDisplayList.setScaleY(scaleY);
8468 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008469 }
8470 }
8471
8472 /**
8473 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8474 * and {@link #setScaleX(float) scaled}.
8475 *
8476 * @see #getRotation()
8477 * @see #getScaleX()
8478 * @see #getScaleY()
8479 * @see #getPivotY()
8480 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008481 *
8482 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008483 */
Chet Haasea5531132012-02-02 13:41:44 -08008484 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008485 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008486 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008487 }
8488
8489 /**
8490 * Sets the x location of the point around which the view is
8491 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008492 * By default, the pivot point is centered on the object.
8493 * Setting this property disables this behavior and causes the view to use only the
8494 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008495 *
8496 * @param pivotX The x location of the pivot point.
8497 * @see #getRotation()
8498 * @see #getScaleX()
8499 * @see #getScaleY()
8500 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008501 *
8502 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008503 */
8504 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008505 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008506 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008507 final TransformationInfo info = mTransformationInfo;
8508 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008509 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008510 info.mPivotX = pivotX;
8511 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008512 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008513 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008514 mDisplayList.setPivotX(pivotX);
8515 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008516 }
8517 }
8518
8519 /**
8520 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8521 * and {@link #setScaleY(float) scaled}.
8522 *
8523 * @see #getRotation()
8524 * @see #getScaleX()
8525 * @see #getScaleY()
8526 * @see #getPivotY()
8527 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008528 *
8529 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008530 */
Chet Haasea5531132012-02-02 13:41:44 -08008531 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008532 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008533 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008534 }
8535
8536 /**
8537 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008538 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8539 * Setting this property disables this behavior and causes the view to use only the
8540 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008541 *
8542 * @param pivotY The y location of the pivot point.
8543 * @see #getRotation()
8544 * @see #getScaleX()
8545 * @see #getScaleY()
8546 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008547 *
8548 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008549 */
8550 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008551 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008552 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008553 final TransformationInfo info = mTransformationInfo;
8554 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008555 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008556 info.mPivotY = pivotY;
8557 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008558 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008559 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008560 mDisplayList.setPivotY(pivotY);
8561 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008562 }
8563 }
8564
8565 /**
8566 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8567 * completely transparent and 1 means the view is completely opaque.
8568 *
Joe Onorato93162322010-09-16 15:42:01 -04008569 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008570 * @return The opacity of the view.
8571 */
Chet Haasea5531132012-02-02 13:41:44 -08008572 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008573 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008574 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008575 }
8576
8577 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008578 * Returns whether this View has content which overlaps. This function, intended to be
8579 * overridden by specific View types, is an optimization when alpha is set on a view. If
8580 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8581 * and then composited it into place, which can be expensive. If the view has no overlapping
8582 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8583 * An example of overlapping rendering is a TextView with a background image, such as a
8584 * Button. An example of non-overlapping rendering is a TextView with no background, or
8585 * an ImageView with only the foreground image. The default implementation returns true;
8586 * subclasses should override if they have cases which can be optimized.
8587 *
8588 * @return true if the content in this view might overlap, false otherwise.
8589 */
8590 public boolean hasOverlappingRendering() {
8591 return true;
8592 }
8593
8594 /**
Romain Guy171c5922011-01-06 10:04:23 -08008595 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8596 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008597 *
Romain Guy171c5922011-01-06 10:04:23 -08008598 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8599 * responsible for applying the opacity itself. Otherwise, calling this method is
8600 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008601 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008602 *
Chet Haasea5531132012-02-02 13:41:44 -08008603 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8604 * performance implications. It is generally best to use the alpha property sparingly and
8605 * transiently, as in the case of fading animations.</p>
8606 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008607 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008608 *
Joe Malin32736f02011-01-19 16:14:20 -08008609 * @see #setLayerType(int, android.graphics.Paint)
8610 *
Chet Haase73066682010-11-29 15:55:32 -08008611 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008612 */
8613 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008614 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008615 if (mTransformationInfo.mAlpha != alpha) {
8616 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008617 if (onSetAlpha((int) (alpha * 255))) {
8618 mPrivateFlags |= ALPHA_SET;
8619 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008620 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008621 invalidate(true);
8622 } else {
8623 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008624 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008625 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008626 mDisplayList.setAlpha(alpha);
8627 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008628 }
Chet Haaseed032702010-10-01 14:05:54 -07008629 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008630 }
8631
8632 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008633 * Faster version of setAlpha() which performs the same steps except there are
8634 * no calls to invalidate(). The caller of this function should perform proper invalidation
8635 * on the parent and this object. The return value indicates whether the subclass handles
8636 * alpha (the return value for onSetAlpha()).
8637 *
8638 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008639 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8640 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008641 */
8642 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008643 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008644 if (mTransformationInfo.mAlpha != alpha) {
8645 mTransformationInfo.mAlpha = alpha;
8646 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8647 if (subclassHandlesAlpha) {
8648 mPrivateFlags |= ALPHA_SET;
8649 return true;
8650 } else {
8651 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008652 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008653 mDisplayList.setAlpha(alpha);
8654 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008655 }
Chet Haasea00f3862011-02-22 06:34:40 -08008656 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008657 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008658 }
8659
8660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 * Top position of this view relative to its parent.
8662 *
8663 * @return The top of this view, in pixels.
8664 */
8665 @ViewDebug.CapturedViewProperty
8666 public final int getTop() {
8667 return mTop;
8668 }
8669
8670 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008671 * Sets the top position of this view relative to its parent. This method is meant to be called
8672 * by the layout system and should not generally be called otherwise, because the property
8673 * may be changed at any time by the layout.
8674 *
8675 * @param top The top of this view, in pixels.
8676 */
8677 public final void setTop(int top) {
8678 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008679 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008680 final boolean matrixIsIdentity = mTransformationInfo == null
8681 || mTransformationInfo.mMatrixIsIdentity;
8682 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008683 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008684 int minTop;
8685 int yLoc;
8686 if (top < mTop) {
8687 minTop = top;
8688 yLoc = top - mTop;
8689 } else {
8690 minTop = mTop;
8691 yLoc = 0;
8692 }
Chet Haasee9140a72011-02-16 16:23:29 -08008693 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008694 }
8695 } else {
8696 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008697 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008698 }
8699
Chet Haaseed032702010-10-01 14:05:54 -07008700 int width = mRight - mLeft;
8701 int oldHeight = mBottom - mTop;
8702
Chet Haase21cd1382010-09-01 17:42:29 -07008703 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07008704 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008705 mDisplayList.setTop(mTop);
8706 }
Chet Haase21cd1382010-09-01 17:42:29 -07008707
Chet Haaseed032702010-10-01 14:05:54 -07008708 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8709
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008710 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008711 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8712 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008713 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008714 }
Chet Haase21cd1382010-09-01 17:42:29 -07008715 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008716 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008717 }
Chet Haase55dbb652010-12-21 20:15:08 -08008718 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008719 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008720 }
8721 }
8722
8723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 * Bottom position of this view relative to its parent.
8725 *
8726 * @return The bottom of this view, in pixels.
8727 */
8728 @ViewDebug.CapturedViewProperty
8729 public final int getBottom() {
8730 return mBottom;
8731 }
8732
8733 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008734 * True if this view has changed since the last time being drawn.
8735 *
8736 * @return The dirty state of this view.
8737 */
8738 public boolean isDirty() {
8739 return (mPrivateFlags & DIRTY_MASK) != 0;
8740 }
8741
8742 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008743 * Sets the bottom position of this view relative to its parent. This method is meant to be
8744 * called by the layout system and should not generally be called otherwise, because the
8745 * property may be changed at any time by the layout.
8746 *
8747 * @param bottom The bottom of this view, in pixels.
8748 */
8749 public final void setBottom(int bottom) {
8750 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008751 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008752 final boolean matrixIsIdentity = mTransformationInfo == null
8753 || mTransformationInfo.mMatrixIsIdentity;
8754 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008755 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008756 int maxBottom;
8757 if (bottom < mBottom) {
8758 maxBottom = mBottom;
8759 } else {
8760 maxBottom = bottom;
8761 }
Chet Haasee9140a72011-02-16 16:23:29 -08008762 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008763 }
8764 } else {
8765 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008766 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008767 }
8768
Chet Haaseed032702010-10-01 14:05:54 -07008769 int width = mRight - mLeft;
8770 int oldHeight = mBottom - mTop;
8771
Chet Haase21cd1382010-09-01 17:42:29 -07008772 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07008773 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008774 mDisplayList.setBottom(mBottom);
8775 }
Chet Haase21cd1382010-09-01 17:42:29 -07008776
Chet Haaseed032702010-10-01 14:05:54 -07008777 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8778
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008779 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008780 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8781 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008782 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008783 }
Chet Haase21cd1382010-09-01 17:42:29 -07008784 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008785 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008786 }
Chet Haase55dbb652010-12-21 20:15:08 -08008787 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008788 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008789 }
8790 }
8791
8792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 * Left position of this view relative to its parent.
8794 *
8795 * @return The left edge of this view, in pixels.
8796 */
8797 @ViewDebug.CapturedViewProperty
8798 public final int getLeft() {
8799 return mLeft;
8800 }
8801
8802 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008803 * Sets the left position of this view relative to its parent. This method is meant to be called
8804 * by the layout system and should not generally be called otherwise, because the property
8805 * may be changed at any time by the layout.
8806 *
8807 * @param left The bottom of this view, in pixels.
8808 */
8809 public final void setLeft(int left) {
8810 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008811 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008812 final boolean matrixIsIdentity = mTransformationInfo == null
8813 || mTransformationInfo.mMatrixIsIdentity;
8814 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008815 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008816 int minLeft;
8817 int xLoc;
8818 if (left < mLeft) {
8819 minLeft = left;
8820 xLoc = left - mLeft;
8821 } else {
8822 minLeft = mLeft;
8823 xLoc = 0;
8824 }
Chet Haasee9140a72011-02-16 16:23:29 -08008825 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008826 }
8827 } else {
8828 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008829 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008830 }
8831
Chet Haaseed032702010-10-01 14:05:54 -07008832 int oldWidth = mRight - mLeft;
8833 int height = mBottom - mTop;
8834
Chet Haase21cd1382010-09-01 17:42:29 -07008835 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07008836 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008837 mDisplayList.setLeft(left);
8838 }
Chet Haase21cd1382010-09-01 17:42:29 -07008839
Chet Haaseed032702010-10-01 14:05:54 -07008840 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8841
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008843 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8844 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008845 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008846 }
Chet Haase21cd1382010-09-01 17:42:29 -07008847 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008848 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008849 }
Chet Haase55dbb652010-12-21 20:15:08 -08008850 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008851 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008852 }
8853 }
8854
8855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 * Right position of this view relative to its parent.
8857 *
8858 * @return The right edge of this view, in pixels.
8859 */
8860 @ViewDebug.CapturedViewProperty
8861 public final int getRight() {
8862 return mRight;
8863 }
8864
8865 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008866 * Sets the right position of this view relative to its parent. This method is meant to be called
8867 * by the layout system and should not generally be called otherwise, because the property
8868 * may be changed at any time by the layout.
8869 *
8870 * @param right The bottom of this view, in pixels.
8871 */
8872 public final void setRight(int right) {
8873 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008874 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008875 final boolean matrixIsIdentity = mTransformationInfo == null
8876 || mTransformationInfo.mMatrixIsIdentity;
8877 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008878 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008879 int maxRight;
8880 if (right < mRight) {
8881 maxRight = mRight;
8882 } else {
8883 maxRight = right;
8884 }
Chet Haasee9140a72011-02-16 16:23:29 -08008885 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008886 }
8887 } else {
8888 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008889 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008890 }
8891
Chet Haaseed032702010-10-01 14:05:54 -07008892 int oldWidth = mRight - mLeft;
8893 int height = mBottom - mTop;
8894
Chet Haase21cd1382010-09-01 17:42:29 -07008895 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07008896 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008897 mDisplayList.setRight(mRight);
8898 }
Chet Haase21cd1382010-09-01 17:42:29 -07008899
Chet Haaseed032702010-10-01 14:05:54 -07008900 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8901
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008902 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008903 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8904 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008905 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008906 }
Chet Haase21cd1382010-09-01 17:42:29 -07008907 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008908 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008909 }
Chet Haase55dbb652010-12-21 20:15:08 -08008910 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008911 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008912 }
8913 }
8914
8915 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008916 * The visual x position of this view, in pixels. This is equivalent to the
8917 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008918 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008919 *
Chet Haasedf030d22010-07-30 17:22:38 -07008920 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008921 */
Chet Haasea5531132012-02-02 13:41:44 -08008922 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008923 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008924 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008925 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008926
Chet Haasedf030d22010-07-30 17:22:38 -07008927 /**
8928 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8929 * {@link #setTranslationX(float) translationX} property to be the difference between
8930 * the x value passed in and the current {@link #getLeft() left} property.
8931 *
8932 * @param x The visual x position of this view, in pixels.
8933 */
8934 public void setX(float x) {
8935 setTranslationX(x - mLeft);
8936 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008937
Chet Haasedf030d22010-07-30 17:22:38 -07008938 /**
8939 * The visual y position of this view, in pixels. This is equivalent to the
8940 * {@link #setTranslationY(float) translationY} property plus the current
8941 * {@link #getTop() top} property.
8942 *
8943 * @return The visual y position of this view, in pixels.
8944 */
Chet Haasea5531132012-02-02 13:41:44 -08008945 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008946 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008947 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008948 }
8949
8950 /**
8951 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8952 * {@link #setTranslationY(float) translationY} property to be the difference between
8953 * the y value passed in and the current {@link #getTop() top} property.
8954 *
8955 * @param y The visual y position of this view, in pixels.
8956 */
8957 public void setY(float y) {
8958 setTranslationY(y - mTop);
8959 }
8960
8961
8962 /**
8963 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8964 * This position is post-layout, in addition to wherever the object's
8965 * layout placed it.
8966 *
8967 * @return The horizontal position of this view relative to its left position, in pixels.
8968 */
Chet Haasea5531132012-02-02 13:41:44 -08008969 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008970 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008971 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008972 }
8973
8974 /**
8975 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8976 * This effectively positions the object post-layout, in addition to wherever the object's
8977 * layout placed it.
8978 *
8979 * @param translationX The horizontal position of this view relative to its left position,
8980 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008981 *
8982 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008983 */
8984 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008985 ensureTransformationInfo();
8986 final TransformationInfo info = mTransformationInfo;
8987 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008988 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008989 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008990 info.mTranslationX = translationX;
8991 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008992 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008993 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008994 mDisplayList.setTranslationX(translationX);
8995 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008996 }
8997 }
8998
8999 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009000 * The horizontal location of this view relative to its {@link #getTop() top} position.
9001 * This position is post-layout, in addition to wherever the object's
9002 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009003 *
Chet Haasedf030d22010-07-30 17:22:38 -07009004 * @return The vertical position of this view relative to its top position,
9005 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009006 */
Chet Haasea5531132012-02-02 13:41:44 -08009007 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009008 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009009 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009010 }
9011
9012 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009013 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9014 * This effectively positions the object post-layout, in addition to wherever the object's
9015 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009016 *
Chet Haasedf030d22010-07-30 17:22:38 -07009017 * @param translationY The vertical position of this view relative to its top position,
9018 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009019 *
9020 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009021 */
Chet Haasedf030d22010-07-30 17:22:38 -07009022 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 ensureTransformationInfo();
9024 final TransformationInfo info = mTransformationInfo;
9025 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009026 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009027 info.mTranslationY = translationY;
9028 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009029 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009030 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009031 mDisplayList.setTranslationY(translationY);
9032 }
Chet Haasedf030d22010-07-30 17:22:38 -07009033 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009034 }
9035
9036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 * Hit rectangle in parent's coordinates
9038 *
9039 * @param outRect The hit rectangle of the view.
9040 */
9041 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009042 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009043 final TransformationInfo info = mTransformationInfo;
9044 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009045 outRect.set(mLeft, mTop, mRight, mBottom);
9046 } else {
9047 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009048 tmpRect.set(-info.mPivotX, -info.mPivotY,
9049 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9050 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009051 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9052 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009053 }
9054 }
9055
9056 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009057 * Determines whether the given point, in local coordinates is inside the view.
9058 */
9059 /*package*/ final boolean pointInView(float localX, float localY) {
9060 return localX >= 0 && localX < (mRight - mLeft)
9061 && localY >= 0 && localY < (mBottom - mTop);
9062 }
9063
9064 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009065 * Utility method to determine whether the given point, in local coordinates,
9066 * is inside the view, where the area of the view is expanded by the slop factor.
9067 * This method is called while processing touch-move events to determine if the event
9068 * is still within the view.
9069 */
9070 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009071 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009072 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 }
9074
9075 /**
9076 * When a view has focus and the user navigates away from it, the next view is searched for
9077 * starting from the rectangle filled in by this method.
9078 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009079 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9080 * of the view. However, if your view maintains some idea of internal selection,
9081 * such as a cursor, or a selected row or column, you should override this method and
9082 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 *
9084 * @param r The rectangle to fill in, in this view's coordinates.
9085 */
9086 public void getFocusedRect(Rect r) {
9087 getDrawingRect(r);
9088 }
9089
9090 /**
9091 * If some part of this view is not clipped by any of its parents, then
9092 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009093 * coordinates (without taking possible View rotations into account), offset
9094 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9095 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096 *
9097 * @param r If true is returned, r holds the global coordinates of the
9098 * visible portion of this view.
9099 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9100 * between this view and its root. globalOffet may be null.
9101 * @return true if r is non-empty (i.e. part of the view is visible at the
9102 * root level.
9103 */
9104 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9105 int width = mRight - mLeft;
9106 int height = mBottom - mTop;
9107 if (width > 0 && height > 0) {
9108 r.set(0, 0, width, height);
9109 if (globalOffset != null) {
9110 globalOffset.set(-mScrollX, -mScrollY);
9111 }
9112 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9113 }
9114 return false;
9115 }
9116
9117 public final boolean getGlobalVisibleRect(Rect r) {
9118 return getGlobalVisibleRect(r, null);
9119 }
9120
9121 public final boolean getLocalVisibleRect(Rect r) {
9122 Point offset = new Point();
9123 if (getGlobalVisibleRect(r, offset)) {
9124 r.offset(-offset.x, -offset.y); // make r local
9125 return true;
9126 }
9127 return false;
9128 }
9129
9130 /**
9131 * Offset this view's vertical location by the specified number of pixels.
9132 *
9133 * @param offset the number of pixels to offset the view by
9134 */
9135 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009136 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009137 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009138 final boolean matrixIsIdentity = mTransformationInfo == null
9139 || mTransformationInfo.mMatrixIsIdentity;
9140 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009141 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009142 invalidateViewProperty(false, false);
9143 } else {
9144 final ViewParent p = mParent;
9145 if (p != null && mAttachInfo != null) {
9146 final Rect r = mAttachInfo.mTmpInvalRect;
9147 int minTop;
9148 int maxBottom;
9149 int yLoc;
9150 if (offset < 0) {
9151 minTop = mTop + offset;
9152 maxBottom = mBottom;
9153 yLoc = offset;
9154 } else {
9155 minTop = mTop;
9156 maxBottom = mBottom + offset;
9157 yLoc = 0;
9158 }
9159 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9160 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009161 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009162 }
9163 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009164 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009166
Chet Haasec3aa3612010-06-17 08:50:37 -07009167 mTop += offset;
9168 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009169 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009170 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009171 invalidateViewProperty(false, false);
9172 } else {
9173 if (!matrixIsIdentity) {
9174 invalidateViewProperty(false, true);
9175 }
9176 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009177 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 }
9180
9181 /**
9182 * Offset this view's horizontal location by the specified amount of pixels.
9183 *
9184 * @param offset the numer of pixels to offset the view by
9185 */
9186 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009187 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009188 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009189 final boolean matrixIsIdentity = mTransformationInfo == null
9190 || mTransformationInfo.mMatrixIsIdentity;
9191 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009192 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009193 invalidateViewProperty(false, false);
9194 } else {
9195 final ViewParent p = mParent;
9196 if (p != null && mAttachInfo != null) {
9197 final Rect r = mAttachInfo.mTmpInvalRect;
9198 int minLeft;
9199 int maxRight;
9200 if (offset < 0) {
9201 minLeft = mLeft + offset;
9202 maxRight = mRight;
9203 } else {
9204 minLeft = mLeft;
9205 maxRight = mRight + offset;
9206 }
9207 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9208 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009209 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009210 }
9211 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009212 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009213 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009214
Chet Haasec3aa3612010-06-17 08:50:37 -07009215 mLeft += offset;
9216 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009217 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009218 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009219 invalidateViewProperty(false, false);
9220 } else {
9221 if (!matrixIsIdentity) {
9222 invalidateViewProperty(false, true);
9223 }
9224 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009225 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 }
9228
9229 /**
9230 * Get the LayoutParams associated with this view. All views should have
9231 * layout parameters. These supply parameters to the <i>parent</i> of this
9232 * view specifying how it should be arranged. There are many subclasses of
9233 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9234 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009235 *
9236 * This method may return null if this View is not attached to a parent
9237 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9238 * was not invoked successfully. When a View is attached to a parent
9239 * ViewGroup, this method must not return null.
9240 *
9241 * @return The LayoutParams associated with this view, or null if no
9242 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009244 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 public ViewGroup.LayoutParams getLayoutParams() {
9246 return mLayoutParams;
9247 }
9248
9249 /**
9250 * Set the layout parameters associated with this view. These supply
9251 * parameters to the <i>parent</i> of this view specifying how it should be
9252 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9253 * correspond to the different subclasses of ViewGroup that are responsible
9254 * for arranging their children.
9255 *
Romain Guy01c174b2011-02-22 11:51:06 -08009256 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 */
9258 public void setLayoutParams(ViewGroup.LayoutParams params) {
9259 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009260 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261 }
9262 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009263 if (mParent instanceof ViewGroup) {
9264 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 requestLayout();
9267 }
9268
9269 /**
9270 * Set the scrolled position of your view. This will cause a call to
9271 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9272 * invalidated.
9273 * @param x the x position to scroll to
9274 * @param y the y position to scroll to
9275 */
9276 public void scrollTo(int x, int y) {
9277 if (mScrollX != x || mScrollY != y) {
9278 int oldX = mScrollX;
9279 int oldY = mScrollY;
9280 mScrollX = x;
9281 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009282 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009284 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009285 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 }
9288 }
9289
9290 /**
9291 * Move the scrolled position of your view. This will cause a call to
9292 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9293 * invalidated.
9294 * @param x the amount of pixels to scroll by horizontally
9295 * @param y the amount of pixels to scroll by vertically
9296 */
9297 public void scrollBy(int x, int y) {
9298 scrollTo(mScrollX + x, mScrollY + y);
9299 }
9300
9301 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009302 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9303 * animation to fade the scrollbars out after a default delay. If a subclass
9304 * provides animated scrolling, the start delay should equal the duration
9305 * of the scrolling animation.</p>
9306 *
9307 * <p>The animation starts only if at least one of the scrollbars is
9308 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9309 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9310 * this method returns true, and false otherwise. If the animation is
9311 * started, this method calls {@link #invalidate()}; in that case the
9312 * caller should not call {@link #invalidate()}.</p>
9313 *
9314 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009315 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009316 *
9317 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9318 * and {@link #scrollTo(int, int)}.</p>
9319 *
9320 * @return true if the animation is played, false otherwise
9321 *
9322 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009323 * @see #scrollBy(int, int)
9324 * @see #scrollTo(int, int)
9325 * @see #isHorizontalScrollBarEnabled()
9326 * @see #isVerticalScrollBarEnabled()
9327 * @see #setHorizontalScrollBarEnabled(boolean)
9328 * @see #setVerticalScrollBarEnabled(boolean)
9329 */
9330 protected boolean awakenScrollBars() {
9331 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009332 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009333 }
9334
9335 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009336 * Trigger the scrollbars to draw.
9337 * This method differs from awakenScrollBars() only in its default duration.
9338 * initialAwakenScrollBars() will show the scroll bars for longer than
9339 * usual to give the user more of a chance to notice them.
9340 *
9341 * @return true if the animation is played, false otherwise.
9342 */
9343 private boolean initialAwakenScrollBars() {
9344 return mScrollCache != null &&
9345 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9346 }
9347
9348 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009349 * <p>
9350 * Trigger the scrollbars to draw. When invoked this method starts an
9351 * animation to fade the scrollbars out after a fixed delay. If a subclass
9352 * provides animated scrolling, the start delay should equal the duration of
9353 * the scrolling animation.
9354 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009355 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009356 * <p>
9357 * The animation starts only if at least one of the scrollbars is enabled,
9358 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9359 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9360 * this method returns true, and false otherwise. If the animation is
9361 * started, this method calls {@link #invalidate()}; in that case the caller
9362 * should not call {@link #invalidate()}.
9363 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009364 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009365 * <p>
9366 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009367 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009368 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009369 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009370 * @param startDelay the delay, in milliseconds, after which the animation
9371 * should start; when the delay is 0, the animation starts
9372 * immediately
9373 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009374 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009375 * @see #scrollBy(int, int)
9376 * @see #scrollTo(int, int)
9377 * @see #isHorizontalScrollBarEnabled()
9378 * @see #isVerticalScrollBarEnabled()
9379 * @see #setHorizontalScrollBarEnabled(boolean)
9380 * @see #setVerticalScrollBarEnabled(boolean)
9381 */
9382 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009383 return awakenScrollBars(startDelay, true);
9384 }
Joe Malin32736f02011-01-19 16:14:20 -08009385
Mike Cleron290947b2009-09-29 18:34:32 -07009386 /**
9387 * <p>
9388 * Trigger the scrollbars to draw. When invoked this method starts an
9389 * animation to fade the scrollbars out after a fixed delay. If a subclass
9390 * provides animated scrolling, the start delay should equal the duration of
9391 * the scrolling animation.
9392 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009393 *
Mike Cleron290947b2009-09-29 18:34:32 -07009394 * <p>
9395 * The animation starts only if at least one of the scrollbars is enabled,
9396 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9397 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9398 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009399 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009400 * is set to true; in that case the caller
9401 * should not call {@link #invalidate()}.
9402 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009403 *
Mike Cleron290947b2009-09-29 18:34:32 -07009404 * <p>
9405 * This method should be invoked everytime a subclass directly updates the
9406 * scroll parameters.
9407 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009408 *
Mike Cleron290947b2009-09-29 18:34:32 -07009409 * @param startDelay the delay, in milliseconds, after which the animation
9410 * should start; when the delay is 0, the animation starts
9411 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009412 *
Mike Cleron290947b2009-09-29 18:34:32 -07009413 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009414 *
Mike Cleron290947b2009-09-29 18:34:32 -07009415 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009416 *
Mike Cleron290947b2009-09-29 18:34:32 -07009417 * @see #scrollBy(int, int)
9418 * @see #scrollTo(int, int)
9419 * @see #isHorizontalScrollBarEnabled()
9420 * @see #isVerticalScrollBarEnabled()
9421 * @see #setHorizontalScrollBarEnabled(boolean)
9422 * @see #setVerticalScrollBarEnabled(boolean)
9423 */
9424 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009425 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009426
Mike Cleronf116bf82009-09-27 19:14:12 -07009427 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9428 return false;
9429 }
9430
9431 if (scrollCache.scrollBar == null) {
9432 scrollCache.scrollBar = new ScrollBarDrawable();
9433 }
9434
9435 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9436
Mike Cleron290947b2009-09-29 18:34:32 -07009437 if (invalidate) {
9438 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009439 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009440 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009441
9442 if (scrollCache.state == ScrollabilityCache.OFF) {
9443 // FIXME: this is copied from WindowManagerService.
9444 // We should get this value from the system when it
9445 // is possible to do so.
9446 final int KEY_REPEAT_FIRST_DELAY = 750;
9447 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9448 }
9449
9450 // Tell mScrollCache when we should start fading. This may
9451 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009452 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009453 scrollCache.fadeStartTime = fadeStartTime;
9454 scrollCache.state = ScrollabilityCache.ON;
9455
9456 // Schedule our fader to run, unscheduling any old ones first
9457 if (mAttachInfo != null) {
9458 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9459 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9460 }
9461
9462 return true;
9463 }
9464
9465 return false;
9466 }
9467
9468 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009469 * Do not invalidate views which are not visible and which are not running an animation. They
9470 * will not get drawn and they should not set dirty flags as if they will be drawn
9471 */
9472 private boolean skipInvalidate() {
9473 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9474 (!(mParent instanceof ViewGroup) ||
9475 !((ViewGroup) mParent).isViewTransitioning(this));
9476 }
9477 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009478 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009479 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9480 * in the future. This must be called from a UI thread. To call from a non-UI
9481 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 *
9483 * WARNING: This method is destructive to dirty.
9484 * @param dirty the rectangle representing the bounds of the dirty region
9485 */
9486 public void invalidate(Rect dirty) {
9487 if (ViewDebug.TRACE_HIERARCHY) {
9488 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9489 }
9490
Chet Haaseaceafe62011-08-26 15:44:33 -07009491 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009492 return;
9493 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009494 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009495 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9496 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009497 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009498 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009499 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009500 final ViewParent p = mParent;
9501 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009502 //noinspection PointlessBooleanExpression,ConstantConditions
9503 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9504 if (p != null && ai != null && ai.mHardwareAccelerated) {
9505 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009506 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009507 p.invalidateChild(this, null);
9508 return;
9509 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 if (p != null && ai != null) {
9512 final int scrollX = mScrollX;
9513 final int scrollY = mScrollY;
9514 final Rect r = ai.mTmpInvalRect;
9515 r.set(dirty.left - scrollX, dirty.top - scrollY,
9516 dirty.right - scrollX, dirty.bottom - scrollY);
9517 mParent.invalidateChild(this, r);
9518 }
9519 }
9520 }
9521
9522 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009523 * 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 -08009524 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009525 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9526 * will be called at some point in the future. This must be called from
9527 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009528 * @param l the left position of the dirty region
9529 * @param t the top position of the dirty region
9530 * @param r the right position of the dirty region
9531 * @param b the bottom position of the dirty region
9532 */
9533 public void invalidate(int l, int t, int r, int b) {
9534 if (ViewDebug.TRACE_HIERARCHY) {
9535 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9536 }
9537
Chet Haaseaceafe62011-08-26 15:44:33 -07009538 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009539 return;
9540 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009541 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009542 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9543 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009544 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009545 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009546 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 final ViewParent p = mParent;
9548 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009549 //noinspection PointlessBooleanExpression,ConstantConditions
9550 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9551 if (p != null && ai != null && ai.mHardwareAccelerated) {
9552 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009553 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009554 p.invalidateChild(this, null);
9555 return;
9556 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 if (p != null && ai != null && l < r && t < b) {
9559 final int scrollX = mScrollX;
9560 final int scrollY = mScrollY;
9561 final Rect tmpr = ai.mTmpInvalRect;
9562 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9563 p.invalidateChild(this, tmpr);
9564 }
9565 }
9566 }
9567
9568 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009569 * Invalidate the whole view. If the view is visible,
9570 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9571 * the future. This must be called from a UI thread. To call from a non-UI thread,
9572 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573 */
9574 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009575 invalidate(true);
9576 }
Joe Malin32736f02011-01-19 16:14:20 -08009577
Chet Haaseed032702010-10-01 14:05:54 -07009578 /**
9579 * This is where the invalidate() work actually happens. A full invalidate()
9580 * causes the drawing cache to be invalidated, but this function can be called with
9581 * invalidateCache set to false to skip that invalidation step for cases that do not
9582 * need it (for example, a component that remains at the same dimensions with the same
9583 * content).
9584 *
9585 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9586 * well. This is usually true for a full invalidate, but may be set to false if the
9587 * View's contents or dimensions have not changed.
9588 */
Romain Guy849d0a32011-02-01 17:20:48 -08009589 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009590 if (ViewDebug.TRACE_HIERARCHY) {
9591 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9592 }
9593
Chet Haaseaceafe62011-08-26 15:44:33 -07009594 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009595 return;
9596 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009597 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009598 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009599 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9600 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009601 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009602 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009603 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009604 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009605 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009607 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009608 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009609 //noinspection PointlessBooleanExpression,ConstantConditions
9610 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9611 if (p != null && ai != null && ai.mHardwareAccelerated) {
9612 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009613 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009614 p.invalidateChild(this, null);
9615 return;
9616 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009617 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 if (p != null && ai != null) {
9620 final Rect r = ai.mTmpInvalRect;
9621 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9622 // Don't call invalidate -- we don't want to internally scroll
9623 // our own bounds
9624 p.invalidateChild(this, r);
9625 }
9626 }
9627 }
9628
9629 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009630 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9631 * set any flags or handle all of the cases handled by the default invalidation methods.
9632 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9633 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9634 * walk up the hierarchy, transforming the dirty rect as necessary.
9635 *
9636 * The method also handles normal invalidation logic if display list properties are not
9637 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9638 * backup approach, to handle these cases used in the various property-setting methods.
9639 *
9640 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9641 * are not being used in this view
9642 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9643 * list properties are not being used in this view
9644 */
9645 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009646 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009647 if (invalidateParent) {
9648 invalidateParentCaches();
9649 }
9650 if (forceRedraw) {
9651 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9652 }
9653 invalidate(false);
9654 } else {
9655 final AttachInfo ai = mAttachInfo;
9656 final ViewParent p = mParent;
9657 if (p != null && ai != null) {
9658 final Rect r = ai.mTmpInvalRect;
9659 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9660 if (mParent instanceof ViewGroup) {
9661 ((ViewGroup) mParent).invalidateChildFast(this, r);
9662 } else {
9663 mParent.invalidateChild(this, r);
9664 }
9665 }
9666 }
9667 }
9668
9669 /**
9670 * Utility method to transform a given Rect by the current matrix of this view.
9671 */
9672 void transformRect(final Rect rect) {
9673 if (!getMatrix().isIdentity()) {
9674 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9675 boundingRect.set(rect);
9676 getMatrix().mapRect(boundingRect);
9677 rect.set((int) (boundingRect.left - 0.5f),
9678 (int) (boundingRect.top - 0.5f),
9679 (int) (boundingRect.right + 0.5f),
9680 (int) (boundingRect.bottom + 0.5f));
9681 }
9682 }
9683
9684 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009685 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009686 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9687 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009688 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9689 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009690 *
9691 * @hide
9692 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009693 protected void invalidateParentCaches() {
9694 if (mParent instanceof View) {
9695 ((View) mParent).mPrivateFlags |= INVALIDATED;
9696 }
9697 }
Joe Malin32736f02011-01-19 16:14:20 -08009698
Romain Guy0fd89bf2011-01-26 15:41:30 -08009699 /**
9700 * Used to indicate that the parent of this view should be invalidated. This functionality
9701 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9702 * which is necessary when various parent-managed properties of the view change, such as
9703 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9704 * an invalidation event to the parent.
9705 *
9706 * @hide
9707 */
9708 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009709 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009710 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009711 }
9712 }
9713
9714 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009715 * Indicates whether this View is opaque. An opaque View guarantees that it will
9716 * draw all the pixels overlapping its bounds using a fully opaque color.
9717 *
9718 * Subclasses of View should override this method whenever possible to indicate
9719 * whether an instance is opaque. Opaque Views are treated in a special way by
9720 * the View hierarchy, possibly allowing it to perform optimizations during
9721 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009722 *
Romain Guy24443ea2009-05-11 11:56:30 -07009723 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009724 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009725 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009726 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009727 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009728 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9729 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009730 }
9731
Adam Powell20232d02010-12-08 21:08:53 -08009732 /**
9733 * @hide
9734 */
9735 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009736 // Opaque if:
9737 // - Has a background
9738 // - Background is opaque
9739 // - Doesn't have scrollbars or scrollbars are inside overlay
9740
Philip Milne6c8ea062012-04-03 17:38:43 -07009741 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009742 mPrivateFlags |= OPAQUE_BACKGROUND;
9743 } else {
9744 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9745 }
9746
9747 final int flags = mViewFlags;
9748 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9749 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9750 mPrivateFlags |= OPAQUE_SCROLLBARS;
9751 } else {
9752 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9753 }
9754 }
9755
9756 /**
9757 * @hide
9758 */
9759 protected boolean hasOpaqueScrollbars() {
9760 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009761 }
9762
9763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 * @return A handler associated with the thread running the View. This
9765 * handler can be used to pump events in the UI events queue.
9766 */
9767 public Handler getHandler() {
9768 if (mAttachInfo != null) {
9769 return mAttachInfo.mHandler;
9770 }
9771 return null;
9772 }
9773
9774 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009775 * Gets the view root associated with the View.
9776 * @return The view root, or null if none.
9777 * @hide
9778 */
9779 public ViewRootImpl getViewRootImpl() {
9780 if (mAttachInfo != null) {
9781 return mAttachInfo.mViewRootImpl;
9782 }
9783 return null;
9784 }
9785
9786 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009787 * <p>Causes the Runnable to be added to the message queue.
9788 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009789 *
Romain Guye63a4f32011-08-11 11:33:31 -07009790 * <p>This method can be invoked from outside of the UI thread
9791 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 *
9793 * @param action The Runnable that will be executed.
9794 *
9795 * @return Returns true if the Runnable was successfully placed in to the
9796 * message queue. Returns false on failure, usually because the
9797 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009798 *
9799 * @see #postDelayed
9800 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 */
9802 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009803 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009804 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009805 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009807 // Assume that post will succeed later
9808 ViewRootImpl.getRunQueue().post(action);
9809 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 }
9811
9812 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009813 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009814 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009815 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009816 *
Romain Guye63a4f32011-08-11 11:33:31 -07009817 * <p>This method can be invoked from outside of the UI thread
9818 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 *
9820 * @param action The Runnable that will be executed.
9821 * @param delayMillis The delay (in milliseconds) until the Runnable
9822 * will be executed.
9823 *
9824 * @return true if the Runnable was successfully placed in to the
9825 * message queue. Returns false on failure, usually because the
9826 * looper processing the message queue is exiting. Note that a
9827 * result of true does not mean the Runnable will be processed --
9828 * if the looper is quit before the delivery time of the message
9829 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009830 *
9831 * @see #post
9832 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 */
9834 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009835 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009836 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009837 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009839 // Assume that post will succeed later
9840 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9841 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 }
9843
9844 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009845 * <p>Causes the Runnable to execute on the next animation time step.
9846 * The runnable will be run on the user interface thread.</p>
9847 *
9848 * <p>This method can be invoked from outside of the UI thread
9849 * only when this View is attached to a window.</p>
9850 *
9851 * @param action The Runnable that will be executed.
9852 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009853 * @see #postOnAnimationDelayed
9854 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009855 */
9856 public void postOnAnimation(Runnable action) {
9857 final AttachInfo attachInfo = mAttachInfo;
9858 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009859 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9860 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009861 } else {
9862 // Assume that post will succeed later
9863 ViewRootImpl.getRunQueue().post(action);
9864 }
9865 }
9866
9867 /**
9868 * <p>Causes the Runnable to execute on the next animation time step,
9869 * after the specified amount of time elapses.
9870 * The runnable will be run on the user interface thread.</p>
9871 *
9872 * <p>This method can be invoked from outside of the UI thread
9873 * only when this View is attached to a window.</p>
9874 *
9875 * @param action The Runnable that will be executed.
9876 * @param delayMillis The delay (in milliseconds) until the Runnable
9877 * will be executed.
9878 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009879 * @see #postOnAnimation
9880 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009881 */
9882 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9883 final AttachInfo attachInfo = mAttachInfo;
9884 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009885 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9886 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009887 } else {
9888 // Assume that post will succeed later
9889 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9890 }
9891 }
9892
9893 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009894 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009895 *
Romain Guye63a4f32011-08-11 11:33:31 -07009896 * <p>This method can be invoked from outside of the UI thread
9897 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 *
9899 * @param action The Runnable to remove from the message handling queue
9900 *
9901 * @return true if this view could ask the Handler to remove the Runnable,
9902 * false otherwise. When the returned value is true, the Runnable
9903 * may or may not have been actually removed from the message queue
9904 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009905 *
9906 * @see #post
9907 * @see #postDelayed
9908 * @see #postOnAnimation
9909 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 */
9911 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009912 if (action != null) {
9913 final AttachInfo attachInfo = mAttachInfo;
9914 if (attachInfo != null) {
9915 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009916 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9917 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009918 } else {
9919 // Assume that post will succeed later
9920 ViewRootImpl.getRunQueue().removeCallbacks(action);
9921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 return true;
9924 }
9925
9926 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009927 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9928 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 *
Romain Guye63a4f32011-08-11 11:33:31 -07009930 * <p>This method can be invoked from outside of the UI thread
9931 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009932 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009933 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009934 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 */
9936 public void postInvalidate() {
9937 postInvalidateDelayed(0);
9938 }
9939
9940 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009941 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9942 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009943 *
Romain Guye63a4f32011-08-11 11:33:31 -07009944 * <p>This method can be invoked from outside of the UI thread
9945 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 *
9947 * @param left The left coordinate of the rectangle to invalidate.
9948 * @param top The top coordinate of the rectangle to invalidate.
9949 * @param right The right coordinate of the rectangle to invalidate.
9950 * @param bottom The bottom coordinate of the rectangle to invalidate.
9951 *
9952 * @see #invalidate(int, int, int, int)
9953 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009954 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009955 */
9956 public void postInvalidate(int left, int top, int right, int bottom) {
9957 postInvalidateDelayed(0, left, top, right, bottom);
9958 }
9959
9960 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009961 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9962 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009963 *
Romain Guye63a4f32011-08-11 11:33:31 -07009964 * <p>This method can be invoked from outside of the UI thread
9965 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009966 *
9967 * @param delayMilliseconds the duration in milliseconds to delay the
9968 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009969 *
9970 * @see #invalidate()
9971 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 */
9973 public void postInvalidateDelayed(long delayMilliseconds) {
9974 // We try only with the AttachInfo because there's no point in invalidating
9975 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009976 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009977 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009978 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 }
9980 }
9981
9982 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009983 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9984 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009985 *
Romain Guye63a4f32011-08-11 11:33:31 -07009986 * <p>This method can be invoked from outside of the UI thread
9987 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 *
9989 * @param delayMilliseconds the duration in milliseconds to delay the
9990 * invalidation by
9991 * @param left The left coordinate of the rectangle to invalidate.
9992 * @param top The top coordinate of the rectangle to invalidate.
9993 * @param right The right coordinate of the rectangle to invalidate.
9994 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009995 *
9996 * @see #invalidate(int, int, int, int)
9997 * @see #invalidate(Rect)
9998 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 */
10000 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10001 int right, int bottom) {
10002
10003 // We try only with the AttachInfo because there's no point in invalidating
10004 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010005 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010006 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010007 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10008 info.target = this;
10009 info.left = left;
10010 info.top = top;
10011 info.right = right;
10012 info.bottom = bottom;
10013
Jeff Browna175a5b2012-02-15 19:18:31 -080010014 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 }
10016 }
10017
10018 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010019 * <p>Cause an invalidate to happen on the next animation time step, typically the
10020 * next display frame.</p>
10021 *
10022 * <p>This method can be invoked from outside of the UI thread
10023 * only when this View is attached to a window.</p>
10024 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010025 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010026 */
10027 public void postInvalidateOnAnimation() {
10028 // We try only with the AttachInfo because there's no point in invalidating
10029 // if we are not attached to our window
10030 final AttachInfo attachInfo = mAttachInfo;
10031 if (attachInfo != null) {
10032 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10033 }
10034 }
10035
10036 /**
10037 * <p>Cause an invalidate of the specified area to happen on the next animation
10038 * time step, typically the next display frame.</p>
10039 *
10040 * <p>This method can be invoked from outside of the UI thread
10041 * only when this View is attached to a window.</p>
10042 *
10043 * @param left The left coordinate of the rectangle to invalidate.
10044 * @param top The top coordinate of the rectangle to invalidate.
10045 * @param right The right coordinate of the rectangle to invalidate.
10046 * @param bottom The bottom coordinate of the rectangle to invalidate.
10047 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010048 * @see #invalidate(int, int, int, int)
10049 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010050 */
10051 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10052 // We try only with the AttachInfo because there's no point in invalidating
10053 // if we are not attached to our window
10054 final AttachInfo attachInfo = mAttachInfo;
10055 if (attachInfo != null) {
10056 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10057 info.target = this;
10058 info.left = left;
10059 info.top = top;
10060 info.right = right;
10061 info.bottom = bottom;
10062
10063 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10064 }
10065 }
10066
10067 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010068 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10069 * This event is sent at most once every
10070 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10071 */
10072 private void postSendViewScrolledAccessibilityEventCallback() {
10073 if (mSendViewScrolledAccessibilityEvent == null) {
10074 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10075 }
10076 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10077 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10078 postDelayed(mSendViewScrolledAccessibilityEvent,
10079 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10080 }
10081 }
10082
10083 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 * Called by a parent to request that a child update its values for mScrollX
10085 * and mScrollY if necessary. This will typically be done if the child is
10086 * animating a scroll using a {@link android.widget.Scroller Scroller}
10087 * object.
10088 */
10089 public void computeScroll() {
10090 }
10091
10092 /**
10093 * <p>Indicate whether the horizontal edges are faded when the view is
10094 * scrolled horizontally.</p>
10095 *
10096 * @return true if the horizontal edges should are faded on scroll, false
10097 * otherwise
10098 *
10099 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010100 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010101 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 */
10103 public boolean isHorizontalFadingEdgeEnabled() {
10104 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10105 }
10106
10107 /**
10108 * <p>Define whether the horizontal edges should be faded when this view
10109 * is scrolled horizontally.</p>
10110 *
10111 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10112 * be faded when the view is scrolled
10113 * horizontally
10114 *
10115 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010116 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010117 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 */
10119 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10120 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10121 if (horizontalFadingEdgeEnabled) {
10122 initScrollCache();
10123 }
10124
10125 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10126 }
10127 }
10128
10129 /**
10130 * <p>Indicate whether the vertical edges are faded when the view is
10131 * scrolled horizontally.</p>
10132 *
10133 * @return true if the vertical edges should are faded on scroll, false
10134 * otherwise
10135 *
10136 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010137 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010138 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 */
10140 public boolean isVerticalFadingEdgeEnabled() {
10141 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10142 }
10143
10144 /**
10145 * <p>Define whether the vertical edges should be faded when this view
10146 * is scrolled vertically.</p>
10147 *
10148 * @param verticalFadingEdgeEnabled true if the vertical edges should
10149 * be faded when the view is scrolled
10150 * vertically
10151 *
10152 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010153 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010154 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 */
10156 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10157 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10158 if (verticalFadingEdgeEnabled) {
10159 initScrollCache();
10160 }
10161
10162 mViewFlags ^= FADING_EDGE_VERTICAL;
10163 }
10164 }
10165
10166 /**
10167 * Returns the strength, or intensity, of the top faded edge. The strength is
10168 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10169 * returns 0.0 or 1.0 but no value in between.
10170 *
10171 * Subclasses should override this method to provide a smoother fade transition
10172 * when scrolling occurs.
10173 *
10174 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10175 */
10176 protected float getTopFadingEdgeStrength() {
10177 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10178 }
10179
10180 /**
10181 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10182 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10183 * returns 0.0 or 1.0 but no value in between.
10184 *
10185 * Subclasses should override this method to provide a smoother fade transition
10186 * when scrolling occurs.
10187 *
10188 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10189 */
10190 protected float getBottomFadingEdgeStrength() {
10191 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10192 computeVerticalScrollRange() ? 1.0f : 0.0f;
10193 }
10194
10195 /**
10196 * Returns the strength, or intensity, of the left faded edge. The strength is
10197 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10198 * returns 0.0 or 1.0 but no value in between.
10199 *
10200 * Subclasses should override this method to provide a smoother fade transition
10201 * when scrolling occurs.
10202 *
10203 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10204 */
10205 protected float getLeftFadingEdgeStrength() {
10206 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10207 }
10208
10209 /**
10210 * Returns the strength, or intensity, of the right faded edge. The strength is
10211 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10212 * returns 0.0 or 1.0 but no value in between.
10213 *
10214 * Subclasses should override this method to provide a smoother fade transition
10215 * when scrolling occurs.
10216 *
10217 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10218 */
10219 protected float getRightFadingEdgeStrength() {
10220 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10221 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10222 }
10223
10224 /**
10225 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10226 * scrollbar is not drawn by default.</p>
10227 *
10228 * @return true if the horizontal scrollbar should be painted, false
10229 * otherwise
10230 *
10231 * @see #setHorizontalScrollBarEnabled(boolean)
10232 */
10233 public boolean isHorizontalScrollBarEnabled() {
10234 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10235 }
10236
10237 /**
10238 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10239 * scrollbar is not drawn by default.</p>
10240 *
10241 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10242 * be painted
10243 *
10244 * @see #isHorizontalScrollBarEnabled()
10245 */
10246 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10247 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10248 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010249 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010250 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010251 }
10252 }
10253
10254 /**
10255 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10256 * scrollbar is not drawn by default.</p>
10257 *
10258 * @return true if the vertical scrollbar should be painted, false
10259 * otherwise
10260 *
10261 * @see #setVerticalScrollBarEnabled(boolean)
10262 */
10263 public boolean isVerticalScrollBarEnabled() {
10264 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10265 }
10266
10267 /**
10268 * <p>Define whether the vertical scrollbar should be drawn or not. The
10269 * scrollbar is not drawn by default.</p>
10270 *
10271 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10272 * be painted
10273 *
10274 * @see #isVerticalScrollBarEnabled()
10275 */
10276 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10277 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10278 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010279 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010280 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 }
10282 }
10283
Adam Powell20232d02010-12-08 21:08:53 -080010284 /**
10285 * @hide
10286 */
10287 protected void recomputePadding() {
10288 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010289 }
Joe Malin32736f02011-01-19 16:14:20 -080010290
Mike Cleronfe81d382009-09-28 14:22:16 -070010291 /**
10292 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010293 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010294 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010295 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010296 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010297 */
10298 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10299 initScrollCache();
10300 final ScrollabilityCache scrollabilityCache = mScrollCache;
10301 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010302 if (fadeScrollbars) {
10303 scrollabilityCache.state = ScrollabilityCache.OFF;
10304 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010305 scrollabilityCache.state = ScrollabilityCache.ON;
10306 }
10307 }
Joe Malin32736f02011-01-19 16:14:20 -080010308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 /**
Joe Malin32736f02011-01-19 16:14:20 -080010310 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010311 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010312 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010313 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010314 *
10315 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010316 */
10317 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010318 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010319 }
Joe Malin32736f02011-01-19 16:14:20 -080010320
Mike Cleron52f0a642009-09-28 18:21:37 -070010321 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010322 *
10323 * Returns the delay before scrollbars fade.
10324 *
10325 * @return the delay before scrollbars fade
10326 *
10327 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10328 */
10329 public int getScrollBarDefaultDelayBeforeFade() {
10330 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10331 mScrollCache.scrollBarDefaultDelayBeforeFade;
10332 }
10333
10334 /**
10335 * Define the delay before scrollbars fade.
10336 *
10337 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10338 *
10339 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10340 */
10341 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10342 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10343 }
10344
10345 /**
10346 *
10347 * Returns the scrollbar fade duration.
10348 *
10349 * @return the scrollbar fade duration
10350 *
10351 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10352 */
10353 public int getScrollBarFadeDuration() {
10354 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10355 mScrollCache.scrollBarFadeDuration;
10356 }
10357
10358 /**
10359 * Define the scrollbar fade duration.
10360 *
10361 * @param scrollBarFadeDuration - the scrollbar fade duration
10362 *
10363 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10364 */
10365 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10366 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10367 }
10368
10369 /**
10370 *
10371 * Returns the scrollbar size.
10372 *
10373 * @return the scrollbar size
10374 *
10375 * @attr ref android.R.styleable#View_scrollbarSize
10376 */
10377 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010378 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010379 mScrollCache.scrollBarSize;
10380 }
10381
10382 /**
10383 * Define the scrollbar size.
10384 *
10385 * @param scrollBarSize - the scrollbar size
10386 *
10387 * @attr ref android.R.styleable#View_scrollbarSize
10388 */
10389 public void setScrollBarSize(int scrollBarSize) {
10390 getScrollCache().scrollBarSize = scrollBarSize;
10391 }
10392
10393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10395 * inset. When inset, they add to the padding of the view. And the scrollbars
10396 * can be drawn inside the padding area or on the edge of the view. For example,
10397 * if a view has a background drawable and you want to draw the scrollbars
10398 * inside the padding specified by the drawable, you can use
10399 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10400 * appear at the edge of the view, ignoring the padding, then you can use
10401 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10402 * @param style the style of the scrollbars. Should be one of
10403 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10404 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10405 * @see #SCROLLBARS_INSIDE_OVERLAY
10406 * @see #SCROLLBARS_INSIDE_INSET
10407 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10408 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010409 *
10410 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 */
10412 public void setScrollBarStyle(int style) {
10413 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10414 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010415 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010416 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 }
10418 }
10419
10420 /**
10421 * <p>Returns the current scrollbar style.</p>
10422 * @return the current scrollbar style
10423 * @see #SCROLLBARS_INSIDE_OVERLAY
10424 * @see #SCROLLBARS_INSIDE_INSET
10425 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10426 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010427 *
10428 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010430 @ViewDebug.ExportedProperty(mapping = {
10431 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10432 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10433 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10434 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10435 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 public int getScrollBarStyle() {
10437 return mViewFlags & SCROLLBARS_STYLE_MASK;
10438 }
10439
10440 /**
10441 * <p>Compute the horizontal range that the horizontal scrollbar
10442 * represents.</p>
10443 *
10444 * <p>The range is expressed in arbitrary units that must be the same as the
10445 * units used by {@link #computeHorizontalScrollExtent()} and
10446 * {@link #computeHorizontalScrollOffset()}.</p>
10447 *
10448 * <p>The default range is the drawing width of this view.</p>
10449 *
10450 * @return the total horizontal range represented by the horizontal
10451 * scrollbar
10452 *
10453 * @see #computeHorizontalScrollExtent()
10454 * @see #computeHorizontalScrollOffset()
10455 * @see android.widget.ScrollBarDrawable
10456 */
10457 protected int computeHorizontalScrollRange() {
10458 return getWidth();
10459 }
10460
10461 /**
10462 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10463 * within the horizontal range. This value is used to compute the position
10464 * of the thumb within the scrollbar's track.</p>
10465 *
10466 * <p>The range is expressed in arbitrary units that must be the same as the
10467 * units used by {@link #computeHorizontalScrollRange()} and
10468 * {@link #computeHorizontalScrollExtent()}.</p>
10469 *
10470 * <p>The default offset is the scroll offset of this view.</p>
10471 *
10472 * @return the horizontal offset of the scrollbar's thumb
10473 *
10474 * @see #computeHorizontalScrollRange()
10475 * @see #computeHorizontalScrollExtent()
10476 * @see android.widget.ScrollBarDrawable
10477 */
10478 protected int computeHorizontalScrollOffset() {
10479 return mScrollX;
10480 }
10481
10482 /**
10483 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10484 * within the horizontal range. This value is used to compute the length
10485 * of the thumb within the scrollbar's track.</p>
10486 *
10487 * <p>The range is expressed in arbitrary units that must be the same as the
10488 * units used by {@link #computeHorizontalScrollRange()} and
10489 * {@link #computeHorizontalScrollOffset()}.</p>
10490 *
10491 * <p>The default extent is the drawing width of this view.</p>
10492 *
10493 * @return the horizontal extent of the scrollbar's thumb
10494 *
10495 * @see #computeHorizontalScrollRange()
10496 * @see #computeHorizontalScrollOffset()
10497 * @see android.widget.ScrollBarDrawable
10498 */
10499 protected int computeHorizontalScrollExtent() {
10500 return getWidth();
10501 }
10502
10503 /**
10504 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10505 *
10506 * <p>The range is expressed in arbitrary units that must be the same as the
10507 * units used by {@link #computeVerticalScrollExtent()} and
10508 * {@link #computeVerticalScrollOffset()}.</p>
10509 *
10510 * @return the total vertical range represented by the vertical scrollbar
10511 *
10512 * <p>The default range is the drawing height of this view.</p>
10513 *
10514 * @see #computeVerticalScrollExtent()
10515 * @see #computeVerticalScrollOffset()
10516 * @see android.widget.ScrollBarDrawable
10517 */
10518 protected int computeVerticalScrollRange() {
10519 return getHeight();
10520 }
10521
10522 /**
10523 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10524 * within the horizontal range. This value is used to compute the position
10525 * of the thumb within the scrollbar's track.</p>
10526 *
10527 * <p>The range is expressed in arbitrary units that must be the same as the
10528 * units used by {@link #computeVerticalScrollRange()} and
10529 * {@link #computeVerticalScrollExtent()}.</p>
10530 *
10531 * <p>The default offset is the scroll offset of this view.</p>
10532 *
10533 * @return the vertical offset of the scrollbar's thumb
10534 *
10535 * @see #computeVerticalScrollRange()
10536 * @see #computeVerticalScrollExtent()
10537 * @see android.widget.ScrollBarDrawable
10538 */
10539 protected int computeVerticalScrollOffset() {
10540 return mScrollY;
10541 }
10542
10543 /**
10544 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10545 * within the vertical range. This value is used to compute the length
10546 * of the thumb within the scrollbar's track.</p>
10547 *
10548 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010549 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 * {@link #computeVerticalScrollOffset()}.</p>
10551 *
10552 * <p>The default extent is the drawing height of this view.</p>
10553 *
10554 * @return the vertical extent of the scrollbar's thumb
10555 *
10556 * @see #computeVerticalScrollRange()
10557 * @see #computeVerticalScrollOffset()
10558 * @see android.widget.ScrollBarDrawable
10559 */
10560 protected int computeVerticalScrollExtent() {
10561 return getHeight();
10562 }
10563
10564 /**
Adam Powell69159442011-06-13 17:53:06 -070010565 * Check if this view can be scrolled horizontally in a certain direction.
10566 *
10567 * @param direction Negative to check scrolling left, positive to check scrolling right.
10568 * @return true if this view can be scrolled in the specified direction, false otherwise.
10569 */
10570 public boolean canScrollHorizontally(int direction) {
10571 final int offset = computeHorizontalScrollOffset();
10572 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10573 if (range == 0) return false;
10574 if (direction < 0) {
10575 return offset > 0;
10576 } else {
10577 return offset < range - 1;
10578 }
10579 }
10580
10581 /**
10582 * Check if this view can be scrolled vertically in a certain direction.
10583 *
10584 * @param direction Negative to check scrolling up, positive to check scrolling down.
10585 * @return true if this view can be scrolled in the specified direction, false otherwise.
10586 */
10587 public boolean canScrollVertically(int direction) {
10588 final int offset = computeVerticalScrollOffset();
10589 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10590 if (range == 0) return false;
10591 if (direction < 0) {
10592 return offset > 0;
10593 } else {
10594 return offset < range - 1;
10595 }
10596 }
10597
10598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10600 * scrollbars are painted only if they have been awakened first.</p>
10601 *
10602 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010603 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010604 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010606 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 // scrollbars are drawn only when the animation is running
10608 final ScrollabilityCache cache = mScrollCache;
10609 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010610
Mike Cleronf116bf82009-09-27 19:14:12 -070010611 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010612
Mike Cleronf116bf82009-09-27 19:14:12 -070010613 if (state == ScrollabilityCache.OFF) {
10614 return;
10615 }
Joe Malin32736f02011-01-19 16:14:20 -080010616
Mike Cleronf116bf82009-09-27 19:14:12 -070010617 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010618
Mike Cleronf116bf82009-09-27 19:14:12 -070010619 if (state == ScrollabilityCache.FADING) {
10620 // We're fading -- get our fade interpolation
10621 if (cache.interpolatorValues == null) {
10622 cache.interpolatorValues = new float[1];
10623 }
Joe Malin32736f02011-01-19 16:14:20 -080010624
Mike Cleronf116bf82009-09-27 19:14:12 -070010625 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010626
Mike Cleronf116bf82009-09-27 19:14:12 -070010627 // Stops the animation if we're done
10628 if (cache.scrollBarInterpolator.timeToValues(values) ==
10629 Interpolator.Result.FREEZE_END) {
10630 cache.state = ScrollabilityCache.OFF;
10631 } else {
10632 cache.scrollBar.setAlpha(Math.round(values[0]));
10633 }
Joe Malin32736f02011-01-19 16:14:20 -080010634
10635 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010636 // drawing. We only want this when we're fading so that
10637 // we prevent excessive redraws
10638 invalidate = true;
10639 } else {
10640 // We're just on -- but we may have been fading before so
10641 // reset alpha
10642 cache.scrollBar.setAlpha(255);
10643 }
10644
Joe Malin32736f02011-01-19 16:14:20 -080010645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 final int viewFlags = mViewFlags;
10647
10648 final boolean drawHorizontalScrollBar =
10649 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10650 final boolean drawVerticalScrollBar =
10651 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10652 && !isVerticalScrollBarHidden();
10653
10654 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10655 final int width = mRight - mLeft;
10656 final int height = mBottom - mTop;
10657
10658 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659
Mike Reede8853fc2009-09-04 14:01:48 -040010660 final int scrollX = mScrollX;
10661 final int scrollY = mScrollY;
10662 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10663
Mike Cleronf116bf82009-09-27 19:14:12 -070010664 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010667 int size = scrollBar.getSize(false);
10668 if (size <= 0) {
10669 size = cache.scrollBarSize;
10670 }
10671
Mike Cleronf116bf82009-09-27 19:14:12 -070010672 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010673 computeHorizontalScrollOffset(),
10674 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010675 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010676 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010677 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010678 left = scrollX + (mPaddingLeft & inside);
10679 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10680 bottom = top + size;
10681 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10682 if (invalidate) {
10683 invalidate(left, top, right, bottom);
10684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010685 }
10686
10687 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010688 int size = scrollBar.getSize(true);
10689 if (size <= 0) {
10690 size = cache.scrollBarSize;
10691 }
10692
Mike Reede8853fc2009-09-04 14:01:48 -040010693 scrollBar.setParameters(computeVerticalScrollRange(),
10694 computeVerticalScrollOffset(),
10695 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010696 switch (mVerticalScrollbarPosition) {
10697 default:
10698 case SCROLLBAR_POSITION_DEFAULT:
10699 case SCROLLBAR_POSITION_RIGHT:
10700 left = scrollX + width - size - (mUserPaddingRight & inside);
10701 break;
10702 case SCROLLBAR_POSITION_LEFT:
10703 left = scrollX + (mUserPaddingLeft & inside);
10704 break;
10705 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010706 top = scrollY + (mPaddingTop & inside);
10707 right = left + size;
10708 bottom = scrollY + height - (mUserPaddingBottom & inside);
10709 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10710 if (invalidate) {
10711 invalidate(left, top, right, bottom);
10712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 }
10714 }
10715 }
10716 }
Romain Guy8506ab42009-06-11 17:35:47 -070010717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010718 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010719 * 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 -080010720 * FastScroller is visible.
10721 * @return whether to temporarily hide the vertical scrollbar
10722 * @hide
10723 */
10724 protected boolean isVerticalScrollBarHidden() {
10725 return false;
10726 }
10727
10728 /**
10729 * <p>Draw the horizontal scrollbar if
10730 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 * @param canvas the canvas on which to draw the scrollbar
10733 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010734 *
10735 * @see #isHorizontalScrollBarEnabled()
10736 * @see #computeHorizontalScrollRange()
10737 * @see #computeHorizontalScrollExtent()
10738 * @see #computeHorizontalScrollOffset()
10739 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010740 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010741 */
Romain Guy8fb95422010-08-17 18:38:51 -070010742 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10743 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010744 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010745 scrollBar.draw(canvas);
10746 }
Mike Reede8853fc2009-09-04 14:01:48 -040010747
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10750 * returns true.</p>
10751 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010752 * @param canvas the canvas on which to draw the scrollbar
10753 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010754 *
10755 * @see #isVerticalScrollBarEnabled()
10756 * @see #computeVerticalScrollRange()
10757 * @see #computeVerticalScrollExtent()
10758 * @see #computeVerticalScrollOffset()
10759 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010760 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 */
Romain Guy8fb95422010-08-17 18:38:51 -070010762 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10763 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010764 scrollBar.setBounds(l, t, r, b);
10765 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 }
10767
10768 /**
10769 * Implement this to do your drawing.
10770 *
10771 * @param canvas the canvas on which the background will be drawn
10772 */
10773 protected void onDraw(Canvas canvas) {
10774 }
10775
10776 /*
10777 * Caller is responsible for calling requestLayout if necessary.
10778 * (This allows addViewInLayout to not request a new layout.)
10779 */
10780 void assignParent(ViewParent parent) {
10781 if (mParent == null) {
10782 mParent = parent;
10783 } else if (parent == null) {
10784 mParent = null;
10785 } else {
10786 throw new RuntimeException("view " + this + " being added, but"
10787 + " it already has a parent");
10788 }
10789 }
10790
10791 /**
10792 * This is called when the view is attached to a window. At this point it
10793 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010794 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10795 * however it may be called any time before the first onDraw -- including
10796 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010797 *
10798 * @see #onDetachedFromWindow()
10799 */
10800 protected void onAttachedToWindow() {
10801 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10802 mParent.requestTransparentRegion(this);
10803 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010804 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10805 initialAwakenScrollBars();
10806 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10807 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010808 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010809 // Order is important here: LayoutDirection MUST be resolved before Padding
10810 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010811 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010812 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010813 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010814 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010815 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010816 if (isFocused()) {
10817 InputMethodManager imm = InputMethodManager.peekInstance();
10818 imm.focusIn(this);
10819 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010820 }
Cibu Johny86666632010-02-22 13:01:02 -080010821
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010822 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010823 * @see #onScreenStateChanged(int)
10824 */
10825 void dispatchScreenStateChanged(int screenState) {
10826 onScreenStateChanged(screenState);
10827 }
10828
10829 /**
10830 * This method is called whenever the state of the screen this view is
10831 * attached to changes. A state change will usually occurs when the screen
10832 * turns on or off (whether it happens automatically or the user does it
10833 * manually.)
10834 *
10835 * @param screenState The new state of the screen. Can be either
10836 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10837 */
10838 public void onScreenStateChanged(int screenState) {
10839 }
10840
10841 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010842 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10843 */
10844 private boolean hasRtlSupport() {
10845 return mContext.getApplicationInfo().hasRtlSupport();
10846 }
10847
10848 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010849 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10850 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010851 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010852 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010853 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010854 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010855 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010856
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010857 if (hasRtlSupport()) {
10858 // Set resolved depending on layout direction
10859 switch (getLayoutDirection()) {
10860 case LAYOUT_DIRECTION_INHERIT:
10861 // If this is root view, no need to look at parent's layout dir.
10862 if (canResolveLayoutDirection()) {
10863 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010864
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010865 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10866 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10867 }
10868 } else {
10869 // Nothing to do, LTR by default
10870 }
10871 break;
10872 case LAYOUT_DIRECTION_RTL:
10873 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10874 break;
10875 case LAYOUT_DIRECTION_LOCALE:
10876 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010877 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10878 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010879 break;
10880 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010881 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010882 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010883 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010884
10885 // Set to resolved
10886 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010887 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010888 // Resolve padding
10889 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010890 }
10891
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010892 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010893 * Called when layout direction has been resolved.
10894 *
10895 * The default implementation does nothing.
10896 */
10897 public void onResolvedLayoutDirectionChanged() {
10898 }
10899
10900 /**
10901 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010902 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010903 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010904 // If the user specified the absolute padding (either with android:padding or
10905 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10906 // use the default padding or the padding from the background drawable
10907 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010908 int resolvedLayoutDirection = getResolvedLayoutDirection();
10909 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010910 case LAYOUT_DIRECTION_RTL:
10911 // Start user padding override Right user padding. Otherwise, if Right user
10912 // padding is not defined, use the default Right padding. If Right user padding
10913 // is defined, just use it.
10914 if (mUserPaddingStart >= 0) {
10915 mUserPaddingRight = mUserPaddingStart;
10916 } else if (mUserPaddingRight < 0) {
10917 mUserPaddingRight = mPaddingRight;
10918 }
10919 if (mUserPaddingEnd >= 0) {
10920 mUserPaddingLeft = mUserPaddingEnd;
10921 } else if (mUserPaddingLeft < 0) {
10922 mUserPaddingLeft = mPaddingLeft;
10923 }
10924 break;
10925 case LAYOUT_DIRECTION_LTR:
10926 default:
10927 // Start user padding override Left user padding. Otherwise, if Left user
10928 // padding is not defined, use the default left padding. If Left user padding
10929 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010930 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010931 mUserPaddingLeft = mUserPaddingStart;
10932 } else if (mUserPaddingLeft < 0) {
10933 mUserPaddingLeft = mPaddingLeft;
10934 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010935 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010936 mUserPaddingRight = mUserPaddingEnd;
10937 } else if (mUserPaddingRight < 0) {
10938 mUserPaddingRight = mPaddingRight;
10939 }
10940 }
10941
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010942 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10943
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010944 if(isPaddingRelative()) {
10945 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10946 } else {
10947 recomputePadding();
10948 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010949 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010950 }
10951
10952 /**
10953 * Resolve padding depending on the layout direction. Subclasses that care about
10954 * padding resolution should override this method. The default implementation does
10955 * nothing.
10956 *
10957 * @param layoutDirection the direction of the layout
10958 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010959 * @see {@link #LAYOUT_DIRECTION_LTR}
10960 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010961 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010962 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010963 }
10964
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010965 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010966 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010967 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010968 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010969 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010970 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010971 switch (getLayoutDirection()) {
10972 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010973 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010974 default:
10975 return true;
10976 }
10977 }
10978
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010979 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010980 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10981 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010982 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010983 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010984 // Reset the current resolved bits
10985 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010986 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010987 // Reset also the text direction
10988 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010989 }
10990
10991 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010992 * Called during reset of resolved layout direction.
10993 *
10994 * Subclasses need to override this method to clear cached information that depends on the
10995 * resolved layout direction, or to inform child views that inherit their layout direction.
10996 *
10997 * The default implementation does nothing.
10998 */
10999 public void onResolvedLayoutDirectionReset() {
11000 }
11001
11002 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011003 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011004 *
11005 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011006 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011007 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011008 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011009 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 }
11011
11012 /**
11013 * This is called when the view is detached from a window. At this point it
11014 * no longer has a surface for drawing.
11015 *
11016 * @see #onAttachedToWindow()
11017 */
11018 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011019 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011020
Romain Guya440b002010-02-24 15:57:54 -080011021 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011022 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011023 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011024 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011027
Romain Guya998dff2012-03-23 18:58:36 -070011028 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011029
11030 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011031 if (mDisplayList != null) {
11032 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11033 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011034 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011035 } else {
11036 if (mDisplayList != null) {
11037 // Should never happen
11038 mDisplayList.invalidate();
11039 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011040 }
11041
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011042 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011043
11044 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011045 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011046 resetAccessibilityStateChanged();
11047 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 }
11049
11050 /**
11051 * @return The number of times this view has been attached to a window
11052 */
11053 protected int getWindowAttachCount() {
11054 return mWindowAttachCount;
11055 }
11056
11057 /**
11058 * Retrieve a unique token identifying the window this view is attached to.
11059 * @return Return the window's token for use in
11060 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11061 */
11062 public IBinder getWindowToken() {
11063 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11064 }
11065
11066 /**
11067 * Retrieve a unique token identifying the top-level "real" window of
11068 * the window that this view is attached to. That is, this is like
11069 * {@link #getWindowToken}, except if the window this view in is a panel
11070 * window (attached to another containing window), then the token of
11071 * the containing window is returned instead.
11072 *
11073 * @return Returns the associated window token, either
11074 * {@link #getWindowToken()} or the containing window's token.
11075 */
11076 public IBinder getApplicationWindowToken() {
11077 AttachInfo ai = mAttachInfo;
11078 if (ai != null) {
11079 IBinder appWindowToken = ai.mPanelParentWindowToken;
11080 if (appWindowToken == null) {
11081 appWindowToken = ai.mWindowToken;
11082 }
11083 return appWindowToken;
11084 }
11085 return null;
11086 }
11087
11088 /**
11089 * Retrieve private session object this view hierarchy is using to
11090 * communicate with the window manager.
11091 * @return the session object to communicate with the window manager
11092 */
11093 /*package*/ IWindowSession getWindowSession() {
11094 return mAttachInfo != null ? mAttachInfo.mSession : null;
11095 }
11096
11097 /**
11098 * @param info the {@link android.view.View.AttachInfo} to associated with
11099 * this view
11100 */
11101 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11102 //System.out.println("Attached! " + this);
11103 mAttachInfo = info;
11104 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011105 // We will need to evaluate the drawable state at least once.
11106 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011107 if (mFloatingTreeObserver != null) {
11108 info.mTreeObserver.merge(mFloatingTreeObserver);
11109 mFloatingTreeObserver = null;
11110 }
11111 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11112 mAttachInfo.mScrollContainers.add(this);
11113 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11114 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011115 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011117
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011118 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011119 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011120 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011121 if (listeners != null && listeners.size() > 0) {
11122 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11123 // perform the dispatching. The iterator is a safe guard against listeners that
11124 // could mutate the list by calling the various add/remove methods. This prevents
11125 // the array from being modified while we iterate it.
11126 for (OnAttachStateChangeListener listener : listeners) {
11127 listener.onViewAttachedToWindow(this);
11128 }
11129 }
11130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 int vis = info.mWindowVisibility;
11132 if (vis != GONE) {
11133 onWindowVisibilityChanged(vis);
11134 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011135 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11136 // If nobody has evaluated the drawable state yet, then do it now.
11137 refreshDrawableState();
11138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 }
11140
11141 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011142 AttachInfo info = mAttachInfo;
11143 if (info != null) {
11144 int vis = info.mWindowVisibility;
11145 if (vis != GONE) {
11146 onWindowVisibilityChanged(GONE);
11147 }
11148 }
11149
11150 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011151
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011152 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011153 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011154 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011155 if (listeners != null && listeners.size() > 0) {
11156 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11157 // perform the dispatching. The iterator is a safe guard against listeners that
11158 // could mutate the list by calling the various add/remove methods. This prevents
11159 // the array from being modified while we iterate it.
11160 for (OnAttachStateChangeListener listener : listeners) {
11161 listener.onViewDetachedFromWindow(this);
11162 }
11163 }
11164
Romain Guy01d5edc2011-01-28 11:28:53 -080011165 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011166 mAttachInfo.mScrollContainers.remove(this);
11167 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11168 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 mAttachInfo = null;
11171 }
11172
11173 /**
11174 * Store this view hierarchy's frozen state into the given container.
11175 *
11176 * @param container The SparseArray in which to save the view's state.
11177 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011178 * @see #restoreHierarchyState(android.util.SparseArray)
11179 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11180 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 */
11182 public void saveHierarchyState(SparseArray<Parcelable> container) {
11183 dispatchSaveInstanceState(container);
11184 }
11185
11186 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011187 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11188 * this view and its children. May be overridden to modify how freezing happens to a
11189 * 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 -080011190 *
11191 * @param container The SparseArray in which to save the view's state.
11192 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011193 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11194 * @see #saveHierarchyState(android.util.SparseArray)
11195 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011196 */
11197 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11198 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11199 mPrivateFlags &= ~SAVE_STATE_CALLED;
11200 Parcelable state = onSaveInstanceState();
11201 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11202 throw new IllegalStateException(
11203 "Derived class did not call super.onSaveInstanceState()");
11204 }
11205 if (state != null) {
11206 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11207 // + ": " + state);
11208 container.put(mID, state);
11209 }
11210 }
11211 }
11212
11213 /**
11214 * Hook allowing a view to generate a representation of its internal state
11215 * that can later be used to create a new instance with that same state.
11216 * This state should only contain information that is not persistent or can
11217 * not be reconstructed later. For example, you will never store your
11218 * current position on screen because that will be computed again when a
11219 * new instance of the view is placed in its view hierarchy.
11220 * <p>
11221 * Some examples of things you may store here: the current cursor position
11222 * in a text view (but usually not the text itself since that is stored in a
11223 * content provider or other persistent storage), the currently selected
11224 * item in a list view.
11225 *
11226 * @return Returns a Parcelable object containing the view's current dynamic
11227 * state, or null if there is nothing interesting to save. The
11228 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011229 * @see #onRestoreInstanceState(android.os.Parcelable)
11230 * @see #saveHierarchyState(android.util.SparseArray)
11231 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 * @see #setSaveEnabled(boolean)
11233 */
11234 protected Parcelable onSaveInstanceState() {
11235 mPrivateFlags |= SAVE_STATE_CALLED;
11236 return BaseSavedState.EMPTY_STATE;
11237 }
11238
11239 /**
11240 * Restore this view hierarchy's frozen state from the given container.
11241 *
11242 * @param container The SparseArray which holds previously frozen states.
11243 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011244 * @see #saveHierarchyState(android.util.SparseArray)
11245 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11246 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011247 */
11248 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11249 dispatchRestoreInstanceState(container);
11250 }
11251
11252 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011253 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11254 * state for this view and its children. May be overridden to modify how restoring
11255 * happens to a view's children; for example, some views may want to not store state
11256 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 *
11258 * @param container The SparseArray which holds previously saved state.
11259 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011260 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11261 * @see #restoreHierarchyState(android.util.SparseArray)
11262 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011263 */
11264 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11265 if (mID != NO_ID) {
11266 Parcelable state = container.get(mID);
11267 if (state != null) {
11268 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11269 // + ": " + state);
11270 mPrivateFlags &= ~SAVE_STATE_CALLED;
11271 onRestoreInstanceState(state);
11272 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11273 throw new IllegalStateException(
11274 "Derived class did not call super.onRestoreInstanceState()");
11275 }
11276 }
11277 }
11278 }
11279
11280 /**
11281 * Hook allowing a view to re-apply a representation of its internal state that had previously
11282 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11283 * null state.
11284 *
11285 * @param state The frozen state that had previously been returned by
11286 * {@link #onSaveInstanceState}.
11287 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011288 * @see #onSaveInstanceState()
11289 * @see #restoreHierarchyState(android.util.SparseArray)
11290 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011291 */
11292 protected void onRestoreInstanceState(Parcelable state) {
11293 mPrivateFlags |= SAVE_STATE_CALLED;
11294 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011295 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11296 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011297 + "when two views of different type have the same id in the same hierarchy. "
11298 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011299 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011300 }
11301 }
11302
11303 /**
11304 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11305 *
11306 * @return the drawing start time in milliseconds
11307 */
11308 public long getDrawingTime() {
11309 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11310 }
11311
11312 /**
11313 * <p>Enables or disables the duplication of the parent's state into this view. When
11314 * duplication is enabled, this view gets its drawable state from its parent rather
11315 * than from its own internal properties.</p>
11316 *
11317 * <p>Note: in the current implementation, setting this property to true after the
11318 * view was added to a ViewGroup might have no effect at all. This property should
11319 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11320 *
11321 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11322 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011323 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011324 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11325 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011326 *
11327 * @param enabled True to enable duplication of the parent's drawable state, false
11328 * to disable it.
11329 *
11330 * @see #getDrawableState()
11331 * @see #isDuplicateParentStateEnabled()
11332 */
11333 public void setDuplicateParentStateEnabled(boolean enabled) {
11334 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11335 }
11336
11337 /**
11338 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11339 *
11340 * @return True if this view's drawable state is duplicated from the parent,
11341 * false otherwise
11342 *
11343 * @see #getDrawableState()
11344 * @see #setDuplicateParentStateEnabled(boolean)
11345 */
11346 public boolean isDuplicateParentStateEnabled() {
11347 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11348 }
11349
11350 /**
Romain Guy171c5922011-01-06 10:04:23 -080011351 * <p>Specifies the type of layer backing this view. The layer can be
11352 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11353 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011354 *
Romain Guy171c5922011-01-06 10:04:23 -080011355 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11356 * instance that controls how the layer is composed on screen. The following
11357 * properties of the paint are taken into account when composing the layer:</p>
11358 * <ul>
11359 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11360 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11361 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11362 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011363 *
Romain Guy171c5922011-01-06 10:04:23 -080011364 * <p>If this view has an alpha value set to < 1.0 by calling
11365 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11366 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11367 * equivalent to setting a hardware layer on this view and providing a paint with
11368 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011369 *
Romain Guy171c5922011-01-06 10:04:23 -080011370 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11371 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11372 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011373 *
Romain Guy171c5922011-01-06 10:04:23 -080011374 * @param layerType The ype of layer to use with this view, must be one of
11375 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11376 * {@link #LAYER_TYPE_HARDWARE}
11377 * @param paint The paint used to compose the layer. This argument is optional
11378 * and can be null. It is ignored when the layer type is
11379 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011380 *
11381 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011382 * @see #LAYER_TYPE_NONE
11383 * @see #LAYER_TYPE_SOFTWARE
11384 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011385 * @see #setAlpha(float)
11386 *
Romain Guy171c5922011-01-06 10:04:23 -080011387 * @attr ref android.R.styleable#View_layerType
11388 */
11389 public void setLayerType(int layerType, Paint paint) {
11390 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011391 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011392 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11393 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011394
Romain Guyd6cd5722011-01-17 14:42:41 -080011395 if (layerType == mLayerType) {
11396 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11397 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011398 invalidateParentCaches();
11399 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011400 }
11401 return;
11402 }
Romain Guy171c5922011-01-06 10:04:23 -080011403
11404 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011405 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011406 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011407 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011408 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011409 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011410 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011411 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011412 default:
11413 break;
Romain Guy171c5922011-01-06 10:04:23 -080011414 }
11415
11416 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011417 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11418 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11419 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011420
Romain Guy0fd89bf2011-01-26 15:41:30 -080011421 invalidateParentCaches();
11422 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011423 }
11424
11425 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011426 * Indicates whether this view has a static layer. A view with layer type
11427 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11428 * dynamic.
11429 */
11430 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011431 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011432 }
11433
11434 /**
Romain Guy171c5922011-01-06 10:04:23 -080011435 * Indicates what type of layer is currently associated with this view. By default
11436 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11437 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11438 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011439 *
Romain Guy171c5922011-01-06 10:04:23 -080011440 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11441 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011442 *
11443 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011444 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011445 * @see #LAYER_TYPE_NONE
11446 * @see #LAYER_TYPE_SOFTWARE
11447 * @see #LAYER_TYPE_HARDWARE
11448 */
11449 public int getLayerType() {
11450 return mLayerType;
11451 }
Joe Malin32736f02011-01-19 16:14:20 -080011452
Romain Guy6c319ca2011-01-11 14:29:25 -080011453 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011454 * Forces this view's layer to be created and this view to be rendered
11455 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11456 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011457 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011458 * This method can for instance be used to render a view into its layer before
11459 * starting an animation. If this view is complex, rendering into the layer
11460 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011461 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011462 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011463 *
11464 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011465 */
11466 public void buildLayer() {
11467 if (mLayerType == LAYER_TYPE_NONE) return;
11468
11469 if (mAttachInfo == null) {
11470 throw new IllegalStateException("This view must be attached to a window first");
11471 }
11472
11473 switch (mLayerType) {
11474 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011475 if (mAttachInfo.mHardwareRenderer != null &&
11476 mAttachInfo.mHardwareRenderer.isEnabled() &&
11477 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011478 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011479 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011480 break;
11481 case LAYER_TYPE_SOFTWARE:
11482 buildDrawingCache(true);
11483 break;
11484 }
11485 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011486
Romain Guy9c4b79a2011-11-10 19:23:58 -080011487 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11488 void flushLayer() {
11489 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11490 mHardwareLayer.flush();
11491 }
11492 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011493
11494 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011495 * <p>Returns a hardware layer that can be used to draw this view again
11496 * without executing its draw method.</p>
11497 *
11498 * @return A HardwareLayer ready to render, or null if an error occurred.
11499 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011500 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011501 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11502 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011503 return null;
11504 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011505
Romain Guy9c4b79a2011-11-10 19:23:58 -080011506 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011507
11508 final int width = mRight - mLeft;
11509 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011510
Romain Guy6c319ca2011-01-11 14:29:25 -080011511 if (width == 0 || height == 0) {
11512 return null;
11513 }
11514
11515 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11516 if (mHardwareLayer == null) {
11517 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11518 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011519 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011520 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11521 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011522 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011523 }
11524
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011525 // The layer is not valid if the underlying GPU resources cannot be allocated
11526 if (!mHardwareLayer.isValid()) {
11527 return null;
11528 }
11529
Chet Haasea1cff502012-02-21 13:43:44 -080011530 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011531 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011532 }
11533
11534 return mHardwareLayer;
11535 }
Romain Guy171c5922011-01-06 10:04:23 -080011536
Romain Guy589b0bb2011-10-10 13:57:47 -070011537 /**
11538 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011539 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011540 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011541 *
11542 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011543 * @see #LAYER_TYPE_HARDWARE
11544 */
Romain Guya998dff2012-03-23 18:58:36 -070011545 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011546 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011547 AttachInfo info = mAttachInfo;
11548 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011549 info.mHardwareRenderer.isEnabled() &&
11550 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011551 mHardwareLayer.destroy();
11552 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011553
Romain Guy9c4b79a2011-11-10 19:23:58 -080011554 invalidate(true);
11555 invalidateParentCaches();
11556 }
Romain Guy65b345f2011-07-27 18:51:50 -070011557 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011558 }
Romain Guy65b345f2011-07-27 18:51:50 -070011559 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011560 }
11561
Romain Guy171c5922011-01-06 10:04:23 -080011562 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011563 * Destroys all hardware rendering resources. This method is invoked
11564 * when the system needs to reclaim resources. Upon execution of this
11565 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011566 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011567 * Note: you <strong>must</strong> call
11568 * <code>super.destroyHardwareResources()</code> when overriding
11569 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011570 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011571 * @hide
11572 */
11573 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011574 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011575 }
11576
11577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11579 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11580 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11581 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11582 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11583 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011584 *
Romain Guy171c5922011-01-06 10:04:23 -080011585 * <p>Enabling the drawing cache is similar to
11586 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011587 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11588 * drawing cache has no effect on rendering because the system uses a different mechanism
11589 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11590 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11591 * for information on how to enable software and hardware layers.</p>
11592 *
11593 * <p>This API can be used to manually generate
11594 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11595 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011596 *
11597 * @param enabled true to enable the drawing cache, false otherwise
11598 *
11599 * @see #isDrawingCacheEnabled()
11600 * @see #getDrawingCache()
11601 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011602 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 */
11604 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011605 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011606 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11607 }
11608
11609 /**
11610 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11611 *
11612 * @return true if the drawing cache is enabled
11613 *
11614 * @see #setDrawingCacheEnabled(boolean)
11615 * @see #getDrawingCache()
11616 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011617 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 public boolean isDrawingCacheEnabled() {
11619 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11620 }
11621
11622 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011623 * Debugging utility which recursively outputs the dirty state of a view and its
11624 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011625 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011626 * @hide
11627 */
Romain Guy676b1732011-02-14 14:45:33 -080011628 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011629 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11630 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11631 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11632 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11633 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11634 if (clear) {
11635 mPrivateFlags &= clearMask;
11636 }
11637 if (this instanceof ViewGroup) {
11638 ViewGroup parent = (ViewGroup) this;
11639 final int count = parent.getChildCount();
11640 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011641 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011642 child.outputDirtyFlags(indent + " ", clear, clearMask);
11643 }
11644 }
11645 }
11646
11647 /**
11648 * This method is used by ViewGroup to cause its children to restore or recreate their
11649 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11650 * to recreate its own display list, which would happen if it went through the normal
11651 * draw/dispatchDraw mechanisms.
11652 *
11653 * @hide
11654 */
11655 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011656
11657 /**
11658 * A view that is not attached or hardware accelerated cannot create a display list.
11659 * This method checks these conditions and returns the appropriate result.
11660 *
11661 * @return true if view has the ability to create a display list, false otherwise.
11662 *
11663 * @hide
11664 */
11665 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011666 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011667 }
Joe Malin32736f02011-01-19 16:14:20 -080011668
Chet Haasedaf98e92011-01-10 14:10:36 -080011669 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011670 * @return The HardwareRenderer associated with that view or null if hardware rendering
11671 * is not supported or this this has not been attached to a window.
11672 *
11673 * @hide
11674 */
11675 public HardwareRenderer getHardwareRenderer() {
11676 if (mAttachInfo != null) {
11677 return mAttachInfo.mHardwareRenderer;
11678 }
11679 return null;
11680 }
11681
11682 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011683 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11684 * Otherwise, the same display list will be returned (after having been rendered into
11685 * along the way, depending on the invalidation state of the view).
11686 *
11687 * @param displayList The previous version of this displayList, could be null.
11688 * @param isLayer Whether the requester of the display list is a layer. If so,
11689 * the view will avoid creating a layer inside the resulting display list.
11690 * @return A new or reused DisplayList object.
11691 */
11692 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11693 if (!canHaveDisplayList()) {
11694 return null;
11695 }
11696
11697 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11698 displayList == null || !displayList.isValid() ||
11699 (!isLayer && mRecreateDisplayList))) {
11700 // Don't need to recreate the display list, just need to tell our
11701 // children to restore/recreate theirs
11702 if (displayList != null && displayList.isValid() &&
11703 !isLayer && !mRecreateDisplayList) {
11704 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11705 mPrivateFlags &= ~DIRTY_MASK;
11706 dispatchGetDisplayList();
11707
11708 return displayList;
11709 }
11710
11711 if (!isLayer) {
11712 // If we got here, we're recreating it. Mark it as such to ensure that
11713 // we copy in child display lists into ours in drawChild()
11714 mRecreateDisplayList = true;
11715 }
11716 if (displayList == null) {
11717 final String name = getClass().getSimpleName();
11718 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11719 // If we're creating a new display list, make sure our parent gets invalidated
11720 // since they will need to recreate their display list to account for this
11721 // new child display list.
11722 invalidateParentCaches();
11723 }
11724
11725 boolean caching = false;
11726 final HardwareCanvas canvas = displayList.start();
11727 int restoreCount = 0;
11728 int width = mRight - mLeft;
11729 int height = mBottom - mTop;
11730
11731 try {
11732 canvas.setViewport(width, height);
11733 // The dirty rect should always be null for a display list
11734 canvas.onPreDraw(null);
11735 int layerType = (
11736 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11737 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070011738 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070011739 if (layerType == LAYER_TYPE_HARDWARE) {
11740 final HardwareLayer layer = getHardwareLayer();
11741 if (layer != null && layer.isValid()) {
11742 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11743 } else {
11744 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11745 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11746 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11747 }
11748 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011749 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011750 buildDrawingCache(true);
11751 Bitmap cache = getDrawingCache(true);
11752 if (cache != null) {
11753 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11754 caching = true;
11755 }
Chet Haasea1cff502012-02-21 13:43:44 -080011756 }
Chet Haasea1cff502012-02-21 13:43:44 -080011757 } else {
11758
11759 computeScroll();
11760
Chet Haasea1cff502012-02-21 13:43:44 -080011761 canvas.translate(-mScrollX, -mScrollY);
11762 if (!isLayer) {
11763 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11764 mPrivateFlags &= ~DIRTY_MASK;
11765 }
11766
11767 // Fast path for layouts with no backgrounds
11768 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11769 dispatchDraw(canvas);
11770 } else {
11771 draw(canvas);
11772 }
11773 }
11774 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080011775 canvas.onPostDraw();
11776
11777 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070011778 displayList.setCaching(caching);
11779 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080011780 displayList.setLeftTopRightBottom(0, 0, width, height);
11781 } else {
11782 setDisplayListProperties(displayList);
11783 }
11784 }
11785 } else if (!isLayer) {
11786 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11787 mPrivateFlags &= ~DIRTY_MASK;
11788 }
11789
11790 return displayList;
11791 }
11792
11793 /**
11794 * Get the DisplayList for the HardwareLayer
11795 *
11796 * @param layer The HardwareLayer whose DisplayList we want
11797 * @return A DisplayList fopr the specified HardwareLayer
11798 */
11799 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11800 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11801 layer.setDisplayList(displayList);
11802 return displayList;
11803 }
11804
11805
11806 /**
Romain Guyb051e892010-09-28 19:09:36 -070011807 * <p>Returns a display list that can be used to draw this view again
11808 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011809 *
Romain Guyb051e892010-09-28 19:09:36 -070011810 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011811 *
11812 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011813 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011814 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011815 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011816 return mDisplayList;
11817 }
11818
11819 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011820 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011821 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011822 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011823 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011824 * @see #getDrawingCache(boolean)
11825 */
11826 public Bitmap getDrawingCache() {
11827 return getDrawingCache(false);
11828 }
11829
11830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11832 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11833 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11834 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11835 * request the drawing cache by calling this method and draw it on screen if the
11836 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011837 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011838 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11839 * this method will create a bitmap of the same size as this view. Because this bitmap
11840 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11841 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11842 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11843 * size than the view. This implies that your application must be able to handle this
11844 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011845 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011846 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11847 * the current density of the screen when the application is in compatibility
11848 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011850 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 * @see #setDrawingCacheEnabled(boolean)
11853 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011854 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011855 * @see #destroyDrawingCache()
11856 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011857 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11859 return null;
11860 }
11861 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011862 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011863 }
Romain Guy02890fd2010-08-06 17:58:44 -070011864 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011865 }
11866
11867 /**
11868 * <p>Frees the resources used by the drawing cache. If you call
11869 * {@link #buildDrawingCache()} manually without calling
11870 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11871 * should cleanup the cache with this method afterwards.</p>
11872 *
11873 * @see #setDrawingCacheEnabled(boolean)
11874 * @see #buildDrawingCache()
11875 * @see #getDrawingCache()
11876 */
11877 public void destroyDrawingCache() {
11878 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011879 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011880 mDrawingCache = null;
11881 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011882 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011883 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011884 mUnscaledDrawingCache = null;
11885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011886 }
11887
11888 /**
11889 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011890 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011891 * view will always be drawn on top of a solid color.
11892 *
11893 * @param color The background color to use for the drawing cache's bitmap
11894 *
11895 * @see #setDrawingCacheEnabled(boolean)
11896 * @see #buildDrawingCache()
11897 * @see #getDrawingCache()
11898 */
11899 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011900 if (color != mDrawingCacheBackgroundColor) {
11901 mDrawingCacheBackgroundColor = color;
11902 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 }
11905
11906 /**
11907 * @see #setDrawingCacheBackgroundColor(int)
11908 *
11909 * @return The background color to used for the drawing cache's bitmap
11910 */
11911 public int getDrawingCacheBackgroundColor() {
11912 return mDrawingCacheBackgroundColor;
11913 }
11914
11915 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011916 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011917 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011918 * @see #buildDrawingCache(boolean)
11919 */
11920 public void buildDrawingCache() {
11921 buildDrawingCache(false);
11922 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011923
Romain Guyfbd8f692009-06-26 14:51:58 -070011924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11926 *
11927 * <p>If you call {@link #buildDrawingCache()} manually without calling
11928 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11929 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011930 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011931 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11932 * this method will create a bitmap of the same size as this view. Because this bitmap
11933 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11934 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11935 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11936 * size than the view. This implies that your application must be able to handle this
11937 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011938 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011939 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11940 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011941 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011942 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011943 *
11944 * @see #getDrawingCache()
11945 * @see #destroyDrawingCache()
11946 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011947 public void buildDrawingCache(boolean autoScale) {
11948 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011949 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011950 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011951
11952 if (ViewDebug.TRACE_HIERARCHY) {
11953 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955
Romain Guy8506ab42009-06-11 17:35:47 -070011956 int width = mRight - mLeft;
11957 int height = mBottom - mTop;
11958
11959 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011960 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011961
Romain Guye1123222009-06-29 14:24:56 -070011962 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011963 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11964 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011966
11967 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011968 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011969 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970
11971 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011972 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011973 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011974 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11975 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011976 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011977 return;
11978 }
11979
11980 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011981 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011982
11983 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011984 Bitmap.Config quality;
11985 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011986 // Never pick ARGB_4444 because it looks awful
11987 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11989 case DRAWING_CACHE_QUALITY_AUTO:
11990 quality = Bitmap.Config.ARGB_8888;
11991 break;
11992 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011993 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011994 break;
11995 case DRAWING_CACHE_QUALITY_HIGH:
11996 quality = Bitmap.Config.ARGB_8888;
11997 break;
11998 default:
11999 quality = Bitmap.Config.ARGB_8888;
12000 break;
12001 }
12002 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012003 // Optimization for translucent windows
12004 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012005 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012006 }
12007
12008 // Try to cleanup memory
12009 if (bitmap != null) bitmap.recycle();
12010
12011 try {
12012 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012013 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012014 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012015 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012016 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012017 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012018 }
Adam Powell26153a32010-11-08 15:22:27 -080012019 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012020 } catch (OutOfMemoryError e) {
12021 // If there is not enough memory to create the bitmap cache, just
12022 // ignore the issue as bitmap caches are not required to draw the
12023 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012024 if (autoScale) {
12025 mDrawingCache = null;
12026 } else {
12027 mUnscaledDrawingCache = null;
12028 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012029 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012030 return;
12031 }
12032
12033 clear = drawingCacheBackgroundColor != 0;
12034 }
12035
12036 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012037 if (attachInfo != null) {
12038 canvas = attachInfo.mCanvas;
12039 if (canvas == null) {
12040 canvas = new Canvas();
12041 }
12042 canvas.setBitmap(bitmap);
12043 // Temporarily clobber the cached Canvas in case one of our children
12044 // is also using a drawing cache. Without this, the children would
12045 // steal the canvas by attaching their own bitmap to it and bad, bad
12046 // thing would happen (invisible views, corrupted drawings, etc.)
12047 attachInfo.mCanvas = null;
12048 } else {
12049 // This case should hopefully never or seldom happen
12050 canvas = new Canvas(bitmap);
12051 }
12052
12053 if (clear) {
12054 bitmap.eraseColor(drawingCacheBackgroundColor);
12055 }
12056
12057 computeScroll();
12058 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012059
Romain Guye1123222009-06-29 14:24:56 -070012060 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012061 final float scale = attachInfo.mApplicationScale;
12062 canvas.scale(scale, scale);
12063 }
Joe Malin32736f02011-01-19 16:14:20 -080012064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065 canvas.translate(-mScrollX, -mScrollY);
12066
Romain Guy5bcdff42009-05-14 21:27:18 -070012067 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012068 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12069 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012070 mPrivateFlags |= DRAWING_CACHE_VALID;
12071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012072
12073 // Fast path for layouts with no backgrounds
12074 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12075 if (ViewDebug.TRACE_HIERARCHY) {
12076 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12077 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012078 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012079 dispatchDraw(canvas);
12080 } else {
12081 draw(canvas);
12082 }
12083
12084 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012085 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086
12087 if (attachInfo != null) {
12088 // Restore the cached Canvas for our siblings
12089 attachInfo.mCanvas = canvas;
12090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 }
12092 }
12093
12094 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012095 * Create a snapshot of the view into a bitmap. We should probably make
12096 * some form of this public, but should think about the API.
12097 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012098 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012099 int width = mRight - mLeft;
12100 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012101
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012102 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012103 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012104 width = (int) ((width * scale) + 0.5f);
12105 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012106
Romain Guy8c11e312009-09-14 15:15:30 -070012107 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012108 if (bitmap == null) {
12109 throw new OutOfMemoryError();
12110 }
12111
Romain Guyc529d8d2011-09-06 15:01:39 -070012112 Resources resources = getResources();
12113 if (resources != null) {
12114 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12115 }
Joe Malin32736f02011-01-19 16:14:20 -080012116
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012117 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012118 if (attachInfo != null) {
12119 canvas = attachInfo.mCanvas;
12120 if (canvas == null) {
12121 canvas = new Canvas();
12122 }
12123 canvas.setBitmap(bitmap);
12124 // Temporarily clobber the cached Canvas in case one of our children
12125 // is also using a drawing cache. Without this, the children would
12126 // steal the canvas by attaching their own bitmap to it and bad, bad
12127 // things would happen (invisible views, corrupted drawings, etc.)
12128 attachInfo.mCanvas = null;
12129 } else {
12130 // This case should hopefully never or seldom happen
12131 canvas = new Canvas(bitmap);
12132 }
12133
Romain Guy5bcdff42009-05-14 21:27:18 -070012134 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012135 bitmap.eraseColor(backgroundColor);
12136 }
12137
12138 computeScroll();
12139 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012140 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012141 canvas.translate(-mScrollX, -mScrollY);
12142
Romain Guy5bcdff42009-05-14 21:27:18 -070012143 // Temporarily remove the dirty mask
12144 int flags = mPrivateFlags;
12145 mPrivateFlags &= ~DIRTY_MASK;
12146
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012147 // Fast path for layouts with no backgrounds
12148 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12149 dispatchDraw(canvas);
12150 } else {
12151 draw(canvas);
12152 }
12153
Romain Guy5bcdff42009-05-14 21:27:18 -070012154 mPrivateFlags = flags;
12155
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012156 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012157 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012158
12159 if (attachInfo != null) {
12160 // Restore the cached Canvas for our siblings
12161 attachInfo.mCanvas = canvas;
12162 }
Romain Guy8506ab42009-06-11 17:35:47 -070012163
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012164 return bitmap;
12165 }
12166
12167 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168 * Indicates whether this View is currently in edit mode. A View is usually
12169 * in edit mode when displayed within a developer tool. For instance, if
12170 * this View is being drawn by a visual user interface builder, this method
12171 * should return true.
12172 *
12173 * Subclasses should check the return value of this method to provide
12174 * different behaviors if their normal behavior might interfere with the
12175 * host environment. For instance: the class spawns a thread in its
12176 * constructor, the drawing code relies on device-specific features, etc.
12177 *
12178 * This method is usually checked in the drawing code of custom widgets.
12179 *
12180 * @return True if this View is in edit mode, false otherwise.
12181 */
12182 public boolean isInEditMode() {
12183 return false;
12184 }
12185
12186 /**
12187 * If the View draws content inside its padding and enables fading edges,
12188 * it needs to support padding offsets. Padding offsets are added to the
12189 * fading edges to extend the length of the fade so that it covers pixels
12190 * drawn inside the padding.
12191 *
12192 * Subclasses of this class should override this method if they need
12193 * to draw content inside the padding.
12194 *
12195 * @return True if padding offset must be applied, false otherwise.
12196 *
12197 * @see #getLeftPaddingOffset()
12198 * @see #getRightPaddingOffset()
12199 * @see #getTopPaddingOffset()
12200 * @see #getBottomPaddingOffset()
12201 *
12202 * @since CURRENT
12203 */
12204 protected boolean isPaddingOffsetRequired() {
12205 return false;
12206 }
12207
12208 /**
12209 * Amount by which to extend the left fading region. Called only when
12210 * {@link #isPaddingOffsetRequired()} returns true.
12211 *
12212 * @return The left padding offset in pixels.
12213 *
12214 * @see #isPaddingOffsetRequired()
12215 *
12216 * @since CURRENT
12217 */
12218 protected int getLeftPaddingOffset() {
12219 return 0;
12220 }
12221
12222 /**
12223 * Amount by which to extend the right fading region. Called only when
12224 * {@link #isPaddingOffsetRequired()} returns true.
12225 *
12226 * @return The right padding offset in pixels.
12227 *
12228 * @see #isPaddingOffsetRequired()
12229 *
12230 * @since CURRENT
12231 */
12232 protected int getRightPaddingOffset() {
12233 return 0;
12234 }
12235
12236 /**
12237 * Amount by which to extend the top fading region. Called only when
12238 * {@link #isPaddingOffsetRequired()} returns true.
12239 *
12240 * @return The top padding offset in pixels.
12241 *
12242 * @see #isPaddingOffsetRequired()
12243 *
12244 * @since CURRENT
12245 */
12246 protected int getTopPaddingOffset() {
12247 return 0;
12248 }
12249
12250 /**
12251 * Amount by which to extend the bottom fading region. Called only when
12252 * {@link #isPaddingOffsetRequired()} returns true.
12253 *
12254 * @return The bottom padding offset in pixels.
12255 *
12256 * @see #isPaddingOffsetRequired()
12257 *
12258 * @since CURRENT
12259 */
12260 protected int getBottomPaddingOffset() {
12261 return 0;
12262 }
12263
12264 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012265 * @hide
12266 * @param offsetRequired
12267 */
12268 protected int getFadeTop(boolean offsetRequired) {
12269 int top = mPaddingTop;
12270 if (offsetRequired) top += getTopPaddingOffset();
12271 return top;
12272 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012273
Romain Guyf2fc4602011-07-19 15:20:03 -070012274 /**
12275 * @hide
12276 * @param offsetRequired
12277 */
12278 protected int getFadeHeight(boolean offsetRequired) {
12279 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012280 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012281 return mBottom - mTop - mPaddingBottom - padding;
12282 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012283
Romain Guyf2fc4602011-07-19 15:20:03 -070012284 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012285 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012286 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012287 *
Romain Guy2bffd262010-09-12 17:40:02 -070012288 * <p>Even if this method returns true, it does not mean that every call
12289 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12290 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012291 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012292 * window is hardware accelerated,
12293 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12294 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012295 *
Romain Guy2bffd262010-09-12 17:40:02 -070012296 * @return True if the view is attached to a window and the window is
12297 * hardware accelerated; false in any other case.
12298 */
12299 public boolean isHardwareAccelerated() {
12300 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12301 }
Joe Malin32736f02011-01-19 16:14:20 -080012302
Romain Guy2bffd262010-09-12 17:40:02 -070012303 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012304 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12305 * case of an active Animation being run on the view.
12306 */
12307 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12308 Animation a, boolean scalingRequired) {
12309 Transformation invalidationTransform;
12310 final int flags = parent.mGroupFlags;
12311 final boolean initialized = a.isInitialized();
12312 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012313 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012314 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12315 onAnimationStart();
12316 }
12317
12318 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12319 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12320 if (parent.mInvalidationTransformation == null) {
12321 parent.mInvalidationTransformation = new Transformation();
12322 }
12323 invalidationTransform = parent.mInvalidationTransformation;
12324 a.getTransformation(drawingTime, invalidationTransform, 1f);
12325 } else {
12326 invalidationTransform = parent.mChildTransformation;
12327 }
12328 if (more) {
12329 if (!a.willChangeBounds()) {
12330 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12331 parent.FLAG_OPTIMIZE_INVALIDATE) {
12332 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12333 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12334 // The child need to draw an animation, potentially offscreen, so
12335 // make sure we do not cancel invalidate requests
12336 parent.mPrivateFlags |= DRAW_ANIMATION;
12337 parent.invalidate(mLeft, mTop, mRight, mBottom);
12338 }
12339 } else {
12340 if (parent.mInvalidateRegion == null) {
12341 parent.mInvalidateRegion = new RectF();
12342 }
12343 final RectF region = parent.mInvalidateRegion;
12344 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12345 invalidationTransform);
12346
12347 // The child need to draw an animation, potentially offscreen, so
12348 // make sure we do not cancel invalidate requests
12349 parent.mPrivateFlags |= DRAW_ANIMATION;
12350
12351 final int left = mLeft + (int) region.left;
12352 final int top = mTop + (int) region.top;
12353 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12354 top + (int) (region.height() + .5f));
12355 }
12356 }
12357 return more;
12358 }
12359
Chet Haasea1cff502012-02-21 13:43:44 -080012360 void setDisplayListProperties() {
12361 setDisplayListProperties(mDisplayList);
12362 }
12363
12364 /**
12365 * This method is called by getDisplayList() when a display list is created or re-rendered.
12366 * It sets or resets the current value of all properties on that display list (resetting is
12367 * necessary when a display list is being re-created, because we need to make sure that
12368 * previously-set transform values
12369 */
12370 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012371 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012372 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012373 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012374 if (mParent instanceof ViewGroup) {
12375 displayList.setClipChildren(
12376 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12377 }
Chet Haase9420abd2012-03-29 16:28:32 -070012378 float alpha = 1;
12379 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12380 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12381 ViewGroup parentVG = (ViewGroup) mParent;
12382 final boolean hasTransform =
12383 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12384 if (hasTransform) {
12385 Transformation transform = parentVG.mChildTransformation;
12386 final int transformType = parentVG.mChildTransformation.getTransformationType();
12387 if (transformType != Transformation.TYPE_IDENTITY) {
12388 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12389 alpha = transform.getAlpha();
12390 }
12391 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12392 displayList.setStaticMatrix(transform.getMatrix());
12393 }
12394 }
12395 }
Chet Haasea1cff502012-02-21 13:43:44 -080012396 }
12397 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012398 alpha *= mTransformationInfo.mAlpha;
12399 if (alpha < 1) {
12400 final int multipliedAlpha = (int) (255 * alpha);
12401 if (onSetAlpha(multipliedAlpha)) {
12402 alpha = 1;
12403 }
12404 }
12405 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012406 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12407 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12408 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12409 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012410 if (mTransformationInfo.mCamera == null) {
12411 mTransformationInfo.mCamera = new Camera();
12412 mTransformationInfo.matrix3D = new Matrix();
12413 }
12414 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012415 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12416 displayList.setPivotX(getPivotX());
12417 displayList.setPivotY(getPivotY());
12418 }
Chet Haase9420abd2012-03-29 16:28:32 -070012419 } else if (alpha < 1) {
12420 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012421 }
12422 }
12423 }
12424
Chet Haasebcca79a2012-02-14 08:45:14 -080012425 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012426 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12427 * This draw() method is an implementation detail and is not intended to be overridden or
12428 * to be called from anywhere else other than ViewGroup.drawChild().
12429 */
12430 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012431 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012432 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012433 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012434 final int flags = parent.mGroupFlags;
12435
Chet Haasea1cff502012-02-21 13:43:44 -080012436 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012437 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012438 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012439 }
12440
12441 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012442 boolean concatMatrix = false;
12443
12444 boolean scalingRequired = false;
12445 boolean caching;
12446 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12447
12448 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012449 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12450 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012451 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012452 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012453 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12454 } else {
12455 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12456 }
12457
Chet Haasebcca79a2012-02-14 08:45:14 -080012458 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012459 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012460 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012461 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012462 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012463 } else if (!useDisplayListProperties &&
12464 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012465 final boolean hasTransform =
12466 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012467 if (hasTransform) {
12468 final int transformType = parent.mChildTransformation.getTransformationType();
12469 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12470 parent.mChildTransformation : null;
12471 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12472 }
12473 }
12474
12475 concatMatrix |= !childHasIdentityMatrix;
12476
12477 // Sets the flag as early as possible to allow draw() implementations
12478 // to call invalidate() successfully when doing animations
12479 mPrivateFlags |= DRAWN;
12480
Chet Haasebcca79a2012-02-14 08:45:14 -080012481 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012482 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12483 return more;
12484 }
12485
12486 if (hardwareAccelerated) {
12487 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12488 // retain the flag's value temporarily in the mRecreateDisplayList flag
12489 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12490 mPrivateFlags &= ~INVALIDATED;
12491 }
12492
12493 computeScroll();
12494
12495 final int sx = mScrollX;
12496 final int sy = mScrollY;
12497
12498 DisplayList displayList = null;
12499 Bitmap cache = null;
12500 boolean hasDisplayList = false;
12501 if (caching) {
12502 if (!hardwareAccelerated) {
12503 if (layerType != LAYER_TYPE_NONE) {
12504 layerType = LAYER_TYPE_SOFTWARE;
12505 buildDrawingCache(true);
12506 }
12507 cache = getDrawingCache(true);
12508 } else {
12509 switch (layerType) {
12510 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012511 if (useDisplayListProperties) {
12512 hasDisplayList = canHaveDisplayList();
12513 } else {
12514 buildDrawingCache(true);
12515 cache = getDrawingCache(true);
12516 }
Chet Haase64a48c12012-02-13 16:33:29 -080012517 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012518 case LAYER_TYPE_HARDWARE:
12519 if (useDisplayListProperties) {
12520 hasDisplayList = canHaveDisplayList();
12521 }
12522 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012523 case LAYER_TYPE_NONE:
12524 // Delay getting the display list until animation-driven alpha values are
12525 // set up and possibly passed on to the view
12526 hasDisplayList = canHaveDisplayList();
12527 break;
12528 }
12529 }
12530 }
Chet Haasea1cff502012-02-21 13:43:44 -080012531 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012532 if (useDisplayListProperties) {
12533 displayList = getDisplayList();
12534 if (!displayList.isValid()) {
12535 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12536 // to getDisplayList(), the display list will be marked invalid and we should not
12537 // try to use it again.
12538 displayList = null;
12539 hasDisplayList = false;
12540 useDisplayListProperties = false;
12541 }
12542 }
Chet Haase64a48c12012-02-13 16:33:29 -080012543
12544 final boolean hasNoCache = cache == null || hasDisplayList;
12545 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12546 layerType != LAYER_TYPE_HARDWARE;
12547
Chet Haasea1cff502012-02-21 13:43:44 -080012548 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012549 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012550 restoreTo = canvas.save();
12551 }
Chet Haase64a48c12012-02-13 16:33:29 -080012552 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012553 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012554 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012555 if (!useDisplayListProperties) {
12556 canvas.translate(mLeft, mTop);
12557 }
Chet Haase64a48c12012-02-13 16:33:29 -080012558 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012559 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012560 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012561 restoreTo = canvas.save();
12562 }
Chet Haase64a48c12012-02-13 16:33:29 -080012563 // mAttachInfo cannot be null, otherwise scalingRequired == false
12564 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12565 canvas.scale(scale, scale);
12566 }
12567 }
12568
Chet Haasea1cff502012-02-21 13:43:44 -080012569 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012570 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012571 if (transformToApply != null || !childHasIdentityMatrix) {
12572 int transX = 0;
12573 int transY = 0;
12574
12575 if (offsetForScroll) {
12576 transX = -sx;
12577 transY = -sy;
12578 }
12579
12580 if (transformToApply != null) {
12581 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012582 if (useDisplayListProperties) {
12583 displayList.setAnimationMatrix(transformToApply.getMatrix());
12584 } else {
12585 // Undo the scroll translation, apply the transformation matrix,
12586 // then redo the scroll translate to get the correct result.
12587 canvas.translate(-transX, -transY);
12588 canvas.concat(transformToApply.getMatrix());
12589 canvas.translate(transX, transY);
12590 }
Chet Haasea1cff502012-02-21 13:43:44 -080012591 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012592 }
12593
12594 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012595 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012596 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012597 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012598 }
12599 }
12600
Chet Haasea1cff502012-02-21 13:43:44 -080012601 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012602 canvas.translate(-transX, -transY);
12603 canvas.concat(getMatrix());
12604 canvas.translate(transX, transY);
12605 }
12606 }
12607
Chet Haase9420abd2012-03-29 16:28:32 -070012608 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012609 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012610 if (hasNoCache) {
12611 final int multipliedAlpha = (int) (255 * alpha);
12612 if (!onSetAlpha(multipliedAlpha)) {
12613 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012614 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012615 layerType != LAYER_TYPE_NONE) {
12616 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12617 }
Chet Haase9420abd2012-03-29 16:28:32 -070012618 if (useDisplayListProperties) {
12619 displayList.setAlpha(alpha * getAlpha());
12620 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012621 final int scrollX = hasDisplayList ? 0 : sx;
12622 final int scrollY = hasDisplayList ? 0 : sy;
12623 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12624 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012625 }
12626 } else {
12627 // Alpha is handled by the child directly, clobber the layer's alpha
12628 mPrivateFlags |= ALPHA_SET;
12629 }
12630 }
12631 }
12632 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12633 onSetAlpha(255);
12634 mPrivateFlags &= ~ALPHA_SET;
12635 }
12636
Chet Haasea1cff502012-02-21 13:43:44 -080012637 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12638 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012639 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012640 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012641 } else {
12642 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012643 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012644 } else {
12645 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12646 }
12647 }
12648 }
12649
Chet Haase9420abd2012-03-29 16:28:32 -070012650 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012651 displayList = getDisplayList();
12652 if (!displayList.isValid()) {
12653 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12654 // to getDisplayList(), the display list will be marked invalid and we should not
12655 // try to use it again.
12656 displayList = null;
12657 hasDisplayList = false;
12658 }
12659 }
12660
12661 if (hasNoCache) {
12662 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012663 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012664 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012665 if (layer != null && layer.isValid()) {
12666 mLayerPaint.setAlpha((int) (alpha * 255));
12667 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12668 layerRendered = true;
12669 } else {
12670 final int scrollX = hasDisplayList ? 0 : sx;
12671 final int scrollY = hasDisplayList ? 0 : sy;
12672 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012673 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012674 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12675 }
12676 }
12677
12678 if (!layerRendered) {
12679 if (!hasDisplayList) {
12680 // Fast path for layouts with no backgrounds
12681 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12682 if (ViewDebug.TRACE_HIERARCHY) {
12683 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12684 }
12685 mPrivateFlags &= ~DIRTY_MASK;
12686 dispatchDraw(canvas);
12687 } else {
12688 draw(canvas);
12689 }
12690 } else {
12691 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070012692 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012693 }
12694 }
12695 } else if (cache != null) {
12696 mPrivateFlags &= ~DIRTY_MASK;
12697 Paint cachePaint;
12698
12699 if (layerType == LAYER_TYPE_NONE) {
12700 cachePaint = parent.mCachePaint;
12701 if (cachePaint == null) {
12702 cachePaint = new Paint();
12703 cachePaint.setDither(false);
12704 parent.mCachePaint = cachePaint;
12705 }
Chet Haase9420abd2012-03-29 16:28:32 -070012706 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012707 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012708 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12709 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012710 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012711 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012712 }
12713 } else {
12714 cachePaint = mLayerPaint;
12715 cachePaint.setAlpha((int) (alpha * 255));
12716 }
12717 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12718 }
12719
Chet Haasea1cff502012-02-21 13:43:44 -080012720 if (restoreTo >= 0) {
12721 canvas.restoreToCount(restoreTo);
12722 }
Chet Haase64a48c12012-02-13 16:33:29 -080012723
12724 if (a != null && !more) {
12725 if (!hardwareAccelerated && !a.getFillAfter()) {
12726 onSetAlpha(255);
12727 }
12728 parent.finishAnimatingView(this, a);
12729 }
12730
12731 if (more && hardwareAccelerated) {
12732 // invalidation is the trigger to recreate display lists, so if we're using
12733 // display lists to render, force an invalidate to allow the animation to
12734 // continue drawing another frame
12735 parent.invalidate(true);
12736 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12737 // alpha animations should cause the child to recreate its display list
12738 invalidate(true);
12739 }
12740 }
12741
12742 mRecreateDisplayList = false;
12743
12744 return more;
12745 }
12746
12747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 * Manually render this view (and all of its children) to the given Canvas.
12749 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012750 * called. When implementing a view, implement
12751 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12752 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012753 *
12754 * @param canvas The Canvas to which the View is rendered.
12755 */
12756 public void draw(Canvas canvas) {
12757 if (ViewDebug.TRACE_HIERARCHY) {
12758 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12759 }
12760
Romain Guy5bcdff42009-05-14 21:27:18 -070012761 final int privateFlags = mPrivateFlags;
12762 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12763 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12764 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 /*
12767 * Draw traversal performs several drawing steps which must be executed
12768 * in the appropriate order:
12769 *
12770 * 1. Draw the background
12771 * 2. If necessary, save the canvas' layers to prepare for fading
12772 * 3. Draw view's content
12773 * 4. Draw children
12774 * 5. If necessary, draw the fading edges and restore layers
12775 * 6. Draw decorations (scrollbars for instance)
12776 */
12777
12778 // Step 1, draw the background, if needed
12779 int saveCount;
12780
Romain Guy24443ea2009-05-11 11:56:30 -070012781 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012782 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012783 if (background != null) {
12784 final int scrollX = mScrollX;
12785 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012786
Romain Guy24443ea2009-05-11 11:56:30 -070012787 if (mBackgroundSizeChanged) {
12788 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12789 mBackgroundSizeChanged = false;
12790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012791
Romain Guy24443ea2009-05-11 11:56:30 -070012792 if ((scrollX | scrollY) == 0) {
12793 background.draw(canvas);
12794 } else {
12795 canvas.translate(scrollX, scrollY);
12796 background.draw(canvas);
12797 canvas.translate(-scrollX, -scrollY);
12798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012799 }
12800 }
12801
12802 // skip step 2 & 5 if possible (common case)
12803 final int viewFlags = mViewFlags;
12804 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12805 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12806 if (!verticalEdges && !horizontalEdges) {
12807 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012808 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012809
12810 // Step 4, draw the children
12811 dispatchDraw(canvas);
12812
12813 // Step 6, draw decorations (scrollbars)
12814 onDrawScrollBars(canvas);
12815
12816 // we're done...
12817 return;
12818 }
12819
12820 /*
12821 * Here we do the full fledged routine...
12822 * (this is an uncommon case where speed matters less,
12823 * this is why we repeat some of the tests that have been
12824 * done above)
12825 */
12826
12827 boolean drawTop = false;
12828 boolean drawBottom = false;
12829 boolean drawLeft = false;
12830 boolean drawRight = false;
12831
12832 float topFadeStrength = 0.0f;
12833 float bottomFadeStrength = 0.0f;
12834 float leftFadeStrength = 0.0f;
12835 float rightFadeStrength = 0.0f;
12836
12837 // Step 2, save the canvas' layers
12838 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012839
12840 final boolean offsetRequired = isPaddingOffsetRequired();
12841 if (offsetRequired) {
12842 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012843 }
12844
12845 int left = mScrollX + paddingLeft;
12846 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012847 int top = mScrollY + getFadeTop(offsetRequired);
12848 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849
12850 if (offsetRequired) {
12851 right += getRightPaddingOffset();
12852 bottom += getBottomPaddingOffset();
12853 }
12854
12855 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012856 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012857 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012858
12859 // clip the fade length if top and bottom fades overlap
12860 // overlapping fades produce odd-looking artifacts
12861 if (verticalEdges && (top + length > bottom - length)) {
12862 length = (bottom - top) / 2;
12863 }
12864
12865 // also clip horizontal fades if necessary
12866 if (horizontalEdges && (left + length > right - length)) {
12867 length = (right - left) / 2;
12868 }
12869
12870 if (verticalEdges) {
12871 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012872 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012874 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012875 }
12876
12877 if (horizontalEdges) {
12878 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012879 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012880 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012881 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882 }
12883
12884 saveCount = canvas.getSaveCount();
12885
12886 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012887 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012888 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12889
12890 if (drawTop) {
12891 canvas.saveLayer(left, top, right, top + length, null, flags);
12892 }
12893
12894 if (drawBottom) {
12895 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12896 }
12897
12898 if (drawLeft) {
12899 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12900 }
12901
12902 if (drawRight) {
12903 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12904 }
12905 } else {
12906 scrollabilityCache.setFadeColor(solidColor);
12907 }
12908
12909 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012910 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012911
12912 // Step 4, draw the children
12913 dispatchDraw(canvas);
12914
12915 // Step 5, draw the fade effect and restore layers
12916 final Paint p = scrollabilityCache.paint;
12917 final Matrix matrix = scrollabilityCache.matrix;
12918 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012919
12920 if (drawTop) {
12921 matrix.setScale(1, fadeHeight * topFadeStrength);
12922 matrix.postTranslate(left, top);
12923 fade.setLocalMatrix(matrix);
12924 canvas.drawRect(left, top, right, top + length, p);
12925 }
12926
12927 if (drawBottom) {
12928 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12929 matrix.postRotate(180);
12930 matrix.postTranslate(left, bottom);
12931 fade.setLocalMatrix(matrix);
12932 canvas.drawRect(left, bottom - length, right, bottom, p);
12933 }
12934
12935 if (drawLeft) {
12936 matrix.setScale(1, fadeHeight * leftFadeStrength);
12937 matrix.postRotate(-90);
12938 matrix.postTranslate(left, top);
12939 fade.setLocalMatrix(matrix);
12940 canvas.drawRect(left, top, left + length, bottom, p);
12941 }
12942
12943 if (drawRight) {
12944 matrix.setScale(1, fadeHeight * rightFadeStrength);
12945 matrix.postRotate(90);
12946 matrix.postTranslate(right, top);
12947 fade.setLocalMatrix(matrix);
12948 canvas.drawRect(right - length, top, right, bottom, p);
12949 }
12950
12951 canvas.restoreToCount(saveCount);
12952
12953 // Step 6, draw decorations (scrollbars)
12954 onDrawScrollBars(canvas);
12955 }
12956
12957 /**
12958 * Override this if your view is known to always be drawn on top of a solid color background,
12959 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12960 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12961 * should be set to 0xFF.
12962 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012963 * @see #setVerticalFadingEdgeEnabled(boolean)
12964 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012965 *
12966 * @return The known solid color background for this view, or 0 if the color may vary
12967 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012968 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012969 public int getSolidColor() {
12970 return 0;
12971 }
12972
12973 /**
12974 * Build a human readable string representation of the specified view flags.
12975 *
12976 * @param flags the view flags to convert to a string
12977 * @return a String representing the supplied flags
12978 */
12979 private static String printFlags(int flags) {
12980 String output = "";
12981 int numFlags = 0;
12982 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12983 output += "TAKES_FOCUS";
12984 numFlags++;
12985 }
12986
12987 switch (flags & VISIBILITY_MASK) {
12988 case INVISIBLE:
12989 if (numFlags > 0) {
12990 output += " ";
12991 }
12992 output += "INVISIBLE";
12993 // USELESS HERE numFlags++;
12994 break;
12995 case GONE:
12996 if (numFlags > 0) {
12997 output += " ";
12998 }
12999 output += "GONE";
13000 // USELESS HERE numFlags++;
13001 break;
13002 default:
13003 break;
13004 }
13005 return output;
13006 }
13007
13008 /**
13009 * Build a human readable string representation of the specified private
13010 * view flags.
13011 *
13012 * @param privateFlags the private view flags to convert to a string
13013 * @return a String representing the supplied flags
13014 */
13015 private static String printPrivateFlags(int privateFlags) {
13016 String output = "";
13017 int numFlags = 0;
13018
13019 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13020 output += "WANTS_FOCUS";
13021 numFlags++;
13022 }
13023
13024 if ((privateFlags & FOCUSED) == FOCUSED) {
13025 if (numFlags > 0) {
13026 output += " ";
13027 }
13028 output += "FOCUSED";
13029 numFlags++;
13030 }
13031
13032 if ((privateFlags & SELECTED) == SELECTED) {
13033 if (numFlags > 0) {
13034 output += " ";
13035 }
13036 output += "SELECTED";
13037 numFlags++;
13038 }
13039
13040 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13041 if (numFlags > 0) {
13042 output += " ";
13043 }
13044 output += "IS_ROOT_NAMESPACE";
13045 numFlags++;
13046 }
13047
13048 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13049 if (numFlags > 0) {
13050 output += " ";
13051 }
13052 output += "HAS_BOUNDS";
13053 numFlags++;
13054 }
13055
13056 if ((privateFlags & DRAWN) == DRAWN) {
13057 if (numFlags > 0) {
13058 output += " ";
13059 }
13060 output += "DRAWN";
13061 // USELESS HERE numFlags++;
13062 }
13063 return output;
13064 }
13065
13066 /**
13067 * <p>Indicates whether or not this view's layout will be requested during
13068 * the next hierarchy layout pass.</p>
13069 *
13070 * @return true if the layout will be forced during next layout pass
13071 */
13072 public boolean isLayoutRequested() {
13073 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13074 }
13075
13076 /**
13077 * Assign a size and position to a view and all of its
13078 * descendants
13079 *
13080 * <p>This is the second phase of the layout mechanism.
13081 * (The first is measuring). In this phase, each parent calls
13082 * layout on all of its children to position them.
13083 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013084 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013085 *
Chet Haase9c087442011-01-12 16:20:16 -080013086 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013087 * Derived classes with children should override
13088 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013089 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013090 *
13091 * @param l Left position, relative to parent
13092 * @param t Top position, relative to parent
13093 * @param r Right position, relative to parent
13094 * @param b Bottom position, relative to parent
13095 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013096 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013097 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013098 int oldL = mLeft;
13099 int oldT = mTop;
13100 int oldB = mBottom;
13101 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013102 boolean changed = setFrame(l, t, r, b);
13103 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13104 if (ViewDebug.TRACE_HIERARCHY) {
13105 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13106 }
13107
13108 onLayout(changed, l, t, r, b);
13109 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013110
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013111 ListenerInfo li = mListenerInfo;
13112 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013113 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013114 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013115 int numListeners = listenersCopy.size();
13116 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013117 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013118 }
13119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013120 }
13121 mPrivateFlags &= ~FORCE_LAYOUT;
13122 }
13123
13124 /**
13125 * Called from layout when this view should
13126 * assign a size and position to each of its children.
13127 *
13128 * Derived classes with children should override
13129 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013130 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013131 * @param changed This is a new size or position for this view
13132 * @param left Left position, relative to parent
13133 * @param top Top position, relative to parent
13134 * @param right Right position, relative to parent
13135 * @param bottom Bottom position, relative to parent
13136 */
13137 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13138 }
13139
13140 /**
13141 * Assign a size and position to this view.
13142 *
13143 * This is called from layout.
13144 *
13145 * @param left Left position, relative to parent
13146 * @param top Top position, relative to parent
13147 * @param right Right position, relative to parent
13148 * @param bottom Bottom position, relative to parent
13149 * @return true if the new size and position are different than the
13150 * previous ones
13151 * {@hide}
13152 */
13153 protected boolean setFrame(int left, int top, int right, int bottom) {
13154 boolean changed = false;
13155
13156 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013157 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013158 + right + "," + bottom + ")");
13159 }
13160
13161 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13162 changed = true;
13163
13164 // Remember our drawn bit
13165 int drawn = mPrivateFlags & DRAWN;
13166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013167 int oldWidth = mRight - mLeft;
13168 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013169 int newWidth = right - left;
13170 int newHeight = bottom - top;
13171 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13172
13173 // Invalidate our old position
13174 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013175
13176 mLeft = left;
13177 mTop = top;
13178 mRight = right;
13179 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013180 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013181 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013183
13184 mPrivateFlags |= HAS_BOUNDS;
13185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013186
Chet Haase75755e22011-07-18 17:48:25 -070013187 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013188 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13189 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013190 if (mTransformationInfo != null) {
13191 mTransformationInfo.mMatrixDirty = true;
13192 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013194 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13195 }
13196
13197 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13198 // If we are visible, force the DRAWN bit to on so that
13199 // this invalidate will go through (at least to our parent).
13200 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013201 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013202 // the DRAWN bit.
13203 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013204 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013205 // parent display list may need to be recreated based on a change in the bounds
13206 // of any child
13207 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013208 }
13209
13210 // Reset drawn bit to original value (invalidate turns it off)
13211 mPrivateFlags |= drawn;
13212
13213 mBackgroundSizeChanged = true;
13214 }
13215 return changed;
13216 }
13217
13218 /**
13219 * Finalize inflating a view from XML. This is called as the last phase
13220 * of inflation, after all child views have been added.
13221 *
13222 * <p>Even if the subclass overrides onFinishInflate, they should always be
13223 * sure to call the super method, so that we get called.
13224 */
13225 protected void onFinishInflate() {
13226 }
13227
13228 /**
13229 * Returns the resources associated with this view.
13230 *
13231 * @return Resources object.
13232 */
13233 public Resources getResources() {
13234 return mResources;
13235 }
13236
13237 /**
13238 * Invalidates the specified Drawable.
13239 *
13240 * @param drawable the drawable to invalidate
13241 */
13242 public void invalidateDrawable(Drawable drawable) {
13243 if (verifyDrawable(drawable)) {
13244 final Rect dirty = drawable.getBounds();
13245 final int scrollX = mScrollX;
13246 final int scrollY = mScrollY;
13247
13248 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13249 dirty.right + scrollX, dirty.bottom + scrollY);
13250 }
13251 }
13252
13253 /**
13254 * Schedules an action on a drawable to occur at a specified time.
13255 *
13256 * @param who the recipient of the action
13257 * @param what the action to run on the drawable
13258 * @param when the time at which the action must occur. Uses the
13259 * {@link SystemClock#uptimeMillis} timebase.
13260 */
13261 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013262 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013263 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013264 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013265 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13266 Choreographer.CALLBACK_ANIMATION, what, who,
13267 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013268 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013269 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013271 }
13272 }
13273
13274 /**
13275 * Cancels a scheduled action on a drawable.
13276 *
13277 * @param who the recipient of the action
13278 * @param what the action to cancel
13279 */
13280 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013281 if (verifyDrawable(who) && what != null) {
13282 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013283 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13284 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013285 } else {
13286 ViewRootImpl.getRunQueue().removeCallbacks(what);
13287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013288 }
13289 }
13290
13291 /**
13292 * Unschedule any events associated with the given Drawable. This can be
13293 * used when selecting a new Drawable into a view, so that the previous
13294 * one is completely unscheduled.
13295 *
13296 * @param who The Drawable to unschedule.
13297 *
13298 * @see #drawableStateChanged
13299 */
13300 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013301 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013302 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13303 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013304 }
13305 }
13306
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013307 /**
13308 * Return the layout direction of a given Drawable.
13309 *
13310 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013311 */
13312 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013313 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013314 }
13315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013316 /**
13317 * If your view subclass is displaying its own Drawable objects, it should
13318 * override this function and return true for any Drawable it is
13319 * displaying. This allows animations for those drawables to be
13320 * scheduled.
13321 *
13322 * <p>Be sure to call through to the super class when overriding this
13323 * function.
13324 *
13325 * @param who The Drawable to verify. Return true if it is one you are
13326 * displaying, else return the result of calling through to the
13327 * super class.
13328 *
13329 * @return boolean If true than the Drawable is being displayed in the
13330 * view; else false and it is not allowed to animate.
13331 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013332 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13333 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013334 */
13335 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013336 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013337 }
13338
13339 /**
13340 * This function is called whenever the state of the view changes in such
13341 * a way that it impacts the state of drawables being shown.
13342 *
13343 * <p>Be sure to call through to the superclass when overriding this
13344 * function.
13345 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013346 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013347 */
13348 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013349 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013350 if (d != null && d.isStateful()) {
13351 d.setState(getDrawableState());
13352 }
13353 }
13354
13355 /**
13356 * Call this to force a view to update its drawable state. This will cause
13357 * drawableStateChanged to be called on this view. Views that are interested
13358 * in the new state should call getDrawableState.
13359 *
13360 * @see #drawableStateChanged
13361 * @see #getDrawableState
13362 */
13363 public void refreshDrawableState() {
13364 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13365 drawableStateChanged();
13366
13367 ViewParent parent = mParent;
13368 if (parent != null) {
13369 parent.childDrawableStateChanged(this);
13370 }
13371 }
13372
13373 /**
13374 * Return an array of resource IDs of the drawable states representing the
13375 * current state of the view.
13376 *
13377 * @return The current drawable state
13378 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013379 * @see Drawable#setState(int[])
13380 * @see #drawableStateChanged()
13381 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013382 */
13383 public final int[] getDrawableState() {
13384 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13385 return mDrawableState;
13386 } else {
13387 mDrawableState = onCreateDrawableState(0);
13388 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13389 return mDrawableState;
13390 }
13391 }
13392
13393 /**
13394 * Generate the new {@link android.graphics.drawable.Drawable} state for
13395 * this view. This is called by the view
13396 * system when the cached Drawable state is determined to be invalid. To
13397 * retrieve the current state, you should use {@link #getDrawableState}.
13398 *
13399 * @param extraSpace if non-zero, this is the number of extra entries you
13400 * would like in the returned array in which you can place your own
13401 * states.
13402 *
13403 * @return Returns an array holding the current {@link Drawable} state of
13404 * the view.
13405 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013406 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013407 */
13408 protected int[] onCreateDrawableState(int extraSpace) {
13409 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13410 mParent instanceof View) {
13411 return ((View) mParent).onCreateDrawableState(extraSpace);
13412 }
13413
13414 int[] drawableState;
13415
13416 int privateFlags = mPrivateFlags;
13417
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013418 int viewStateIndex = 0;
13419 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13420 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13421 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013422 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013423 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13424 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013425 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13426 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013427 // This is set if HW acceleration is requested, even if the current
13428 // process doesn't allow it. This is just to allow app preview
13429 // windows to better match their app.
13430 viewStateIndex |= VIEW_STATE_ACCELERATED;
13431 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013432 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013433
Christopher Tate3d4bf172011-03-28 16:16:46 -070013434 final int privateFlags2 = mPrivateFlags2;
13435 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13436 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013438 drawableState = VIEW_STATE_SETS[viewStateIndex];
13439
13440 //noinspection ConstantIfStatement
13441 if (false) {
13442 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13443 Log.i("View", toString()
13444 + " pressed=" + ((privateFlags & PRESSED) != 0)
13445 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13446 + " fo=" + hasFocus()
13447 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013448 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013449 + ": " + Arrays.toString(drawableState));
13450 }
13451
13452 if (extraSpace == 0) {
13453 return drawableState;
13454 }
13455
13456 final int[] fullState;
13457 if (drawableState != null) {
13458 fullState = new int[drawableState.length + extraSpace];
13459 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13460 } else {
13461 fullState = new int[extraSpace];
13462 }
13463
13464 return fullState;
13465 }
13466
13467 /**
13468 * Merge your own state values in <var>additionalState</var> into the base
13469 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013470 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013471 *
13472 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013473 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013474 * own additional state values.
13475 *
13476 * @param additionalState The additional state values you would like
13477 * added to <var>baseState</var>; this array is not modified.
13478 *
13479 * @return As a convenience, the <var>baseState</var> array you originally
13480 * passed into the function is returned.
13481 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013482 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013483 */
13484 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13485 final int N = baseState.length;
13486 int i = N - 1;
13487 while (i >= 0 && baseState[i] == 0) {
13488 i--;
13489 }
13490 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13491 return baseState;
13492 }
13493
13494 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013495 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13496 * on all Drawable objects associated with this view.
13497 */
13498 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013499 if (mBackground != null) {
13500 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013501 }
13502 }
13503
13504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013505 * Sets the background color for this view.
13506 * @param color the color of the background
13507 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013508 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013509 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013510 if (mBackground instanceof ColorDrawable) {
13511 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013512 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013513 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013515 }
13516
13517 /**
13518 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013519 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013520 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013522 * @attr ref android.R.styleable#View_background
13523 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013524 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 public void setBackgroundResource(int resid) {
13526 if (resid != 0 && resid == mBackgroundResource) {
13527 return;
13528 }
13529
13530 Drawable d= null;
13531 if (resid != 0) {
13532 d = mResources.getDrawable(resid);
13533 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013534 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013535
13536 mBackgroundResource = resid;
13537 }
13538
13539 /**
13540 * Set the background to a given Drawable, or remove the background. If the
13541 * background has padding, this View's padding is set to the background's
13542 * padding. However, when a background is removed, this View's padding isn't
13543 * touched. If setting the padding is desired, please use
13544 * {@link #setPadding(int, int, int, int)}.
13545 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013546 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013547 * background
13548 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013549 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013550 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013551 setBackgroundDrawable(background);
13552 }
13553
13554 /**
13555 * @deprecated use {@link #setBackground(Drawable)} instead
13556 */
13557 @Deprecated
13558 public void setBackgroundDrawable(Drawable background) {
13559 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013560 return;
13561 }
13562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013563 boolean requestLayout = false;
13564
13565 mBackgroundResource = 0;
13566
13567 /*
13568 * Regardless of whether we're setting a new background or not, we want
13569 * to clear the previous drawable.
13570 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013571 if (mBackground != null) {
13572 mBackground.setCallback(null);
13573 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013574 }
13575
Philip Milne6c8ea062012-04-03 17:38:43 -070013576 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013577 Rect padding = sThreadLocal.get();
13578 if (padding == null) {
13579 padding = new Rect();
13580 sThreadLocal.set(padding);
13581 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013582 if (background.getPadding(padding)) {
13583 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013584 case LAYOUT_DIRECTION_RTL:
13585 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13586 break;
13587 case LAYOUT_DIRECTION_LTR:
13588 default:
13589 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013591 }
13592
13593 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13594 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013595 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13596 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013597 requestLayout = true;
13598 }
13599
Philip Milne6c8ea062012-04-03 17:38:43 -070013600 background.setCallback(this);
13601 if (background.isStateful()) {
13602 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013604 background.setVisible(getVisibility() == VISIBLE, false);
13605 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606
13607 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13608 mPrivateFlags &= ~SKIP_DRAW;
13609 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13610 requestLayout = true;
13611 }
13612 } else {
13613 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013614 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013615
13616 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13617 /*
13618 * This view ONLY drew the background before and we're removing
13619 * the background, so now it won't draw anything
13620 * (hence we SKIP_DRAW)
13621 */
13622 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13623 mPrivateFlags |= SKIP_DRAW;
13624 }
13625
13626 /*
13627 * When the background is set, we try to apply its padding to this
13628 * View. When the background is removed, we don't touch this View's
13629 * padding. This is noted in the Javadocs. Hence, we don't need to
13630 * requestLayout(), the invalidate() below is sufficient.
13631 */
13632
13633 // The old background's minimum size could have affected this
13634 // View's layout, so let's requestLayout
13635 requestLayout = true;
13636 }
13637
Romain Guy8f1344f52009-05-15 16:03:59 -070013638 computeOpaqueFlags();
13639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 if (requestLayout) {
13641 requestLayout();
13642 }
13643
13644 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013645 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013646 }
13647
13648 /**
13649 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013651 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013652 *
13653 * @see #setBackground(Drawable)
13654 *
13655 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656 */
13657 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013658 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013659 }
13660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013661 /**
13662 * Sets the padding. The view may add on the space required to display
13663 * the scrollbars, depending on the style and visibility of the scrollbars.
13664 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13665 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13666 * from the values set in this call.
13667 *
13668 * @attr ref android.R.styleable#View_padding
13669 * @attr ref android.R.styleable#View_paddingBottom
13670 * @attr ref android.R.styleable#View_paddingLeft
13671 * @attr ref android.R.styleable#View_paddingRight
13672 * @attr ref android.R.styleable#View_paddingTop
13673 * @param left the left padding in pixels
13674 * @param top the top padding in pixels
13675 * @param right the right padding in pixels
13676 * @param bottom the bottom padding in pixels
13677 */
13678 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013679 mUserPaddingStart = -1;
13680 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013681 mUserPaddingRelative = false;
13682
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013683 internalSetPadding(left, top, right, bottom);
13684 }
13685
13686 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013687 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013688 mUserPaddingRight = right;
13689 mUserPaddingBottom = bottom;
13690
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013691 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013692 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013693
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013694 // Common case is there are no scroll bars.
13695 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013696 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013697 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013698 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013699 switch (mVerticalScrollbarPosition) {
13700 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013701 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13702 left += offset;
13703 } else {
13704 right += offset;
13705 }
13706 break;
Adam Powell20232d02010-12-08 21:08:53 -080013707 case SCROLLBAR_POSITION_RIGHT:
13708 right += offset;
13709 break;
13710 case SCROLLBAR_POSITION_LEFT:
13711 left += offset;
13712 break;
13713 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013714 }
Adam Powell20232d02010-12-08 21:08:53 -080013715 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013716 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13717 ? 0 : getHorizontalScrollbarHeight();
13718 }
13719 }
Romain Guy8506ab42009-06-11 17:35:47 -070013720
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013721 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013722 changed = true;
13723 mPaddingLeft = left;
13724 }
13725 if (mPaddingTop != top) {
13726 changed = true;
13727 mPaddingTop = top;
13728 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013729 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013730 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013731 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013732 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013733 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013734 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013735 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736 }
13737
13738 if (changed) {
13739 requestLayout();
13740 }
13741 }
13742
13743 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013744 * Sets the relative padding. The view may add on the space required to display
13745 * the scrollbars, depending on the style and visibility of the scrollbars.
13746 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13747 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13748 * from the values set in this call.
13749 *
13750 * @attr ref android.R.styleable#View_padding
13751 * @attr ref android.R.styleable#View_paddingBottom
13752 * @attr ref android.R.styleable#View_paddingStart
13753 * @attr ref android.R.styleable#View_paddingEnd
13754 * @attr ref android.R.styleable#View_paddingTop
13755 * @param start the start padding in pixels
13756 * @param top the top padding in pixels
13757 * @param end the end padding in pixels
13758 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013759 */
13760 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013761 mUserPaddingStart = start;
13762 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013763 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013764
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013765 switch(getResolvedLayoutDirection()) {
13766 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013767 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013768 break;
13769 case LAYOUT_DIRECTION_LTR:
13770 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013771 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013772 }
13773 }
13774
13775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013776 * Returns the top padding of this view.
13777 *
13778 * @return the top padding in pixels
13779 */
13780 public int getPaddingTop() {
13781 return mPaddingTop;
13782 }
13783
13784 /**
13785 * Returns the bottom padding of this view. If there are inset and enabled
13786 * scrollbars, this value may include the space required to display the
13787 * scrollbars as well.
13788 *
13789 * @return the bottom padding in pixels
13790 */
13791 public int getPaddingBottom() {
13792 return mPaddingBottom;
13793 }
13794
13795 /**
13796 * Returns the left padding of this view. If there are inset and enabled
13797 * scrollbars, this value may include the space required to display the
13798 * scrollbars as well.
13799 *
13800 * @return the left padding in pixels
13801 */
13802 public int getPaddingLeft() {
13803 return mPaddingLeft;
13804 }
13805
13806 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013807 * Returns the start padding of this view depending on its resolved layout direction.
13808 * If there are inset and enabled scrollbars, this value may include the space
13809 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013810 *
13811 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013812 */
13813 public int getPaddingStart() {
13814 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13815 mPaddingRight : mPaddingLeft;
13816 }
13817
13818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013819 * Returns the right padding of this view. If there are inset and enabled
13820 * scrollbars, this value may include the space required to display the
13821 * scrollbars as well.
13822 *
13823 * @return the right padding in pixels
13824 */
13825 public int getPaddingRight() {
13826 return mPaddingRight;
13827 }
13828
13829 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013830 * Returns the end padding of this view depending on its resolved layout direction.
13831 * If there are inset and enabled scrollbars, this value may include the space
13832 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013833 *
13834 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013835 */
13836 public int getPaddingEnd() {
13837 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13838 mPaddingLeft : mPaddingRight;
13839 }
13840
13841 /**
13842 * Return if the padding as been set thru relative values
13843 * {@link #setPaddingRelative(int, int, int, int)} or thru
13844 * @attr ref android.R.styleable#View_paddingStart or
13845 * @attr ref android.R.styleable#View_paddingEnd
13846 *
13847 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013848 */
13849 public boolean isPaddingRelative() {
13850 return mUserPaddingRelative;
13851 }
13852
13853 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013854 * @hide
13855 */
13856 public Insets getLayoutInsets() {
13857 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013858 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013859 }
13860 return mLayoutInsets;
13861 }
13862
13863 /**
13864 * @hide
13865 */
13866 public void setLayoutInsets(Insets layoutInsets) {
13867 mLayoutInsets = layoutInsets;
13868 }
13869
13870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013871 * Changes the selection state of this view. A view can be selected or not.
13872 * Note that selection is not the same as focus. Views are typically
13873 * selected in the context of an AdapterView like ListView or GridView;
13874 * the selected view is the view that is highlighted.
13875 *
13876 * @param selected true if the view must be selected, false otherwise
13877 */
13878 public void setSelected(boolean selected) {
13879 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13880 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013881 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013882 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013883 refreshDrawableState();
13884 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013885 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13886 notifyAccessibilityStateChanged();
13887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013888 }
13889 }
13890
13891 /**
13892 * Dispatch setSelected to all of this View's children.
13893 *
13894 * @see #setSelected(boolean)
13895 *
13896 * @param selected The new selected state
13897 */
13898 protected void dispatchSetSelected(boolean selected) {
13899 }
13900
13901 /**
13902 * Indicates the selection state of this view.
13903 *
13904 * @return true if the view is selected, false otherwise
13905 */
13906 @ViewDebug.ExportedProperty
13907 public boolean isSelected() {
13908 return (mPrivateFlags & SELECTED) != 0;
13909 }
13910
13911 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013912 * Changes the activated state of this view. A view can be activated or not.
13913 * Note that activation is not the same as selection. Selection is
13914 * a transient property, representing the view (hierarchy) the user is
13915 * currently interacting with. Activation is a longer-term state that the
13916 * user can move views in and out of. For example, in a list view with
13917 * single or multiple selection enabled, the views in the current selection
13918 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13919 * here.) The activated state is propagated down to children of the view it
13920 * is set on.
13921 *
13922 * @param activated true if the view must be activated, false otherwise
13923 */
13924 public void setActivated(boolean activated) {
13925 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13926 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013927 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013928 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013929 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013930 }
13931 }
13932
13933 /**
13934 * Dispatch setActivated to all of this View's children.
13935 *
13936 * @see #setActivated(boolean)
13937 *
13938 * @param activated The new activated state
13939 */
13940 protected void dispatchSetActivated(boolean activated) {
13941 }
13942
13943 /**
13944 * Indicates the activation state of this view.
13945 *
13946 * @return true if the view is activated, false otherwise
13947 */
13948 @ViewDebug.ExportedProperty
13949 public boolean isActivated() {
13950 return (mPrivateFlags & ACTIVATED) != 0;
13951 }
13952
13953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13955 * observer can be used to get notifications when global events, like
13956 * layout, happen.
13957 *
13958 * The returned ViewTreeObserver observer is not guaranteed to remain
13959 * valid for the lifetime of this View. If the caller of this method keeps
13960 * a long-lived reference to ViewTreeObserver, it should always check for
13961 * the return value of {@link ViewTreeObserver#isAlive()}.
13962 *
13963 * @return The ViewTreeObserver for this view's hierarchy.
13964 */
13965 public ViewTreeObserver getViewTreeObserver() {
13966 if (mAttachInfo != null) {
13967 return mAttachInfo.mTreeObserver;
13968 }
13969 if (mFloatingTreeObserver == null) {
13970 mFloatingTreeObserver = new ViewTreeObserver();
13971 }
13972 return mFloatingTreeObserver;
13973 }
13974
13975 /**
13976 * <p>Finds the topmost view in the current view hierarchy.</p>
13977 *
13978 * @return the topmost view containing this view
13979 */
13980 public View getRootView() {
13981 if (mAttachInfo != null) {
13982 final View v = mAttachInfo.mRootView;
13983 if (v != null) {
13984 return v;
13985 }
13986 }
Romain Guy8506ab42009-06-11 17:35:47 -070013987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013988 View parent = this;
13989
13990 while (parent.mParent != null && parent.mParent instanceof View) {
13991 parent = (View) parent.mParent;
13992 }
13993
13994 return parent;
13995 }
13996
13997 /**
13998 * <p>Computes the coordinates of this view on the screen. The argument
13999 * must be an array of two integers. After the method returns, the array
14000 * contains the x and y location in that order.</p>
14001 *
14002 * @param location an array of two integers in which to hold the coordinates
14003 */
14004 public void getLocationOnScreen(int[] location) {
14005 getLocationInWindow(location);
14006
14007 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014008 if (info != null) {
14009 location[0] += info.mWindowLeft;
14010 location[1] += info.mWindowTop;
14011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 }
14013
14014 /**
14015 * <p>Computes the coordinates of this view in its window. The argument
14016 * must be an array of two integers. After the method returns, the array
14017 * contains the x and y location in that order.</p>
14018 *
14019 * @param location an array of two integers in which to hold the coordinates
14020 */
14021 public void getLocationInWindow(int[] location) {
14022 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014023 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014024 }
14025
Gilles Debunne6583ce52011-12-06 18:09:02 -080014026 if (mAttachInfo == null) {
14027 // When the view is not attached to a window, this method does not make sense
14028 location[0] = location[1] = 0;
14029 return;
14030 }
14031
Gilles Debunnecea45132011-11-24 02:19:27 +010014032 float[] position = mAttachInfo.mTmpTransformLocation;
14033 position[0] = position[1] = 0.0f;
14034
14035 if (!hasIdentityMatrix()) {
14036 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014038
Gilles Debunnecea45132011-11-24 02:19:27 +010014039 position[0] += mLeft;
14040 position[1] += mTop;
14041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042 ViewParent viewParent = mParent;
14043 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014044 final View view = (View) viewParent;
14045
14046 position[0] -= view.mScrollX;
14047 position[1] -= view.mScrollY;
14048
14049 if (!view.hasIdentityMatrix()) {
14050 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014051 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014052
14053 position[0] += view.mLeft;
14054 position[1] += view.mTop;
14055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014056 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014057 }
Romain Guy8506ab42009-06-11 17:35:47 -070014058
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014059 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014061 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14062 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014063 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014064
14065 location[0] = (int) (position[0] + 0.5f);
14066 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 }
14068
14069 /**
14070 * {@hide}
14071 * @param id the id of the view to be found
14072 * @return the view of the specified id, null if cannot be found
14073 */
14074 protected View findViewTraversal(int id) {
14075 if (id == mID) {
14076 return this;
14077 }
14078 return null;
14079 }
14080
14081 /**
14082 * {@hide}
14083 * @param tag the tag of the view to be found
14084 * @return the view of specified tag, null if cannot be found
14085 */
14086 protected View findViewWithTagTraversal(Object tag) {
14087 if (tag != null && tag.equals(mTag)) {
14088 return this;
14089 }
14090 return null;
14091 }
14092
14093 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014094 * {@hide}
14095 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014096 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014097 * @return The first view that matches the predicate or null.
14098 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014099 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014100 if (predicate.apply(this)) {
14101 return this;
14102 }
14103 return null;
14104 }
14105
14106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014107 * Look for a child view with the given id. If this view has the given
14108 * id, return this view.
14109 *
14110 * @param id The id to search for.
14111 * @return The view that has the given id in the hierarchy or null
14112 */
14113 public final View findViewById(int id) {
14114 if (id < 0) {
14115 return null;
14116 }
14117 return findViewTraversal(id);
14118 }
14119
14120 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014121 * Finds a view by its unuque and stable accessibility id.
14122 *
14123 * @param accessibilityId The searched accessibility id.
14124 * @return The found view.
14125 */
14126 final View findViewByAccessibilityId(int accessibilityId) {
14127 if (accessibilityId < 0) {
14128 return null;
14129 }
14130 return findViewByAccessibilityIdTraversal(accessibilityId);
14131 }
14132
14133 /**
14134 * Performs the traversal to find a view by its unuque and stable accessibility id.
14135 *
14136 * <strong>Note:</strong>This method does not stop at the root namespace
14137 * boundary since the user can touch the screen at an arbitrary location
14138 * potentially crossing the root namespace bounday which will send an
14139 * accessibility event to accessibility services and they should be able
14140 * to obtain the event source. Also accessibility ids are guaranteed to be
14141 * unique in the window.
14142 *
14143 * @param accessibilityId The accessibility id.
14144 * @return The found view.
14145 */
14146 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14147 if (getAccessibilityViewId() == accessibilityId) {
14148 return this;
14149 }
14150 return null;
14151 }
14152
14153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 * Look for a child view with the given tag. If this view has the given
14155 * tag, return this view.
14156 *
14157 * @param tag The tag to search for, using "tag.equals(getTag())".
14158 * @return The View that has the given tag in the hierarchy or null
14159 */
14160 public final View findViewWithTag(Object tag) {
14161 if (tag == null) {
14162 return null;
14163 }
14164 return findViewWithTagTraversal(tag);
14165 }
14166
14167 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014168 * {@hide}
14169 * Look for a child view that matches the specified predicate.
14170 * If this view matches the predicate, return this view.
14171 *
14172 * @param predicate The predicate to evaluate.
14173 * @return The first view that matches the predicate or null.
14174 */
14175 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014176 return findViewByPredicateTraversal(predicate, null);
14177 }
14178
14179 /**
14180 * {@hide}
14181 * Look for a child view that matches the specified predicate,
14182 * starting with the specified view and its descendents and then
14183 * recusively searching the ancestors and siblings of that view
14184 * until this view is reached.
14185 *
14186 * This method is useful in cases where the predicate does not match
14187 * a single unique view (perhaps multiple views use the same id)
14188 * and we are trying to find the view that is "closest" in scope to the
14189 * starting view.
14190 *
14191 * @param start The view to start from.
14192 * @param predicate The predicate to evaluate.
14193 * @return The first view that matches the predicate or null.
14194 */
14195 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14196 View childToSkip = null;
14197 for (;;) {
14198 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14199 if (view != null || start == this) {
14200 return view;
14201 }
14202
14203 ViewParent parent = start.getParent();
14204 if (parent == null || !(parent instanceof View)) {
14205 return null;
14206 }
14207
14208 childToSkip = start;
14209 start = (View) parent;
14210 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014211 }
14212
14213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014214 * Sets the identifier for this view. The identifier does not have to be
14215 * unique in this view's hierarchy. The identifier should be a positive
14216 * number.
14217 *
14218 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014219 * @see #getId()
14220 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014221 *
14222 * @param id a number used to identify the view
14223 *
14224 * @attr ref android.R.styleable#View_id
14225 */
14226 public void setId(int id) {
14227 mID = id;
14228 }
14229
14230 /**
14231 * {@hide}
14232 *
14233 * @param isRoot true if the view belongs to the root namespace, false
14234 * otherwise
14235 */
14236 public void setIsRootNamespace(boolean isRoot) {
14237 if (isRoot) {
14238 mPrivateFlags |= IS_ROOT_NAMESPACE;
14239 } else {
14240 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14241 }
14242 }
14243
14244 /**
14245 * {@hide}
14246 *
14247 * @return true if the view belongs to the root namespace, false otherwise
14248 */
14249 public boolean isRootNamespace() {
14250 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14251 }
14252
14253 /**
14254 * Returns this view's identifier.
14255 *
14256 * @return a positive integer used to identify the view or {@link #NO_ID}
14257 * if the view has no ID
14258 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014259 * @see #setId(int)
14260 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014261 * @attr ref android.R.styleable#View_id
14262 */
14263 @ViewDebug.CapturedViewProperty
14264 public int getId() {
14265 return mID;
14266 }
14267
14268 /**
14269 * Returns this view's tag.
14270 *
14271 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014272 *
14273 * @see #setTag(Object)
14274 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014275 */
14276 @ViewDebug.ExportedProperty
14277 public Object getTag() {
14278 return mTag;
14279 }
14280
14281 /**
14282 * Sets the tag associated with this view. A tag can be used to mark
14283 * a view in its hierarchy and does not have to be unique within the
14284 * hierarchy. Tags can also be used to store data within a view without
14285 * resorting to another data structure.
14286 *
14287 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014288 *
14289 * @see #getTag()
14290 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014291 */
14292 public void setTag(final Object tag) {
14293 mTag = tag;
14294 }
14295
14296 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014297 * Returns the tag associated with this view and the specified key.
14298 *
14299 * @param key The key identifying the tag
14300 *
14301 * @return the Object stored in this view as a tag
14302 *
14303 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014304 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014305 */
14306 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014307 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014308 return null;
14309 }
14310
14311 /**
14312 * Sets a tag associated with this view and a key. A tag can be used
14313 * to mark a view in its hierarchy and does not have to be unique within
14314 * the hierarchy. Tags can also be used to store data within a view
14315 * without resorting to another data structure.
14316 *
14317 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014318 * application to ensure it is unique (see the <a
14319 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14320 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014321 * the Android framework or not associated with any package will cause
14322 * an {@link IllegalArgumentException} to be thrown.
14323 *
14324 * @param key The key identifying the tag
14325 * @param tag An Object to tag the view with
14326 *
14327 * @throws IllegalArgumentException If they specified key is not valid
14328 *
14329 * @see #setTag(Object)
14330 * @see #getTag(int)
14331 */
14332 public void setTag(int key, final Object tag) {
14333 // If the package id is 0x00 or 0x01, it's either an undefined package
14334 // or a framework id
14335 if ((key >>> 24) < 2) {
14336 throw new IllegalArgumentException("The key must be an application-specific "
14337 + "resource id.");
14338 }
14339
Adam Powell2b2f6d62011-09-23 11:15:39 -070014340 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014341 }
14342
14343 /**
14344 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14345 * framework id.
14346 *
14347 * @hide
14348 */
14349 public void setTagInternal(int key, Object tag) {
14350 if ((key >>> 24) != 0x1) {
14351 throw new IllegalArgumentException("The key must be a framework-specific "
14352 + "resource id.");
14353 }
14354
Adam Powell2b2f6d62011-09-23 11:15:39 -070014355 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014356 }
14357
Adam Powell2b2f6d62011-09-23 11:15:39 -070014358 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014359 if (mKeyedTags == null) {
14360 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014361 }
14362
Adam Powell7db82ac2011-09-22 19:44:04 -070014363 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014364 }
14365
14366 /**
Romain Guy13922e02009-05-12 17:56:14 -070014367 * @param consistency The type of consistency. See ViewDebug for more information.
14368 *
14369 * @hide
14370 */
14371 protected boolean dispatchConsistencyCheck(int consistency) {
14372 return onConsistencyCheck(consistency);
14373 }
14374
14375 /**
14376 * Method that subclasses should implement to check their consistency. The type of
14377 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014378 *
Romain Guy13922e02009-05-12 17:56:14 -070014379 * @param consistency The type of consistency. See ViewDebug for more information.
14380 *
14381 * @throws IllegalStateException if the view is in an inconsistent state.
14382 *
14383 * @hide
14384 */
14385 protected boolean onConsistencyCheck(int consistency) {
14386 boolean result = true;
14387
14388 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14389 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14390
14391 if (checkLayout) {
14392 if (getParent() == null) {
14393 result = false;
14394 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14395 "View " + this + " does not have a parent.");
14396 }
14397
14398 if (mAttachInfo == null) {
14399 result = false;
14400 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14401 "View " + this + " is not attached to a window.");
14402 }
14403 }
14404
14405 if (checkDrawing) {
14406 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14407 // from their draw() method
14408
14409 if ((mPrivateFlags & DRAWN) != DRAWN &&
14410 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14411 result = false;
14412 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14413 "View " + this + " was invalidated but its drawing cache is valid.");
14414 }
14415 }
14416
14417 return result;
14418 }
14419
14420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 * Prints information about this view in the log output, with the tag
14422 * {@link #VIEW_LOG_TAG}.
14423 *
14424 * @hide
14425 */
14426 public void debug() {
14427 debug(0);
14428 }
14429
14430 /**
14431 * Prints information about this view in the log output, with the tag
14432 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14433 * indentation defined by the <code>depth</code>.
14434 *
14435 * @param depth the indentation level
14436 *
14437 * @hide
14438 */
14439 protected void debug(int depth) {
14440 String output = debugIndent(depth - 1);
14441
14442 output += "+ " + this;
14443 int id = getId();
14444 if (id != -1) {
14445 output += " (id=" + id + ")";
14446 }
14447 Object tag = getTag();
14448 if (tag != null) {
14449 output += " (tag=" + tag + ")";
14450 }
14451 Log.d(VIEW_LOG_TAG, output);
14452
14453 if ((mPrivateFlags & FOCUSED) != 0) {
14454 output = debugIndent(depth) + " FOCUSED";
14455 Log.d(VIEW_LOG_TAG, output);
14456 }
14457
14458 output = debugIndent(depth);
14459 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14460 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14461 + "} ";
14462 Log.d(VIEW_LOG_TAG, output);
14463
14464 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14465 || mPaddingBottom != 0) {
14466 output = debugIndent(depth);
14467 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14468 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14469 Log.d(VIEW_LOG_TAG, output);
14470 }
14471
14472 output = debugIndent(depth);
14473 output += "mMeasureWidth=" + mMeasuredWidth +
14474 " mMeasureHeight=" + mMeasuredHeight;
14475 Log.d(VIEW_LOG_TAG, output);
14476
14477 output = debugIndent(depth);
14478 if (mLayoutParams == null) {
14479 output += "BAD! no layout params";
14480 } else {
14481 output = mLayoutParams.debug(output);
14482 }
14483 Log.d(VIEW_LOG_TAG, output);
14484
14485 output = debugIndent(depth);
14486 output += "flags={";
14487 output += View.printFlags(mViewFlags);
14488 output += "}";
14489 Log.d(VIEW_LOG_TAG, output);
14490
14491 output = debugIndent(depth);
14492 output += "privateFlags={";
14493 output += View.printPrivateFlags(mPrivateFlags);
14494 output += "}";
14495 Log.d(VIEW_LOG_TAG, output);
14496 }
14497
14498 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014499 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014500 *
14501 * @param depth the indentation level
14502 * @return a String containing (depth * 2 + 3) * 2 white spaces
14503 *
14504 * @hide
14505 */
14506 protected static String debugIndent(int depth) {
14507 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14508 for (int i = 0; i < (depth * 2) + 3; i++) {
14509 spaces.append(' ').append(' ');
14510 }
14511 return spaces.toString();
14512 }
14513
14514 /**
14515 * <p>Return the offset of the widget's text baseline from the widget's top
14516 * boundary. If this widget does not support baseline alignment, this
14517 * method returns -1. </p>
14518 *
14519 * @return the offset of the baseline within the widget's bounds or -1
14520 * if baseline alignment is not supported
14521 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014522 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014523 public int getBaseline() {
14524 return -1;
14525 }
14526
14527 /**
14528 * Call this when something has changed which has invalidated the
14529 * layout of this view. This will schedule a layout pass of the view
14530 * tree.
14531 */
14532 public void requestLayout() {
14533 if (ViewDebug.TRACE_HIERARCHY) {
14534 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14535 }
14536
14537 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014538 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014539
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014540 if (mLayoutParams != null) {
14541 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14542 }
14543
14544 if (mParent != null && !mParent.isLayoutRequested()) {
14545 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 }
14547 }
14548
14549 /**
14550 * Forces this view to be laid out during the next layout pass.
14551 * This method does not call requestLayout() or forceLayout()
14552 * on the parent.
14553 */
14554 public void forceLayout() {
14555 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014556 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014557 }
14558
14559 /**
14560 * <p>
14561 * This is called to find out how big a view should be. The parent
14562 * supplies constraint information in the width and height parameters.
14563 * </p>
14564 *
14565 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014566 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014567 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014568 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 * </p>
14570 *
14571 *
14572 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14573 * parent
14574 * @param heightMeasureSpec Vertical space requirements as imposed by the
14575 * parent
14576 *
14577 * @see #onMeasure(int, int)
14578 */
14579 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14580 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14581 widthMeasureSpec != mOldWidthMeasureSpec ||
14582 heightMeasureSpec != mOldHeightMeasureSpec) {
14583
14584 // first clears the measured dimension flag
14585 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14586
14587 if (ViewDebug.TRACE_HIERARCHY) {
14588 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14589 }
14590
14591 // measure ourselves, this should set the measured dimension flag back
14592 onMeasure(widthMeasureSpec, heightMeasureSpec);
14593
14594 // flag not set, setMeasuredDimension() was not invoked, we raise
14595 // an exception to warn the developer
14596 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14597 throw new IllegalStateException("onMeasure() did not set the"
14598 + " measured dimension by calling"
14599 + " setMeasuredDimension()");
14600 }
14601
14602 mPrivateFlags |= LAYOUT_REQUIRED;
14603 }
14604
14605 mOldWidthMeasureSpec = widthMeasureSpec;
14606 mOldHeightMeasureSpec = heightMeasureSpec;
14607 }
14608
14609 /**
14610 * <p>
14611 * Measure the view and its content to determine the measured width and the
14612 * measured height. This method is invoked by {@link #measure(int, int)} and
14613 * should be overriden by subclasses to provide accurate and efficient
14614 * measurement of their contents.
14615 * </p>
14616 *
14617 * <p>
14618 * <strong>CONTRACT:</strong> When overriding this method, you
14619 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14620 * measured width and height of this view. Failure to do so will trigger an
14621 * <code>IllegalStateException</code>, thrown by
14622 * {@link #measure(int, int)}. Calling the superclass'
14623 * {@link #onMeasure(int, int)} is a valid use.
14624 * </p>
14625 *
14626 * <p>
14627 * The base class implementation of measure defaults to the background size,
14628 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14629 * override {@link #onMeasure(int, int)} to provide better measurements of
14630 * their content.
14631 * </p>
14632 *
14633 * <p>
14634 * If this method is overridden, it is the subclass's responsibility to make
14635 * sure the measured height and width are at least the view's minimum height
14636 * and width ({@link #getSuggestedMinimumHeight()} and
14637 * {@link #getSuggestedMinimumWidth()}).
14638 * </p>
14639 *
14640 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14641 * The requirements are encoded with
14642 * {@link android.view.View.MeasureSpec}.
14643 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14644 * The requirements are encoded with
14645 * {@link android.view.View.MeasureSpec}.
14646 *
14647 * @see #getMeasuredWidth()
14648 * @see #getMeasuredHeight()
14649 * @see #setMeasuredDimension(int, int)
14650 * @see #getSuggestedMinimumHeight()
14651 * @see #getSuggestedMinimumWidth()
14652 * @see android.view.View.MeasureSpec#getMode(int)
14653 * @see android.view.View.MeasureSpec#getSize(int)
14654 */
14655 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14656 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14657 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14658 }
14659
14660 /**
14661 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14662 * measured width and measured height. Failing to do so will trigger an
14663 * exception at measurement time.</p>
14664 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014665 * @param measuredWidth The measured width of this view. May be a complex
14666 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14667 * {@link #MEASURED_STATE_TOO_SMALL}.
14668 * @param measuredHeight The measured height of this view. May be a complex
14669 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14670 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014671 */
14672 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14673 mMeasuredWidth = measuredWidth;
14674 mMeasuredHeight = measuredHeight;
14675
14676 mPrivateFlags |= MEASURED_DIMENSION_SET;
14677 }
14678
14679 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014680 * Merge two states as returned by {@link #getMeasuredState()}.
14681 * @param curState The current state as returned from a view or the result
14682 * of combining multiple views.
14683 * @param newState The new view state to combine.
14684 * @return Returns a new integer reflecting the combination of the two
14685 * states.
14686 */
14687 public static int combineMeasuredStates(int curState, int newState) {
14688 return curState | newState;
14689 }
14690
14691 /**
14692 * Version of {@link #resolveSizeAndState(int, int, int)}
14693 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14694 */
14695 public static int resolveSize(int size, int measureSpec) {
14696 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14697 }
14698
14699 /**
14700 * Utility to reconcile a desired size and state, with constraints imposed
14701 * by a MeasureSpec. Will take the desired size, unless a different size
14702 * is imposed by the constraints. The returned value is a compound integer,
14703 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14704 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14705 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014706 *
14707 * @param size How big the view wants to be
14708 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014709 * @return Size information bit mask as defined by
14710 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014711 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014712 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014713 int result = size;
14714 int specMode = MeasureSpec.getMode(measureSpec);
14715 int specSize = MeasureSpec.getSize(measureSpec);
14716 switch (specMode) {
14717 case MeasureSpec.UNSPECIFIED:
14718 result = size;
14719 break;
14720 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014721 if (specSize < size) {
14722 result = specSize | MEASURED_STATE_TOO_SMALL;
14723 } else {
14724 result = size;
14725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014726 break;
14727 case MeasureSpec.EXACTLY:
14728 result = specSize;
14729 break;
14730 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014731 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014732 }
14733
14734 /**
14735 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014736 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014737 * by the MeasureSpec.
14738 *
14739 * @param size Default size for this view
14740 * @param measureSpec Constraints imposed by the parent
14741 * @return The size this view should be.
14742 */
14743 public static int getDefaultSize(int size, int measureSpec) {
14744 int result = size;
14745 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014746 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014747
14748 switch (specMode) {
14749 case MeasureSpec.UNSPECIFIED:
14750 result = size;
14751 break;
14752 case MeasureSpec.AT_MOST:
14753 case MeasureSpec.EXACTLY:
14754 result = specSize;
14755 break;
14756 }
14757 return result;
14758 }
14759
14760 /**
14761 * Returns the suggested minimum height that the view should use. This
14762 * returns the maximum of the view's minimum height
14763 * and the background's minimum height
14764 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14765 * <p>
14766 * When being used in {@link #onMeasure(int, int)}, the caller should still
14767 * ensure the returned height is within the requirements of the parent.
14768 *
14769 * @return The suggested minimum height of the view.
14770 */
14771 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014772 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014774 }
14775
14776 /**
14777 * Returns the suggested minimum width that the view should use. This
14778 * returns the maximum of the view's minimum width)
14779 * and the background's minimum width
14780 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14781 * <p>
14782 * When being used in {@link #onMeasure(int, int)}, the caller should still
14783 * ensure the returned width is within the requirements of the parent.
14784 *
14785 * @return The suggested minimum width of the view.
14786 */
14787 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014788 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014790
Philip Milne6c8ea062012-04-03 17:38:43 -070014791 /**
14792 * Returns the minimum height of the view.
14793 *
14794 * @return the minimum height the view will try to be.
14795 *
14796 * @see #setMinimumHeight(int)
14797 *
14798 * @attr ref android.R.styleable#View_minHeight
14799 */
14800 public int getMinimumHeight() {
14801 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014802 }
14803
14804 /**
14805 * Sets the minimum height of the view. It is not guaranteed the view will
14806 * be able to achieve this minimum height (for example, if its parent layout
14807 * constrains it with less available height).
14808 *
14809 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014810 *
14811 * @see #getMinimumHeight()
14812 *
14813 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014814 */
14815 public void setMinimumHeight(int minHeight) {
14816 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014817 requestLayout();
14818 }
14819
14820 /**
14821 * Returns the minimum width of the view.
14822 *
14823 * @return the minimum width the view will try to be.
14824 *
14825 * @see #setMinimumWidth(int)
14826 *
14827 * @attr ref android.R.styleable#View_minWidth
14828 */
14829 public int getMinimumWidth() {
14830 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014831 }
14832
14833 /**
14834 * Sets the minimum width of the view. It is not guaranteed the view will
14835 * be able to achieve this minimum width (for example, if its parent layout
14836 * constrains it with less available width).
14837 *
14838 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014839 *
14840 * @see #getMinimumWidth()
14841 *
14842 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843 */
14844 public void setMinimumWidth(int minWidth) {
14845 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014846 requestLayout();
14847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 }
14849
14850 /**
14851 * Get the animation currently associated with this view.
14852 *
14853 * @return The animation that is currently playing or
14854 * scheduled to play for this view.
14855 */
14856 public Animation getAnimation() {
14857 return mCurrentAnimation;
14858 }
14859
14860 /**
14861 * Start the specified animation now.
14862 *
14863 * @param animation the animation to start now
14864 */
14865 public void startAnimation(Animation animation) {
14866 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14867 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014868 invalidateParentCaches();
14869 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014870 }
14871
14872 /**
14873 * Cancels any animations for this view.
14874 */
14875 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014876 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014877 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014879 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014880 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014881 }
14882
14883 /**
14884 * Sets the next animation to play for this view.
14885 * If you want the animation to play immediately, use
14886 * startAnimation. This method provides allows fine-grained
14887 * control over the start time and invalidation, but you
14888 * must make sure that 1) the animation has a start time set, and
14889 * 2) the view will be invalidated when the animation is supposed to
14890 * start.
14891 *
14892 * @param animation The next animation, or null.
14893 */
14894 public void setAnimation(Animation animation) {
14895 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014897 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014898 // If the screen is off assume the animation start time is now instead of
14899 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14900 // would cause the animation to start when the screen turns back on
14901 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14902 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14903 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014905 animation.reset();
14906 }
14907 }
14908
14909 /**
14910 * Invoked by a parent ViewGroup to notify the start of the animation
14911 * currently associated with this view. If you override this method,
14912 * always call super.onAnimationStart();
14913 *
14914 * @see #setAnimation(android.view.animation.Animation)
14915 * @see #getAnimation()
14916 */
14917 protected void onAnimationStart() {
14918 mPrivateFlags |= ANIMATION_STARTED;
14919 }
14920
14921 /**
14922 * Invoked by a parent ViewGroup to notify the end of the animation
14923 * currently associated with this view. If you override this method,
14924 * always call super.onAnimationEnd();
14925 *
14926 * @see #setAnimation(android.view.animation.Animation)
14927 * @see #getAnimation()
14928 */
14929 protected void onAnimationEnd() {
14930 mPrivateFlags &= ~ANIMATION_STARTED;
14931 }
14932
14933 /**
14934 * Invoked if there is a Transform that involves alpha. Subclass that can
14935 * draw themselves with the specified alpha should return true, and then
14936 * respect that alpha when their onDraw() is called. If this returns false
14937 * then the view may be redirected to draw into an offscreen buffer to
14938 * fulfill the request, which will look fine, but may be slower than if the
14939 * subclass handles it internally. The default implementation returns false.
14940 *
14941 * @param alpha The alpha (0..255) to apply to the view's drawing
14942 * @return true if the view can draw with the specified alpha.
14943 */
14944 protected boolean onSetAlpha(int alpha) {
14945 return false;
14946 }
14947
14948 /**
14949 * This is used by the RootView to perform an optimization when
14950 * the view hierarchy contains one or several SurfaceView.
14951 * SurfaceView is always considered transparent, but its children are not,
14952 * therefore all View objects remove themselves from the global transparent
14953 * region (passed as a parameter to this function).
14954 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014955 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014956 *
14957 * @return Returns true if the effective visibility of the view at this
14958 * point is opaque, regardless of the transparent region; returns false
14959 * if it is possible for underlying windows to be seen behind the view.
14960 *
14961 * {@hide}
14962 */
14963 public boolean gatherTransparentRegion(Region region) {
14964 final AttachInfo attachInfo = mAttachInfo;
14965 if (region != null && attachInfo != null) {
14966 final int pflags = mPrivateFlags;
14967 if ((pflags & SKIP_DRAW) == 0) {
14968 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14969 // remove it from the transparent region.
14970 final int[] location = attachInfo.mTransparentLocation;
14971 getLocationInWindow(location);
14972 region.op(location[0], location[1], location[0] + mRight - mLeft,
14973 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014974 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014975 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14976 // exists, so we remove the background drawable's non-transparent
14977 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014978 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014979 }
14980 }
14981 return true;
14982 }
14983
14984 /**
14985 * Play a sound effect for this view.
14986 *
14987 * <p>The framework will play sound effects for some built in actions, such as
14988 * clicking, but you may wish to play these effects in your widget,
14989 * for instance, for internal navigation.
14990 *
14991 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14992 * {@link #isSoundEffectsEnabled()} is true.
14993 *
14994 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14995 */
14996 public void playSoundEffect(int soundConstant) {
14997 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14998 return;
14999 }
15000 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15001 }
15002
15003 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015004 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015005 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015006 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015007 *
15008 * <p>The framework will provide haptic feedback for some built in actions,
15009 * such as long presses, but you may wish to provide feedback for your
15010 * own widget.
15011 *
15012 * <p>The feedback will only be performed if
15013 * {@link #isHapticFeedbackEnabled()} is true.
15014 *
15015 * @param feedbackConstant One of the constants defined in
15016 * {@link HapticFeedbackConstants}
15017 */
15018 public boolean performHapticFeedback(int feedbackConstant) {
15019 return performHapticFeedback(feedbackConstant, 0);
15020 }
15021
15022 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015023 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015024 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015025 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015026 *
15027 * @param feedbackConstant One of the constants defined in
15028 * {@link HapticFeedbackConstants}
15029 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15030 */
15031 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15032 if (mAttachInfo == null) {
15033 return false;
15034 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015035 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015036 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015037 && !isHapticFeedbackEnabled()) {
15038 return false;
15039 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015040 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15041 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015042 }
15043
15044 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015045 * Request that the visibility of the status bar or other screen/window
15046 * decorations be changed.
15047 *
15048 * <p>This method is used to put the over device UI into temporary modes
15049 * where the user's attention is focused more on the application content,
15050 * by dimming or hiding surrounding system affordances. This is typically
15051 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15052 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15053 * to be placed behind the action bar (and with these flags other system
15054 * affordances) so that smooth transitions between hiding and showing them
15055 * can be done.
15056 *
15057 * <p>Two representative examples of the use of system UI visibility is
15058 * implementing a content browsing application (like a magazine reader)
15059 * and a video playing application.
15060 *
15061 * <p>The first code shows a typical implementation of a View in a content
15062 * browsing application. In this implementation, the application goes
15063 * into a content-oriented mode by hiding the status bar and action bar,
15064 * and putting the navigation elements into lights out mode. The user can
15065 * then interact with content while in this mode. Such an application should
15066 * provide an easy way for the user to toggle out of the mode (such as to
15067 * check information in the status bar or access notifications). In the
15068 * implementation here, this is done simply by tapping on the content.
15069 *
15070 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15071 * content}
15072 *
15073 * <p>This second code sample shows a typical implementation of a View
15074 * in a video playing application. In this situation, while the video is
15075 * playing the application would like to go into a complete full-screen mode,
15076 * to use as much of the display as possible for the video. When in this state
15077 * the user can not interact with the application; the system intercepts
15078 * touching on the screen to pop the UI out of full screen mode.
15079 *
15080 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15081 * content}
15082 *
15083 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15084 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15085 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15086 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015087 */
15088 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015089 if (visibility != mSystemUiVisibility) {
15090 mSystemUiVisibility = visibility;
15091 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15092 mParent.recomputeViewAttributes(this);
15093 }
Joe Onorato664644d2011-01-23 17:53:23 -080015094 }
15095 }
15096
15097 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015098 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15099 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15100 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15101 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15102 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015103 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015104 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015105 return mSystemUiVisibility;
15106 }
15107
Scott Mainec6331b2011-05-24 16:55:56 -070015108 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015109 * Returns the current system UI visibility that is currently set for
15110 * the entire window. This is the combination of the
15111 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15112 * views in the window.
15113 */
15114 public int getWindowSystemUiVisibility() {
15115 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15116 }
15117
15118 /**
15119 * Override to find out when the window's requested system UI visibility
15120 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15121 * This is different from the callbacks recieved through
15122 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15123 * in that this is only telling you about the local request of the window,
15124 * not the actual values applied by the system.
15125 */
15126 public void onWindowSystemUiVisibilityChanged(int visible) {
15127 }
15128
15129 /**
15130 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15131 * the view hierarchy.
15132 */
15133 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15134 onWindowSystemUiVisibilityChanged(visible);
15135 }
15136
15137 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015138 * Set a listener to receive callbacks when the visibility of the system bar changes.
15139 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15140 */
Joe Onorato664644d2011-01-23 17:53:23 -080015141 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015142 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015143 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15144 mParent.recomputeViewAttributes(this);
15145 }
15146 }
15147
15148 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015149 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15150 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015151 */
15152 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015153 ListenerInfo li = mListenerInfo;
15154 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15155 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015156 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015157 }
15158 }
15159
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015160 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15161 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15162 if (val != mSystemUiVisibility) {
15163 setSystemUiVisibility(val);
15164 }
15165 }
15166
Joe Onorato664644d2011-01-23 17:53:23 -080015167 /**
Joe Malin32736f02011-01-19 16:14:20 -080015168 * Creates an image that the system displays during the drag and drop
15169 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15170 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15171 * appearance as the given View. The default also positions the center of the drag shadow
15172 * directly under the touch point. If no View is provided (the constructor with no parameters
15173 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15174 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15175 * default is an invisible drag shadow.
15176 * <p>
15177 * You are not required to use the View you provide to the constructor as the basis of the
15178 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15179 * anything you want as the drag shadow.
15180 * </p>
15181 * <p>
15182 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15183 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15184 * size and position of the drag shadow. It uses this data to construct a
15185 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15186 * so that your application can draw the shadow image in the Canvas.
15187 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015188 *
15189 * <div class="special reference">
15190 * <h3>Developer Guides</h3>
15191 * <p>For a guide to implementing drag and drop features, read the
15192 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15193 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015194 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015195 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015196 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015197
15198 /**
Joe Malin32736f02011-01-19 16:14:20 -080015199 * Constructs a shadow image builder based on a View. By default, the resulting drag
15200 * shadow will have the same appearance and dimensions as the View, with the touch point
15201 * over the center of the View.
15202 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015203 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015204 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015205 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015206 }
15207
Christopher Tate17ed60c2011-01-18 12:50:26 -080015208 /**
15209 * Construct a shadow builder object with no associated View. This
15210 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15211 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15212 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015213 * reference to any View object. If they are not overridden, then the result is an
15214 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015215 */
15216 public DragShadowBuilder() {
15217 mView = new WeakReference<View>(null);
15218 }
15219
15220 /**
15221 * Returns the View object that had been passed to the
15222 * {@link #View.DragShadowBuilder(View)}
15223 * constructor. If that View parameter was {@code null} or if the
15224 * {@link #View.DragShadowBuilder()}
15225 * constructor was used to instantiate the builder object, this method will return
15226 * null.
15227 *
15228 * @return The View object associate with this builder object.
15229 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015230 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015231 final public View getView() {
15232 return mView.get();
15233 }
15234
Christopher Tate2c095f32010-10-04 14:13:40 -070015235 /**
Joe Malin32736f02011-01-19 16:14:20 -080015236 * Provides the metrics for the shadow image. These include the dimensions of
15237 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015238 * be centered under the touch location while dragging.
15239 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015240 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015241 * same as the dimensions of the View itself and centers the shadow under
15242 * the touch point.
15243 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015244 *
Joe Malin32736f02011-01-19 16:14:20 -080015245 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15246 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15247 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15248 * image.
15249 *
15250 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15251 * shadow image that should be underneath the touch point during the drag and drop
15252 * operation. Your application must set {@link android.graphics.Point#x} to the
15253 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015254 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015255 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015256 final View view = mView.get();
15257 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015258 shadowSize.set(view.getWidth(), view.getHeight());
15259 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015260 } else {
15261 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15262 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015263 }
15264
15265 /**
Joe Malin32736f02011-01-19 16:14:20 -080015266 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15267 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015268 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015269 *
Joe Malin32736f02011-01-19 16:14:20 -080015270 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015271 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015272 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015273 final View view = mView.get();
15274 if (view != null) {
15275 view.draw(canvas);
15276 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015277 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015278 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015279 }
15280 }
15281
15282 /**
Joe Malin32736f02011-01-19 16:14:20 -080015283 * Starts a drag and drop operation. When your application calls this method, it passes a
15284 * {@link android.view.View.DragShadowBuilder} object to the system. The
15285 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15286 * to get metrics for the drag shadow, and then calls the object's
15287 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15288 * <p>
15289 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15290 * drag events to all the View objects in your application that are currently visible. It does
15291 * this either by calling the View object's drag listener (an implementation of
15292 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15293 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15294 * Both are passed a {@link android.view.DragEvent} object that has a
15295 * {@link android.view.DragEvent#getAction()} value of
15296 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15297 * </p>
15298 * <p>
15299 * Your application can invoke startDrag() on any attached View object. The View object does not
15300 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15301 * be related to the View the user selected for dragging.
15302 * </p>
15303 * @param data A {@link android.content.ClipData} object pointing to the data to be
15304 * transferred by the drag and drop operation.
15305 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15306 * drag shadow.
15307 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15308 * drop operation. This Object is put into every DragEvent object sent by the system during the
15309 * current drag.
15310 * <p>
15311 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15312 * to the target Views. For example, it can contain flags that differentiate between a
15313 * a copy operation and a move operation.
15314 * </p>
15315 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15316 * so the parameter should be set to 0.
15317 * @return {@code true} if the method completes successfully, or
15318 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15319 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015320 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015321 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015322 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015323 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015324 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015325 }
15326 boolean okay = false;
15327
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015328 Point shadowSize = new Point();
15329 Point shadowTouchPoint = new Point();
15330 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015331
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015332 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15333 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15334 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015335 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015336
Chris Tatea32dcf72010-10-14 12:13:50 -070015337 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015338 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15339 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015340 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015341 Surface surface = new Surface();
15342 try {
15343 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015344 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015345 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015346 + " surface=" + surface);
15347 if (token != null) {
15348 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015349 try {
Chris Tate6b391282010-10-14 15:48:59 -070015350 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015351 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015352 } finally {
15353 surface.unlockCanvasAndPost(canvas);
15354 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015355
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015356 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015357
15358 // Cache the local state object for delivery with DragEvents
15359 root.setLocalDragState(myLocalState);
15360
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015361 // repurpose 'shadowSize' for the last touch point
15362 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015363
Christopher Tatea53146c2010-09-07 11:57:52 -070015364 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015365 shadowSize.x, shadowSize.y,
15366 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015367 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015368
15369 // Off and running! Release our local surface instance; the drag
15370 // shadow surface is now managed by the system process.
15371 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015372 }
15373 } catch (Exception e) {
15374 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15375 surface.destroy();
15376 }
15377
15378 return okay;
15379 }
15380
Christopher Tatea53146c2010-09-07 11:57:52 -070015381 /**
Joe Malin32736f02011-01-19 16:14:20 -080015382 * Handles drag events sent by the system following a call to
15383 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15384 *<p>
15385 * When the system calls this method, it passes a
15386 * {@link android.view.DragEvent} object. A call to
15387 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15388 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15389 * operation.
15390 * @param event The {@link android.view.DragEvent} sent by the system.
15391 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15392 * in DragEvent, indicating the type of drag event represented by this object.
15393 * @return {@code true} if the method was successful, otherwise {@code false}.
15394 * <p>
15395 * The method should return {@code true} in response to an action type of
15396 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15397 * operation.
15398 * </p>
15399 * <p>
15400 * The method should also return {@code true} in response to an action type of
15401 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15402 * {@code false} if it didn't.
15403 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015404 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015405 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015406 return false;
15407 }
15408
15409 /**
Joe Malin32736f02011-01-19 16:14:20 -080015410 * Detects if this View is enabled and has a drag event listener.
15411 * If both are true, then it calls the drag event listener with the
15412 * {@link android.view.DragEvent} it received. If the drag event listener returns
15413 * {@code true}, then dispatchDragEvent() returns {@code true}.
15414 * <p>
15415 * For all other cases, the method calls the
15416 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15417 * method and returns its result.
15418 * </p>
15419 * <p>
15420 * This ensures that a drag event is always consumed, even if the View does not have a drag
15421 * event listener. However, if the View has a listener and the listener returns true, then
15422 * onDragEvent() is not called.
15423 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015424 */
15425 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015426 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015427 ListenerInfo li = mListenerInfo;
15428 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15429 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015430 return true;
15431 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015432 return onDragEvent(event);
15433 }
15434
Christopher Tate3d4bf172011-03-28 16:16:46 -070015435 boolean canAcceptDrag() {
15436 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15437 }
15438
Christopher Tatea53146c2010-09-07 11:57:52 -070015439 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015440 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15441 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015442 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015443 */
15444 public void onCloseSystemDialogs(String reason) {
15445 }
Joe Malin32736f02011-01-19 16:14:20 -080015446
Dianne Hackbornffa42482009-09-23 22:20:11 -070015447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015448 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015449 * update a Region being computed for
15450 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015451 * that any non-transparent parts of the Drawable are removed from the
15452 * given transparent region.
15453 *
15454 * @param dr The Drawable whose transparency is to be applied to the region.
15455 * @param region A Region holding the current transparency information,
15456 * where any parts of the region that are set are considered to be
15457 * transparent. On return, this region will be modified to have the
15458 * transparency information reduced by the corresponding parts of the
15459 * Drawable that are not transparent.
15460 * {@hide}
15461 */
15462 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15463 if (DBG) {
15464 Log.i("View", "Getting transparent region for: " + this);
15465 }
15466 final Region r = dr.getTransparentRegion();
15467 final Rect db = dr.getBounds();
15468 final AttachInfo attachInfo = mAttachInfo;
15469 if (r != null && attachInfo != null) {
15470 final int w = getRight()-getLeft();
15471 final int h = getBottom()-getTop();
15472 if (db.left > 0) {
15473 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15474 r.op(0, 0, db.left, h, Region.Op.UNION);
15475 }
15476 if (db.right < w) {
15477 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15478 r.op(db.right, 0, w, h, Region.Op.UNION);
15479 }
15480 if (db.top > 0) {
15481 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15482 r.op(0, 0, w, db.top, Region.Op.UNION);
15483 }
15484 if (db.bottom < h) {
15485 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15486 r.op(0, db.bottom, w, h, Region.Op.UNION);
15487 }
15488 final int[] location = attachInfo.mTransparentLocation;
15489 getLocationInWindow(location);
15490 r.translate(location[0], location[1]);
15491 region.op(r, Region.Op.INTERSECT);
15492 } else {
15493 region.op(db, Region.Op.DIFFERENCE);
15494 }
15495 }
15496
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015497 private void checkForLongClick(int delayOffset) {
15498 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15499 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015500
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015501 if (mPendingCheckForLongPress == null) {
15502 mPendingCheckForLongPress = new CheckForLongPress();
15503 }
15504 mPendingCheckForLongPress.rememberWindowAttachCount();
15505 postDelayed(mPendingCheckForLongPress,
15506 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015508 }
15509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015510 /**
15511 * Inflate a view from an XML resource. This convenience method wraps the {@link
15512 * LayoutInflater} class, which provides a full range of options for view inflation.
15513 *
15514 * @param context The Context object for your activity or application.
15515 * @param resource The resource ID to inflate
15516 * @param root A view group that will be the parent. Used to properly inflate the
15517 * layout_* parameters.
15518 * @see LayoutInflater
15519 */
15520 public static View inflate(Context context, int resource, ViewGroup root) {
15521 LayoutInflater factory = LayoutInflater.from(context);
15522 return factory.inflate(resource, root);
15523 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015525 /**
Adam Powell637d3372010-08-25 14:37:03 -070015526 * Scroll the view with standard behavior for scrolling beyond the normal
15527 * content boundaries. Views that call this method should override
15528 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15529 * results of an over-scroll operation.
15530 *
15531 * Views can use this method to handle any touch or fling-based scrolling.
15532 *
15533 * @param deltaX Change in X in pixels
15534 * @param deltaY Change in Y in pixels
15535 * @param scrollX Current X scroll value in pixels before applying deltaX
15536 * @param scrollY Current Y scroll value in pixels before applying deltaY
15537 * @param scrollRangeX Maximum content scroll range along the X axis
15538 * @param scrollRangeY Maximum content scroll range along the Y axis
15539 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15540 * along the X axis.
15541 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15542 * along the Y axis.
15543 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15544 * @return true if scrolling was clamped to an over-scroll boundary along either
15545 * axis, false otherwise.
15546 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015547 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015548 protected boolean overScrollBy(int deltaX, int deltaY,
15549 int scrollX, int scrollY,
15550 int scrollRangeX, int scrollRangeY,
15551 int maxOverScrollX, int maxOverScrollY,
15552 boolean isTouchEvent) {
15553 final int overScrollMode = mOverScrollMode;
15554 final boolean canScrollHorizontal =
15555 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15556 final boolean canScrollVertical =
15557 computeVerticalScrollRange() > computeVerticalScrollExtent();
15558 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15559 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15560 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15561 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15562
15563 int newScrollX = scrollX + deltaX;
15564 if (!overScrollHorizontal) {
15565 maxOverScrollX = 0;
15566 }
15567
15568 int newScrollY = scrollY + deltaY;
15569 if (!overScrollVertical) {
15570 maxOverScrollY = 0;
15571 }
15572
15573 // Clamp values if at the limits and record
15574 final int left = -maxOverScrollX;
15575 final int right = maxOverScrollX + scrollRangeX;
15576 final int top = -maxOverScrollY;
15577 final int bottom = maxOverScrollY + scrollRangeY;
15578
15579 boolean clampedX = false;
15580 if (newScrollX > right) {
15581 newScrollX = right;
15582 clampedX = true;
15583 } else if (newScrollX < left) {
15584 newScrollX = left;
15585 clampedX = true;
15586 }
15587
15588 boolean clampedY = false;
15589 if (newScrollY > bottom) {
15590 newScrollY = bottom;
15591 clampedY = true;
15592 } else if (newScrollY < top) {
15593 newScrollY = top;
15594 clampedY = true;
15595 }
15596
15597 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15598
15599 return clampedX || clampedY;
15600 }
15601
15602 /**
15603 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15604 * respond to the results of an over-scroll operation.
15605 *
15606 * @param scrollX New X scroll value in pixels
15607 * @param scrollY New Y scroll value in pixels
15608 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15609 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15610 */
15611 protected void onOverScrolled(int scrollX, int scrollY,
15612 boolean clampedX, boolean clampedY) {
15613 // Intentionally empty.
15614 }
15615
15616 /**
15617 * Returns the over-scroll mode for this view. The result will be
15618 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15619 * (allow over-scrolling only if the view content is larger than the container),
15620 * or {@link #OVER_SCROLL_NEVER}.
15621 *
15622 * @return This view's over-scroll mode.
15623 */
15624 public int getOverScrollMode() {
15625 return mOverScrollMode;
15626 }
15627
15628 /**
15629 * Set the over-scroll mode for this view. Valid over-scroll modes are
15630 * {@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 * Setting the over-scroll mode of a view will have an effect only if the
15635 * view is capable of scrolling.
15636 *
15637 * @param overScrollMode The new over-scroll mode for this view.
15638 */
15639 public void setOverScrollMode(int overScrollMode) {
15640 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15641 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15642 overScrollMode != OVER_SCROLL_NEVER) {
15643 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15644 }
15645 mOverScrollMode = overScrollMode;
15646 }
15647
15648 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015649 * Gets a scale factor that determines the distance the view should scroll
15650 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15651 * @return The vertical scroll scale factor.
15652 * @hide
15653 */
15654 protected float getVerticalScrollFactor() {
15655 if (mVerticalScrollFactor == 0) {
15656 TypedValue outValue = new TypedValue();
15657 if (!mContext.getTheme().resolveAttribute(
15658 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15659 throw new IllegalStateException(
15660 "Expected theme to define listPreferredItemHeight.");
15661 }
15662 mVerticalScrollFactor = outValue.getDimension(
15663 mContext.getResources().getDisplayMetrics());
15664 }
15665 return mVerticalScrollFactor;
15666 }
15667
15668 /**
15669 * Gets a scale factor that determines the distance the view should scroll
15670 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15671 * @return The horizontal scroll scale factor.
15672 * @hide
15673 */
15674 protected float getHorizontalScrollFactor() {
15675 // TODO: Should use something else.
15676 return getVerticalScrollFactor();
15677 }
15678
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015679 /**
15680 * Return the value specifying the text direction or policy that was set with
15681 * {@link #setTextDirection(int)}.
15682 *
15683 * @return the defined text direction. It can be one of:
15684 *
15685 * {@link #TEXT_DIRECTION_INHERIT},
15686 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15687 * {@link #TEXT_DIRECTION_ANY_RTL},
15688 * {@link #TEXT_DIRECTION_LTR},
15689 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015690 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015691 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015692 @ViewDebug.ExportedProperty(category = "text", mapping = {
15693 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15694 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15695 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15696 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15697 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15698 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15699 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015700 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015701 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015702 }
15703
15704 /**
15705 * Set the text direction.
15706 *
15707 * @param textDirection the direction to set. Should be one of:
15708 *
15709 * {@link #TEXT_DIRECTION_INHERIT},
15710 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15711 * {@link #TEXT_DIRECTION_ANY_RTL},
15712 * {@link #TEXT_DIRECTION_LTR},
15713 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015714 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015715 */
15716 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015717 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015718 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015719 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015720 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015721 // Set the new text direction
15722 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015723 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015724 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015725 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015726 }
15727 }
15728
15729 /**
15730 * Return the resolved text direction.
15731 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015732 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15733 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15734 * up the parent chain of the view. if there is no parent, then it will return the default
15735 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15736 *
15737 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015738 *
Doug Feltcb3791202011-07-07 11:57:48 -070015739 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15740 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015741 * {@link #TEXT_DIRECTION_LTR},
15742 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015743 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015744 */
15745 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015746 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015747 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015748 resolveTextDirection();
15749 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015750 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015751 }
15752
15753 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015754 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15755 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015756 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015757 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015758 // Reset any previous text direction resolution
15759 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15760
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015761 if (hasRtlSupport()) {
15762 // Set resolved text direction flag depending on text direction flag
15763 final int textDirection = getTextDirection();
15764 switch(textDirection) {
15765 case TEXT_DIRECTION_INHERIT:
15766 if (canResolveTextDirection()) {
15767 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015768
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015769 // Set current resolved direction to the same value as the parent's one
15770 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15771 switch (parentResolvedDirection) {
15772 case TEXT_DIRECTION_FIRST_STRONG:
15773 case TEXT_DIRECTION_ANY_RTL:
15774 case TEXT_DIRECTION_LTR:
15775 case TEXT_DIRECTION_RTL:
15776 case TEXT_DIRECTION_LOCALE:
15777 mPrivateFlags2 |=
15778 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15779 break;
15780 default:
15781 // Default resolved direction is "first strong" heuristic
15782 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15783 }
15784 } else {
15785 // We cannot do the resolution if there is no parent, so use the default one
15786 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015787 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015788 break;
15789 case TEXT_DIRECTION_FIRST_STRONG:
15790 case TEXT_DIRECTION_ANY_RTL:
15791 case TEXT_DIRECTION_LTR:
15792 case TEXT_DIRECTION_RTL:
15793 case TEXT_DIRECTION_LOCALE:
15794 // Resolved direction is the same as text direction
15795 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15796 break;
15797 default:
15798 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015799 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015800 }
15801 } else {
15802 // Default resolved direction is "first strong" heuristic
15803 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015804 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015805
15806 // Set to resolved
15807 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015808 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015809 }
15810
15811 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015812 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015813 * resolution should override this method.
15814 *
15815 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015816 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015817 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015818 }
15819
15820 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015821 * Check if text direction resolution can be done.
15822 *
15823 * @return true if text direction resolution can be done otherwise return false.
15824 */
15825 public boolean canResolveTextDirection() {
15826 switch (getTextDirection()) {
15827 case TEXT_DIRECTION_INHERIT:
15828 return (mParent != null) && (mParent instanceof ViewGroup);
15829 default:
15830 return true;
15831 }
15832 }
15833
15834 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015835 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015836 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015837 * reset is done.
15838 */
15839 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015840 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015841 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015842 }
15843
15844 /**
15845 * Called when text direction is reset. Subclasses that care about text direction reset should
15846 * override this method and do a reset of the text direction of their children. The default
15847 * implementation does nothing.
15848 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015849 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015850 }
15851
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015852 /**
15853 * Return the value specifying the text alignment or policy that was set with
15854 * {@link #setTextAlignment(int)}.
15855 *
15856 * @return the defined text alignment. It can be one of:
15857 *
15858 * {@link #TEXT_ALIGNMENT_INHERIT},
15859 * {@link #TEXT_ALIGNMENT_GRAVITY},
15860 * {@link #TEXT_ALIGNMENT_CENTER},
15861 * {@link #TEXT_ALIGNMENT_TEXT_START},
15862 * {@link #TEXT_ALIGNMENT_TEXT_END},
15863 * {@link #TEXT_ALIGNMENT_VIEW_START},
15864 * {@link #TEXT_ALIGNMENT_VIEW_END}
15865 */
15866 @ViewDebug.ExportedProperty(category = "text", mapping = {
15867 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15868 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15869 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15870 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15871 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15872 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15873 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15874 })
15875 public int getTextAlignment() {
15876 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15877 }
15878
15879 /**
15880 * Set the text alignment.
15881 *
15882 * @param textAlignment The text alignment to set. Should be one of
15883 *
15884 * {@link #TEXT_ALIGNMENT_INHERIT},
15885 * {@link #TEXT_ALIGNMENT_GRAVITY},
15886 * {@link #TEXT_ALIGNMENT_CENTER},
15887 * {@link #TEXT_ALIGNMENT_TEXT_START},
15888 * {@link #TEXT_ALIGNMENT_TEXT_END},
15889 * {@link #TEXT_ALIGNMENT_VIEW_START},
15890 * {@link #TEXT_ALIGNMENT_VIEW_END}
15891 *
15892 * @attr ref android.R.styleable#View_textAlignment
15893 */
15894 public void setTextAlignment(int textAlignment) {
15895 if (textAlignment != getTextAlignment()) {
15896 // Reset the current and resolved text alignment
15897 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15898 resetResolvedTextAlignment();
15899 // Set the new text alignment
15900 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15901 // Refresh
15902 requestLayout();
15903 invalidate(true);
15904 }
15905 }
15906
15907 /**
15908 * Return the resolved text alignment.
15909 *
15910 * The resolved text alignment. This needs resolution if the value is
15911 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15912 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15913 *
15914 * @return the resolved text alignment. Returns one of:
15915 *
15916 * {@link #TEXT_ALIGNMENT_GRAVITY},
15917 * {@link #TEXT_ALIGNMENT_CENTER},
15918 * {@link #TEXT_ALIGNMENT_TEXT_START},
15919 * {@link #TEXT_ALIGNMENT_TEXT_END},
15920 * {@link #TEXT_ALIGNMENT_VIEW_START},
15921 * {@link #TEXT_ALIGNMENT_VIEW_END}
15922 */
15923 @ViewDebug.ExportedProperty(category = "text", mapping = {
15924 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15925 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15926 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15927 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15928 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15929 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15930 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15931 })
15932 public int getResolvedTextAlignment() {
15933 // If text alignment is not resolved, then resolve it
15934 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15935 resolveTextAlignment();
15936 }
15937 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15938 }
15939
15940 /**
15941 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15942 * resolution is done.
15943 */
15944 public void resolveTextAlignment() {
15945 // Reset any previous text alignment resolution
15946 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15947
15948 if (hasRtlSupport()) {
15949 // Set resolved text alignment flag depending on text alignment flag
15950 final int textAlignment = getTextAlignment();
15951 switch (textAlignment) {
15952 case TEXT_ALIGNMENT_INHERIT:
15953 // Check if we can resolve the text alignment
15954 if (canResolveLayoutDirection() && mParent instanceof View) {
15955 View view = (View) mParent;
15956
15957 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15958 switch (parentResolvedTextAlignment) {
15959 case TEXT_ALIGNMENT_GRAVITY:
15960 case TEXT_ALIGNMENT_TEXT_START:
15961 case TEXT_ALIGNMENT_TEXT_END:
15962 case TEXT_ALIGNMENT_CENTER:
15963 case TEXT_ALIGNMENT_VIEW_START:
15964 case TEXT_ALIGNMENT_VIEW_END:
15965 // Resolved text alignment is the same as the parent resolved
15966 // text alignment
15967 mPrivateFlags2 |=
15968 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15969 break;
15970 default:
15971 // Use default resolved text alignment
15972 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15973 }
15974 }
15975 else {
15976 // We cannot do the resolution if there is no parent so use the default
15977 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15978 }
15979 break;
15980 case TEXT_ALIGNMENT_GRAVITY:
15981 case TEXT_ALIGNMENT_TEXT_START:
15982 case TEXT_ALIGNMENT_TEXT_END:
15983 case TEXT_ALIGNMENT_CENTER:
15984 case TEXT_ALIGNMENT_VIEW_START:
15985 case TEXT_ALIGNMENT_VIEW_END:
15986 // Resolved text alignment is the same as text alignment
15987 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15988 break;
15989 default:
15990 // Use default resolved text alignment
15991 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15992 }
15993 } else {
15994 // Use default resolved text alignment
15995 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15996 }
15997
15998 // Set the resolved
15999 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16000 onResolvedTextAlignmentChanged();
16001 }
16002
16003 /**
16004 * Check if text alignment resolution can be done.
16005 *
16006 * @return true if text alignment resolution can be done otherwise return false.
16007 */
16008 public boolean canResolveTextAlignment() {
16009 switch (getTextAlignment()) {
16010 case TEXT_DIRECTION_INHERIT:
16011 return (mParent != null);
16012 default:
16013 return true;
16014 }
16015 }
16016
16017 /**
16018 * Called when text alignment has been resolved. Subclasses that care about text alignment
16019 * resolution should override this method.
16020 *
16021 * The default implementation does nothing.
16022 */
16023 public void onResolvedTextAlignmentChanged() {
16024 }
16025
16026 /**
16027 * Reset resolved text alignment. Text alignment can be resolved with a call to
16028 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16029 * reset is done.
16030 */
16031 public void resetResolvedTextAlignment() {
16032 // Reset any previous text alignment resolution
16033 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16034 onResolvedTextAlignmentReset();
16035 }
16036
16037 /**
16038 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16039 * override this method and do a reset of the text alignment of their children. The default
16040 * implementation does nothing.
16041 */
16042 public void onResolvedTextAlignmentReset() {
16043 }
16044
Chet Haaseb39f0512011-05-24 14:36:40 -070016045 //
16046 // Properties
16047 //
16048 /**
16049 * A Property wrapper around the <code>alpha</code> functionality handled by the
16050 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16051 */
Chet Haased47f1532011-12-16 11:18:52 -080016052 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016053 @Override
16054 public void setValue(View object, float value) {
16055 object.setAlpha(value);
16056 }
16057
16058 @Override
16059 public Float get(View object) {
16060 return object.getAlpha();
16061 }
16062 };
16063
16064 /**
16065 * A Property wrapper around the <code>translationX</code> functionality handled by the
16066 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16067 */
Chet Haased47f1532011-12-16 11:18:52 -080016068 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016069 @Override
16070 public void setValue(View object, float value) {
16071 object.setTranslationX(value);
16072 }
16073
16074 @Override
16075 public Float get(View object) {
16076 return object.getTranslationX();
16077 }
16078 };
16079
16080 /**
16081 * A Property wrapper around the <code>translationY</code> functionality handled by the
16082 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16083 */
Chet Haased47f1532011-12-16 11:18:52 -080016084 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016085 @Override
16086 public void setValue(View object, float value) {
16087 object.setTranslationY(value);
16088 }
16089
16090 @Override
16091 public Float get(View object) {
16092 return object.getTranslationY();
16093 }
16094 };
16095
16096 /**
16097 * A Property wrapper around the <code>x</code> functionality handled by the
16098 * {@link View#setX(float)} and {@link View#getX()} methods.
16099 */
Chet Haased47f1532011-12-16 11:18:52 -080016100 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016101 @Override
16102 public void setValue(View object, float value) {
16103 object.setX(value);
16104 }
16105
16106 @Override
16107 public Float get(View object) {
16108 return object.getX();
16109 }
16110 };
16111
16112 /**
16113 * A Property wrapper around the <code>y</code> functionality handled by the
16114 * {@link View#setY(float)} and {@link View#getY()} methods.
16115 */
Chet Haased47f1532011-12-16 11:18:52 -080016116 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016117 @Override
16118 public void setValue(View object, float value) {
16119 object.setY(value);
16120 }
16121
16122 @Override
16123 public Float get(View object) {
16124 return object.getY();
16125 }
16126 };
16127
16128 /**
16129 * A Property wrapper around the <code>rotation</code> functionality handled by the
16130 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16131 */
Chet Haased47f1532011-12-16 11:18:52 -080016132 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016133 @Override
16134 public void setValue(View object, float value) {
16135 object.setRotation(value);
16136 }
16137
16138 @Override
16139 public Float get(View object) {
16140 return object.getRotation();
16141 }
16142 };
16143
16144 /**
16145 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16146 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16147 */
Chet Haased47f1532011-12-16 11:18:52 -080016148 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016149 @Override
16150 public void setValue(View object, float value) {
16151 object.setRotationX(value);
16152 }
16153
16154 @Override
16155 public Float get(View object) {
16156 return object.getRotationX();
16157 }
16158 };
16159
16160 /**
16161 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16162 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16163 */
Chet Haased47f1532011-12-16 11:18:52 -080016164 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016165 @Override
16166 public void setValue(View object, float value) {
16167 object.setRotationY(value);
16168 }
16169
16170 @Override
16171 public Float get(View object) {
16172 return object.getRotationY();
16173 }
16174 };
16175
16176 /**
16177 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16178 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16179 */
Chet Haased47f1532011-12-16 11:18:52 -080016180 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016181 @Override
16182 public void setValue(View object, float value) {
16183 object.setScaleX(value);
16184 }
16185
16186 @Override
16187 public Float get(View object) {
16188 return object.getScaleX();
16189 }
16190 };
16191
16192 /**
16193 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16194 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16195 */
Chet Haased47f1532011-12-16 11:18:52 -080016196 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016197 @Override
16198 public void setValue(View object, float value) {
16199 object.setScaleY(value);
16200 }
16201
16202 @Override
16203 public Float get(View object) {
16204 return object.getScaleY();
16205 }
16206 };
16207
Jeff Brown33bbfd22011-02-24 20:55:35 -080016208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016209 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16210 * Each MeasureSpec represents a requirement for either the width or the height.
16211 * A MeasureSpec is comprised of a size and a mode. There are three possible
16212 * modes:
16213 * <dl>
16214 * <dt>UNSPECIFIED</dt>
16215 * <dd>
16216 * The parent has not imposed any constraint on the child. It can be whatever size
16217 * it wants.
16218 * </dd>
16219 *
16220 * <dt>EXACTLY</dt>
16221 * <dd>
16222 * The parent has determined an exact size for the child. The child is going to be
16223 * given those bounds regardless of how big it wants to be.
16224 * </dd>
16225 *
16226 * <dt>AT_MOST</dt>
16227 * <dd>
16228 * The child can be as large as it wants up to the specified size.
16229 * </dd>
16230 * </dl>
16231 *
16232 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16233 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16234 */
16235 public static class MeasureSpec {
16236 private static final int MODE_SHIFT = 30;
16237 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16238
16239 /**
16240 * Measure specification mode: The parent has not imposed any constraint
16241 * on the child. It can be whatever size it wants.
16242 */
16243 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16244
16245 /**
16246 * Measure specification mode: The parent has determined an exact size
16247 * for the child. The child is going to be given those bounds regardless
16248 * of how big it wants to be.
16249 */
16250 public static final int EXACTLY = 1 << MODE_SHIFT;
16251
16252 /**
16253 * Measure specification mode: The child can be as large as it wants up
16254 * to the specified size.
16255 */
16256 public static final int AT_MOST = 2 << MODE_SHIFT;
16257
16258 /**
16259 * Creates a measure specification based on the supplied size and mode.
16260 *
16261 * The mode must always be one of the following:
16262 * <ul>
16263 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16264 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16265 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16266 * </ul>
16267 *
16268 * @param size the size of the measure specification
16269 * @param mode the mode of the measure specification
16270 * @return the measure specification based on size and mode
16271 */
16272 public static int makeMeasureSpec(int size, int mode) {
16273 return size + mode;
16274 }
16275
16276 /**
16277 * Extracts the mode from the supplied measure specification.
16278 *
16279 * @param measureSpec the measure specification to extract the mode from
16280 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16281 * {@link android.view.View.MeasureSpec#AT_MOST} or
16282 * {@link android.view.View.MeasureSpec#EXACTLY}
16283 */
16284 public static int getMode(int measureSpec) {
16285 return (measureSpec & MODE_MASK);
16286 }
16287
16288 /**
16289 * Extracts the size from the supplied measure specification.
16290 *
16291 * @param measureSpec the measure specification to extract the size from
16292 * @return the size in pixels defined in the supplied measure specification
16293 */
16294 public static int getSize(int measureSpec) {
16295 return (measureSpec & ~MODE_MASK);
16296 }
16297
16298 /**
16299 * Returns a String representation of the specified measure
16300 * specification.
16301 *
16302 * @param measureSpec the measure specification to convert to a String
16303 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16304 */
16305 public static String toString(int measureSpec) {
16306 int mode = getMode(measureSpec);
16307 int size = getSize(measureSpec);
16308
16309 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16310
16311 if (mode == UNSPECIFIED)
16312 sb.append("UNSPECIFIED ");
16313 else if (mode == EXACTLY)
16314 sb.append("EXACTLY ");
16315 else if (mode == AT_MOST)
16316 sb.append("AT_MOST ");
16317 else
16318 sb.append(mode).append(" ");
16319
16320 sb.append(size);
16321 return sb.toString();
16322 }
16323 }
16324
16325 class CheckForLongPress implements Runnable {
16326
16327 private int mOriginalWindowAttachCount;
16328
16329 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016330 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016331 && mOriginalWindowAttachCount == mWindowAttachCount) {
16332 if (performLongClick()) {
16333 mHasPerformedLongPress = true;
16334 }
16335 }
16336 }
16337
16338 public void rememberWindowAttachCount() {
16339 mOriginalWindowAttachCount = mWindowAttachCount;
16340 }
16341 }
Joe Malin32736f02011-01-19 16:14:20 -080016342
Adam Powelle14579b2009-12-16 18:39:52 -080016343 private final class CheckForTap implements Runnable {
16344 public void run() {
16345 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016346 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016347 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016348 }
16349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016350
Adam Powella35d7682010-03-12 14:48:13 -080016351 private final class PerformClick implements Runnable {
16352 public void run() {
16353 performClick();
16354 }
16355 }
16356
Dianne Hackborn63042d62011-01-26 18:56:29 -080016357 /** @hide */
16358 public void hackTurnOffWindowResizeAnim(boolean off) {
16359 mAttachInfo.mTurnOffWindowResizeAnim = off;
16360 }
Joe Malin32736f02011-01-19 16:14:20 -080016361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016362 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016363 * This method returns a ViewPropertyAnimator object, which can be used to animate
16364 * specific properties on this View.
16365 *
16366 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16367 */
16368 public ViewPropertyAnimator animate() {
16369 if (mAnimator == null) {
16370 mAnimator = new ViewPropertyAnimator(this);
16371 }
16372 return mAnimator;
16373 }
16374
16375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016376 * Interface definition for a callback to be invoked when a key event is
16377 * dispatched to this view. The callback will be invoked before the key
16378 * event is given to the view.
16379 */
16380 public interface OnKeyListener {
16381 /**
16382 * Called when a key is dispatched to a view. This allows listeners to
16383 * get a chance to respond before the target view.
16384 *
16385 * @param v The view the key has been dispatched to.
16386 * @param keyCode The code for the physical key that was pressed
16387 * @param event The KeyEvent object containing full information about
16388 * the event.
16389 * @return True if the listener has consumed the event, false otherwise.
16390 */
16391 boolean onKey(View v, int keyCode, KeyEvent event);
16392 }
16393
16394 /**
16395 * Interface definition for a callback to be invoked when a touch event is
16396 * dispatched to this view. The callback will be invoked before the touch
16397 * event is given to the view.
16398 */
16399 public interface OnTouchListener {
16400 /**
16401 * Called when a touch event is dispatched to a view. This allows listeners to
16402 * get a chance to respond before the target view.
16403 *
16404 * @param v The view the touch event has been dispatched to.
16405 * @param event The MotionEvent object containing full information about
16406 * the event.
16407 * @return True if the listener has consumed the event, false otherwise.
16408 */
16409 boolean onTouch(View v, MotionEvent event);
16410 }
16411
16412 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016413 * Interface definition for a callback to be invoked when a hover event is
16414 * dispatched to this view. The callback will be invoked before the hover
16415 * event is given to the view.
16416 */
16417 public interface OnHoverListener {
16418 /**
16419 * Called when a hover event is dispatched to a view. This allows listeners to
16420 * get a chance to respond before the target view.
16421 *
16422 * @param v The view the hover event has been dispatched to.
16423 * @param event The MotionEvent object containing full information about
16424 * the event.
16425 * @return True if the listener has consumed the event, false otherwise.
16426 */
16427 boolean onHover(View v, MotionEvent event);
16428 }
16429
16430 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016431 * Interface definition for a callback to be invoked when a generic motion event is
16432 * dispatched to this view. The callback will be invoked before the generic motion
16433 * event is given to the view.
16434 */
16435 public interface OnGenericMotionListener {
16436 /**
16437 * Called when a generic motion event is dispatched to a view. This allows listeners to
16438 * get a chance to respond before the target view.
16439 *
16440 * @param v The view the generic motion event has been dispatched to.
16441 * @param event The MotionEvent object containing full information about
16442 * the event.
16443 * @return True if the listener has consumed the event, false otherwise.
16444 */
16445 boolean onGenericMotion(View v, MotionEvent event);
16446 }
16447
16448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016449 * Interface definition for a callback to be invoked when a view has been clicked and held.
16450 */
16451 public interface OnLongClickListener {
16452 /**
16453 * Called when a view has been clicked and held.
16454 *
16455 * @param v The view that was clicked and held.
16456 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016457 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016458 */
16459 boolean onLongClick(View v);
16460 }
16461
16462 /**
Chris Tate32affef2010-10-18 15:29:21 -070016463 * Interface definition for a callback to be invoked when a drag is being dispatched
16464 * to this view. The callback will be invoked before the hosting view's own
16465 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16466 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016467 *
16468 * <div class="special reference">
16469 * <h3>Developer Guides</h3>
16470 * <p>For a guide to implementing drag and drop features, read the
16471 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16472 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016473 */
16474 public interface OnDragListener {
16475 /**
16476 * Called when a drag event is dispatched to a view. This allows listeners
16477 * to get a chance to override base View behavior.
16478 *
Joe Malin32736f02011-01-19 16:14:20 -080016479 * @param v The View that received the drag event.
16480 * @param event The {@link android.view.DragEvent} object for the drag event.
16481 * @return {@code true} if the drag event was handled successfully, or {@code false}
16482 * if the drag event was not handled. Note that {@code false} will trigger the View
16483 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016484 */
16485 boolean onDrag(View v, DragEvent event);
16486 }
16487
16488 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016489 * Interface definition for a callback to be invoked when the focus state of
16490 * a view changed.
16491 */
16492 public interface OnFocusChangeListener {
16493 /**
16494 * Called when the focus state of a view has changed.
16495 *
16496 * @param v The view whose state has changed.
16497 * @param hasFocus The new focus state of v.
16498 */
16499 void onFocusChange(View v, boolean hasFocus);
16500 }
16501
16502 /**
16503 * Interface definition for a callback to be invoked when a view is clicked.
16504 */
16505 public interface OnClickListener {
16506 /**
16507 * Called when a view has been clicked.
16508 *
16509 * @param v The view that was clicked.
16510 */
16511 void onClick(View v);
16512 }
16513
16514 /**
16515 * Interface definition for a callback to be invoked when the context menu
16516 * for this view is being built.
16517 */
16518 public interface OnCreateContextMenuListener {
16519 /**
16520 * Called when the context menu for this view is being built. It is not
16521 * safe to hold onto the menu after this method returns.
16522 *
16523 * @param menu The context menu that is being built
16524 * @param v The view for which the context menu is being built
16525 * @param menuInfo Extra information about the item for which the
16526 * context menu should be shown. This information will vary
16527 * depending on the class of v.
16528 */
16529 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16530 }
16531
Joe Onorato664644d2011-01-23 17:53:23 -080016532 /**
16533 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016534 * visibility. This reports <strong>global</strong> changes to the system UI
16535 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016536 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016537 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016538 */
16539 public interface OnSystemUiVisibilityChangeListener {
16540 /**
16541 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016542 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016543 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016544 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016545 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16546 * <strong>global</strong> state of the UI visibility flags, not what your
16547 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016548 */
16549 public void onSystemUiVisibilityChange(int visibility);
16550 }
16551
Adam Powell4afd62b2011-02-18 15:02:18 -080016552 /**
16553 * Interface definition for a callback to be invoked when this view is attached
16554 * or detached from its window.
16555 */
16556 public interface OnAttachStateChangeListener {
16557 /**
16558 * Called when the view is attached to a window.
16559 * @param v The view that was attached
16560 */
16561 public void onViewAttachedToWindow(View v);
16562 /**
16563 * Called when the view is detached from a window.
16564 * @param v The view that was detached
16565 */
16566 public void onViewDetachedFromWindow(View v);
16567 }
16568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016569 private final class UnsetPressedState implements Runnable {
16570 public void run() {
16571 setPressed(false);
16572 }
16573 }
16574
16575 /**
16576 * Base class for derived classes that want to save and restore their own
16577 * state in {@link android.view.View#onSaveInstanceState()}.
16578 */
16579 public static class BaseSavedState extends AbsSavedState {
16580 /**
16581 * Constructor used when reading from a parcel. Reads the state of the superclass.
16582 *
16583 * @param source
16584 */
16585 public BaseSavedState(Parcel source) {
16586 super(source);
16587 }
16588
16589 /**
16590 * Constructor called by derived classes when creating their SavedState objects
16591 *
16592 * @param superState The state of the superclass of this view
16593 */
16594 public BaseSavedState(Parcelable superState) {
16595 super(superState);
16596 }
16597
16598 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16599 new Parcelable.Creator<BaseSavedState>() {
16600 public BaseSavedState createFromParcel(Parcel in) {
16601 return new BaseSavedState(in);
16602 }
16603
16604 public BaseSavedState[] newArray(int size) {
16605 return new BaseSavedState[size];
16606 }
16607 };
16608 }
16609
16610 /**
16611 * A set of information given to a view when it is attached to its parent
16612 * window.
16613 */
16614 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016615 interface Callbacks {
16616 void playSoundEffect(int effectId);
16617 boolean performHapticFeedback(int effectId, boolean always);
16618 }
16619
16620 /**
16621 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16622 * to a Handler. This class contains the target (View) to invalidate and
16623 * the coordinates of the dirty rectangle.
16624 *
16625 * For performance purposes, this class also implements a pool of up to
16626 * POOL_LIMIT objects that get reused. This reduces memory allocations
16627 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016628 */
Romain Guyd928d682009-03-31 17:52:16 -070016629 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016630 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016631 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16632 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016633 public InvalidateInfo newInstance() {
16634 return new InvalidateInfo();
16635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016636
Romain Guyd928d682009-03-31 17:52:16 -070016637 public void onAcquired(InvalidateInfo element) {
16638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016639
Romain Guyd928d682009-03-31 17:52:16 -070016640 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016641 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016642 }
16643 }, POOL_LIMIT)
16644 );
16645
16646 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016647 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016648
16649 View target;
16650
16651 int left;
16652 int top;
16653 int right;
16654 int bottom;
16655
Romain Guyd928d682009-03-31 17:52:16 -070016656 public void setNextPoolable(InvalidateInfo element) {
16657 mNext = element;
16658 }
16659
16660 public InvalidateInfo getNextPoolable() {
16661 return mNext;
16662 }
16663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016664 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016665 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016666 }
16667
16668 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016669 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016670 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016671
16672 public boolean isPooled() {
16673 return mIsPooled;
16674 }
16675
16676 public void setPooled(boolean isPooled) {
16677 mIsPooled = isPooled;
16678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016679 }
16680
16681 final IWindowSession mSession;
16682
16683 final IWindow mWindow;
16684
16685 final IBinder mWindowToken;
16686
16687 final Callbacks mRootCallbacks;
16688
Romain Guy59a12ca2011-06-09 17:48:21 -070016689 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016691 /**
16692 * The top view of the hierarchy.
16693 */
16694 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016696 IBinder mPanelParentWindowToken;
16697 Surface mSurface;
16698
Romain Guyb051e892010-09-28 19:09:36 -070016699 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016700 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016701 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016702
Romain Guy7e4e5612012-03-05 14:37:29 -080016703 boolean mScreenOn;
16704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016705 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016706 * Scale factor used by the compatibility mode
16707 */
16708 float mApplicationScale;
16709
16710 /**
16711 * Indicates whether the application is in compatibility mode
16712 */
16713 boolean mScalingRequired;
16714
16715 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016716 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016717 */
16718 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016719
Dianne Hackborn63042d62011-01-26 18:56:29 -080016720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016721 * Left position of this view's window
16722 */
16723 int mWindowLeft;
16724
16725 /**
16726 * Top position of this view's window
16727 */
16728 int mWindowTop;
16729
16730 /**
Adam Powell26153a32010-11-08 15:22:27 -080016731 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016732 */
Adam Powell26153a32010-11-08 15:22:27 -080016733 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016734
16735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016736 * For windows that are full-screen but using insets to layout inside
16737 * of the screen decorations, these are the current insets for the
16738 * content of the window.
16739 */
16740 final Rect mContentInsets = new Rect();
16741
16742 /**
16743 * For windows that are full-screen but using insets to layout inside
16744 * of the screen decorations, these are the current insets for the
16745 * actual visible parts of the window.
16746 */
16747 final Rect mVisibleInsets = new Rect();
16748
16749 /**
16750 * The internal insets given by this window. This value is
16751 * supplied by the client (through
16752 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16753 * be given to the window manager when changed to be used in laying
16754 * out windows behind it.
16755 */
16756 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16757 = new ViewTreeObserver.InternalInsetsInfo();
16758
16759 /**
16760 * All views in the window's hierarchy that serve as scroll containers,
16761 * used to determine if the window can be resized or must be panned
16762 * to adjust for a soft input area.
16763 */
16764 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16765
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016766 final KeyEvent.DispatcherState mKeyDispatchState
16767 = new KeyEvent.DispatcherState();
16768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016769 /**
16770 * Indicates whether the view's window currently has the focus.
16771 */
16772 boolean mHasWindowFocus;
16773
16774 /**
16775 * The current visibility of the window.
16776 */
16777 int mWindowVisibility;
16778
16779 /**
16780 * Indicates the time at which drawing started to occur.
16781 */
16782 long mDrawingTime;
16783
16784 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016785 * Indicates whether or not ignoring the DIRTY_MASK flags.
16786 */
16787 boolean mIgnoreDirtyState;
16788
16789 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016790 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16791 * to avoid clearing that flag prematurely.
16792 */
16793 boolean mSetIgnoreDirtyState = false;
16794
16795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016796 * Indicates whether the view's window is currently in touch mode.
16797 */
16798 boolean mInTouchMode;
16799
16800 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016801 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016802 * the next time it performs a traversal
16803 */
16804 boolean mRecomputeGlobalAttributes;
16805
16806 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016807 * Always report new attributes at next traversal.
16808 */
16809 boolean mForceReportNewAttributes;
16810
16811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016812 * Set during a traveral if any views want to keep the screen on.
16813 */
16814 boolean mKeepScreenOn;
16815
16816 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016817 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16818 */
16819 int mSystemUiVisibility;
16820
16821 /**
16822 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16823 * attached.
16824 */
16825 boolean mHasSystemUiListeners;
16826
16827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016828 * Set if the visibility of any views has changed.
16829 */
16830 boolean mViewVisibilityChanged;
16831
16832 /**
16833 * Set to true if a view has been scrolled.
16834 */
16835 boolean mViewScrollChanged;
16836
16837 /**
16838 * Global to the view hierarchy used as a temporary for dealing with
16839 * x/y points in the transparent region computations.
16840 */
16841 final int[] mTransparentLocation = new int[2];
16842
16843 /**
16844 * Global to the view hierarchy used as a temporary for dealing with
16845 * x/y points in the ViewGroup.invalidateChild implementation.
16846 */
16847 final int[] mInvalidateChildLocation = new int[2];
16848
Chet Haasec3aa3612010-06-17 08:50:37 -070016849
16850 /**
16851 * Global to the view hierarchy used as a temporary for dealing with
16852 * x/y location when view is transformed.
16853 */
16854 final float[] mTmpTransformLocation = new float[2];
16855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016856 /**
16857 * The view tree observer used to dispatch global events like
16858 * layout, pre-draw, touch mode change, etc.
16859 */
16860 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16861
16862 /**
16863 * A Canvas used by the view hierarchy to perform bitmap caching.
16864 */
16865 Canvas mCanvas;
16866
16867 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016868 * The view root impl.
16869 */
16870 final ViewRootImpl mViewRootImpl;
16871
16872 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016873 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016874 * handler can be used to pump events in the UI events queue.
16875 */
16876 final Handler mHandler;
16877
16878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016879 * Temporary for use in computing invalidate rectangles while
16880 * calling up the hierarchy.
16881 */
16882 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016883
16884 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016885 * Temporary for use in computing hit areas with transformed views
16886 */
16887 final RectF mTmpTransformRect = new RectF();
16888
16889 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016890 * Temporary list for use in collecting focusable descendents of a view.
16891 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016892 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016894 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016895 * The id of the window for accessibility purposes.
16896 */
16897 int mAccessibilityWindowId = View.NO_ID;
16898
16899 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016900 * Whether to ingore not exposed for accessibility Views when
16901 * reporting the view tree to accessibility services.
16902 */
16903 boolean mIncludeNotImportantViews;
16904
16905 /**
16906 * The drawable for highlighting accessibility focus.
16907 */
16908 Drawable mAccessibilityFocusDrawable;
16909
16910 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070016911 * Show where the margins, bounds and layout bounds are for each view.
16912 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070016913 final boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070016914
16915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016916 * Creates a new set of attachment information with the specified
16917 * events handler and thread.
16918 *
16919 * @param handler the events handler the view must use
16920 */
16921 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016922 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 mSession = session;
16924 mWindow = window;
16925 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016926 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016927 mHandler = handler;
16928 mRootCallbacks = effectPlayer;
16929 }
16930 }
16931
16932 /**
16933 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16934 * is supported. This avoids keeping too many unused fields in most
16935 * instances of View.</p>
16936 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016937 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016938
Mike Cleronf116bf82009-09-27 19:14:12 -070016939 /**
16940 * Scrollbars are not visible
16941 */
16942 public static final int OFF = 0;
16943
16944 /**
16945 * Scrollbars are visible
16946 */
16947 public static final int ON = 1;
16948
16949 /**
16950 * Scrollbars are fading away
16951 */
16952 public static final int FADING = 2;
16953
16954 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016956 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016957 public int scrollBarDefaultDelayBeforeFade;
16958 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016959
16960 public int scrollBarSize;
16961 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016962 public float[] interpolatorValues;
16963 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016964
16965 public final Paint paint;
16966 public final Matrix matrix;
16967 public Shader shader;
16968
Mike Cleronf116bf82009-09-27 19:14:12 -070016969 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16970
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016971 private static final float[] OPAQUE = { 255 };
16972 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016973
Mike Cleronf116bf82009-09-27 19:14:12 -070016974 /**
16975 * When fading should start. This time moves into the future every time
16976 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16977 */
16978 public long fadeStartTime;
16979
16980
16981 /**
16982 * The current state of the scrollbars: ON, OFF, or FADING
16983 */
16984 public int state = OFF;
16985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016986 private int mLastColor;
16987
Mike Cleronf116bf82009-09-27 19:14:12 -070016988 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016989 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16990 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016991 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16992 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016993
16994 paint = new Paint();
16995 matrix = new Matrix();
16996 // use use a height of 1, and then wack the matrix each time we
16997 // actually use it.
16998 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017000 paint.setShader(shader);
17001 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017002 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017003 }
Romain Guy8506ab42009-06-11 17:35:47 -070017004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017005 public void setFadeColor(int color) {
17006 if (color != 0 && color != mLastColor) {
17007 mLastColor = color;
17008 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017009
Romain Guye55e1a72009-08-27 10:42:26 -070017010 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17011 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017013 paint.setShader(shader);
17014 // Restore the default transfer mode (src_over)
17015 paint.setXfermode(null);
17016 }
17017 }
Joe Malin32736f02011-01-19 16:14:20 -080017018
Mike Cleronf116bf82009-09-27 19:14:12 -070017019 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017020 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017021 if (now >= fadeStartTime) {
17022
17023 // the animation fades the scrollbars out by changing
17024 // the opacity (alpha) from fully opaque to fully
17025 // transparent
17026 int nextFrame = (int) now;
17027 int framesCount = 0;
17028
17029 Interpolator interpolator = scrollBarInterpolator;
17030
17031 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017032 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017033
17034 // End transparent
17035 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017036 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017037
17038 state = FADING;
17039
17040 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017041 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017042 }
17043 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017044 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017045
Svetoslav Ganova0156172011-06-26 17:55:44 -070017046 /**
17047 * Resuable callback for sending
17048 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17049 */
17050 private class SendViewScrolledAccessibilityEvent implements Runnable {
17051 public volatile boolean mIsPending;
17052
17053 public void run() {
17054 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17055 mIsPending = false;
17056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017057 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017058
17059 /**
17060 * <p>
17061 * This class represents a delegate that can be registered in a {@link View}
17062 * to enhance accessibility support via composition rather via inheritance.
17063 * It is specifically targeted to widget developers that extend basic View
17064 * classes i.e. classes in package android.view, that would like their
17065 * applications to be backwards compatible.
17066 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017067 * <div class="special reference">
17068 * <h3>Developer Guides</h3>
17069 * <p>For more information about making applications accessible, read the
17070 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17071 * developer guide.</p>
17072 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017073 * <p>
17074 * A scenario in which a developer would like to use an accessibility delegate
17075 * is overriding a method introduced in a later API version then the minimal API
17076 * version supported by the application. For example, the method
17077 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17078 * in API version 4 when the accessibility APIs were first introduced. If a
17079 * developer would like his application to run on API version 4 devices (assuming
17080 * all other APIs used by the application are version 4 or lower) and take advantage
17081 * of this method, instead of overriding the method which would break the application's
17082 * backwards compatibility, he can override the corresponding method in this
17083 * delegate and register the delegate in the target View if the API version of
17084 * the system is high enough i.e. the API version is same or higher to the API
17085 * version that introduced
17086 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17087 * </p>
17088 * <p>
17089 * Here is an example implementation:
17090 * </p>
17091 * <code><pre><p>
17092 * if (Build.VERSION.SDK_INT >= 14) {
17093 * // If the API version is equal of higher than the version in
17094 * // which onInitializeAccessibilityNodeInfo was introduced we
17095 * // register a delegate with a customized implementation.
17096 * View view = findViewById(R.id.view_id);
17097 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17098 * public void onInitializeAccessibilityNodeInfo(View host,
17099 * AccessibilityNodeInfo info) {
17100 * // Let the default implementation populate the info.
17101 * super.onInitializeAccessibilityNodeInfo(host, info);
17102 * // Set some other information.
17103 * info.setEnabled(host.isEnabled());
17104 * }
17105 * });
17106 * }
17107 * </code></pre></p>
17108 * <p>
17109 * This delegate contains methods that correspond to the accessibility methods
17110 * in View. If a delegate has been specified the implementation in View hands
17111 * off handling to the corresponding method in this delegate. The default
17112 * implementation the delegate methods behaves exactly as the corresponding
17113 * method in View for the case of no accessibility delegate been set. Hence,
17114 * to customize the behavior of a View method, clients can override only the
17115 * corresponding delegate method without altering the behavior of the rest
17116 * accessibility related methods of the host view.
17117 * </p>
17118 */
17119 public static class AccessibilityDelegate {
17120
17121 /**
17122 * Sends an accessibility event of the given type. If accessibility is not
17123 * enabled this method has no effect.
17124 * <p>
17125 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17126 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17127 * been set.
17128 * </p>
17129 *
17130 * @param host The View hosting the delegate.
17131 * @param eventType The type of the event to send.
17132 *
17133 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17134 */
17135 public void sendAccessibilityEvent(View host, int eventType) {
17136 host.sendAccessibilityEventInternal(eventType);
17137 }
17138
17139 /**
17140 * Sends an accessibility event. This method behaves exactly as
17141 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17142 * empty {@link AccessibilityEvent} and does not perform a check whether
17143 * accessibility is enabled.
17144 * <p>
17145 * The default implementation behaves as
17146 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17147 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17148 * the case of no accessibility delegate been set.
17149 * </p>
17150 *
17151 * @param host The View hosting the delegate.
17152 * @param event The event to send.
17153 *
17154 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17155 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17156 */
17157 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17158 host.sendAccessibilityEventUncheckedInternal(event);
17159 }
17160
17161 /**
17162 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17163 * to its children for adding their text content to the event.
17164 * <p>
17165 * The default implementation behaves as
17166 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17167 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17168 * the case of no accessibility delegate been set.
17169 * </p>
17170 *
17171 * @param host The View hosting the delegate.
17172 * @param event The event.
17173 * @return True if the event population was completed.
17174 *
17175 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17176 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17177 */
17178 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17179 return host.dispatchPopulateAccessibilityEventInternal(event);
17180 }
17181
17182 /**
17183 * Gives a chance to the host View to populate the accessibility event with its
17184 * text content.
17185 * <p>
17186 * The default implementation behaves as
17187 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17188 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17189 * the case of no accessibility delegate been set.
17190 * </p>
17191 *
17192 * @param host The View hosting the delegate.
17193 * @param event The accessibility event which to populate.
17194 *
17195 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17196 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17197 */
17198 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17199 host.onPopulateAccessibilityEventInternal(event);
17200 }
17201
17202 /**
17203 * Initializes an {@link AccessibilityEvent} with information about the
17204 * the host View which is the event source.
17205 * <p>
17206 * The default implementation behaves as
17207 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17208 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17209 * the case of no accessibility delegate been set.
17210 * </p>
17211 *
17212 * @param host The View hosting the delegate.
17213 * @param event The event to initialize.
17214 *
17215 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17216 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17217 */
17218 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17219 host.onInitializeAccessibilityEventInternal(event);
17220 }
17221
17222 /**
17223 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17224 * <p>
17225 * The default implementation behaves as
17226 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17227 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17228 * the case of no accessibility delegate been set.
17229 * </p>
17230 *
17231 * @param host The View hosting the delegate.
17232 * @param info The instance to initialize.
17233 *
17234 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17235 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17236 */
17237 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17238 host.onInitializeAccessibilityNodeInfoInternal(info);
17239 }
17240
17241 /**
17242 * Called when a child of the host View has requested sending an
17243 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17244 * to augment the event.
17245 * <p>
17246 * The default implementation behaves as
17247 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17248 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17249 * the case of no accessibility delegate been set.
17250 * </p>
17251 *
17252 * @param host The View hosting the delegate.
17253 * @param child The child which requests sending the event.
17254 * @param event The event to be sent.
17255 * @return True if the event should be sent
17256 *
17257 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17258 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17259 */
17260 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17261 AccessibilityEvent event) {
17262 return host.onRequestSendAccessibilityEventInternal(child, event);
17263 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017264
17265 /**
17266 * Gets the provider for managing a virtual view hierarchy rooted at this View
17267 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17268 * that explore the window content.
17269 * <p>
17270 * The default implementation behaves as
17271 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17272 * the case of no accessibility delegate been set.
17273 * </p>
17274 *
17275 * @return The provider.
17276 *
17277 * @see AccessibilityNodeProvider
17278 */
17279 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17280 return null;
17281 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017283}