blob: 2f67481aba221e2d83b1c7bdee58132d8134909d [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 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004671
4672 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4673 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4674
4675 if (isClickable()) {
4676 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4677 }
4678
4679 if (isLongClickable()) {
4680 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4681 }
4682
4683 if (getContentDescription() != null) {
4684 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_GRANULARITY);
4685 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_GRANULARITY);
4686 info.setGranularities(AccessibilityNodeInfo.GRANULARITY_CHARACTER
4687 | AccessibilityNodeInfo.GRANULARITY_WORD);
4688 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004689 }
4690
4691 /**
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004692 * Computes whether this view is visible on the screen.
4693 *
4694 * @return Whether the view is visible on the screen.
4695 */
4696 boolean isDisplayedOnScreen() {
4697 // The first two checks are made also made by isShown() which
4698 // however traverses the tree up to the parent to catch that.
4699 // Therefore, we do some fail fast check to minimize the up
4700 // tree traversal.
4701 return (mAttachInfo != null
4702 && mAttachInfo.mWindowVisibility == View.VISIBLE
4703 && getAlpha() > 0
4704 && isShown()
4705 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4706 }
4707
4708 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004709 * Sets a delegate for implementing accessibility support via compositon as
4710 * opposed to inheritance. The delegate's primary use is for implementing
4711 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4712 *
4713 * @param delegate The delegate instance.
4714 *
4715 * @see AccessibilityDelegate
4716 */
4717 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4718 mAccessibilityDelegate = delegate;
4719 }
4720
4721 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004722 * Gets the provider for managing a virtual view hierarchy rooted at this View
4723 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4724 * that explore the window content.
4725 * <p>
4726 * If this method returns an instance, this instance is responsible for managing
4727 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4728 * View including the one representing the View itself. Similarly the returned
4729 * instance is responsible for performing accessibility actions on any virtual
4730 * view or the root view itself.
4731 * </p>
4732 * <p>
4733 * If an {@link AccessibilityDelegate} has been specified via calling
4734 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4735 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4736 * is responsible for handling this call.
4737 * </p>
4738 *
4739 * @return The provider.
4740 *
4741 * @see AccessibilityNodeProvider
4742 */
4743 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4744 if (mAccessibilityDelegate != null) {
4745 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4746 } else {
4747 return null;
4748 }
4749 }
4750
4751 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004752 * Gets the unique identifier of this view on the screen for accessibility purposes.
4753 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4754 *
4755 * @return The view accessibility id.
4756 *
4757 * @hide
4758 */
4759 public int getAccessibilityViewId() {
4760 if (mAccessibilityViewId == NO_ID) {
4761 mAccessibilityViewId = sNextAccessibilityViewId++;
4762 }
4763 return mAccessibilityViewId;
4764 }
4765
4766 /**
4767 * Gets the unique identifier of the window in which this View reseides.
4768 *
4769 * @return The window accessibility id.
4770 *
4771 * @hide
4772 */
4773 public int getAccessibilityWindowId() {
4774 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4775 }
4776
4777 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004778 * Gets the {@link View} description. It briefly describes the view and is
4779 * primarily used for accessibility support. Set this property to enable
4780 * better accessibility support for your application. This is especially
4781 * true for views that do not have textual representation (For example,
4782 * ImageButton).
4783 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004784 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004785 *
4786 * @attr ref android.R.styleable#View_contentDescription
4787 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004788 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004789 public CharSequence getContentDescription() {
4790 return mContentDescription;
4791 }
4792
4793 /**
4794 * Sets the {@link View} description. It briefly describes the view and is
4795 * primarily used for accessibility support. Set this property to enable
4796 * better accessibility support for your application. This is especially
4797 * true for views that do not have textual representation (For example,
4798 * ImageButton).
4799 *
4800 * @param contentDescription The content description.
4801 *
4802 * @attr ref android.R.styleable#View_contentDescription
4803 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004804 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004805 public void setContentDescription(CharSequence contentDescription) {
4806 mContentDescription = contentDescription;
4807 }
4808
4809 /**
Romain Guya2431d02009-04-30 16:30:00 -07004810 * Invoked whenever this view loses focus, either by losing window focus or by losing
4811 * focus within its window. This method can be used to clear any state tied to the
4812 * focus. For instance, if a button is held pressed with the trackball and the window
4813 * loses focus, this method can be used to cancel the press.
4814 *
4815 * Subclasses of View overriding this method should always call super.onFocusLost().
4816 *
4817 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004818 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004819 *
4820 * @hide pending API council approval
4821 */
4822 protected void onFocusLost() {
4823 resetPressedState();
4824 }
4825
4826 private void resetPressedState() {
4827 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4828 return;
4829 }
4830
4831 if (isPressed()) {
4832 setPressed(false);
4833
4834 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004835 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004836 }
4837 }
4838 }
4839
4840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 * Returns true if this view has focus
4842 *
4843 * @return True if this view has focus, false otherwise.
4844 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004845 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 public boolean isFocused() {
4847 return (mPrivateFlags & FOCUSED) != 0;
4848 }
4849
4850 /**
4851 * Find the view in the hierarchy rooted at this view that currently has
4852 * focus.
4853 *
4854 * @return The view that currently has focus, or null if no focused view can
4855 * be found.
4856 */
4857 public View findFocus() {
4858 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4859 }
4860
4861 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004862 * Indicates whether this view is one of the set of scrollable containers in
4863 * its window.
4864 *
4865 * @return whether this view is one of the set of scrollable containers in
4866 * its window
4867 *
4868 * @attr ref android.R.styleable#View_isScrollContainer
4869 */
4870 public boolean isScrollContainer() {
4871 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4872 }
4873
4874 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004875 * Change whether this view is one of the set of scrollable containers in
4876 * its window. This will be used to determine whether the window can
4877 * resize or must pan when a soft input area is open -- scrollable
4878 * containers allow the window to use resize mode since the container
4879 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004880 *
4881 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 */
4883 public void setScrollContainer(boolean isScrollContainer) {
4884 if (isScrollContainer) {
4885 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4886 mAttachInfo.mScrollContainers.add(this);
4887 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4888 }
4889 mPrivateFlags |= SCROLL_CONTAINER;
4890 } else {
4891 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4892 mAttachInfo.mScrollContainers.remove(this);
4893 }
4894 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4895 }
4896 }
4897
4898 /**
4899 * Returns the quality of the drawing cache.
4900 *
4901 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4902 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4903 *
4904 * @see #setDrawingCacheQuality(int)
4905 * @see #setDrawingCacheEnabled(boolean)
4906 * @see #isDrawingCacheEnabled()
4907 *
4908 * @attr ref android.R.styleable#View_drawingCacheQuality
4909 */
4910 public int getDrawingCacheQuality() {
4911 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4912 }
4913
4914 /**
4915 * Set the drawing cache quality of this view. This value is used only when the
4916 * drawing cache is enabled
4917 *
4918 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4919 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4920 *
4921 * @see #getDrawingCacheQuality()
4922 * @see #setDrawingCacheEnabled(boolean)
4923 * @see #isDrawingCacheEnabled()
4924 *
4925 * @attr ref android.R.styleable#View_drawingCacheQuality
4926 */
4927 public void setDrawingCacheQuality(int quality) {
4928 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4929 }
4930
4931 /**
4932 * Returns whether the screen should remain on, corresponding to the current
4933 * value of {@link #KEEP_SCREEN_ON}.
4934 *
4935 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4936 *
4937 * @see #setKeepScreenOn(boolean)
4938 *
4939 * @attr ref android.R.styleable#View_keepScreenOn
4940 */
4941 public boolean getKeepScreenOn() {
4942 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4943 }
4944
4945 /**
4946 * Controls whether the screen should remain on, modifying the
4947 * value of {@link #KEEP_SCREEN_ON}.
4948 *
4949 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4950 *
4951 * @see #getKeepScreenOn()
4952 *
4953 * @attr ref android.R.styleable#View_keepScreenOn
4954 */
4955 public void setKeepScreenOn(boolean keepScreenOn) {
4956 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4957 }
4958
4959 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004960 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4961 * @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 -08004962 *
4963 * @attr ref android.R.styleable#View_nextFocusLeft
4964 */
4965 public int getNextFocusLeftId() {
4966 return mNextFocusLeftId;
4967 }
4968
4969 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004970 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4971 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4972 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 *
4974 * @attr ref android.R.styleable#View_nextFocusLeft
4975 */
4976 public void setNextFocusLeftId(int nextFocusLeftId) {
4977 mNextFocusLeftId = nextFocusLeftId;
4978 }
4979
4980 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004981 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4982 * @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 -08004983 *
4984 * @attr ref android.R.styleable#View_nextFocusRight
4985 */
4986 public int getNextFocusRightId() {
4987 return mNextFocusRightId;
4988 }
4989
4990 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004991 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4992 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4993 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 *
4995 * @attr ref android.R.styleable#View_nextFocusRight
4996 */
4997 public void setNextFocusRightId(int nextFocusRightId) {
4998 mNextFocusRightId = nextFocusRightId;
4999 }
5000
5001 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005002 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5003 * @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 -08005004 *
5005 * @attr ref android.R.styleable#View_nextFocusUp
5006 */
5007 public int getNextFocusUpId() {
5008 return mNextFocusUpId;
5009 }
5010
5011 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005012 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5013 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5014 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 *
5016 * @attr ref android.R.styleable#View_nextFocusUp
5017 */
5018 public void setNextFocusUpId(int nextFocusUpId) {
5019 mNextFocusUpId = nextFocusUpId;
5020 }
5021
5022 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005023 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5024 * @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 -08005025 *
5026 * @attr ref android.R.styleable#View_nextFocusDown
5027 */
5028 public int getNextFocusDownId() {
5029 return mNextFocusDownId;
5030 }
5031
5032 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005033 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5034 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5035 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 *
5037 * @attr ref android.R.styleable#View_nextFocusDown
5038 */
5039 public void setNextFocusDownId(int nextFocusDownId) {
5040 mNextFocusDownId = nextFocusDownId;
5041 }
5042
5043 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005044 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5045 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5046 *
5047 * @attr ref android.R.styleable#View_nextFocusForward
5048 */
5049 public int getNextFocusForwardId() {
5050 return mNextFocusForwardId;
5051 }
5052
5053 /**
5054 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5055 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5056 * decide automatically.
5057 *
5058 * @attr ref android.R.styleable#View_nextFocusForward
5059 */
5060 public void setNextFocusForwardId(int nextFocusForwardId) {
5061 mNextFocusForwardId = nextFocusForwardId;
5062 }
5063
5064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 * Returns the visibility of this view and all of its ancestors
5066 *
5067 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5068 */
5069 public boolean isShown() {
5070 View current = this;
5071 //noinspection ConstantConditions
5072 do {
5073 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5074 return false;
5075 }
5076 ViewParent parent = current.mParent;
5077 if (parent == null) {
5078 return false; // We are not attached to the view root
5079 }
5080 if (!(parent instanceof View)) {
5081 return true;
5082 }
5083 current = (View) parent;
5084 } while (current != null);
5085
5086 return false;
5087 }
5088
5089 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005090 * Called by the view hierarchy when the content insets for a window have
5091 * changed, to allow it to adjust its content to fit within those windows.
5092 * The content insets tell you the space that the status bar, input method,
5093 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005095 * <p>You do not normally need to deal with this function, since the default
5096 * window decoration given to applications takes care of applying it to the
5097 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5098 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5099 * and your content can be placed under those system elements. You can then
5100 * use this method within your view hierarchy if you have parts of your UI
5101 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005102 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005103 * <p>The default implementation of this method simply applies the content
5104 * inset's to the view's padding. This can be enabled through
5105 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5106 * the method and handle the insets however you would like. Note that the
5107 * insets provided by the framework are always relative to the far edges
5108 * of the window, not accounting for the location of the called view within
5109 * that window. (In fact when this method is called you do not yet know
5110 * where the layout will place the view, as it is done before layout happens.)
5111 *
5112 * <p>Note: unlike many View methods, there is no dispatch phase to this
5113 * call. If you are overriding it in a ViewGroup and want to allow the
5114 * call to continue to your children, you must be sure to call the super
5115 * implementation.
5116 *
5117 * @param insets Current content insets of the window. Prior to
5118 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5119 * the insets or else you and Android will be unhappy.
5120 *
5121 * @return Return true if this view applied the insets and it should not
5122 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 */
5124 protected boolean fitSystemWindows(Rect insets) {
5125 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005126 mUserPaddingStart = -1;
5127 mUserPaddingEnd = -1;
5128 mUserPaddingRelative = false;
5129 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5130 || mAttachInfo == null
5131 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5132 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5133 return true;
5134 } else {
5135 internalSetPadding(0, 0, 0, 0);
5136 return false;
5137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 }
5139 return false;
5140 }
5141
5142 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005143 * Set whether or not this view should account for system screen decorations
5144 * such as the status bar and inset its content. This allows this view to be
5145 * positioned in absolute screen coordinates and remain visible to the user.
5146 *
5147 * <p>This should only be used by top-level window decor views.
5148 *
5149 * @param fitSystemWindows true to inset content for system screen decorations, false for
5150 * default behavior.
5151 *
5152 * @attr ref android.R.styleable#View_fitsSystemWindows
5153 */
5154 public void setFitsSystemWindows(boolean fitSystemWindows) {
5155 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5156 }
5157
5158 /**
5159 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5160 * will account for system screen decorations such as the status bar and inset its
5161 * content. This allows the view to be positioned in absolute screen coordinates
5162 * and remain visible to the user.
5163 *
5164 * @return true if this view will adjust its content bounds for system screen decorations.
5165 *
5166 * @attr ref android.R.styleable#View_fitsSystemWindows
5167 */
5168 public boolean fitsSystemWindows() {
5169 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5170 }
5171
5172 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005173 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5174 */
5175 public void requestFitSystemWindows() {
5176 if (mParent != null) {
5177 mParent.requestFitSystemWindows();
5178 }
5179 }
5180
5181 /**
5182 * For use by PhoneWindow to make its own system window fitting optional.
5183 * @hide
5184 */
5185 public void makeOptionalFitsSystemWindows() {
5186 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5187 }
5188
5189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 * Returns the visibility status for this view.
5191 *
5192 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5193 * @attr ref android.R.styleable#View_visibility
5194 */
5195 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005196 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5197 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5198 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 })
5200 public int getVisibility() {
5201 return mViewFlags & VISIBILITY_MASK;
5202 }
5203
5204 /**
5205 * Set the enabled state of this view.
5206 *
5207 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5208 * @attr ref android.R.styleable#View_visibility
5209 */
5210 @RemotableViewMethod
5211 public void setVisibility(int visibility) {
5212 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005213 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 }
5215
5216 /**
5217 * Returns the enabled status for this view. The interpretation of the
5218 * enabled state varies by subclass.
5219 *
5220 * @return True if this view is enabled, false otherwise.
5221 */
5222 @ViewDebug.ExportedProperty
5223 public boolean isEnabled() {
5224 return (mViewFlags & ENABLED_MASK) == ENABLED;
5225 }
5226
5227 /**
5228 * Set the enabled state of this view. The interpretation of the enabled
5229 * state varies by subclass.
5230 *
5231 * @param enabled True if this view is enabled, false otherwise.
5232 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005233 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005235 if (enabled == isEnabled()) return;
5236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5238
5239 /*
5240 * The View most likely has to change its appearance, so refresh
5241 * the drawable state.
5242 */
5243 refreshDrawableState();
5244
5245 // Invalidate too, since the default behavior for views is to be
5246 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005247 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 }
5249
5250 /**
5251 * Set whether this view can receive the focus.
5252 *
5253 * Setting this to false will also ensure that this view is not focusable
5254 * in touch mode.
5255 *
5256 * @param focusable If true, this view can receive the focus.
5257 *
5258 * @see #setFocusableInTouchMode(boolean)
5259 * @attr ref android.R.styleable#View_focusable
5260 */
5261 public void setFocusable(boolean focusable) {
5262 if (!focusable) {
5263 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5264 }
5265 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5266 }
5267
5268 /**
5269 * Set whether this view can receive focus while in touch mode.
5270 *
5271 * Setting this to true will also ensure that this view is focusable.
5272 *
5273 * @param focusableInTouchMode If true, this view can receive the focus while
5274 * in touch mode.
5275 *
5276 * @see #setFocusable(boolean)
5277 * @attr ref android.R.styleable#View_focusableInTouchMode
5278 */
5279 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5280 // Focusable in touch mode should always be set before the focusable flag
5281 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5282 // which, in touch mode, will not successfully request focus on this view
5283 // because the focusable in touch mode flag is not set
5284 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5285 if (focusableInTouchMode) {
5286 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5287 }
5288 }
5289
5290 /**
5291 * Set whether this view should have sound effects enabled for events such as
5292 * clicking and touching.
5293 *
5294 * <p>You may wish to disable sound effects for a view if you already play sounds,
5295 * for instance, a dial key that plays dtmf tones.
5296 *
5297 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5298 * @see #isSoundEffectsEnabled()
5299 * @see #playSoundEffect(int)
5300 * @attr ref android.R.styleable#View_soundEffectsEnabled
5301 */
5302 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5303 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5304 }
5305
5306 /**
5307 * @return whether this view should have sound effects enabled for events such as
5308 * clicking and touching.
5309 *
5310 * @see #setSoundEffectsEnabled(boolean)
5311 * @see #playSoundEffect(int)
5312 * @attr ref android.R.styleable#View_soundEffectsEnabled
5313 */
5314 @ViewDebug.ExportedProperty
5315 public boolean isSoundEffectsEnabled() {
5316 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5317 }
5318
5319 /**
5320 * Set whether this view should have haptic feedback for events such as
5321 * long presses.
5322 *
5323 * <p>You may wish to disable haptic feedback if your view already controls
5324 * its own haptic feedback.
5325 *
5326 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5327 * @see #isHapticFeedbackEnabled()
5328 * @see #performHapticFeedback(int)
5329 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5330 */
5331 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5332 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5333 }
5334
5335 /**
5336 * @return whether this view should have haptic feedback enabled for events
5337 * long presses.
5338 *
5339 * @see #setHapticFeedbackEnabled(boolean)
5340 * @see #performHapticFeedback(int)
5341 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5342 */
5343 @ViewDebug.ExportedProperty
5344 public boolean isHapticFeedbackEnabled() {
5345 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5346 }
5347
5348 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005349 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005350 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005351 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5352 * {@link #LAYOUT_DIRECTION_RTL},
5353 * {@link #LAYOUT_DIRECTION_INHERIT} or
5354 * {@link #LAYOUT_DIRECTION_LOCALE}.
5355 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005356 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005357 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005358 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5359 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5360 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5361 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005362 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005363 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005364 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005365 }
5366
5367 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005368 * Set the layout direction for this view. This will propagate a reset of layout direction
5369 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005370 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005371 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5372 * {@link #LAYOUT_DIRECTION_RTL},
5373 * {@link #LAYOUT_DIRECTION_INHERIT} or
5374 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005375 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005376 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005377 */
5378 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005379 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005380 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005381 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005382 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005383 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005384 // Set the new layout direction (filtered) and ask for a layout pass
5385 mPrivateFlags2 |=
5386 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5387 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005388 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005389 }
5390
5391 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005392 * Returns the resolved layout direction for this view.
5393 *
5394 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005395 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005396 */
5397 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005398 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5399 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005400 })
5401 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005402 // The layout diretion will be resolved only if needed
5403 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5404 resolveLayoutDirection();
5405 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005406 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005407 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5408 }
5409
5410 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005411 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5412 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005413 *
5414 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005415 */
5416 @ViewDebug.ExportedProperty(category = "layout")
5417 public boolean isLayoutRtl() {
5418 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5419 }
5420
5421 /**
Adam Powell539ee872012-02-03 19:00:49 -08005422 * Indicates whether the view is currently tracking transient state that the
5423 * app should not need to concern itself with saving and restoring, but that
5424 * the framework should take special note to preserve when possible.
5425 *
Adam Powell785c4472012-05-02 21:25:39 -07005426 * <p>A view with transient state cannot be trivially rebound from an external
5427 * data source, such as an adapter binding item views in a list. This may be
5428 * because the view is performing an animation, tracking user selection
5429 * of content, or similar.</p>
5430 *
Adam Powell539ee872012-02-03 19:00:49 -08005431 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005432 */
5433 @ViewDebug.ExportedProperty(category = "layout")
5434 public boolean hasTransientState() {
5435 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5436 }
5437
5438 /**
5439 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005440 * framework should attempt to preserve when possible. This flag is reference counted,
5441 * so every call to setHasTransientState(true) should be paired with a later call
5442 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005443 *
Adam Powell785c4472012-05-02 21:25:39 -07005444 * <p>A view with transient state cannot be trivially rebound from an external
5445 * data source, such as an adapter binding item views in a list. This may be
5446 * because the view is performing an animation, tracking user selection
5447 * of content, or similar.</p>
5448 *
Adam Powell539ee872012-02-03 19:00:49 -08005449 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005450 */
5451 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005452 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5453 mTransientStateCount - 1;
5454 if (mTransientStateCount < 0) {
5455 mTransientStateCount = 0;
5456 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5457 "unmatched pair of setHasTransientState calls");
5458 }
5459 if ((hasTransientState && mTransientStateCount == 1) ||
5460 (hasTransientState && mTransientStateCount == 0)) {
5461 // update flag if we've just incremented up from 0 or decremented down to 0
5462 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5463 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5464 if (mParent != null) {
5465 try {
5466 mParent.childHasTransientStateChanged(this, hasTransientState);
5467 } catch (AbstractMethodError e) {
5468 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5469 " does not fully implement ViewParent", e);
5470 }
Adam Powell539ee872012-02-03 19:00:49 -08005471 }
5472 }
5473 }
5474
5475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 * If this view doesn't do any drawing on its own, set this flag to
5477 * allow further optimizations. By default, this flag is not set on
5478 * View, but could be set on some View subclasses such as ViewGroup.
5479 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005480 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5481 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 *
5483 * @param willNotDraw whether or not this View draw on its own
5484 */
5485 public void setWillNotDraw(boolean willNotDraw) {
5486 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5487 }
5488
5489 /**
5490 * Returns whether or not this View draws on its own.
5491 *
5492 * @return true if this view has nothing to draw, false otherwise
5493 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005494 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 public boolean willNotDraw() {
5496 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5497 }
5498
5499 /**
5500 * When a View's drawing cache is enabled, drawing is redirected to an
5501 * offscreen bitmap. Some views, like an ImageView, must be able to
5502 * bypass this mechanism if they already draw a single bitmap, to avoid
5503 * unnecessary usage of the memory.
5504 *
5505 * @param willNotCacheDrawing true if this view does not cache its
5506 * drawing, false otherwise
5507 */
5508 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5509 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5510 }
5511
5512 /**
5513 * Returns whether or not this View can cache its drawing or not.
5514 *
5515 * @return true if this view does not cache its drawing, false otherwise
5516 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005517 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 public boolean willNotCacheDrawing() {
5519 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5520 }
5521
5522 /**
5523 * Indicates whether this view reacts to click events or not.
5524 *
5525 * @return true if the view is clickable, false otherwise
5526 *
5527 * @see #setClickable(boolean)
5528 * @attr ref android.R.styleable#View_clickable
5529 */
5530 @ViewDebug.ExportedProperty
5531 public boolean isClickable() {
5532 return (mViewFlags & CLICKABLE) == CLICKABLE;
5533 }
5534
5535 /**
5536 * Enables or disables click events for this view. When a view
5537 * is clickable it will change its state to "pressed" on every click.
5538 * Subclasses should set the view clickable to visually react to
5539 * user's clicks.
5540 *
5541 * @param clickable true to make the view clickable, false otherwise
5542 *
5543 * @see #isClickable()
5544 * @attr ref android.R.styleable#View_clickable
5545 */
5546 public void setClickable(boolean clickable) {
5547 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5548 }
5549
5550 /**
5551 * Indicates whether this view reacts to long click events or not.
5552 *
5553 * @return true if the view is long clickable, false otherwise
5554 *
5555 * @see #setLongClickable(boolean)
5556 * @attr ref android.R.styleable#View_longClickable
5557 */
5558 public boolean isLongClickable() {
5559 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5560 }
5561
5562 /**
5563 * Enables or disables long click events for this view. When a view is long
5564 * clickable it reacts to the user holding down the button for a longer
5565 * duration than a tap. This event can either launch the listener or a
5566 * context menu.
5567 *
5568 * @param longClickable true to make the view long clickable, false otherwise
5569 * @see #isLongClickable()
5570 * @attr ref android.R.styleable#View_longClickable
5571 */
5572 public void setLongClickable(boolean longClickable) {
5573 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5574 }
5575
5576 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005577 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005578 *
5579 * @see #isClickable()
5580 * @see #setClickable(boolean)
5581 *
5582 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5583 * the View's internal state from a previously set "pressed" state.
5584 */
5585 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005586 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 if (pressed) {
5589 mPrivateFlags |= PRESSED;
5590 } else {
5591 mPrivateFlags &= ~PRESSED;
5592 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005593
5594 if (needsRefresh) {
5595 refreshDrawableState();
5596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 dispatchSetPressed(pressed);
5598 }
5599
5600 /**
5601 * Dispatch setPressed to all of this View's children.
5602 *
5603 * @see #setPressed(boolean)
5604 *
5605 * @param pressed The new pressed state
5606 */
5607 protected void dispatchSetPressed(boolean pressed) {
5608 }
5609
5610 /**
5611 * Indicates whether the view is currently in pressed state. Unless
5612 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5613 * the pressed state.
5614 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005615 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 * @see #isClickable()
5617 * @see #setClickable(boolean)
5618 *
5619 * @return true if the view is currently pressed, false otherwise
5620 */
5621 public boolean isPressed() {
5622 return (mPrivateFlags & PRESSED) == PRESSED;
5623 }
5624
5625 /**
5626 * Indicates whether this view will save its state (that is,
5627 * whether its {@link #onSaveInstanceState} method will be called).
5628 *
5629 * @return Returns true if the view state saving is enabled, else false.
5630 *
5631 * @see #setSaveEnabled(boolean)
5632 * @attr ref android.R.styleable#View_saveEnabled
5633 */
5634 public boolean isSaveEnabled() {
5635 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5636 }
5637
5638 /**
5639 * Controls whether the saving of this view's state is
5640 * enabled (that is, whether its {@link #onSaveInstanceState} method
5641 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005642 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 * for its state to be saved. This flag can only disable the
5644 * saving of this view; any child views may still have their state saved.
5645 *
5646 * @param enabled Set to false to <em>disable</em> state saving, or true
5647 * (the default) to allow it.
5648 *
5649 * @see #isSaveEnabled()
5650 * @see #setId(int)
5651 * @see #onSaveInstanceState()
5652 * @attr ref android.R.styleable#View_saveEnabled
5653 */
5654 public void setSaveEnabled(boolean enabled) {
5655 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5656 }
5657
Jeff Brown85a31762010-09-01 17:01:00 -07005658 /**
5659 * Gets whether the framework should discard touches when the view's
5660 * window is obscured by another visible window.
5661 * Refer to the {@link View} security documentation for more details.
5662 *
5663 * @return True if touch filtering is enabled.
5664 *
5665 * @see #setFilterTouchesWhenObscured(boolean)
5666 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5667 */
5668 @ViewDebug.ExportedProperty
5669 public boolean getFilterTouchesWhenObscured() {
5670 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5671 }
5672
5673 /**
5674 * Sets whether the framework should discard touches when the view's
5675 * window is obscured by another visible window.
5676 * Refer to the {@link View} security documentation for more details.
5677 *
5678 * @param enabled True if touch filtering should be enabled.
5679 *
5680 * @see #getFilterTouchesWhenObscured
5681 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5682 */
5683 public void setFilterTouchesWhenObscured(boolean enabled) {
5684 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5685 FILTER_TOUCHES_WHEN_OBSCURED);
5686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687
5688 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005689 * Indicates whether the entire hierarchy under this view will save its
5690 * state when a state saving traversal occurs from its parent. The default
5691 * is true; if false, these views will not be saved unless
5692 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5693 *
5694 * @return Returns true if the view state saving from parent is enabled, else false.
5695 *
5696 * @see #setSaveFromParentEnabled(boolean)
5697 */
5698 public boolean isSaveFromParentEnabled() {
5699 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5700 }
5701
5702 /**
5703 * Controls whether the entire hierarchy under this view will save its
5704 * state when a state saving traversal occurs from its parent. The default
5705 * is true; if false, these views will not be saved unless
5706 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5707 *
5708 * @param enabled Set to false to <em>disable</em> state saving, or true
5709 * (the default) to allow it.
5710 *
5711 * @see #isSaveFromParentEnabled()
5712 * @see #setId(int)
5713 * @see #onSaveInstanceState()
5714 */
5715 public void setSaveFromParentEnabled(boolean enabled) {
5716 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5717 }
5718
5719
5720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 * Returns whether this View is able to take focus.
5722 *
5723 * @return True if this view can take focus, or false otherwise.
5724 * @attr ref android.R.styleable#View_focusable
5725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005726 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 public final boolean isFocusable() {
5728 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5729 }
5730
5731 /**
5732 * When a view is focusable, it may not want to take focus when in touch mode.
5733 * For example, a button would like focus when the user is navigating via a D-pad
5734 * so that the user can click on it, but once the user starts touching the screen,
5735 * the button shouldn't take focus
5736 * @return Whether the view is focusable in touch mode.
5737 * @attr ref android.R.styleable#View_focusableInTouchMode
5738 */
5739 @ViewDebug.ExportedProperty
5740 public final boolean isFocusableInTouchMode() {
5741 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5742 }
5743
5744 /**
5745 * Find the nearest view in the specified direction that can take focus.
5746 * This does not actually give focus to that view.
5747 *
5748 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5749 *
5750 * @return The nearest focusable in the specified direction, or null if none
5751 * can be found.
5752 */
5753 public View focusSearch(int direction) {
5754 if (mParent != null) {
5755 return mParent.focusSearch(this, direction);
5756 } else {
5757 return null;
5758 }
5759 }
5760
5761 /**
5762 * This method is the last chance for the focused view and its ancestors to
5763 * respond to an arrow key. This is called when the focused view did not
5764 * consume the key internally, nor could the view system find a new view in
5765 * the requested direction to give focus to.
5766 *
5767 * @param focused The currently focused view.
5768 * @param direction The direction focus wants to move. One of FOCUS_UP,
5769 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5770 * @return True if the this view consumed this unhandled move.
5771 */
5772 public boolean dispatchUnhandledMove(View focused, int direction) {
5773 return false;
5774 }
5775
5776 /**
5777 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005778 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005780 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5781 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 * @return The user specified next view, or null if there is none.
5783 */
5784 View findUserSetNextFocus(View root, int direction) {
5785 switch (direction) {
5786 case FOCUS_LEFT:
5787 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005788 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 case FOCUS_RIGHT:
5790 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005791 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 case FOCUS_UP:
5793 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005794 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 case FOCUS_DOWN:
5796 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005797 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005798 case FOCUS_FORWARD:
5799 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005800 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005801 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005802 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005803 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005804 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005805 @Override
5806 public boolean apply(View t) {
5807 return t.mNextFocusForwardId == id;
5808 }
5809 });
5810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 }
5812 return null;
5813 }
5814
Jeff Brown4dfbec22011-08-15 14:55:37 -07005815 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5816 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5817 @Override
5818 public boolean apply(View t) {
5819 return t.mID == childViewId;
5820 }
5821 });
5822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 if (result == null) {
5824 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5825 + "by user for id " + childViewId);
5826 }
5827 return result;
5828 }
5829
5830 /**
5831 * Find and return all focusable views that are descendants of this view,
5832 * possibly including this view if it is focusable itself.
5833 *
5834 * @param direction The direction of the focus
5835 * @return A list of focusable views
5836 */
5837 public ArrayList<View> getFocusables(int direction) {
5838 ArrayList<View> result = new ArrayList<View>(24);
5839 addFocusables(result, direction);
5840 return result;
5841 }
5842
5843 /**
5844 * Add any focusable views that are descendants of this view (possibly
5845 * including this view if it is focusable itself) to views. If we are in touch mode,
5846 * only add views that are also focusable in touch mode.
5847 *
5848 * @param views Focusable views found so far
5849 * @param direction The direction of the focus
5850 */
5851 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005852 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854
svetoslavganov75986cf2009-05-14 22:28:01 -07005855 /**
5856 * Adds any focusable views that are descendants of this view (possibly
5857 * including this view if it is focusable itself) to views. This method
5858 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005859 * only views focusable in touch mode if we are in touch mode or
5860 * only views that can take accessibility focus if accessibility is enabeld
5861 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005862 *
5863 * @param views Focusable views found so far or null if all we are interested is
5864 * the number of focusables.
5865 * @param direction The direction of the focus.
5866 * @param focusableMode The type of focusables to be added.
5867 *
5868 * @see #FOCUSABLES_ALL
5869 * @see #FOCUSABLES_TOUCH_MODE
5870 */
5871 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005872 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005873 return;
5874 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005875 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5876 if (AccessibilityManager.getInstance(mContext).isEnabled()
5877 && includeForAccessibility()) {
5878 views.add(this);
5879 return;
5880 }
5881 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005882 if (!isFocusable()) {
5883 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005884 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005885 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5886 && isInTouchMode() && !isFocusableInTouchMode()) {
5887 return;
5888 }
5889 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 }
5891
5892 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005893 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005894 * The search is performed by either the text that the View renders or the content
5895 * description that describes the view for accessibility purposes and the view does
5896 * not render or both. Clients can specify how the search is to be performed via
5897 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5898 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005899 *
5900 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005901 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005902 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005903 * @see #FIND_VIEWS_WITH_TEXT
5904 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5905 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005906 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005907 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005908 if (getAccessibilityNodeProvider() != null) {
5909 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5910 outViews.add(this);
5911 }
5912 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5913 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005914 String searchedLowerCase = searched.toString().toLowerCase();
5915 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5916 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5917 outViews.add(this);
5918 }
5919 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005920 }
5921
5922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 * Find and return all touchable views that are descendants of this view,
5924 * possibly including this view if it is touchable itself.
5925 *
5926 * @return A list of touchable views
5927 */
5928 public ArrayList<View> getTouchables() {
5929 ArrayList<View> result = new ArrayList<View>();
5930 addTouchables(result);
5931 return result;
5932 }
5933
5934 /**
5935 * Add any touchable views that are descendants of this view (possibly
5936 * including this view if it is touchable itself) to views.
5937 *
5938 * @param views Touchable views found so far
5939 */
5940 public void addTouchables(ArrayList<View> views) {
5941 final int viewFlags = mViewFlags;
5942
5943 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5944 && (viewFlags & ENABLED_MASK) == ENABLED) {
5945 views.add(this);
5946 }
5947 }
5948
5949 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005950 * Returns whether this View is accessibility focused.
5951 *
5952 * @return True if this View is accessibility focused.
5953 */
5954 boolean isAccessibilityFocused() {
5955 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5956 }
5957
5958 /**
5959 * Call this to try to give accessibility focus to this view.
5960 *
5961 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5962 * returns false or the view is no visible or the view already has accessibility
5963 * focus.
5964 *
5965 * See also {@link #focusSearch(int)}, which is what you call to say that you
5966 * have focus, and you want your parent to look for the next one.
5967 *
5968 * @return Whether this view actually took accessibility focus.
5969 *
5970 * @hide
5971 */
5972 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07005973 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
5974 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005975 return false;
5976 }
5977 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5978 return false;
5979 }
5980 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5981 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5982 ViewRootImpl viewRootImpl = getViewRootImpl();
5983 if (viewRootImpl != null) {
5984 viewRootImpl.setAccessibilityFocusedHost(this);
5985 }
5986 invalidate();
5987 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5988 notifyAccessibilityStateChanged();
5989 // Try to give input focus to this view - not a descendant.
5990 requestFocusNoSearch(View.FOCUS_DOWN, null);
5991 return true;
5992 }
5993 return false;
5994 }
5995
5996 /**
5997 * Call this to try to clear accessibility focus of this view.
5998 *
5999 * See also {@link #focusSearch(int)}, which is what you call to say that you
6000 * have focus, and you want your parent to look for the next one.
6001 *
6002 * @hide
6003 */
6004 public void clearAccessibilityFocus() {
6005 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6006 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6007 ViewRootImpl viewRootImpl = getViewRootImpl();
6008 if (viewRootImpl != null) {
6009 viewRootImpl.setAccessibilityFocusedHost(null);
6010 }
6011 invalidate();
6012 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6013 notifyAccessibilityStateChanged();
6014 // Try to move accessibility focus to the input focus.
6015 View rootView = getRootView();
6016 if (rootView != null) {
6017 View inputFocus = rootView.findFocus();
6018 if (inputFocus != null) {
6019 inputFocus.requestAccessibilityFocus();
6020 }
6021 }
6022 }
6023 }
6024
6025 /**
6026 * Find the best view to take accessibility focus from a hover.
6027 * This function finds the deepest actionable view and if that
6028 * fails ask the parent to take accessibility focus from hover.
6029 *
6030 * @param x The X hovered location in this view coorditantes.
6031 * @param y The Y hovered location in this view coorditantes.
6032 * @return Whether the request was handled.
6033 *
6034 * @hide
6035 */
6036 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6037 if (onRequestAccessibilityFocusFromHover(x, y)) {
6038 return true;
6039 }
6040 ViewParent parent = mParent;
6041 if (parent instanceof View) {
6042 View parentView = (View) parent;
6043
6044 float[] position = mAttachInfo.mTmpTransformLocation;
6045 position[0] = x;
6046 position[1] = y;
6047
6048 // Compensate for the transformation of the current matrix.
6049 if (!hasIdentityMatrix()) {
6050 getMatrix().mapPoints(position);
6051 }
6052
6053 // Compensate for the parent scroll and the offset
6054 // of this view stop from the parent top.
6055 position[0] += mLeft - parentView.mScrollX;
6056 position[1] += mTop - parentView.mScrollY;
6057
6058 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6059 }
6060 return false;
6061 }
6062
6063 /**
6064 * Requests to give this View focus from hover.
6065 *
6066 * @param x The X hovered location in this view coorditantes.
6067 * @param y The Y hovered location in this view coorditantes.
6068 * @return Whether the request was handled.
6069 *
6070 * @hide
6071 */
6072 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6073 if (includeForAccessibility()
6074 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6075 return requestAccessibilityFocus();
6076 }
6077 return false;
6078 }
6079
6080 /**
6081 * Clears accessibility focus without calling any callback methods
6082 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6083 * is used for clearing accessibility focus when giving this focus to
6084 * another view.
6085 */
6086 void clearAccessibilityFocusNoCallbacks() {
6087 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6088 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6089 invalidate();
6090 }
6091 }
6092
6093 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 * Call this to try to give focus to a specific view or to one of its
6095 * descendants.
6096 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006097 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6098 * false), or if it is focusable and it is not focusable in touch mode
6099 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006101 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 * have focus, and you want your parent to look for the next one.
6103 *
6104 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6105 * {@link #FOCUS_DOWN} and <code>null</code>.
6106 *
6107 * @return Whether this view or one of its descendants actually took focus.
6108 */
6109 public final boolean requestFocus() {
6110 return requestFocus(View.FOCUS_DOWN);
6111 }
6112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 /**
6114 * Call this to try to give focus to a specific view or to one of its
6115 * descendants and give it a hint about what direction focus is heading.
6116 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006117 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6118 * false), or if it is focusable and it is not focusable in touch mode
6119 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006121 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 * have focus, and you want your parent to look for the next one.
6123 *
6124 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6125 * <code>null</code> set for the previously focused rectangle.
6126 *
6127 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6128 * @return Whether this view or one of its descendants actually took focus.
6129 */
6130 public final boolean requestFocus(int direction) {
6131 return requestFocus(direction, null);
6132 }
6133
6134 /**
6135 * Call this to try to give focus to a specific view or to one of its descendants
6136 * and give it hints about the direction and a specific rectangle that the focus
6137 * is coming from. The rectangle can help give larger views a finer grained hint
6138 * about where focus is coming from, and therefore, where to show selection, or
6139 * forward focus change internally.
6140 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006141 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6142 * false), or if it is focusable and it is not focusable in touch mode
6143 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 *
6145 * A View will not take focus if it is not visible.
6146 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006147 * A View will not take focus if one of its parents has
6148 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6149 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006151 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 * have focus, and you want your parent to look for the next one.
6153 *
6154 * You may wish to override this method if your custom {@link View} has an internal
6155 * {@link View} that it wishes to forward the request to.
6156 *
6157 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6158 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6159 * to give a finer grained hint about where focus is coming from. May be null
6160 * if there is no hint.
6161 * @return Whether this view or one of its descendants actually took focus.
6162 */
6163 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006164 return requestFocusNoSearch(direction, previouslyFocusedRect);
6165 }
6166
6167 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 // need to be focusable
6169 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6170 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6171 return false;
6172 }
6173
6174 // need to be focusable in touch mode if in touch mode
6175 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006176 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6177 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 }
6179
6180 // need to not have any parents blocking us
6181 if (hasAncestorThatBlocksDescendantFocus()) {
6182 return false;
6183 }
6184
6185 handleFocusGainInternal(direction, previouslyFocusedRect);
6186 return true;
6187 }
6188
6189 /**
6190 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6191 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6192 * touch mode to request focus when they are touched.
6193 *
6194 * @return Whether this view or one of its descendants actually took focus.
6195 *
6196 * @see #isInTouchMode()
6197 *
6198 */
6199 public final boolean requestFocusFromTouch() {
6200 // Leave touch mode if we need to
6201 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006202 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006203 if (viewRoot != null) {
6204 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 }
6206 }
6207 return requestFocus(View.FOCUS_DOWN);
6208 }
6209
6210 /**
6211 * @return Whether any ancestor of this view blocks descendant focus.
6212 */
6213 private boolean hasAncestorThatBlocksDescendantFocus() {
6214 ViewParent ancestor = mParent;
6215 while (ancestor instanceof ViewGroup) {
6216 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6217 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6218 return true;
6219 } else {
6220 ancestor = vgAncestor.getParent();
6221 }
6222 }
6223 return false;
6224 }
6225
6226 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006227 * Gets the mode for determining whether this View is important for accessibility
6228 * which is if it fires accessibility events and if it is reported to
6229 * accessibility services that query the screen.
6230 *
6231 * @return The mode for determining whether a View is important for accessibility.
6232 *
6233 * @attr ref android.R.styleable#View_importantForAccessibility
6234 *
6235 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6236 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6237 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6238 */
6239 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6240 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6241 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6242 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6243 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6244 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6245 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6246 })
6247 public int getImportantForAccessibility() {
6248 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6249 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6250 }
6251
6252 /**
6253 * Sets how to determine whether this view is important for accessibility
6254 * which is if it fires accessibility events and if it is reported to
6255 * accessibility services that query the screen.
6256 *
6257 * @param mode How to determine whether this view is important for accessibility.
6258 *
6259 * @attr ref android.R.styleable#View_importantForAccessibility
6260 *
6261 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6262 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6263 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6264 */
6265 public void setImportantForAccessibility(int mode) {
6266 if (mode != getImportantForAccessibility()) {
6267 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6268 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6269 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6270 notifyAccessibilityStateChanged();
6271 }
6272 }
6273
6274 /**
6275 * Gets whether this view should be exposed for accessibility.
6276 *
6277 * @return Whether the view is exposed for accessibility.
6278 *
6279 * @hide
6280 */
6281 public boolean isImportantForAccessibility() {
6282 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6283 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6284 switch (mode) {
6285 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6286 return true;
6287 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6288 return false;
6289 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6290 return isActionableForAccessibility() || hasListenersForAccessibility();
6291 default:
6292 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6293 + mode);
6294 }
6295 }
6296
6297 /**
6298 * Gets the parent for accessibility purposes. Note that the parent for
6299 * accessibility is not necessary the immediate parent. It is the first
6300 * predecessor that is important for accessibility.
6301 *
6302 * @return The parent for accessibility purposes.
6303 */
6304 public ViewParent getParentForAccessibility() {
6305 if (mParent instanceof View) {
6306 View parentView = (View) mParent;
6307 if (parentView.includeForAccessibility()) {
6308 return mParent;
6309 } else {
6310 return mParent.getParentForAccessibility();
6311 }
6312 }
6313 return null;
6314 }
6315
6316 /**
6317 * Adds the children of a given View for accessibility. Since some Views are
6318 * not important for accessibility the children for accessibility are not
6319 * necessarily direct children of the riew, rather they are the first level of
6320 * descendants important for accessibility.
6321 *
6322 * @param children The list of children for accessibility.
6323 */
6324 public void addChildrenForAccessibility(ArrayList<View> children) {
6325 if (includeForAccessibility()) {
6326 children.add(this);
6327 }
6328 }
6329
6330 /**
6331 * Whether to regard this view for accessibility. A view is regarded for
6332 * accessibility if it is important for accessibility or the querying
6333 * accessibility service has explicitly requested that view not
6334 * important for accessibility are regarded.
6335 *
6336 * @return Whether to regard the view for accessibility.
6337 */
6338 boolean includeForAccessibility() {
6339 if (mAttachInfo != null) {
6340 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006341 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006342 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006343 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006344 }
6345 }
6346 return false;
6347 }
6348
6349 /**
6350 * Returns whether the View is considered actionable from
6351 * accessibility perspective. Such view are important for
6352 * accessiiblity.
6353 *
6354 * @return True if the view is actionable for accessibility.
6355 */
6356 private boolean isActionableForAccessibility() {
6357 return (isClickable() || isLongClickable() || isFocusable());
6358 }
6359
6360 /**
6361 * Returns whether the View has registered callbacks wich makes it
6362 * important for accessiiblity.
6363 *
6364 * @return True if the view is actionable for accessibility.
6365 */
6366 private boolean hasListenersForAccessibility() {
6367 ListenerInfo info = getListenerInfo();
6368 return mTouchDelegate != null || info.mOnKeyListener != null
6369 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6370 || info.mOnHoverListener != null || info.mOnDragListener != null;
6371 }
6372
6373 /**
6374 * Notifies accessibility services that some view's important for
6375 * accessibility state has changed. Note that such notifications
6376 * are made at most once every
6377 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6378 * to avoid unnecessary load to the system. Also once a view has
6379 * made a notifucation this method is a NOP until the notification has
6380 * been sent to clients.
6381 *
6382 * @hide
6383 *
6384 * TODO: Makse sure this method is called for any view state change
6385 * that is interesting for accessilility purposes.
6386 */
6387 public void notifyAccessibilityStateChanged() {
6388 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6389 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6390 if (mParent != null) {
6391 mParent.childAccessibilityStateChanged(this);
6392 }
6393 }
6394 }
6395
6396 /**
6397 * Reset the state indicating the this view has requested clients
6398 * interested in its accessiblity state to be notified.
6399 *
6400 * @hide
6401 */
6402 public void resetAccessibilityStateChanged() {
6403 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6404 }
6405
6406 /**
6407 * Performs the specified accessibility action on the view. For
6408 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6409 *
6410 * @param action The action to perform.
6411 * @return Whether the action was performed.
6412 */
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07006413 public boolean performAccessibilityAction(int action, Bundle args) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006414 switch (action) {
6415 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006416 if (isClickable()) {
6417 performClick();
6418 }
6419 } break;
6420 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6421 if (isLongClickable()) {
6422 performLongClick();
6423 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006424 } break;
6425 case AccessibilityNodeInfo.ACTION_FOCUS: {
6426 if (!hasFocus()) {
6427 // Get out of touch mode since accessibility
6428 // wants to move focus around.
6429 getViewRootImpl().ensureTouchMode(false);
6430 return requestFocus();
6431 }
6432 } break;
6433 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6434 if (hasFocus()) {
6435 clearFocus();
6436 return !isFocused();
6437 }
6438 } break;
6439 case AccessibilityNodeInfo.ACTION_SELECT: {
6440 if (!isSelected()) {
6441 setSelected(true);
6442 return isSelected();
6443 }
6444 } break;
6445 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6446 if (isSelected()) {
6447 setSelected(false);
6448 return !isSelected();
6449 }
6450 } break;
6451 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6452 if (!isAccessibilityFocused()) {
6453 return requestAccessibilityFocus();
6454 }
6455 } break;
6456 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6457 if (isAccessibilityFocused()) {
6458 clearAccessibilityFocus();
6459 return true;
6460 }
6461 } break;
6462 }
6463 return false;
6464 }
6465
6466 /**
Romain Guya440b002010-02-24 15:57:54 -08006467 * @hide
6468 */
6469 public void dispatchStartTemporaryDetach() {
6470 onStartTemporaryDetach();
6471 }
6472
6473 /**
6474 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6476 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006477 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 */
6479 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006480 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006481 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006482 }
6483
6484 /**
6485 * @hide
6486 */
6487 public void dispatchFinishTemporaryDetach() {
6488 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 }
Romain Guy8506ab42009-06-11 17:35:47 -07006490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 /**
6492 * Called after {@link #onStartTemporaryDetach} when the container is done
6493 * changing the view.
6494 */
6495 public void onFinishTemporaryDetach() {
6496 }
Romain Guy8506ab42009-06-11 17:35:47 -07006497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006499 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6500 * for this view's window. Returns null if the view is not currently attached
6501 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006502 * just use the standard high-level event callbacks like
6503 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006504 */
6505 public KeyEvent.DispatcherState getKeyDispatcherState() {
6506 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6507 }
Joe Malin32736f02011-01-19 16:14:20 -08006508
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 * Dispatch a key event before it is processed by any input method
6511 * associated with the view hierarchy. This can be used to intercept
6512 * key events in special situations before the IME consumes them; a
6513 * typical example would be handling the BACK key to update the application's
6514 * UI instead of allowing the IME to see it and close itself.
6515 *
6516 * @param event The key event to be dispatched.
6517 * @return True if the event was handled, false otherwise.
6518 */
6519 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6520 return onKeyPreIme(event.getKeyCode(), event);
6521 }
6522
6523 /**
6524 * Dispatch a key event to the next view on the focus path. This path runs
6525 * from the top of the view tree down to the currently focused view. If this
6526 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6527 * the next node down the focus path. This method also fires any key
6528 * listeners.
6529 *
6530 * @param event The key event to be dispatched.
6531 * @return True if the event was handled, false otherwise.
6532 */
6533 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006534 if (mInputEventConsistencyVerifier != null) {
6535 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537
Jeff Brown21bc5c92011-02-28 18:27:14 -08006538 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006539 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006540 ListenerInfo li = mListenerInfo;
6541 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6542 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 return true;
6544 }
6545
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006546 if (event.dispatch(this, mAttachInfo != null
6547 ? mAttachInfo.mKeyDispatchState : null, this)) {
6548 return true;
6549 }
6550
6551 if (mInputEventConsistencyVerifier != null) {
6552 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6553 }
6554 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 }
6556
6557 /**
6558 * Dispatches a key shortcut event.
6559 *
6560 * @param event The key event to be dispatched.
6561 * @return True if the event was handled by the view, false otherwise.
6562 */
6563 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6564 return onKeyShortcut(event.getKeyCode(), event);
6565 }
6566
6567 /**
6568 * Pass the touch screen motion event down to the target view, or this
6569 * view if it is the target.
6570 *
6571 * @param event The motion event to be dispatched.
6572 * @return True if the event was handled by the view, false otherwise.
6573 */
6574 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006575 if (mInputEventConsistencyVerifier != null) {
6576 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6577 }
6578
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006579 if (onFilterTouchEventForSecurity(event)) {
6580 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006581 ListenerInfo li = mListenerInfo;
6582 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6583 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006584 return true;
6585 }
6586
6587 if (onTouchEvent(event)) {
6588 return true;
6589 }
Jeff Brown85a31762010-09-01 17:01:00 -07006590 }
6591
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006592 if (mInputEventConsistencyVerifier != null) {
6593 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006595 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 }
6597
6598 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006599 * Filter the touch event to apply security policies.
6600 *
6601 * @param event The motion event to be filtered.
6602 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006603 *
Jeff Brown85a31762010-09-01 17:01:00 -07006604 * @see #getFilterTouchesWhenObscured
6605 */
6606 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006607 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006608 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6609 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6610 // Window is obscured, drop this touch.
6611 return false;
6612 }
6613 return true;
6614 }
6615
6616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 * Pass a trackball motion event down to the focused view.
6618 *
6619 * @param event The motion event to be dispatched.
6620 * @return True if the event was handled by the view, false otherwise.
6621 */
6622 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006623 if (mInputEventConsistencyVerifier != null) {
6624 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6625 }
6626
Romain Guy02ccac62011-06-24 13:20:23 -07006627 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 }
6629
6630 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006631 * Dispatch a generic motion event.
6632 * <p>
6633 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6634 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006635 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006636 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006637 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006638 *
6639 * @param event The motion event to be dispatched.
6640 * @return True if the event was handled by the view, false otherwise.
6641 */
6642 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006643 if (mInputEventConsistencyVerifier != null) {
6644 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6645 }
6646
Jeff Browna032cc02011-03-07 16:56:21 -08006647 final int source = event.getSource();
6648 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6649 final int action = event.getAction();
6650 if (action == MotionEvent.ACTION_HOVER_ENTER
6651 || action == MotionEvent.ACTION_HOVER_MOVE
6652 || action == MotionEvent.ACTION_HOVER_EXIT) {
6653 if (dispatchHoverEvent(event)) {
6654 return true;
6655 }
6656 } else if (dispatchGenericPointerEvent(event)) {
6657 return true;
6658 }
6659 } else if (dispatchGenericFocusedEvent(event)) {
6660 return true;
6661 }
6662
Jeff Brown10b62902011-06-20 16:40:37 -07006663 if (dispatchGenericMotionEventInternal(event)) {
6664 return true;
6665 }
6666
6667 if (mInputEventConsistencyVerifier != null) {
6668 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6669 }
6670 return false;
6671 }
6672
6673 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006674 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006675 ListenerInfo li = mListenerInfo;
6676 if (li != null && li.mOnGenericMotionListener != null
6677 && (mViewFlags & ENABLED_MASK) == ENABLED
6678 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006679 return true;
6680 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006681
6682 if (onGenericMotionEvent(event)) {
6683 return true;
6684 }
6685
6686 if (mInputEventConsistencyVerifier != null) {
6687 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6688 }
6689 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006690 }
6691
6692 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006693 * Dispatch a hover event.
6694 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006695 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006696 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006697 * </p>
6698 *
6699 * @param event The motion event to be dispatched.
6700 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006701 */
6702 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006703 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006704 ListenerInfo li = mListenerInfo;
6705 if (li != null && li.mOnHoverListener != null
6706 && (mViewFlags & ENABLED_MASK) == ENABLED
6707 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006708 return true;
6709 }
6710
Jeff Browna032cc02011-03-07 16:56:21 -08006711 return onHoverEvent(event);
6712 }
6713
6714 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006715 * Returns true if the view has a child to which it has recently sent
6716 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6717 * it does not have a hovered child, then it must be the innermost hovered view.
6718 * @hide
6719 */
6720 protected boolean hasHoveredChild() {
6721 return false;
6722 }
6723
6724 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006725 * Dispatch a generic motion event to the view under the first pointer.
6726 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006727 * Do not call this method directly.
6728 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006729 * </p>
6730 *
6731 * @param event The motion event to be dispatched.
6732 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006733 */
6734 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6735 return false;
6736 }
6737
6738 /**
6739 * Dispatch a generic motion event to the currently focused view.
6740 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006741 * Do not call this method directly.
6742 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006743 * </p>
6744 *
6745 * @param event The motion event to be dispatched.
6746 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006747 */
6748 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6749 return false;
6750 }
6751
6752 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006753 * Dispatch a pointer event.
6754 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006755 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6756 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6757 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006758 * and should not be expected to handle other pointing device features.
6759 * </p>
6760 *
6761 * @param event The motion event to be dispatched.
6762 * @return True if the event was handled by the view, false otherwise.
6763 * @hide
6764 */
6765 public final boolean dispatchPointerEvent(MotionEvent event) {
6766 if (event.isTouchEvent()) {
6767 return dispatchTouchEvent(event);
6768 } else {
6769 return dispatchGenericMotionEvent(event);
6770 }
6771 }
6772
6773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 * Called when the window containing this view gains or loses window focus.
6775 * ViewGroups should override to route to their children.
6776 *
6777 * @param hasFocus True if the window containing this view now has focus,
6778 * false otherwise.
6779 */
6780 public void dispatchWindowFocusChanged(boolean hasFocus) {
6781 onWindowFocusChanged(hasFocus);
6782 }
6783
6784 /**
6785 * Called when the window containing this view gains or loses focus. Note
6786 * that this is separate from view focus: to receive key events, both
6787 * your view and its window must have focus. If a window is displayed
6788 * on top of yours that takes input focus, then your own window will lose
6789 * focus but the view focus will remain unchanged.
6790 *
6791 * @param hasWindowFocus True if the window containing this view now has
6792 * focus, false otherwise.
6793 */
6794 public void onWindowFocusChanged(boolean hasWindowFocus) {
6795 InputMethodManager imm = InputMethodManager.peekInstance();
6796 if (!hasWindowFocus) {
6797 if (isPressed()) {
6798 setPressed(false);
6799 }
6800 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6801 imm.focusOut(this);
6802 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006803 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006804 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006805 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6807 imm.focusIn(this);
6808 }
6809 refreshDrawableState();
6810 }
6811
6812 /**
6813 * Returns true if this view is in a window that currently has window focus.
6814 * Note that this is not the same as the view itself having focus.
6815 *
6816 * @return True if this view is in a window that currently has window focus.
6817 */
6818 public boolean hasWindowFocus() {
6819 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6820 }
6821
6822 /**
Adam Powell326d8082009-12-09 15:10:07 -08006823 * Dispatch a view visibility change down the view hierarchy.
6824 * ViewGroups should override to route to their children.
6825 * @param changedView The view whose visibility changed. Could be 'this' or
6826 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006827 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6828 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006829 */
6830 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6831 onVisibilityChanged(changedView, visibility);
6832 }
6833
6834 /**
6835 * Called when the visibility of the view or an ancestor of the view is changed.
6836 * @param changedView The view whose visibility changed. Could be 'this' or
6837 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006838 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6839 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006840 */
6841 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006842 if (visibility == VISIBLE) {
6843 if (mAttachInfo != null) {
6844 initialAwakenScrollBars();
6845 } else {
6846 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6847 }
6848 }
Adam Powell326d8082009-12-09 15:10:07 -08006849 }
6850
6851 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006852 * Dispatch a hint about whether this view is displayed. For instance, when
6853 * a View moves out of the screen, it might receives a display hint indicating
6854 * the view is not displayed. Applications should not <em>rely</em> on this hint
6855 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006856 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006857 * @param hint A hint about whether or not this view is displayed:
6858 * {@link #VISIBLE} or {@link #INVISIBLE}.
6859 */
6860 public void dispatchDisplayHint(int hint) {
6861 onDisplayHint(hint);
6862 }
6863
6864 /**
6865 * Gives this view a hint about whether is displayed or not. For instance, when
6866 * a View moves out of the screen, it might receives a display hint indicating
6867 * the view is not displayed. Applications should not <em>rely</em> on this hint
6868 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006869 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006870 * @param hint A hint about whether or not this view is displayed:
6871 * {@link #VISIBLE} or {@link #INVISIBLE}.
6872 */
6873 protected void onDisplayHint(int hint) {
6874 }
6875
6876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 * Dispatch a window visibility change down the view hierarchy.
6878 * ViewGroups should override to route to their children.
6879 *
6880 * @param visibility The new visibility of the window.
6881 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006882 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 */
6884 public void dispatchWindowVisibilityChanged(int visibility) {
6885 onWindowVisibilityChanged(visibility);
6886 }
6887
6888 /**
6889 * Called when the window containing has change its visibility
6890 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6891 * that this tells you whether or not your window is being made visible
6892 * to the window manager; this does <em>not</em> tell you whether or not
6893 * your window is obscured by other windows on the screen, even if it
6894 * is itself visible.
6895 *
6896 * @param visibility The new visibility of the window.
6897 */
6898 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006899 if (visibility == VISIBLE) {
6900 initialAwakenScrollBars();
6901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006902 }
6903
6904 /**
6905 * Returns the current visibility of the window this view is attached to
6906 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6907 *
6908 * @return Returns the current visibility of the view's window.
6909 */
6910 public int getWindowVisibility() {
6911 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6912 }
6913
6914 /**
6915 * Retrieve the overall visible display size in which the window this view is
6916 * attached to has been positioned in. This takes into account screen
6917 * decorations above the window, for both cases where the window itself
6918 * is being position inside of them or the window is being placed under
6919 * then and covered insets are used for the window to position its content
6920 * inside. In effect, this tells you the available area where content can
6921 * be placed and remain visible to users.
6922 *
6923 * <p>This function requires an IPC back to the window manager to retrieve
6924 * the requested information, so should not be used in performance critical
6925 * code like drawing.
6926 *
6927 * @param outRect Filled in with the visible display frame. If the view
6928 * is not attached to a window, this is simply the raw display size.
6929 */
6930 public void getWindowVisibleDisplayFrame(Rect outRect) {
6931 if (mAttachInfo != null) {
6932 try {
6933 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6934 } catch (RemoteException e) {
6935 return;
6936 }
6937 // XXX This is really broken, and probably all needs to be done
6938 // in the window manager, and we need to know more about whether
6939 // we want the area behind or in front of the IME.
6940 final Rect insets = mAttachInfo.mVisibleInsets;
6941 outRect.left += insets.left;
6942 outRect.top += insets.top;
6943 outRect.right -= insets.right;
6944 outRect.bottom -= insets.bottom;
6945 return;
6946 }
6947 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006948 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 }
6950
6951 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006952 * Dispatch a notification about a resource configuration change down
6953 * the view hierarchy.
6954 * ViewGroups should override to route to their children.
6955 *
6956 * @param newConfig The new resource configuration.
6957 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006958 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006959 */
6960 public void dispatchConfigurationChanged(Configuration newConfig) {
6961 onConfigurationChanged(newConfig);
6962 }
6963
6964 /**
6965 * Called when the current configuration of the resources being used
6966 * by the application have changed. You can use this to decide when
6967 * to reload resources that can changed based on orientation and other
6968 * configuration characterstics. You only need to use this if you are
6969 * not relying on the normal {@link android.app.Activity} mechanism of
6970 * recreating the activity instance upon a configuration change.
6971 *
6972 * @param newConfig The new resource configuration.
6973 */
6974 protected void onConfigurationChanged(Configuration newConfig) {
6975 }
6976
6977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 * Private function to aggregate all per-view attributes in to the view
6979 * root.
6980 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006981 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6982 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 }
6984
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006985 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6986 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006987 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006988 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006989 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006990 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006991 ListenerInfo li = mListenerInfo;
6992 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006993 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 }
6996 }
6997
6998 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006999 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007001 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7002 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 ai.mRecomputeGlobalAttributes = true;
7004 }
7005 }
7006 }
7007
7008 /**
7009 * Returns whether the device is currently in touch mode. Touch mode is entered
7010 * once the user begins interacting with the device by touch, and affects various
7011 * things like whether focus is always visible to the user.
7012 *
7013 * @return Whether the device is in touch mode.
7014 */
7015 @ViewDebug.ExportedProperty
7016 public boolean isInTouchMode() {
7017 if (mAttachInfo != null) {
7018 return mAttachInfo.mInTouchMode;
7019 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007020 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021 }
7022 }
7023
7024 /**
7025 * Returns the context the view is running in, through which it can
7026 * access the current theme, resources, etc.
7027 *
7028 * @return The view's Context.
7029 */
7030 @ViewDebug.CapturedViewProperty
7031 public final Context getContext() {
7032 return mContext;
7033 }
7034
7035 /**
7036 * Handle a key event before it is processed by any input method
7037 * associated with the view hierarchy. This can be used to intercept
7038 * key events in special situations before the IME consumes them; a
7039 * typical example would be handling the BACK key to update the application's
7040 * UI instead of allowing the IME to see it and close itself.
7041 *
7042 * @param keyCode The value in event.getKeyCode().
7043 * @param event Description of the key event.
7044 * @return If you handled the event, return true. If you want to allow the
7045 * event to be handled by the next receiver, return false.
7046 */
7047 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7048 return false;
7049 }
7050
7051 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007052 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7053 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7055 * is released, if the view is enabled and clickable.
7056 *
7057 * @param keyCode A key code that represents the button pressed, from
7058 * {@link android.view.KeyEvent}.
7059 * @param event The KeyEvent object that defines the button action.
7060 */
7061 public boolean onKeyDown(int keyCode, KeyEvent event) {
7062 boolean result = false;
7063
7064 switch (keyCode) {
7065 case KeyEvent.KEYCODE_DPAD_CENTER:
7066 case KeyEvent.KEYCODE_ENTER: {
7067 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7068 return true;
7069 }
7070 // Long clickable items don't necessarily have to be clickable
7071 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7072 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7073 (event.getRepeatCount() == 0)) {
7074 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007075 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 return true;
7077 }
7078 break;
7079 }
7080 }
7081 return result;
7082 }
7083
7084 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007085 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7086 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7087 * the event).
7088 */
7089 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7090 return false;
7091 }
7092
7093 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007094 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7095 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7097 * {@link KeyEvent#KEYCODE_ENTER} is released.
7098 *
7099 * @param keyCode A key code that represents the button pressed, from
7100 * {@link android.view.KeyEvent}.
7101 * @param event The KeyEvent object that defines the button action.
7102 */
7103 public boolean onKeyUp(int keyCode, KeyEvent event) {
7104 boolean result = false;
7105
7106 switch (keyCode) {
7107 case KeyEvent.KEYCODE_DPAD_CENTER:
7108 case KeyEvent.KEYCODE_ENTER: {
7109 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7110 return true;
7111 }
7112 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7113 setPressed(false);
7114
7115 if (!mHasPerformedLongPress) {
7116 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007117 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118
7119 result = performClick();
7120 }
7121 }
7122 break;
7123 }
7124 }
7125 return result;
7126 }
7127
7128 /**
7129 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7130 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7131 * the event).
7132 *
7133 * @param keyCode A key code that represents the button pressed, from
7134 * {@link android.view.KeyEvent}.
7135 * @param repeatCount The number of times the action was made.
7136 * @param event The KeyEvent object that defines the button action.
7137 */
7138 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7139 return false;
7140 }
7141
7142 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007143 * Called on the focused view when a key shortcut event is not handled.
7144 * Override this method to implement local key shortcuts for the View.
7145 * Key shortcuts can also be implemented by setting the
7146 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007147 *
7148 * @param keyCode The value in event.getKeyCode().
7149 * @param event Description of the key event.
7150 * @return If you handled the event, return true. If you want to allow the
7151 * event to be handled by the next receiver, return false.
7152 */
7153 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7154 return false;
7155 }
7156
7157 /**
7158 * Check whether the called view is a text editor, in which case it
7159 * would make sense to automatically display a soft input window for
7160 * it. Subclasses should override this if they implement
7161 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007162 * a call on that method would return a non-null InputConnection, and
7163 * they are really a first-class editor that the user would normally
7164 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007165 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007166 * <p>The default implementation always returns false. This does
7167 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7168 * will not be called or the user can not otherwise perform edits on your
7169 * view; it is just a hint to the system that this is not the primary
7170 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 * @return Returns true if this view is a text editor, else false.
7173 */
7174 public boolean onCheckIsTextEditor() {
7175 return false;
7176 }
Romain Guy8506ab42009-06-11 17:35:47 -07007177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 /**
7179 * Create a new InputConnection for an InputMethod to interact
7180 * with the view. The default implementation returns null, since it doesn't
7181 * support input methods. You can override this to implement such support.
7182 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 * <p>When implementing this, you probably also want to implement
7185 * {@link #onCheckIsTextEditor()} to indicate you will return a
7186 * non-null InputConnection.
7187 *
7188 * @param outAttrs Fill in with attribute information about the connection.
7189 */
7190 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7191 return null;
7192 }
7193
7194 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007195 * Called by the {@link android.view.inputmethod.InputMethodManager}
7196 * when a view who is not the current
7197 * input connection target is trying to make a call on the manager. The
7198 * default implementation returns false; you can override this to return
7199 * true for certain views if you are performing InputConnection proxying
7200 * to them.
7201 * @param view The View that is making the InputMethodManager call.
7202 * @return Return true to allow the call, false to reject.
7203 */
7204 public boolean checkInputConnectionProxy(View view) {
7205 return false;
7206 }
Romain Guy8506ab42009-06-11 17:35:47 -07007207
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 * Show the context menu for this view. It is not safe to hold on to the
7210 * menu after returning from this method.
7211 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007212 * You should normally not overload this method. Overload
7213 * {@link #onCreateContextMenu(ContextMenu)} or define an
7214 * {@link OnCreateContextMenuListener} to add items to the context menu.
7215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 * @param menu The context menu to populate
7217 */
7218 public void createContextMenu(ContextMenu menu) {
7219 ContextMenuInfo menuInfo = getContextMenuInfo();
7220
7221 // Sets the current menu info so all items added to menu will have
7222 // my extra info set.
7223 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7224
7225 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007226 ListenerInfo li = mListenerInfo;
7227 if (li != null && li.mOnCreateContextMenuListener != null) {
7228 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 }
7230
7231 // Clear the extra information so subsequent items that aren't mine don't
7232 // have my extra info.
7233 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7234
7235 if (mParent != null) {
7236 mParent.createContextMenu(menu);
7237 }
7238 }
7239
7240 /**
7241 * Views should implement this if they have extra information to associate
7242 * with the context menu. The return result is supplied as a parameter to
7243 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7244 * callback.
7245 *
7246 * @return Extra information about the item for which the context menu
7247 * should be shown. This information will vary across different
7248 * subclasses of View.
7249 */
7250 protected ContextMenuInfo getContextMenuInfo() {
7251 return null;
7252 }
7253
7254 /**
7255 * Views should implement this if the view itself is going to add items to
7256 * the context menu.
7257 *
7258 * @param menu the context menu to populate
7259 */
7260 protected void onCreateContextMenu(ContextMenu menu) {
7261 }
7262
7263 /**
7264 * Implement this method to handle trackball motion events. The
7265 * <em>relative</em> movement of the trackball since the last event
7266 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7267 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7268 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7269 * they will often be fractional values, representing the more fine-grained
7270 * movement information available from a trackball).
7271 *
7272 * @param event The motion event.
7273 * @return True if the event was handled, false otherwise.
7274 */
7275 public boolean onTrackballEvent(MotionEvent event) {
7276 return false;
7277 }
7278
7279 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007280 * Implement this method to handle generic motion events.
7281 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007282 * Generic motion events describe joystick movements, mouse hovers, track pad
7283 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007284 * {@link MotionEvent#getSource() source} of the motion event specifies
7285 * the class of input that was received. Implementations of this method
7286 * must examine the bits in the source before processing the event.
7287 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007288 * </p><p>
7289 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7290 * are delivered to the view under the pointer. All other generic motion events are
7291 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007292 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007293 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007294 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007295 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7296 * // process the joystick movement...
7297 * return true;
7298 * }
7299 * }
7300 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7301 * switch (event.getAction()) {
7302 * case MotionEvent.ACTION_HOVER_MOVE:
7303 * // process the mouse hover movement...
7304 * return true;
7305 * case MotionEvent.ACTION_SCROLL:
7306 * // process the scroll wheel movement...
7307 * return true;
7308 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007309 * }
7310 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007311 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007312 *
7313 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007314 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007315 */
7316 public boolean onGenericMotionEvent(MotionEvent event) {
7317 return false;
7318 }
7319
7320 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007321 * Implement this method to handle hover events.
7322 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007323 * This method is called whenever a pointer is hovering into, over, or out of the
7324 * bounds of a view and the view is not currently being touched.
7325 * Hover events are represented as pointer events with action
7326 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7327 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7328 * </p>
7329 * <ul>
7330 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7331 * when the pointer enters the bounds of the view.</li>
7332 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7333 * when the pointer has already entered the bounds of the view and has moved.</li>
7334 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7335 * when the pointer has exited the bounds of the view or when the pointer is
7336 * about to go down due to a button click, tap, or similar user action that
7337 * causes the view to be touched.</li>
7338 * </ul>
7339 * <p>
7340 * The view should implement this method to return true to indicate that it is
7341 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007342 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007343 * The default implementation calls {@link #setHovered} to update the hovered state
7344 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007345 * is enabled and is clickable. The default implementation also sends hover
7346 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007347 * </p>
7348 *
7349 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007350 * @return True if the view handled the hover event.
7351 *
7352 * @see #isHovered
7353 * @see #setHovered
7354 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007355 */
7356 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007357 // The root view may receive hover (or touch) events that are outside the bounds of
7358 // the window. This code ensures that we only send accessibility events for
7359 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007360 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007361 if (!mSendingHoverAccessibilityEvents) {
7362 if ((action == MotionEvent.ACTION_HOVER_ENTER
7363 || action == MotionEvent.ACTION_HOVER_MOVE)
7364 && !hasHoveredChild()
7365 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007366 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007367 mSendingHoverAccessibilityEvents = true;
7368 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007369 }
7370 } else {
7371 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007372 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007373 && !pointInView(event.getX(), event.getY()))) {
7374 mSendingHoverAccessibilityEvents = false;
7375 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007376 // If the window does not have input focus we take away accessibility
7377 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007378 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007379 getViewRootImpl().setAccessibilityFocusedHost(null);
7380 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007381 }
Jeff Browna1b24182011-07-28 13:38:24 -07007382 }
7383
Jeff Brown87b7f802011-06-21 18:35:45 -07007384 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007385 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007386 case MotionEvent.ACTION_HOVER_ENTER:
7387 setHovered(true);
7388 break;
7389 case MotionEvent.ACTION_HOVER_EXIT:
7390 setHovered(false);
7391 break;
7392 }
Jeff Browna1b24182011-07-28 13:38:24 -07007393
7394 // Dispatch the event to onGenericMotionEvent before returning true.
7395 // This is to provide compatibility with existing applications that
7396 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7397 // break because of the new default handling for hoverable views
7398 // in onHoverEvent.
7399 // Note that onGenericMotionEvent will be called by default when
7400 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7401 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007402 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007403 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007404
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007405 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007406 }
7407
7408 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007409 * Returns true if the view should handle {@link #onHoverEvent}
7410 * by calling {@link #setHovered} to change its hovered state.
7411 *
7412 * @return True if the view is hoverable.
7413 */
7414 private boolean isHoverable() {
7415 final int viewFlags = mViewFlags;
7416 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7417 return false;
7418 }
7419
7420 return (viewFlags & CLICKABLE) == CLICKABLE
7421 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7422 }
7423
7424 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007425 * Returns true if the view is currently hovered.
7426 *
7427 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007428 *
7429 * @see #setHovered
7430 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007431 */
Jeff Brown10b62902011-06-20 16:40:37 -07007432 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007433 public boolean isHovered() {
7434 return (mPrivateFlags & HOVERED) != 0;
7435 }
7436
7437 /**
7438 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007439 * <p>
7440 * Calling this method also changes the drawable state of the view. This
7441 * enables the view to react to hover by using different drawable resources
7442 * to change its appearance.
7443 * </p><p>
7444 * The {@link #onHoverChanged} method is called when the hovered state changes.
7445 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007446 *
7447 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007448 *
7449 * @see #isHovered
7450 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007451 */
7452 public void setHovered(boolean hovered) {
7453 if (hovered) {
7454 if ((mPrivateFlags & HOVERED) == 0) {
7455 mPrivateFlags |= HOVERED;
7456 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007457 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007458 }
7459 } else {
7460 if ((mPrivateFlags & HOVERED) != 0) {
7461 mPrivateFlags &= ~HOVERED;
7462 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007463 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007464 }
7465 }
7466 }
7467
7468 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007469 * Implement this method to handle hover state changes.
7470 * <p>
7471 * This method is called whenever the hover state changes as a result of a
7472 * call to {@link #setHovered}.
7473 * </p>
7474 *
7475 * @param hovered The current hover state, as returned by {@link #isHovered}.
7476 *
7477 * @see #isHovered
7478 * @see #setHovered
7479 */
7480 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007481 }
7482
7483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 * Implement this method to handle touch screen motion events.
7485 *
7486 * @param event The motion event.
7487 * @return True if the event was handled, false otherwise.
7488 */
7489 public boolean onTouchEvent(MotionEvent event) {
7490 final int viewFlags = mViewFlags;
7491
7492 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007493 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007494 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 // A disabled view that is clickable still consumes the touch
7497 // events, it just doesn't respond to them.
7498 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7499 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7500 }
7501
7502 if (mTouchDelegate != null) {
7503 if (mTouchDelegate.onTouchEvent(event)) {
7504 return true;
7505 }
7506 }
7507
7508 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7509 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7510 switch (event.getAction()) {
7511 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007512 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7513 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 // take focus if we don't have it already and we should in
7515 // touch mode.
7516 boolean focusTaken = false;
7517 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7518 focusTaken = requestFocus();
7519 }
7520
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007521 if (prepressed) {
7522 // The button is being released before we actually
7523 // showed it as pressed. Make it show the pressed
7524 // state now (before scheduling the click) to ensure
7525 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007526 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007527 }
Joe Malin32736f02011-01-19 16:14:20 -08007528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007529 if (!mHasPerformedLongPress) {
7530 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007531 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007532
7533 // Only perform take click actions if we were in the pressed state
7534 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007535 // Use a Runnable and post this rather than calling
7536 // performClick directly. This lets other visual state
7537 // of the view update before click actions start.
7538 if (mPerformClick == null) {
7539 mPerformClick = new PerformClick();
7540 }
7541 if (!post(mPerformClick)) {
7542 performClick();
7543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544 }
7545 }
7546
7547 if (mUnsetPressedState == null) {
7548 mUnsetPressedState = new UnsetPressedState();
7549 }
7550
Adam Powelle14579b2009-12-16 18:39:52 -08007551 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007552 postDelayed(mUnsetPressedState,
7553 ViewConfiguration.getPressedStateDuration());
7554 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 // If the post failed, unpress right now
7556 mUnsetPressedState.run();
7557 }
Adam Powelle14579b2009-12-16 18:39:52 -08007558 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 }
7560 break;
7561
7562 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007563 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007564
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007565 if (performButtonActionOnTouchDown(event)) {
7566 break;
7567 }
7568
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007569 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007570 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007571
7572 // For views inside a scrolling container, delay the pressed feedback for
7573 // a short period in case this is a scroll.
7574 if (isInScrollingContainer) {
7575 mPrivateFlags |= PREPRESSED;
7576 if (mPendingCheckForTap == null) {
7577 mPendingCheckForTap = new CheckForTap();
7578 }
7579 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7580 } else {
7581 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007582 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007583 checkForLongClick(0);
7584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 break;
7586
7587 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007588 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007589 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590 break;
7591
7592 case MotionEvent.ACTION_MOVE:
7593 final int x = (int) event.getX();
7594 final int y = (int) event.getY();
7595
7596 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007597 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007599 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007601 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007602 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007603
Adam Powell4d6f0662012-02-21 15:11:11 -08007604 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 }
7607 break;
7608 }
7609 return true;
7610 }
7611
7612 return false;
7613 }
7614
7615 /**
Adam Powell10298662011-08-14 18:26:30 -07007616 * @hide
7617 */
7618 public boolean isInScrollingContainer() {
7619 ViewParent p = getParent();
7620 while (p != null && p instanceof ViewGroup) {
7621 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7622 return true;
7623 }
7624 p = p.getParent();
7625 }
7626 return false;
7627 }
7628
7629 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007630 * Remove the longpress detection timer.
7631 */
7632 private void removeLongPressCallback() {
7633 if (mPendingCheckForLongPress != null) {
7634 removeCallbacks(mPendingCheckForLongPress);
7635 }
7636 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007637
7638 /**
7639 * Remove the pending click action
7640 */
7641 private void removePerformClickCallback() {
7642 if (mPerformClick != null) {
7643 removeCallbacks(mPerformClick);
7644 }
7645 }
7646
Adam Powelle14579b2009-12-16 18:39:52 -08007647 /**
Romain Guya440b002010-02-24 15:57:54 -08007648 * Remove the prepress detection timer.
7649 */
7650 private void removeUnsetPressCallback() {
7651 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7652 setPressed(false);
7653 removeCallbacks(mUnsetPressedState);
7654 }
7655 }
7656
7657 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007658 * Remove the tap detection timer.
7659 */
7660 private void removeTapCallback() {
7661 if (mPendingCheckForTap != null) {
7662 mPrivateFlags &= ~PREPRESSED;
7663 removeCallbacks(mPendingCheckForTap);
7664 }
7665 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007666
7667 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 * Cancels a pending long press. Your subclass can use this if you
7669 * want the context menu to come up if the user presses and holds
7670 * at the same place, but you don't want it to come up if they press
7671 * and then move around enough to cause scrolling.
7672 */
7673 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007674 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007675
7676 /*
7677 * The prepressed state handled by the tap callback is a display
7678 * construct, but the tap callback will post a long press callback
7679 * less its own timeout. Remove it here.
7680 */
7681 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 }
7683
7684 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007685 * Remove the pending callback for sending a
7686 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7687 */
7688 private void removeSendViewScrolledAccessibilityEventCallback() {
7689 if (mSendViewScrolledAccessibilityEvent != null) {
7690 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7691 }
7692 }
7693
7694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 * Sets the TouchDelegate for this View.
7696 */
7697 public void setTouchDelegate(TouchDelegate delegate) {
7698 mTouchDelegate = delegate;
7699 }
7700
7701 /**
7702 * Gets the TouchDelegate for this View.
7703 */
7704 public TouchDelegate getTouchDelegate() {
7705 return mTouchDelegate;
7706 }
7707
7708 /**
7709 * Set flags controlling behavior of this view.
7710 *
7711 * @param flags Constant indicating the value which should be set
7712 * @param mask Constant indicating the bit range that should be changed
7713 */
7714 void setFlags(int flags, int mask) {
7715 int old = mViewFlags;
7716 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7717
7718 int changed = mViewFlags ^ old;
7719 if (changed == 0) {
7720 return;
7721 }
7722 int privateFlags = mPrivateFlags;
7723
7724 /* Check if the FOCUSABLE bit has changed */
7725 if (((changed & FOCUSABLE_MASK) != 0) &&
7726 ((privateFlags & HAS_BOUNDS) !=0)) {
7727 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7728 && ((privateFlags & FOCUSED) != 0)) {
7729 /* Give up focus if we are no longer focusable */
7730 clearFocus();
7731 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7732 && ((privateFlags & FOCUSED) == 0)) {
7733 /*
7734 * Tell the view system that we are now available to take focus
7735 * if no one else already has it.
7736 */
7737 if (mParent != null) mParent.focusableViewAvailable(this);
7738 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007739 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7740 notifyAccessibilityStateChanged();
7741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007742 }
7743
7744 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7745 if ((changed & VISIBILITY_MASK) != 0) {
7746 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007747 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007748 * it was not visible. Marking it drawn ensures that the invalidation will
7749 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007751 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007752 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753
7754 needGlobalAttributesUpdate(true);
7755
7756 // a view becoming visible is worth notifying the parent
7757 // about in case nothing has focus. even if this specific view
7758 // isn't focusable, it may contain something that is, so let
7759 // the root view try to give this focus if nothing else does.
7760 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7761 mParent.focusableViewAvailable(this);
7762 }
7763 }
7764 }
7765
7766 /* Check if the GONE bit has changed */
7767 if ((changed & GONE) != 0) {
7768 needGlobalAttributesUpdate(false);
7769 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770
Romain Guyecd80ee2009-12-03 17:13:02 -08007771 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7772 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007773 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007774 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007775 if (mParent instanceof View) {
7776 // GONE views noop invalidation, so invalidate the parent
7777 ((View) mParent).invalidate(true);
7778 }
7779 // Mark the view drawn to ensure that it gets invalidated properly the next
7780 // time it is visible and gets invalidated
7781 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 }
7783 if (mAttachInfo != null) {
7784 mAttachInfo.mViewVisibilityChanged = true;
7785 }
7786 }
7787
7788 /* Check if the VISIBLE bit has changed */
7789 if ((changed & INVISIBLE) != 0) {
7790 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007791 /*
7792 * If this view is becoming invisible, set the DRAWN flag so that
7793 * the next invalidate() will not be skipped.
7794 */
7795 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007796
7797 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007798 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 if (getRootView() != this) {
7800 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007801 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 }
7803 }
7804 if (mAttachInfo != null) {
7805 mAttachInfo.mViewVisibilityChanged = true;
7806 }
7807 }
7808
Adam Powell326d8082009-12-09 15:10:07 -08007809 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007810 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007811 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7812 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007813 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007814 } else if (mParent != null) {
7815 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007816 }
Adam Powell326d8082009-12-09 15:10:07 -08007817 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7818 }
7819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7821 destroyDrawingCache();
7822 }
7823
7824 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7825 destroyDrawingCache();
7826 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007827 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 }
7829
7830 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7831 destroyDrawingCache();
7832 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7833 }
7834
7835 if ((changed & DRAW_MASK) != 0) {
7836 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007837 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 mPrivateFlags &= ~SKIP_DRAW;
7839 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7840 } else {
7841 mPrivateFlags |= SKIP_DRAW;
7842 }
7843 } else {
7844 mPrivateFlags &= ~SKIP_DRAW;
7845 }
7846 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007847 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849
7850 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007851 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 mParent.recomputeViewAttributes(this);
7853 }
7854 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007855
7856 if (AccessibilityManager.getInstance(mContext).isEnabled()
7857 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7858 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7859 notifyAccessibilityStateChanged();
7860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 }
7862
7863 /**
7864 * Change the view's z order in the tree, so it's on top of other sibling
7865 * views
7866 */
7867 public void bringToFront() {
7868 if (mParent != null) {
7869 mParent.bringChildToFront(this);
7870 }
7871 }
7872
7873 /**
7874 * This is called in response to an internal scroll in this view (i.e., the
7875 * view scrolled its own contents). This is typically as a result of
7876 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7877 * called.
7878 *
7879 * @param l Current horizontal scroll origin.
7880 * @param t Current vertical scroll origin.
7881 * @param oldl Previous horizontal scroll origin.
7882 * @param oldt Previous vertical scroll origin.
7883 */
7884 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007885 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7886 postSendViewScrolledAccessibilityEventCallback();
7887 }
7888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 mBackgroundSizeChanged = true;
7890
7891 final AttachInfo ai = mAttachInfo;
7892 if (ai != null) {
7893 ai.mViewScrollChanged = true;
7894 }
7895 }
7896
7897 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007898 * Interface definition for a callback to be invoked when the layout bounds of a view
7899 * changes due to layout processing.
7900 */
7901 public interface OnLayoutChangeListener {
7902 /**
7903 * Called when the focus state of a view has changed.
7904 *
7905 * @param v The view whose state has changed.
7906 * @param left The new value of the view's left property.
7907 * @param top The new value of the view's top property.
7908 * @param right The new value of the view's right property.
7909 * @param bottom The new value of the view's bottom property.
7910 * @param oldLeft The previous value of the view's left property.
7911 * @param oldTop The previous value of the view's top property.
7912 * @param oldRight The previous value of the view's right property.
7913 * @param oldBottom The previous value of the view's bottom property.
7914 */
7915 void onLayoutChange(View v, int left, int top, int right, int bottom,
7916 int oldLeft, int oldTop, int oldRight, int oldBottom);
7917 }
7918
7919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 * This is called during layout when the size of this view has changed. If
7921 * you were just added to the view hierarchy, you're called with the old
7922 * values of 0.
7923 *
7924 * @param w Current width of this view.
7925 * @param h Current height of this view.
7926 * @param oldw Old width of this view.
7927 * @param oldh Old height of this view.
7928 */
7929 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7930 }
7931
7932 /**
7933 * Called by draw to draw the child views. This may be overridden
7934 * by derived classes to gain control just before its children are drawn
7935 * (but after its own view has been drawn).
7936 * @param canvas the canvas on which to draw the view
7937 */
7938 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 }
7941
7942 /**
7943 * Gets the parent of this view. Note that the parent is a
7944 * ViewParent and not necessarily a View.
7945 *
7946 * @return Parent of this view.
7947 */
7948 public final ViewParent getParent() {
7949 return mParent;
7950 }
7951
7952 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007953 * Set the horizontal scrolled position of your view. This will cause a call to
7954 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7955 * invalidated.
7956 * @param value the x position to scroll to
7957 */
7958 public void setScrollX(int value) {
7959 scrollTo(value, mScrollY);
7960 }
7961
7962 /**
7963 * Set the vertical scrolled position of your view. This will cause a call to
7964 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7965 * invalidated.
7966 * @param value the y position to scroll to
7967 */
7968 public void setScrollY(int value) {
7969 scrollTo(mScrollX, value);
7970 }
7971
7972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 * Return the scrolled left position of this view. This is the left edge of
7974 * the displayed part of your view. You do not need to draw any pixels
7975 * farther left, since those are outside of the frame of your view on
7976 * screen.
7977 *
7978 * @return The left edge of the displayed part of your view, in pixels.
7979 */
7980 public final int getScrollX() {
7981 return mScrollX;
7982 }
7983
7984 /**
7985 * Return the scrolled top position of this view. This is the top edge of
7986 * the displayed part of your view. You do not need to draw any pixels above
7987 * it, since those are outside of the frame of your view on screen.
7988 *
7989 * @return The top edge of the displayed part of your view, in pixels.
7990 */
7991 public final int getScrollY() {
7992 return mScrollY;
7993 }
7994
7995 /**
7996 * Return the width of the your view.
7997 *
7998 * @return The width of your view, in pixels.
7999 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008000 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008001 public final int getWidth() {
8002 return mRight - mLeft;
8003 }
8004
8005 /**
8006 * Return the height of your view.
8007 *
8008 * @return The height of your view, in pixels.
8009 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008010 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 public final int getHeight() {
8012 return mBottom - mTop;
8013 }
8014
8015 /**
8016 * Return the visible drawing bounds of your view. Fills in the output
8017 * rectangle with the values from getScrollX(), getScrollY(),
8018 * getWidth(), and getHeight().
8019 *
8020 * @param outRect The (scrolled) drawing bounds of the view.
8021 */
8022 public void getDrawingRect(Rect outRect) {
8023 outRect.left = mScrollX;
8024 outRect.top = mScrollY;
8025 outRect.right = mScrollX + (mRight - mLeft);
8026 outRect.bottom = mScrollY + (mBottom - mTop);
8027 }
8028
8029 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008030 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8031 * raw width component (that is the result is masked by
8032 * {@link #MEASURED_SIZE_MASK}).
8033 *
8034 * @return The raw measured width of this view.
8035 */
8036 public final int getMeasuredWidth() {
8037 return mMeasuredWidth & MEASURED_SIZE_MASK;
8038 }
8039
8040 /**
8041 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008042 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008043 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 * This should be used during measurement and layout calculations only. Use
8045 * {@link #getWidth()} to see how wide a view is after layout.
8046 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008047 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008049 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 return mMeasuredWidth;
8051 }
8052
8053 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008054 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8055 * raw width component (that is the result is masked by
8056 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008058 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 */
8060 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008061 return mMeasuredHeight & MEASURED_SIZE_MASK;
8062 }
8063
8064 /**
8065 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008066 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008067 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8068 * This should be used during measurement and layout calculations only. Use
8069 * {@link #getHeight()} to see how wide a view is after layout.
8070 *
8071 * @return The measured width of this view as a bit mask.
8072 */
8073 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074 return mMeasuredHeight;
8075 }
8076
8077 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008078 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8079 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8080 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8081 * and the height component is at the shifted bits
8082 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8083 */
8084 public final int getMeasuredState() {
8085 return (mMeasuredWidth&MEASURED_STATE_MASK)
8086 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8087 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8088 }
8089
8090 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008091 * The transform matrix of this view, which is calculated based on the current
8092 * roation, scale, and pivot properties.
8093 *
8094 * @see #getRotation()
8095 * @see #getScaleX()
8096 * @see #getScaleY()
8097 * @see #getPivotX()
8098 * @see #getPivotY()
8099 * @return The current transform matrix for the view
8100 */
8101 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008102 if (mTransformationInfo != null) {
8103 updateMatrix();
8104 return mTransformationInfo.mMatrix;
8105 }
8106 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008107 }
8108
8109 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008110 * Utility function to determine if the value is far enough away from zero to be
8111 * considered non-zero.
8112 * @param value A floating point value to check for zero-ness
8113 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8114 */
8115 private static boolean nonzero(float value) {
8116 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8117 }
8118
8119 /**
Jeff Brown86671742010-09-30 20:00:15 -07008120 * Returns true if the transform matrix is the identity matrix.
8121 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008122 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008123 * @return True if the transform matrix is the identity matrix, false otherwise.
8124 */
Jeff Brown86671742010-09-30 20:00:15 -07008125 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008126 if (mTransformationInfo != null) {
8127 updateMatrix();
8128 return mTransformationInfo.mMatrixIsIdentity;
8129 }
8130 return true;
8131 }
8132
8133 void ensureTransformationInfo() {
8134 if (mTransformationInfo == null) {
8135 mTransformationInfo = new TransformationInfo();
8136 }
Jeff Brown86671742010-09-30 20:00:15 -07008137 }
8138
8139 /**
8140 * Recomputes the transform matrix if necessary.
8141 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008142 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008143 final TransformationInfo info = mTransformationInfo;
8144 if (info == null) {
8145 return;
8146 }
8147 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 // transform-related properties have changed since the last time someone
8149 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008150
8151 // Figure out if we need to update the pivot point
8152 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008153 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8154 info.mPrevWidth = mRight - mLeft;
8155 info.mPrevHeight = mBottom - mTop;
8156 info.mPivotX = info.mPrevWidth / 2f;
8157 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008158 }
8159 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008160 info.mMatrix.reset();
8161 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8162 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8163 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8164 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008165 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008166 if (info.mCamera == null) {
8167 info.mCamera = new Camera();
8168 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008169 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008170 info.mCamera.save();
8171 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8172 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8173 info.mCamera.getMatrix(info.matrix3D);
8174 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8175 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8176 info.mPivotY + info.mTranslationY);
8177 info.mMatrix.postConcat(info.matrix3D);
8178 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008179 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008180 info.mMatrixDirty = false;
8181 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8182 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008183 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008184 }
8185
8186 /**
8187 * Utility method to retrieve the inverse of the current mMatrix property.
8188 * We cache the matrix to avoid recalculating it when transform properties
8189 * have not changed.
8190 *
8191 * @return The inverse of the current matrix of this view.
8192 */
Jeff Brown86671742010-09-30 20:00:15 -07008193 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008194 final TransformationInfo info = mTransformationInfo;
8195 if (info != null) {
8196 updateMatrix();
8197 if (info.mInverseMatrixDirty) {
8198 if (info.mInverseMatrix == null) {
8199 info.mInverseMatrix = new Matrix();
8200 }
8201 info.mMatrix.invert(info.mInverseMatrix);
8202 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008203 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008204 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008205 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008206 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008207 }
8208
8209 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008210 * Gets the distance along the Z axis from the camera to this view.
8211 *
8212 * @see #setCameraDistance(float)
8213 *
8214 * @return The distance along the Z axis.
8215 */
8216 public float getCameraDistance() {
8217 ensureTransformationInfo();
8218 final float dpi = mResources.getDisplayMetrics().densityDpi;
8219 final TransformationInfo info = mTransformationInfo;
8220 if (info.mCamera == null) {
8221 info.mCamera = new Camera();
8222 info.matrix3D = new Matrix();
8223 }
8224 return -(info.mCamera.getLocationZ() * dpi);
8225 }
8226
8227 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008228 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8229 * views are drawn) from the camera to this view. The camera's distance
8230 * affects 3D transformations, for instance rotations around the X and Y
8231 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008232 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008233 * use a camera distance that's greater than the height (X axis rotation) or
8234 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008235 *
Romain Guya5364ee2011-02-24 14:46:04 -08008236 * <p>The distance of the camera from the view plane can have an affect on the
8237 * perspective distortion of the view when it is rotated around the x or y axis.
8238 * For example, a large distance will result in a large viewing angle, and there
8239 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008240 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008241 * also result in some drawing artifacts if the rotated view ends up partially
8242 * behind the camera (which is why the recommendation is to use a distance at
8243 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008244 *
Romain Guya5364ee2011-02-24 14:46:04 -08008245 * <p>The distance is expressed in "depth pixels." The default distance depends
8246 * on the screen density. For instance, on a medium density display, the
8247 * default distance is 1280. On a high density display, the default distance
8248 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008249 *
Romain Guya5364ee2011-02-24 14:46:04 -08008250 * <p>If you want to specify a distance that leads to visually consistent
8251 * results across various densities, use the following formula:</p>
8252 * <pre>
8253 * float scale = context.getResources().getDisplayMetrics().density;
8254 * view.setCameraDistance(distance * scale);
8255 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008256 *
Romain Guya5364ee2011-02-24 14:46:04 -08008257 * <p>The density scale factor of a high density display is 1.5,
8258 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008259 *
Romain Guya5364ee2011-02-24 14:46:04 -08008260 * @param distance The distance in "depth pixels", if negative the opposite
8261 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008262 *
8263 * @see #setRotationX(float)
8264 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008265 */
8266 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008267 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008268
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008269 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008270 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008271 final TransformationInfo info = mTransformationInfo;
8272 if (info.mCamera == null) {
8273 info.mCamera = new Camera();
8274 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008275 }
8276
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008277 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8278 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008279
Chet Haase9d1992d2012-03-13 11:03:25 -07008280 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008281 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008282 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008283 }
Romain Guya5364ee2011-02-24 14:46:04 -08008284 }
8285
8286 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008287 * The degrees that the view is rotated around the pivot point.
8288 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008289 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008290 * @see #getPivotX()
8291 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008292 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008293 * @return The degrees of rotation.
8294 */
Chet Haasea5531132012-02-02 13:41:44 -08008295 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008296 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008297 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008298 }
8299
8300 /**
Chet Haase897247b2010-09-09 14:54:47 -07008301 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8302 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008303 *
8304 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008305 *
8306 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008307 * @see #getPivotX()
8308 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008309 * @see #setRotationX(float)
8310 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008311 *
8312 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008313 */
8314 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008315 ensureTransformationInfo();
8316 final TransformationInfo info = mTransformationInfo;
8317 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008318 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008319 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008320 info.mRotation = rotation;
8321 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008322 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008323 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008324 mDisplayList.setRotation(rotation);
8325 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008326 }
8327 }
8328
8329 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008330 * The degrees that the view is rotated around the vertical axis through the pivot point.
8331 *
8332 * @see #getPivotX()
8333 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008334 * @see #setRotationY(float)
8335 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008336 * @return The degrees of Y rotation.
8337 */
Chet Haasea5531132012-02-02 13:41:44 -08008338 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008339 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008340 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008341 }
8342
8343 /**
Chet Haase897247b2010-09-09 14:54:47 -07008344 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8345 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8346 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008347 *
Romain Guya5364ee2011-02-24 14:46:04 -08008348 * When rotating large views, it is recommended to adjust the camera distance
8349 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008350 *
8351 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008352 *
8353 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008354 * @see #getPivotX()
8355 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008356 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008357 * @see #setRotationX(float)
8358 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008359 *
8360 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008361 */
8362 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008363 ensureTransformationInfo();
8364 final TransformationInfo info = mTransformationInfo;
8365 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008366 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008367 info.mRotationY = rotationY;
8368 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008369 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008370 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008371 mDisplayList.setRotationY(rotationY);
8372 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008373 }
8374 }
8375
8376 /**
8377 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8378 *
8379 * @see #getPivotX()
8380 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008381 * @see #setRotationX(float)
8382 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008383 * @return The degrees of X rotation.
8384 */
Chet Haasea5531132012-02-02 13:41:44 -08008385 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008386 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008387 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008388 }
8389
8390 /**
Chet Haase897247b2010-09-09 14:54:47 -07008391 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8392 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8393 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008394 *
Romain Guya5364ee2011-02-24 14:46:04 -08008395 * When rotating large views, it is recommended to adjust the camera distance
8396 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008397 *
8398 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008399 *
8400 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008401 * @see #getPivotX()
8402 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008403 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008404 * @see #setRotationY(float)
8405 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008406 *
8407 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008408 */
8409 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008410 ensureTransformationInfo();
8411 final TransformationInfo info = mTransformationInfo;
8412 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008413 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008414 info.mRotationX = rotationX;
8415 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008416 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008417 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008418 mDisplayList.setRotationX(rotationX);
8419 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008420 }
8421 }
8422
8423 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008424 * The amount that the view is scaled in x around the pivot point, as a proportion of
8425 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8426 *
Joe Onorato93162322010-09-16 15:42:01 -04008427 * <p>By default, this is 1.0f.
8428 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008429 * @see #getPivotX()
8430 * @see #getPivotY()
8431 * @return The scaling factor.
8432 */
Chet Haasea5531132012-02-02 13:41:44 -08008433 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008434 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008435 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008436 }
8437
8438 /**
8439 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8440 * the view's unscaled width. A value of 1 means that no scaling is applied.
8441 *
8442 * @param scaleX The scaling factor.
8443 * @see #getPivotX()
8444 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008445 *
8446 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008447 */
8448 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008449 ensureTransformationInfo();
8450 final TransformationInfo info = mTransformationInfo;
8451 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008452 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008453 info.mScaleX = scaleX;
8454 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008455 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008456 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008457 mDisplayList.setScaleX(scaleX);
8458 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008459 }
8460 }
8461
8462 /**
8463 * The amount that the view is scaled in y around the pivot point, as a proportion of
8464 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8465 *
Joe Onorato93162322010-09-16 15:42:01 -04008466 * <p>By default, this is 1.0f.
8467 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008468 * @see #getPivotX()
8469 * @see #getPivotY()
8470 * @return The scaling factor.
8471 */
Chet Haasea5531132012-02-02 13:41:44 -08008472 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008473 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008474 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008475 }
8476
8477 /**
8478 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8479 * the view's unscaled width. A value of 1 means that no scaling is applied.
8480 *
8481 * @param scaleY The scaling factor.
8482 * @see #getPivotX()
8483 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008484 *
8485 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008486 */
8487 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008488 ensureTransformationInfo();
8489 final TransformationInfo info = mTransformationInfo;
8490 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008491 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008492 info.mScaleY = scaleY;
8493 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008494 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008495 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008496 mDisplayList.setScaleY(scaleY);
8497 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008498 }
8499 }
8500
8501 /**
8502 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8503 * and {@link #setScaleX(float) scaled}.
8504 *
8505 * @see #getRotation()
8506 * @see #getScaleX()
8507 * @see #getScaleY()
8508 * @see #getPivotY()
8509 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008510 *
8511 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008512 */
Chet Haasea5531132012-02-02 13:41:44 -08008513 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008514 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008515 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008516 }
8517
8518 /**
8519 * Sets the x location of the point around which the view is
8520 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008521 * By default, the pivot point is centered on the object.
8522 * Setting this property disables this behavior and causes the view to use only the
8523 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008524 *
8525 * @param pivotX The x location of the pivot point.
8526 * @see #getRotation()
8527 * @see #getScaleX()
8528 * @see #getScaleY()
8529 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008530 *
8531 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008532 */
8533 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008534 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008535 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008536 final TransformationInfo info = mTransformationInfo;
8537 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008538 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008539 info.mPivotX = pivotX;
8540 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008541 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008542 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008543 mDisplayList.setPivotX(pivotX);
8544 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008545 }
8546 }
8547
8548 /**
8549 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8550 * and {@link #setScaleY(float) scaled}.
8551 *
8552 * @see #getRotation()
8553 * @see #getScaleX()
8554 * @see #getScaleY()
8555 * @see #getPivotY()
8556 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008557 *
8558 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008559 */
Chet Haasea5531132012-02-02 13:41:44 -08008560 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008561 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008562 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008563 }
8564
8565 /**
8566 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008567 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8568 * Setting this property disables this behavior and causes the view to use only the
8569 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008570 *
8571 * @param pivotY The y location of the pivot point.
8572 * @see #getRotation()
8573 * @see #getScaleX()
8574 * @see #getScaleY()
8575 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008576 *
8577 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008578 */
8579 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008580 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008581 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008582 final TransformationInfo info = mTransformationInfo;
8583 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008584 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008585 info.mPivotY = pivotY;
8586 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008587 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008588 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008589 mDisplayList.setPivotY(pivotY);
8590 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008591 }
8592 }
8593
8594 /**
8595 * 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.
8597 *
Joe Onorato93162322010-09-16 15:42:01 -04008598 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 * @return The opacity of the view.
8600 */
Chet Haasea5531132012-02-02 13:41:44 -08008601 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008602 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008603 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008604 }
8605
8606 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008607 * Returns whether this View has content which overlaps. This function, intended to be
8608 * overridden by specific View types, is an optimization when alpha is set on a view. If
8609 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8610 * and then composited it into place, which can be expensive. If the view has no overlapping
8611 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8612 * An example of overlapping rendering is a TextView with a background image, such as a
8613 * Button. An example of non-overlapping rendering is a TextView with no background, or
8614 * an ImageView with only the foreground image. The default implementation returns true;
8615 * subclasses should override if they have cases which can be optimized.
8616 *
8617 * @return true if the content in this view might overlap, false otherwise.
8618 */
8619 public boolean hasOverlappingRendering() {
8620 return true;
8621 }
8622
8623 /**
Romain Guy171c5922011-01-06 10:04:23 -08008624 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8625 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008626 *
Romain Guy171c5922011-01-06 10:04:23 -08008627 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8628 * responsible for applying the opacity itself. Otherwise, calling this method is
8629 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008630 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 *
Chet Haasea5531132012-02-02 13:41:44 -08008632 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8633 * performance implications. It is generally best to use the alpha property sparingly and
8634 * transiently, as in the case of fading animations.</p>
8635 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008636 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008637 *
Joe Malin32736f02011-01-19 16:14:20 -08008638 * @see #setLayerType(int, android.graphics.Paint)
8639 *
Chet Haase73066682010-11-29 15:55:32 -08008640 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008641 */
8642 public void setAlpha(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;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008646 if (onSetAlpha((int) (alpha * 255))) {
8647 mPrivateFlags |= ALPHA_SET;
8648 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008649 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008650 invalidate(true);
8651 } else {
8652 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008653 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008654 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008655 mDisplayList.setAlpha(alpha);
8656 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008657 }
Chet Haaseed032702010-10-01 14:05:54 -07008658 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008659 }
8660
8661 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008662 * Faster version of setAlpha() which performs the same steps except there are
8663 * no calls to invalidate(). The caller of this function should perform proper invalidation
8664 * on the parent and this object. The return value indicates whether the subclass handles
8665 * alpha (the return value for onSetAlpha()).
8666 *
8667 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008668 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8669 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008670 */
8671 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008672 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008673 if (mTransformationInfo.mAlpha != alpha) {
8674 mTransformationInfo.mAlpha = alpha;
8675 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8676 if (subclassHandlesAlpha) {
8677 mPrivateFlags |= ALPHA_SET;
8678 return true;
8679 } else {
8680 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008681 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008682 mDisplayList.setAlpha(alpha);
8683 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008684 }
Chet Haasea00f3862011-02-22 06:34:40 -08008685 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008686 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008687 }
8688
8689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008690 * Top position of this view relative to its parent.
8691 *
8692 * @return The top of this view, in pixels.
8693 */
8694 @ViewDebug.CapturedViewProperty
8695 public final int getTop() {
8696 return mTop;
8697 }
8698
8699 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008700 * Sets the top position of this view relative to its parent. This method is meant to be called
8701 * by the layout system and should not generally be called otherwise, because the property
8702 * may be changed at any time by the layout.
8703 *
8704 * @param top The top of this view, in pixels.
8705 */
8706 public final void setTop(int top) {
8707 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008708 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008709 final boolean matrixIsIdentity = mTransformationInfo == null
8710 || mTransformationInfo.mMatrixIsIdentity;
8711 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008712 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008713 int minTop;
8714 int yLoc;
8715 if (top < mTop) {
8716 minTop = top;
8717 yLoc = top - mTop;
8718 } else {
8719 minTop = mTop;
8720 yLoc = 0;
8721 }
Chet Haasee9140a72011-02-16 16:23:29 -08008722 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008723 }
8724 } else {
8725 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008726 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008727 }
8728
Chet Haaseed032702010-10-01 14:05:54 -07008729 int width = mRight - mLeft;
8730 int oldHeight = mBottom - mTop;
8731
Chet Haase21cd1382010-09-01 17:42:29 -07008732 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07008733 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008734 mDisplayList.setTop(mTop);
8735 }
Chet Haase21cd1382010-09-01 17:42:29 -07008736
Chet Haaseed032702010-10-01 14:05:54 -07008737 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8738
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008739 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008740 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8741 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008742 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008743 }
Chet Haase21cd1382010-09-01 17:42:29 -07008744 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008745 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008746 }
Chet Haase55dbb652010-12-21 20:15:08 -08008747 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008748 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008749 }
8750 }
8751
8752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 * Bottom position of this view relative to its parent.
8754 *
8755 * @return The bottom of this view, in pixels.
8756 */
8757 @ViewDebug.CapturedViewProperty
8758 public final int getBottom() {
8759 return mBottom;
8760 }
8761
8762 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008763 * True if this view has changed since the last time being drawn.
8764 *
8765 * @return The dirty state of this view.
8766 */
8767 public boolean isDirty() {
8768 return (mPrivateFlags & DIRTY_MASK) != 0;
8769 }
8770
8771 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008772 * Sets the bottom position of this view relative to its parent. This method is meant to be
8773 * called by the layout system and should not generally be called otherwise, because the
8774 * property may be changed at any time by the layout.
8775 *
8776 * @param bottom The bottom of this view, in pixels.
8777 */
8778 public final void setBottom(int bottom) {
8779 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008780 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008781 final boolean matrixIsIdentity = mTransformationInfo == null
8782 || mTransformationInfo.mMatrixIsIdentity;
8783 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008784 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008785 int maxBottom;
8786 if (bottom < mBottom) {
8787 maxBottom = mBottom;
8788 } else {
8789 maxBottom = bottom;
8790 }
Chet Haasee9140a72011-02-16 16:23:29 -08008791 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008792 }
8793 } else {
8794 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008795 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008796 }
8797
Chet Haaseed032702010-10-01 14:05:54 -07008798 int width = mRight - mLeft;
8799 int oldHeight = mBottom - mTop;
8800
Chet Haase21cd1382010-09-01 17:42:29 -07008801 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07008802 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008803 mDisplayList.setBottom(mBottom);
8804 }
Chet Haase21cd1382010-09-01 17:42:29 -07008805
Chet Haaseed032702010-10-01 14:05:54 -07008806 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8807
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008808 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008809 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8810 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008811 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008812 }
Chet Haase21cd1382010-09-01 17:42:29 -07008813 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008814 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008815 }
Chet Haase55dbb652010-12-21 20:15:08 -08008816 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008817 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008818 }
8819 }
8820
8821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008822 * Left position of this view relative to its parent.
8823 *
8824 * @return The left edge of this view, in pixels.
8825 */
8826 @ViewDebug.CapturedViewProperty
8827 public final int getLeft() {
8828 return mLeft;
8829 }
8830
8831 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008832 * Sets the left position of this view relative to its parent. This method is meant to be called
8833 * by the layout system and should not generally be called otherwise, because the property
8834 * may be changed at any time by the layout.
8835 *
8836 * @param left The bottom of this view, in pixels.
8837 */
8838 public final void setLeft(int left) {
8839 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008840 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008841 final boolean matrixIsIdentity = mTransformationInfo == null
8842 || mTransformationInfo.mMatrixIsIdentity;
8843 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008844 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008845 int minLeft;
8846 int xLoc;
8847 if (left < mLeft) {
8848 minLeft = left;
8849 xLoc = left - mLeft;
8850 } else {
8851 minLeft = mLeft;
8852 xLoc = 0;
8853 }
Chet Haasee9140a72011-02-16 16:23:29 -08008854 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008855 }
8856 } else {
8857 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008858 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008859 }
8860
Chet Haaseed032702010-10-01 14:05:54 -07008861 int oldWidth = mRight - mLeft;
8862 int height = mBottom - mTop;
8863
Chet Haase21cd1382010-09-01 17:42:29 -07008864 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07008865 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008866 mDisplayList.setLeft(left);
8867 }
Chet Haase21cd1382010-09-01 17:42:29 -07008868
Chet Haaseed032702010-10-01 14:05:54 -07008869 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8870
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008871 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008872 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8873 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008874 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008875 }
Chet Haase21cd1382010-09-01 17:42:29 -07008876 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008877 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008878 }
Chet Haase55dbb652010-12-21 20:15:08 -08008879 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008880 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008881 }
8882 }
8883
8884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 * Right position of this view relative to its parent.
8886 *
8887 * @return The right edge of this view, in pixels.
8888 */
8889 @ViewDebug.CapturedViewProperty
8890 public final int getRight() {
8891 return mRight;
8892 }
8893
8894 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008895 * Sets the right position of this view relative to its parent. This method is meant to be called
8896 * by the layout system and should not generally be called otherwise, because the property
8897 * may be changed at any time by the layout.
8898 *
8899 * @param right The bottom of this view, in pixels.
8900 */
8901 public final void setRight(int right) {
8902 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008903 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008904 final boolean matrixIsIdentity = mTransformationInfo == null
8905 || mTransformationInfo.mMatrixIsIdentity;
8906 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008907 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008908 int maxRight;
8909 if (right < mRight) {
8910 maxRight = mRight;
8911 } else {
8912 maxRight = right;
8913 }
Chet Haasee9140a72011-02-16 16:23:29 -08008914 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008915 }
8916 } else {
8917 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008918 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008919 }
8920
Chet Haaseed032702010-10-01 14:05:54 -07008921 int oldWidth = mRight - mLeft;
8922 int height = mBottom - mTop;
8923
Chet Haase21cd1382010-09-01 17:42:29 -07008924 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07008925 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008926 mDisplayList.setRight(mRight);
8927 }
Chet Haase21cd1382010-09-01 17:42:29 -07008928
Chet Haaseed032702010-10-01 14:05:54 -07008929 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8930
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008931 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008932 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8933 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008934 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008935 }
Chet Haase21cd1382010-09-01 17:42:29 -07008936 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008937 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008938 }
Chet Haase55dbb652010-12-21 20:15:08 -08008939 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008940 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008941 }
8942 }
8943
8944 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008945 * The visual x position of this view, in pixels. This is equivalent to the
8946 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008947 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008948 *
Chet Haasedf030d22010-07-30 17:22:38 -07008949 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 */
Chet Haasea5531132012-02-02 13:41:44 -08008951 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008952 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008953 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008954 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008955
Chet Haasedf030d22010-07-30 17:22:38 -07008956 /**
8957 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8958 * {@link #setTranslationX(float) translationX} property to be the difference between
8959 * the x value passed in and the current {@link #getLeft() left} property.
8960 *
8961 * @param x The visual x position of this view, in pixels.
8962 */
8963 public void setX(float x) {
8964 setTranslationX(x - mLeft);
8965 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008966
Chet Haasedf030d22010-07-30 17:22:38 -07008967 /**
8968 * The visual y position of this view, in pixels. This is equivalent to the
8969 * {@link #setTranslationY(float) translationY} property plus the current
8970 * {@link #getTop() top} property.
8971 *
8972 * @return The visual y position of this view, in pixels.
8973 */
Chet Haasea5531132012-02-02 13:41:44 -08008974 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008975 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008976 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008977 }
8978
8979 /**
8980 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8981 * {@link #setTranslationY(float) translationY} property to be the difference between
8982 * the y value passed in and the current {@link #getTop() top} property.
8983 *
8984 * @param y The visual y position of this view, in pixels.
8985 */
8986 public void setY(float y) {
8987 setTranslationY(y - mTop);
8988 }
8989
8990
8991 /**
8992 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8993 * This position is post-layout, in addition to wherever the object's
8994 * layout placed it.
8995 *
8996 * @return The horizontal position of this view relative to its left position, in pixels.
8997 */
Chet Haasea5531132012-02-02 13:41:44 -08008998 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008999 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009000 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009001 }
9002
9003 /**
9004 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9005 * This effectively positions the object post-layout, in addition to wherever the object's
9006 * layout placed it.
9007 *
9008 * @param translationX The horizontal position of this view relative to its left position,
9009 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009010 *
9011 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009012 */
9013 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009014 ensureTransformationInfo();
9015 final TransformationInfo info = mTransformationInfo;
9016 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009017 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009018 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009019 info.mTranslationX = translationX;
9020 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009021 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009022 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009023 mDisplayList.setTranslationX(translationX);
9024 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009025 }
9026 }
9027
9028 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009029 * The horizontal location of this view relative to its {@link #getTop() top} position.
9030 * This position is post-layout, in addition to wherever the object's
9031 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009032 *
Chet Haasedf030d22010-07-30 17:22:38 -07009033 * @return The vertical position of this view relative to its top position,
9034 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009035 */
Chet Haasea5531132012-02-02 13:41:44 -08009036 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009037 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009038 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009039 }
9040
9041 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009042 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9043 * This effectively positions the object post-layout, in addition to wherever the object's
9044 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009045 *
Chet Haasedf030d22010-07-30 17:22:38 -07009046 * @param translationY The vertical position of this view relative to its top position,
9047 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009048 *
9049 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009050 */
Chet Haasedf030d22010-07-30 17:22:38 -07009051 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009052 ensureTransformationInfo();
9053 final TransformationInfo info = mTransformationInfo;
9054 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009055 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009056 info.mTranslationY = translationY;
9057 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009058 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009059 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009060 mDisplayList.setTranslationY(translationY);
9061 }
Chet Haasedf030d22010-07-30 17:22:38 -07009062 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009063 }
9064
9065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009066 * Hit rectangle in parent's coordinates
9067 *
9068 * @param outRect The hit rectangle of the view.
9069 */
9070 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009071 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009072 final TransformationInfo info = mTransformationInfo;
9073 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009074 outRect.set(mLeft, mTop, mRight, mBottom);
9075 } else {
9076 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009077 tmpRect.set(-info.mPivotX, -info.mPivotY,
9078 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9079 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009080 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9081 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009082 }
9083 }
9084
9085 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009086 * Determines whether the given point, in local coordinates is inside the view.
9087 */
9088 /*package*/ final boolean pointInView(float localX, float localY) {
9089 return localX >= 0 && localX < (mRight - mLeft)
9090 && localY >= 0 && localY < (mBottom - mTop);
9091 }
9092
9093 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009094 * Utility method to determine whether the given point, in local coordinates,
9095 * is inside the view, where the area of the view is expanded by the slop factor.
9096 * This method is called while processing touch-move events to determine if the event
9097 * is still within the view.
9098 */
9099 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009100 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009101 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 }
9103
9104 /**
9105 * When a view has focus and the user navigates away from it, the next view is searched for
9106 * starting from the rectangle filled in by this method.
9107 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009108 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9109 * of the view. However, if your view maintains some idea of internal selection,
9110 * such as a cursor, or a selected row or column, you should override this method and
9111 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009112 *
9113 * @param r The rectangle to fill in, in this view's coordinates.
9114 */
9115 public void getFocusedRect(Rect r) {
9116 getDrawingRect(r);
9117 }
9118
9119 /**
9120 * If some part of this view is not clipped by any of its parents, then
9121 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009122 * coordinates (without taking possible View rotations into account), offset
9123 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9124 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125 *
9126 * @param r If true is returned, r holds the global coordinates of the
9127 * visible portion of this view.
9128 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9129 * between this view and its root. globalOffet may be null.
9130 * @return true if r is non-empty (i.e. part of the view is visible at the
9131 * root level.
9132 */
9133 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9134 int width = mRight - mLeft;
9135 int height = mBottom - mTop;
9136 if (width > 0 && height > 0) {
9137 r.set(0, 0, width, height);
9138 if (globalOffset != null) {
9139 globalOffset.set(-mScrollX, -mScrollY);
9140 }
9141 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9142 }
9143 return false;
9144 }
9145
9146 public final boolean getGlobalVisibleRect(Rect r) {
9147 return getGlobalVisibleRect(r, null);
9148 }
9149
9150 public final boolean getLocalVisibleRect(Rect r) {
9151 Point offset = new Point();
9152 if (getGlobalVisibleRect(r, offset)) {
9153 r.offset(-offset.x, -offset.y); // make r local
9154 return true;
9155 }
9156 return false;
9157 }
9158
9159 /**
9160 * Offset this view's vertical location by the specified number of pixels.
9161 *
9162 * @param offset the number of pixels to offset the view by
9163 */
9164 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009166 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009167 final boolean matrixIsIdentity = mTransformationInfo == null
9168 || mTransformationInfo.mMatrixIsIdentity;
9169 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009170 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009171 invalidateViewProperty(false, false);
9172 } else {
9173 final ViewParent p = mParent;
9174 if (p != null && mAttachInfo != null) {
9175 final Rect r = mAttachInfo.mTmpInvalRect;
9176 int minTop;
9177 int maxBottom;
9178 int yLoc;
9179 if (offset < 0) {
9180 minTop = mTop + offset;
9181 maxBottom = mBottom;
9182 yLoc = offset;
9183 } else {
9184 minTop = mTop;
9185 maxBottom = mBottom + offset;
9186 yLoc = 0;
9187 }
9188 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9189 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009190 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009191 }
9192 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009193 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009194 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009195
Chet Haasec3aa3612010-06-17 08:50:37 -07009196 mTop += offset;
9197 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009198 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009199 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009200 invalidateViewProperty(false, false);
9201 } else {
9202 if (!matrixIsIdentity) {
9203 invalidateViewProperty(false, true);
9204 }
9205 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009206 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 }
9209
9210 /**
9211 * Offset this view's horizontal location by the specified amount of pixels.
9212 *
9213 * @param offset the numer of pixels to offset the view by
9214 */
9215 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009216 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009217 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009218 final boolean matrixIsIdentity = mTransformationInfo == null
9219 || mTransformationInfo.mMatrixIsIdentity;
9220 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009221 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009222 invalidateViewProperty(false, false);
9223 } else {
9224 final ViewParent p = mParent;
9225 if (p != null && mAttachInfo != null) {
9226 final Rect r = mAttachInfo.mTmpInvalRect;
9227 int minLeft;
9228 int maxRight;
9229 if (offset < 0) {
9230 minLeft = mLeft + offset;
9231 maxRight = mRight;
9232 } else {
9233 minLeft = mLeft;
9234 maxRight = mRight + offset;
9235 }
9236 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9237 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009238 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009239 }
9240 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009241 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009242 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009243
Chet Haasec3aa3612010-06-17 08:50:37 -07009244 mLeft += offset;
9245 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009246 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009247 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009248 invalidateViewProperty(false, false);
9249 } else {
9250 if (!matrixIsIdentity) {
9251 invalidateViewProperty(false, true);
9252 }
9253 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009254 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009256 }
9257
9258 /**
9259 * Get the LayoutParams associated with this view. All views should have
9260 * layout parameters. These supply parameters to the <i>parent</i> of this
9261 * view specifying how it should be arranged. There are many subclasses of
9262 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9263 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009264 *
9265 * This method may return null if this View is not attached to a parent
9266 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9267 * was not invoked successfully. When a View is attached to a parent
9268 * ViewGroup, this method must not return null.
9269 *
9270 * @return The LayoutParams associated with this view, or null if no
9271 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009273 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274 public ViewGroup.LayoutParams getLayoutParams() {
9275 return mLayoutParams;
9276 }
9277
9278 /**
9279 * Set the layout parameters associated with this view. These supply
9280 * parameters to the <i>parent</i> of this view specifying how it should be
9281 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9282 * correspond to the different subclasses of ViewGroup that are responsible
9283 * for arranging their children.
9284 *
Romain Guy01c174b2011-02-22 11:51:06 -08009285 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 */
9287 public void setLayoutParams(ViewGroup.LayoutParams params) {
9288 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009289 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009290 }
9291 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009292 if (mParent instanceof ViewGroup) {
9293 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 requestLayout();
9296 }
9297
9298 /**
9299 * Set the scrolled position of your view. This will cause a call to
9300 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9301 * invalidated.
9302 * @param x the x position to scroll to
9303 * @param y the y position to scroll to
9304 */
9305 public void scrollTo(int x, int y) {
9306 if (mScrollX != x || mScrollY != y) {
9307 int oldX = mScrollX;
9308 int oldY = mScrollY;
9309 mScrollX = x;
9310 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009311 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009313 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009314 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 }
9317 }
9318
9319 /**
9320 * Move the scrolled position of your view. This will cause a call to
9321 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9322 * invalidated.
9323 * @param x the amount of pixels to scroll by horizontally
9324 * @param y the amount of pixels to scroll by vertically
9325 */
9326 public void scrollBy(int x, int y) {
9327 scrollTo(mScrollX + x, mScrollY + y);
9328 }
9329
9330 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009331 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9332 * animation to fade the scrollbars out after a default delay. If a subclass
9333 * provides animated scrolling, the start delay should equal the duration
9334 * of the scrolling animation.</p>
9335 *
9336 * <p>The animation starts only if at least one of the scrollbars is
9337 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9338 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9339 * this method returns true, and false otherwise. If the animation is
9340 * started, this method calls {@link #invalidate()}; in that case the
9341 * caller should not call {@link #invalidate()}.</p>
9342 *
9343 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009344 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009345 *
9346 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9347 * and {@link #scrollTo(int, int)}.</p>
9348 *
9349 * @return true if the animation is played, false otherwise
9350 *
9351 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009352 * @see #scrollBy(int, int)
9353 * @see #scrollTo(int, int)
9354 * @see #isHorizontalScrollBarEnabled()
9355 * @see #isVerticalScrollBarEnabled()
9356 * @see #setHorizontalScrollBarEnabled(boolean)
9357 * @see #setVerticalScrollBarEnabled(boolean)
9358 */
9359 protected boolean awakenScrollBars() {
9360 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009361 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009362 }
9363
9364 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009365 * Trigger the scrollbars to draw.
9366 * This method differs from awakenScrollBars() only in its default duration.
9367 * initialAwakenScrollBars() will show the scroll bars for longer than
9368 * usual to give the user more of a chance to notice them.
9369 *
9370 * @return true if the animation is played, false otherwise.
9371 */
9372 private boolean initialAwakenScrollBars() {
9373 return mScrollCache != null &&
9374 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9375 }
9376
9377 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009378 * <p>
9379 * Trigger the scrollbars to draw. When invoked this method starts an
9380 * animation to fade the scrollbars out after a fixed delay. If a subclass
9381 * provides animated scrolling, the start delay should equal the duration of
9382 * the scrolling animation.
9383 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009384 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009385 * <p>
9386 * The animation starts only if at least one of the scrollbars is enabled,
9387 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9388 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9389 * this method returns true, and false otherwise. If the animation is
9390 * started, this method calls {@link #invalidate()}; in that case the caller
9391 * should not call {@link #invalidate()}.
9392 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009393 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009394 * <p>
9395 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009396 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009397 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009398 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009399 * @param startDelay the delay, in milliseconds, after which the animation
9400 * should start; when the delay is 0, the animation starts
9401 * immediately
9402 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009403 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009404 * @see #scrollBy(int, int)
9405 * @see #scrollTo(int, int)
9406 * @see #isHorizontalScrollBarEnabled()
9407 * @see #isVerticalScrollBarEnabled()
9408 * @see #setHorizontalScrollBarEnabled(boolean)
9409 * @see #setVerticalScrollBarEnabled(boolean)
9410 */
9411 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009412 return awakenScrollBars(startDelay, true);
9413 }
Joe Malin32736f02011-01-19 16:14:20 -08009414
Mike Cleron290947b2009-09-29 18:34:32 -07009415 /**
9416 * <p>
9417 * Trigger the scrollbars to draw. When invoked this method starts an
9418 * animation to fade the scrollbars out after a fixed delay. If a subclass
9419 * provides animated scrolling, the start delay should equal the duration of
9420 * the scrolling animation.
9421 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009422 *
Mike Cleron290947b2009-09-29 18:34:32 -07009423 * <p>
9424 * The animation starts only if at least one of the scrollbars is enabled,
9425 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9426 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9427 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009428 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009429 * is set to true; in that case the caller
9430 * should not call {@link #invalidate()}.
9431 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009432 *
Mike Cleron290947b2009-09-29 18:34:32 -07009433 * <p>
9434 * This method should be invoked everytime a subclass directly updates the
9435 * scroll parameters.
9436 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009437 *
Mike Cleron290947b2009-09-29 18:34:32 -07009438 * @param startDelay the delay, in milliseconds, after which the animation
9439 * should start; when the delay is 0, the animation starts
9440 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009441 *
Mike Cleron290947b2009-09-29 18:34:32 -07009442 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009443 *
Mike Cleron290947b2009-09-29 18:34:32 -07009444 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009445 *
Mike Cleron290947b2009-09-29 18:34:32 -07009446 * @see #scrollBy(int, int)
9447 * @see #scrollTo(int, int)
9448 * @see #isHorizontalScrollBarEnabled()
9449 * @see #isVerticalScrollBarEnabled()
9450 * @see #setHorizontalScrollBarEnabled(boolean)
9451 * @see #setVerticalScrollBarEnabled(boolean)
9452 */
9453 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009454 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009455
Mike Cleronf116bf82009-09-27 19:14:12 -07009456 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9457 return false;
9458 }
9459
9460 if (scrollCache.scrollBar == null) {
9461 scrollCache.scrollBar = new ScrollBarDrawable();
9462 }
9463
9464 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9465
Mike Cleron290947b2009-09-29 18:34:32 -07009466 if (invalidate) {
9467 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009468 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009469 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009470
9471 if (scrollCache.state == ScrollabilityCache.OFF) {
9472 // FIXME: this is copied from WindowManagerService.
9473 // We should get this value from the system when it
9474 // is possible to do so.
9475 final int KEY_REPEAT_FIRST_DELAY = 750;
9476 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9477 }
9478
9479 // Tell mScrollCache when we should start fading. This may
9480 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009481 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009482 scrollCache.fadeStartTime = fadeStartTime;
9483 scrollCache.state = ScrollabilityCache.ON;
9484
9485 // Schedule our fader to run, unscheduling any old ones first
9486 if (mAttachInfo != null) {
9487 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9488 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9489 }
9490
9491 return true;
9492 }
9493
9494 return false;
9495 }
9496
9497 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009498 * Do not invalidate views which are not visible and which are not running an animation. They
9499 * will not get drawn and they should not set dirty flags as if they will be drawn
9500 */
9501 private boolean skipInvalidate() {
9502 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9503 (!(mParent instanceof ViewGroup) ||
9504 !((ViewGroup) mParent).isViewTransitioning(this));
9505 }
9506 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009507 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009508 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9509 * in the future. This must be called from a UI thread. To call from a non-UI
9510 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 *
9512 * WARNING: This method is destructive to dirty.
9513 * @param dirty the rectangle representing the bounds of the dirty region
9514 */
9515 public void invalidate(Rect dirty) {
9516 if (ViewDebug.TRACE_HIERARCHY) {
9517 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9518 }
9519
Chet Haaseaceafe62011-08-26 15:44:33 -07009520 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009521 return;
9522 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009523 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009524 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9525 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009526 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009527 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009528 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 final ViewParent p = mParent;
9530 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009531 //noinspection PointlessBooleanExpression,ConstantConditions
9532 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9533 if (p != null && ai != null && ai.mHardwareAccelerated) {
9534 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009535 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009536 p.invalidateChild(this, null);
9537 return;
9538 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 if (p != null && ai != null) {
9541 final int scrollX = mScrollX;
9542 final int scrollY = mScrollY;
9543 final Rect r = ai.mTmpInvalRect;
9544 r.set(dirty.left - scrollX, dirty.top - scrollY,
9545 dirty.right - scrollX, dirty.bottom - scrollY);
9546 mParent.invalidateChild(this, r);
9547 }
9548 }
9549 }
9550
9551 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009552 * 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 -08009553 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009554 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9555 * will be called at some point in the future. This must be called from
9556 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 * @param l the left position of the dirty region
9558 * @param t the top position of the dirty region
9559 * @param r the right position of the dirty region
9560 * @param b the bottom position of the dirty region
9561 */
9562 public void invalidate(int l, int t, int r, int b) {
9563 if (ViewDebug.TRACE_HIERARCHY) {
9564 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9565 }
9566
Chet Haaseaceafe62011-08-26 15:44:33 -07009567 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009568 return;
9569 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009570 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009571 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9572 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009574 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009575 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 final ViewParent p = mParent;
9577 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009578 //noinspection PointlessBooleanExpression,ConstantConditions
9579 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9580 if (p != null && ai != null && ai.mHardwareAccelerated) {
9581 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009582 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009583 p.invalidateChild(this, null);
9584 return;
9585 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587 if (p != null && ai != null && l < r && t < b) {
9588 final int scrollX = mScrollX;
9589 final int scrollY = mScrollY;
9590 final Rect tmpr = ai.mTmpInvalRect;
9591 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9592 p.invalidateChild(this, tmpr);
9593 }
9594 }
9595 }
9596
9597 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009598 * Invalidate the whole view. If the view is visible,
9599 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9600 * the future. This must be called from a UI thread. To call from a non-UI thread,
9601 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 */
9603 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009604 invalidate(true);
9605 }
Joe Malin32736f02011-01-19 16:14:20 -08009606
Chet Haaseed032702010-10-01 14:05:54 -07009607 /**
9608 * This is where the invalidate() work actually happens. A full invalidate()
9609 * causes the drawing cache to be invalidated, but this function can be called with
9610 * invalidateCache set to false to skip that invalidation step for cases that do not
9611 * need it (for example, a component that remains at the same dimensions with the same
9612 * content).
9613 *
9614 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9615 * well. This is usually true for a full invalidate, but may be set to false if the
9616 * View's contents or dimensions have not changed.
9617 */
Romain Guy849d0a32011-02-01 17:20:48 -08009618 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 if (ViewDebug.TRACE_HIERARCHY) {
9620 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9621 }
9622
Chet Haaseaceafe62011-08-26 15:44:33 -07009623 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009624 return;
9625 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009626 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009627 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009628 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9629 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009630 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009631 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009632 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009633 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009634 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009636 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009637 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009638 //noinspection PointlessBooleanExpression,ConstantConditions
9639 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9640 if (p != null && ai != null && ai.mHardwareAccelerated) {
9641 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009642 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009643 p.invalidateChild(this, null);
9644 return;
9645 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009646 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 if (p != null && ai != null) {
9649 final Rect r = ai.mTmpInvalRect;
9650 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9651 // Don't call invalidate -- we don't want to internally scroll
9652 // our own bounds
9653 p.invalidateChild(this, r);
9654 }
9655 }
9656 }
9657
9658 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009659 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9660 * set any flags or handle all of the cases handled by the default invalidation methods.
9661 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9662 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9663 * walk up the hierarchy, transforming the dirty rect as necessary.
9664 *
9665 * The method also handles normal invalidation logic if display list properties are not
9666 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9667 * backup approach, to handle these cases used in the various property-setting methods.
9668 *
9669 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9670 * are not being used in this view
9671 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9672 * list properties are not being used in this view
9673 */
9674 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009675 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009676 if (invalidateParent) {
9677 invalidateParentCaches();
9678 }
9679 if (forceRedraw) {
9680 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9681 }
9682 invalidate(false);
9683 } else {
9684 final AttachInfo ai = mAttachInfo;
9685 final ViewParent p = mParent;
9686 if (p != null && ai != null) {
9687 final Rect r = ai.mTmpInvalRect;
9688 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9689 if (mParent instanceof ViewGroup) {
9690 ((ViewGroup) mParent).invalidateChildFast(this, r);
9691 } else {
9692 mParent.invalidateChild(this, r);
9693 }
9694 }
9695 }
9696 }
9697
9698 /**
9699 * Utility method to transform a given Rect by the current matrix of this view.
9700 */
9701 void transformRect(final Rect rect) {
9702 if (!getMatrix().isIdentity()) {
9703 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9704 boundingRect.set(rect);
9705 getMatrix().mapRect(boundingRect);
9706 rect.set((int) (boundingRect.left - 0.5f),
9707 (int) (boundingRect.top - 0.5f),
9708 (int) (boundingRect.right + 0.5f),
9709 (int) (boundingRect.bottom + 0.5f));
9710 }
9711 }
9712
9713 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009714 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009715 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9716 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009717 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9718 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009719 *
9720 * @hide
9721 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009722 protected void invalidateParentCaches() {
9723 if (mParent instanceof View) {
9724 ((View) mParent).mPrivateFlags |= INVALIDATED;
9725 }
9726 }
Joe Malin32736f02011-01-19 16:14:20 -08009727
Romain Guy0fd89bf2011-01-26 15:41:30 -08009728 /**
9729 * Used to indicate that the parent of this view should be invalidated. This functionality
9730 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9731 * which is necessary when various parent-managed properties of the view change, such as
9732 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9733 * an invalidation event to the parent.
9734 *
9735 * @hide
9736 */
9737 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009738 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009739 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009740 }
9741 }
9742
9743 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009744 * Indicates whether this View is opaque. An opaque View guarantees that it will
9745 * draw all the pixels overlapping its bounds using a fully opaque color.
9746 *
9747 * Subclasses of View should override this method whenever possible to indicate
9748 * whether an instance is opaque. Opaque Views are treated in a special way by
9749 * the View hierarchy, possibly allowing it to perform optimizations during
9750 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009751 *
Romain Guy24443ea2009-05-11 11:56:30 -07009752 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009753 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009754 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009755 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009756 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009757 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9758 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009759 }
9760
Adam Powell20232d02010-12-08 21:08:53 -08009761 /**
9762 * @hide
9763 */
9764 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009765 // Opaque if:
9766 // - Has a background
9767 // - Background is opaque
9768 // - Doesn't have scrollbars or scrollbars are inside overlay
9769
Philip Milne6c8ea062012-04-03 17:38:43 -07009770 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009771 mPrivateFlags |= OPAQUE_BACKGROUND;
9772 } else {
9773 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9774 }
9775
9776 final int flags = mViewFlags;
9777 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9778 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9779 mPrivateFlags |= OPAQUE_SCROLLBARS;
9780 } else {
9781 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9782 }
9783 }
9784
9785 /**
9786 * @hide
9787 */
9788 protected boolean hasOpaqueScrollbars() {
9789 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009790 }
9791
9792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 * @return A handler associated with the thread running the View. This
9794 * handler can be used to pump events in the UI events queue.
9795 */
9796 public Handler getHandler() {
9797 if (mAttachInfo != null) {
9798 return mAttachInfo.mHandler;
9799 }
9800 return null;
9801 }
9802
9803 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009804 * Gets the view root associated with the View.
9805 * @return The view root, or null if none.
9806 * @hide
9807 */
9808 public ViewRootImpl getViewRootImpl() {
9809 if (mAttachInfo != null) {
9810 return mAttachInfo.mViewRootImpl;
9811 }
9812 return null;
9813 }
9814
9815 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009816 * <p>Causes the Runnable to be added to the message queue.
9817 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009818 *
Romain Guye63a4f32011-08-11 11:33:31 -07009819 * <p>This method can be invoked from outside of the UI thread
9820 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009821 *
9822 * @param action The Runnable that will be executed.
9823 *
9824 * @return Returns 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.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009827 *
9828 * @see #postDelayed
9829 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 */
9831 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009832 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009833 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009834 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009836 // Assume that post will succeed later
9837 ViewRootImpl.getRunQueue().post(action);
9838 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 }
9840
9841 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009842 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009843 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009844 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009845 *
Romain Guye63a4f32011-08-11 11:33:31 -07009846 * <p>This method can be invoked from outside of the UI thread
9847 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009848 *
9849 * @param action The Runnable that will be executed.
9850 * @param delayMillis The delay (in milliseconds) until the Runnable
9851 * will be executed.
9852 *
9853 * @return true if the Runnable was successfully placed in to the
9854 * message queue. Returns false on failure, usually because the
9855 * looper processing the message queue is exiting. Note that a
9856 * result of true does not mean the Runnable will be processed --
9857 * if the looper is quit before the delivery time of the message
9858 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009859 *
9860 * @see #post
9861 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 */
9863 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009864 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009865 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009866 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009868 // Assume that post will succeed later
9869 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9870 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009871 }
9872
9873 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009874 * <p>Causes the Runnable to execute on the next animation time step.
9875 * The runnable will be run on the user interface thread.</p>
9876 *
9877 * <p>This method can be invoked from outside of the UI thread
9878 * only when this View is attached to a window.</p>
9879 *
9880 * @param action The Runnable that will be executed.
9881 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009882 * @see #postOnAnimationDelayed
9883 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009884 */
9885 public void postOnAnimation(Runnable action) {
9886 final AttachInfo attachInfo = mAttachInfo;
9887 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009888 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9889 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009890 } else {
9891 // Assume that post will succeed later
9892 ViewRootImpl.getRunQueue().post(action);
9893 }
9894 }
9895
9896 /**
9897 * <p>Causes the Runnable to execute on the next animation time step,
9898 * after the specified amount of time elapses.
9899 * The runnable will be run on the user interface thread.</p>
9900 *
9901 * <p>This method can be invoked from outside of the UI thread
9902 * only when this View is attached to a window.</p>
9903 *
9904 * @param action The Runnable that will be executed.
9905 * @param delayMillis The delay (in milliseconds) until the Runnable
9906 * will be executed.
9907 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009908 * @see #postOnAnimation
9909 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009910 */
9911 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9912 final AttachInfo attachInfo = mAttachInfo;
9913 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009914 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9915 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009916 } else {
9917 // Assume that post will succeed later
9918 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9919 }
9920 }
9921
9922 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009923 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009924 *
Romain Guye63a4f32011-08-11 11:33:31 -07009925 * <p>This method can be invoked from outside of the UI thread
9926 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 *
9928 * @param action The Runnable to remove from the message handling queue
9929 *
9930 * @return true if this view could ask the Handler to remove the Runnable,
9931 * false otherwise. When the returned value is true, the Runnable
9932 * may or may not have been actually removed from the message queue
9933 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009934 *
9935 * @see #post
9936 * @see #postDelayed
9937 * @see #postOnAnimation
9938 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009939 */
9940 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009941 if (action != null) {
9942 final AttachInfo attachInfo = mAttachInfo;
9943 if (attachInfo != null) {
9944 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009945 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9946 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009947 } else {
9948 // Assume that post will succeed later
9949 ViewRootImpl.getRunQueue().removeCallbacks(action);
9950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 return true;
9953 }
9954
9955 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009956 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9957 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 *
Romain Guye63a4f32011-08-11 11:33:31 -07009959 * <p>This method can be invoked from outside of the UI thread
9960 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009961 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009963 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 */
9965 public void postInvalidate() {
9966 postInvalidateDelayed(0);
9967 }
9968
9969 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009970 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9971 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009972 *
Romain Guye63a4f32011-08-11 11:33:31 -07009973 * <p>This method can be invoked from outside of the UI thread
9974 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 *
9976 * @param left The left coordinate of the rectangle to invalidate.
9977 * @param top The top coordinate of the rectangle to invalidate.
9978 * @param right The right coordinate of the rectangle to invalidate.
9979 * @param bottom The bottom coordinate of the rectangle to invalidate.
9980 *
9981 * @see #invalidate(int, int, int, int)
9982 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009983 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 */
9985 public void postInvalidate(int left, int top, int right, int bottom) {
9986 postInvalidateDelayed(0, left, top, right, bottom);
9987 }
9988
9989 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009990 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9991 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009992 *
Romain Guye63a4f32011-08-11 11:33:31 -07009993 * <p>This method can be invoked from outside of the UI thread
9994 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 *
9996 * @param delayMilliseconds the duration in milliseconds to delay the
9997 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009998 *
9999 * @see #invalidate()
10000 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010001 */
10002 public void postInvalidateDelayed(long delayMilliseconds) {
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) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010007 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 }
10009 }
10010
10011 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010012 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10013 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010014 *
Romain Guye63a4f32011-08-11 11:33:31 -070010015 * <p>This method can be invoked from outside of the UI thread
10016 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 *
10018 * @param delayMilliseconds the duration in milliseconds to delay the
10019 * invalidation by
10020 * @param left The left coordinate of the rectangle to invalidate.
10021 * @param top The top coordinate of the rectangle to invalidate.
10022 * @param right The right coordinate of the rectangle to invalidate.
10023 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010024 *
10025 * @see #invalidate(int, int, int, int)
10026 * @see #invalidate(Rect)
10027 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 */
10029 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10030 int right, int bottom) {
10031
10032 // We try only with the AttachInfo because there's no point in invalidating
10033 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010034 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010035 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010036 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10037 info.target = this;
10038 info.left = left;
10039 info.top = top;
10040 info.right = right;
10041 info.bottom = bottom;
10042
Jeff Browna175a5b2012-02-15 19:18:31 -080010043 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010044 }
10045 }
10046
10047 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010048 * <p>Cause an invalidate to happen on the next animation time step, typically the
10049 * next display frame.</p>
10050 *
10051 * <p>This method can be invoked from outside of the UI thread
10052 * only when this View is attached to a window.</p>
10053 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010054 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010055 */
10056 public void postInvalidateOnAnimation() {
10057 // We try only with the AttachInfo because there's no point in invalidating
10058 // if we are not attached to our window
10059 final AttachInfo attachInfo = mAttachInfo;
10060 if (attachInfo != null) {
10061 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10062 }
10063 }
10064
10065 /**
10066 * <p>Cause an invalidate of the specified area to happen on the next animation
10067 * time step, typically the next display frame.</p>
10068 *
10069 * <p>This method can be invoked from outside of the UI thread
10070 * only when this View is attached to a window.</p>
10071 *
10072 * @param left The left coordinate of the rectangle to invalidate.
10073 * @param top The top coordinate of the rectangle to invalidate.
10074 * @param right The right coordinate of the rectangle to invalidate.
10075 * @param bottom The bottom coordinate of the rectangle to invalidate.
10076 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010077 * @see #invalidate(int, int, int, int)
10078 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010079 */
10080 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10081 // We try only with the AttachInfo because there's no point in invalidating
10082 // if we are not attached to our window
10083 final AttachInfo attachInfo = mAttachInfo;
10084 if (attachInfo != null) {
10085 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10086 info.target = this;
10087 info.left = left;
10088 info.top = top;
10089 info.right = right;
10090 info.bottom = bottom;
10091
10092 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10093 }
10094 }
10095
10096 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010097 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10098 * This event is sent at most once every
10099 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10100 */
10101 private void postSendViewScrolledAccessibilityEventCallback() {
10102 if (mSendViewScrolledAccessibilityEvent == null) {
10103 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10104 }
10105 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10106 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10107 postDelayed(mSendViewScrolledAccessibilityEvent,
10108 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10109 }
10110 }
10111
10112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010113 * Called by a parent to request that a child update its values for mScrollX
10114 * and mScrollY if necessary. This will typically be done if the child is
10115 * animating a scroll using a {@link android.widget.Scroller Scroller}
10116 * object.
10117 */
10118 public void computeScroll() {
10119 }
10120
10121 /**
10122 * <p>Indicate whether the horizontal edges are faded when the view is
10123 * scrolled horizontally.</p>
10124 *
10125 * @return true if the horizontal edges should are faded on scroll, false
10126 * otherwise
10127 *
10128 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010129 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010130 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 */
10132 public boolean isHorizontalFadingEdgeEnabled() {
10133 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10134 }
10135
10136 /**
10137 * <p>Define whether the horizontal edges should be faded when this view
10138 * is scrolled horizontally.</p>
10139 *
10140 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10141 * be faded when the view is scrolled
10142 * horizontally
10143 *
10144 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010145 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010146 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010147 */
10148 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10149 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10150 if (horizontalFadingEdgeEnabled) {
10151 initScrollCache();
10152 }
10153
10154 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10155 }
10156 }
10157
10158 /**
10159 * <p>Indicate whether the vertical edges are faded when the view is
10160 * scrolled horizontally.</p>
10161 *
10162 * @return true if the vertical edges should are faded on scroll, false
10163 * otherwise
10164 *
10165 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010166 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010167 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 */
10169 public boolean isVerticalFadingEdgeEnabled() {
10170 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10171 }
10172
10173 /**
10174 * <p>Define whether the vertical edges should be faded when this view
10175 * is scrolled vertically.</p>
10176 *
10177 * @param verticalFadingEdgeEnabled true if the vertical edges should
10178 * be faded when the view is scrolled
10179 * vertically
10180 *
10181 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010182 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010183 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 */
10185 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10186 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10187 if (verticalFadingEdgeEnabled) {
10188 initScrollCache();
10189 }
10190
10191 mViewFlags ^= FADING_EDGE_VERTICAL;
10192 }
10193 }
10194
10195 /**
10196 * Returns the strength, or intensity, of the top 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 top fade as a float between 0.0f and 1.0f
10204 */
10205 protected float getTopFadingEdgeStrength() {
10206 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10207 }
10208
10209 /**
10210 * Returns the strength, or intensity, of the bottom 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 bottom fade as a float between 0.0f and 1.0f
10218 */
10219 protected float getBottomFadingEdgeStrength() {
10220 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10221 computeVerticalScrollRange() ? 1.0f : 0.0f;
10222 }
10223
10224 /**
10225 * Returns the strength, or intensity, of the left faded edge. The strength is
10226 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10227 * returns 0.0 or 1.0 but no value in between.
10228 *
10229 * Subclasses should override this method to provide a smoother fade transition
10230 * when scrolling occurs.
10231 *
10232 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10233 */
10234 protected float getLeftFadingEdgeStrength() {
10235 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10236 }
10237
10238 /**
10239 * Returns the strength, or intensity, of the right faded edge. The strength is
10240 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10241 * returns 0.0 or 1.0 but no value in between.
10242 *
10243 * Subclasses should override this method to provide a smoother fade transition
10244 * when scrolling occurs.
10245 *
10246 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10247 */
10248 protected float getRightFadingEdgeStrength() {
10249 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10250 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10251 }
10252
10253 /**
10254 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10255 * scrollbar is not drawn by default.</p>
10256 *
10257 * @return true if the horizontal scrollbar should be painted, false
10258 * otherwise
10259 *
10260 * @see #setHorizontalScrollBarEnabled(boolean)
10261 */
10262 public boolean isHorizontalScrollBarEnabled() {
10263 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10264 }
10265
10266 /**
10267 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10268 * scrollbar is not drawn by default.</p>
10269 *
10270 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10271 * be painted
10272 *
10273 * @see #isHorizontalScrollBarEnabled()
10274 */
10275 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10276 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10277 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010278 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010279 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 }
10281 }
10282
10283 /**
10284 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10285 * scrollbar is not drawn by default.</p>
10286 *
10287 * @return true if the vertical scrollbar should be painted, false
10288 * otherwise
10289 *
10290 * @see #setVerticalScrollBarEnabled(boolean)
10291 */
10292 public boolean isVerticalScrollBarEnabled() {
10293 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10294 }
10295
10296 /**
10297 * <p>Define whether the vertical scrollbar should be drawn or not. The
10298 * scrollbar is not drawn by default.</p>
10299 *
10300 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10301 * be painted
10302 *
10303 * @see #isVerticalScrollBarEnabled()
10304 */
10305 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10306 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10307 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010308 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010309 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 }
10311 }
10312
Adam Powell20232d02010-12-08 21:08:53 -080010313 /**
10314 * @hide
10315 */
10316 protected void recomputePadding() {
10317 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010318 }
Joe Malin32736f02011-01-19 16:14:20 -080010319
Mike Cleronfe81d382009-09-28 14:22:16 -070010320 /**
10321 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010322 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010323 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010324 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010325 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010326 */
10327 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10328 initScrollCache();
10329 final ScrollabilityCache scrollabilityCache = mScrollCache;
10330 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010331 if (fadeScrollbars) {
10332 scrollabilityCache.state = ScrollabilityCache.OFF;
10333 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010334 scrollabilityCache.state = ScrollabilityCache.ON;
10335 }
10336 }
Joe Malin32736f02011-01-19 16:14:20 -080010337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 /**
Joe Malin32736f02011-01-19 16:14:20 -080010339 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010340 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010341 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010342 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010343 *
10344 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010345 */
10346 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010347 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010348 }
Joe Malin32736f02011-01-19 16:14:20 -080010349
Mike Cleron52f0a642009-09-28 18:21:37 -070010350 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010351 *
10352 * Returns the delay before scrollbars fade.
10353 *
10354 * @return the delay before scrollbars fade
10355 *
10356 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10357 */
10358 public int getScrollBarDefaultDelayBeforeFade() {
10359 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10360 mScrollCache.scrollBarDefaultDelayBeforeFade;
10361 }
10362
10363 /**
10364 * Define the delay before scrollbars fade.
10365 *
10366 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10367 *
10368 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10369 */
10370 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10371 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10372 }
10373
10374 /**
10375 *
10376 * Returns the scrollbar fade duration.
10377 *
10378 * @return the scrollbar fade duration
10379 *
10380 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10381 */
10382 public int getScrollBarFadeDuration() {
10383 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10384 mScrollCache.scrollBarFadeDuration;
10385 }
10386
10387 /**
10388 * Define the scrollbar fade duration.
10389 *
10390 * @param scrollBarFadeDuration - the scrollbar fade duration
10391 *
10392 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10393 */
10394 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10395 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10396 }
10397
10398 /**
10399 *
10400 * Returns the scrollbar size.
10401 *
10402 * @return the scrollbar size
10403 *
10404 * @attr ref android.R.styleable#View_scrollbarSize
10405 */
10406 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010407 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010408 mScrollCache.scrollBarSize;
10409 }
10410
10411 /**
10412 * Define the scrollbar size.
10413 *
10414 * @param scrollBarSize - the scrollbar size
10415 *
10416 * @attr ref android.R.styleable#View_scrollbarSize
10417 */
10418 public void setScrollBarSize(int scrollBarSize) {
10419 getScrollCache().scrollBarSize = scrollBarSize;
10420 }
10421
10422 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10424 * inset. When inset, they add to the padding of the view. And the scrollbars
10425 * can be drawn inside the padding area or on the edge of the view. For example,
10426 * if a view has a background drawable and you want to draw the scrollbars
10427 * inside the padding specified by the drawable, you can use
10428 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10429 * appear at the edge of the view, ignoring the padding, then you can use
10430 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10431 * @param style the style of the scrollbars. Should be one of
10432 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10433 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10434 * @see #SCROLLBARS_INSIDE_OVERLAY
10435 * @see #SCROLLBARS_INSIDE_INSET
10436 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10437 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010438 *
10439 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 */
10441 public void setScrollBarStyle(int style) {
10442 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10443 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010444 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010445 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 }
10447 }
10448
10449 /**
10450 * <p>Returns the current scrollbar style.</p>
10451 * @return the current scrollbar style
10452 * @see #SCROLLBARS_INSIDE_OVERLAY
10453 * @see #SCROLLBARS_INSIDE_INSET
10454 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10455 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010456 *
10457 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010459 @ViewDebug.ExportedProperty(mapping = {
10460 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10461 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10462 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10463 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10464 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 public int getScrollBarStyle() {
10466 return mViewFlags & SCROLLBARS_STYLE_MASK;
10467 }
10468
10469 /**
10470 * <p>Compute the horizontal range that the horizontal scrollbar
10471 * represents.</p>
10472 *
10473 * <p>The range is expressed in arbitrary units that must be the same as the
10474 * units used by {@link #computeHorizontalScrollExtent()} and
10475 * {@link #computeHorizontalScrollOffset()}.</p>
10476 *
10477 * <p>The default range is the drawing width of this view.</p>
10478 *
10479 * @return the total horizontal range represented by the horizontal
10480 * scrollbar
10481 *
10482 * @see #computeHorizontalScrollExtent()
10483 * @see #computeHorizontalScrollOffset()
10484 * @see android.widget.ScrollBarDrawable
10485 */
10486 protected int computeHorizontalScrollRange() {
10487 return getWidth();
10488 }
10489
10490 /**
10491 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10492 * within the horizontal range. This value is used to compute the position
10493 * of the thumb within the scrollbar's track.</p>
10494 *
10495 * <p>The range is expressed in arbitrary units that must be the same as the
10496 * units used by {@link #computeHorizontalScrollRange()} and
10497 * {@link #computeHorizontalScrollExtent()}.</p>
10498 *
10499 * <p>The default offset is the scroll offset of this view.</p>
10500 *
10501 * @return the horizontal offset of the scrollbar's thumb
10502 *
10503 * @see #computeHorizontalScrollRange()
10504 * @see #computeHorizontalScrollExtent()
10505 * @see android.widget.ScrollBarDrawable
10506 */
10507 protected int computeHorizontalScrollOffset() {
10508 return mScrollX;
10509 }
10510
10511 /**
10512 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10513 * within the horizontal range. This value is used to compute the length
10514 * of the thumb within the scrollbar's track.</p>
10515 *
10516 * <p>The range is expressed in arbitrary units that must be the same as the
10517 * units used by {@link #computeHorizontalScrollRange()} and
10518 * {@link #computeHorizontalScrollOffset()}.</p>
10519 *
10520 * <p>The default extent is the drawing width of this view.</p>
10521 *
10522 * @return the horizontal extent of the scrollbar's thumb
10523 *
10524 * @see #computeHorizontalScrollRange()
10525 * @see #computeHorizontalScrollOffset()
10526 * @see android.widget.ScrollBarDrawable
10527 */
10528 protected int computeHorizontalScrollExtent() {
10529 return getWidth();
10530 }
10531
10532 /**
10533 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10534 *
10535 * <p>The range is expressed in arbitrary units that must be the same as the
10536 * units used by {@link #computeVerticalScrollExtent()} and
10537 * {@link #computeVerticalScrollOffset()}.</p>
10538 *
10539 * @return the total vertical range represented by the vertical scrollbar
10540 *
10541 * <p>The default range is the drawing height of this view.</p>
10542 *
10543 * @see #computeVerticalScrollExtent()
10544 * @see #computeVerticalScrollOffset()
10545 * @see android.widget.ScrollBarDrawable
10546 */
10547 protected int computeVerticalScrollRange() {
10548 return getHeight();
10549 }
10550
10551 /**
10552 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10553 * within the horizontal range. This value is used to compute the position
10554 * of the thumb within the scrollbar's track.</p>
10555 *
10556 * <p>The range is expressed in arbitrary units that must be the same as the
10557 * units used by {@link #computeVerticalScrollRange()} and
10558 * {@link #computeVerticalScrollExtent()}.</p>
10559 *
10560 * <p>The default offset is the scroll offset of this view.</p>
10561 *
10562 * @return the vertical offset of the scrollbar's thumb
10563 *
10564 * @see #computeVerticalScrollRange()
10565 * @see #computeVerticalScrollExtent()
10566 * @see android.widget.ScrollBarDrawable
10567 */
10568 protected int computeVerticalScrollOffset() {
10569 return mScrollY;
10570 }
10571
10572 /**
10573 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10574 * within the vertical range. This value is used to compute the length
10575 * of the thumb within the scrollbar's track.</p>
10576 *
10577 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010578 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 * {@link #computeVerticalScrollOffset()}.</p>
10580 *
10581 * <p>The default extent is the drawing height of this view.</p>
10582 *
10583 * @return the vertical extent of the scrollbar's thumb
10584 *
10585 * @see #computeVerticalScrollRange()
10586 * @see #computeVerticalScrollOffset()
10587 * @see android.widget.ScrollBarDrawable
10588 */
10589 protected int computeVerticalScrollExtent() {
10590 return getHeight();
10591 }
10592
10593 /**
Adam Powell69159442011-06-13 17:53:06 -070010594 * Check if this view can be scrolled horizontally in a certain direction.
10595 *
10596 * @param direction Negative to check scrolling left, positive to check scrolling right.
10597 * @return true if this view can be scrolled in the specified direction, false otherwise.
10598 */
10599 public boolean canScrollHorizontally(int direction) {
10600 final int offset = computeHorizontalScrollOffset();
10601 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10602 if (range == 0) return false;
10603 if (direction < 0) {
10604 return offset > 0;
10605 } else {
10606 return offset < range - 1;
10607 }
10608 }
10609
10610 /**
10611 * Check if this view can be scrolled vertically in a certain direction.
10612 *
10613 * @param direction Negative to check scrolling up, positive to check scrolling down.
10614 * @return true if this view can be scrolled in the specified direction, false otherwise.
10615 */
10616 public boolean canScrollVertically(int direction) {
10617 final int offset = computeVerticalScrollOffset();
10618 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10619 if (range == 0) return false;
10620 if (direction < 0) {
10621 return offset > 0;
10622 } else {
10623 return offset < range - 1;
10624 }
10625 }
10626
10627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010628 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10629 * scrollbars are painted only if they have been awakened first.</p>
10630 *
10631 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010632 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010633 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010635 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 // scrollbars are drawn only when the animation is running
10637 final ScrollabilityCache cache = mScrollCache;
10638 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010639
Mike Cleronf116bf82009-09-27 19:14:12 -070010640 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010641
Mike Cleronf116bf82009-09-27 19:14:12 -070010642 if (state == ScrollabilityCache.OFF) {
10643 return;
10644 }
Joe Malin32736f02011-01-19 16:14:20 -080010645
Mike Cleronf116bf82009-09-27 19:14:12 -070010646 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010647
Mike Cleronf116bf82009-09-27 19:14:12 -070010648 if (state == ScrollabilityCache.FADING) {
10649 // We're fading -- get our fade interpolation
10650 if (cache.interpolatorValues == null) {
10651 cache.interpolatorValues = new float[1];
10652 }
Joe Malin32736f02011-01-19 16:14:20 -080010653
Mike Cleronf116bf82009-09-27 19:14:12 -070010654 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010655
Mike Cleronf116bf82009-09-27 19:14:12 -070010656 // Stops the animation if we're done
10657 if (cache.scrollBarInterpolator.timeToValues(values) ==
10658 Interpolator.Result.FREEZE_END) {
10659 cache.state = ScrollabilityCache.OFF;
10660 } else {
10661 cache.scrollBar.setAlpha(Math.round(values[0]));
10662 }
Joe Malin32736f02011-01-19 16:14:20 -080010663
10664 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010665 // drawing. We only want this when we're fading so that
10666 // we prevent excessive redraws
10667 invalidate = true;
10668 } else {
10669 // We're just on -- but we may have been fading before so
10670 // reset alpha
10671 cache.scrollBar.setAlpha(255);
10672 }
10673
Joe Malin32736f02011-01-19 16:14:20 -080010674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 final int viewFlags = mViewFlags;
10676
10677 final boolean drawHorizontalScrollBar =
10678 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10679 final boolean drawVerticalScrollBar =
10680 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10681 && !isVerticalScrollBarHidden();
10682
10683 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10684 final int width = mRight - mLeft;
10685 final int height = mBottom - mTop;
10686
10687 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688
Mike Reede8853fc2009-09-04 14:01:48 -040010689 final int scrollX = mScrollX;
10690 final int scrollY = mScrollY;
10691 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10692
Mike Cleronf116bf82009-09-27 19:14:12 -070010693 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010696 int size = scrollBar.getSize(false);
10697 if (size <= 0) {
10698 size = cache.scrollBarSize;
10699 }
10700
Mike Cleronf116bf82009-09-27 19:14:12 -070010701 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010702 computeHorizontalScrollOffset(),
10703 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010704 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010705 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010706 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010707 left = scrollX + (mPaddingLeft & inside);
10708 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10709 bottom = top + size;
10710 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10711 if (invalidate) {
10712 invalidate(left, top, right, bottom);
10713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010714 }
10715
10716 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010717 int size = scrollBar.getSize(true);
10718 if (size <= 0) {
10719 size = cache.scrollBarSize;
10720 }
10721
Mike Reede8853fc2009-09-04 14:01:48 -040010722 scrollBar.setParameters(computeVerticalScrollRange(),
10723 computeVerticalScrollOffset(),
10724 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010725 switch (mVerticalScrollbarPosition) {
10726 default:
10727 case SCROLLBAR_POSITION_DEFAULT:
10728 case SCROLLBAR_POSITION_RIGHT:
10729 left = scrollX + width - size - (mUserPaddingRight & inside);
10730 break;
10731 case SCROLLBAR_POSITION_LEFT:
10732 left = scrollX + (mUserPaddingLeft & inside);
10733 break;
10734 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010735 top = scrollY + (mPaddingTop & inside);
10736 right = left + size;
10737 bottom = scrollY + height - (mUserPaddingBottom & inside);
10738 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10739 if (invalidate) {
10740 invalidate(left, top, right, bottom);
10741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 }
10743 }
10744 }
10745 }
Romain Guy8506ab42009-06-11 17:35:47 -070010746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010748 * 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 -080010749 * FastScroller is visible.
10750 * @return whether to temporarily hide the vertical scrollbar
10751 * @hide
10752 */
10753 protected boolean isVerticalScrollBarHidden() {
10754 return false;
10755 }
10756
10757 /**
10758 * <p>Draw the horizontal scrollbar if
10759 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10760 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 * @param canvas the canvas on which to draw the scrollbar
10762 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 *
10764 * @see #isHorizontalScrollBarEnabled()
10765 * @see #computeHorizontalScrollRange()
10766 * @see #computeHorizontalScrollExtent()
10767 * @see #computeHorizontalScrollOffset()
10768 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010769 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010770 */
Romain Guy8fb95422010-08-17 18:38:51 -070010771 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10772 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010773 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010774 scrollBar.draw(canvas);
10775 }
Mike Reede8853fc2009-09-04 14:01:48 -040010776
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10779 * returns true.</p>
10780 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010781 * @param canvas the canvas on which to draw the scrollbar
10782 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 *
10784 * @see #isVerticalScrollBarEnabled()
10785 * @see #computeVerticalScrollRange()
10786 * @see #computeVerticalScrollExtent()
10787 * @see #computeVerticalScrollOffset()
10788 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010789 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010790 */
Romain Guy8fb95422010-08-17 18:38:51 -070010791 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10792 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010793 scrollBar.setBounds(l, t, r, b);
10794 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 }
10796
10797 /**
10798 * Implement this to do your drawing.
10799 *
10800 * @param canvas the canvas on which the background will be drawn
10801 */
10802 protected void onDraw(Canvas canvas) {
10803 }
10804
10805 /*
10806 * Caller is responsible for calling requestLayout if necessary.
10807 * (This allows addViewInLayout to not request a new layout.)
10808 */
10809 void assignParent(ViewParent parent) {
10810 if (mParent == null) {
10811 mParent = parent;
10812 } else if (parent == null) {
10813 mParent = null;
10814 } else {
10815 throw new RuntimeException("view " + this + " being added, but"
10816 + " it already has a parent");
10817 }
10818 }
10819
10820 /**
10821 * This is called when the view is attached to a window. At this point it
10822 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010823 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10824 * however it may be called any time before the first onDraw -- including
10825 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010826 *
10827 * @see #onDetachedFromWindow()
10828 */
10829 protected void onAttachedToWindow() {
10830 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10831 mParent.requestTransparentRegion(this);
10832 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010833 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10834 initialAwakenScrollBars();
10835 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10836 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010837 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010838 // Order is important here: LayoutDirection MUST be resolved before Padding
10839 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010840 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010841 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010842 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010843 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010844 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010845 if (isFocused()) {
10846 InputMethodManager imm = InputMethodManager.peekInstance();
10847 imm.focusIn(this);
10848 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010849 }
Cibu Johny86666632010-02-22 13:01:02 -080010850
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010851 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010852 * @see #onScreenStateChanged(int)
10853 */
10854 void dispatchScreenStateChanged(int screenState) {
10855 onScreenStateChanged(screenState);
10856 }
10857
10858 /**
10859 * This method is called whenever the state of the screen this view is
10860 * attached to changes. A state change will usually occurs when the screen
10861 * turns on or off (whether it happens automatically or the user does it
10862 * manually.)
10863 *
10864 * @param screenState The new state of the screen. Can be either
10865 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10866 */
10867 public void onScreenStateChanged(int screenState) {
10868 }
10869
10870 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010871 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10872 */
10873 private boolean hasRtlSupport() {
10874 return mContext.getApplicationInfo().hasRtlSupport();
10875 }
10876
10877 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010878 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10879 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010880 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010881 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010882 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010883 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010884 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010885
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010886 if (hasRtlSupport()) {
10887 // Set resolved depending on layout direction
10888 switch (getLayoutDirection()) {
10889 case LAYOUT_DIRECTION_INHERIT:
10890 // If this is root view, no need to look at parent's layout dir.
10891 if (canResolveLayoutDirection()) {
10892 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010893
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010894 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10895 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10896 }
10897 } else {
10898 // Nothing to do, LTR by default
10899 }
10900 break;
10901 case LAYOUT_DIRECTION_RTL:
10902 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10903 break;
10904 case LAYOUT_DIRECTION_LOCALE:
10905 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010906 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10907 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010908 break;
10909 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010910 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010911 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010912 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010913
10914 // Set to resolved
10915 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010916 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010917 // Resolve padding
10918 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010919 }
10920
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010921 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010922 * Called when layout direction has been resolved.
10923 *
10924 * The default implementation does nothing.
10925 */
10926 public void onResolvedLayoutDirectionChanged() {
10927 }
10928
10929 /**
10930 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010931 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010932 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010933 // If the user specified the absolute padding (either with android:padding or
10934 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10935 // use the default padding or the padding from the background drawable
10936 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010937 int resolvedLayoutDirection = getResolvedLayoutDirection();
10938 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010939 case LAYOUT_DIRECTION_RTL:
10940 // Start user padding override Right user padding. Otherwise, if Right user
10941 // padding is not defined, use the default Right padding. If Right user padding
10942 // is defined, just use it.
10943 if (mUserPaddingStart >= 0) {
10944 mUserPaddingRight = mUserPaddingStart;
10945 } else if (mUserPaddingRight < 0) {
10946 mUserPaddingRight = mPaddingRight;
10947 }
10948 if (mUserPaddingEnd >= 0) {
10949 mUserPaddingLeft = mUserPaddingEnd;
10950 } else if (mUserPaddingLeft < 0) {
10951 mUserPaddingLeft = mPaddingLeft;
10952 }
10953 break;
10954 case LAYOUT_DIRECTION_LTR:
10955 default:
10956 // Start user padding override Left user padding. Otherwise, if Left user
10957 // padding is not defined, use the default left padding. If Left user padding
10958 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010959 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010960 mUserPaddingLeft = mUserPaddingStart;
10961 } else if (mUserPaddingLeft < 0) {
10962 mUserPaddingLeft = mPaddingLeft;
10963 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010964 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010965 mUserPaddingRight = mUserPaddingEnd;
10966 } else if (mUserPaddingRight < 0) {
10967 mUserPaddingRight = mPaddingRight;
10968 }
10969 }
10970
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010971 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10972
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010973 if(isPaddingRelative()) {
10974 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10975 } else {
10976 recomputePadding();
10977 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010978 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010979 }
10980
10981 /**
10982 * Resolve padding depending on the layout direction. Subclasses that care about
10983 * padding resolution should override this method. The default implementation does
10984 * nothing.
10985 *
10986 * @param layoutDirection the direction of the layout
10987 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010988 * @see {@link #LAYOUT_DIRECTION_LTR}
10989 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010990 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010991 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010992 }
10993
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010994 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010995 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010996 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010997 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010998 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010999 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011000 switch (getLayoutDirection()) {
11001 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011002 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011003 default:
11004 return true;
11005 }
11006 }
11007
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011008 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011009 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11010 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011011 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011012 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011013 // Reset the current resolved bits
11014 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011015 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011016 // Reset also the text direction
11017 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011018 }
11019
11020 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011021 * Called during reset of resolved layout direction.
11022 *
11023 * Subclasses need to override this method to clear cached information that depends on the
11024 * resolved layout direction, or to inform child views that inherit their layout direction.
11025 *
11026 * The default implementation does nothing.
11027 */
11028 public void onResolvedLayoutDirectionReset() {
11029 }
11030
11031 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011032 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011033 *
11034 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011035 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011036 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011037 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011038 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011039 }
11040
11041 /**
11042 * This is called when the view is detached from a window. At this point it
11043 * no longer has a surface for drawing.
11044 *
11045 * @see #onAttachedToWindow()
11046 */
11047 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011048 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011049
Romain Guya440b002010-02-24 15:57:54 -080011050 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011051 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011052 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011053 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011056
Romain Guya998dff2012-03-23 18:58:36 -070011057 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011058
11059 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011060 if (mDisplayList != null) {
11061 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11062 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011063 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011064 } else {
11065 if (mDisplayList != null) {
11066 // Should never happen
11067 mDisplayList.invalidate();
11068 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011069 }
11070
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011071 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011072
11073 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011074 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011075 resetAccessibilityStateChanged();
11076 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011077 }
11078
11079 /**
11080 * @return The number of times this view has been attached to a window
11081 */
11082 protected int getWindowAttachCount() {
11083 return mWindowAttachCount;
11084 }
11085
11086 /**
11087 * Retrieve a unique token identifying the window this view is attached to.
11088 * @return Return the window's token for use in
11089 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11090 */
11091 public IBinder getWindowToken() {
11092 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11093 }
11094
11095 /**
11096 * Retrieve a unique token identifying the top-level "real" window of
11097 * the window that this view is attached to. That is, this is like
11098 * {@link #getWindowToken}, except if the window this view in is a panel
11099 * window (attached to another containing window), then the token of
11100 * the containing window is returned instead.
11101 *
11102 * @return Returns the associated window token, either
11103 * {@link #getWindowToken()} or the containing window's token.
11104 */
11105 public IBinder getApplicationWindowToken() {
11106 AttachInfo ai = mAttachInfo;
11107 if (ai != null) {
11108 IBinder appWindowToken = ai.mPanelParentWindowToken;
11109 if (appWindowToken == null) {
11110 appWindowToken = ai.mWindowToken;
11111 }
11112 return appWindowToken;
11113 }
11114 return null;
11115 }
11116
11117 /**
11118 * Retrieve private session object this view hierarchy is using to
11119 * communicate with the window manager.
11120 * @return the session object to communicate with the window manager
11121 */
11122 /*package*/ IWindowSession getWindowSession() {
11123 return mAttachInfo != null ? mAttachInfo.mSession : null;
11124 }
11125
11126 /**
11127 * @param info the {@link android.view.View.AttachInfo} to associated with
11128 * this view
11129 */
11130 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11131 //System.out.println("Attached! " + this);
11132 mAttachInfo = info;
11133 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011134 // We will need to evaluate the drawable state at least once.
11135 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011136 if (mFloatingTreeObserver != null) {
11137 info.mTreeObserver.merge(mFloatingTreeObserver);
11138 mFloatingTreeObserver = null;
11139 }
11140 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11141 mAttachInfo.mScrollContainers.add(this);
11142 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11143 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011144 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011146
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011147 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011148 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011149 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011150 if (listeners != null && listeners.size() > 0) {
11151 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11152 // perform the dispatching. The iterator is a safe guard against listeners that
11153 // could mutate the list by calling the various add/remove methods. This prevents
11154 // the array from being modified while we iterate it.
11155 for (OnAttachStateChangeListener listener : listeners) {
11156 listener.onViewAttachedToWindow(this);
11157 }
11158 }
11159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 int vis = info.mWindowVisibility;
11161 if (vis != GONE) {
11162 onWindowVisibilityChanged(vis);
11163 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011164 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11165 // If nobody has evaluated the drawable state yet, then do it now.
11166 refreshDrawableState();
11167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168 }
11169
11170 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011171 AttachInfo info = mAttachInfo;
11172 if (info != null) {
11173 int vis = info.mWindowVisibility;
11174 if (vis != GONE) {
11175 onWindowVisibilityChanged(GONE);
11176 }
11177 }
11178
11179 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011180
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011181 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011182 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011183 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011184 if (listeners != null && listeners.size() > 0) {
11185 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11186 // perform the dispatching. The iterator is a safe guard against listeners that
11187 // could mutate the list by calling the various add/remove methods. This prevents
11188 // the array from being modified while we iterate it.
11189 for (OnAttachStateChangeListener listener : listeners) {
11190 listener.onViewDetachedFromWindow(this);
11191 }
11192 }
11193
Romain Guy01d5edc2011-01-28 11:28:53 -080011194 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011195 mAttachInfo.mScrollContainers.remove(this);
11196 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11197 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011199 mAttachInfo = null;
11200 }
11201
11202 /**
11203 * Store this view hierarchy's frozen state into the given container.
11204 *
11205 * @param container The SparseArray in which to save the view's state.
11206 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011207 * @see #restoreHierarchyState(android.util.SparseArray)
11208 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11209 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 */
11211 public void saveHierarchyState(SparseArray<Parcelable> container) {
11212 dispatchSaveInstanceState(container);
11213 }
11214
11215 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011216 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11217 * this view and its children. May be overridden to modify how freezing happens to a
11218 * 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 -080011219 *
11220 * @param container The SparseArray in which to save the view's state.
11221 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011222 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11223 * @see #saveHierarchyState(android.util.SparseArray)
11224 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 */
11226 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11227 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11228 mPrivateFlags &= ~SAVE_STATE_CALLED;
11229 Parcelable state = onSaveInstanceState();
11230 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11231 throw new IllegalStateException(
11232 "Derived class did not call super.onSaveInstanceState()");
11233 }
11234 if (state != null) {
11235 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11236 // + ": " + state);
11237 container.put(mID, state);
11238 }
11239 }
11240 }
11241
11242 /**
11243 * Hook allowing a view to generate a representation of its internal state
11244 * that can later be used to create a new instance with that same state.
11245 * This state should only contain information that is not persistent or can
11246 * not be reconstructed later. For example, you will never store your
11247 * current position on screen because that will be computed again when a
11248 * new instance of the view is placed in its view hierarchy.
11249 * <p>
11250 * Some examples of things you may store here: the current cursor position
11251 * in a text view (but usually not the text itself since that is stored in a
11252 * content provider or other persistent storage), the currently selected
11253 * item in a list view.
11254 *
11255 * @return Returns a Parcelable object containing the view's current dynamic
11256 * state, or null if there is nothing interesting to save. The
11257 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011258 * @see #onRestoreInstanceState(android.os.Parcelable)
11259 * @see #saveHierarchyState(android.util.SparseArray)
11260 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 * @see #setSaveEnabled(boolean)
11262 */
11263 protected Parcelable onSaveInstanceState() {
11264 mPrivateFlags |= SAVE_STATE_CALLED;
11265 return BaseSavedState.EMPTY_STATE;
11266 }
11267
11268 /**
11269 * Restore this view hierarchy's frozen state from the given container.
11270 *
11271 * @param container The SparseArray which holds previously frozen states.
11272 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011273 * @see #saveHierarchyState(android.util.SparseArray)
11274 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11275 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011276 */
11277 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11278 dispatchRestoreInstanceState(container);
11279 }
11280
11281 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011282 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11283 * state for this view and its children. May be overridden to modify how restoring
11284 * happens to a view's children; for example, some views may want to not store state
11285 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011286 *
11287 * @param container The SparseArray which holds previously saved state.
11288 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011289 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11290 * @see #restoreHierarchyState(android.util.SparseArray)
11291 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 */
11293 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11294 if (mID != NO_ID) {
11295 Parcelable state = container.get(mID);
11296 if (state != null) {
11297 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11298 // + ": " + state);
11299 mPrivateFlags &= ~SAVE_STATE_CALLED;
11300 onRestoreInstanceState(state);
11301 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11302 throw new IllegalStateException(
11303 "Derived class did not call super.onRestoreInstanceState()");
11304 }
11305 }
11306 }
11307 }
11308
11309 /**
11310 * Hook allowing a view to re-apply a representation of its internal state that had previously
11311 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11312 * null state.
11313 *
11314 * @param state The frozen state that had previously been returned by
11315 * {@link #onSaveInstanceState}.
11316 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011317 * @see #onSaveInstanceState()
11318 * @see #restoreHierarchyState(android.util.SparseArray)
11319 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 */
11321 protected void onRestoreInstanceState(Parcelable state) {
11322 mPrivateFlags |= SAVE_STATE_CALLED;
11323 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011324 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11325 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011326 + "when two views of different type have the same id in the same hierarchy. "
11327 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011328 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011329 }
11330 }
11331
11332 /**
11333 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11334 *
11335 * @return the drawing start time in milliseconds
11336 */
11337 public long getDrawingTime() {
11338 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11339 }
11340
11341 /**
11342 * <p>Enables or disables the duplication of the parent's state into this view. When
11343 * duplication is enabled, this view gets its drawable state from its parent rather
11344 * than from its own internal properties.</p>
11345 *
11346 * <p>Note: in the current implementation, setting this property to true after the
11347 * view was added to a ViewGroup might have no effect at all. This property should
11348 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11349 *
11350 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11351 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011352 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011353 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11354 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011355 *
11356 * @param enabled True to enable duplication of the parent's drawable state, false
11357 * to disable it.
11358 *
11359 * @see #getDrawableState()
11360 * @see #isDuplicateParentStateEnabled()
11361 */
11362 public void setDuplicateParentStateEnabled(boolean enabled) {
11363 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11364 }
11365
11366 /**
11367 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11368 *
11369 * @return True if this view's drawable state is duplicated from the parent,
11370 * false otherwise
11371 *
11372 * @see #getDrawableState()
11373 * @see #setDuplicateParentStateEnabled(boolean)
11374 */
11375 public boolean isDuplicateParentStateEnabled() {
11376 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11377 }
11378
11379 /**
Romain Guy171c5922011-01-06 10:04:23 -080011380 * <p>Specifies the type of layer backing this view. The layer can be
11381 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11382 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011383 *
Romain Guy171c5922011-01-06 10:04:23 -080011384 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11385 * instance that controls how the layer is composed on screen. The following
11386 * properties of the paint are taken into account when composing the layer:</p>
11387 * <ul>
11388 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11389 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11390 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11391 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011392 *
Romain Guy171c5922011-01-06 10:04:23 -080011393 * <p>If this view has an alpha value set to < 1.0 by calling
11394 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11395 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11396 * equivalent to setting a hardware layer on this view and providing a paint with
11397 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011398 *
Romain Guy171c5922011-01-06 10:04:23 -080011399 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11400 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11401 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011402 *
Romain Guy171c5922011-01-06 10:04:23 -080011403 * @param layerType The ype of layer to use with this view, must be one of
11404 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11405 * {@link #LAYER_TYPE_HARDWARE}
11406 * @param paint The paint used to compose the layer. This argument is optional
11407 * and can be null. It is ignored when the layer type is
11408 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011409 *
11410 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011411 * @see #LAYER_TYPE_NONE
11412 * @see #LAYER_TYPE_SOFTWARE
11413 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011414 * @see #setAlpha(float)
11415 *
Romain Guy171c5922011-01-06 10:04:23 -080011416 * @attr ref android.R.styleable#View_layerType
11417 */
11418 public void setLayerType(int layerType, Paint paint) {
11419 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011420 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011421 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11422 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011423
Romain Guyd6cd5722011-01-17 14:42:41 -080011424 if (layerType == mLayerType) {
11425 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11426 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011427 invalidateParentCaches();
11428 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011429 }
11430 return;
11431 }
Romain Guy171c5922011-01-06 10:04:23 -080011432
11433 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011434 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011435 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011436 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011437 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011438 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011439 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011440 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011441 default:
11442 break;
Romain Guy171c5922011-01-06 10:04:23 -080011443 }
11444
11445 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011446 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11447 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11448 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011449
Romain Guy0fd89bf2011-01-26 15:41:30 -080011450 invalidateParentCaches();
11451 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011452 }
11453
11454 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011455 * Indicates whether this view has a static layer. A view with layer type
11456 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11457 * dynamic.
11458 */
11459 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011460 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011461 }
11462
11463 /**
Romain Guy171c5922011-01-06 10:04:23 -080011464 * Indicates what type of layer is currently associated with this view. By default
11465 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11466 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11467 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011468 *
Romain Guy171c5922011-01-06 10:04:23 -080011469 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11470 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011471 *
11472 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011473 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011474 * @see #LAYER_TYPE_NONE
11475 * @see #LAYER_TYPE_SOFTWARE
11476 * @see #LAYER_TYPE_HARDWARE
11477 */
11478 public int getLayerType() {
11479 return mLayerType;
11480 }
Joe Malin32736f02011-01-19 16:14:20 -080011481
Romain Guy6c319ca2011-01-11 14:29:25 -080011482 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011483 * Forces this view's layer to be created and this view to be rendered
11484 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11485 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011486 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011487 * This method can for instance be used to render a view into its layer before
11488 * starting an animation. If this view is complex, rendering into the layer
11489 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011490 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011491 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011492 *
11493 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011494 */
11495 public void buildLayer() {
11496 if (mLayerType == LAYER_TYPE_NONE) return;
11497
11498 if (mAttachInfo == null) {
11499 throw new IllegalStateException("This view must be attached to a window first");
11500 }
11501
11502 switch (mLayerType) {
11503 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011504 if (mAttachInfo.mHardwareRenderer != null &&
11505 mAttachInfo.mHardwareRenderer.isEnabled() &&
11506 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011507 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011508 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011509 break;
11510 case LAYER_TYPE_SOFTWARE:
11511 buildDrawingCache(true);
11512 break;
11513 }
11514 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011515
Romain Guy9c4b79a2011-11-10 19:23:58 -080011516 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11517 void flushLayer() {
11518 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11519 mHardwareLayer.flush();
11520 }
11521 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011522
11523 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011524 * <p>Returns a hardware layer that can be used to draw this view again
11525 * without executing its draw method.</p>
11526 *
11527 * @return A HardwareLayer ready to render, or null if an error occurred.
11528 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011529 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011530 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11531 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011532 return null;
11533 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011534
Romain Guy9c4b79a2011-11-10 19:23:58 -080011535 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011536
11537 final int width = mRight - mLeft;
11538 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011539
Romain Guy6c319ca2011-01-11 14:29:25 -080011540 if (width == 0 || height == 0) {
11541 return null;
11542 }
11543
11544 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11545 if (mHardwareLayer == null) {
11546 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11547 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011548 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011549 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11550 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011551 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011552 }
11553
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011554 // The layer is not valid if the underlying GPU resources cannot be allocated
11555 if (!mHardwareLayer.isValid()) {
11556 return null;
11557 }
11558
Chet Haasea1cff502012-02-21 13:43:44 -080011559 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011560 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011561 }
11562
11563 return mHardwareLayer;
11564 }
Romain Guy171c5922011-01-06 10:04:23 -080011565
Romain Guy589b0bb2011-10-10 13:57:47 -070011566 /**
11567 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011568 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011569 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011570 *
11571 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011572 * @see #LAYER_TYPE_HARDWARE
11573 */
Romain Guya998dff2012-03-23 18:58:36 -070011574 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011575 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011576 AttachInfo info = mAttachInfo;
11577 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011578 info.mHardwareRenderer.isEnabled() &&
11579 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011580 mHardwareLayer.destroy();
11581 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011582
Romain Guy9c4b79a2011-11-10 19:23:58 -080011583 invalidate(true);
11584 invalidateParentCaches();
11585 }
Romain Guy65b345f2011-07-27 18:51:50 -070011586 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011587 }
Romain Guy65b345f2011-07-27 18:51:50 -070011588 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011589 }
11590
Romain Guy171c5922011-01-06 10:04:23 -080011591 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011592 * Destroys all hardware rendering resources. This method is invoked
11593 * when the system needs to reclaim resources. Upon execution of this
11594 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011595 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011596 * Note: you <strong>must</strong> call
11597 * <code>super.destroyHardwareResources()</code> when overriding
11598 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011599 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011600 * @hide
11601 */
11602 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011603 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011604 }
11605
11606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011607 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11608 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11609 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11610 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11611 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11612 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011613 *
Romain Guy171c5922011-01-06 10:04:23 -080011614 * <p>Enabling the drawing cache is similar to
11615 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011616 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11617 * drawing cache has no effect on rendering because the system uses a different mechanism
11618 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11619 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11620 * for information on how to enable software and hardware layers.</p>
11621 *
11622 * <p>This API can be used to manually generate
11623 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11624 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011625 *
11626 * @param enabled true to enable the drawing cache, false otherwise
11627 *
11628 * @see #isDrawingCacheEnabled()
11629 * @see #getDrawingCache()
11630 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011631 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 */
11633 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011634 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011635 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11636 }
11637
11638 /**
11639 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11640 *
11641 * @return true if the drawing cache is enabled
11642 *
11643 * @see #setDrawingCacheEnabled(boolean)
11644 * @see #getDrawingCache()
11645 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011646 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 public boolean isDrawingCacheEnabled() {
11648 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11649 }
11650
11651 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011652 * Debugging utility which recursively outputs the dirty state of a view and its
11653 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011654 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011655 * @hide
11656 */
Romain Guy676b1732011-02-14 14:45:33 -080011657 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011658 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11659 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11660 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11661 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11662 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11663 if (clear) {
11664 mPrivateFlags &= clearMask;
11665 }
11666 if (this instanceof ViewGroup) {
11667 ViewGroup parent = (ViewGroup) this;
11668 final int count = parent.getChildCount();
11669 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011670 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011671 child.outputDirtyFlags(indent + " ", clear, clearMask);
11672 }
11673 }
11674 }
11675
11676 /**
11677 * This method is used by ViewGroup to cause its children to restore or recreate their
11678 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11679 * to recreate its own display list, which would happen if it went through the normal
11680 * draw/dispatchDraw mechanisms.
11681 *
11682 * @hide
11683 */
11684 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011685
11686 /**
11687 * A view that is not attached or hardware accelerated cannot create a display list.
11688 * This method checks these conditions and returns the appropriate result.
11689 *
11690 * @return true if view has the ability to create a display list, false otherwise.
11691 *
11692 * @hide
11693 */
11694 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011695 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011696 }
Joe Malin32736f02011-01-19 16:14:20 -080011697
Chet Haasedaf98e92011-01-10 14:10:36 -080011698 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011699 * @return The HardwareRenderer associated with that view or null if hardware rendering
11700 * is not supported or this this has not been attached to a window.
11701 *
11702 * @hide
11703 */
11704 public HardwareRenderer getHardwareRenderer() {
11705 if (mAttachInfo != null) {
11706 return mAttachInfo.mHardwareRenderer;
11707 }
11708 return null;
11709 }
11710
11711 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011712 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11713 * Otherwise, the same display list will be returned (after having been rendered into
11714 * along the way, depending on the invalidation state of the view).
11715 *
11716 * @param displayList The previous version of this displayList, could be null.
11717 * @param isLayer Whether the requester of the display list is a layer. If so,
11718 * the view will avoid creating a layer inside the resulting display list.
11719 * @return A new or reused DisplayList object.
11720 */
11721 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11722 if (!canHaveDisplayList()) {
11723 return null;
11724 }
11725
11726 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11727 displayList == null || !displayList.isValid() ||
11728 (!isLayer && mRecreateDisplayList))) {
11729 // Don't need to recreate the display list, just need to tell our
11730 // children to restore/recreate theirs
11731 if (displayList != null && displayList.isValid() &&
11732 !isLayer && !mRecreateDisplayList) {
11733 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11734 mPrivateFlags &= ~DIRTY_MASK;
11735 dispatchGetDisplayList();
11736
11737 return displayList;
11738 }
11739
11740 if (!isLayer) {
11741 // If we got here, we're recreating it. Mark it as such to ensure that
11742 // we copy in child display lists into ours in drawChild()
11743 mRecreateDisplayList = true;
11744 }
11745 if (displayList == null) {
11746 final String name = getClass().getSimpleName();
11747 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11748 // If we're creating a new display list, make sure our parent gets invalidated
11749 // since they will need to recreate their display list to account for this
11750 // new child display list.
11751 invalidateParentCaches();
11752 }
11753
11754 boolean caching = false;
11755 final HardwareCanvas canvas = displayList.start();
11756 int restoreCount = 0;
11757 int width = mRight - mLeft;
11758 int height = mBottom - mTop;
11759
11760 try {
11761 canvas.setViewport(width, height);
11762 // The dirty rect should always be null for a display list
11763 canvas.onPreDraw(null);
11764 int layerType = (
11765 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11766 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070011767 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070011768 if (layerType == LAYER_TYPE_HARDWARE) {
11769 final HardwareLayer layer = getHardwareLayer();
11770 if (layer != null && layer.isValid()) {
11771 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11772 } else {
11773 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11774 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11775 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11776 }
11777 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011778 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011779 buildDrawingCache(true);
11780 Bitmap cache = getDrawingCache(true);
11781 if (cache != null) {
11782 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11783 caching = true;
11784 }
Chet Haasea1cff502012-02-21 13:43:44 -080011785 }
Chet Haasea1cff502012-02-21 13:43:44 -080011786 } else {
11787
11788 computeScroll();
11789
Chet Haasea1cff502012-02-21 13:43:44 -080011790 canvas.translate(-mScrollX, -mScrollY);
11791 if (!isLayer) {
11792 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11793 mPrivateFlags &= ~DIRTY_MASK;
11794 }
11795
11796 // Fast path for layouts with no backgrounds
11797 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11798 dispatchDraw(canvas);
11799 } else {
11800 draw(canvas);
11801 }
11802 }
11803 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080011804 canvas.onPostDraw();
11805
11806 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070011807 displayList.setCaching(caching);
11808 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080011809 displayList.setLeftTopRightBottom(0, 0, width, height);
11810 } else {
11811 setDisplayListProperties(displayList);
11812 }
11813 }
11814 } else if (!isLayer) {
11815 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11816 mPrivateFlags &= ~DIRTY_MASK;
11817 }
11818
11819 return displayList;
11820 }
11821
11822 /**
11823 * Get the DisplayList for the HardwareLayer
11824 *
11825 * @param layer The HardwareLayer whose DisplayList we want
11826 * @return A DisplayList fopr the specified HardwareLayer
11827 */
11828 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11829 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11830 layer.setDisplayList(displayList);
11831 return displayList;
11832 }
11833
11834
11835 /**
Romain Guyb051e892010-09-28 19:09:36 -070011836 * <p>Returns a display list that can be used to draw this view again
11837 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011838 *
Romain Guyb051e892010-09-28 19:09:36 -070011839 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011840 *
11841 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011842 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011843 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011844 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011845 return mDisplayList;
11846 }
11847
11848 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011849 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011850 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011851 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011852 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011853 * @see #getDrawingCache(boolean)
11854 */
11855 public Bitmap getDrawingCache() {
11856 return getDrawingCache(false);
11857 }
11858
11859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11861 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11862 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11863 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11864 * request the drawing cache by calling this method and draw it on screen if the
11865 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011866 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011867 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11868 * this method will create a bitmap of the same size as this view. Because this bitmap
11869 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11870 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11871 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11872 * size than the view. This implies that your application must be able to handle this
11873 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011874 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011875 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11876 * the current density of the screen when the application is in compatibility
11877 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011878 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011879 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011880 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011881 * @see #setDrawingCacheEnabled(boolean)
11882 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011883 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 * @see #destroyDrawingCache()
11885 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011886 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11888 return null;
11889 }
11890 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011891 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 }
Romain Guy02890fd2010-08-06 17:58:44 -070011893 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011894 }
11895
11896 /**
11897 * <p>Frees the resources used by the drawing cache. If you call
11898 * {@link #buildDrawingCache()} manually without calling
11899 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11900 * should cleanup the cache with this method afterwards.</p>
11901 *
11902 * @see #setDrawingCacheEnabled(boolean)
11903 * @see #buildDrawingCache()
11904 * @see #getDrawingCache()
11905 */
11906 public void destroyDrawingCache() {
11907 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011908 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011909 mDrawingCache = null;
11910 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011911 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011912 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011913 mUnscaledDrawingCache = null;
11914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011915 }
11916
11917 /**
11918 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011919 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 * view will always be drawn on top of a solid color.
11921 *
11922 * @param color The background color to use for the drawing cache's bitmap
11923 *
11924 * @see #setDrawingCacheEnabled(boolean)
11925 * @see #buildDrawingCache()
11926 * @see #getDrawingCache()
11927 */
11928 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011929 if (color != mDrawingCacheBackgroundColor) {
11930 mDrawingCacheBackgroundColor = color;
11931 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011933 }
11934
11935 /**
11936 * @see #setDrawingCacheBackgroundColor(int)
11937 *
11938 * @return The background color to used for the drawing cache's bitmap
11939 */
11940 public int getDrawingCacheBackgroundColor() {
11941 return mDrawingCacheBackgroundColor;
11942 }
11943
11944 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011945 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011946 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011947 * @see #buildDrawingCache(boolean)
11948 */
11949 public void buildDrawingCache() {
11950 buildDrawingCache(false);
11951 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011952
Romain Guyfbd8f692009-06-26 14:51:58 -070011953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011954 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11955 *
11956 * <p>If you call {@link #buildDrawingCache()} manually without calling
11957 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11958 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011959 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011960 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11961 * this method will create a bitmap of the same size as this view. Because this bitmap
11962 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11963 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11964 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11965 * size than the view. This implies that your application must be able to handle this
11966 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011967 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011968 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11969 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011970 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011971 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011972 *
11973 * @see #getDrawingCache()
11974 * @see #destroyDrawingCache()
11975 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011976 public void buildDrawingCache(boolean autoScale) {
11977 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011978 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011979 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011980
11981 if (ViewDebug.TRACE_HIERARCHY) {
11982 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011984
Romain Guy8506ab42009-06-11 17:35:47 -070011985 int width = mRight - mLeft;
11986 int height = mBottom - mTop;
11987
11988 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011989 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011990
Romain Guye1123222009-06-29 14:24:56 -070011991 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011992 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11993 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995
11996 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011997 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011998 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999
12000 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012001 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012002 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12004 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012005 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012006 return;
12007 }
12008
12009 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012010 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012011
12012 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012013 Bitmap.Config quality;
12014 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012015 // Never pick ARGB_4444 because it looks awful
12016 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012017 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12018 case DRAWING_CACHE_QUALITY_AUTO:
12019 quality = Bitmap.Config.ARGB_8888;
12020 break;
12021 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012022 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012023 break;
12024 case DRAWING_CACHE_QUALITY_HIGH:
12025 quality = Bitmap.Config.ARGB_8888;
12026 break;
12027 default:
12028 quality = Bitmap.Config.ARGB_8888;
12029 break;
12030 }
12031 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012032 // Optimization for translucent windows
12033 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012034 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012035 }
12036
12037 // Try to cleanup memory
12038 if (bitmap != null) bitmap.recycle();
12039
12040 try {
12041 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012042 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012043 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012044 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012045 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012046 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012047 }
Adam Powell26153a32010-11-08 15:22:27 -080012048 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 } catch (OutOfMemoryError e) {
12050 // If there is not enough memory to create the bitmap cache, just
12051 // ignore the issue as bitmap caches are not required to draw the
12052 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012053 if (autoScale) {
12054 mDrawingCache = null;
12055 } else {
12056 mUnscaledDrawingCache = null;
12057 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012058 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012059 return;
12060 }
12061
12062 clear = drawingCacheBackgroundColor != 0;
12063 }
12064
12065 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 if (attachInfo != null) {
12067 canvas = attachInfo.mCanvas;
12068 if (canvas == null) {
12069 canvas = new Canvas();
12070 }
12071 canvas.setBitmap(bitmap);
12072 // Temporarily clobber the cached Canvas in case one of our children
12073 // is also using a drawing cache. Without this, the children would
12074 // steal the canvas by attaching their own bitmap to it and bad, bad
12075 // thing would happen (invisible views, corrupted drawings, etc.)
12076 attachInfo.mCanvas = null;
12077 } else {
12078 // This case should hopefully never or seldom happen
12079 canvas = new Canvas(bitmap);
12080 }
12081
12082 if (clear) {
12083 bitmap.eraseColor(drawingCacheBackgroundColor);
12084 }
12085
12086 computeScroll();
12087 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012088
Romain Guye1123222009-06-29 14:24:56 -070012089 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012090 final float scale = attachInfo.mApplicationScale;
12091 canvas.scale(scale, scale);
12092 }
Joe Malin32736f02011-01-19 16:14:20 -080012093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012094 canvas.translate(-mScrollX, -mScrollY);
12095
Romain Guy5bcdff42009-05-14 21:27:18 -070012096 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012097 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12098 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012099 mPrivateFlags |= DRAWING_CACHE_VALID;
12100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012101
12102 // Fast path for layouts with no backgrounds
12103 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12104 if (ViewDebug.TRACE_HIERARCHY) {
12105 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12106 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012107 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012108 dispatchDraw(canvas);
12109 } else {
12110 draw(canvas);
12111 }
12112
12113 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012114 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115
12116 if (attachInfo != null) {
12117 // Restore the cached Canvas for our siblings
12118 attachInfo.mCanvas = canvas;
12119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012120 }
12121 }
12122
12123 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012124 * Create a snapshot of the view into a bitmap. We should probably make
12125 * some form of this public, but should think about the API.
12126 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012127 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012128 int width = mRight - mLeft;
12129 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012130
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012131 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012132 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012133 width = (int) ((width * scale) + 0.5f);
12134 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012135
Romain Guy8c11e312009-09-14 15:15:30 -070012136 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012137 if (bitmap == null) {
12138 throw new OutOfMemoryError();
12139 }
12140
Romain Guyc529d8d2011-09-06 15:01:39 -070012141 Resources resources = getResources();
12142 if (resources != null) {
12143 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12144 }
Joe Malin32736f02011-01-19 16:14:20 -080012145
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012146 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012147 if (attachInfo != null) {
12148 canvas = attachInfo.mCanvas;
12149 if (canvas == null) {
12150 canvas = new Canvas();
12151 }
12152 canvas.setBitmap(bitmap);
12153 // Temporarily clobber the cached Canvas in case one of our children
12154 // is also using a drawing cache. Without this, the children would
12155 // steal the canvas by attaching their own bitmap to it and bad, bad
12156 // things would happen (invisible views, corrupted drawings, etc.)
12157 attachInfo.mCanvas = null;
12158 } else {
12159 // This case should hopefully never or seldom happen
12160 canvas = new Canvas(bitmap);
12161 }
12162
Romain Guy5bcdff42009-05-14 21:27:18 -070012163 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012164 bitmap.eraseColor(backgroundColor);
12165 }
12166
12167 computeScroll();
12168 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012169 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012170 canvas.translate(-mScrollX, -mScrollY);
12171
Romain Guy5bcdff42009-05-14 21:27:18 -070012172 // Temporarily remove the dirty mask
12173 int flags = mPrivateFlags;
12174 mPrivateFlags &= ~DIRTY_MASK;
12175
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012176 // Fast path for layouts with no backgrounds
12177 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12178 dispatchDraw(canvas);
12179 } else {
12180 draw(canvas);
12181 }
12182
Romain Guy5bcdff42009-05-14 21:27:18 -070012183 mPrivateFlags = flags;
12184
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012185 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012186 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012187
12188 if (attachInfo != null) {
12189 // Restore the cached Canvas for our siblings
12190 attachInfo.mCanvas = canvas;
12191 }
Romain Guy8506ab42009-06-11 17:35:47 -070012192
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012193 return bitmap;
12194 }
12195
12196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012197 * Indicates whether this View is currently in edit mode. A View is usually
12198 * in edit mode when displayed within a developer tool. For instance, if
12199 * this View is being drawn by a visual user interface builder, this method
12200 * should return true.
12201 *
12202 * Subclasses should check the return value of this method to provide
12203 * different behaviors if their normal behavior might interfere with the
12204 * host environment. For instance: the class spawns a thread in its
12205 * constructor, the drawing code relies on device-specific features, etc.
12206 *
12207 * This method is usually checked in the drawing code of custom widgets.
12208 *
12209 * @return True if this View is in edit mode, false otherwise.
12210 */
12211 public boolean isInEditMode() {
12212 return false;
12213 }
12214
12215 /**
12216 * If the View draws content inside its padding and enables fading edges,
12217 * it needs to support padding offsets. Padding offsets are added to the
12218 * fading edges to extend the length of the fade so that it covers pixels
12219 * drawn inside the padding.
12220 *
12221 * Subclasses of this class should override this method if they need
12222 * to draw content inside the padding.
12223 *
12224 * @return True if padding offset must be applied, false otherwise.
12225 *
12226 * @see #getLeftPaddingOffset()
12227 * @see #getRightPaddingOffset()
12228 * @see #getTopPaddingOffset()
12229 * @see #getBottomPaddingOffset()
12230 *
12231 * @since CURRENT
12232 */
12233 protected boolean isPaddingOffsetRequired() {
12234 return false;
12235 }
12236
12237 /**
12238 * Amount by which to extend the left fading region. Called only when
12239 * {@link #isPaddingOffsetRequired()} returns true.
12240 *
12241 * @return The left padding offset in pixels.
12242 *
12243 * @see #isPaddingOffsetRequired()
12244 *
12245 * @since CURRENT
12246 */
12247 protected int getLeftPaddingOffset() {
12248 return 0;
12249 }
12250
12251 /**
12252 * Amount by which to extend the right fading region. Called only when
12253 * {@link #isPaddingOffsetRequired()} returns true.
12254 *
12255 * @return The right padding offset in pixels.
12256 *
12257 * @see #isPaddingOffsetRequired()
12258 *
12259 * @since CURRENT
12260 */
12261 protected int getRightPaddingOffset() {
12262 return 0;
12263 }
12264
12265 /**
12266 * Amount by which to extend the top fading region. Called only when
12267 * {@link #isPaddingOffsetRequired()} returns true.
12268 *
12269 * @return The top padding offset in pixels.
12270 *
12271 * @see #isPaddingOffsetRequired()
12272 *
12273 * @since CURRENT
12274 */
12275 protected int getTopPaddingOffset() {
12276 return 0;
12277 }
12278
12279 /**
12280 * Amount by which to extend the bottom fading region. Called only when
12281 * {@link #isPaddingOffsetRequired()} returns true.
12282 *
12283 * @return The bottom padding offset in pixels.
12284 *
12285 * @see #isPaddingOffsetRequired()
12286 *
12287 * @since CURRENT
12288 */
12289 protected int getBottomPaddingOffset() {
12290 return 0;
12291 }
12292
12293 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012294 * @hide
12295 * @param offsetRequired
12296 */
12297 protected int getFadeTop(boolean offsetRequired) {
12298 int top = mPaddingTop;
12299 if (offsetRequired) top += getTopPaddingOffset();
12300 return top;
12301 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012302
Romain Guyf2fc4602011-07-19 15:20:03 -070012303 /**
12304 * @hide
12305 * @param offsetRequired
12306 */
12307 protected int getFadeHeight(boolean offsetRequired) {
12308 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012309 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012310 return mBottom - mTop - mPaddingBottom - padding;
12311 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012312
Romain Guyf2fc4602011-07-19 15:20:03 -070012313 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012314 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012315 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012316 *
Romain Guy2bffd262010-09-12 17:40:02 -070012317 * <p>Even if this method returns true, it does not mean that every call
12318 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12319 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012320 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012321 * window is hardware accelerated,
12322 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12323 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012324 *
Romain Guy2bffd262010-09-12 17:40:02 -070012325 * @return True if the view is attached to a window and the window is
12326 * hardware accelerated; false in any other case.
12327 */
12328 public boolean isHardwareAccelerated() {
12329 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12330 }
Joe Malin32736f02011-01-19 16:14:20 -080012331
Romain Guy2bffd262010-09-12 17:40:02 -070012332 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012333 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12334 * case of an active Animation being run on the view.
12335 */
12336 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12337 Animation a, boolean scalingRequired) {
12338 Transformation invalidationTransform;
12339 final int flags = parent.mGroupFlags;
12340 final boolean initialized = a.isInitialized();
12341 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012342 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012343 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12344 onAnimationStart();
12345 }
12346
12347 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12348 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12349 if (parent.mInvalidationTransformation == null) {
12350 parent.mInvalidationTransformation = new Transformation();
12351 }
12352 invalidationTransform = parent.mInvalidationTransformation;
12353 a.getTransformation(drawingTime, invalidationTransform, 1f);
12354 } else {
12355 invalidationTransform = parent.mChildTransformation;
12356 }
12357 if (more) {
12358 if (!a.willChangeBounds()) {
12359 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12360 parent.FLAG_OPTIMIZE_INVALIDATE) {
12361 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12362 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12363 // The child need to draw an animation, potentially offscreen, so
12364 // make sure we do not cancel invalidate requests
12365 parent.mPrivateFlags |= DRAW_ANIMATION;
12366 parent.invalidate(mLeft, mTop, mRight, mBottom);
12367 }
12368 } else {
12369 if (parent.mInvalidateRegion == null) {
12370 parent.mInvalidateRegion = new RectF();
12371 }
12372 final RectF region = parent.mInvalidateRegion;
12373 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12374 invalidationTransform);
12375
12376 // The child need to draw an animation, potentially offscreen, so
12377 // make sure we do not cancel invalidate requests
12378 parent.mPrivateFlags |= DRAW_ANIMATION;
12379
12380 final int left = mLeft + (int) region.left;
12381 final int top = mTop + (int) region.top;
12382 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12383 top + (int) (region.height() + .5f));
12384 }
12385 }
12386 return more;
12387 }
12388
Chet Haasea1cff502012-02-21 13:43:44 -080012389 void setDisplayListProperties() {
12390 setDisplayListProperties(mDisplayList);
12391 }
12392
12393 /**
12394 * This method is called by getDisplayList() when a display list is created or re-rendered.
12395 * It sets or resets the current value of all properties on that display list (resetting is
12396 * necessary when a display list is being re-created, because we need to make sure that
12397 * previously-set transform values
12398 */
12399 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012400 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012401 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012402 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012403 if (mParent instanceof ViewGroup) {
12404 displayList.setClipChildren(
12405 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12406 }
Chet Haase9420abd2012-03-29 16:28:32 -070012407 float alpha = 1;
12408 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12409 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12410 ViewGroup parentVG = (ViewGroup) mParent;
12411 final boolean hasTransform =
12412 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12413 if (hasTransform) {
12414 Transformation transform = parentVG.mChildTransformation;
12415 final int transformType = parentVG.mChildTransformation.getTransformationType();
12416 if (transformType != Transformation.TYPE_IDENTITY) {
12417 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12418 alpha = transform.getAlpha();
12419 }
12420 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12421 displayList.setStaticMatrix(transform.getMatrix());
12422 }
12423 }
12424 }
Chet Haasea1cff502012-02-21 13:43:44 -080012425 }
12426 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012427 alpha *= mTransformationInfo.mAlpha;
12428 if (alpha < 1) {
12429 final int multipliedAlpha = (int) (255 * alpha);
12430 if (onSetAlpha(multipliedAlpha)) {
12431 alpha = 1;
12432 }
12433 }
12434 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012435 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12436 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12437 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12438 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012439 if (mTransformationInfo.mCamera == null) {
12440 mTransformationInfo.mCamera = new Camera();
12441 mTransformationInfo.matrix3D = new Matrix();
12442 }
12443 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012444 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12445 displayList.setPivotX(getPivotX());
12446 displayList.setPivotY(getPivotY());
12447 }
Chet Haase9420abd2012-03-29 16:28:32 -070012448 } else if (alpha < 1) {
12449 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012450 }
12451 }
12452 }
12453
Chet Haasebcca79a2012-02-14 08:45:14 -080012454 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012455 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12456 * This draw() method is an implementation detail and is not intended to be overridden or
12457 * to be called from anywhere else other than ViewGroup.drawChild().
12458 */
12459 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012460 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012461 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012462 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012463 final int flags = parent.mGroupFlags;
12464
Chet Haasea1cff502012-02-21 13:43:44 -080012465 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012466 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012467 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012468 }
12469
12470 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012471 boolean concatMatrix = false;
12472
12473 boolean scalingRequired = false;
12474 boolean caching;
12475 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12476
12477 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012478 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12479 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012480 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012481 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012482 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12483 } else {
12484 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12485 }
12486
Chet Haasebcca79a2012-02-14 08:45:14 -080012487 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012488 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012489 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012490 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012491 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012492 } else if (!useDisplayListProperties &&
12493 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012494 final boolean hasTransform =
12495 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012496 if (hasTransform) {
12497 final int transformType = parent.mChildTransformation.getTransformationType();
12498 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12499 parent.mChildTransformation : null;
12500 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12501 }
12502 }
12503
12504 concatMatrix |= !childHasIdentityMatrix;
12505
12506 // Sets the flag as early as possible to allow draw() implementations
12507 // to call invalidate() successfully when doing animations
12508 mPrivateFlags |= DRAWN;
12509
Chet Haasebcca79a2012-02-14 08:45:14 -080012510 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012511 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12512 return more;
12513 }
12514
12515 if (hardwareAccelerated) {
12516 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12517 // retain the flag's value temporarily in the mRecreateDisplayList flag
12518 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12519 mPrivateFlags &= ~INVALIDATED;
12520 }
12521
12522 computeScroll();
12523
12524 final int sx = mScrollX;
12525 final int sy = mScrollY;
12526
12527 DisplayList displayList = null;
12528 Bitmap cache = null;
12529 boolean hasDisplayList = false;
12530 if (caching) {
12531 if (!hardwareAccelerated) {
12532 if (layerType != LAYER_TYPE_NONE) {
12533 layerType = LAYER_TYPE_SOFTWARE;
12534 buildDrawingCache(true);
12535 }
12536 cache = getDrawingCache(true);
12537 } else {
12538 switch (layerType) {
12539 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012540 if (useDisplayListProperties) {
12541 hasDisplayList = canHaveDisplayList();
12542 } else {
12543 buildDrawingCache(true);
12544 cache = getDrawingCache(true);
12545 }
Chet Haase64a48c12012-02-13 16:33:29 -080012546 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012547 case LAYER_TYPE_HARDWARE:
12548 if (useDisplayListProperties) {
12549 hasDisplayList = canHaveDisplayList();
12550 }
12551 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012552 case LAYER_TYPE_NONE:
12553 // Delay getting the display list until animation-driven alpha values are
12554 // set up and possibly passed on to the view
12555 hasDisplayList = canHaveDisplayList();
12556 break;
12557 }
12558 }
12559 }
Chet Haasea1cff502012-02-21 13:43:44 -080012560 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012561 if (useDisplayListProperties) {
12562 displayList = getDisplayList();
12563 if (!displayList.isValid()) {
12564 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12565 // to getDisplayList(), the display list will be marked invalid and we should not
12566 // try to use it again.
12567 displayList = null;
12568 hasDisplayList = false;
12569 useDisplayListProperties = false;
12570 }
12571 }
Chet Haase64a48c12012-02-13 16:33:29 -080012572
12573 final boolean hasNoCache = cache == null || hasDisplayList;
12574 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12575 layerType != LAYER_TYPE_HARDWARE;
12576
Chet Haasea1cff502012-02-21 13:43:44 -080012577 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012578 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012579 restoreTo = canvas.save();
12580 }
Chet Haase64a48c12012-02-13 16:33:29 -080012581 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012582 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012583 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012584 if (!useDisplayListProperties) {
12585 canvas.translate(mLeft, mTop);
12586 }
Chet Haase64a48c12012-02-13 16:33:29 -080012587 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012588 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012589 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012590 restoreTo = canvas.save();
12591 }
Chet Haase64a48c12012-02-13 16:33:29 -080012592 // mAttachInfo cannot be null, otherwise scalingRequired == false
12593 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12594 canvas.scale(scale, scale);
12595 }
12596 }
12597
Chet Haasea1cff502012-02-21 13:43:44 -080012598 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012599 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012600 if (transformToApply != null || !childHasIdentityMatrix) {
12601 int transX = 0;
12602 int transY = 0;
12603
12604 if (offsetForScroll) {
12605 transX = -sx;
12606 transY = -sy;
12607 }
12608
12609 if (transformToApply != null) {
12610 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012611 if (useDisplayListProperties) {
12612 displayList.setAnimationMatrix(transformToApply.getMatrix());
12613 } else {
12614 // Undo the scroll translation, apply the transformation matrix,
12615 // then redo the scroll translate to get the correct result.
12616 canvas.translate(-transX, -transY);
12617 canvas.concat(transformToApply.getMatrix());
12618 canvas.translate(transX, transY);
12619 }
Chet Haasea1cff502012-02-21 13:43:44 -080012620 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012621 }
12622
12623 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012624 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012625 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012626 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012627 }
12628 }
12629
Chet Haasea1cff502012-02-21 13:43:44 -080012630 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012631 canvas.translate(-transX, -transY);
12632 canvas.concat(getMatrix());
12633 canvas.translate(transX, transY);
12634 }
12635 }
12636
Chet Haase9420abd2012-03-29 16:28:32 -070012637 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012638 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012639 if (hasNoCache) {
12640 final int multipliedAlpha = (int) (255 * alpha);
12641 if (!onSetAlpha(multipliedAlpha)) {
12642 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012643 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012644 layerType != LAYER_TYPE_NONE) {
12645 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12646 }
Chet Haase9420abd2012-03-29 16:28:32 -070012647 if (useDisplayListProperties) {
12648 displayList.setAlpha(alpha * getAlpha());
12649 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012650 final int scrollX = hasDisplayList ? 0 : sx;
12651 final int scrollY = hasDisplayList ? 0 : sy;
12652 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12653 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012654 }
12655 } else {
12656 // Alpha is handled by the child directly, clobber the layer's alpha
12657 mPrivateFlags |= ALPHA_SET;
12658 }
12659 }
12660 }
12661 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12662 onSetAlpha(255);
12663 mPrivateFlags &= ~ALPHA_SET;
12664 }
12665
Chet Haasea1cff502012-02-21 13:43:44 -080012666 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12667 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012668 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012669 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012670 } else {
12671 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012672 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012673 } else {
12674 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12675 }
12676 }
12677 }
12678
Chet Haase9420abd2012-03-29 16:28:32 -070012679 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012680 displayList = getDisplayList();
12681 if (!displayList.isValid()) {
12682 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12683 // to getDisplayList(), the display list will be marked invalid and we should not
12684 // try to use it again.
12685 displayList = null;
12686 hasDisplayList = false;
12687 }
12688 }
12689
12690 if (hasNoCache) {
12691 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012692 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012693 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012694 if (layer != null && layer.isValid()) {
12695 mLayerPaint.setAlpha((int) (alpha * 255));
12696 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12697 layerRendered = true;
12698 } else {
12699 final int scrollX = hasDisplayList ? 0 : sx;
12700 final int scrollY = hasDisplayList ? 0 : sy;
12701 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012702 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012703 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12704 }
12705 }
12706
12707 if (!layerRendered) {
12708 if (!hasDisplayList) {
12709 // Fast path for layouts with no backgrounds
12710 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12711 if (ViewDebug.TRACE_HIERARCHY) {
12712 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12713 }
12714 mPrivateFlags &= ~DIRTY_MASK;
12715 dispatchDraw(canvas);
12716 } else {
12717 draw(canvas);
12718 }
12719 } else {
12720 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070012721 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012722 }
12723 }
12724 } else if (cache != null) {
12725 mPrivateFlags &= ~DIRTY_MASK;
12726 Paint cachePaint;
12727
12728 if (layerType == LAYER_TYPE_NONE) {
12729 cachePaint = parent.mCachePaint;
12730 if (cachePaint == null) {
12731 cachePaint = new Paint();
12732 cachePaint.setDither(false);
12733 parent.mCachePaint = cachePaint;
12734 }
Chet Haase9420abd2012-03-29 16:28:32 -070012735 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012736 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012737 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12738 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012739 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012740 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012741 }
12742 } else {
12743 cachePaint = mLayerPaint;
12744 cachePaint.setAlpha((int) (alpha * 255));
12745 }
12746 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12747 }
12748
Chet Haasea1cff502012-02-21 13:43:44 -080012749 if (restoreTo >= 0) {
12750 canvas.restoreToCount(restoreTo);
12751 }
Chet Haase64a48c12012-02-13 16:33:29 -080012752
12753 if (a != null && !more) {
12754 if (!hardwareAccelerated && !a.getFillAfter()) {
12755 onSetAlpha(255);
12756 }
12757 parent.finishAnimatingView(this, a);
12758 }
12759
12760 if (more && hardwareAccelerated) {
12761 // invalidation is the trigger to recreate display lists, so if we're using
12762 // display lists to render, force an invalidate to allow the animation to
12763 // continue drawing another frame
12764 parent.invalidate(true);
12765 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12766 // alpha animations should cause the child to recreate its display list
12767 invalidate(true);
12768 }
12769 }
12770
12771 mRecreateDisplayList = false;
12772
12773 return more;
12774 }
12775
12776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 * Manually render this view (and all of its children) to the given Canvas.
12778 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012779 * called. When implementing a view, implement
12780 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12781 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 *
12783 * @param canvas The Canvas to which the View is rendered.
12784 */
12785 public void draw(Canvas canvas) {
12786 if (ViewDebug.TRACE_HIERARCHY) {
12787 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12788 }
12789
Romain Guy5bcdff42009-05-14 21:27:18 -070012790 final int privateFlags = mPrivateFlags;
12791 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12792 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12793 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012795 /*
12796 * Draw traversal performs several drawing steps which must be executed
12797 * in the appropriate order:
12798 *
12799 * 1. Draw the background
12800 * 2. If necessary, save the canvas' layers to prepare for fading
12801 * 3. Draw view's content
12802 * 4. Draw children
12803 * 5. If necessary, draw the fading edges and restore layers
12804 * 6. Draw decorations (scrollbars for instance)
12805 */
12806
12807 // Step 1, draw the background, if needed
12808 int saveCount;
12809
Romain Guy24443ea2009-05-11 11:56:30 -070012810 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012811 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012812 if (background != null) {
12813 final int scrollX = mScrollX;
12814 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815
Romain Guy24443ea2009-05-11 11:56:30 -070012816 if (mBackgroundSizeChanged) {
12817 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12818 mBackgroundSizeChanged = false;
12819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012820
Romain Guy24443ea2009-05-11 11:56:30 -070012821 if ((scrollX | scrollY) == 0) {
12822 background.draw(canvas);
12823 } else {
12824 canvas.translate(scrollX, scrollY);
12825 background.draw(canvas);
12826 canvas.translate(-scrollX, -scrollY);
12827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 }
12829 }
12830
12831 // skip step 2 & 5 if possible (common case)
12832 final int viewFlags = mViewFlags;
12833 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12834 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12835 if (!verticalEdges && !horizontalEdges) {
12836 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012837 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012838
12839 // Step 4, draw the children
12840 dispatchDraw(canvas);
12841
12842 // Step 6, draw decorations (scrollbars)
12843 onDrawScrollBars(canvas);
12844
12845 // we're done...
12846 return;
12847 }
12848
12849 /*
12850 * Here we do the full fledged routine...
12851 * (this is an uncommon case where speed matters less,
12852 * this is why we repeat some of the tests that have been
12853 * done above)
12854 */
12855
12856 boolean drawTop = false;
12857 boolean drawBottom = false;
12858 boolean drawLeft = false;
12859 boolean drawRight = false;
12860
12861 float topFadeStrength = 0.0f;
12862 float bottomFadeStrength = 0.0f;
12863 float leftFadeStrength = 0.0f;
12864 float rightFadeStrength = 0.0f;
12865
12866 // Step 2, save the canvas' layers
12867 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012868
12869 final boolean offsetRequired = isPaddingOffsetRequired();
12870 if (offsetRequired) {
12871 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 }
12873
12874 int left = mScrollX + paddingLeft;
12875 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012876 int top = mScrollY + getFadeTop(offsetRequired);
12877 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012878
12879 if (offsetRequired) {
12880 right += getRightPaddingOffset();
12881 bottom += getBottomPaddingOffset();
12882 }
12883
12884 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012885 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012886 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012887
12888 // clip the fade length if top and bottom fades overlap
12889 // overlapping fades produce odd-looking artifacts
12890 if (verticalEdges && (top + length > bottom - length)) {
12891 length = (bottom - top) / 2;
12892 }
12893
12894 // also clip horizontal fades if necessary
12895 if (horizontalEdges && (left + length > right - length)) {
12896 length = (right - left) / 2;
12897 }
12898
12899 if (verticalEdges) {
12900 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012901 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012902 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012903 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012904 }
12905
12906 if (horizontalEdges) {
12907 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012908 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012909 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012910 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012911 }
12912
12913 saveCount = canvas.getSaveCount();
12914
12915 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012916 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012917 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12918
12919 if (drawTop) {
12920 canvas.saveLayer(left, top, right, top + length, null, flags);
12921 }
12922
12923 if (drawBottom) {
12924 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12925 }
12926
12927 if (drawLeft) {
12928 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12929 }
12930
12931 if (drawRight) {
12932 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12933 }
12934 } else {
12935 scrollabilityCache.setFadeColor(solidColor);
12936 }
12937
12938 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012939 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012940
12941 // Step 4, draw the children
12942 dispatchDraw(canvas);
12943
12944 // Step 5, draw the fade effect and restore layers
12945 final Paint p = scrollabilityCache.paint;
12946 final Matrix matrix = scrollabilityCache.matrix;
12947 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012948
12949 if (drawTop) {
12950 matrix.setScale(1, fadeHeight * topFadeStrength);
12951 matrix.postTranslate(left, top);
12952 fade.setLocalMatrix(matrix);
12953 canvas.drawRect(left, top, right, top + length, p);
12954 }
12955
12956 if (drawBottom) {
12957 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12958 matrix.postRotate(180);
12959 matrix.postTranslate(left, bottom);
12960 fade.setLocalMatrix(matrix);
12961 canvas.drawRect(left, bottom - length, right, bottom, p);
12962 }
12963
12964 if (drawLeft) {
12965 matrix.setScale(1, fadeHeight * leftFadeStrength);
12966 matrix.postRotate(-90);
12967 matrix.postTranslate(left, top);
12968 fade.setLocalMatrix(matrix);
12969 canvas.drawRect(left, top, left + length, bottom, p);
12970 }
12971
12972 if (drawRight) {
12973 matrix.setScale(1, fadeHeight * rightFadeStrength);
12974 matrix.postRotate(90);
12975 matrix.postTranslate(right, top);
12976 fade.setLocalMatrix(matrix);
12977 canvas.drawRect(right - length, top, right, bottom, p);
12978 }
12979
12980 canvas.restoreToCount(saveCount);
12981
12982 // Step 6, draw decorations (scrollbars)
12983 onDrawScrollBars(canvas);
12984 }
12985
12986 /**
12987 * Override this if your view is known to always be drawn on top of a solid color background,
12988 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12989 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12990 * should be set to 0xFF.
12991 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012992 * @see #setVerticalFadingEdgeEnabled(boolean)
12993 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012994 *
12995 * @return The known solid color background for this view, or 0 if the color may vary
12996 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012997 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012998 public int getSolidColor() {
12999 return 0;
13000 }
13001
13002 /**
13003 * Build a human readable string representation of the specified view flags.
13004 *
13005 * @param flags the view flags to convert to a string
13006 * @return a String representing the supplied flags
13007 */
13008 private static String printFlags(int flags) {
13009 String output = "";
13010 int numFlags = 0;
13011 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13012 output += "TAKES_FOCUS";
13013 numFlags++;
13014 }
13015
13016 switch (flags & VISIBILITY_MASK) {
13017 case INVISIBLE:
13018 if (numFlags > 0) {
13019 output += " ";
13020 }
13021 output += "INVISIBLE";
13022 // USELESS HERE numFlags++;
13023 break;
13024 case GONE:
13025 if (numFlags > 0) {
13026 output += " ";
13027 }
13028 output += "GONE";
13029 // USELESS HERE numFlags++;
13030 break;
13031 default:
13032 break;
13033 }
13034 return output;
13035 }
13036
13037 /**
13038 * Build a human readable string representation of the specified private
13039 * view flags.
13040 *
13041 * @param privateFlags the private view flags to convert to a string
13042 * @return a String representing the supplied flags
13043 */
13044 private static String printPrivateFlags(int privateFlags) {
13045 String output = "";
13046 int numFlags = 0;
13047
13048 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13049 output += "WANTS_FOCUS";
13050 numFlags++;
13051 }
13052
13053 if ((privateFlags & FOCUSED) == FOCUSED) {
13054 if (numFlags > 0) {
13055 output += " ";
13056 }
13057 output += "FOCUSED";
13058 numFlags++;
13059 }
13060
13061 if ((privateFlags & SELECTED) == SELECTED) {
13062 if (numFlags > 0) {
13063 output += " ";
13064 }
13065 output += "SELECTED";
13066 numFlags++;
13067 }
13068
13069 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13070 if (numFlags > 0) {
13071 output += " ";
13072 }
13073 output += "IS_ROOT_NAMESPACE";
13074 numFlags++;
13075 }
13076
13077 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13078 if (numFlags > 0) {
13079 output += " ";
13080 }
13081 output += "HAS_BOUNDS";
13082 numFlags++;
13083 }
13084
13085 if ((privateFlags & DRAWN) == DRAWN) {
13086 if (numFlags > 0) {
13087 output += " ";
13088 }
13089 output += "DRAWN";
13090 // USELESS HERE numFlags++;
13091 }
13092 return output;
13093 }
13094
13095 /**
13096 * <p>Indicates whether or not this view's layout will be requested during
13097 * the next hierarchy layout pass.</p>
13098 *
13099 * @return true if the layout will be forced during next layout pass
13100 */
13101 public boolean isLayoutRequested() {
13102 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13103 }
13104
13105 /**
13106 * Assign a size and position to a view and all of its
13107 * descendants
13108 *
13109 * <p>This is the second phase of the layout mechanism.
13110 * (The first is measuring). In this phase, each parent calls
13111 * layout on all of its children to position them.
13112 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013113 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013114 *
Chet Haase9c087442011-01-12 16:20:16 -080013115 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013116 * Derived classes with children should override
13117 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013118 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013119 *
13120 * @param l Left position, relative to parent
13121 * @param t Top position, relative to parent
13122 * @param r Right position, relative to parent
13123 * @param b Bottom position, relative to parent
13124 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013125 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013126 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013127 int oldL = mLeft;
13128 int oldT = mTop;
13129 int oldB = mBottom;
13130 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013131 boolean changed = setFrame(l, t, r, b);
13132 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13133 if (ViewDebug.TRACE_HIERARCHY) {
13134 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13135 }
13136
13137 onLayout(changed, l, t, r, b);
13138 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013139
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013140 ListenerInfo li = mListenerInfo;
13141 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013142 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013143 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013144 int numListeners = listenersCopy.size();
13145 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013146 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013147 }
13148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013149 }
13150 mPrivateFlags &= ~FORCE_LAYOUT;
13151 }
13152
13153 /**
13154 * Called from layout when this view should
13155 * assign a size and position to each of its children.
13156 *
13157 * Derived classes with children should override
13158 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013159 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013160 * @param changed This is a new size or position for this view
13161 * @param left Left position, relative to parent
13162 * @param top Top position, relative to parent
13163 * @param right Right position, relative to parent
13164 * @param bottom Bottom position, relative to parent
13165 */
13166 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13167 }
13168
13169 /**
13170 * Assign a size and position to this view.
13171 *
13172 * This is called from layout.
13173 *
13174 * @param left Left position, relative to parent
13175 * @param top Top position, relative to parent
13176 * @param right Right position, relative to parent
13177 * @param bottom Bottom position, relative to parent
13178 * @return true if the new size and position are different than the
13179 * previous ones
13180 * {@hide}
13181 */
13182 protected boolean setFrame(int left, int top, int right, int bottom) {
13183 boolean changed = false;
13184
13185 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013186 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187 + right + "," + bottom + ")");
13188 }
13189
13190 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13191 changed = true;
13192
13193 // Remember our drawn bit
13194 int drawn = mPrivateFlags & DRAWN;
13195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013196 int oldWidth = mRight - mLeft;
13197 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013198 int newWidth = right - left;
13199 int newHeight = bottom - top;
13200 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13201
13202 // Invalidate our old position
13203 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013204
13205 mLeft = left;
13206 mTop = top;
13207 mRight = right;
13208 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013209 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013210 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013212
13213 mPrivateFlags |= HAS_BOUNDS;
13214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013215
Chet Haase75755e22011-07-18 17:48:25 -070013216 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013217 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13218 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013219 if (mTransformationInfo != null) {
13220 mTransformationInfo.mMatrixDirty = true;
13221 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013223 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13224 }
13225
13226 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13227 // If we are visible, force the DRAWN bit to on so that
13228 // this invalidate will go through (at least to our parent).
13229 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013230 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013231 // the DRAWN bit.
13232 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013233 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013234 // parent display list may need to be recreated based on a change in the bounds
13235 // of any child
13236 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013237 }
13238
13239 // Reset drawn bit to original value (invalidate turns it off)
13240 mPrivateFlags |= drawn;
13241
13242 mBackgroundSizeChanged = true;
13243 }
13244 return changed;
13245 }
13246
13247 /**
13248 * Finalize inflating a view from XML. This is called as the last phase
13249 * of inflation, after all child views have been added.
13250 *
13251 * <p>Even if the subclass overrides onFinishInflate, they should always be
13252 * sure to call the super method, so that we get called.
13253 */
13254 protected void onFinishInflate() {
13255 }
13256
13257 /**
13258 * Returns the resources associated with this view.
13259 *
13260 * @return Resources object.
13261 */
13262 public Resources getResources() {
13263 return mResources;
13264 }
13265
13266 /**
13267 * Invalidates the specified Drawable.
13268 *
13269 * @param drawable the drawable to invalidate
13270 */
13271 public void invalidateDrawable(Drawable drawable) {
13272 if (verifyDrawable(drawable)) {
13273 final Rect dirty = drawable.getBounds();
13274 final int scrollX = mScrollX;
13275 final int scrollY = mScrollY;
13276
13277 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13278 dirty.right + scrollX, dirty.bottom + scrollY);
13279 }
13280 }
13281
13282 /**
13283 * Schedules an action on a drawable to occur at a specified time.
13284 *
13285 * @param who the recipient of the action
13286 * @param what the action to run on the drawable
13287 * @param when the time at which the action must occur. Uses the
13288 * {@link SystemClock#uptimeMillis} timebase.
13289 */
13290 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013291 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013292 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013293 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013294 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13295 Choreographer.CALLBACK_ANIMATION, what, who,
13296 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013297 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013298 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013300 }
13301 }
13302
13303 /**
13304 * Cancels a scheduled action on a drawable.
13305 *
13306 * @param who the recipient of the action
13307 * @param what the action to cancel
13308 */
13309 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013310 if (verifyDrawable(who) && what != null) {
13311 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013312 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13313 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013314 } else {
13315 ViewRootImpl.getRunQueue().removeCallbacks(what);
13316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013317 }
13318 }
13319
13320 /**
13321 * Unschedule any events associated with the given Drawable. This can be
13322 * used when selecting a new Drawable into a view, so that the previous
13323 * one is completely unscheduled.
13324 *
13325 * @param who The Drawable to unschedule.
13326 *
13327 * @see #drawableStateChanged
13328 */
13329 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013330 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013331 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13332 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013333 }
13334 }
13335
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013336 /**
13337 * Return the layout direction of a given Drawable.
13338 *
13339 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013340 */
13341 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013342 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013343 }
13344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013345 /**
13346 * If your view subclass is displaying its own Drawable objects, it should
13347 * override this function and return true for any Drawable it is
13348 * displaying. This allows animations for those drawables to be
13349 * scheduled.
13350 *
13351 * <p>Be sure to call through to the super class when overriding this
13352 * function.
13353 *
13354 * @param who The Drawable to verify. Return true if it is one you are
13355 * displaying, else return the result of calling through to the
13356 * super class.
13357 *
13358 * @return boolean If true than the Drawable is being displayed in the
13359 * view; else false and it is not allowed to animate.
13360 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013361 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13362 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013363 */
13364 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013365 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 }
13367
13368 /**
13369 * This function is called whenever the state of the view changes in such
13370 * a way that it impacts the state of drawables being shown.
13371 *
13372 * <p>Be sure to call through to the superclass when overriding this
13373 * function.
13374 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013375 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013376 */
13377 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013378 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013379 if (d != null && d.isStateful()) {
13380 d.setState(getDrawableState());
13381 }
13382 }
13383
13384 /**
13385 * Call this to force a view to update its drawable state. This will cause
13386 * drawableStateChanged to be called on this view. Views that are interested
13387 * in the new state should call getDrawableState.
13388 *
13389 * @see #drawableStateChanged
13390 * @see #getDrawableState
13391 */
13392 public void refreshDrawableState() {
13393 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13394 drawableStateChanged();
13395
13396 ViewParent parent = mParent;
13397 if (parent != null) {
13398 parent.childDrawableStateChanged(this);
13399 }
13400 }
13401
13402 /**
13403 * Return an array of resource IDs of the drawable states representing the
13404 * current state of the view.
13405 *
13406 * @return The current drawable state
13407 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013408 * @see Drawable#setState(int[])
13409 * @see #drawableStateChanged()
13410 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 */
13412 public final int[] getDrawableState() {
13413 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13414 return mDrawableState;
13415 } else {
13416 mDrawableState = onCreateDrawableState(0);
13417 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13418 return mDrawableState;
13419 }
13420 }
13421
13422 /**
13423 * Generate the new {@link android.graphics.drawable.Drawable} state for
13424 * this view. This is called by the view
13425 * system when the cached Drawable state is determined to be invalid. To
13426 * retrieve the current state, you should use {@link #getDrawableState}.
13427 *
13428 * @param extraSpace if non-zero, this is the number of extra entries you
13429 * would like in the returned array in which you can place your own
13430 * states.
13431 *
13432 * @return Returns an array holding the current {@link Drawable} state of
13433 * the view.
13434 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013435 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013436 */
13437 protected int[] onCreateDrawableState(int extraSpace) {
13438 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13439 mParent instanceof View) {
13440 return ((View) mParent).onCreateDrawableState(extraSpace);
13441 }
13442
13443 int[] drawableState;
13444
13445 int privateFlags = mPrivateFlags;
13446
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013447 int viewStateIndex = 0;
13448 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13449 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13450 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013451 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013452 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13453 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013454 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13455 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013456 // This is set if HW acceleration is requested, even if the current
13457 // process doesn't allow it. This is just to allow app preview
13458 // windows to better match their app.
13459 viewStateIndex |= VIEW_STATE_ACCELERATED;
13460 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013461 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013462
Christopher Tate3d4bf172011-03-28 16:16:46 -070013463 final int privateFlags2 = mPrivateFlags2;
13464 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13465 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013467 drawableState = VIEW_STATE_SETS[viewStateIndex];
13468
13469 //noinspection ConstantIfStatement
13470 if (false) {
13471 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13472 Log.i("View", toString()
13473 + " pressed=" + ((privateFlags & PRESSED) != 0)
13474 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13475 + " fo=" + hasFocus()
13476 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013477 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478 + ": " + Arrays.toString(drawableState));
13479 }
13480
13481 if (extraSpace == 0) {
13482 return drawableState;
13483 }
13484
13485 final int[] fullState;
13486 if (drawableState != null) {
13487 fullState = new int[drawableState.length + extraSpace];
13488 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13489 } else {
13490 fullState = new int[extraSpace];
13491 }
13492
13493 return fullState;
13494 }
13495
13496 /**
13497 * Merge your own state values in <var>additionalState</var> into the base
13498 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013499 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500 *
13501 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013502 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013503 * own additional state values.
13504 *
13505 * @param additionalState The additional state values you would like
13506 * added to <var>baseState</var>; this array is not modified.
13507 *
13508 * @return As a convenience, the <var>baseState</var> array you originally
13509 * passed into the function is returned.
13510 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013511 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 */
13513 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13514 final int N = baseState.length;
13515 int i = N - 1;
13516 while (i >= 0 && baseState[i] == 0) {
13517 i--;
13518 }
13519 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13520 return baseState;
13521 }
13522
13523 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013524 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13525 * on all Drawable objects associated with this view.
13526 */
13527 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013528 if (mBackground != null) {
13529 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013530 }
13531 }
13532
13533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013534 * Sets the background color for this view.
13535 * @param color the color of the background
13536 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013537 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013538 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013539 if (mBackground instanceof ColorDrawable) {
13540 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013541 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013542 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013544 }
13545
13546 /**
13547 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013548 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013549 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013551 * @attr ref android.R.styleable#View_background
13552 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013553 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013554 public void setBackgroundResource(int resid) {
13555 if (resid != 0 && resid == mBackgroundResource) {
13556 return;
13557 }
13558
13559 Drawable d= null;
13560 if (resid != 0) {
13561 d = mResources.getDrawable(resid);
13562 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013563 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013564
13565 mBackgroundResource = resid;
13566 }
13567
13568 /**
13569 * Set the background to a given Drawable, or remove the background. If the
13570 * background has padding, this View's padding is set to the background's
13571 * padding. However, when a background is removed, this View's padding isn't
13572 * touched. If setting the padding is desired, please use
13573 * {@link #setPadding(int, int, int, int)}.
13574 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013575 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013576 * background
13577 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013578 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013579 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013580 setBackgroundDrawable(background);
13581 }
13582
13583 /**
13584 * @deprecated use {@link #setBackground(Drawable)} instead
13585 */
13586 @Deprecated
13587 public void setBackgroundDrawable(Drawable background) {
13588 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013589 return;
13590 }
13591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013592 boolean requestLayout = false;
13593
13594 mBackgroundResource = 0;
13595
13596 /*
13597 * Regardless of whether we're setting a new background or not, we want
13598 * to clear the previous drawable.
13599 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013600 if (mBackground != null) {
13601 mBackground.setCallback(null);
13602 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 }
13604
Philip Milne6c8ea062012-04-03 17:38:43 -070013605 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606 Rect padding = sThreadLocal.get();
13607 if (padding == null) {
13608 padding = new Rect();
13609 sThreadLocal.set(padding);
13610 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013611 if (background.getPadding(padding)) {
13612 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013613 case LAYOUT_DIRECTION_RTL:
13614 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13615 break;
13616 case LAYOUT_DIRECTION_LTR:
13617 default:
13618 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013620 }
13621
13622 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13623 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013624 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13625 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013626 requestLayout = true;
13627 }
13628
Philip Milne6c8ea062012-04-03 17:38:43 -070013629 background.setCallback(this);
13630 if (background.isStateful()) {
13631 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013632 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013633 background.setVisible(getVisibility() == VISIBLE, false);
13634 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013635
13636 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13637 mPrivateFlags &= ~SKIP_DRAW;
13638 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13639 requestLayout = true;
13640 }
13641 } else {
13642 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013643 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013644
13645 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13646 /*
13647 * This view ONLY drew the background before and we're removing
13648 * the background, so now it won't draw anything
13649 * (hence we SKIP_DRAW)
13650 */
13651 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13652 mPrivateFlags |= SKIP_DRAW;
13653 }
13654
13655 /*
13656 * When the background is set, we try to apply its padding to this
13657 * View. When the background is removed, we don't touch this View's
13658 * padding. This is noted in the Javadocs. Hence, we don't need to
13659 * requestLayout(), the invalidate() below is sufficient.
13660 */
13661
13662 // The old background's minimum size could have affected this
13663 // View's layout, so let's requestLayout
13664 requestLayout = true;
13665 }
13666
Romain Guy8f1344f52009-05-15 16:03:59 -070013667 computeOpaqueFlags();
13668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013669 if (requestLayout) {
13670 requestLayout();
13671 }
13672
13673 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013674 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013675 }
13676
13677 /**
13678 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013679 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013680 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013681 *
13682 * @see #setBackground(Drawable)
13683 *
13684 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 */
13686 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013687 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013688 }
13689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013690 /**
13691 * Sets the padding. The view may add on the space required to display
13692 * the scrollbars, depending on the style and visibility of the scrollbars.
13693 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13694 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13695 * from the values set in this call.
13696 *
13697 * @attr ref android.R.styleable#View_padding
13698 * @attr ref android.R.styleable#View_paddingBottom
13699 * @attr ref android.R.styleable#View_paddingLeft
13700 * @attr ref android.R.styleable#View_paddingRight
13701 * @attr ref android.R.styleable#View_paddingTop
13702 * @param left the left padding in pixels
13703 * @param top the top padding in pixels
13704 * @param right the right padding in pixels
13705 * @param bottom the bottom padding in pixels
13706 */
13707 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013708 mUserPaddingStart = -1;
13709 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013710 mUserPaddingRelative = false;
13711
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013712 internalSetPadding(left, top, right, bottom);
13713 }
13714
13715 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013716 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013717 mUserPaddingRight = right;
13718 mUserPaddingBottom = bottom;
13719
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013720 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013721 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013722
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013723 // Common case is there are no scroll bars.
13724 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013725 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013726 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013727 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013728 switch (mVerticalScrollbarPosition) {
13729 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013730 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13731 left += offset;
13732 } else {
13733 right += offset;
13734 }
13735 break;
Adam Powell20232d02010-12-08 21:08:53 -080013736 case SCROLLBAR_POSITION_RIGHT:
13737 right += offset;
13738 break;
13739 case SCROLLBAR_POSITION_LEFT:
13740 left += offset;
13741 break;
13742 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013743 }
Adam Powell20232d02010-12-08 21:08:53 -080013744 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013745 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13746 ? 0 : getHorizontalScrollbarHeight();
13747 }
13748 }
Romain Guy8506ab42009-06-11 17:35:47 -070013749
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013750 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 changed = true;
13752 mPaddingLeft = left;
13753 }
13754 if (mPaddingTop != top) {
13755 changed = true;
13756 mPaddingTop = top;
13757 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013758 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013759 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013760 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013761 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013762 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013763 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013764 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765 }
13766
13767 if (changed) {
13768 requestLayout();
13769 }
13770 }
13771
13772 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013773 * Sets the relative padding. The view may add on the space required to display
13774 * the scrollbars, depending on the style and visibility of the scrollbars.
13775 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13776 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13777 * from the values set in this call.
13778 *
13779 * @attr ref android.R.styleable#View_padding
13780 * @attr ref android.R.styleable#View_paddingBottom
13781 * @attr ref android.R.styleable#View_paddingStart
13782 * @attr ref android.R.styleable#View_paddingEnd
13783 * @attr ref android.R.styleable#View_paddingTop
13784 * @param start the start padding in pixels
13785 * @param top the top padding in pixels
13786 * @param end the end padding in pixels
13787 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013788 */
13789 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013790 mUserPaddingStart = start;
13791 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013792 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013793
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013794 switch(getResolvedLayoutDirection()) {
13795 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013796 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013797 break;
13798 case LAYOUT_DIRECTION_LTR:
13799 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013800 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013801 }
13802 }
13803
13804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013805 * Returns the top padding of this view.
13806 *
13807 * @return the top padding in pixels
13808 */
13809 public int getPaddingTop() {
13810 return mPaddingTop;
13811 }
13812
13813 /**
13814 * Returns the bottom padding of this view. If there are inset and enabled
13815 * scrollbars, this value may include the space required to display the
13816 * scrollbars as well.
13817 *
13818 * @return the bottom padding in pixels
13819 */
13820 public int getPaddingBottom() {
13821 return mPaddingBottom;
13822 }
13823
13824 /**
13825 * Returns the left padding of this view. If there are inset and enabled
13826 * scrollbars, this value may include the space required to display the
13827 * scrollbars as well.
13828 *
13829 * @return the left padding in pixels
13830 */
13831 public int getPaddingLeft() {
13832 return mPaddingLeft;
13833 }
13834
13835 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013836 * Returns the start padding of this view depending on its resolved layout direction.
13837 * If there are inset and enabled scrollbars, this value may include the space
13838 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013839 *
13840 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013841 */
13842 public int getPaddingStart() {
13843 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13844 mPaddingRight : mPaddingLeft;
13845 }
13846
13847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013848 * Returns the right padding of this view. If there are inset and enabled
13849 * scrollbars, this value may include the space required to display the
13850 * scrollbars as well.
13851 *
13852 * @return the right padding in pixels
13853 */
13854 public int getPaddingRight() {
13855 return mPaddingRight;
13856 }
13857
13858 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013859 * Returns the end padding of this view depending on its resolved layout direction.
13860 * If there are inset and enabled scrollbars, this value may include the space
13861 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013862 *
13863 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013864 */
13865 public int getPaddingEnd() {
13866 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13867 mPaddingLeft : mPaddingRight;
13868 }
13869
13870 /**
13871 * Return if the padding as been set thru relative values
13872 * {@link #setPaddingRelative(int, int, int, int)} or thru
13873 * @attr ref android.R.styleable#View_paddingStart or
13874 * @attr ref android.R.styleable#View_paddingEnd
13875 *
13876 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013877 */
13878 public boolean isPaddingRelative() {
13879 return mUserPaddingRelative;
13880 }
13881
13882 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013883 * @hide
13884 */
Philip Milne7a23b492012-04-24 22:12:36 -070013885 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070013886 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013887 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013888 }
13889 return mLayoutInsets;
13890 }
13891
13892 /**
13893 * @hide
13894 */
13895 public void setLayoutInsets(Insets layoutInsets) {
13896 mLayoutInsets = layoutInsets;
13897 }
13898
13899 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 * Changes the selection state of this view. A view can be selected or not.
13901 * Note that selection is not the same as focus. Views are typically
13902 * selected in the context of an AdapterView like ListView or GridView;
13903 * the selected view is the view that is highlighted.
13904 *
13905 * @param selected true if the view must be selected, false otherwise
13906 */
13907 public void setSelected(boolean selected) {
13908 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13909 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013910 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013911 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 refreshDrawableState();
13913 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013914 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13915 notifyAccessibilityStateChanged();
13916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013917 }
13918 }
13919
13920 /**
13921 * Dispatch setSelected to all of this View's children.
13922 *
13923 * @see #setSelected(boolean)
13924 *
13925 * @param selected The new selected state
13926 */
13927 protected void dispatchSetSelected(boolean selected) {
13928 }
13929
13930 /**
13931 * Indicates the selection state of this view.
13932 *
13933 * @return true if the view is selected, false otherwise
13934 */
13935 @ViewDebug.ExportedProperty
13936 public boolean isSelected() {
13937 return (mPrivateFlags & SELECTED) != 0;
13938 }
13939
13940 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013941 * Changes the activated state of this view. A view can be activated or not.
13942 * Note that activation is not the same as selection. Selection is
13943 * a transient property, representing the view (hierarchy) the user is
13944 * currently interacting with. Activation is a longer-term state that the
13945 * user can move views in and out of. For example, in a list view with
13946 * single or multiple selection enabled, the views in the current selection
13947 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13948 * here.) The activated state is propagated down to children of the view it
13949 * is set on.
13950 *
13951 * @param activated true if the view must be activated, false otherwise
13952 */
13953 public void setActivated(boolean activated) {
13954 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13955 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013956 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013957 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013958 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013959 }
13960 }
13961
13962 /**
13963 * Dispatch setActivated to all of this View's children.
13964 *
13965 * @see #setActivated(boolean)
13966 *
13967 * @param activated The new activated state
13968 */
13969 protected void dispatchSetActivated(boolean activated) {
13970 }
13971
13972 /**
13973 * Indicates the activation state of this view.
13974 *
13975 * @return true if the view is activated, false otherwise
13976 */
13977 @ViewDebug.ExportedProperty
13978 public boolean isActivated() {
13979 return (mPrivateFlags & ACTIVATED) != 0;
13980 }
13981
13982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13984 * observer can be used to get notifications when global events, like
13985 * layout, happen.
13986 *
13987 * The returned ViewTreeObserver observer is not guaranteed to remain
13988 * valid for the lifetime of this View. If the caller of this method keeps
13989 * a long-lived reference to ViewTreeObserver, it should always check for
13990 * the return value of {@link ViewTreeObserver#isAlive()}.
13991 *
13992 * @return The ViewTreeObserver for this view's hierarchy.
13993 */
13994 public ViewTreeObserver getViewTreeObserver() {
13995 if (mAttachInfo != null) {
13996 return mAttachInfo.mTreeObserver;
13997 }
13998 if (mFloatingTreeObserver == null) {
13999 mFloatingTreeObserver = new ViewTreeObserver();
14000 }
14001 return mFloatingTreeObserver;
14002 }
14003
14004 /**
14005 * <p>Finds the topmost view in the current view hierarchy.</p>
14006 *
14007 * @return the topmost view containing this view
14008 */
14009 public View getRootView() {
14010 if (mAttachInfo != null) {
14011 final View v = mAttachInfo.mRootView;
14012 if (v != null) {
14013 return v;
14014 }
14015 }
Romain Guy8506ab42009-06-11 17:35:47 -070014016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 View parent = this;
14018
14019 while (parent.mParent != null && parent.mParent instanceof View) {
14020 parent = (View) parent.mParent;
14021 }
14022
14023 return parent;
14024 }
14025
14026 /**
14027 * <p>Computes the coordinates of this view on the screen. The argument
14028 * must be an array of two integers. After the method returns, the array
14029 * contains the x and y location in that order.</p>
14030 *
14031 * @param location an array of two integers in which to hold the coordinates
14032 */
14033 public void getLocationOnScreen(int[] location) {
14034 getLocationInWindow(location);
14035
14036 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014037 if (info != null) {
14038 location[0] += info.mWindowLeft;
14039 location[1] += info.mWindowTop;
14040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014041 }
14042
14043 /**
14044 * <p>Computes the coordinates of this view in its window. The argument
14045 * must be an array of two integers. After the method returns, the array
14046 * contains the x and y location in that order.</p>
14047 *
14048 * @param location an array of two integers in which to hold the coordinates
14049 */
14050 public void getLocationInWindow(int[] location) {
14051 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014052 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014053 }
14054
Gilles Debunne6583ce52011-12-06 18:09:02 -080014055 if (mAttachInfo == null) {
14056 // When the view is not attached to a window, this method does not make sense
14057 location[0] = location[1] = 0;
14058 return;
14059 }
14060
Gilles Debunnecea45132011-11-24 02:19:27 +010014061 float[] position = mAttachInfo.mTmpTransformLocation;
14062 position[0] = position[1] = 0.0f;
14063
14064 if (!hasIdentityMatrix()) {
14065 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067
Gilles Debunnecea45132011-11-24 02:19:27 +010014068 position[0] += mLeft;
14069 position[1] += mTop;
14070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014071 ViewParent viewParent = mParent;
14072 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014073 final View view = (View) viewParent;
14074
14075 position[0] -= view.mScrollX;
14076 position[1] -= view.mScrollY;
14077
14078 if (!view.hasIdentityMatrix()) {
14079 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014080 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014081
14082 position[0] += view.mLeft;
14083 position[1] += view.mTop;
14084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014086 }
Romain Guy8506ab42009-06-11 17:35:47 -070014087
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014088 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014089 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014090 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14091 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014093
14094 location[0] = (int) (position[0] + 0.5f);
14095 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014096 }
14097
14098 /**
14099 * {@hide}
14100 * @param id the id of the view to be found
14101 * @return the view of the specified id, null if cannot be found
14102 */
14103 protected View findViewTraversal(int id) {
14104 if (id == mID) {
14105 return this;
14106 }
14107 return null;
14108 }
14109
14110 /**
14111 * {@hide}
14112 * @param tag the tag of the view to be found
14113 * @return the view of specified tag, null if cannot be found
14114 */
14115 protected View findViewWithTagTraversal(Object tag) {
14116 if (tag != null && tag.equals(mTag)) {
14117 return this;
14118 }
14119 return null;
14120 }
14121
14122 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014123 * {@hide}
14124 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014125 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014126 * @return The first view that matches the predicate or null.
14127 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014128 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014129 if (predicate.apply(this)) {
14130 return this;
14131 }
14132 return null;
14133 }
14134
14135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014136 * Look for a child view with the given id. If this view has the given
14137 * id, return this view.
14138 *
14139 * @param id The id to search for.
14140 * @return The view that has the given id in the hierarchy or null
14141 */
14142 public final View findViewById(int id) {
14143 if (id < 0) {
14144 return null;
14145 }
14146 return findViewTraversal(id);
14147 }
14148
14149 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014150 * Finds a view by its unuque and stable accessibility id.
14151 *
14152 * @param accessibilityId The searched accessibility id.
14153 * @return The found view.
14154 */
14155 final View findViewByAccessibilityId(int accessibilityId) {
14156 if (accessibilityId < 0) {
14157 return null;
14158 }
14159 return findViewByAccessibilityIdTraversal(accessibilityId);
14160 }
14161
14162 /**
14163 * Performs the traversal to find a view by its unuque and stable accessibility id.
14164 *
14165 * <strong>Note:</strong>This method does not stop at the root namespace
14166 * boundary since the user can touch the screen at an arbitrary location
14167 * potentially crossing the root namespace bounday which will send an
14168 * accessibility event to accessibility services and they should be able
14169 * to obtain the event source. Also accessibility ids are guaranteed to be
14170 * unique in the window.
14171 *
14172 * @param accessibilityId The accessibility id.
14173 * @return The found view.
14174 */
14175 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14176 if (getAccessibilityViewId() == accessibilityId) {
14177 return this;
14178 }
14179 return null;
14180 }
14181
14182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183 * Look for a child view with the given tag. If this view has the given
14184 * tag, return this view.
14185 *
14186 * @param tag The tag to search for, using "tag.equals(getTag())".
14187 * @return The View that has the given tag in the hierarchy or null
14188 */
14189 public final View findViewWithTag(Object tag) {
14190 if (tag == null) {
14191 return null;
14192 }
14193 return findViewWithTagTraversal(tag);
14194 }
14195
14196 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014197 * {@hide}
14198 * Look for a child view that matches the specified predicate.
14199 * If this view matches the predicate, return this view.
14200 *
14201 * @param predicate The predicate to evaluate.
14202 * @return The first view that matches the predicate or null.
14203 */
14204 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014205 return findViewByPredicateTraversal(predicate, null);
14206 }
14207
14208 /**
14209 * {@hide}
14210 * Look for a child view that matches the specified predicate,
14211 * starting with the specified view and its descendents and then
14212 * recusively searching the ancestors and siblings of that view
14213 * until this view is reached.
14214 *
14215 * This method is useful in cases where the predicate does not match
14216 * a single unique view (perhaps multiple views use the same id)
14217 * and we are trying to find the view that is "closest" in scope to the
14218 * starting view.
14219 *
14220 * @param start The view to start from.
14221 * @param predicate The predicate to evaluate.
14222 * @return The first view that matches the predicate or null.
14223 */
14224 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14225 View childToSkip = null;
14226 for (;;) {
14227 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14228 if (view != null || start == this) {
14229 return view;
14230 }
14231
14232 ViewParent parent = start.getParent();
14233 if (parent == null || !(parent instanceof View)) {
14234 return null;
14235 }
14236
14237 childToSkip = start;
14238 start = (View) parent;
14239 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014240 }
14241
14242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 * Sets the identifier for this view. The identifier does not have to be
14244 * unique in this view's hierarchy. The identifier should be a positive
14245 * number.
14246 *
14247 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014248 * @see #getId()
14249 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014250 *
14251 * @param id a number used to identify the view
14252 *
14253 * @attr ref android.R.styleable#View_id
14254 */
14255 public void setId(int id) {
14256 mID = id;
14257 }
14258
14259 /**
14260 * {@hide}
14261 *
14262 * @param isRoot true if the view belongs to the root namespace, false
14263 * otherwise
14264 */
14265 public void setIsRootNamespace(boolean isRoot) {
14266 if (isRoot) {
14267 mPrivateFlags |= IS_ROOT_NAMESPACE;
14268 } else {
14269 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14270 }
14271 }
14272
14273 /**
14274 * {@hide}
14275 *
14276 * @return true if the view belongs to the root namespace, false otherwise
14277 */
14278 public boolean isRootNamespace() {
14279 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14280 }
14281
14282 /**
14283 * Returns this view's identifier.
14284 *
14285 * @return a positive integer used to identify the view or {@link #NO_ID}
14286 * if the view has no ID
14287 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014288 * @see #setId(int)
14289 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014290 * @attr ref android.R.styleable#View_id
14291 */
14292 @ViewDebug.CapturedViewProperty
14293 public int getId() {
14294 return mID;
14295 }
14296
14297 /**
14298 * Returns this view's tag.
14299 *
14300 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014301 *
14302 * @see #setTag(Object)
14303 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014304 */
14305 @ViewDebug.ExportedProperty
14306 public Object getTag() {
14307 return mTag;
14308 }
14309
14310 /**
14311 * Sets the tag associated with this view. A tag can be used to mark
14312 * a view in its hierarchy and does not have to be unique within the
14313 * hierarchy. Tags can also be used to store data within a view without
14314 * resorting to another data structure.
14315 *
14316 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014317 *
14318 * @see #getTag()
14319 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 */
14321 public void setTag(final Object tag) {
14322 mTag = tag;
14323 }
14324
14325 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014326 * Returns the tag associated with this view and the specified key.
14327 *
14328 * @param key The key identifying the tag
14329 *
14330 * @return the Object stored in this view as a tag
14331 *
14332 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014333 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014334 */
14335 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014336 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014337 return null;
14338 }
14339
14340 /**
14341 * Sets a tag associated with this view and a key. A tag can be used
14342 * to mark a view in its hierarchy and does not have to be unique within
14343 * the hierarchy. Tags can also be used to store data within a view
14344 * without resorting to another data structure.
14345 *
14346 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014347 * application to ensure it is unique (see the <a
14348 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14349 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014350 * the Android framework or not associated with any package will cause
14351 * an {@link IllegalArgumentException} to be thrown.
14352 *
14353 * @param key The key identifying the tag
14354 * @param tag An Object to tag the view with
14355 *
14356 * @throws IllegalArgumentException If they specified key is not valid
14357 *
14358 * @see #setTag(Object)
14359 * @see #getTag(int)
14360 */
14361 public void setTag(int key, final Object tag) {
14362 // If the package id is 0x00 or 0x01, it's either an undefined package
14363 // or a framework id
14364 if ((key >>> 24) < 2) {
14365 throw new IllegalArgumentException("The key must be an application-specific "
14366 + "resource id.");
14367 }
14368
Adam Powell2b2f6d62011-09-23 11:15:39 -070014369 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014370 }
14371
14372 /**
14373 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14374 * framework id.
14375 *
14376 * @hide
14377 */
14378 public void setTagInternal(int key, Object tag) {
14379 if ((key >>> 24) != 0x1) {
14380 throw new IllegalArgumentException("The key must be a framework-specific "
14381 + "resource id.");
14382 }
14383
Adam Powell2b2f6d62011-09-23 11:15:39 -070014384 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014385 }
14386
Adam Powell2b2f6d62011-09-23 11:15:39 -070014387 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014388 if (mKeyedTags == null) {
14389 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014390 }
14391
Adam Powell7db82ac2011-09-22 19:44:04 -070014392 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014393 }
14394
14395 /**
Romain Guy13922e02009-05-12 17:56:14 -070014396 * @param consistency The type of consistency. See ViewDebug for more information.
14397 *
14398 * @hide
14399 */
14400 protected boolean dispatchConsistencyCheck(int consistency) {
14401 return onConsistencyCheck(consistency);
14402 }
14403
14404 /**
14405 * Method that subclasses should implement to check their consistency. The type of
14406 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014407 *
Romain Guy13922e02009-05-12 17:56:14 -070014408 * @param consistency The type of consistency. See ViewDebug for more information.
14409 *
14410 * @throws IllegalStateException if the view is in an inconsistent state.
14411 *
14412 * @hide
14413 */
14414 protected boolean onConsistencyCheck(int consistency) {
14415 boolean result = true;
14416
14417 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14418 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14419
14420 if (checkLayout) {
14421 if (getParent() == null) {
14422 result = false;
14423 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14424 "View " + this + " does not have a parent.");
14425 }
14426
14427 if (mAttachInfo == null) {
14428 result = false;
14429 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14430 "View " + this + " is not attached to a window.");
14431 }
14432 }
14433
14434 if (checkDrawing) {
14435 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14436 // from their draw() method
14437
14438 if ((mPrivateFlags & DRAWN) != DRAWN &&
14439 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14440 result = false;
14441 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14442 "View " + this + " was invalidated but its drawing cache is valid.");
14443 }
14444 }
14445
14446 return result;
14447 }
14448
14449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014450 * Prints information about this view in the log output, with the tag
14451 * {@link #VIEW_LOG_TAG}.
14452 *
14453 * @hide
14454 */
14455 public void debug() {
14456 debug(0);
14457 }
14458
14459 /**
14460 * Prints information about this view in the log output, with the tag
14461 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14462 * indentation defined by the <code>depth</code>.
14463 *
14464 * @param depth the indentation level
14465 *
14466 * @hide
14467 */
14468 protected void debug(int depth) {
14469 String output = debugIndent(depth - 1);
14470
14471 output += "+ " + this;
14472 int id = getId();
14473 if (id != -1) {
14474 output += " (id=" + id + ")";
14475 }
14476 Object tag = getTag();
14477 if (tag != null) {
14478 output += " (tag=" + tag + ")";
14479 }
14480 Log.d(VIEW_LOG_TAG, output);
14481
14482 if ((mPrivateFlags & FOCUSED) != 0) {
14483 output = debugIndent(depth) + " FOCUSED";
14484 Log.d(VIEW_LOG_TAG, output);
14485 }
14486
14487 output = debugIndent(depth);
14488 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14489 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14490 + "} ";
14491 Log.d(VIEW_LOG_TAG, output);
14492
14493 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14494 || mPaddingBottom != 0) {
14495 output = debugIndent(depth);
14496 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14497 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14498 Log.d(VIEW_LOG_TAG, output);
14499 }
14500
14501 output = debugIndent(depth);
14502 output += "mMeasureWidth=" + mMeasuredWidth +
14503 " mMeasureHeight=" + mMeasuredHeight;
14504 Log.d(VIEW_LOG_TAG, output);
14505
14506 output = debugIndent(depth);
14507 if (mLayoutParams == null) {
14508 output += "BAD! no layout params";
14509 } else {
14510 output = mLayoutParams.debug(output);
14511 }
14512 Log.d(VIEW_LOG_TAG, output);
14513
14514 output = debugIndent(depth);
14515 output += "flags={";
14516 output += View.printFlags(mViewFlags);
14517 output += "}";
14518 Log.d(VIEW_LOG_TAG, output);
14519
14520 output = debugIndent(depth);
14521 output += "privateFlags={";
14522 output += View.printPrivateFlags(mPrivateFlags);
14523 output += "}";
14524 Log.d(VIEW_LOG_TAG, output);
14525 }
14526
14527 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014528 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014529 *
14530 * @param depth the indentation level
14531 * @return a String containing (depth * 2 + 3) * 2 white spaces
14532 *
14533 * @hide
14534 */
14535 protected static String debugIndent(int depth) {
14536 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14537 for (int i = 0; i < (depth * 2) + 3; i++) {
14538 spaces.append(' ').append(' ');
14539 }
14540 return spaces.toString();
14541 }
14542
14543 /**
14544 * <p>Return the offset of the widget's text baseline from the widget's top
14545 * boundary. If this widget does not support baseline alignment, this
14546 * method returns -1. </p>
14547 *
14548 * @return the offset of the baseline within the widget's bounds or -1
14549 * if baseline alignment is not supported
14550 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014551 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 public int getBaseline() {
14553 return -1;
14554 }
14555
14556 /**
14557 * Call this when something has changed which has invalidated the
14558 * layout of this view. This will schedule a layout pass of the view
14559 * tree.
14560 */
14561 public void requestLayout() {
14562 if (ViewDebug.TRACE_HIERARCHY) {
14563 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14564 }
14565
14566 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014567 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014568
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014569 if (mLayoutParams != null) {
14570 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14571 }
14572
14573 if (mParent != null && !mParent.isLayoutRequested()) {
14574 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 }
14576 }
14577
14578 /**
14579 * Forces this view to be laid out during the next layout pass.
14580 * This method does not call requestLayout() or forceLayout()
14581 * on the parent.
14582 */
14583 public void forceLayout() {
14584 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014585 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014586 }
14587
14588 /**
14589 * <p>
14590 * This is called to find out how big a view should be. The parent
14591 * supplies constraint information in the width and height parameters.
14592 * </p>
14593 *
14594 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014595 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014596 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014597 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014598 * </p>
14599 *
14600 *
14601 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14602 * parent
14603 * @param heightMeasureSpec Vertical space requirements as imposed by the
14604 * parent
14605 *
14606 * @see #onMeasure(int, int)
14607 */
14608 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14609 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14610 widthMeasureSpec != mOldWidthMeasureSpec ||
14611 heightMeasureSpec != mOldHeightMeasureSpec) {
14612
14613 // first clears the measured dimension flag
14614 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14615
14616 if (ViewDebug.TRACE_HIERARCHY) {
14617 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14618 }
14619
14620 // measure ourselves, this should set the measured dimension flag back
14621 onMeasure(widthMeasureSpec, heightMeasureSpec);
14622
14623 // flag not set, setMeasuredDimension() was not invoked, we raise
14624 // an exception to warn the developer
14625 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14626 throw new IllegalStateException("onMeasure() did not set the"
14627 + " measured dimension by calling"
14628 + " setMeasuredDimension()");
14629 }
14630
14631 mPrivateFlags |= LAYOUT_REQUIRED;
14632 }
14633
14634 mOldWidthMeasureSpec = widthMeasureSpec;
14635 mOldHeightMeasureSpec = heightMeasureSpec;
14636 }
14637
14638 /**
14639 * <p>
14640 * Measure the view and its content to determine the measured width and the
14641 * measured height. This method is invoked by {@link #measure(int, int)} and
14642 * should be overriden by subclasses to provide accurate and efficient
14643 * measurement of their contents.
14644 * </p>
14645 *
14646 * <p>
14647 * <strong>CONTRACT:</strong> When overriding this method, you
14648 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14649 * measured width and height of this view. Failure to do so will trigger an
14650 * <code>IllegalStateException</code>, thrown by
14651 * {@link #measure(int, int)}. Calling the superclass'
14652 * {@link #onMeasure(int, int)} is a valid use.
14653 * </p>
14654 *
14655 * <p>
14656 * The base class implementation of measure defaults to the background size,
14657 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14658 * override {@link #onMeasure(int, int)} to provide better measurements of
14659 * their content.
14660 * </p>
14661 *
14662 * <p>
14663 * If this method is overridden, it is the subclass's responsibility to make
14664 * sure the measured height and width are at least the view's minimum height
14665 * and width ({@link #getSuggestedMinimumHeight()} and
14666 * {@link #getSuggestedMinimumWidth()}).
14667 * </p>
14668 *
14669 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14670 * The requirements are encoded with
14671 * {@link android.view.View.MeasureSpec}.
14672 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14673 * The requirements are encoded with
14674 * {@link android.view.View.MeasureSpec}.
14675 *
14676 * @see #getMeasuredWidth()
14677 * @see #getMeasuredHeight()
14678 * @see #setMeasuredDimension(int, int)
14679 * @see #getSuggestedMinimumHeight()
14680 * @see #getSuggestedMinimumWidth()
14681 * @see android.view.View.MeasureSpec#getMode(int)
14682 * @see android.view.View.MeasureSpec#getSize(int)
14683 */
14684 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14685 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14686 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14687 }
14688
14689 /**
14690 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14691 * measured width and measured height. Failing to do so will trigger an
14692 * exception at measurement time.</p>
14693 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014694 * @param measuredWidth The measured width of this view. May be a complex
14695 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14696 * {@link #MEASURED_STATE_TOO_SMALL}.
14697 * @param measuredHeight The measured height of this view. May be a complex
14698 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14699 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014700 */
14701 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14702 mMeasuredWidth = measuredWidth;
14703 mMeasuredHeight = measuredHeight;
14704
14705 mPrivateFlags |= MEASURED_DIMENSION_SET;
14706 }
14707
14708 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014709 * Merge two states as returned by {@link #getMeasuredState()}.
14710 * @param curState The current state as returned from a view or the result
14711 * of combining multiple views.
14712 * @param newState The new view state to combine.
14713 * @return Returns a new integer reflecting the combination of the two
14714 * states.
14715 */
14716 public static int combineMeasuredStates(int curState, int newState) {
14717 return curState | newState;
14718 }
14719
14720 /**
14721 * Version of {@link #resolveSizeAndState(int, int, int)}
14722 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14723 */
14724 public static int resolveSize(int size, int measureSpec) {
14725 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14726 }
14727
14728 /**
14729 * Utility to reconcile a desired size and state, with constraints imposed
14730 * by a MeasureSpec. Will take the desired size, unless a different size
14731 * is imposed by the constraints. The returned value is a compound integer,
14732 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14733 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14734 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014735 *
14736 * @param size How big the view wants to be
14737 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014738 * @return Size information bit mask as defined by
14739 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014740 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014741 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014742 int result = size;
14743 int specMode = MeasureSpec.getMode(measureSpec);
14744 int specSize = MeasureSpec.getSize(measureSpec);
14745 switch (specMode) {
14746 case MeasureSpec.UNSPECIFIED:
14747 result = size;
14748 break;
14749 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014750 if (specSize < size) {
14751 result = specSize | MEASURED_STATE_TOO_SMALL;
14752 } else {
14753 result = size;
14754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014755 break;
14756 case MeasureSpec.EXACTLY:
14757 result = specSize;
14758 break;
14759 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014760 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014761 }
14762
14763 /**
14764 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014765 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014766 * by the MeasureSpec.
14767 *
14768 * @param size Default size for this view
14769 * @param measureSpec Constraints imposed by the parent
14770 * @return The size this view should be.
14771 */
14772 public static int getDefaultSize(int size, int measureSpec) {
14773 int result = size;
14774 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014775 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014776
14777 switch (specMode) {
14778 case MeasureSpec.UNSPECIFIED:
14779 result = size;
14780 break;
14781 case MeasureSpec.AT_MOST:
14782 case MeasureSpec.EXACTLY:
14783 result = specSize;
14784 break;
14785 }
14786 return result;
14787 }
14788
14789 /**
14790 * Returns the suggested minimum height that the view should use. This
14791 * returns the maximum of the view's minimum height
14792 * and the background's minimum height
14793 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14794 * <p>
14795 * When being used in {@link #onMeasure(int, int)}, the caller should still
14796 * ensure the returned height is within the requirements of the parent.
14797 *
14798 * @return The suggested minimum height of the view.
14799 */
14800 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014801 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014803 }
14804
14805 /**
14806 * Returns the suggested minimum width that the view should use. This
14807 * returns the maximum of the view's minimum width)
14808 * and the background's minimum width
14809 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14810 * <p>
14811 * When being used in {@link #onMeasure(int, int)}, the caller should still
14812 * ensure the returned width is within the requirements of the parent.
14813 *
14814 * @return The suggested minimum width of the view.
14815 */
14816 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014817 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014819
Philip Milne6c8ea062012-04-03 17:38:43 -070014820 /**
14821 * Returns the minimum height of the view.
14822 *
14823 * @return the minimum height the view will try to be.
14824 *
14825 * @see #setMinimumHeight(int)
14826 *
14827 * @attr ref android.R.styleable#View_minHeight
14828 */
14829 public int getMinimumHeight() {
14830 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014831 }
14832
14833 /**
14834 * Sets the minimum height of the view. It is not guaranteed the view will
14835 * be able to achieve this minimum height (for example, if its parent layout
14836 * constrains it with less available height).
14837 *
14838 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014839 *
14840 * @see #getMinimumHeight()
14841 *
14842 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843 */
14844 public void setMinimumHeight(int minHeight) {
14845 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014846 requestLayout();
14847 }
14848
14849 /**
14850 * Returns the minimum width of the view.
14851 *
14852 * @return the minimum width the view will try to be.
14853 *
14854 * @see #setMinimumWidth(int)
14855 *
14856 * @attr ref android.R.styleable#View_minWidth
14857 */
14858 public int getMinimumWidth() {
14859 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 }
14861
14862 /**
14863 * Sets the minimum width of the view. It is not guaranteed the view will
14864 * be able to achieve this minimum width (for example, if its parent layout
14865 * constrains it with less available width).
14866 *
14867 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014868 *
14869 * @see #getMinimumWidth()
14870 *
14871 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014872 */
14873 public void setMinimumWidth(int minWidth) {
14874 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014875 requestLayout();
14876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014877 }
14878
14879 /**
14880 * Get the animation currently associated with this view.
14881 *
14882 * @return The animation that is currently playing or
14883 * scheduled to play for this view.
14884 */
14885 public Animation getAnimation() {
14886 return mCurrentAnimation;
14887 }
14888
14889 /**
14890 * Start the specified animation now.
14891 *
14892 * @param animation the animation to start now
14893 */
14894 public void startAnimation(Animation animation) {
14895 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14896 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014897 invalidateParentCaches();
14898 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014899 }
14900
14901 /**
14902 * Cancels any animations for this view.
14903 */
14904 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014905 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014906 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014908 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014909 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014910 }
14911
14912 /**
14913 * Sets the next animation to play for this view.
14914 * If you want the animation to play immediately, use
14915 * startAnimation. This method provides allows fine-grained
14916 * control over the start time and invalidation, but you
14917 * must make sure that 1) the animation has a start time set, and
14918 * 2) the view will be invalidated when the animation is supposed to
14919 * start.
14920 *
14921 * @param animation The next animation, or null.
14922 */
14923 public void setAnimation(Animation animation) {
14924 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014926 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014927 // If the screen is off assume the animation start time is now instead of
14928 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14929 // would cause the animation to start when the screen turns back on
14930 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14931 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14932 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014934 animation.reset();
14935 }
14936 }
14937
14938 /**
14939 * Invoked by a parent ViewGroup to notify the start of the animation
14940 * currently associated with this view. If you override this method,
14941 * always call super.onAnimationStart();
14942 *
14943 * @see #setAnimation(android.view.animation.Animation)
14944 * @see #getAnimation()
14945 */
14946 protected void onAnimationStart() {
14947 mPrivateFlags |= ANIMATION_STARTED;
14948 }
14949
14950 /**
14951 * Invoked by a parent ViewGroup to notify the end of the animation
14952 * currently associated with this view. If you override this method,
14953 * always call super.onAnimationEnd();
14954 *
14955 * @see #setAnimation(android.view.animation.Animation)
14956 * @see #getAnimation()
14957 */
14958 protected void onAnimationEnd() {
14959 mPrivateFlags &= ~ANIMATION_STARTED;
14960 }
14961
14962 /**
14963 * Invoked if there is a Transform that involves alpha. Subclass that can
14964 * draw themselves with the specified alpha should return true, and then
14965 * respect that alpha when their onDraw() is called. If this returns false
14966 * then the view may be redirected to draw into an offscreen buffer to
14967 * fulfill the request, which will look fine, but may be slower than if the
14968 * subclass handles it internally. The default implementation returns false.
14969 *
14970 * @param alpha The alpha (0..255) to apply to the view's drawing
14971 * @return true if the view can draw with the specified alpha.
14972 */
14973 protected boolean onSetAlpha(int alpha) {
14974 return false;
14975 }
14976
14977 /**
14978 * This is used by the RootView to perform an optimization when
14979 * the view hierarchy contains one or several SurfaceView.
14980 * SurfaceView is always considered transparent, but its children are not,
14981 * therefore all View objects remove themselves from the global transparent
14982 * region (passed as a parameter to this function).
14983 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014984 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014985 *
14986 * @return Returns true if the effective visibility of the view at this
14987 * point is opaque, regardless of the transparent region; returns false
14988 * if it is possible for underlying windows to be seen behind the view.
14989 *
14990 * {@hide}
14991 */
14992 public boolean gatherTransparentRegion(Region region) {
14993 final AttachInfo attachInfo = mAttachInfo;
14994 if (region != null && attachInfo != null) {
14995 final int pflags = mPrivateFlags;
14996 if ((pflags & SKIP_DRAW) == 0) {
14997 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14998 // remove it from the transparent region.
14999 final int[] location = attachInfo.mTransparentLocation;
15000 getLocationInWindow(location);
15001 region.op(location[0], location[1], location[0] + mRight - mLeft,
15002 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015003 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015004 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15005 // exists, so we remove the background drawable's non-transparent
15006 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015007 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015008 }
15009 }
15010 return true;
15011 }
15012
15013 /**
15014 * Play a sound effect for this view.
15015 *
15016 * <p>The framework will play sound effects for some built in actions, such as
15017 * clicking, but you may wish to play these effects in your widget,
15018 * for instance, for internal navigation.
15019 *
15020 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15021 * {@link #isSoundEffectsEnabled()} is true.
15022 *
15023 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15024 */
15025 public void playSoundEffect(int soundConstant) {
15026 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15027 return;
15028 }
15029 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15030 }
15031
15032 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015033 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015034 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015035 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015036 *
15037 * <p>The framework will provide haptic feedback for some built in actions,
15038 * such as long presses, but you may wish to provide feedback for your
15039 * own widget.
15040 *
15041 * <p>The feedback will only be performed if
15042 * {@link #isHapticFeedbackEnabled()} is true.
15043 *
15044 * @param feedbackConstant One of the constants defined in
15045 * {@link HapticFeedbackConstants}
15046 */
15047 public boolean performHapticFeedback(int feedbackConstant) {
15048 return performHapticFeedback(feedbackConstant, 0);
15049 }
15050
15051 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015052 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015053 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015054 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015055 *
15056 * @param feedbackConstant One of the constants defined in
15057 * {@link HapticFeedbackConstants}
15058 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15059 */
15060 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15061 if (mAttachInfo == null) {
15062 return false;
15063 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015064 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015065 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015066 && !isHapticFeedbackEnabled()) {
15067 return false;
15068 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015069 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15070 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015071 }
15072
15073 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015074 * Request that the visibility of the status bar or other screen/window
15075 * decorations be changed.
15076 *
15077 * <p>This method is used to put the over device UI into temporary modes
15078 * where the user's attention is focused more on the application content,
15079 * by dimming or hiding surrounding system affordances. This is typically
15080 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15081 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15082 * to be placed behind the action bar (and with these flags other system
15083 * affordances) so that smooth transitions between hiding and showing them
15084 * can be done.
15085 *
15086 * <p>Two representative examples of the use of system UI visibility is
15087 * implementing a content browsing application (like a magazine reader)
15088 * and a video playing application.
15089 *
15090 * <p>The first code shows a typical implementation of a View in a content
15091 * browsing application. In this implementation, the application goes
15092 * into a content-oriented mode by hiding the status bar and action bar,
15093 * and putting the navigation elements into lights out mode. The user can
15094 * then interact with content while in this mode. Such an application should
15095 * provide an easy way for the user to toggle out of the mode (such as to
15096 * check information in the status bar or access notifications). In the
15097 * implementation here, this is done simply by tapping on the content.
15098 *
15099 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15100 * content}
15101 *
15102 * <p>This second code sample shows a typical implementation of a View
15103 * in a video playing application. In this situation, while the video is
15104 * playing the application would like to go into a complete full-screen mode,
15105 * to use as much of the display as possible for the video. When in this state
15106 * the user can not interact with the application; the system intercepts
15107 * touching on the screen to pop the UI out of full screen mode.
15108 *
15109 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15110 * content}
15111 *
15112 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15113 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15114 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15115 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015116 */
15117 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015118 if (visibility != mSystemUiVisibility) {
15119 mSystemUiVisibility = visibility;
15120 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15121 mParent.recomputeViewAttributes(this);
15122 }
Joe Onorato664644d2011-01-23 17:53:23 -080015123 }
15124 }
15125
15126 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015127 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15128 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15129 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15130 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15131 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015132 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015133 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015134 return mSystemUiVisibility;
15135 }
15136
Scott Mainec6331b2011-05-24 16:55:56 -070015137 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015138 * Returns the current system UI visibility that is currently set for
15139 * the entire window. This is the combination of the
15140 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15141 * views in the window.
15142 */
15143 public int getWindowSystemUiVisibility() {
15144 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15145 }
15146
15147 /**
15148 * Override to find out when the window's requested system UI visibility
15149 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15150 * This is different from the callbacks recieved through
15151 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15152 * in that this is only telling you about the local request of the window,
15153 * not the actual values applied by the system.
15154 */
15155 public void onWindowSystemUiVisibilityChanged(int visible) {
15156 }
15157
15158 /**
15159 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15160 * the view hierarchy.
15161 */
15162 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15163 onWindowSystemUiVisibilityChanged(visible);
15164 }
15165
15166 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015167 * Set a listener to receive callbacks when the visibility of the system bar changes.
15168 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15169 */
Joe Onorato664644d2011-01-23 17:53:23 -080015170 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015171 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015172 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15173 mParent.recomputeViewAttributes(this);
15174 }
15175 }
15176
15177 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015178 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15179 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015180 */
15181 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015182 ListenerInfo li = mListenerInfo;
15183 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15184 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015185 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015186 }
15187 }
15188
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015189 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15190 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15191 if (val != mSystemUiVisibility) {
15192 setSystemUiVisibility(val);
15193 }
15194 }
15195
Joe Onorato664644d2011-01-23 17:53:23 -080015196 /**
Joe Malin32736f02011-01-19 16:14:20 -080015197 * Creates an image that the system displays during the drag and drop
15198 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15199 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15200 * appearance as the given View. The default also positions the center of the drag shadow
15201 * directly under the touch point. If no View is provided (the constructor with no parameters
15202 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15203 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15204 * default is an invisible drag shadow.
15205 * <p>
15206 * You are not required to use the View you provide to the constructor as the basis of the
15207 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15208 * anything you want as the drag shadow.
15209 * </p>
15210 * <p>
15211 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15212 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15213 * size and position of the drag shadow. It uses this data to construct a
15214 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15215 * so that your application can draw the shadow image in the Canvas.
15216 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015217 *
15218 * <div class="special reference">
15219 * <h3>Developer Guides</h3>
15220 * <p>For a guide to implementing drag and drop features, read the
15221 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15222 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015223 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015224 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015225 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015226
15227 /**
Joe Malin32736f02011-01-19 16:14:20 -080015228 * Constructs a shadow image builder based on a View. By default, the resulting drag
15229 * shadow will have the same appearance and dimensions as the View, with the touch point
15230 * over the center of the View.
15231 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015232 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015233 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015234 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015235 }
15236
Christopher Tate17ed60c2011-01-18 12:50:26 -080015237 /**
15238 * Construct a shadow builder object with no associated View. This
15239 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15240 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15241 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015242 * reference to any View object. If they are not overridden, then the result is an
15243 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015244 */
15245 public DragShadowBuilder() {
15246 mView = new WeakReference<View>(null);
15247 }
15248
15249 /**
15250 * Returns the View object that had been passed to the
15251 * {@link #View.DragShadowBuilder(View)}
15252 * constructor. If that View parameter was {@code null} or if the
15253 * {@link #View.DragShadowBuilder()}
15254 * constructor was used to instantiate the builder object, this method will return
15255 * null.
15256 *
15257 * @return The View object associate with this builder object.
15258 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015259 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015260 final public View getView() {
15261 return mView.get();
15262 }
15263
Christopher Tate2c095f32010-10-04 14:13:40 -070015264 /**
Joe Malin32736f02011-01-19 16:14:20 -080015265 * Provides the metrics for the shadow image. These include the dimensions of
15266 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015267 * be centered under the touch location while dragging.
15268 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015269 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015270 * same as the dimensions of the View itself and centers the shadow under
15271 * the touch point.
15272 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015273 *
Joe Malin32736f02011-01-19 16:14:20 -080015274 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15275 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15276 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15277 * image.
15278 *
15279 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15280 * shadow image that should be underneath the touch point during the drag and drop
15281 * operation. Your application must set {@link android.graphics.Point#x} to the
15282 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015283 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015284 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015285 final View view = mView.get();
15286 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015287 shadowSize.set(view.getWidth(), view.getHeight());
15288 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015289 } else {
15290 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15291 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015292 }
15293
15294 /**
Joe Malin32736f02011-01-19 16:14:20 -080015295 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15296 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015297 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015298 *
Joe Malin32736f02011-01-19 16:14:20 -080015299 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015300 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015301 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015302 final View view = mView.get();
15303 if (view != null) {
15304 view.draw(canvas);
15305 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015306 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015307 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015308 }
15309 }
15310
15311 /**
Joe Malin32736f02011-01-19 16:14:20 -080015312 * Starts a drag and drop operation. When your application calls this method, it passes a
15313 * {@link android.view.View.DragShadowBuilder} object to the system. The
15314 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15315 * to get metrics for the drag shadow, and then calls the object's
15316 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15317 * <p>
15318 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15319 * drag events to all the View objects in your application that are currently visible. It does
15320 * this either by calling the View object's drag listener (an implementation of
15321 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15322 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15323 * Both are passed a {@link android.view.DragEvent} object that has a
15324 * {@link android.view.DragEvent#getAction()} value of
15325 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15326 * </p>
15327 * <p>
15328 * Your application can invoke startDrag() on any attached View object. The View object does not
15329 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15330 * be related to the View the user selected for dragging.
15331 * </p>
15332 * @param data A {@link android.content.ClipData} object pointing to the data to be
15333 * transferred by the drag and drop operation.
15334 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15335 * drag shadow.
15336 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15337 * drop operation. This Object is put into every DragEvent object sent by the system during the
15338 * current drag.
15339 * <p>
15340 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15341 * to the target Views. For example, it can contain flags that differentiate between a
15342 * a copy operation and a move operation.
15343 * </p>
15344 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15345 * so the parameter should be set to 0.
15346 * @return {@code true} if the method completes successfully, or
15347 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15348 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015349 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015350 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015351 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015352 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015353 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015354 }
15355 boolean okay = false;
15356
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015357 Point shadowSize = new Point();
15358 Point shadowTouchPoint = new Point();
15359 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015360
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015361 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15362 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15363 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015364 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015365
Chris Tatea32dcf72010-10-14 12:13:50 -070015366 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015367 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15368 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015369 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015370 Surface surface = new Surface();
15371 try {
15372 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015373 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015374 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015375 + " surface=" + surface);
15376 if (token != null) {
15377 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015378 try {
Chris Tate6b391282010-10-14 15:48:59 -070015379 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015380 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015381 } finally {
15382 surface.unlockCanvasAndPost(canvas);
15383 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015384
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015385 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015386
15387 // Cache the local state object for delivery with DragEvents
15388 root.setLocalDragState(myLocalState);
15389
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015390 // repurpose 'shadowSize' for the last touch point
15391 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015392
Christopher Tatea53146c2010-09-07 11:57:52 -070015393 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015394 shadowSize.x, shadowSize.y,
15395 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015396 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015397
15398 // Off and running! Release our local surface instance; the drag
15399 // shadow surface is now managed by the system process.
15400 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015401 }
15402 } catch (Exception e) {
15403 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15404 surface.destroy();
15405 }
15406
15407 return okay;
15408 }
15409
Christopher Tatea53146c2010-09-07 11:57:52 -070015410 /**
Joe Malin32736f02011-01-19 16:14:20 -080015411 * Handles drag events sent by the system following a call to
15412 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15413 *<p>
15414 * When the system calls this method, it passes a
15415 * {@link android.view.DragEvent} object. A call to
15416 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15417 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15418 * operation.
15419 * @param event The {@link android.view.DragEvent} sent by the system.
15420 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15421 * in DragEvent, indicating the type of drag event represented by this object.
15422 * @return {@code true} if the method was successful, otherwise {@code false}.
15423 * <p>
15424 * The method should return {@code true} in response to an action type of
15425 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15426 * operation.
15427 * </p>
15428 * <p>
15429 * The method should also return {@code true} in response to an action type of
15430 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15431 * {@code false} if it didn't.
15432 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015433 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015434 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015435 return false;
15436 }
15437
15438 /**
Joe Malin32736f02011-01-19 16:14:20 -080015439 * Detects if this View is enabled and has a drag event listener.
15440 * If both are true, then it calls the drag event listener with the
15441 * {@link android.view.DragEvent} it received. If the drag event listener returns
15442 * {@code true}, then dispatchDragEvent() returns {@code true}.
15443 * <p>
15444 * For all other cases, the method calls the
15445 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15446 * method and returns its result.
15447 * </p>
15448 * <p>
15449 * This ensures that a drag event is always consumed, even if the View does not have a drag
15450 * event listener. However, if the View has a listener and the listener returns true, then
15451 * onDragEvent() is not called.
15452 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015453 */
15454 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015455 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015456 ListenerInfo li = mListenerInfo;
15457 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15458 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015459 return true;
15460 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015461 return onDragEvent(event);
15462 }
15463
Christopher Tate3d4bf172011-03-28 16:16:46 -070015464 boolean canAcceptDrag() {
15465 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15466 }
15467
Christopher Tatea53146c2010-09-07 11:57:52 -070015468 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015469 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15470 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015471 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015472 */
15473 public void onCloseSystemDialogs(String reason) {
15474 }
Joe Malin32736f02011-01-19 16:14:20 -080015475
Dianne Hackbornffa42482009-09-23 22:20:11 -070015476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015477 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015478 * update a Region being computed for
15479 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015480 * that any non-transparent parts of the Drawable are removed from the
15481 * given transparent region.
15482 *
15483 * @param dr The Drawable whose transparency is to be applied to the region.
15484 * @param region A Region holding the current transparency information,
15485 * where any parts of the region that are set are considered to be
15486 * transparent. On return, this region will be modified to have the
15487 * transparency information reduced by the corresponding parts of the
15488 * Drawable that are not transparent.
15489 * {@hide}
15490 */
15491 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15492 if (DBG) {
15493 Log.i("View", "Getting transparent region for: " + this);
15494 }
15495 final Region r = dr.getTransparentRegion();
15496 final Rect db = dr.getBounds();
15497 final AttachInfo attachInfo = mAttachInfo;
15498 if (r != null && attachInfo != null) {
15499 final int w = getRight()-getLeft();
15500 final int h = getBottom()-getTop();
15501 if (db.left > 0) {
15502 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15503 r.op(0, 0, db.left, h, Region.Op.UNION);
15504 }
15505 if (db.right < w) {
15506 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15507 r.op(db.right, 0, w, h, Region.Op.UNION);
15508 }
15509 if (db.top > 0) {
15510 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15511 r.op(0, 0, w, db.top, Region.Op.UNION);
15512 }
15513 if (db.bottom < h) {
15514 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15515 r.op(0, db.bottom, w, h, Region.Op.UNION);
15516 }
15517 final int[] location = attachInfo.mTransparentLocation;
15518 getLocationInWindow(location);
15519 r.translate(location[0], location[1]);
15520 region.op(r, Region.Op.INTERSECT);
15521 } else {
15522 region.op(db, Region.Op.DIFFERENCE);
15523 }
15524 }
15525
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015526 private void checkForLongClick(int delayOffset) {
15527 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15528 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015529
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015530 if (mPendingCheckForLongPress == null) {
15531 mPendingCheckForLongPress = new CheckForLongPress();
15532 }
15533 mPendingCheckForLongPress.rememberWindowAttachCount();
15534 postDelayed(mPendingCheckForLongPress,
15535 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015537 }
15538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015539 /**
15540 * Inflate a view from an XML resource. This convenience method wraps the {@link
15541 * LayoutInflater} class, which provides a full range of options for view inflation.
15542 *
15543 * @param context The Context object for your activity or application.
15544 * @param resource The resource ID to inflate
15545 * @param root A view group that will be the parent. Used to properly inflate the
15546 * layout_* parameters.
15547 * @see LayoutInflater
15548 */
15549 public static View inflate(Context context, int resource, ViewGroup root) {
15550 LayoutInflater factory = LayoutInflater.from(context);
15551 return factory.inflate(resource, root);
15552 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015554 /**
Adam Powell637d3372010-08-25 14:37:03 -070015555 * Scroll the view with standard behavior for scrolling beyond the normal
15556 * content boundaries. Views that call this method should override
15557 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15558 * results of an over-scroll operation.
15559 *
15560 * Views can use this method to handle any touch or fling-based scrolling.
15561 *
15562 * @param deltaX Change in X in pixels
15563 * @param deltaY Change in Y in pixels
15564 * @param scrollX Current X scroll value in pixels before applying deltaX
15565 * @param scrollY Current Y scroll value in pixels before applying deltaY
15566 * @param scrollRangeX Maximum content scroll range along the X axis
15567 * @param scrollRangeY Maximum content scroll range along the Y axis
15568 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15569 * along the X axis.
15570 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15571 * along the Y axis.
15572 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15573 * @return true if scrolling was clamped to an over-scroll boundary along either
15574 * axis, false otherwise.
15575 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015576 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015577 protected boolean overScrollBy(int deltaX, int deltaY,
15578 int scrollX, int scrollY,
15579 int scrollRangeX, int scrollRangeY,
15580 int maxOverScrollX, int maxOverScrollY,
15581 boolean isTouchEvent) {
15582 final int overScrollMode = mOverScrollMode;
15583 final boolean canScrollHorizontal =
15584 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15585 final boolean canScrollVertical =
15586 computeVerticalScrollRange() > computeVerticalScrollExtent();
15587 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15588 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15589 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15590 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15591
15592 int newScrollX = scrollX + deltaX;
15593 if (!overScrollHorizontal) {
15594 maxOverScrollX = 0;
15595 }
15596
15597 int newScrollY = scrollY + deltaY;
15598 if (!overScrollVertical) {
15599 maxOverScrollY = 0;
15600 }
15601
15602 // Clamp values if at the limits and record
15603 final int left = -maxOverScrollX;
15604 final int right = maxOverScrollX + scrollRangeX;
15605 final int top = -maxOverScrollY;
15606 final int bottom = maxOverScrollY + scrollRangeY;
15607
15608 boolean clampedX = false;
15609 if (newScrollX > right) {
15610 newScrollX = right;
15611 clampedX = true;
15612 } else if (newScrollX < left) {
15613 newScrollX = left;
15614 clampedX = true;
15615 }
15616
15617 boolean clampedY = false;
15618 if (newScrollY > bottom) {
15619 newScrollY = bottom;
15620 clampedY = true;
15621 } else if (newScrollY < top) {
15622 newScrollY = top;
15623 clampedY = true;
15624 }
15625
15626 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15627
15628 return clampedX || clampedY;
15629 }
15630
15631 /**
15632 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15633 * respond to the results of an over-scroll operation.
15634 *
15635 * @param scrollX New X scroll value in pixels
15636 * @param scrollY New Y scroll value in pixels
15637 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15638 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15639 */
15640 protected void onOverScrolled(int scrollX, int scrollY,
15641 boolean clampedX, boolean clampedY) {
15642 // Intentionally empty.
15643 }
15644
15645 /**
15646 * Returns the over-scroll mode for this view. The result will be
15647 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15648 * (allow over-scrolling only if the view content is larger than the container),
15649 * or {@link #OVER_SCROLL_NEVER}.
15650 *
15651 * @return This view's over-scroll mode.
15652 */
15653 public int getOverScrollMode() {
15654 return mOverScrollMode;
15655 }
15656
15657 /**
15658 * Set the over-scroll mode for this view. Valid over-scroll modes are
15659 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15660 * (allow over-scrolling only if the view content is larger than the container),
15661 * or {@link #OVER_SCROLL_NEVER}.
15662 *
15663 * Setting the over-scroll mode of a view will have an effect only if the
15664 * view is capable of scrolling.
15665 *
15666 * @param overScrollMode The new over-scroll mode for this view.
15667 */
15668 public void setOverScrollMode(int overScrollMode) {
15669 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15670 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15671 overScrollMode != OVER_SCROLL_NEVER) {
15672 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15673 }
15674 mOverScrollMode = overScrollMode;
15675 }
15676
15677 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015678 * Gets a scale factor that determines the distance the view should scroll
15679 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15680 * @return The vertical scroll scale factor.
15681 * @hide
15682 */
15683 protected float getVerticalScrollFactor() {
15684 if (mVerticalScrollFactor == 0) {
15685 TypedValue outValue = new TypedValue();
15686 if (!mContext.getTheme().resolveAttribute(
15687 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15688 throw new IllegalStateException(
15689 "Expected theme to define listPreferredItemHeight.");
15690 }
15691 mVerticalScrollFactor = outValue.getDimension(
15692 mContext.getResources().getDisplayMetrics());
15693 }
15694 return mVerticalScrollFactor;
15695 }
15696
15697 /**
15698 * Gets a scale factor that determines the distance the view should scroll
15699 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15700 * @return The horizontal scroll scale factor.
15701 * @hide
15702 */
15703 protected float getHorizontalScrollFactor() {
15704 // TODO: Should use something else.
15705 return getVerticalScrollFactor();
15706 }
15707
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015708 /**
15709 * Return the value specifying the text direction or policy that was set with
15710 * {@link #setTextDirection(int)}.
15711 *
15712 * @return the defined text direction. It can be one of:
15713 *
15714 * {@link #TEXT_DIRECTION_INHERIT},
15715 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15716 * {@link #TEXT_DIRECTION_ANY_RTL},
15717 * {@link #TEXT_DIRECTION_LTR},
15718 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015719 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015720 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015721 @ViewDebug.ExportedProperty(category = "text", mapping = {
15722 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15723 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15724 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15725 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15726 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15727 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15728 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015729 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015730 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015731 }
15732
15733 /**
15734 * Set the text direction.
15735 *
15736 * @param textDirection the direction to set. Should be one of:
15737 *
15738 * {@link #TEXT_DIRECTION_INHERIT},
15739 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15740 * {@link #TEXT_DIRECTION_ANY_RTL},
15741 * {@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 void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015746 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015747 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015748 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015749 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015750 // Set the new text direction
15751 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015752 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015753 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015754 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015755 }
15756 }
15757
15758 /**
15759 * Return the resolved text direction.
15760 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015761 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15762 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15763 * up the parent chain of the view. if there is no parent, then it will return the default
15764 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15765 *
15766 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015767 *
Doug Feltcb3791202011-07-07 11:57:48 -070015768 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15769 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015770 * {@link #TEXT_DIRECTION_LTR},
15771 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015772 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015773 */
15774 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015775 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015776 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015777 resolveTextDirection();
15778 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015779 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015780 }
15781
15782 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015783 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15784 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015785 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015786 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015787 // Reset any previous text direction resolution
15788 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15789
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015790 if (hasRtlSupport()) {
15791 // Set resolved text direction flag depending on text direction flag
15792 final int textDirection = getTextDirection();
15793 switch(textDirection) {
15794 case TEXT_DIRECTION_INHERIT:
15795 if (canResolveTextDirection()) {
15796 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015797
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015798 // Set current resolved direction to the same value as the parent's one
15799 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15800 switch (parentResolvedDirection) {
15801 case TEXT_DIRECTION_FIRST_STRONG:
15802 case TEXT_DIRECTION_ANY_RTL:
15803 case TEXT_DIRECTION_LTR:
15804 case TEXT_DIRECTION_RTL:
15805 case TEXT_DIRECTION_LOCALE:
15806 mPrivateFlags2 |=
15807 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15808 break;
15809 default:
15810 // Default resolved direction is "first strong" heuristic
15811 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15812 }
15813 } else {
15814 // We cannot do the resolution if there is no parent, so use the default one
15815 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015816 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015817 break;
15818 case TEXT_DIRECTION_FIRST_STRONG:
15819 case TEXT_DIRECTION_ANY_RTL:
15820 case TEXT_DIRECTION_LTR:
15821 case TEXT_DIRECTION_RTL:
15822 case TEXT_DIRECTION_LOCALE:
15823 // Resolved direction is the same as text direction
15824 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15825 break;
15826 default:
15827 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015828 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015829 }
15830 } else {
15831 // Default resolved direction is "first strong" heuristic
15832 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015833 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015834
15835 // Set to resolved
15836 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015837 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015838 }
15839
15840 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015841 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015842 * resolution should override this method.
15843 *
15844 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015845 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015846 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015847 }
15848
15849 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015850 * Check if text direction resolution can be done.
15851 *
15852 * @return true if text direction resolution can be done otherwise return false.
15853 */
15854 public boolean canResolveTextDirection() {
15855 switch (getTextDirection()) {
15856 case TEXT_DIRECTION_INHERIT:
15857 return (mParent != null) && (mParent instanceof ViewGroup);
15858 default:
15859 return true;
15860 }
15861 }
15862
15863 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015864 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015865 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015866 * reset is done.
15867 */
15868 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015869 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015870 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015871 }
15872
15873 /**
15874 * Called when text direction is reset. Subclasses that care about text direction reset should
15875 * override this method and do a reset of the text direction of their children. The default
15876 * implementation does nothing.
15877 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015878 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015879 }
15880
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015881 /**
15882 * Return the value specifying the text alignment or policy that was set with
15883 * {@link #setTextAlignment(int)}.
15884 *
15885 * @return the defined text alignment. It can be one of:
15886 *
15887 * {@link #TEXT_ALIGNMENT_INHERIT},
15888 * {@link #TEXT_ALIGNMENT_GRAVITY},
15889 * {@link #TEXT_ALIGNMENT_CENTER},
15890 * {@link #TEXT_ALIGNMENT_TEXT_START},
15891 * {@link #TEXT_ALIGNMENT_TEXT_END},
15892 * {@link #TEXT_ALIGNMENT_VIEW_START},
15893 * {@link #TEXT_ALIGNMENT_VIEW_END}
15894 */
15895 @ViewDebug.ExportedProperty(category = "text", mapping = {
15896 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15897 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15898 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15899 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15900 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15901 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15902 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15903 })
15904 public int getTextAlignment() {
15905 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15906 }
15907
15908 /**
15909 * Set the text alignment.
15910 *
15911 * @param textAlignment The text alignment to set. Should be one of
15912 *
15913 * {@link #TEXT_ALIGNMENT_INHERIT},
15914 * {@link #TEXT_ALIGNMENT_GRAVITY},
15915 * {@link #TEXT_ALIGNMENT_CENTER},
15916 * {@link #TEXT_ALIGNMENT_TEXT_START},
15917 * {@link #TEXT_ALIGNMENT_TEXT_END},
15918 * {@link #TEXT_ALIGNMENT_VIEW_START},
15919 * {@link #TEXT_ALIGNMENT_VIEW_END}
15920 *
15921 * @attr ref android.R.styleable#View_textAlignment
15922 */
15923 public void setTextAlignment(int textAlignment) {
15924 if (textAlignment != getTextAlignment()) {
15925 // Reset the current and resolved text alignment
15926 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15927 resetResolvedTextAlignment();
15928 // Set the new text alignment
15929 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15930 // Refresh
15931 requestLayout();
15932 invalidate(true);
15933 }
15934 }
15935
15936 /**
15937 * Return the resolved text alignment.
15938 *
15939 * The resolved text alignment. This needs resolution if the value is
15940 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15941 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15942 *
15943 * @return the resolved text alignment. Returns one of:
15944 *
15945 * {@link #TEXT_ALIGNMENT_GRAVITY},
15946 * {@link #TEXT_ALIGNMENT_CENTER},
15947 * {@link #TEXT_ALIGNMENT_TEXT_START},
15948 * {@link #TEXT_ALIGNMENT_TEXT_END},
15949 * {@link #TEXT_ALIGNMENT_VIEW_START},
15950 * {@link #TEXT_ALIGNMENT_VIEW_END}
15951 */
15952 @ViewDebug.ExportedProperty(category = "text", mapping = {
15953 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15954 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15955 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15956 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15957 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15958 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15959 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15960 })
15961 public int getResolvedTextAlignment() {
15962 // If text alignment is not resolved, then resolve it
15963 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15964 resolveTextAlignment();
15965 }
15966 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15967 }
15968
15969 /**
15970 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15971 * resolution is done.
15972 */
15973 public void resolveTextAlignment() {
15974 // Reset any previous text alignment resolution
15975 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15976
15977 if (hasRtlSupport()) {
15978 // Set resolved text alignment flag depending on text alignment flag
15979 final int textAlignment = getTextAlignment();
15980 switch (textAlignment) {
15981 case TEXT_ALIGNMENT_INHERIT:
15982 // Check if we can resolve the text alignment
15983 if (canResolveLayoutDirection() && mParent instanceof View) {
15984 View view = (View) mParent;
15985
15986 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15987 switch (parentResolvedTextAlignment) {
15988 case TEXT_ALIGNMENT_GRAVITY:
15989 case TEXT_ALIGNMENT_TEXT_START:
15990 case TEXT_ALIGNMENT_TEXT_END:
15991 case TEXT_ALIGNMENT_CENTER:
15992 case TEXT_ALIGNMENT_VIEW_START:
15993 case TEXT_ALIGNMENT_VIEW_END:
15994 // Resolved text alignment is the same as the parent resolved
15995 // text alignment
15996 mPrivateFlags2 |=
15997 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15998 break;
15999 default:
16000 // Use default resolved text alignment
16001 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16002 }
16003 }
16004 else {
16005 // We cannot do the resolution if there is no parent so use the default
16006 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16007 }
16008 break;
16009 case TEXT_ALIGNMENT_GRAVITY:
16010 case TEXT_ALIGNMENT_TEXT_START:
16011 case TEXT_ALIGNMENT_TEXT_END:
16012 case TEXT_ALIGNMENT_CENTER:
16013 case TEXT_ALIGNMENT_VIEW_START:
16014 case TEXT_ALIGNMENT_VIEW_END:
16015 // Resolved text alignment is the same as text alignment
16016 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16017 break;
16018 default:
16019 // Use default resolved text alignment
16020 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16021 }
16022 } else {
16023 // Use default resolved text alignment
16024 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16025 }
16026
16027 // Set the resolved
16028 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16029 onResolvedTextAlignmentChanged();
16030 }
16031
16032 /**
16033 * Check if text alignment resolution can be done.
16034 *
16035 * @return true if text alignment resolution can be done otherwise return false.
16036 */
16037 public boolean canResolveTextAlignment() {
16038 switch (getTextAlignment()) {
16039 case TEXT_DIRECTION_INHERIT:
16040 return (mParent != null);
16041 default:
16042 return true;
16043 }
16044 }
16045
16046 /**
16047 * Called when text alignment has been resolved. Subclasses that care about text alignment
16048 * resolution should override this method.
16049 *
16050 * The default implementation does nothing.
16051 */
16052 public void onResolvedTextAlignmentChanged() {
16053 }
16054
16055 /**
16056 * Reset resolved text alignment. Text alignment can be resolved with a call to
16057 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16058 * reset is done.
16059 */
16060 public void resetResolvedTextAlignment() {
16061 // Reset any previous text alignment resolution
16062 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16063 onResolvedTextAlignmentReset();
16064 }
16065
16066 /**
16067 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16068 * override this method and do a reset of the text alignment of their children. The default
16069 * implementation does nothing.
16070 */
16071 public void onResolvedTextAlignmentReset() {
16072 }
16073
Chet Haaseb39f0512011-05-24 14:36:40 -070016074 //
16075 // Properties
16076 //
16077 /**
16078 * A Property wrapper around the <code>alpha</code> functionality handled by the
16079 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16080 */
Chet Haased47f1532011-12-16 11:18:52 -080016081 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016082 @Override
16083 public void setValue(View object, float value) {
16084 object.setAlpha(value);
16085 }
16086
16087 @Override
16088 public Float get(View object) {
16089 return object.getAlpha();
16090 }
16091 };
16092
16093 /**
16094 * A Property wrapper around the <code>translationX</code> functionality handled by the
16095 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16096 */
Chet Haased47f1532011-12-16 11:18:52 -080016097 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016098 @Override
16099 public void setValue(View object, float value) {
16100 object.setTranslationX(value);
16101 }
16102
16103 @Override
16104 public Float get(View object) {
16105 return object.getTranslationX();
16106 }
16107 };
16108
16109 /**
16110 * A Property wrapper around the <code>translationY</code> functionality handled by the
16111 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16112 */
Chet Haased47f1532011-12-16 11:18:52 -080016113 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016114 @Override
16115 public void setValue(View object, float value) {
16116 object.setTranslationY(value);
16117 }
16118
16119 @Override
16120 public Float get(View object) {
16121 return object.getTranslationY();
16122 }
16123 };
16124
16125 /**
16126 * A Property wrapper around the <code>x</code> functionality handled by the
16127 * {@link View#setX(float)} and {@link View#getX()} methods.
16128 */
Chet Haased47f1532011-12-16 11:18:52 -080016129 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016130 @Override
16131 public void setValue(View object, float value) {
16132 object.setX(value);
16133 }
16134
16135 @Override
16136 public Float get(View object) {
16137 return object.getX();
16138 }
16139 };
16140
16141 /**
16142 * A Property wrapper around the <code>y</code> functionality handled by the
16143 * {@link View#setY(float)} and {@link View#getY()} methods.
16144 */
Chet Haased47f1532011-12-16 11:18:52 -080016145 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016146 @Override
16147 public void setValue(View object, float value) {
16148 object.setY(value);
16149 }
16150
16151 @Override
16152 public Float get(View object) {
16153 return object.getY();
16154 }
16155 };
16156
16157 /**
16158 * A Property wrapper around the <code>rotation</code> functionality handled by the
16159 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16160 */
Chet Haased47f1532011-12-16 11:18:52 -080016161 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016162 @Override
16163 public void setValue(View object, float value) {
16164 object.setRotation(value);
16165 }
16166
16167 @Override
16168 public Float get(View object) {
16169 return object.getRotation();
16170 }
16171 };
16172
16173 /**
16174 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16175 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16176 */
Chet Haased47f1532011-12-16 11:18:52 -080016177 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016178 @Override
16179 public void setValue(View object, float value) {
16180 object.setRotationX(value);
16181 }
16182
16183 @Override
16184 public Float get(View object) {
16185 return object.getRotationX();
16186 }
16187 };
16188
16189 /**
16190 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16191 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16192 */
Chet Haased47f1532011-12-16 11:18:52 -080016193 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016194 @Override
16195 public void setValue(View object, float value) {
16196 object.setRotationY(value);
16197 }
16198
16199 @Override
16200 public Float get(View object) {
16201 return object.getRotationY();
16202 }
16203 };
16204
16205 /**
16206 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16207 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16208 */
Chet Haased47f1532011-12-16 11:18:52 -080016209 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016210 @Override
16211 public void setValue(View object, float value) {
16212 object.setScaleX(value);
16213 }
16214
16215 @Override
16216 public Float get(View object) {
16217 return object.getScaleX();
16218 }
16219 };
16220
16221 /**
16222 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16223 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16224 */
Chet Haased47f1532011-12-16 11:18:52 -080016225 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016226 @Override
16227 public void setValue(View object, float value) {
16228 object.setScaleY(value);
16229 }
16230
16231 @Override
16232 public Float get(View object) {
16233 return object.getScaleY();
16234 }
16235 };
16236
Jeff Brown33bbfd22011-02-24 20:55:35 -080016237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016238 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16239 * Each MeasureSpec represents a requirement for either the width or the height.
16240 * A MeasureSpec is comprised of a size and a mode. There are three possible
16241 * modes:
16242 * <dl>
16243 * <dt>UNSPECIFIED</dt>
16244 * <dd>
16245 * The parent has not imposed any constraint on the child. It can be whatever size
16246 * it wants.
16247 * </dd>
16248 *
16249 * <dt>EXACTLY</dt>
16250 * <dd>
16251 * The parent has determined an exact size for the child. The child is going to be
16252 * given those bounds regardless of how big it wants to be.
16253 * </dd>
16254 *
16255 * <dt>AT_MOST</dt>
16256 * <dd>
16257 * The child can be as large as it wants up to the specified size.
16258 * </dd>
16259 * </dl>
16260 *
16261 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16262 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16263 */
16264 public static class MeasureSpec {
16265 private static final int MODE_SHIFT = 30;
16266 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16267
16268 /**
16269 * Measure specification mode: The parent has not imposed any constraint
16270 * on the child. It can be whatever size it wants.
16271 */
16272 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16273
16274 /**
16275 * Measure specification mode: The parent has determined an exact size
16276 * for the child. The child is going to be given those bounds regardless
16277 * of how big it wants to be.
16278 */
16279 public static final int EXACTLY = 1 << MODE_SHIFT;
16280
16281 /**
16282 * Measure specification mode: The child can be as large as it wants up
16283 * to the specified size.
16284 */
16285 public static final int AT_MOST = 2 << MODE_SHIFT;
16286
16287 /**
16288 * Creates a measure specification based on the supplied size and mode.
16289 *
16290 * The mode must always be one of the following:
16291 * <ul>
16292 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16293 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16294 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16295 * </ul>
16296 *
16297 * @param size the size of the measure specification
16298 * @param mode the mode of the measure specification
16299 * @return the measure specification based on size and mode
16300 */
16301 public static int makeMeasureSpec(int size, int mode) {
16302 return size + mode;
16303 }
16304
16305 /**
16306 * Extracts the mode from the supplied measure specification.
16307 *
16308 * @param measureSpec the measure specification to extract the mode from
16309 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16310 * {@link android.view.View.MeasureSpec#AT_MOST} or
16311 * {@link android.view.View.MeasureSpec#EXACTLY}
16312 */
16313 public static int getMode(int measureSpec) {
16314 return (measureSpec & MODE_MASK);
16315 }
16316
16317 /**
16318 * Extracts the size from the supplied measure specification.
16319 *
16320 * @param measureSpec the measure specification to extract the size from
16321 * @return the size in pixels defined in the supplied measure specification
16322 */
16323 public static int getSize(int measureSpec) {
16324 return (measureSpec & ~MODE_MASK);
16325 }
16326
16327 /**
16328 * Returns a String representation of the specified measure
16329 * specification.
16330 *
16331 * @param measureSpec the measure specification to convert to a String
16332 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16333 */
16334 public static String toString(int measureSpec) {
16335 int mode = getMode(measureSpec);
16336 int size = getSize(measureSpec);
16337
16338 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16339
16340 if (mode == UNSPECIFIED)
16341 sb.append("UNSPECIFIED ");
16342 else if (mode == EXACTLY)
16343 sb.append("EXACTLY ");
16344 else if (mode == AT_MOST)
16345 sb.append("AT_MOST ");
16346 else
16347 sb.append(mode).append(" ");
16348
16349 sb.append(size);
16350 return sb.toString();
16351 }
16352 }
16353
16354 class CheckForLongPress implements Runnable {
16355
16356 private int mOriginalWindowAttachCount;
16357
16358 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016359 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016360 && mOriginalWindowAttachCount == mWindowAttachCount) {
16361 if (performLongClick()) {
16362 mHasPerformedLongPress = true;
16363 }
16364 }
16365 }
16366
16367 public void rememberWindowAttachCount() {
16368 mOriginalWindowAttachCount = mWindowAttachCount;
16369 }
16370 }
Joe Malin32736f02011-01-19 16:14:20 -080016371
Adam Powelle14579b2009-12-16 18:39:52 -080016372 private final class CheckForTap implements Runnable {
16373 public void run() {
16374 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016375 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016376 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016377 }
16378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016379
Adam Powella35d7682010-03-12 14:48:13 -080016380 private final class PerformClick implements Runnable {
16381 public void run() {
16382 performClick();
16383 }
16384 }
16385
Dianne Hackborn63042d62011-01-26 18:56:29 -080016386 /** @hide */
16387 public void hackTurnOffWindowResizeAnim(boolean off) {
16388 mAttachInfo.mTurnOffWindowResizeAnim = off;
16389 }
Joe Malin32736f02011-01-19 16:14:20 -080016390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016391 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016392 * This method returns a ViewPropertyAnimator object, which can be used to animate
16393 * specific properties on this View.
16394 *
16395 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16396 */
16397 public ViewPropertyAnimator animate() {
16398 if (mAnimator == null) {
16399 mAnimator = new ViewPropertyAnimator(this);
16400 }
16401 return mAnimator;
16402 }
16403
16404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016405 * Interface definition for a callback to be invoked when a key event is
16406 * dispatched to this view. The callback will be invoked before the key
16407 * event is given to the view.
16408 */
16409 public interface OnKeyListener {
16410 /**
16411 * Called when a key is dispatched to a view. This allows listeners to
16412 * get a chance to respond before the target view.
16413 *
16414 * @param v The view the key has been dispatched to.
16415 * @param keyCode The code for the physical key that was pressed
16416 * @param event The KeyEvent object containing full information about
16417 * the event.
16418 * @return True if the listener has consumed the event, false otherwise.
16419 */
16420 boolean onKey(View v, int keyCode, KeyEvent event);
16421 }
16422
16423 /**
16424 * Interface definition for a callback to be invoked when a touch event is
16425 * dispatched to this view. The callback will be invoked before the touch
16426 * event is given to the view.
16427 */
16428 public interface OnTouchListener {
16429 /**
16430 * Called when a touch event is dispatched to a view. This allows listeners to
16431 * get a chance to respond before the target view.
16432 *
16433 * @param v The view the touch event has been dispatched to.
16434 * @param event The MotionEvent object containing full information about
16435 * the event.
16436 * @return True if the listener has consumed the event, false otherwise.
16437 */
16438 boolean onTouch(View v, MotionEvent event);
16439 }
16440
16441 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016442 * Interface definition for a callback to be invoked when a hover event is
16443 * dispatched to this view. The callback will be invoked before the hover
16444 * event is given to the view.
16445 */
16446 public interface OnHoverListener {
16447 /**
16448 * Called when a hover event is dispatched to a view. This allows listeners to
16449 * get a chance to respond before the target view.
16450 *
16451 * @param v The view the hover event has been dispatched to.
16452 * @param event The MotionEvent object containing full information about
16453 * the event.
16454 * @return True if the listener has consumed the event, false otherwise.
16455 */
16456 boolean onHover(View v, MotionEvent event);
16457 }
16458
16459 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016460 * Interface definition for a callback to be invoked when a generic motion event is
16461 * dispatched to this view. The callback will be invoked before the generic motion
16462 * event is given to the view.
16463 */
16464 public interface OnGenericMotionListener {
16465 /**
16466 * Called when a generic motion event is dispatched to a view. This allows listeners to
16467 * get a chance to respond before the target view.
16468 *
16469 * @param v The view the generic motion event has been dispatched to.
16470 * @param event The MotionEvent object containing full information about
16471 * the event.
16472 * @return True if the listener has consumed the event, false otherwise.
16473 */
16474 boolean onGenericMotion(View v, MotionEvent event);
16475 }
16476
16477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016478 * Interface definition for a callback to be invoked when a view has been clicked and held.
16479 */
16480 public interface OnLongClickListener {
16481 /**
16482 * Called when a view has been clicked and held.
16483 *
16484 * @param v The view that was clicked and held.
16485 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016486 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016487 */
16488 boolean onLongClick(View v);
16489 }
16490
16491 /**
Chris Tate32affef2010-10-18 15:29:21 -070016492 * Interface definition for a callback to be invoked when a drag is being dispatched
16493 * to this view. The callback will be invoked before the hosting view's own
16494 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16495 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016496 *
16497 * <div class="special reference">
16498 * <h3>Developer Guides</h3>
16499 * <p>For a guide to implementing drag and drop features, read the
16500 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16501 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016502 */
16503 public interface OnDragListener {
16504 /**
16505 * Called when a drag event is dispatched to a view. This allows listeners
16506 * to get a chance to override base View behavior.
16507 *
Joe Malin32736f02011-01-19 16:14:20 -080016508 * @param v The View that received the drag event.
16509 * @param event The {@link android.view.DragEvent} object for the drag event.
16510 * @return {@code true} if the drag event was handled successfully, or {@code false}
16511 * if the drag event was not handled. Note that {@code false} will trigger the View
16512 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016513 */
16514 boolean onDrag(View v, DragEvent event);
16515 }
16516
16517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016518 * Interface definition for a callback to be invoked when the focus state of
16519 * a view changed.
16520 */
16521 public interface OnFocusChangeListener {
16522 /**
16523 * Called when the focus state of a view has changed.
16524 *
16525 * @param v The view whose state has changed.
16526 * @param hasFocus The new focus state of v.
16527 */
16528 void onFocusChange(View v, boolean hasFocus);
16529 }
16530
16531 /**
16532 * Interface definition for a callback to be invoked when a view is clicked.
16533 */
16534 public interface OnClickListener {
16535 /**
16536 * Called when a view has been clicked.
16537 *
16538 * @param v The view that was clicked.
16539 */
16540 void onClick(View v);
16541 }
16542
16543 /**
16544 * Interface definition for a callback to be invoked when the context menu
16545 * for this view is being built.
16546 */
16547 public interface OnCreateContextMenuListener {
16548 /**
16549 * Called when the context menu for this view is being built. It is not
16550 * safe to hold onto the menu after this method returns.
16551 *
16552 * @param menu The context menu that is being built
16553 * @param v The view for which the context menu is being built
16554 * @param menuInfo Extra information about the item for which the
16555 * context menu should be shown. This information will vary
16556 * depending on the class of v.
16557 */
16558 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16559 }
16560
Joe Onorato664644d2011-01-23 17:53:23 -080016561 /**
16562 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016563 * visibility. This reports <strong>global</strong> changes to the system UI
16564 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016565 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016566 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016567 */
16568 public interface OnSystemUiVisibilityChangeListener {
16569 /**
16570 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016571 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016572 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016573 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016574 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16575 * <strong>global</strong> state of the UI visibility flags, not what your
16576 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016577 */
16578 public void onSystemUiVisibilityChange(int visibility);
16579 }
16580
Adam Powell4afd62b2011-02-18 15:02:18 -080016581 /**
16582 * Interface definition for a callback to be invoked when this view is attached
16583 * or detached from its window.
16584 */
16585 public interface OnAttachStateChangeListener {
16586 /**
16587 * Called when the view is attached to a window.
16588 * @param v The view that was attached
16589 */
16590 public void onViewAttachedToWindow(View v);
16591 /**
16592 * Called when the view is detached from a window.
16593 * @param v The view that was detached
16594 */
16595 public void onViewDetachedFromWindow(View v);
16596 }
16597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016598 private final class UnsetPressedState implements Runnable {
16599 public void run() {
16600 setPressed(false);
16601 }
16602 }
16603
16604 /**
16605 * Base class for derived classes that want to save and restore their own
16606 * state in {@link android.view.View#onSaveInstanceState()}.
16607 */
16608 public static class BaseSavedState extends AbsSavedState {
16609 /**
16610 * Constructor used when reading from a parcel. Reads the state of the superclass.
16611 *
16612 * @param source
16613 */
16614 public BaseSavedState(Parcel source) {
16615 super(source);
16616 }
16617
16618 /**
16619 * Constructor called by derived classes when creating their SavedState objects
16620 *
16621 * @param superState The state of the superclass of this view
16622 */
16623 public BaseSavedState(Parcelable superState) {
16624 super(superState);
16625 }
16626
16627 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16628 new Parcelable.Creator<BaseSavedState>() {
16629 public BaseSavedState createFromParcel(Parcel in) {
16630 return new BaseSavedState(in);
16631 }
16632
16633 public BaseSavedState[] newArray(int size) {
16634 return new BaseSavedState[size];
16635 }
16636 };
16637 }
16638
16639 /**
16640 * A set of information given to a view when it is attached to its parent
16641 * window.
16642 */
16643 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016644 interface Callbacks {
16645 void playSoundEffect(int effectId);
16646 boolean performHapticFeedback(int effectId, boolean always);
16647 }
16648
16649 /**
16650 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16651 * to a Handler. This class contains the target (View) to invalidate and
16652 * the coordinates of the dirty rectangle.
16653 *
16654 * For performance purposes, this class also implements a pool of up to
16655 * POOL_LIMIT objects that get reused. This reduces memory allocations
16656 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016657 */
Romain Guyd928d682009-03-31 17:52:16 -070016658 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016659 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016660 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16661 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016662 public InvalidateInfo newInstance() {
16663 return new InvalidateInfo();
16664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016665
Romain Guyd928d682009-03-31 17:52:16 -070016666 public void onAcquired(InvalidateInfo element) {
16667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016668
Romain Guyd928d682009-03-31 17:52:16 -070016669 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016670 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016671 }
16672 }, POOL_LIMIT)
16673 );
16674
16675 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016676 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016677
16678 View target;
16679
16680 int left;
16681 int top;
16682 int right;
16683 int bottom;
16684
Romain Guyd928d682009-03-31 17:52:16 -070016685 public void setNextPoolable(InvalidateInfo element) {
16686 mNext = element;
16687 }
16688
16689 public InvalidateInfo getNextPoolable() {
16690 return mNext;
16691 }
16692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016693 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016694 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016695 }
16696
16697 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016698 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016699 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016700
16701 public boolean isPooled() {
16702 return mIsPooled;
16703 }
16704
16705 public void setPooled(boolean isPooled) {
16706 mIsPooled = isPooled;
16707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016708 }
16709
16710 final IWindowSession mSession;
16711
16712 final IWindow mWindow;
16713
16714 final IBinder mWindowToken;
16715
16716 final Callbacks mRootCallbacks;
16717
Romain Guy59a12ca2011-06-09 17:48:21 -070016718 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016720 /**
16721 * The top view of the hierarchy.
16722 */
16723 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016725 IBinder mPanelParentWindowToken;
16726 Surface mSurface;
16727
Romain Guyb051e892010-09-28 19:09:36 -070016728 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016729 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016730 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016731
Romain Guy7e4e5612012-03-05 14:37:29 -080016732 boolean mScreenOn;
16733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016734 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016735 * Scale factor used by the compatibility mode
16736 */
16737 float mApplicationScale;
16738
16739 /**
16740 * Indicates whether the application is in compatibility mode
16741 */
16742 boolean mScalingRequired;
16743
16744 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016745 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016746 */
16747 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016748
Dianne Hackborn63042d62011-01-26 18:56:29 -080016749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016750 * Left position of this view's window
16751 */
16752 int mWindowLeft;
16753
16754 /**
16755 * Top position of this view's window
16756 */
16757 int mWindowTop;
16758
16759 /**
Adam Powell26153a32010-11-08 15:22:27 -080016760 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016761 */
Adam Powell26153a32010-11-08 15:22:27 -080016762 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016763
16764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016765 * For windows that are full-screen but using insets to layout inside
16766 * of the screen decorations, these are the current insets for the
16767 * content of the window.
16768 */
16769 final Rect mContentInsets = new Rect();
16770
16771 /**
16772 * For windows that are full-screen but using insets to layout inside
16773 * of the screen decorations, these are the current insets for the
16774 * actual visible parts of the window.
16775 */
16776 final Rect mVisibleInsets = new Rect();
16777
16778 /**
16779 * The internal insets given by this window. This value is
16780 * supplied by the client (through
16781 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16782 * be given to the window manager when changed to be used in laying
16783 * out windows behind it.
16784 */
16785 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16786 = new ViewTreeObserver.InternalInsetsInfo();
16787
16788 /**
16789 * All views in the window's hierarchy that serve as scroll containers,
16790 * used to determine if the window can be resized or must be panned
16791 * to adjust for a soft input area.
16792 */
16793 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16794
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016795 final KeyEvent.DispatcherState mKeyDispatchState
16796 = new KeyEvent.DispatcherState();
16797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016798 /**
16799 * Indicates whether the view's window currently has the focus.
16800 */
16801 boolean mHasWindowFocus;
16802
16803 /**
16804 * The current visibility of the window.
16805 */
16806 int mWindowVisibility;
16807
16808 /**
16809 * Indicates the time at which drawing started to occur.
16810 */
16811 long mDrawingTime;
16812
16813 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016814 * Indicates whether or not ignoring the DIRTY_MASK flags.
16815 */
16816 boolean mIgnoreDirtyState;
16817
16818 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016819 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16820 * to avoid clearing that flag prematurely.
16821 */
16822 boolean mSetIgnoreDirtyState = false;
16823
16824 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016825 * Indicates whether the view's window is currently in touch mode.
16826 */
16827 boolean mInTouchMode;
16828
16829 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016830 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016831 * the next time it performs a traversal
16832 */
16833 boolean mRecomputeGlobalAttributes;
16834
16835 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016836 * Always report new attributes at next traversal.
16837 */
16838 boolean mForceReportNewAttributes;
16839
16840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016841 * Set during a traveral if any views want to keep the screen on.
16842 */
16843 boolean mKeepScreenOn;
16844
16845 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016846 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16847 */
16848 int mSystemUiVisibility;
16849
16850 /**
16851 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16852 * attached.
16853 */
16854 boolean mHasSystemUiListeners;
16855
16856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016857 * Set if the visibility of any views has changed.
16858 */
16859 boolean mViewVisibilityChanged;
16860
16861 /**
16862 * Set to true if a view has been scrolled.
16863 */
16864 boolean mViewScrollChanged;
16865
16866 /**
16867 * Global to the view hierarchy used as a temporary for dealing with
16868 * x/y points in the transparent region computations.
16869 */
16870 final int[] mTransparentLocation = new int[2];
16871
16872 /**
16873 * Global to the view hierarchy used as a temporary for dealing with
16874 * x/y points in the ViewGroup.invalidateChild implementation.
16875 */
16876 final int[] mInvalidateChildLocation = new int[2];
16877
Chet Haasec3aa3612010-06-17 08:50:37 -070016878
16879 /**
16880 * Global to the view hierarchy used as a temporary for dealing with
16881 * x/y location when view is transformed.
16882 */
16883 final float[] mTmpTransformLocation = new float[2];
16884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016885 /**
16886 * The view tree observer used to dispatch global events like
16887 * layout, pre-draw, touch mode change, etc.
16888 */
16889 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16890
16891 /**
16892 * A Canvas used by the view hierarchy to perform bitmap caching.
16893 */
16894 Canvas mCanvas;
16895
16896 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016897 * The view root impl.
16898 */
16899 final ViewRootImpl mViewRootImpl;
16900
16901 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016902 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016903 * handler can be used to pump events in the UI events queue.
16904 */
16905 final Handler mHandler;
16906
16907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016908 * Temporary for use in computing invalidate rectangles while
16909 * calling up the hierarchy.
16910 */
16911 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016912
16913 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016914 * Temporary for use in computing hit areas with transformed views
16915 */
16916 final RectF mTmpTransformRect = new RectF();
16917
16918 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016919 * Temporary list for use in collecting focusable descendents of a view.
16920 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016921 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016924 * The id of the window for accessibility purposes.
16925 */
16926 int mAccessibilityWindowId = View.NO_ID;
16927
16928 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016929 * Whether to ingore not exposed for accessibility Views when
16930 * reporting the view tree to accessibility services.
16931 */
16932 boolean mIncludeNotImportantViews;
16933
16934 /**
16935 * The drawable for highlighting accessibility focus.
16936 */
16937 Drawable mAccessibilityFocusDrawable;
16938
16939 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070016940 * Show where the margins, bounds and layout bounds are for each view.
16941 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070016942 final boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070016943
16944 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016945 * Creates a new set of attachment information with the specified
16946 * events handler and thread.
16947 *
16948 * @param handler the events handler the view must use
16949 */
16950 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016951 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016952 mSession = session;
16953 mWindow = window;
16954 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016955 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016956 mHandler = handler;
16957 mRootCallbacks = effectPlayer;
16958 }
16959 }
16960
16961 /**
16962 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16963 * is supported. This avoids keeping too many unused fields in most
16964 * instances of View.</p>
16965 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016966 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016967
Mike Cleronf116bf82009-09-27 19:14:12 -070016968 /**
16969 * Scrollbars are not visible
16970 */
16971 public static final int OFF = 0;
16972
16973 /**
16974 * Scrollbars are visible
16975 */
16976 public static final int ON = 1;
16977
16978 /**
16979 * Scrollbars are fading away
16980 */
16981 public static final int FADING = 2;
16982
16983 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016985 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016986 public int scrollBarDefaultDelayBeforeFade;
16987 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016988
16989 public int scrollBarSize;
16990 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016991 public float[] interpolatorValues;
16992 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016993
16994 public final Paint paint;
16995 public final Matrix matrix;
16996 public Shader shader;
16997
Mike Cleronf116bf82009-09-27 19:14:12 -070016998 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16999
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017000 private static final float[] OPAQUE = { 255 };
17001 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017002
Mike Cleronf116bf82009-09-27 19:14:12 -070017003 /**
17004 * When fading should start. This time moves into the future every time
17005 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17006 */
17007 public long fadeStartTime;
17008
17009
17010 /**
17011 * The current state of the scrollbars: ON, OFF, or FADING
17012 */
17013 public int state = OFF;
17014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017015 private int mLastColor;
17016
Mike Cleronf116bf82009-09-27 19:14:12 -070017017 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017018 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17019 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017020 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17021 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017022
17023 paint = new Paint();
17024 matrix = new Matrix();
17025 // use use a height of 1, and then wack the matrix each time we
17026 // actually use it.
17027 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017029 paint.setShader(shader);
17030 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017031 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017032 }
Romain Guy8506ab42009-06-11 17:35:47 -070017033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017034 public void setFadeColor(int color) {
17035 if (color != 0 && color != mLastColor) {
17036 mLastColor = color;
17037 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017038
Romain Guye55e1a72009-08-27 10:42:26 -070017039 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17040 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017042 paint.setShader(shader);
17043 // Restore the default transfer mode (src_over)
17044 paint.setXfermode(null);
17045 }
17046 }
Joe Malin32736f02011-01-19 16:14:20 -080017047
Mike Cleronf116bf82009-09-27 19:14:12 -070017048 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017049 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017050 if (now >= fadeStartTime) {
17051
17052 // the animation fades the scrollbars out by changing
17053 // the opacity (alpha) from fully opaque to fully
17054 // transparent
17055 int nextFrame = (int) now;
17056 int framesCount = 0;
17057
17058 Interpolator interpolator = scrollBarInterpolator;
17059
17060 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017061 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017062
17063 // End transparent
17064 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017065 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017066
17067 state = FADING;
17068
17069 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017070 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017071 }
17072 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017073 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017074
Svetoslav Ganova0156172011-06-26 17:55:44 -070017075 /**
17076 * Resuable callback for sending
17077 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17078 */
17079 private class SendViewScrolledAccessibilityEvent implements Runnable {
17080 public volatile boolean mIsPending;
17081
17082 public void run() {
17083 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17084 mIsPending = false;
17085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017086 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017087
17088 /**
17089 * <p>
17090 * This class represents a delegate that can be registered in a {@link View}
17091 * to enhance accessibility support via composition rather via inheritance.
17092 * It is specifically targeted to widget developers that extend basic View
17093 * classes i.e. classes in package android.view, that would like their
17094 * applications to be backwards compatible.
17095 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017096 * <div class="special reference">
17097 * <h3>Developer Guides</h3>
17098 * <p>For more information about making applications accessible, read the
17099 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17100 * developer guide.</p>
17101 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017102 * <p>
17103 * A scenario in which a developer would like to use an accessibility delegate
17104 * is overriding a method introduced in a later API version then the minimal API
17105 * version supported by the application. For example, the method
17106 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17107 * in API version 4 when the accessibility APIs were first introduced. If a
17108 * developer would like his application to run on API version 4 devices (assuming
17109 * all other APIs used by the application are version 4 or lower) and take advantage
17110 * of this method, instead of overriding the method which would break the application's
17111 * backwards compatibility, he can override the corresponding method in this
17112 * delegate and register the delegate in the target View if the API version of
17113 * the system is high enough i.e. the API version is same or higher to the API
17114 * version that introduced
17115 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17116 * </p>
17117 * <p>
17118 * Here is an example implementation:
17119 * </p>
17120 * <code><pre><p>
17121 * if (Build.VERSION.SDK_INT >= 14) {
17122 * // If the API version is equal of higher than the version in
17123 * // which onInitializeAccessibilityNodeInfo was introduced we
17124 * // register a delegate with a customized implementation.
17125 * View view = findViewById(R.id.view_id);
17126 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17127 * public void onInitializeAccessibilityNodeInfo(View host,
17128 * AccessibilityNodeInfo info) {
17129 * // Let the default implementation populate the info.
17130 * super.onInitializeAccessibilityNodeInfo(host, info);
17131 * // Set some other information.
17132 * info.setEnabled(host.isEnabled());
17133 * }
17134 * });
17135 * }
17136 * </code></pre></p>
17137 * <p>
17138 * This delegate contains methods that correspond to the accessibility methods
17139 * in View. If a delegate has been specified the implementation in View hands
17140 * off handling to the corresponding method in this delegate. The default
17141 * implementation the delegate methods behaves exactly as the corresponding
17142 * method in View for the case of no accessibility delegate been set. Hence,
17143 * to customize the behavior of a View method, clients can override only the
17144 * corresponding delegate method without altering the behavior of the rest
17145 * accessibility related methods of the host view.
17146 * </p>
17147 */
17148 public static class AccessibilityDelegate {
17149
17150 /**
17151 * Sends an accessibility event of the given type. If accessibility is not
17152 * enabled this method has no effect.
17153 * <p>
17154 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17155 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17156 * been set.
17157 * </p>
17158 *
17159 * @param host The View hosting the delegate.
17160 * @param eventType The type of the event to send.
17161 *
17162 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17163 */
17164 public void sendAccessibilityEvent(View host, int eventType) {
17165 host.sendAccessibilityEventInternal(eventType);
17166 }
17167
17168 /**
17169 * Sends an accessibility event. This method behaves exactly as
17170 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17171 * empty {@link AccessibilityEvent} and does not perform a check whether
17172 * accessibility is enabled.
17173 * <p>
17174 * The default implementation behaves as
17175 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17176 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17177 * the case of no accessibility delegate been set.
17178 * </p>
17179 *
17180 * @param host The View hosting the delegate.
17181 * @param event The event to send.
17182 *
17183 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17184 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17185 */
17186 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17187 host.sendAccessibilityEventUncheckedInternal(event);
17188 }
17189
17190 /**
17191 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17192 * to its children for adding their text content to the event.
17193 * <p>
17194 * The default implementation behaves as
17195 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17196 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17197 * the case of no accessibility delegate been set.
17198 * </p>
17199 *
17200 * @param host The View hosting the delegate.
17201 * @param event The event.
17202 * @return True if the event population was completed.
17203 *
17204 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17205 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17206 */
17207 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17208 return host.dispatchPopulateAccessibilityEventInternal(event);
17209 }
17210
17211 /**
17212 * Gives a chance to the host View to populate the accessibility event with its
17213 * text content.
17214 * <p>
17215 * The default implementation behaves as
17216 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17217 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17218 * the case of no accessibility delegate been set.
17219 * </p>
17220 *
17221 * @param host The View hosting the delegate.
17222 * @param event The accessibility event which to populate.
17223 *
17224 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17225 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17226 */
17227 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17228 host.onPopulateAccessibilityEventInternal(event);
17229 }
17230
17231 /**
17232 * Initializes an {@link AccessibilityEvent} with information about the
17233 * the host View which is the event source.
17234 * <p>
17235 * The default implementation behaves as
17236 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17237 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17238 * the case of no accessibility delegate been set.
17239 * </p>
17240 *
17241 * @param host The View hosting the delegate.
17242 * @param event The event to initialize.
17243 *
17244 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17245 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17246 */
17247 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17248 host.onInitializeAccessibilityEventInternal(event);
17249 }
17250
17251 /**
17252 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17253 * <p>
17254 * The default implementation behaves as
17255 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17256 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17257 * the case of no accessibility delegate been set.
17258 * </p>
17259 *
17260 * @param host The View hosting the delegate.
17261 * @param info The instance to initialize.
17262 *
17263 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17264 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17265 */
17266 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17267 host.onInitializeAccessibilityNodeInfoInternal(info);
17268 }
17269
17270 /**
17271 * Called when a child of the host View has requested sending an
17272 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17273 * to augment the event.
17274 * <p>
17275 * The default implementation behaves as
17276 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17277 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17278 * the case of no accessibility delegate been set.
17279 * </p>
17280 *
17281 * @param host The View hosting the delegate.
17282 * @param child The child which requests sending the event.
17283 * @param event The event to be sent.
17284 * @return True if the event should be sent
17285 *
17286 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17287 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17288 */
17289 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17290 AccessibilityEvent event) {
17291 return host.onRequestSendAccessibilityEventInternal(child, event);
17292 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017293
17294 /**
17295 * Gets the provider for managing a virtual view hierarchy rooted at this View
17296 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17297 * that explore the window content.
17298 * <p>
17299 * The default implementation behaves as
17300 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17301 * the case of no accessibility delegate been set.
17302 * </p>
17303 *
17304 * @return The provider.
17305 *
17306 * @see AccessibilityNodeProvider
17307 */
17308 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17309 return null;
17310 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017312}