blob: a300639ba5e17533eca5693d278c53e17f2655fc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070050import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070052import android.util.FloatProperty;
53import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070059import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityEventSource;
65import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070066import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070067import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080070import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070071import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.inputmethod.InputConnection;
73import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.ScrollBarDrawable;
75
Romain Guy1ef3fdb2011-09-09 15:30:30 -070076import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070077import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070078
Doug Feltcb3791202011-07-07 11:57:48 -070079import com.android.internal.R;
80import com.android.internal.util.Predicate;
81import com.android.internal.view.menu.MenuBuilder;
82
Christopher Tatea0374192010-10-05 13:06:41 -070083import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070084import java.lang.reflect.InvocationTargetException;
85import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.ArrayList;
87import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070088import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080089import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * <p>
93 * This class represents the basic building block for user interface components. A View
94 * occupies a rectangular area on the screen and is responsible for drawing and
95 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070096 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
98 * are invisible containers that hold other Views (or other ViewGroups) and define
99 * their layout properties.
100 * </p>
101 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700102 * <div class="special reference">
103 * <h3>Developer Guides</h3>
104 * <p>For information about using this class to develop your application's user interface,
105 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * <a name="Using"></a>
109 * <h3>Using Views</h3>
110 * <p>
111 * All of the views in a window are arranged in a single tree. You can add views
112 * either from code or by specifying a tree of views in one or more XML layout
113 * files. There are many specialized subclasses of views that act as controls or
114 * are capable of displaying text, images, or other content.
115 * </p>
116 * <p>
117 * Once you have created a tree of views, there are typically a few types of
118 * common operations you may wish to perform:
119 * <ul>
120 * <li><strong>Set properties:</strong> for example setting the text of a
121 * {@link android.widget.TextView}. The available properties and the methods
122 * that set them will vary among the different subclasses of views. Note that
123 * properties that are known at build time can be set in the XML layout
124 * files.</li>
125 * <li><strong>Set focus:</strong> The framework will handled moving focus in
126 * response to user input. To force focus to a specific view, call
127 * {@link #requestFocus}.</li>
128 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
129 * that will be notified when something interesting happens to the view. For
130 * example, all views will let you set a listener to be notified when the view
131 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700133 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700136 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * </ul>
138 * </p>
139 * <p><em>
140 * Note: The Android framework is responsible for measuring, laying out and
141 * drawing views. You should not call methods that perform these actions on
142 * views yourself unless you are actually implementing a
143 * {@link android.view.ViewGroup}.
144 * </em></p>
145 *
146 * <a name="Lifecycle"></a>
147 * <h3>Implementing a Custom View</h3>
148 *
149 * <p>
150 * To implement a custom view, you will usually begin by providing overrides for
151 * some of the standard methods that the framework calls on all views. You do
152 * not need to override all of these methods. In fact, you can start by just
153 * overriding {@link #onDraw(android.graphics.Canvas)}.
154 * <table border="2" width="85%" align="center" cellpadding="5">
155 * <thead>
156 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
157 * </thead>
158 *
159 * <tbody>
160 * <tr>
161 * <td rowspan="2">Creation</td>
162 * <td>Constructors</td>
163 * <td>There is a form of the constructor that are called when the view
164 * is created from code and a form that is called when the view is
165 * inflated from a layout file. The second form should parse and apply
166 * any attributes defined in the layout file.
167 * </td>
168 * </tr>
169 * <tr>
170 * <td><code>{@link #onFinishInflate()}</code></td>
171 * <td>Called after a view and all of its children has been inflated
172 * from XML.</td>
173 * </tr>
174 *
175 * <tr>
176 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700177 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * <td>Called to determine the size requirements for this view and all
179 * of its children.
180 * </td>
181 * </tr>
182 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700183 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <td>Called when this view should assign a size and position to all
185 * of its children.
186 * </td>
187 * </tr>
188 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700189 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * <td>Called when the size of this view has changed.
191 * </td>
192 * </tr>
193 *
194 * <tr>
195 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700196 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * <td>Called when the view should render its content.
198 * </td>
199 * </tr>
200 *
201 * <tr>
202 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700203 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <td>Called when a new key event occurs.
205 * </td>
206 * </tr>
207 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a key up event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a trackball motion event occurs.
215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a touch screen motion event occurs.
220 * </td>
221 * </tr>
222 *
223 * <tr>
224 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700225 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * <td>Called when the view gains or loses focus.
227 * </td>
228 * </tr>
229 *
230 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700231 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * <td>Called when the window containing the view gains or loses focus.
233 * </td>
234 * </tr>
235 *
236 * <tr>
237 * <td rowspan="3">Attaching</td>
238 * <td><code>{@link #onAttachedToWindow()}</code></td>
239 * <td>Called when the view is attached to a window.
240 * </td>
241 * </tr>
242 *
243 * <tr>
244 * <td><code>{@link #onDetachedFromWindow}</code></td>
245 * <td>Called when the view is detached from its window.
246 * </td>
247 * </tr>
248 *
249 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700250 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * <td>Called when the visibility of the window containing the view
252 * has changed.
253 * </td>
254 * </tr>
255 * </tbody>
256 *
257 * </table>
258 * </p>
259 *
260 * <a name="IDs"></a>
261 * <h3>IDs</h3>
262 * Views may have an integer id associated with them. These ids are typically
263 * assigned in the layout XML files, and are used to find specific views within
264 * the view tree. A common pattern is to:
265 * <ul>
266 * <li>Define a Button in the layout file and assign it a unique ID.
267 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700268 * &lt;Button
269 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * android:layout_width="wrap_content"
271 * android:layout_height="wrap_content"
272 * android:text="@string/my_button_text"/&gt;
273 * </pre></li>
274 * <li>From the onCreate method of an Activity, find the Button
275 * <pre class="prettyprint">
276 * Button myButton = (Button) findViewById(R.id.my_button);
277 * </pre></li>
278 * </ul>
279 * <p>
280 * View IDs need not be unique throughout the tree, but it is good practice to
281 * ensure that they are at least unique within the part of the tree you are
282 * searching.
283 * </p>
284 *
285 * <a name="Position"></a>
286 * <h3>Position</h3>
287 * <p>
288 * The geometry of a view is that of a rectangle. A view has a location,
289 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
290 * two dimensions, expressed as a width and a height. The unit for location
291 * and dimensions is the pixel.
292 * </p>
293 *
294 * <p>
295 * It is possible to retrieve the location of a view by invoking the methods
296 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
297 * coordinate of the rectangle representing the view. The latter returns the
298 * top, or Y, coordinate of the rectangle representing the view. These methods
299 * both return the location of the view relative to its parent. For instance,
300 * when getLeft() returns 20, that means the view is located 20 pixels to the
301 * right of the left edge of its direct parent.
302 * </p>
303 *
304 * <p>
305 * In addition, several convenience methods are offered to avoid unnecessary
306 * computations, namely {@link #getRight()} and {@link #getBottom()}.
307 * These methods return the coordinates of the right and bottom edges of the
308 * rectangle representing the view. For instance, calling {@link #getRight()}
309 * is similar to the following computation: <code>getLeft() + getWidth()</code>
310 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
311 * </p>
312 *
313 * <a name="SizePaddingMargins"></a>
314 * <h3>Size, padding and margins</h3>
315 * <p>
316 * The size of a view is expressed with a width and a height. A view actually
317 * possess two pairs of width and height values.
318 * </p>
319 *
320 * <p>
321 * The first pair is known as <em>measured width</em> and
322 * <em>measured height</em>. These dimensions define how big a view wants to be
323 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
324 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
325 * and {@link #getMeasuredHeight()}.
326 * </p>
327 *
328 * <p>
329 * The second pair is simply known as <em>width</em> and <em>height</em>, or
330 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
331 * dimensions define the actual size of the view on screen, at drawing time and
332 * after layout. These values may, but do not have to, be different from the
333 * measured width and height. The width and height can be obtained by calling
334 * {@link #getWidth()} and {@link #getHeight()}.
335 * </p>
336 *
337 * <p>
338 * To measure its dimensions, a view takes into account its padding. The padding
339 * is expressed in pixels for the left, top, right and bottom parts of the view.
340 * Padding can be used to offset the content of the view by a specific amount of
341 * pixels. For instance, a left padding of 2 will push the view's content by
342 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700343 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
344 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
345 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
346 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * </p>
348 *
349 * <p>
350 * Even though a view can define a padding, it does not provide any support for
351 * margins. However, view groups provide such a support. Refer to
352 * {@link android.view.ViewGroup} and
353 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
354 * </p>
355 *
356 * <a name="Layout"></a>
357 * <h3>Layout</h3>
358 * <p>
359 * Layout is a two pass process: a measure pass and a layout pass. The measuring
360 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
361 * of the view tree. Each view pushes dimension specifications down the tree
362 * during the recursion. At the end of the measure pass, every view has stored
363 * its measurements. The second pass happens in
364 * {@link #layout(int,int,int,int)} and is also top-down. During
365 * this pass each parent is responsible for positioning all of its children
366 * using the sizes computed in the measure pass.
367 * </p>
368 *
369 * <p>
370 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
371 * {@link #getMeasuredHeight()} values must be set, along with those for all of
372 * that view's descendants. A view's measured width and measured height values
373 * must respect the constraints imposed by the view's parents. This guarantees
374 * that at the end of the measure pass, all parents accept all of their
375 * children's measurements. A parent view may call measure() more than once on
376 * its children. For example, the parent may measure each child once with
377 * unspecified dimensions to find out how big they want to be, then call
378 * measure() on them again with actual numbers if the sum of all the children's
379 * unconstrained sizes is too big or too small.
380 * </p>
381 *
382 * <p>
383 * The measure pass uses two classes to communicate dimensions. The
384 * {@link MeasureSpec} class is used by views to tell their parents how they
385 * want to be measured and positioned. The base LayoutParams class just
386 * describes how big the view wants to be for both width and height. For each
387 * dimension, it can specify one of:
388 * <ul>
389 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800390 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * (minus padding)
392 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
393 * enclose its content (plus padding).
394 * </ul>
395 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
396 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
397 * an X and Y value.
398 * </p>
399 *
400 * <p>
401 * MeasureSpecs are used to push requirements down the tree from parent to
402 * child. A MeasureSpec can be in one of three modes:
403 * <ul>
404 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
405 * of a child view. For example, a LinearLayout may call measure() on its child
406 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
407 * tall the child view wants to be given a width of 240 pixels.
408 * <li>EXACTLY: This is used by the parent to impose an exact size on the
409 * child. The child must use this size, and guarantee that all of its
410 * descendants will fit within this size.
411 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
412 * child. The child must gurantee that it and all of its descendants will fit
413 * within this size.
414 * </ul>
415 * </p>
416 *
417 * <p>
418 * To intiate a layout, call {@link #requestLayout}. This method is typically
419 * called by a view on itself when it believes that is can no longer fit within
420 * its current bounds.
421 * </p>
422 *
423 * <a name="Drawing"></a>
424 * <h3>Drawing</h3>
425 * <p>
426 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700427 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * this means that parents will draw before (i.e., behind) their children, with
429 * siblings drawn in the order they appear in the tree.
430 * If you set a background drawable for a View, then the View will draw it for you
431 * before calling back to its <code>onDraw()</code> method.
432 * </p>
433 *
434 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700435 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * </p>
437 *
438 * <p>
439 * To force a view to draw, call {@link #invalidate()}.
440 * </p>
441 *
442 * <a name="EventHandlingThreading"></a>
443 * <h3>Event Handling and Threading</h3>
444 * <p>
445 * The basic cycle of a view is as follows:
446 * <ol>
447 * <li>An event comes in and is dispatched to the appropriate view. The view
448 * handles the event and notifies any listeners.</li>
449 * <li>If in the course of processing the event, the view's bounds may need
450 * to be changed, the view will call {@link #requestLayout()}.</li>
451 * <li>Similarly, if in the course of processing the event the view's appearance
452 * may need to be changed, the view will call {@link #invalidate()}.</li>
453 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
454 * the framework will take care of measuring, laying out, and drawing the tree
455 * as appropriate.</li>
456 * </ol>
457 * </p>
458 *
459 * <p><em>Note: The entire view tree is single threaded. You must always be on
460 * the UI thread when calling any method on any view.</em>
461 * If you are doing work on other threads and want to update the state of a view
462 * from that thread, you should use a {@link Handler}.
463 * </p>
464 *
465 * <a name="FocusHandling"></a>
466 * <h3>Focus Handling</h3>
467 * <p>
468 * The framework will handle routine focus movement in response to user input.
469 * This includes changing the focus as views are removed or hidden, or as new
470 * views become available. Views indicate their willingness to take focus
471 * through the {@link #isFocusable} method. To change whether a view can take
472 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
473 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
474 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
475 * </p>
476 * <p>
477 * Focus movement is based on an algorithm which finds the nearest neighbor in a
478 * given direction. In rare cases, the default algorithm may not match the
479 * intended behavior of the developer. In these situations, you can provide
480 * explicit overrides by using these XML attributes in the layout file:
481 * <pre>
482 * nextFocusDown
483 * nextFocusLeft
484 * nextFocusRight
485 * nextFocusUp
486 * </pre>
487 * </p>
488 *
489 *
490 * <p>
491 * To get a particular view to take focus, call {@link #requestFocus()}.
492 * </p>
493 *
494 * <a name="TouchMode"></a>
495 * <h3>Touch Mode</h3>
496 * <p>
497 * When a user is navigating a user interface via directional keys such as a D-pad, it is
498 * necessary to give focus to actionable items such as buttons so the user can see
499 * what will take input. If the device has touch capabilities, however, and the user
500 * begins interacting with the interface by touching it, it is no longer necessary to
501 * always highlight, or give focus to, a particular view. This motivates a mode
502 * for interaction named 'touch mode'.
503 * </p>
504 * <p>
505 * For a touch capable device, once the user touches the screen, the device
506 * will enter touch mode. From this point onward, only views for which
507 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
508 * Other views that are touchable, like buttons, will not take focus when touched; they will
509 * only fire the on click listeners.
510 * </p>
511 * <p>
512 * Any time a user hits a directional key, such as a D-pad direction, the view device will
513 * exit touch mode, and find a view to take focus, so that the user may resume interacting
514 * with the user interface without touching the screen again.
515 * </p>
516 * <p>
517 * The touch mode state is maintained across {@link android.app.Activity}s. Call
518 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
519 * </p>
520 *
521 * <a name="Scrolling"></a>
522 * <h3>Scrolling</h3>
523 * <p>
524 * The framework provides basic support for views that wish to internally
525 * scroll their content. This includes keeping track of the X and Y scroll
526 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800527 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700528 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 * </p>
530 *
531 * <a name="Tags"></a>
532 * <h3>Tags</h3>
533 * <p>
534 * Unlike IDs, tags are not used to identify views. Tags are essentially an
535 * extra piece of information that can be associated with a view. They are most
536 * often used as a convenience to store data related to views in the views
537 * themselves rather than by putting them in a separate structure.
538 * </p>
539 *
540 * <a name="Animation"></a>
541 * <h3>Animation</h3>
542 * <p>
543 * You can attach an {@link Animation} object to a view using
544 * {@link #setAnimation(Animation)} or
545 * {@link #startAnimation(Animation)}. The animation can alter the scale,
546 * rotation, translation and alpha of a view over time. If the animation is
547 * attached to a view that has children, the animation will affect the entire
548 * subtree rooted by that node. When an animation is started, the framework will
549 * take care of redrawing the appropriate views until the animation completes.
550 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800551 * <p>
552 * Starting with Android 3.0, the preferred way of animating views is to use the
553 * {@link android.animation} package APIs.
554 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 *
Jeff Brown85a31762010-09-01 17:01:00 -0700556 * <a name="Security"></a>
557 * <h3>Security</h3>
558 * <p>
559 * Sometimes it is essential that an application be able to verify that an action
560 * is being performed with the full knowledge and consent of the user, such as
561 * granting a permission request, making a purchase or clicking on an advertisement.
562 * Unfortunately, a malicious application could try to spoof the user into
563 * performing these actions, unaware, by concealing the intended purpose of the view.
564 * As a remedy, the framework offers a touch filtering mechanism that can be used to
565 * improve the security of views that provide access to sensitive functionality.
566 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700567 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800568 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700569 * will discard touches that are received whenever the view's window is obscured by
570 * another visible window. As a result, the view will not receive touches whenever a
571 * toast, dialog or other window appears above the view's window.
572 * </p><p>
573 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700574 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
575 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700576 * </p>
577 *
Romain Guy171c5922011-01-06 10:04:23 -0800578 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700579 * @attr ref android.R.styleable#View_background
580 * @attr ref android.R.styleable#View_clickable
581 * @attr ref android.R.styleable#View_contentDescription
582 * @attr ref android.R.styleable#View_drawingCacheQuality
583 * @attr ref android.R.styleable#View_duplicateParentState
584 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700585 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700586 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700588 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_isScrollContainer
591 * @attr ref android.R.styleable#View_focusable
592 * @attr ref android.R.styleable#View_focusableInTouchMode
593 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
594 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800595 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700596 * @attr ref android.R.styleable#View_longClickable
597 * @attr ref android.R.styleable#View_minHeight
598 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * @attr ref android.R.styleable#View_nextFocusDown
600 * @attr ref android.R.styleable#View_nextFocusLeft
601 * @attr ref android.R.styleable#View_nextFocusRight
602 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700603 * @attr ref android.R.styleable#View_onClick
604 * @attr ref android.R.styleable#View_padding
605 * @attr ref android.R.styleable#View_paddingBottom
606 * @attr ref android.R.styleable#View_paddingLeft
607 * @attr ref android.R.styleable#View_paddingRight
608 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800609 * @attr ref android.R.styleable#View_paddingStart
610 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800612 * @attr ref android.R.styleable#View_rotation
613 * @attr ref android.R.styleable#View_rotationX
614 * @attr ref android.R.styleable#View_rotationY
615 * @attr ref android.R.styleable#View_scaleX
616 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_scrollX
618 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_scrollbarSize
620 * @attr ref android.R.styleable#View_scrollbarStyle
621 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700622 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
623 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
625 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 * @attr ref android.R.styleable#View_scrollbarThumbVertical
627 * @attr ref android.R.styleable#View_scrollbarTrackVertical
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700630 * @attr ref android.R.styleable#View_soundEffectsEnabled
631 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700632 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700641public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
642 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 private static final boolean DBG = false;
644
645 /**
646 * The logging tag used by this class with android.util.Log.
647 */
648 protected static final String VIEW_LOG_TAG = "View";
649
650 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700651 * When set to true, apps will draw debugging information about their layouts.
652 *
653 * @hide
654 */
655 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
656
657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 * Used to mark a View that has no ID.
659 */
660 public static final int NO_ID = -1;
661
662 /**
663 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
664 * calling setFlags.
665 */
666 private static final int NOT_FOCUSABLE = 0x00000000;
667
668 /**
669 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
670 * setFlags.
671 */
672 private static final int FOCUSABLE = 0x00000001;
673
674 /**
675 * Mask for use with setFlags indicating bits used for focus.
676 */
677 private static final int FOCUSABLE_MASK = 0x00000001;
678
679 /**
680 * This view will adjust its padding to fit sytem windows (e.g. status bar)
681 */
682 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
683
684 /**
Scott Main812634c22011-07-27 13:22:35 -0700685 * This view is visible.
686 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int VISIBLE = 0x00000000;
690
691 /**
692 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700693 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
694 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
696 public static final int INVISIBLE = 0x00000004;
697
698 /**
699 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700700 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
701 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 */
703 public static final int GONE = 0x00000008;
704
705 /**
706 * Mask for use with setFlags indicating bits used for visibility.
707 * {@hide}
708 */
709 static final int VISIBILITY_MASK = 0x0000000C;
710
711 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
712
713 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700714 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 * Use with ENABLED_MASK when calling setFlags.
716 * {@hide}
717 */
718 static final int ENABLED = 0x00000000;
719
720 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700721 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 * Use with ENABLED_MASK when calling setFlags.
723 * {@hide}
724 */
725 static final int DISABLED = 0x00000020;
726
727 /**
728 * Mask for use with setFlags indicating bits used for indicating whether
729 * this view is enabled
730 * {@hide}
731 */
732 static final int ENABLED_MASK = 0x00000020;
733
734 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700735 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
736 * called and further optimizations will be performed. It is okay to have
737 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * {@hide}
739 */
740 static final int WILL_NOT_DRAW = 0x00000080;
741
742 /**
743 * Mask for use with setFlags indicating bits used for indicating whether
744 * this view is will draw
745 * {@hide}
746 */
747 static final int DRAW_MASK = 0x00000080;
748
749 /**
750 * <p>This view doesn't show scrollbars.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_NONE = 0x00000000;
754
755 /**
756 * <p>This view shows horizontal scrollbars.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
760
761 /**
762 * <p>This view shows vertical scrollbars.</p>
763 * {@hide}
764 */
765 static final int SCROLLBARS_VERTICAL = 0x00000200;
766
767 /**
768 * <p>Mask for use with setFlags indicating bits used for indicating which
769 * scrollbars are enabled.</p>
770 * {@hide}
771 */
772 static final int SCROLLBARS_MASK = 0x00000300;
773
Jeff Brown85a31762010-09-01 17:01:00 -0700774 /**
775 * Indicates that the view should filter touches when its window is obscured.
776 * Refer to the class comments for more information about this security feature.
777 * {@hide}
778 */
779 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
780
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700781 /**
782 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
783 * that they are optional and should be skipped if the window has
784 * requested system UI flags that ignore those insets for layout.
785 */
786 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
788 /**
789 * <p>This view doesn't show fading edges.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_NONE = 0x00000000;
793
794 /**
795 * <p>This view shows horizontal fading edges.</p>
796 * {@hide}
797 */
798 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
799
800 /**
801 * <p>This view shows vertical fading edges.</p>
802 * {@hide}
803 */
804 static final int FADING_EDGE_VERTICAL = 0x00002000;
805
806 /**
807 * <p>Mask for use with setFlags indicating bits used for indicating which
808 * fading edges are enabled.</p>
809 * {@hide}
810 */
811 static final int FADING_EDGE_MASK = 0x00003000;
812
813 /**
814 * <p>Indicates this view can be clicked. When clickable, a View reacts
815 * to clicks by notifying the OnClickListener.<p>
816 * {@hide}
817 */
818 static final int CLICKABLE = 0x00004000;
819
820 /**
821 * <p>Indicates this view is caching its drawing into a bitmap.</p>
822 * {@hide}
823 */
824 static final int DRAWING_CACHE_ENABLED = 0x00008000;
825
826 /**
827 * <p>Indicates that no icicle should be saved for this view.<p>
828 * {@hide}
829 */
830 static final int SAVE_DISABLED = 0x000010000;
831
832 /**
833 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
834 * property.</p>
835 * {@hide}
836 */
837 static final int SAVE_DISABLED_MASK = 0x000010000;
838
839 /**
840 * <p>Indicates that no drawing cache should ever be created for this view.<p>
841 * {@hide}
842 */
843 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
844
845 /**
846 * <p>Indicates this view can take / keep focus when int touch mode.</p>
847 * {@hide}
848 */
849 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
850
851 /**
852 * <p>Enables low quality mode for the drawing cache.</p>
853 */
854 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
855
856 /**
857 * <p>Enables high quality mode for the drawing cache.</p>
858 */
859 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
860
861 /**
862 * <p>Enables automatic quality mode for the drawing cache.</p>
863 */
864 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
865
866 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
867 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
868 };
869
870 /**
871 * <p>Mask for use with setFlags indicating bits used for the cache
872 * quality property.</p>
873 * {@hide}
874 */
875 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
876
877 /**
878 * <p>
879 * Indicates this view can be long clicked. When long clickable, a View
880 * reacts to long clicks by notifying the OnLongClickListener or showing a
881 * context menu.
882 * </p>
883 * {@hide}
884 */
885 static final int LONG_CLICKABLE = 0x00200000;
886
887 /**
888 * <p>Indicates that this view gets its drawable states from its direct parent
889 * and ignores its original internal states.</p>
890 *
891 * @hide
892 */
893 static final int DUPLICATE_PARENT_STATE = 0x00400000;
894
895 /**
896 * The scrollbar style to display the scrollbars inside the content area,
897 * without increasing the padding. The scrollbars will be overlaid with
898 * translucency on the view's content.
899 */
900 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
901
902 /**
903 * The scrollbar style to display the scrollbars inside the padded area,
904 * increasing the padding of the view. The scrollbars will not overlap the
905 * content area of the view.
906 */
907 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
908
909 /**
910 * The scrollbar style to display the scrollbars at the edge of the view,
911 * without increasing the padding. The scrollbars will be overlaid with
912 * translucency.
913 */
914 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
915
916 /**
917 * The scrollbar style to display the scrollbars at the edge of the view,
918 * increasing the padding of the view. The scrollbars will only overlap the
919 * background, if any.
920 */
921 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
922
923 /**
924 * Mask to check if the scrollbar style is overlay or inset.
925 * {@hide}
926 */
927 static final int SCROLLBARS_INSET_MASK = 0x01000000;
928
929 /**
930 * Mask to check if the scrollbar style is inside or outside.
931 * {@hide}
932 */
933 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
934
935 /**
936 * Mask for scrollbar style.
937 * {@hide}
938 */
939 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
940
941 /**
942 * View flag indicating that the screen should remain on while the
943 * window containing this view is visible to the user. This effectively
944 * takes care of automatically setting the WindowManager's
945 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
946 */
947 public static final int KEEP_SCREEN_ON = 0x04000000;
948
949 /**
950 * View flag indicating whether this view should have sound effects enabled
951 * for events such as clicking and touching.
952 */
953 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
954
955 /**
956 * View flag indicating whether this view should have haptic feedback
957 * enabled for events such as long presses.
958 */
959 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
960
961 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700962 * <p>Indicates that the view hierarchy should stop saving state when
963 * it reaches this view. If state saving is initiated immediately at
964 * the view, it will be allowed.
965 * {@hide}
966 */
967 static final int PARENT_SAVE_DISABLED = 0x20000000;
968
969 /**
970 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
971 * {@hide}
972 */
973 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
974
975 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700976 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
977 * should add all focusable Views regardless if they are focusable in touch mode.
978 */
979 public static final int FOCUSABLES_ALL = 0x00000000;
980
981 /**
982 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
983 * should add only Views focusable in touch mode.
984 */
985 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
986
987 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700988 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
989 * should add only accessibility focusable Views.
990 *
991 * @hide
992 */
993 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
994
995 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700996 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 * item.
998 */
999 public static final int FOCUS_BACKWARD = 0x00000001;
1000
1001 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001002 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 * item.
1004 */
1005 public static final int FOCUS_FORWARD = 0x00000002;
1006
1007 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001008 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 */
1010 public static final int FOCUS_LEFT = 0x00000011;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 */
1015 public static final int FOCUS_UP = 0x00000021;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 */
1020 public static final int FOCUS_RIGHT = 0x00000042;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_DOWN = 0x00000082;
1026
Svetoslav Ganov42138042012-03-20 11:51:39 -07001027 // Accessibility focus directions.
1028
1029 /**
1030 * The accessibility focus which is the current user position when
1031 * interacting with the accessibility framework.
1032 */
1033 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1034
1035 /**
1036 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1037 */
1038 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1039
1040 /**
1041 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1042 */
1043 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1044
1045 /**
1046 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1047 */
1048 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1049
1050 /**
1051 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1052 */
1053 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1054
1055 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001056 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001057 */
1058 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1059
1060 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001061 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001062 */
1063 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1064
1065 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001066 * Bits of {@link #getMeasuredWidthAndState()} and
1067 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1068 */
1069 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1070
1071 /**
1072 * Bits of {@link #getMeasuredWidthAndState()} and
1073 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1074 */
1075 public static final int MEASURED_STATE_MASK = 0xff000000;
1076
1077 /**
1078 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1079 * for functions that combine both width and height into a single int,
1080 * such as {@link #getMeasuredState()} and the childState argument of
1081 * {@link #resolveSizeAndState(int, int, int)}.
1082 */
1083 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1084
1085 /**
1086 * Bit of {@link #getMeasuredWidthAndState()} and
1087 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1088 * is smaller that the space the view would like to have.
1089 */
1090 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1091
1092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 * Base View state sets
1094 */
1095 // Singles
1096 /**
1097 * Indicates the view has no states set. States are used with
1098 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1099 * view depending on its state.
1100 *
1101 * @see android.graphics.drawable.Drawable
1102 * @see #getDrawableState()
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is enabled. States are used with
1107 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1108 * view depending on its state.
1109 *
1110 * @see android.graphics.drawable.Drawable
1111 * @see #getDrawableState()
1112 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001113 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
1115 * Indicates the view is focused. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is selected. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is pressed. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 * @hide
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view's window has focus. States are used with
1144 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1145 * view depending on its state.
1146 *
1147 * @see android.graphics.drawable.Drawable
1148 * @see #getDrawableState()
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Doubles
1152 /**
1153 * Indicates the view is enabled and has the focus.
1154 *
1155 * @see #ENABLED_STATE_SET
1156 * @see #FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled and selected.
1161 *
1162 * @see #ENABLED_STATE_SET
1163 * @see #SELECTED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is enabled and that its window has focus.
1168 *
1169 * @see #ENABLED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 /**
1174 * Indicates the view is focused and selected.
1175 *
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view has the focus and that its window has the focus.
1182 *
1183 * @see #FOCUSED_STATE_SET
1184 * @see #WINDOW_FOCUSED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is selected and that its window has the focus.
1189 *
1190 * @see #SELECTED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 // Triples
1195 /**
1196 * Indicates the view is enabled, focused and selected.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is enabled, focused and its window has the focus.
1205 *
1206 * @see #ENABLED_STATE_SET
1207 * @see #FOCUSED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, selected and its window has the focus.
1213 *
1214 * @see #ENABLED_STATE_SET
1215 * @see #SELECTED_STATE_SET
1216 * @see #WINDOW_FOCUSED_STATE_SET
1217 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001218 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 /**
1220 * Indicates the view is focused, selected and its window has the focus.
1221 *
1222 * @see #FOCUSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is enabled, focused, selected and its window
1229 * has the focus.
1230 *
1231 * @see #ENABLED_STATE_SET
1232 * @see #FOCUSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed and its window has the focus.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed and selected.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #SELECTED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, selected and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed and focused.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #SELECTED_STATE_SET
1279 * @see #FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, focused, selected and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #FOCUSED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed and enabled.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled and its window has the focus.
1300 *
1301 * @see #PRESSED_STATE_SET
1302 * @see #ENABLED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 /**
1307 * Indicates the view is pressed, enabled and selected.
1308 *
1309 * @see #PRESSED_STATE_SET
1310 * @see #ENABLED_STATE_SET
1311 * @see #SELECTED_STATE_SET
1312 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001313 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 /**
1315 * Indicates the view is pressed, enabled, selected and its window has the
1316 * focus.
1317 *
1318 * @see #PRESSED_STATE_SET
1319 * @see #ENABLED_STATE_SET
1320 * @see #SELECTED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and focused.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled, focused and selected.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #SELECTED_STATE_SET
1348 * @see #FOCUSED_STATE_SET
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 /**
1352 * Indicates the view is pressed, enabled, focused, selected and its window
1353 * has the focus.
1354 *
1355 * @see #PRESSED_STATE_SET
1356 * @see #ENABLED_STATE_SET
1357 * @see #SELECTED_STATE_SET
1358 * @see #FOCUSED_STATE_SET
1359 * @see #WINDOW_FOCUSED_STATE_SET
1360 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362
1363 /**
1364 * The order here is very important to {@link #getDrawableState()}
1365 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001366 private static final int[][] VIEW_STATE_SETS;
1367
Romain Guyb051e892010-09-28 19:09:36 -07001368 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1369 static final int VIEW_STATE_SELECTED = 1 << 1;
1370 static final int VIEW_STATE_FOCUSED = 1 << 2;
1371 static final int VIEW_STATE_ENABLED = 1 << 3;
1372 static final int VIEW_STATE_PRESSED = 1 << 4;
1373 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001374 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001375 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001376 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1377 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378
1379 static final int[] VIEW_STATE_IDS = new int[] {
1380 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1381 R.attr.state_selected, VIEW_STATE_SELECTED,
1382 R.attr.state_focused, VIEW_STATE_FOCUSED,
1383 R.attr.state_enabled, VIEW_STATE_ENABLED,
1384 R.attr.state_pressed, VIEW_STATE_PRESSED,
1385 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001386 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001387 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001388 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001389 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 };
1391
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001393 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1394 throw new IllegalStateException(
1395 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1396 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001398 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001401 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001402 orderedIds[i * 2] = viewState;
1403 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001404 }
1405 }
1406 }
Romain Guyb051e892010-09-28 19:09:36 -07001407 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1408 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1409 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 int numBits = Integer.bitCount(i);
1411 int[] set = new int[numBits];
1412 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001413 for (int j = 0; j < orderedIds.length; j += 2) {
1414 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 set[pos++] = orderedIds[j];
1416 }
1417 }
1418 VIEW_STATE_SETS[i] = set;
1419 }
1420
1421 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1422 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1423 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1424 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1426 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1427 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1429 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1431 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1433 | VIEW_STATE_FOCUSED];
1434 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1435 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1437 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1439 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_ENABLED];
1442 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1444 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_ENABLED];
1447 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_ENABLED];
1450 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1453
1454 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1455 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1459 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1464 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_PRESSED];
1470 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1472 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1477 | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1482 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1483 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1484 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1485 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1486 | VIEW_STATE_PRESSED];
1487 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1488 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1489 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1490 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1491 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1492 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1495 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1496 | VIEW_STATE_PRESSED];
1497 }
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001500 * Accessibility event types that are dispatched for text population.
1501 */
1502 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1503 AccessibilityEvent.TYPE_VIEW_CLICKED
1504 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1505 | AccessibilityEvent.TYPE_VIEW_SELECTED
1506 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1507 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1508 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001509 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001510 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001511 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1512 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * Temporary Rect currently for use in setBackground(). This will probably
1516 * be extended in the future to hold our own class with more than just
1517 * a Rect. :)
1518 */
1519 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001520
1521 /**
1522 * Map used to store views' tags.
1523 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001524 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001527 * The next available accessiiblity id.
1528 */
1529 private static int sNextAccessibilityViewId;
1530
1531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 * The animation currently associated with this view.
1533 * @hide
1534 */
1535 protected Animation mCurrentAnimation = null;
1536
1537 /**
1538 * Width as measured during measure pass.
1539 * {@hide}
1540 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001541 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001542 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543
1544 /**
1545 * Height as measured during measure pass.
1546 * {@hide}
1547 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001548 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001549 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
1551 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001552 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1553 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1554 * its display list. This flag, used only when hw accelerated, allows us to clear the
1555 * flag while retaining this information until it's needed (at getDisplayList() time and
1556 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1557 *
1558 * {@hide}
1559 */
1560 boolean mRecreateDisplayList = false;
1561
1562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 * The view's identifier.
1564 * {@hide}
1565 *
1566 * @see #setId(int)
1567 * @see #getId()
1568 */
1569 @ViewDebug.ExportedProperty(resolveId = true)
1570 int mID = NO_ID;
1571
1572 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001573 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001574 */
1575 int mAccessibilityViewId = NO_ID;
1576
1577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 * The view's tag.
1579 * {@hide}
1580 *
1581 * @see #setTag(Object)
1582 * @see #getTag()
1583 */
1584 protected Object mTag;
1585
1586 // for mPrivateFlags:
1587 /** {@hide} */
1588 static final int WANTS_FOCUS = 0x00000001;
1589 /** {@hide} */
1590 static final int FOCUSED = 0x00000002;
1591 /** {@hide} */
1592 static final int SELECTED = 0x00000004;
1593 /** {@hide} */
1594 static final int IS_ROOT_NAMESPACE = 0x00000008;
1595 /** {@hide} */
1596 static final int HAS_BOUNDS = 0x00000010;
1597 /** {@hide} */
1598 static final int DRAWN = 0x00000020;
1599 /**
1600 * When this flag is set, this view is running an animation on behalf of its
1601 * children and should therefore not cancel invalidate requests, even if they
1602 * lie outside of this view's bounds.
1603 *
1604 * {@hide}
1605 */
1606 static final int DRAW_ANIMATION = 0x00000040;
1607 /** {@hide} */
1608 static final int SKIP_DRAW = 0x00000080;
1609 /** {@hide} */
1610 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1611 /** {@hide} */
1612 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1613 /** {@hide} */
1614 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1615 /** {@hide} */
1616 static final int MEASURED_DIMENSION_SET = 0x00000800;
1617 /** {@hide} */
1618 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001619 /** {@hide} */
1620 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621
1622 private static final int PRESSED = 0x00004000;
1623
1624 /** {@hide} */
1625 static final int DRAWING_CACHE_VALID = 0x00008000;
1626 /**
1627 * Flag used to indicate that this view should be drawn once more (and only once
1628 * more) after its animation has completed.
1629 * {@hide}
1630 */
1631 static final int ANIMATION_STARTED = 0x00010000;
1632
1633 private static final int SAVE_STATE_CALLED = 0x00020000;
1634
1635 /**
1636 * Indicates that the View returned true when onSetAlpha() was called and that
1637 * the alpha must be restored.
1638 * {@hide}
1639 */
1640 static final int ALPHA_SET = 0x00040000;
1641
1642 /**
1643 * Set by {@link #setScrollContainer(boolean)}.
1644 */
1645 static final int SCROLL_CONTAINER = 0x00080000;
1646
1647 /**
1648 * Set by {@link #setScrollContainer(boolean)}.
1649 */
1650 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1651
1652 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001653 * View flag indicating whether this view was invalidated (fully or partially.)
1654 *
1655 * @hide
1656 */
1657 static final int DIRTY = 0x00200000;
1658
1659 /**
1660 * View flag indicating whether this view was invalidated by an opaque
1661 * invalidate request.
1662 *
1663 * @hide
1664 */
1665 static final int DIRTY_OPAQUE = 0x00400000;
1666
1667 /**
1668 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1669 *
1670 * @hide
1671 */
1672 static final int DIRTY_MASK = 0x00600000;
1673
1674 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001675 * Indicates whether the background is opaque.
1676 *
1677 * @hide
1678 */
1679 static final int OPAQUE_BACKGROUND = 0x00800000;
1680
1681 /**
1682 * Indicates whether the scrollbars are opaque.
1683 *
1684 * @hide
1685 */
1686 static final int OPAQUE_SCROLLBARS = 0x01000000;
1687
1688 /**
1689 * Indicates whether the view is opaque.
1690 *
1691 * @hide
1692 */
1693 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powelle14579b2009-12-16 18:39:52 -08001695 /**
1696 * Indicates a prepressed state;
1697 * the short time between ACTION_DOWN and recognizing
1698 * a 'real' press. Prepressed is used to recognize quick taps
1699 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001700 *
Adam Powelle14579b2009-12-16 18:39:52 -08001701 * @hide
1702 */
1703 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001704
Adam Powellc9fbaab2010-02-16 17:16:19 -08001705 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001706 * Indicates whether the view is temporarily detached.
1707 *
1708 * @hide
1709 */
1710 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001711
Adam Powell8568c3a2010-04-19 14:26:11 -07001712 /**
1713 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001714 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001715 * @hide
1716 */
1717 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001718
1719 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001720 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1721 * @hide
1722 */
1723 private static final int HOVERED = 0x10000000;
1724
1725 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001726 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1727 * for transform operations
1728 *
1729 * @hide
1730 */
Adam Powellf37df072010-09-17 16:22:49 -07001731 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001732
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001733 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001734 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001735
Chet Haasefd2b0022010-08-06 13:08:56 -07001736 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001737 * Indicates that this view was specifically invalidated, not just dirtied because some
1738 * child view was invalidated. The flag is used to determine when we need to recreate
1739 * a view's display list (as opposed to just returning a reference to its existing
1740 * display list).
1741 *
1742 * @hide
1743 */
1744 static final int INVALIDATED = 0x80000000;
1745
Christopher Tate3d4bf172011-03-28 16:16:46 -07001746 /* Masks for mPrivateFlags2 */
1747
1748 /**
1749 * Indicates that this view has reported that it can accept the current drag's content.
1750 * Cleared when the drag operation concludes.
1751 * @hide
1752 */
1753 static final int DRAG_CAN_ACCEPT = 0x00000001;
1754
1755 /**
1756 * Indicates that this view is currently directly under the drag location in a
1757 * drag-and-drop operation involving content that it can accept. Cleared when
1758 * the drag exits the view, or when the drag operation concludes.
1759 * @hide
1760 */
1761 static final int DRAG_HOVERED = 0x00000002;
1762
Cibu Johny86666632010-02-22 13:01:02 -08001763 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001764 * Horizontal layout direction of this view is from Left to Right.
1765 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001766 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001767 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001768
1769 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001770 * Horizontal layout direction of this view is from Right to Left.
1771 * Use with {@link #setLayoutDirection}.
1772 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001773 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001774
1775 /**
1776 * Horizontal layout direction of this view is inherited from its parent.
1777 * Use with {@link #setLayoutDirection}.
1778 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001779 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780
1781 /**
1782 * Horizontal layout direction of this view is from deduced from the default language
1783 * script for the locale. Use with {@link #setLayoutDirection}.
1784 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001785 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001786
1787 /**
1788 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001789 * @hide
1790 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001791 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1792
1793 /**
1794 * Mask for use with private flags indicating bits used for horizontal layout direction.
1795 * @hide
1796 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001797 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798
1799 /**
1800 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1801 * right-to-left direction.
1802 * @hide
1803 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001804 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805
1806 /**
1807 * Indicates whether the view horizontal layout direction has been resolved.
1808 * @hide
1809 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001810 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001811
1812 /**
1813 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1814 * @hide
1815 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001816 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001817
1818 /*
1819 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1820 * flag value.
1821 * @hide
1822 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001823 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1824 LAYOUT_DIRECTION_LTR,
1825 LAYOUT_DIRECTION_RTL,
1826 LAYOUT_DIRECTION_INHERIT,
1827 LAYOUT_DIRECTION_LOCALE
1828 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001829
1830 /**
1831 * Default horizontal layout direction.
1832 * @hide
1833 */
1834 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001835
Adam Powell539ee872012-02-03 19:00:49 -08001836 /**
1837 * Indicates that the view is tracking some sort of transient state
1838 * that the app should not need to be aware of, but that the framework
1839 * should take special care to preserve.
1840 *
1841 * @hide
1842 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001843 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001844
1845
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001846 /**
1847 * Text direction is inherited thru {@link ViewGroup}
1848 */
1849 public static final int TEXT_DIRECTION_INHERIT = 0;
1850
1851 /**
1852 * Text direction is using "first strong algorithm". The first strong directional character
1853 * determines the paragraph direction. If there is no strong directional character, the
1854 * paragraph direction is the view's resolved layout direction.
1855 */
1856 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1857
1858 /**
1859 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1860 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1861 * If there are neither, the paragraph direction is the view's resolved layout direction.
1862 */
1863 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1864
1865 /**
1866 * Text direction is forced to LTR.
1867 */
1868 public static final int TEXT_DIRECTION_LTR = 3;
1869
1870 /**
1871 * Text direction is forced to RTL.
1872 */
1873 public static final int TEXT_DIRECTION_RTL = 4;
1874
1875 /**
1876 * Text direction is coming from the system Locale.
1877 */
1878 public static final int TEXT_DIRECTION_LOCALE = 5;
1879
1880 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001881 * Default text direction is inherited
1882 */
1883 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1884
1885 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001886 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1887 * @hide
1888 */
1889 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1890
1891 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001892 * Mask for use with private flags indicating bits used for text direction.
1893 * @hide
1894 */
1895 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1896
1897 /**
1898 * Array of text direction flags for mapping attribute "textDirection" to correct
1899 * flag value.
1900 * @hide
1901 */
1902 private static final int[] TEXT_DIRECTION_FLAGS = {
1903 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1904 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1905 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1906 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1907 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1908 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1909 };
1910
1911 /**
1912 * Indicates whether the view text direction has been resolved.
1913 * @hide
1914 */
1915 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1916
1917 /**
1918 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1919 * @hide
1920 */
1921 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1922
1923 /**
1924 * Mask for use with private flags indicating bits used for resolved text direction.
1925 * @hide
1926 */
1927 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1928
1929 /**
1930 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1931 * @hide
1932 */
1933 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1934 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1935
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001936 /*
1937 * Default text alignment. The text alignment of this View is inherited from its parent.
1938 * Use with {@link #setTextAlignment(int)}
1939 */
1940 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1941
1942 /**
1943 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1944 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1945 *
1946 * Use with {@link #setTextAlignment(int)}
1947 */
1948 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1949
1950 /**
1951 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1952 *
1953 * Use with {@link #setTextAlignment(int)}
1954 */
1955 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1956
1957 /**
1958 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1959 *
1960 * Use with {@link #setTextAlignment(int)}
1961 */
1962 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1963
1964 /**
1965 * Center the paragraph, e.g. ALIGN_CENTER.
1966 *
1967 * Use with {@link #setTextAlignment(int)}
1968 */
1969 public static final int TEXT_ALIGNMENT_CENTER = 4;
1970
1971 /**
1972 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1973 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1974 *
1975 * Use with {@link #setTextAlignment(int)}
1976 */
1977 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1978
1979 /**
1980 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1981 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1982 *
1983 * Use with {@link #setTextAlignment(int)}
1984 */
1985 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1986
1987 /**
1988 * Default text alignment is inherited
1989 */
1990 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1991
1992 /**
1993 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1994 * @hide
1995 */
1996 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1997
1998 /**
1999 * Mask for use with private flags indicating bits used for text alignment.
2000 * @hide
2001 */
2002 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2003
2004 /**
2005 * Array of text direction flags for mapping attribute "textAlignment" to correct
2006 * flag value.
2007 * @hide
2008 */
2009 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2010 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2011 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2012 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2013 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2014 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2015 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2016 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2017 };
2018
2019 /**
2020 * Indicates whether the view text alignment has been resolved.
2021 * @hide
2022 */
2023 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2024
2025 /**
2026 * Bit shift to get the resolved text alignment.
2027 * @hide
2028 */
2029 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2030
2031 /**
2032 * Mask for use with private flags indicating bits used for text alignment.
2033 * @hide
2034 */
2035 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2036
2037 /**
2038 * Indicates whether if the view text alignment has been resolved to gravity
2039 */
2040 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2041 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2042
Svetoslav Ganov42138042012-03-20 11:51:39 -07002043 // Accessiblity constants for mPrivateFlags2
2044
2045 /**
2046 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2047 */
2048 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2049
2050 /**
2051 * Automatically determine whether a view is important for accessibility.
2052 */
2053 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2054
2055 /**
2056 * The view is important for accessibility.
2057 */
2058 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2059
2060 /**
2061 * The view is not important for accessibility.
2062 */
2063 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2064
2065 /**
2066 * The default whether the view is important for accessiblity.
2067 */
2068 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2069
2070 /**
2071 * Mask for obtainig the bits which specify how to determine
2072 * whether a view is important for accessibility.
2073 */
2074 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2075 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2076 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2077
2078 /**
2079 * Flag indicating whether a view has accessibility focus.
2080 */
2081 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2082
2083 /**
2084 * Flag indicating whether a view state for accessibility has changed.
2085 */
2086 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002087
Christopher Tate3d4bf172011-03-28 16:16:46 -07002088 /* End of masks for mPrivateFlags2 */
2089
2090 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2091
Chet Haasedaf98e92011-01-10 14:10:36 -08002092 /**
Adam Powell637d3372010-08-25 14:37:03 -07002093 * Always allow a user to over-scroll this view, provided it is a
2094 * view that can scroll.
2095 *
2096 * @see #getOverScrollMode()
2097 * @see #setOverScrollMode(int)
2098 */
2099 public static final int OVER_SCROLL_ALWAYS = 0;
2100
2101 /**
2102 * Allow a user to over-scroll this view only if the content is large
2103 * enough to meaningfully scroll, provided it is a view that can scroll.
2104 *
2105 * @see #getOverScrollMode()
2106 * @see #setOverScrollMode(int)
2107 */
2108 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2109
2110 /**
2111 * Never allow a user to over-scroll this view.
2112 *
2113 * @see #getOverScrollMode()
2114 * @see #setOverScrollMode(int)
2115 */
2116 public static final int OVER_SCROLL_NEVER = 2;
2117
2118 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002119 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2120 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002121 *
Joe Malin32736f02011-01-19 16:14:20 -08002122 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002123 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002124 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002125
2126 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002127 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2128 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002129 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002130 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002131 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002132 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002133 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002134 *
Joe Malin32736f02011-01-19 16:14:20 -08002135 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002136 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002137 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2138
2139 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002140 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2141 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002142 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002143 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002144 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2145 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2146 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002147 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002148 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2149 * window flags) for displaying content using every last pixel on the display.
2150 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002151 * <p>There is a limitation: because navigation controls are so important, the least user
2152 * interaction will cause them to reappear immediately. When this happens, both
2153 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2154 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002155 *
2156 * @see #setSystemUiVisibility(int)
2157 */
2158 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2159
2160 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2162 * into the normal fullscreen mode so that its content can take over the screen
2163 * while still allowing the user to interact with the application.
2164 *
2165 * <p>This has the same visual effect as
2166 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2167 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2168 * meaning that non-critical screen decorations (such as the status bar) will be
2169 * hidden while the user is in the View's window, focusing the experience on
2170 * that content. Unlike the window flag, if you are using ActionBar in
2171 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2172 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2173 * hide the action bar.
2174 *
2175 * <p>This approach to going fullscreen is best used over the window flag when
2176 * it is a transient state -- that is, the application does this at certain
2177 * points in its user interaction where it wants to allow the user to focus
2178 * on content, but not as a continuous state. For situations where the application
2179 * would like to simply stay full screen the entire time (such as a game that
2180 * wants to take over the screen), the
2181 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2182 * is usually a better approach. The state set here will be removed by the system
2183 * in various situations (such as the user moving to another application) like
2184 * the other system UI states.
2185 *
2186 * <p>When using this flag, the application should provide some easy facility
2187 * for the user to go out of it. A common example would be in an e-book
2188 * reader, where tapping on the screen brings back whatever screen and UI
2189 * decorations that had been hidden while the user was immersed in reading
2190 * the book.
2191 *
2192 * @see #setSystemUiVisibility(int)
2193 */
2194 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2195
2196 /**
2197 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2198 * flags, we would like a stable view of the content insets given to
2199 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2200 * will always represent the worst case that the application can expect
2201 * as a continue state. In practice this means with any of system bar,
2202 * nav bar, and status bar shown, but not the space that would be needed
2203 * for an input method.
2204 *
2205 * <p>If you are using ActionBar in
2206 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2207 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2208 * insets it adds to those given to the application.
2209 */
2210 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2211
2212 /**
2213 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2214 * to be layed out as if it has requested
2215 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2216 * allows it to avoid artifacts when switching in and out of that mode, at
2217 * the expense that some of its user interface may be covered by screen
2218 * decorations when they are shown. You can perform layout of your inner
2219 * UI elements to account for the navagation system UI through the
2220 * {@link #fitSystemWindows(Rect)} method.
2221 */
2222 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2223
2224 /**
2225 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2226 * to be layed out as if it has requested
2227 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2228 * allows it to avoid artifacts when switching in and out of that mode, at
2229 * the expense that some of its user interface may be covered by screen
2230 * decorations when they are shown. You can perform layout of your inner
2231 * UI elements to account for non-fullscreen system UI through the
2232 * {@link #fitSystemWindows(Rect)} method.
2233 */
2234 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2235
2236 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002237 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2238 */
2239 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2240
2241 /**
2242 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2243 */
2244 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002245
2246 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002247 * @hide
2248 *
2249 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2250 * out of the public fields to keep the undefined bits out of the developer's way.
2251 *
2252 * Flag to make the status bar not expandable. Unless you also
2253 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2254 */
2255 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2256
2257 /**
2258 * @hide
2259 *
2260 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2261 * out of the public fields to keep the undefined bits out of the developer's way.
2262 *
2263 * Flag to hide notification icons and scrolling ticker text.
2264 */
2265 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2266
2267 /**
2268 * @hide
2269 *
2270 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2271 * out of the public fields to keep the undefined bits out of the developer's way.
2272 *
2273 * Flag to disable incoming notification alerts. This will not block
2274 * icons, but it will block sound, vibrating and other visual or aural notifications.
2275 */
2276 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2277
2278 /**
2279 * @hide
2280 *
2281 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2282 * out of the public fields to keep the undefined bits out of the developer's way.
2283 *
2284 * Flag to hide only the scrolling ticker. Note that
2285 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2286 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2287 */
2288 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2289
2290 /**
2291 * @hide
2292 *
2293 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2294 * out of the public fields to keep the undefined bits out of the developer's way.
2295 *
2296 * Flag to hide the center system info area.
2297 */
2298 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2299
2300 /**
2301 * @hide
2302 *
2303 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2304 * out of the public fields to keep the undefined bits out of the developer's way.
2305 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002306 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002307 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2308 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002309 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002310
2311 /**
2312 * @hide
2313 *
2314 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2315 * out of the public fields to keep the undefined bits out of the developer's way.
2316 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002317 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002318 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2319 */
2320 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2321
2322 /**
2323 * @hide
2324 *
2325 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2326 * out of the public fields to keep the undefined bits out of the developer's way.
2327 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002328 * Flag to hide only the clock. You might use this if your activity has
2329 * its own clock making the status bar's clock redundant.
2330 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002331 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2332
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002333 /**
2334 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002335 *
2336 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2337 * out of the public fields to keep the undefined bits out of the developer's way.
2338 *
2339 * Flag to hide only the recent apps button. Don't use this
2340 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2341 */
2342 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2343
2344 /**
2345 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002346 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002347 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002348
2349 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002350 * These are the system UI flags that can be cleared by events outside
2351 * of an application. Currently this is just the ability to tap on the
2352 * screen while hiding the navigation bar to have it return.
2353 * @hide
2354 */
2355 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002356 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2357 | SYSTEM_UI_FLAG_FULLSCREEN;
2358
2359 /**
2360 * Flags that can impact the layout in relation to system UI.
2361 */
2362 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2363 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2364 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002365
2366 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002367 * Find views that render the specified text.
2368 *
2369 * @see #findViewsWithText(ArrayList, CharSequence, int)
2370 */
2371 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2372
2373 /**
2374 * Find find views that contain the specified content description.
2375 *
2376 * @see #findViewsWithText(ArrayList, CharSequence, int)
2377 */
2378 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2379
2380 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002381 * Find views that contain {@link AccessibilityNodeProvider}. Such
2382 * a View is a root of virtual view hierarchy and may contain the searched
2383 * text. If this flag is set Views with providers are automatically
2384 * added and it is a responsibility of the client to call the APIs of
2385 * the provider to determine whether the virtual tree rooted at this View
2386 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2387 * represeting the virtual views with this text.
2388 *
2389 * @see #findViewsWithText(ArrayList, CharSequence, int)
2390 *
2391 * @hide
2392 */
2393 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2394
2395 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002396 * Indicates that the screen has changed state and is now off.
2397 *
2398 * @see #onScreenStateChanged(int)
2399 */
2400 public static final int SCREEN_STATE_OFF = 0x0;
2401
2402 /**
2403 * Indicates that the screen has changed state and is now on.
2404 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002405 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002406 */
2407 public static final int SCREEN_STATE_ON = 0x1;
2408
2409 /**
Adam Powell637d3372010-08-25 14:37:03 -07002410 * Controls the over-scroll mode for this view.
2411 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2412 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2413 * and {@link #OVER_SCROLL_NEVER}.
2414 */
2415 private int mOverScrollMode;
2416
2417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 * The parent this view is attached to.
2419 * {@hide}
2420 *
2421 * @see #getParent()
2422 */
2423 protected ViewParent mParent;
2424
2425 /**
2426 * {@hide}
2427 */
2428 AttachInfo mAttachInfo;
2429
2430 /**
2431 * {@hide}
2432 */
Romain Guy809a7f62009-05-14 15:44:42 -07002433 @ViewDebug.ExportedProperty(flagMapping = {
2434 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2435 name = "FORCE_LAYOUT"),
2436 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2437 name = "LAYOUT_REQUIRED"),
2438 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002439 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002440 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2441 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2442 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2443 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2444 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002446 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447
2448 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002449 * This view's request for the visibility of the status bar.
2450 * @hide
2451 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002452 @ViewDebug.ExportedProperty(flagMapping = {
2453 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2454 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2455 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2456 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2457 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2458 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2459 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2460 equals = SYSTEM_UI_FLAG_VISIBLE,
2461 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2462 })
Joe Onorato664644d2011-01-23 17:53:23 -08002463 int mSystemUiVisibility;
2464
2465 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002466 * Reference count for transient state.
2467 * @see #setHasTransientState(boolean)
2468 */
2469 int mTransientStateCount = 0;
2470
2471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 * Count of how many windows this view has been attached to.
2473 */
2474 int mWindowAttachCount;
2475
2476 /**
2477 * The layout parameters associated with this view and used by the parent
2478 * {@link android.view.ViewGroup} to determine how this view should be
2479 * laid out.
2480 * {@hide}
2481 */
2482 protected ViewGroup.LayoutParams mLayoutParams;
2483
2484 /**
2485 * The view flags hold various views states.
2486 * {@hide}
2487 */
2488 @ViewDebug.ExportedProperty
2489 int mViewFlags;
2490
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002491 static class TransformationInfo {
2492 /**
2493 * The transform matrix for the View. This transform is calculated internally
2494 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2495 * is used by default. Do *not* use this variable directly; instead call
2496 * getMatrix(), which will automatically recalculate the matrix if necessary
2497 * to get the correct matrix based on the latest rotation and scale properties.
2498 */
2499 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002500
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002501 /**
2502 * The transform matrix for the View. This transform is calculated internally
2503 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2504 * is used by default. Do *not* use this variable directly; instead call
2505 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2506 * to get the correct matrix based on the latest rotation and scale properties.
2507 */
2508 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002509
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002510 /**
2511 * An internal variable that tracks whether we need to recalculate the
2512 * transform matrix, based on whether the rotation or scaleX/Y properties
2513 * have changed since the matrix was last calculated.
2514 */
2515 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002516
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002517 /**
2518 * An internal variable that tracks whether we need to recalculate the
2519 * transform matrix, based on whether the rotation or scaleX/Y properties
2520 * have changed since the matrix was last calculated.
2521 */
2522 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002523
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002524 /**
2525 * A variable that tracks whether we need to recalculate the
2526 * transform matrix, based on whether the rotation or scaleX/Y properties
2527 * have changed since the matrix was last calculated. This variable
2528 * is only valid after a call to updateMatrix() or to a function that
2529 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2530 */
2531 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002532
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002533 /**
2534 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2535 */
2536 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002537
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002538 /**
2539 * This matrix is used when computing the matrix for 3D rotations.
2540 */
2541 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002542
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002543 /**
2544 * These prev values are used to recalculate a centered pivot point when necessary. The
2545 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2546 * set), so thes values are only used then as well.
2547 */
2548 private int mPrevWidth = -1;
2549 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002550
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002551 /**
2552 * The degrees rotation around the vertical axis through the pivot point.
2553 */
2554 @ViewDebug.ExportedProperty
2555 float mRotationY = 0f;
2556
2557 /**
2558 * The degrees rotation around the horizontal axis through the pivot point.
2559 */
2560 @ViewDebug.ExportedProperty
2561 float mRotationX = 0f;
2562
2563 /**
2564 * The degrees rotation around the pivot point.
2565 */
2566 @ViewDebug.ExportedProperty
2567 float mRotation = 0f;
2568
2569 /**
2570 * The amount of translation of the object away from its left property (post-layout).
2571 */
2572 @ViewDebug.ExportedProperty
2573 float mTranslationX = 0f;
2574
2575 /**
2576 * The amount of translation of the object away from its top property (post-layout).
2577 */
2578 @ViewDebug.ExportedProperty
2579 float mTranslationY = 0f;
2580
2581 /**
2582 * The amount of scale in the x direction around the pivot point. A
2583 * value of 1 means no scaling is applied.
2584 */
2585 @ViewDebug.ExportedProperty
2586 float mScaleX = 1f;
2587
2588 /**
2589 * The amount of scale in the y direction around the pivot point. A
2590 * value of 1 means no scaling is applied.
2591 */
2592 @ViewDebug.ExportedProperty
2593 float mScaleY = 1f;
2594
2595 /**
Chet Haasea33de552012-02-03 16:28:24 -08002596 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002597 */
2598 @ViewDebug.ExportedProperty
2599 float mPivotX = 0f;
2600
2601 /**
Chet Haasea33de552012-02-03 16:28:24 -08002602 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002603 */
2604 @ViewDebug.ExportedProperty
2605 float mPivotY = 0f;
2606
2607 /**
2608 * The opacity of the View. This is a value from 0 to 1, where 0 means
2609 * completely transparent and 1 means completely opaque.
2610 */
2611 @ViewDebug.ExportedProperty
2612 float mAlpha = 1f;
2613 }
2614
2615 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002616
Joe Malin32736f02011-01-19 16:14:20 -08002617 private boolean mLastIsOpaque;
2618
Chet Haasefd2b0022010-08-06 13:08:56 -07002619 /**
2620 * Convenience value to check for float values that are close enough to zero to be considered
2621 * zero.
2622 */
Romain Guy2542d192010-08-18 11:47:12 -07002623 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002624
2625 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 * The distance in pixels from the left edge of this view's parent
2627 * to the left edge of this view.
2628 * {@hide}
2629 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002630 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 protected int mLeft;
2632 /**
2633 * The distance in pixels from the left edge of this view's parent
2634 * to the right edge of this view.
2635 * {@hide}
2636 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002637 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 protected int mRight;
2639 /**
2640 * The distance in pixels from the top edge of this view's parent
2641 * to the top edge of this view.
2642 * {@hide}
2643 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002644 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 protected int mTop;
2646 /**
2647 * The distance in pixels from the top edge of this view's parent
2648 * to the bottom edge of this view.
2649 * {@hide}
2650 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002651 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 protected int mBottom;
2653
2654 /**
2655 * The offset, in pixels, by which the content of this view is scrolled
2656 * horizontally.
2657 * {@hide}
2658 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002659 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 protected int mScrollX;
2661 /**
2662 * The offset, in pixels, by which the content of this view is scrolled
2663 * vertically.
2664 * {@hide}
2665 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002666 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 protected int mScrollY;
2668
2669 /**
2670 * The left padding in pixels, that is the distance in pixels between the
2671 * left edge of this view and the left edge of its content.
2672 * {@hide}
2673 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002674 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 protected int mPaddingLeft;
2676 /**
2677 * The right padding in pixels, that is the distance in pixels between the
2678 * right edge of this view and the right edge of its content.
2679 * {@hide}
2680 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002681 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 protected int mPaddingRight;
2683 /**
2684 * The top padding in pixels, that is the distance in pixels between the
2685 * top edge of this view and the top edge of its content.
2686 * {@hide}
2687 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002688 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 protected int mPaddingTop;
2690 /**
2691 * The bottom padding in pixels, that is the distance in pixels between the
2692 * bottom edge of this view and the bottom edge of its content.
2693 * {@hide}
2694 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002695 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 protected int mPaddingBottom;
2697
2698 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002699 * The layout insets in pixels, that is the distance in pixels between the
2700 * visible edges of this view its bounds.
2701 */
2702 private Insets mLayoutInsets;
2703
2704 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002705 * Briefly describes the view and is primarily used for accessibility support.
2706 */
2707 private CharSequence mContentDescription;
2708
2709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002711 *
2712 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002714 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002715 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716
2717 /**
2718 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002719 *
2720 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002722 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002723 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002725 /**
Adam Powell20232d02010-12-08 21:08:53 -08002726 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002727 *
2728 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002729 */
2730 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002731 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002732
2733 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002734 * Cache if the user padding is relative.
2735 *
2736 */
2737 @ViewDebug.ExportedProperty(category = "padding")
2738 boolean mUserPaddingRelative;
2739
2740 /**
2741 * Cache the paddingStart set by the user to append to the scrollbar's size.
2742 *
2743 */
2744 @ViewDebug.ExportedProperty(category = "padding")
2745 int mUserPaddingStart;
2746
2747 /**
2748 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2749 *
2750 */
2751 @ViewDebug.ExportedProperty(category = "padding")
2752 int mUserPaddingEnd;
2753
2754 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002755 * @hide
2756 */
2757 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2758 /**
2759 * @hide
2760 */
2761 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762
Philip Milne6c8ea062012-04-03 17:38:43 -07002763 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
2765 private int mBackgroundResource;
2766 private boolean mBackgroundSizeChanged;
2767
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002768 static class ListenerInfo {
2769 /**
2770 * Listener used to dispatch focus change events.
2771 * This field should be made private, so it is hidden from the SDK.
2772 * {@hide}
2773 */
2774 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002776 /**
2777 * Listeners for layout change events.
2778 */
2779 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002780
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002781 /**
2782 * Listeners for attach events.
2783 */
2784 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002785
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002786 /**
2787 * Listener used to dispatch click events.
2788 * This field should be made private, so it is hidden from the SDK.
2789 * {@hide}
2790 */
2791 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002793 /**
2794 * Listener used to dispatch long click events.
2795 * This field should be made private, so it is hidden from the SDK.
2796 * {@hide}
2797 */
2798 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002800 /**
2801 * Listener used to build the context menu.
2802 * This field should be made private, so it is hidden from the SDK.
2803 * {@hide}
2804 */
2805 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002807 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002809 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002812
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002813 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002814
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002815 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002816
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002817 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2818 }
2819
2820 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 /**
2823 * The application environment this view lives in.
2824 * This field should be made private, so it is hidden from the SDK.
2825 * {@hide}
2826 */
2827 protected Context mContext;
2828
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002829 private final Resources mResources;
2830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 private ScrollabilityCache mScrollCache;
2832
2833 private int[] mDrawableState = null;
2834
Romain Guy0211a0a2011-02-14 16:34:59 -08002835 /**
2836 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002837 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002838 * @hide
2839 */
2840 public boolean mCachingFailed;
2841
Romain Guy02890fd2010-08-06 17:58:44 -07002842 private Bitmap mDrawingCache;
2843 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002844 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002845 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846
2847 /**
2848 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2849 * the user may specify which view to go to next.
2850 */
2851 private int mNextFocusLeftId = View.NO_ID;
2852
2853 /**
2854 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2855 * the user may specify which view to go to next.
2856 */
2857 private int mNextFocusRightId = View.NO_ID;
2858
2859 /**
2860 * When this view has focus and the next focus is {@link #FOCUS_UP},
2861 * the user may specify which view to go to next.
2862 */
2863 private int mNextFocusUpId = View.NO_ID;
2864
2865 /**
2866 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2867 * the user may specify which view to go to next.
2868 */
2869 private int mNextFocusDownId = View.NO_ID;
2870
Jeff Brown4e6319b2010-12-13 10:36:51 -08002871 /**
2872 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2873 * the user may specify which view to go to next.
2874 */
2875 int mNextFocusForwardId = View.NO_ID;
2876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002878 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002879 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002880 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 private UnsetPressedState mUnsetPressedState;
2883
2884 /**
2885 * Whether the long press's action has been invoked. The tap's action is invoked on the
2886 * up event while a long press is invoked as soon as the long press duration is reached, so
2887 * a long press could be performed before the tap is checked, in which case the tap's action
2888 * should not be invoked.
2889 */
2890 private boolean mHasPerformedLongPress;
2891
2892 /**
2893 * The minimum height of the view. We'll try our best to have the height
2894 * of this view to at least this amount.
2895 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002896 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 private int mMinHeight;
2898
2899 /**
2900 * The minimum width of the view. We'll try our best to have the width
2901 * of this view to at least this amount.
2902 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002903 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 private int mMinWidth;
2905
2906 /**
2907 * The delegate to handle touch events that are physically in this view
2908 * but should be handled by another view.
2909 */
2910 private TouchDelegate mTouchDelegate = null;
2911
2912 /**
2913 * Solid color to use as a background when creating the drawing cache. Enables
2914 * the cache to use 16 bit bitmaps instead of 32 bit.
2915 */
2916 private int mDrawingCacheBackgroundColor = 0;
2917
2918 /**
2919 * Special tree observer used when mAttachInfo is null.
2920 */
2921 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002922
Adam Powelle14579b2009-12-16 18:39:52 -08002923 /**
2924 * Cache the touch slop from the context that created the view.
2925 */
2926 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002929 * Object that handles automatic animation of view properties.
2930 */
2931 private ViewPropertyAnimator mAnimator = null;
2932
2933 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002934 * Flag indicating that a drag can cross window boundaries. When
2935 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2936 * with this flag set, all visible applications will be able to participate
2937 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002938 *
2939 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002940 */
2941 public static final int DRAG_FLAG_GLOBAL = 1;
2942
2943 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002944 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2945 */
2946 private float mVerticalScrollFactor;
2947
2948 /**
Adam Powell20232d02010-12-08 21:08:53 -08002949 * Position of the vertical scroll bar.
2950 */
2951 private int mVerticalScrollbarPosition;
2952
2953 /**
2954 * Position the scroll bar at the default position as determined by the system.
2955 */
2956 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2957
2958 /**
2959 * Position the scroll bar along the left edge.
2960 */
2961 public static final int SCROLLBAR_POSITION_LEFT = 1;
2962
2963 /**
2964 * Position the scroll bar along the right edge.
2965 */
2966 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2967
2968 /**
Romain Guy171c5922011-01-06 10:04:23 -08002969 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002970 *
2971 * @see #getLayerType()
2972 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002973 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002974 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002975 */
2976 public static final int LAYER_TYPE_NONE = 0;
2977
2978 /**
2979 * <p>Indicates that the view has a software layer. A software layer is backed
2980 * by a bitmap and causes the view to be rendered using Android's software
2981 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002982 *
Romain Guy171c5922011-01-06 10:04:23 -08002983 * <p>Software layers have various usages:</p>
2984 * <p>When the application is not using hardware acceleration, a software layer
2985 * is useful to apply a specific color filter and/or blending mode and/or
2986 * translucency to a view and all its children.</p>
2987 * <p>When the application is using hardware acceleration, a software layer
2988 * is useful to render drawing primitives not supported by the hardware
2989 * accelerated pipeline. It can also be used to cache a complex view tree
2990 * into a texture and reduce the complexity of drawing operations. For instance,
2991 * when animating a complex view tree with a translation, a software layer can
2992 * be used to render the view tree only once.</p>
2993 * <p>Software layers should be avoided when the affected view tree updates
2994 * often. Every update will require to re-render the software layer, which can
2995 * potentially be slow (particularly when hardware acceleration is turned on
2996 * since the layer will have to be uploaded into a hardware texture after every
2997 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002998 *
2999 * @see #getLayerType()
3000 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003001 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003002 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003003 */
3004 public static final int LAYER_TYPE_SOFTWARE = 1;
3005
3006 /**
3007 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3008 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3009 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3010 * rendering pipeline, but only if hardware acceleration is turned on for the
3011 * view hierarchy. When hardware acceleration is turned off, hardware layers
3012 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003013 *
Romain Guy171c5922011-01-06 10:04:23 -08003014 * <p>A hardware layer is useful to apply a specific color filter and/or
3015 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003016 * <p>A hardware layer can be used to cache a complex view tree into a
3017 * texture and reduce the complexity of drawing operations. For instance,
3018 * when animating a complex view tree with a translation, a hardware layer can
3019 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003020 * <p>A hardware layer can also be used to increase the rendering quality when
3021 * rotation transformations are applied on a view. It can also be used to
3022 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003023 *
3024 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003025 * @see #setLayerType(int, android.graphics.Paint)
3026 * @see #LAYER_TYPE_NONE
3027 * @see #LAYER_TYPE_SOFTWARE
3028 */
3029 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003030
Romain Guy3aaff3a2011-01-12 14:18:47 -08003031 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3032 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3033 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3034 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3035 })
Romain Guy171c5922011-01-06 10:04:23 -08003036 int mLayerType = LAYER_TYPE_NONE;
3037 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003038 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003039
3040 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003041 * Set to true when the view is sending hover accessibility events because it
3042 * is the innermost hovered view.
3043 */
3044 private boolean mSendingHoverAccessibilityEvents;
3045
3046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 * Simple constructor to use when creating a view from code.
3048 *
3049 * @param context The Context the view is running in, through which it can
3050 * access the current theme, resources, etc.
3051 */
3052 public View(Context context) {
3053 mContext = context;
3054 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003055 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003056 // Set layout and text direction defaults
3057 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003058 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003059 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3060 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003061 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003062 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003063 mUserPaddingStart = -1;
3064 mUserPaddingEnd = -1;
3065 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 }
3067
3068 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003069 * Delegate for injecting accessiblity functionality.
3070 */
3071 AccessibilityDelegate mAccessibilityDelegate;
3072
3073 /**
3074 * Consistency verifier for debugging purposes.
3075 * @hide
3076 */
3077 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3078 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3079 new InputEventConsistencyVerifier(this, 0) : null;
3080
3081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * Constructor that is called when inflating a view from XML. This is called
3083 * when a view is being constructed from an XML file, supplying attributes
3084 * that were specified in the XML file. This version uses a default style of
3085 * 0, so the only attribute values applied are those in the Context's Theme
3086 * and the given AttributeSet.
3087 *
3088 * <p>
3089 * The method onFinishInflate() will be called after all children have been
3090 * added.
3091 *
3092 * @param context The Context the view is running in, through which it can
3093 * access the current theme, resources, etc.
3094 * @param attrs The attributes of the XML tag that is inflating the view.
3095 * @see #View(Context, AttributeSet, int)
3096 */
3097 public View(Context context, AttributeSet attrs) {
3098 this(context, attrs, 0);
3099 }
3100
3101 /**
3102 * Perform inflation from XML and apply a class-specific base style. This
3103 * constructor of View allows subclasses to use their own base style when
3104 * they are inflating. For example, a Button class's constructor would call
3105 * this version of the super class constructor and supply
3106 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3107 * the theme's button style to modify all of the base view attributes (in
3108 * particular its background) as well as the Button class's attributes.
3109 *
3110 * @param context The Context the view is running in, through which it can
3111 * access the current theme, resources, etc.
3112 * @param attrs The attributes of the XML tag that is inflating the view.
3113 * @param defStyle The default style to apply to this view. If 0, no style
3114 * will be applied (beyond what is included in the theme). This may
3115 * either be an attribute resource, whose value will be retrieved
3116 * from the current theme, or an explicit style resource.
3117 * @see #View(Context, AttributeSet)
3118 */
3119 public View(Context context, AttributeSet attrs, int defStyle) {
3120 this(context);
3121
3122 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3123 defStyle, 0);
3124
3125 Drawable background = null;
3126
3127 int leftPadding = -1;
3128 int topPadding = -1;
3129 int rightPadding = -1;
3130 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003131 int startPadding = -1;
3132 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133
3134 int padding = -1;
3135
3136 int viewFlagValues = 0;
3137 int viewFlagMasks = 0;
3138
3139 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 int x = 0;
3142 int y = 0;
3143
Chet Haase73066682010-11-29 15:55:32 -08003144 float tx = 0;
3145 float ty = 0;
3146 float rotation = 0;
3147 float rotationX = 0;
3148 float rotationY = 0;
3149 float sx = 1f;
3150 float sy = 1f;
3151 boolean transformSet = false;
3152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3154
Adam Powell637d3372010-08-25 14:37:03 -07003155 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 final int N = a.getIndexCount();
3157 for (int i = 0; i < N; i++) {
3158 int attr = a.getIndex(i);
3159 switch (attr) {
3160 case com.android.internal.R.styleable.View_background:
3161 background = a.getDrawable(attr);
3162 break;
3163 case com.android.internal.R.styleable.View_padding:
3164 padding = a.getDimensionPixelSize(attr, -1);
3165 break;
3166 case com.android.internal.R.styleable.View_paddingLeft:
3167 leftPadding = a.getDimensionPixelSize(attr, -1);
3168 break;
3169 case com.android.internal.R.styleable.View_paddingTop:
3170 topPadding = a.getDimensionPixelSize(attr, -1);
3171 break;
3172 case com.android.internal.R.styleable.View_paddingRight:
3173 rightPadding = a.getDimensionPixelSize(attr, -1);
3174 break;
3175 case com.android.internal.R.styleable.View_paddingBottom:
3176 bottomPadding = a.getDimensionPixelSize(attr, -1);
3177 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003178 case com.android.internal.R.styleable.View_paddingStart:
3179 startPadding = a.getDimensionPixelSize(attr, -1);
3180 break;
3181 case com.android.internal.R.styleable.View_paddingEnd:
3182 endPadding = a.getDimensionPixelSize(attr, -1);
3183 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 case com.android.internal.R.styleable.View_scrollX:
3185 x = a.getDimensionPixelOffset(attr, 0);
3186 break;
3187 case com.android.internal.R.styleable.View_scrollY:
3188 y = a.getDimensionPixelOffset(attr, 0);
3189 break;
Chet Haase73066682010-11-29 15:55:32 -08003190 case com.android.internal.R.styleable.View_alpha:
3191 setAlpha(a.getFloat(attr, 1f));
3192 break;
3193 case com.android.internal.R.styleable.View_transformPivotX:
3194 setPivotX(a.getDimensionPixelOffset(attr, 0));
3195 break;
3196 case com.android.internal.R.styleable.View_transformPivotY:
3197 setPivotY(a.getDimensionPixelOffset(attr, 0));
3198 break;
3199 case com.android.internal.R.styleable.View_translationX:
3200 tx = a.getDimensionPixelOffset(attr, 0);
3201 transformSet = true;
3202 break;
3203 case com.android.internal.R.styleable.View_translationY:
3204 ty = a.getDimensionPixelOffset(attr, 0);
3205 transformSet = true;
3206 break;
3207 case com.android.internal.R.styleable.View_rotation:
3208 rotation = a.getFloat(attr, 0);
3209 transformSet = true;
3210 break;
3211 case com.android.internal.R.styleable.View_rotationX:
3212 rotationX = a.getFloat(attr, 0);
3213 transformSet = true;
3214 break;
3215 case com.android.internal.R.styleable.View_rotationY:
3216 rotationY = a.getFloat(attr, 0);
3217 transformSet = true;
3218 break;
3219 case com.android.internal.R.styleable.View_scaleX:
3220 sx = a.getFloat(attr, 1f);
3221 transformSet = true;
3222 break;
3223 case com.android.internal.R.styleable.View_scaleY:
3224 sy = a.getFloat(attr, 1f);
3225 transformSet = true;
3226 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 case com.android.internal.R.styleable.View_id:
3228 mID = a.getResourceId(attr, NO_ID);
3229 break;
3230 case com.android.internal.R.styleable.View_tag:
3231 mTag = a.getText(attr);
3232 break;
3233 case com.android.internal.R.styleable.View_fitsSystemWindows:
3234 if (a.getBoolean(attr, false)) {
3235 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3236 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3237 }
3238 break;
3239 case com.android.internal.R.styleable.View_focusable:
3240 if (a.getBoolean(attr, false)) {
3241 viewFlagValues |= FOCUSABLE;
3242 viewFlagMasks |= FOCUSABLE_MASK;
3243 }
3244 break;
3245 case com.android.internal.R.styleable.View_focusableInTouchMode:
3246 if (a.getBoolean(attr, false)) {
3247 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3248 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3249 }
3250 break;
3251 case com.android.internal.R.styleable.View_clickable:
3252 if (a.getBoolean(attr, false)) {
3253 viewFlagValues |= CLICKABLE;
3254 viewFlagMasks |= CLICKABLE;
3255 }
3256 break;
3257 case com.android.internal.R.styleable.View_longClickable:
3258 if (a.getBoolean(attr, false)) {
3259 viewFlagValues |= LONG_CLICKABLE;
3260 viewFlagMasks |= LONG_CLICKABLE;
3261 }
3262 break;
3263 case com.android.internal.R.styleable.View_saveEnabled:
3264 if (!a.getBoolean(attr, true)) {
3265 viewFlagValues |= SAVE_DISABLED;
3266 viewFlagMasks |= SAVE_DISABLED_MASK;
3267 }
3268 break;
3269 case com.android.internal.R.styleable.View_duplicateParentState:
3270 if (a.getBoolean(attr, false)) {
3271 viewFlagValues |= DUPLICATE_PARENT_STATE;
3272 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3273 }
3274 break;
3275 case com.android.internal.R.styleable.View_visibility:
3276 final int visibility = a.getInt(attr, 0);
3277 if (visibility != 0) {
3278 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3279 viewFlagMasks |= VISIBILITY_MASK;
3280 }
3281 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003282 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003283 // Clear any layout direction flags (included resolved bits) already set
3284 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3285 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003286 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003287 final int value = (layoutDirection != -1) ?
3288 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3289 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003290 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 case com.android.internal.R.styleable.View_drawingCacheQuality:
3292 final int cacheQuality = a.getInt(attr, 0);
3293 if (cacheQuality != 0) {
3294 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3295 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3296 }
3297 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003298 case com.android.internal.R.styleable.View_contentDescription:
3299 mContentDescription = a.getString(attr);
3300 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3302 if (!a.getBoolean(attr, true)) {
3303 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3304 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3305 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003306 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3308 if (!a.getBoolean(attr, true)) {
3309 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3310 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3311 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003312 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 case R.styleable.View_scrollbars:
3314 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3315 if (scrollbars != SCROLLBARS_NONE) {
3316 viewFlagValues |= scrollbars;
3317 viewFlagMasks |= SCROLLBARS_MASK;
3318 initializeScrollbars(a);
3319 }
3320 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003321 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003323 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3324 // Ignore the attribute starting with ICS
3325 break;
3326 }
3327 // With builds < ICS, fall through and apply fading edges
3328 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3330 if (fadingEdge != FADING_EDGE_NONE) {
3331 viewFlagValues |= fadingEdge;
3332 viewFlagMasks |= FADING_EDGE_MASK;
3333 initializeFadingEdge(a);
3334 }
3335 break;
3336 case R.styleable.View_scrollbarStyle:
3337 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3338 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3339 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3340 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3341 }
3342 break;
3343 case R.styleable.View_isScrollContainer:
3344 setScrollContainer = true;
3345 if (a.getBoolean(attr, false)) {
3346 setScrollContainer(true);
3347 }
3348 break;
3349 case com.android.internal.R.styleable.View_keepScreenOn:
3350 if (a.getBoolean(attr, false)) {
3351 viewFlagValues |= KEEP_SCREEN_ON;
3352 viewFlagMasks |= KEEP_SCREEN_ON;
3353 }
3354 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003355 case R.styleable.View_filterTouchesWhenObscured:
3356 if (a.getBoolean(attr, false)) {
3357 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3358 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3359 }
3360 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 case R.styleable.View_nextFocusLeft:
3362 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3363 break;
3364 case R.styleable.View_nextFocusRight:
3365 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3366 break;
3367 case R.styleable.View_nextFocusUp:
3368 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3369 break;
3370 case R.styleable.View_nextFocusDown:
3371 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3372 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003373 case R.styleable.View_nextFocusForward:
3374 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3375 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 case R.styleable.View_minWidth:
3377 mMinWidth = a.getDimensionPixelSize(attr, 0);
3378 break;
3379 case R.styleable.View_minHeight:
3380 mMinHeight = a.getDimensionPixelSize(attr, 0);
3381 break;
Romain Guy9a817362009-05-01 10:57:14 -07003382 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003383 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003384 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003385 + "be used within a restricted context");
3386 }
3387
Romain Guy9a817362009-05-01 10:57:14 -07003388 final String handlerName = a.getString(attr);
3389 if (handlerName != null) {
3390 setOnClickListener(new OnClickListener() {
3391 private Method mHandler;
3392
3393 public void onClick(View v) {
3394 if (mHandler == null) {
3395 try {
3396 mHandler = getContext().getClass().getMethod(handlerName,
3397 View.class);
3398 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003399 int id = getId();
3400 String idText = id == NO_ID ? "" : " with id '"
3401 + getContext().getResources().getResourceEntryName(
3402 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003403 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003404 handlerName + "(View) in the activity "
3405 + getContext().getClass() + " for onClick handler"
3406 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003407 }
3408 }
3409
3410 try {
3411 mHandler.invoke(getContext(), View.this);
3412 } catch (IllegalAccessException e) {
3413 throw new IllegalStateException("Could not execute non "
3414 + "public method of the activity", e);
3415 } catch (InvocationTargetException e) {
3416 throw new IllegalStateException("Could not execute "
3417 + "method of the activity", e);
3418 }
3419 }
3420 });
3421 }
3422 break;
Adam Powell637d3372010-08-25 14:37:03 -07003423 case R.styleable.View_overScrollMode:
3424 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3425 break;
Adam Powell20232d02010-12-08 21:08:53 -08003426 case R.styleable.View_verticalScrollbarPosition:
3427 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3428 break;
Romain Guy171c5922011-01-06 10:04:23 -08003429 case R.styleable.View_layerType:
3430 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3431 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003432 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003433 // Clear any text direction flag already set
3434 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3435 // Set the text direction flags depending on the value of the attribute
3436 final int textDirection = a.getInt(attr, -1);
3437 if (textDirection != -1) {
3438 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3439 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003440 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003441 case R.styleable.View_textAlignment:
3442 // Clear any text alignment flag already set
3443 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3444 // Set the text alignment flag depending on the value of the attribute
3445 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3446 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3447 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003448 case R.styleable.View_importantForAccessibility:
3449 setImportantForAccessibility(a.getInt(attr,
3450 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 }
3452 }
3453
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003454 a.recycle();
3455
Adam Powell637d3372010-08-25 14:37:03 -07003456 setOverScrollMode(overScrollMode);
3457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003459 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 }
3461
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003462 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3463 // layout direction). Those cached values will be used later during padding resolution.
3464 mUserPaddingStart = startPadding;
3465 mUserPaddingEnd = endPadding;
3466
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003467 updateUserPaddingRelative();
3468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 if (padding >= 0) {
3470 leftPadding = padding;
3471 topPadding = padding;
3472 rightPadding = padding;
3473 bottomPadding = padding;
3474 }
3475
3476 // If the user specified the padding (either with android:padding or
3477 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3478 // use the default padding or the padding from the background drawable
3479 // (stored at this point in mPadding*)
3480 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3481 topPadding >= 0 ? topPadding : mPaddingTop,
3482 rightPadding >= 0 ? rightPadding : mPaddingRight,
3483 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3484
3485 if (viewFlagMasks != 0) {
3486 setFlags(viewFlagValues, viewFlagMasks);
3487 }
3488
3489 // Needs to be called after mViewFlags is set
3490 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3491 recomputePadding();
3492 }
3493
3494 if (x != 0 || y != 0) {
3495 scrollTo(x, y);
3496 }
3497
Chet Haase73066682010-11-29 15:55:32 -08003498 if (transformSet) {
3499 setTranslationX(tx);
3500 setTranslationY(ty);
3501 setRotation(rotation);
3502 setRotationX(rotationX);
3503 setRotationY(rotationY);
3504 setScaleX(sx);
3505 setScaleY(sy);
3506 }
3507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3509 setScrollContainer(true);
3510 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003511
3512 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 }
3514
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003515 private void updateUserPaddingRelative() {
3516 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3517 }
3518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 /**
3520 * Non-public constructor for use in testing
3521 */
3522 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003523 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 }
3525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 /**
3527 * <p>
3528 * Initializes the fading edges from a given set of styled attributes. This
3529 * method should be called by subclasses that need fading edges and when an
3530 * instance of these subclasses is created programmatically rather than
3531 * being inflated from XML. This method is automatically called when the XML
3532 * is inflated.
3533 * </p>
3534 *
3535 * @param a the styled attributes set to initialize the fading edges from
3536 */
3537 protected void initializeFadingEdge(TypedArray a) {
3538 initScrollCache();
3539
3540 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3541 R.styleable.View_fadingEdgeLength,
3542 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3543 }
3544
3545 /**
3546 * Returns the size of the vertical faded edges used to indicate that more
3547 * content in this view is visible.
3548 *
3549 * @return The size in pixels of the vertical faded edge or 0 if vertical
3550 * faded edges are not enabled for this view.
3551 * @attr ref android.R.styleable#View_fadingEdgeLength
3552 */
3553 public int getVerticalFadingEdgeLength() {
3554 if (isVerticalFadingEdgeEnabled()) {
3555 ScrollabilityCache cache = mScrollCache;
3556 if (cache != null) {
3557 return cache.fadingEdgeLength;
3558 }
3559 }
3560 return 0;
3561 }
3562
3563 /**
3564 * Set the size of the faded edge used to indicate that more content in this
3565 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003566 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3567 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3568 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 *
3570 * @param length The size in pixels of the faded edge used to indicate that more
3571 * content in this view is visible.
3572 */
3573 public void setFadingEdgeLength(int length) {
3574 initScrollCache();
3575 mScrollCache.fadingEdgeLength = length;
3576 }
3577
3578 /**
3579 * Returns the size of the horizontal faded edges used to indicate that more
3580 * content in this view is visible.
3581 *
3582 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3583 * faded edges are not enabled for this view.
3584 * @attr ref android.R.styleable#View_fadingEdgeLength
3585 */
3586 public int getHorizontalFadingEdgeLength() {
3587 if (isHorizontalFadingEdgeEnabled()) {
3588 ScrollabilityCache cache = mScrollCache;
3589 if (cache != null) {
3590 return cache.fadingEdgeLength;
3591 }
3592 }
3593 return 0;
3594 }
3595
3596 /**
3597 * Returns the width of the vertical scrollbar.
3598 *
3599 * @return The width in pixels of the vertical scrollbar or 0 if there
3600 * is no vertical scrollbar.
3601 */
3602 public int getVerticalScrollbarWidth() {
3603 ScrollabilityCache cache = mScrollCache;
3604 if (cache != null) {
3605 ScrollBarDrawable scrollBar = cache.scrollBar;
3606 if (scrollBar != null) {
3607 int size = scrollBar.getSize(true);
3608 if (size <= 0) {
3609 size = cache.scrollBarSize;
3610 }
3611 return size;
3612 }
3613 return 0;
3614 }
3615 return 0;
3616 }
3617
3618 /**
3619 * Returns the height of the horizontal scrollbar.
3620 *
3621 * @return The height in pixels of the horizontal scrollbar or 0 if
3622 * there is no horizontal scrollbar.
3623 */
3624 protected int getHorizontalScrollbarHeight() {
3625 ScrollabilityCache cache = mScrollCache;
3626 if (cache != null) {
3627 ScrollBarDrawable scrollBar = cache.scrollBar;
3628 if (scrollBar != null) {
3629 int size = scrollBar.getSize(false);
3630 if (size <= 0) {
3631 size = cache.scrollBarSize;
3632 }
3633 return size;
3634 }
3635 return 0;
3636 }
3637 return 0;
3638 }
3639
3640 /**
3641 * <p>
3642 * Initializes the scrollbars from a given set of styled attributes. This
3643 * method should be called by subclasses that need scrollbars and when an
3644 * instance of these subclasses is created programmatically rather than
3645 * being inflated from XML. This method is automatically called when the XML
3646 * is inflated.
3647 * </p>
3648 *
3649 * @param a the styled attributes set to initialize the scrollbars from
3650 */
3651 protected void initializeScrollbars(TypedArray a) {
3652 initScrollCache();
3653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003655
Mike Cleronf116bf82009-09-27 19:14:12 -07003656 if (scrollabilityCache.scrollBar == null) {
3657 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3658 }
Joe Malin32736f02011-01-19 16:14:20 -08003659
Romain Guy8bda2482010-03-02 11:42:11 -08003660 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661
Mike Cleronf116bf82009-09-27 19:14:12 -07003662 if (!fadeScrollbars) {
3663 scrollabilityCache.state = ScrollabilityCache.ON;
3664 }
3665 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003666
3667
Mike Cleronf116bf82009-09-27 19:14:12 -07003668 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3669 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3670 .getScrollBarFadeDuration());
3671 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3672 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3673 ViewConfiguration.getScrollDefaultDelay());
3674
Joe Malin32736f02011-01-19 16:14:20 -08003675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3677 com.android.internal.R.styleable.View_scrollbarSize,
3678 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3679
3680 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3681 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3682
3683 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3684 if (thumb != null) {
3685 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3686 }
3687
3688 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3689 false);
3690 if (alwaysDraw) {
3691 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3692 }
3693
3694 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3695 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3696
3697 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3698 if (thumb != null) {
3699 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3700 }
3701
3702 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3703 false);
3704 if (alwaysDraw) {
3705 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3706 }
3707
3708 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003709 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 }
3711
3712 /**
3713 * <p>
3714 * Initalizes the scrollability cache if necessary.
3715 * </p>
3716 */
3717 private void initScrollCache() {
3718 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003719 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
3721 }
3722
Philip Milne6c8ea062012-04-03 17:38:43 -07003723 private ScrollabilityCache getScrollCache() {
3724 initScrollCache();
3725 return mScrollCache;
3726 }
3727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 /**
Adam Powell20232d02010-12-08 21:08:53 -08003729 * Set the position of the vertical scroll bar. Should be one of
3730 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3731 * {@link #SCROLLBAR_POSITION_RIGHT}.
3732 *
3733 * @param position Where the vertical scroll bar should be positioned.
3734 */
3735 public void setVerticalScrollbarPosition(int position) {
3736 if (mVerticalScrollbarPosition != position) {
3737 mVerticalScrollbarPosition = position;
3738 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003739 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003740 }
3741 }
3742
3743 /**
3744 * @return The position where the vertical scroll bar will show, if applicable.
3745 * @see #setVerticalScrollbarPosition(int)
3746 */
3747 public int getVerticalScrollbarPosition() {
3748 return mVerticalScrollbarPosition;
3749 }
3750
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003751 ListenerInfo getListenerInfo() {
3752 if (mListenerInfo != null) {
3753 return mListenerInfo;
3754 }
3755 mListenerInfo = new ListenerInfo();
3756 return mListenerInfo;
3757 }
3758
Adam Powell20232d02010-12-08 21:08:53 -08003759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 * Register a callback to be invoked when focus of this view changed.
3761 *
3762 * @param l The callback that will run.
3763 */
3764 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003765 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 }
3767
3768 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003769 * Add a listener that will be called when the bounds of the view change due to
3770 * layout processing.
3771 *
3772 * @param listener The listener that will be called when layout bounds change.
3773 */
3774 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003775 ListenerInfo li = getListenerInfo();
3776 if (li.mOnLayoutChangeListeners == null) {
3777 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003778 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003779 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3780 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003781 }
Chet Haase21cd1382010-09-01 17:42:29 -07003782 }
3783
3784 /**
3785 * Remove a listener for layout changes.
3786 *
3787 * @param listener The listener for layout bounds change.
3788 */
3789 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 ListenerInfo li = mListenerInfo;
3791 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003792 return;
3793 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003794 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003795 }
3796
3797 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003798 * Add a listener for attach state changes.
3799 *
3800 * This listener will be called whenever this view is attached or detached
3801 * from a window. Remove the listener using
3802 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3803 *
3804 * @param listener Listener to attach
3805 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3806 */
3807 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 ListenerInfo li = getListenerInfo();
3809 if (li.mOnAttachStateChangeListeners == null) {
3810 li.mOnAttachStateChangeListeners
3811 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003812 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003813 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003814 }
3815
3816 /**
3817 * Remove a listener for attach state changes. The listener will receive no further
3818 * notification of window attach/detach events.
3819 *
3820 * @param listener Listener to remove
3821 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3822 */
3823 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003824 ListenerInfo li = mListenerInfo;
3825 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003826 return;
3827 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003828 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003829 }
3830
3831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 * Returns the focus-change callback registered for this view.
3833 *
3834 * @return The callback, or null if one is not registered.
3835 */
3836 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003837 ListenerInfo li = mListenerInfo;
3838 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 }
3840
3841 /**
3842 * Register a callback to be invoked when this view is clicked. If this view is not
3843 * clickable, it becomes clickable.
3844 *
3845 * @param l The callback that will run
3846 *
3847 * @see #setClickable(boolean)
3848 */
3849 public void setOnClickListener(OnClickListener l) {
3850 if (!isClickable()) {
3851 setClickable(true);
3852 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003853 getListenerInfo().mOnClickListener = l;
3854 }
3855
3856 /**
3857 * Return whether this view has an attached OnClickListener. Returns
3858 * true if there is a listener, false if there is none.
3859 */
3860 public boolean hasOnClickListeners() {
3861 ListenerInfo li = mListenerInfo;
3862 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
3864
3865 /**
3866 * Register a callback to be invoked when this view is clicked and held. If this view is not
3867 * long clickable, it becomes long clickable.
3868 *
3869 * @param l The callback that will run
3870 *
3871 * @see #setLongClickable(boolean)
3872 */
3873 public void setOnLongClickListener(OnLongClickListener l) {
3874 if (!isLongClickable()) {
3875 setLongClickable(true);
3876 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003877 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879
3880 /**
3881 * Register a callback to be invoked when the context menu for this view is
3882 * being built. If this view is not long clickable, it becomes long clickable.
3883 *
3884 * @param l The callback that will run
3885 *
3886 */
3887 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3888 if (!isLongClickable()) {
3889 setLongClickable(true);
3890 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003891 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 }
3893
3894 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003895 * Call this view's OnClickListener, if it is defined. Performs all normal
3896 * actions associated with clicking: reporting accessibility event, playing
3897 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 *
3899 * @return True there was an assigned OnClickListener that was called, false
3900 * otherwise is returned.
3901 */
3902 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003903 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3904
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003905 ListenerInfo li = mListenerInfo;
3906 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003908 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 return true;
3910 }
3911
3912 return false;
3913 }
3914
3915 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003916 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3917 * this only calls the listener, and does not do any associated clicking
3918 * actions like reporting an accessibility event.
3919 *
3920 * @return True there was an assigned OnClickListener that was called, false
3921 * otherwise is returned.
3922 */
3923 public boolean callOnClick() {
3924 ListenerInfo li = mListenerInfo;
3925 if (li != null && li.mOnClickListener != null) {
3926 li.mOnClickListener.onClick(this);
3927 return true;
3928 }
3929 return false;
3930 }
3931
3932 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003933 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3934 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003936 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 */
3938 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003939 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003942 ListenerInfo li = mListenerInfo;
3943 if (li != null && li.mOnLongClickListener != null) {
3944 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 }
3946 if (!handled) {
3947 handled = showContextMenu();
3948 }
3949 if (handled) {
3950 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3951 }
3952 return handled;
3953 }
3954
3955 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003956 * Performs button-related actions during a touch down event.
3957 *
3958 * @param event The event.
3959 * @return True if the down was consumed.
3960 *
3961 * @hide
3962 */
3963 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3964 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3965 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3966 return true;
3967 }
3968 }
3969 return false;
3970 }
3971
3972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 * Bring up the context menu for this view.
3974 *
3975 * @return Whether a context menu was displayed.
3976 */
3977 public boolean showContextMenu() {
3978 return getParent().showContextMenuForChild(this);
3979 }
3980
3981 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003982 * Bring up the context menu for this view, referring to the item under the specified point.
3983 *
3984 * @param x The referenced x coordinate.
3985 * @param y The referenced y coordinate.
3986 * @param metaState The keyboard modifiers that were pressed.
3987 * @return Whether a context menu was displayed.
3988 *
3989 * @hide
3990 */
3991 public boolean showContextMenu(float x, float y, int metaState) {
3992 return showContextMenu();
3993 }
3994
3995 /**
Adam Powell6e346362010-07-23 10:18:23 -07003996 * Start an action mode.
3997 *
3998 * @param callback Callback that will control the lifecycle of the action mode
3999 * @return The new action mode if it is started, null otherwise
4000 *
4001 * @see ActionMode
4002 */
4003 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004004 ViewParent parent = getParent();
4005 if (parent == null) return null;
4006 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004007 }
4008
4009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 * Register a callback to be invoked when a key is pressed in this view.
4011 * @param l the key listener to attach to this view
4012 */
4013 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004014 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 }
4016
4017 /**
4018 * Register a callback to be invoked when a touch event is sent to this view.
4019 * @param l the touch listener to attach to this view
4020 */
4021 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004022 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024
4025 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004026 * Register a callback to be invoked when a generic motion event is sent to this view.
4027 * @param l the generic motion listener to attach to this view
4028 */
4029 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004030 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004031 }
4032
4033 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004034 * Register a callback to be invoked when a hover event is sent to this view.
4035 * @param l the hover listener to attach to this view
4036 */
4037 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004039 }
4040
4041 /**
Joe Malin32736f02011-01-19 16:14:20 -08004042 * Register a drag event listener callback object for this View. The parameter is
4043 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4044 * View, the system calls the
4045 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4046 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004047 */
4048 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004049 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004050 }
4051
4052 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004053 * Give this view focus. This will cause
4054 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 *
4056 * Note: this does not check whether this {@link View} should get focus, it just
4057 * gives it focus no matter what. It should only be called internally by framework
4058 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4059 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004060 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4061 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 * focus moved when requestFocus() is called. It may not always
4063 * apply, in which case use the default View.FOCUS_DOWN.
4064 * @param previouslyFocusedRect The rectangle of the view that had focus
4065 * prior in this View's coordinate system.
4066 */
4067 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4068 if (DBG) {
4069 System.out.println(this + " requestFocus()");
4070 }
4071
4072 if ((mPrivateFlags & FOCUSED) == 0) {
4073 mPrivateFlags |= FOCUSED;
4074
4075 if (mParent != null) {
4076 mParent.requestChildFocus(this, this);
4077 }
4078
4079 onFocusChanged(true, direction, previouslyFocusedRect);
4080 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004081
4082 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4083 notifyAccessibilityStateChanged();
4084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 }
4086 }
4087
4088 /**
4089 * Request that a rectangle of this view be visible on the screen,
4090 * scrolling if necessary just enough.
4091 *
4092 * <p>A View should call this if it maintains some notion of which part
4093 * of its content is interesting. For example, a text editing view
4094 * should call this when its cursor moves.
4095 *
4096 * @param rectangle The rectangle.
4097 * @return Whether any parent scrolled.
4098 */
4099 public boolean requestRectangleOnScreen(Rect rectangle) {
4100 return requestRectangleOnScreen(rectangle, false);
4101 }
4102
4103 /**
4104 * Request that a rectangle of this view be visible on the screen,
4105 * scrolling if necessary just enough.
4106 *
4107 * <p>A View should call this if it maintains some notion of which part
4108 * of its content is interesting. For example, a text editing view
4109 * should call this when its cursor moves.
4110 *
4111 * <p>When <code>immediate</code> is set to true, scrolling will not be
4112 * animated.
4113 *
4114 * @param rectangle The rectangle.
4115 * @param immediate True to forbid animated scrolling, false otherwise
4116 * @return Whether any parent scrolled.
4117 */
4118 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4119 View child = this;
4120 ViewParent parent = mParent;
4121 boolean scrolled = false;
4122 while (parent != null) {
4123 scrolled |= parent.requestChildRectangleOnScreen(child,
4124 rectangle, immediate);
4125
4126 // offset rect so next call has the rectangle in the
4127 // coordinate system of its direct child.
4128 rectangle.offset(child.getLeft(), child.getTop());
4129 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4130
4131 if (!(parent instanceof View)) {
4132 break;
4133 }
Romain Guy8506ab42009-06-11 17:35:47 -07004134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 child = (View) parent;
4136 parent = child.getParent();
4137 }
4138 return scrolled;
4139 }
4140
4141 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004142 * Called when this view wants to give up focus. If focus is cleared
4143 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4144 * <p>
4145 * <strong>Note:</strong> When a View clears focus the framework is trying
4146 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004147 * View is the first from the top that can take focus, then all callbacks
4148 * related to clearing focus will be invoked after wich the framework will
4149 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004150 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 */
4152 public void clearFocus() {
4153 if (DBG) {
4154 System.out.println(this + " clearFocus()");
4155 }
4156
4157 if ((mPrivateFlags & FOCUSED) != 0) {
4158 mPrivateFlags &= ~FOCUSED;
4159
4160 if (mParent != null) {
4161 mParent.clearChildFocus(this);
4162 }
4163
4164 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004167
4168 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004169
4170 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4171 notifyAccessibilityStateChanged();
4172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174 }
4175
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004176 void ensureInputFocusOnFirstFocusable() {
4177 View root = getRootView();
4178 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004179 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
4181 }
4182
4183 /**
4184 * Called internally by the view system when a new view is getting focus.
4185 * This is what clears the old focus.
4186 */
4187 void unFocus() {
4188 if (DBG) {
4189 System.out.println(this + " unFocus()");
4190 }
4191
4192 if ((mPrivateFlags & FOCUSED) != 0) {
4193 mPrivateFlags &= ~FOCUSED;
4194
4195 onFocusChanged(false, 0, null);
4196 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004197
4198 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4199 notifyAccessibilityStateChanged();
4200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 }
4202 }
4203
4204 /**
4205 * Returns true if this view has focus iteself, or is the ancestor of the
4206 * view that has focus.
4207 *
4208 * @return True if this view has or contains focus, false otherwise.
4209 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004210 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 public boolean hasFocus() {
4212 return (mPrivateFlags & FOCUSED) != 0;
4213 }
4214
4215 /**
4216 * Returns true if this view is focusable or if it contains a reachable View
4217 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4218 * is a View whose parents do not block descendants focus.
4219 *
4220 * Only {@link #VISIBLE} views are considered focusable.
4221 *
4222 * @return True if the view is focusable or if the view contains a focusable
4223 * View, false otherwise.
4224 *
4225 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4226 */
4227 public boolean hasFocusable() {
4228 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4229 }
4230
4231 /**
4232 * Called by the view system when the focus state of this view changes.
4233 * When the focus change event is caused by directional navigation, direction
4234 * and previouslyFocusedRect provide insight into where the focus is coming from.
4235 * When overriding, be sure to call up through to the super class so that
4236 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 * @param gainFocus True if the View has focus; false otherwise.
4239 * @param direction The direction focus has moved when requestFocus()
4240 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004241 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4242 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4243 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4245 * system, of the previously focused view. If applicable, this will be
4246 * passed in as finer grained information about where the focus is coming
4247 * from (in addition to direction). Will be <code>null</code> otherwise.
4248 */
4249 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004250 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004251 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4252 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4253 requestAccessibilityFocus();
4254 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004255 }
4256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 InputMethodManager imm = InputMethodManager.peekInstance();
4258 if (!gainFocus) {
4259 if (isPressed()) {
4260 setPressed(false);
4261 }
4262 if (imm != null && mAttachInfo != null
4263 && mAttachInfo.mHasWindowFocus) {
4264 imm.focusOut(this);
4265 }
Romain Guya2431d02009-04-30 16:30:00 -07004266 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 } else if (imm != null && mAttachInfo != null
4268 && mAttachInfo.mHasWindowFocus) {
4269 imm.focusIn(this);
4270 }
Romain Guy8506ab42009-06-11 17:35:47 -07004271
Romain Guy0fd89bf2011-01-26 15:41:30 -08004272 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004273 ListenerInfo li = mListenerInfo;
4274 if (li != null && li.mOnFocusChangeListener != null) {
4275 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
Joe Malin32736f02011-01-19 16:14:20 -08004277
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004278 if (mAttachInfo != null) {
4279 mAttachInfo.mKeyDispatchState.reset(this);
4280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 }
4282
4283 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004284 * Sends an accessibility event of the given type. If accessiiblity is
4285 * not enabled this method has no effect. The default implementation calls
4286 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4287 * to populate information about the event source (this View), then calls
4288 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4289 * populate the text content of the event source including its descendants,
4290 * and last calls
4291 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4292 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004293 * <p>
4294 * If an {@link AccessibilityDelegate} has been specified via calling
4295 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4296 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4297 * responsible for handling this call.
4298 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004299 *
Scott Mainb303d832011-10-12 16:45:18 -07004300 * @param eventType The type of the event to send, as defined by several types from
4301 * {@link android.view.accessibility.AccessibilityEvent}, such as
4302 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4303 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004304 *
4305 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4306 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4307 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004308 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004309 */
4310 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004311 if (mAccessibilityDelegate != null) {
4312 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4313 } else {
4314 sendAccessibilityEventInternal(eventType);
4315 }
4316 }
4317
4318 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004319 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4320 * {@link AccessibilityEvent} to make an announcement which is related to some
4321 * sort of a context change for which none of the events representing UI transitions
4322 * is a good fit. For example, announcing a new page in a book. If accessibility
4323 * is not enabled this method does nothing.
4324 *
4325 * @param text The announcement text.
4326 */
4327 public void announceForAccessibility(CharSequence text) {
4328 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4329 AccessibilityEvent event = AccessibilityEvent.obtain(
4330 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4331 event.getText().add(text);
4332 sendAccessibilityEventUnchecked(event);
4333 }
4334 }
4335
4336 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004337 * @see #sendAccessibilityEvent(int)
4338 *
4339 * Note: Called from the default {@link AccessibilityDelegate}.
4340 */
4341 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004342 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4343 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4344 }
4345 }
4346
4347 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004348 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4349 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004350 * perform a check whether accessibility is enabled.
4351 * <p>
4352 * If an {@link AccessibilityDelegate} has been specified via calling
4353 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4354 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4355 * is responsible for handling this call.
4356 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004357 *
4358 * @param event The event to send.
4359 *
4360 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004361 */
4362 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004363 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004364 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004365 } else {
4366 sendAccessibilityEventUncheckedInternal(event);
4367 }
4368 }
4369
4370 /**
4371 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4372 *
4373 * Note: Called from the default {@link AccessibilityDelegate}.
4374 */
4375 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004376 if (!isShown()) {
4377 return;
4378 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004379 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004380 // Only a subset of accessibility events populates text content.
4381 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4382 dispatchPopulateAccessibilityEvent(event);
4383 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004384 // Intercept accessibility focus events fired by virtual nodes to keep
4385 // track of accessibility focus position in such nodes.
4386 final int eventType = event.getEventType();
4387 switch (eventType) {
4388 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4389 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4390 event.getSourceNodeId());
4391 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4392 ViewRootImpl viewRootImpl = getViewRootImpl();
4393 if (viewRootImpl != null) {
4394 viewRootImpl.setAccessibilityFocusedHost(this);
4395 }
4396 }
4397 } break;
4398 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4399 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4400 event.getSourceNodeId());
4401 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4402 ViewRootImpl viewRootImpl = getViewRootImpl();
4403 if (viewRootImpl != null) {
4404 viewRootImpl.setAccessibilityFocusedHost(null);
4405 }
4406 }
4407 } break;
4408 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004409 // In the beginning we called #isShown(), so we know that getParent() is not null.
4410 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004411 }
4412
4413 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004414 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4415 * to its children for adding their text content to the event. Note that the
4416 * event text is populated in a separate dispatch path since we add to the
4417 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004418 * A typical implementation will call
4419 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4420 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4421 * on each child. Override this method if custom population of the event text
4422 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004423 * <p>
4424 * If an {@link AccessibilityDelegate} has been specified via calling
4425 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4426 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4427 * is responsible for handling this call.
4428 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004429 * <p>
4430 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4431 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4432 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004433 *
4434 * @param event The event.
4435 *
4436 * @return True if the event population was completed.
4437 */
4438 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004439 if (mAccessibilityDelegate != null) {
4440 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4441 } else {
4442 return dispatchPopulateAccessibilityEventInternal(event);
4443 }
4444 }
4445
4446 /**
4447 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4448 *
4449 * Note: Called from the default {@link AccessibilityDelegate}.
4450 */
4451 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004452 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004453 return false;
4454 }
4455
4456 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004457 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004458 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004459 * text content. While this method is free to modify event
4460 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4462 * <p>
4463 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004464 * to the text added by the super implementation:
4465 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004466 * super.onPopulateAccessibilityEvent(event);
4467 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4468 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4469 * mCurrentDate.getTimeInMillis(), flags);
4470 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004471 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004472 * <p>
4473 * If an {@link AccessibilityDelegate} has been specified via calling
4474 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4475 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4476 * is responsible for handling this call.
4477 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004478 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4479 * information to the event, in case the default implementation has basic information to add.
4480 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004481 *
4482 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004483 *
4484 * @see #sendAccessibilityEvent(int)
4485 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004486 */
4487 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004488 if (mAccessibilityDelegate != null) {
4489 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4490 } else {
4491 onPopulateAccessibilityEventInternal(event);
4492 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004493 }
4494
4495 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004496 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4497 *
4498 * Note: Called from the default {@link AccessibilityDelegate}.
4499 */
4500 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4501
4502 }
4503
4504 /**
4505 * Initializes an {@link AccessibilityEvent} with information about
4506 * this View which is the event source. In other words, the source of
4507 * an accessibility event is the view whose state change triggered firing
4508 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004509 * <p>
4510 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004511 * to properties set by the super implementation:
4512 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4513 * super.onInitializeAccessibilityEvent(event);
4514 * event.setPassword(true);
4515 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004516 * <p>
4517 * If an {@link AccessibilityDelegate} has been specified via calling
4518 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4519 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4520 * is responsible for handling this call.
4521 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004522 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4523 * information to the event, in case the default implementation has basic information to add.
4524 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004525 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004526 *
4527 * @see #sendAccessibilityEvent(int)
4528 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4529 */
4530 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004531 if (mAccessibilityDelegate != null) {
4532 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4533 } else {
4534 onInitializeAccessibilityEventInternal(event);
4535 }
4536 }
4537
4538 /**
4539 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4540 *
4541 * Note: Called from the default {@link AccessibilityDelegate}.
4542 */
4543 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004544 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004545 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004546 event.setPackageName(getContext().getPackageName());
4547 event.setEnabled(isEnabled());
4548 event.setContentDescription(mContentDescription);
4549
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004550 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004551 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004552 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4553 FOCUSABLES_ALL);
4554 event.setItemCount(focusablesTempList.size());
4555 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4556 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004557 }
4558 }
4559
4560 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004561 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4562 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4563 * This method is responsible for obtaining an accessibility node info from a
4564 * pool of reusable instances and calling
4565 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4566 * initialize the former.
4567 * <p>
4568 * Note: The client is responsible for recycling the obtained instance by calling
4569 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4570 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004571 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004572 * @return A populated {@link AccessibilityNodeInfo}.
4573 *
4574 * @see AccessibilityNodeInfo
4575 */
4576 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004577 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4578 if (provider != null) {
4579 return provider.createAccessibilityNodeInfo(View.NO_ID);
4580 } else {
4581 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4582 onInitializeAccessibilityNodeInfo(info);
4583 return info;
4584 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004585 }
4586
4587 /**
4588 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4589 * The base implementation sets:
4590 * <ul>
4591 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004592 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4593 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004594 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4595 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4596 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4597 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4598 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4599 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4600 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4601 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4602 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4603 * </ul>
4604 * <p>
4605 * Subclasses should override this method, call the super implementation,
4606 * and set additional attributes.
4607 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004608 * <p>
4609 * If an {@link AccessibilityDelegate} has been specified via calling
4610 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4611 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4612 * is responsible for handling this call.
4613 * </p>
4614 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004615 * @param info The instance to initialize.
4616 */
4617 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004618 if (mAccessibilityDelegate != null) {
4619 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4620 } else {
4621 onInitializeAccessibilityNodeInfoInternal(info);
4622 }
4623 }
4624
4625 /**
4626 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4627 *
4628 * Note: Called from the default {@link AccessibilityDelegate}.
4629 */
4630 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004631 Rect bounds = mAttachInfo.mTmpInvalRect;
4632 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004633 info.setBoundsInParent(bounds);
4634
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004635 getGlobalVisibleRect(bounds);
4636 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004637 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004638
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004639 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004640 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004641 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004642 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004643 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004644 }
4645
4646 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004647 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004648 info.setContentDescription(getContentDescription());
4649
4650 info.setEnabled(isEnabled());
4651 info.setClickable(isClickable());
4652 info.setFocusable(isFocusable());
4653 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004654 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004655 info.setSelected(isSelected());
4656 info.setLongClickable(isLongClickable());
4657
4658 // TODO: These make sense only if we are in an AdapterView but all
4659 // views can be selected. Maybe from accessiiblity perspective
4660 // we should report as selectable view in an AdapterView.
4661 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4662 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4663
4664 if (isFocusable()) {
4665 if (isFocused()) {
4666 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4667 } else {
4668 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4669 }
4670 }
4671 }
4672
4673 /**
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004674 * Computes whether this view is visible on the screen.
4675 *
4676 * @return Whether the view is visible on the screen.
4677 */
4678 boolean isDisplayedOnScreen() {
4679 // The first two checks are made also made by isShown() which
4680 // however traverses the tree up to the parent to catch that.
4681 // Therefore, we do some fail fast check to minimize the up
4682 // tree traversal.
4683 return (mAttachInfo != null
4684 && mAttachInfo.mWindowVisibility == View.VISIBLE
4685 && getAlpha() > 0
4686 && isShown()
4687 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4688 }
4689
4690 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004691 * Sets a delegate for implementing accessibility support via compositon as
4692 * opposed to inheritance. The delegate's primary use is for implementing
4693 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4694 *
4695 * @param delegate The delegate instance.
4696 *
4697 * @see AccessibilityDelegate
4698 */
4699 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4700 mAccessibilityDelegate = delegate;
4701 }
4702
4703 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004704 * Gets the provider for managing a virtual view hierarchy rooted at this View
4705 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4706 * that explore the window content.
4707 * <p>
4708 * If this method returns an instance, this instance is responsible for managing
4709 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4710 * View including the one representing the View itself. Similarly the returned
4711 * instance is responsible for performing accessibility actions on any virtual
4712 * view or the root view itself.
4713 * </p>
4714 * <p>
4715 * If an {@link AccessibilityDelegate} has been specified via calling
4716 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4717 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4718 * is responsible for handling this call.
4719 * </p>
4720 *
4721 * @return The provider.
4722 *
4723 * @see AccessibilityNodeProvider
4724 */
4725 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4726 if (mAccessibilityDelegate != null) {
4727 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4728 } else {
4729 return null;
4730 }
4731 }
4732
4733 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004734 * Gets the unique identifier of this view on the screen for accessibility purposes.
4735 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4736 *
4737 * @return The view accessibility id.
4738 *
4739 * @hide
4740 */
4741 public int getAccessibilityViewId() {
4742 if (mAccessibilityViewId == NO_ID) {
4743 mAccessibilityViewId = sNextAccessibilityViewId++;
4744 }
4745 return mAccessibilityViewId;
4746 }
4747
4748 /**
4749 * Gets the unique identifier of the window in which this View reseides.
4750 *
4751 * @return The window accessibility id.
4752 *
4753 * @hide
4754 */
4755 public int getAccessibilityWindowId() {
4756 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4757 }
4758
4759 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004760 * Gets the {@link View} description. It briefly describes the view and is
4761 * primarily used for accessibility support. Set this property to enable
4762 * better accessibility support for your application. This is especially
4763 * true for views that do not have textual representation (For example,
4764 * ImageButton).
4765 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004766 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004767 *
4768 * @attr ref android.R.styleable#View_contentDescription
4769 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004770 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004771 public CharSequence getContentDescription() {
4772 return mContentDescription;
4773 }
4774
4775 /**
4776 * Sets the {@link View} description. It briefly describes the view and is
4777 * primarily used for accessibility support. Set this property to enable
4778 * better accessibility support for your application. This is especially
4779 * true for views that do not have textual representation (For example,
4780 * ImageButton).
4781 *
4782 * @param contentDescription The content description.
4783 *
4784 * @attr ref android.R.styleable#View_contentDescription
4785 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004786 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004787 public void setContentDescription(CharSequence contentDescription) {
4788 mContentDescription = contentDescription;
4789 }
4790
4791 /**
Romain Guya2431d02009-04-30 16:30:00 -07004792 * Invoked whenever this view loses focus, either by losing window focus or by losing
4793 * focus within its window. This method can be used to clear any state tied to the
4794 * focus. For instance, if a button is held pressed with the trackball and the window
4795 * loses focus, this method can be used to cancel the press.
4796 *
4797 * Subclasses of View overriding this method should always call super.onFocusLost().
4798 *
4799 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004800 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004801 *
4802 * @hide pending API council approval
4803 */
4804 protected void onFocusLost() {
4805 resetPressedState();
4806 }
4807
4808 private void resetPressedState() {
4809 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4810 return;
4811 }
4812
4813 if (isPressed()) {
4814 setPressed(false);
4815
4816 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004817 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004818 }
4819 }
4820 }
4821
4822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 * Returns true if this view has focus
4824 *
4825 * @return True if this view has focus, false otherwise.
4826 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004827 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 public boolean isFocused() {
4829 return (mPrivateFlags & FOCUSED) != 0;
4830 }
4831
4832 /**
4833 * Find the view in the hierarchy rooted at this view that currently has
4834 * focus.
4835 *
4836 * @return The view that currently has focus, or null if no focused view can
4837 * be found.
4838 */
4839 public View findFocus() {
4840 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4841 }
4842
4843 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004844 * Indicates whether this view is one of the set of scrollable containers in
4845 * its window.
4846 *
4847 * @return whether this view is one of the set of scrollable containers in
4848 * its window
4849 *
4850 * @attr ref android.R.styleable#View_isScrollContainer
4851 */
4852 public boolean isScrollContainer() {
4853 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4854 }
4855
4856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 * Change whether this view is one of the set of scrollable containers in
4858 * its window. This will be used to determine whether the window can
4859 * resize or must pan when a soft input area is open -- scrollable
4860 * containers allow the window to use resize mode since the container
4861 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004862 *
4863 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 */
4865 public void setScrollContainer(boolean isScrollContainer) {
4866 if (isScrollContainer) {
4867 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4868 mAttachInfo.mScrollContainers.add(this);
4869 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4870 }
4871 mPrivateFlags |= SCROLL_CONTAINER;
4872 } else {
4873 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4874 mAttachInfo.mScrollContainers.remove(this);
4875 }
4876 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4877 }
4878 }
4879
4880 /**
4881 * Returns the quality of the drawing cache.
4882 *
4883 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4884 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4885 *
4886 * @see #setDrawingCacheQuality(int)
4887 * @see #setDrawingCacheEnabled(boolean)
4888 * @see #isDrawingCacheEnabled()
4889 *
4890 * @attr ref android.R.styleable#View_drawingCacheQuality
4891 */
4892 public int getDrawingCacheQuality() {
4893 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4894 }
4895
4896 /**
4897 * Set the drawing cache quality of this view. This value is used only when the
4898 * drawing cache is enabled
4899 *
4900 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4901 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4902 *
4903 * @see #getDrawingCacheQuality()
4904 * @see #setDrawingCacheEnabled(boolean)
4905 * @see #isDrawingCacheEnabled()
4906 *
4907 * @attr ref android.R.styleable#View_drawingCacheQuality
4908 */
4909 public void setDrawingCacheQuality(int quality) {
4910 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4911 }
4912
4913 /**
4914 * Returns whether the screen should remain on, corresponding to the current
4915 * value of {@link #KEEP_SCREEN_ON}.
4916 *
4917 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4918 *
4919 * @see #setKeepScreenOn(boolean)
4920 *
4921 * @attr ref android.R.styleable#View_keepScreenOn
4922 */
4923 public boolean getKeepScreenOn() {
4924 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4925 }
4926
4927 /**
4928 * Controls whether the screen should remain on, modifying the
4929 * value of {@link #KEEP_SCREEN_ON}.
4930 *
4931 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4932 *
4933 * @see #getKeepScreenOn()
4934 *
4935 * @attr ref android.R.styleable#View_keepScreenOn
4936 */
4937 public void setKeepScreenOn(boolean keepScreenOn) {
4938 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4939 }
4940
4941 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004942 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4943 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 *
4945 * @attr ref android.R.styleable#View_nextFocusLeft
4946 */
4947 public int getNextFocusLeftId() {
4948 return mNextFocusLeftId;
4949 }
4950
4951 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004952 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4953 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4954 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 *
4956 * @attr ref android.R.styleable#View_nextFocusLeft
4957 */
4958 public void setNextFocusLeftId(int nextFocusLeftId) {
4959 mNextFocusLeftId = nextFocusLeftId;
4960 }
4961
4962 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004963 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4964 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 *
4966 * @attr ref android.R.styleable#View_nextFocusRight
4967 */
4968 public int getNextFocusRightId() {
4969 return mNextFocusRightId;
4970 }
4971
4972 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004973 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4974 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4975 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 *
4977 * @attr ref android.R.styleable#View_nextFocusRight
4978 */
4979 public void setNextFocusRightId(int nextFocusRightId) {
4980 mNextFocusRightId = nextFocusRightId;
4981 }
4982
4983 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004984 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4985 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 *
4987 * @attr ref android.R.styleable#View_nextFocusUp
4988 */
4989 public int getNextFocusUpId() {
4990 return mNextFocusUpId;
4991 }
4992
4993 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004994 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4995 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4996 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 *
4998 * @attr ref android.R.styleable#View_nextFocusUp
4999 */
5000 public void setNextFocusUpId(int nextFocusUpId) {
5001 mNextFocusUpId = nextFocusUpId;
5002 }
5003
5004 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005005 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5006 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 *
5008 * @attr ref android.R.styleable#View_nextFocusDown
5009 */
5010 public int getNextFocusDownId() {
5011 return mNextFocusDownId;
5012 }
5013
5014 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005015 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5016 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5017 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 *
5019 * @attr ref android.R.styleable#View_nextFocusDown
5020 */
5021 public void setNextFocusDownId(int nextFocusDownId) {
5022 mNextFocusDownId = nextFocusDownId;
5023 }
5024
5025 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005026 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5027 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5028 *
5029 * @attr ref android.R.styleable#View_nextFocusForward
5030 */
5031 public int getNextFocusForwardId() {
5032 return mNextFocusForwardId;
5033 }
5034
5035 /**
5036 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5037 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5038 * decide automatically.
5039 *
5040 * @attr ref android.R.styleable#View_nextFocusForward
5041 */
5042 public void setNextFocusForwardId(int nextFocusForwardId) {
5043 mNextFocusForwardId = nextFocusForwardId;
5044 }
5045
5046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 * Returns the visibility of this view and all of its ancestors
5048 *
5049 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5050 */
5051 public boolean isShown() {
5052 View current = this;
5053 //noinspection ConstantConditions
5054 do {
5055 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5056 return false;
5057 }
5058 ViewParent parent = current.mParent;
5059 if (parent == null) {
5060 return false; // We are not attached to the view root
5061 }
5062 if (!(parent instanceof View)) {
5063 return true;
5064 }
5065 current = (View) parent;
5066 } while (current != null);
5067
5068 return false;
5069 }
5070
5071 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005072 * Called by the view hierarchy when the content insets for a window have
5073 * changed, to allow it to adjust its content to fit within those windows.
5074 * The content insets tell you the space that the status bar, input method,
5075 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005077 * <p>You do not normally need to deal with this function, since the default
5078 * window decoration given to applications takes care of applying it to the
5079 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5080 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5081 * and your content can be placed under those system elements. You can then
5082 * use this method within your view hierarchy if you have parts of your UI
5083 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005085 * <p>The default implementation of this method simply applies the content
5086 * inset's to the view's padding. This can be enabled through
5087 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5088 * the method and handle the insets however you would like. Note that the
5089 * insets provided by the framework are always relative to the far edges
5090 * of the window, not accounting for the location of the called view within
5091 * that window. (In fact when this method is called you do not yet know
5092 * where the layout will place the view, as it is done before layout happens.)
5093 *
5094 * <p>Note: unlike many View methods, there is no dispatch phase to this
5095 * call. If you are overriding it in a ViewGroup and want to allow the
5096 * call to continue to your children, you must be sure to call the super
5097 * implementation.
5098 *
5099 * @param insets Current content insets of the window. Prior to
5100 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5101 * the insets or else you and Android will be unhappy.
5102 *
5103 * @return Return true if this view applied the insets and it should not
5104 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 */
5106 protected boolean fitSystemWindows(Rect insets) {
5107 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005108 mUserPaddingStart = -1;
5109 mUserPaddingEnd = -1;
5110 mUserPaddingRelative = false;
5111 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5112 || mAttachInfo == null
5113 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5114 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5115 return true;
5116 } else {
5117 internalSetPadding(0, 0, 0, 0);
5118 return false;
5119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 }
5121 return false;
5122 }
5123
5124 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005125 * Set whether or not this view should account for system screen decorations
5126 * such as the status bar and inset its content. This allows this view to be
5127 * positioned in absolute screen coordinates and remain visible to the user.
5128 *
5129 * <p>This should only be used by top-level window decor views.
5130 *
5131 * @param fitSystemWindows true to inset content for system screen decorations, false for
5132 * default behavior.
5133 *
5134 * @attr ref android.R.styleable#View_fitsSystemWindows
5135 */
5136 public void setFitsSystemWindows(boolean fitSystemWindows) {
5137 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5138 }
5139
5140 /**
5141 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5142 * will account for system screen decorations such as the status bar and inset its
5143 * content. This allows the view to be positioned in absolute screen coordinates
5144 * and remain visible to the user.
5145 *
5146 * @return true if this view will adjust its content bounds for system screen decorations.
5147 *
5148 * @attr ref android.R.styleable#View_fitsSystemWindows
5149 */
5150 public boolean fitsSystemWindows() {
5151 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5152 }
5153
5154 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005155 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5156 */
5157 public void requestFitSystemWindows() {
5158 if (mParent != null) {
5159 mParent.requestFitSystemWindows();
5160 }
5161 }
5162
5163 /**
5164 * For use by PhoneWindow to make its own system window fitting optional.
5165 * @hide
5166 */
5167 public void makeOptionalFitsSystemWindows() {
5168 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5169 }
5170
5171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 * Returns the visibility status for this view.
5173 *
5174 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5175 * @attr ref android.R.styleable#View_visibility
5176 */
5177 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005178 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5179 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5180 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 })
5182 public int getVisibility() {
5183 return mViewFlags & VISIBILITY_MASK;
5184 }
5185
5186 /**
5187 * Set the enabled state of this view.
5188 *
5189 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5190 * @attr ref android.R.styleable#View_visibility
5191 */
5192 @RemotableViewMethod
5193 public void setVisibility(int visibility) {
5194 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005195 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 }
5197
5198 /**
5199 * Returns the enabled status for this view. The interpretation of the
5200 * enabled state varies by subclass.
5201 *
5202 * @return True if this view is enabled, false otherwise.
5203 */
5204 @ViewDebug.ExportedProperty
5205 public boolean isEnabled() {
5206 return (mViewFlags & ENABLED_MASK) == ENABLED;
5207 }
5208
5209 /**
5210 * Set the enabled state of this view. The interpretation of the enabled
5211 * state varies by subclass.
5212 *
5213 * @param enabled True if this view is enabled, false otherwise.
5214 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005215 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005217 if (enabled == isEnabled()) return;
5218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5220
5221 /*
5222 * The View most likely has to change its appearance, so refresh
5223 * the drawable state.
5224 */
5225 refreshDrawableState();
5226
5227 // Invalidate too, since the default behavior for views is to be
5228 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005229 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 }
5231
5232 /**
5233 * Set whether this view can receive the focus.
5234 *
5235 * Setting this to false will also ensure that this view is not focusable
5236 * in touch mode.
5237 *
5238 * @param focusable If true, this view can receive the focus.
5239 *
5240 * @see #setFocusableInTouchMode(boolean)
5241 * @attr ref android.R.styleable#View_focusable
5242 */
5243 public void setFocusable(boolean focusable) {
5244 if (!focusable) {
5245 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5246 }
5247 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5248 }
5249
5250 /**
5251 * Set whether this view can receive focus while in touch mode.
5252 *
5253 * Setting this to true will also ensure that this view is focusable.
5254 *
5255 * @param focusableInTouchMode If true, this view can receive the focus while
5256 * in touch mode.
5257 *
5258 * @see #setFocusable(boolean)
5259 * @attr ref android.R.styleable#View_focusableInTouchMode
5260 */
5261 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5262 // Focusable in touch mode should always be set before the focusable flag
5263 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5264 // which, in touch mode, will not successfully request focus on this view
5265 // because the focusable in touch mode flag is not set
5266 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5267 if (focusableInTouchMode) {
5268 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5269 }
5270 }
5271
5272 /**
5273 * Set whether this view should have sound effects enabled for events such as
5274 * clicking and touching.
5275 *
5276 * <p>You may wish to disable sound effects for a view if you already play sounds,
5277 * for instance, a dial key that plays dtmf tones.
5278 *
5279 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5280 * @see #isSoundEffectsEnabled()
5281 * @see #playSoundEffect(int)
5282 * @attr ref android.R.styleable#View_soundEffectsEnabled
5283 */
5284 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5285 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5286 }
5287
5288 /**
5289 * @return whether this view should have sound effects enabled for events such as
5290 * clicking and touching.
5291 *
5292 * @see #setSoundEffectsEnabled(boolean)
5293 * @see #playSoundEffect(int)
5294 * @attr ref android.R.styleable#View_soundEffectsEnabled
5295 */
5296 @ViewDebug.ExportedProperty
5297 public boolean isSoundEffectsEnabled() {
5298 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5299 }
5300
5301 /**
5302 * Set whether this view should have haptic feedback for events such as
5303 * long presses.
5304 *
5305 * <p>You may wish to disable haptic feedback if your view already controls
5306 * its own haptic feedback.
5307 *
5308 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5309 * @see #isHapticFeedbackEnabled()
5310 * @see #performHapticFeedback(int)
5311 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5312 */
5313 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5314 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5315 }
5316
5317 /**
5318 * @return whether this view should have haptic feedback enabled for events
5319 * long presses.
5320 *
5321 * @see #setHapticFeedbackEnabled(boolean)
5322 * @see #performHapticFeedback(int)
5323 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5324 */
5325 @ViewDebug.ExportedProperty
5326 public boolean isHapticFeedbackEnabled() {
5327 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5328 }
5329
5330 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005331 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005332 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005333 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5334 * {@link #LAYOUT_DIRECTION_RTL},
5335 * {@link #LAYOUT_DIRECTION_INHERIT} or
5336 * {@link #LAYOUT_DIRECTION_LOCALE}.
5337 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005338 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005339 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005340 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5341 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5342 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5343 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005344 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005345 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005346 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005347 }
5348
5349 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005350 * Set the layout direction for this view. This will propagate a reset of layout direction
5351 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005352 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005353 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5354 * {@link #LAYOUT_DIRECTION_RTL},
5355 * {@link #LAYOUT_DIRECTION_INHERIT} or
5356 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005357 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005358 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005359 */
5360 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005361 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005362 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005363 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005364 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005365 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005366 // Set the new layout direction (filtered) and ask for a layout pass
5367 mPrivateFlags2 |=
5368 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5369 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005370 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005371 }
5372
5373 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005374 * Returns the resolved layout direction for this view.
5375 *
5376 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005377 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005378 */
5379 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005380 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5381 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005382 })
5383 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005384 // The layout diretion will be resolved only if needed
5385 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5386 resolveLayoutDirection();
5387 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005388 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005389 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5390 }
5391
5392 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005393 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5394 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005395 *
5396 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005397 */
5398 @ViewDebug.ExportedProperty(category = "layout")
5399 public boolean isLayoutRtl() {
5400 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5401 }
5402
5403 /**
Adam Powell539ee872012-02-03 19:00:49 -08005404 * Indicates whether the view is currently tracking transient state that the
5405 * app should not need to concern itself with saving and restoring, but that
5406 * the framework should take special note to preserve when possible.
5407 *
5408 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005409 */
5410 @ViewDebug.ExportedProperty(category = "layout")
5411 public boolean hasTransientState() {
5412 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5413 }
5414
5415 /**
5416 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005417 * framework should attempt to preserve when possible. This flag is reference counted,
5418 * so every call to setHasTransientState(true) should be paired with a later call
5419 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005420 *
5421 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005422 */
5423 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005424 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5425 mTransientStateCount - 1;
5426 if (mTransientStateCount < 0) {
5427 mTransientStateCount = 0;
5428 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5429 "unmatched pair of setHasTransientState calls");
5430 }
5431 if ((hasTransientState && mTransientStateCount == 1) ||
5432 (hasTransientState && mTransientStateCount == 0)) {
5433 // update flag if we've just incremented up from 0 or decremented down to 0
5434 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5435 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5436 if (mParent != null) {
5437 try {
5438 mParent.childHasTransientStateChanged(this, hasTransientState);
5439 } catch (AbstractMethodError e) {
5440 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5441 " does not fully implement ViewParent", e);
5442 }
Adam Powell539ee872012-02-03 19:00:49 -08005443 }
5444 }
5445 }
5446
5447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 * If this view doesn't do any drawing on its own, set this flag to
5449 * allow further optimizations. By default, this flag is not set on
5450 * View, but could be set on some View subclasses such as ViewGroup.
5451 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005452 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5453 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 *
5455 * @param willNotDraw whether or not this View draw on its own
5456 */
5457 public void setWillNotDraw(boolean willNotDraw) {
5458 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5459 }
5460
5461 /**
5462 * Returns whether or not this View draws on its own.
5463 *
5464 * @return true if this view has nothing to draw, false otherwise
5465 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005466 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 public boolean willNotDraw() {
5468 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5469 }
5470
5471 /**
5472 * When a View's drawing cache is enabled, drawing is redirected to an
5473 * offscreen bitmap. Some views, like an ImageView, must be able to
5474 * bypass this mechanism if they already draw a single bitmap, to avoid
5475 * unnecessary usage of the memory.
5476 *
5477 * @param willNotCacheDrawing true if this view does not cache its
5478 * drawing, false otherwise
5479 */
5480 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5481 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5482 }
5483
5484 /**
5485 * Returns whether or not this View can cache its drawing or not.
5486 *
5487 * @return true if this view does not cache its drawing, false otherwise
5488 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005489 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 public boolean willNotCacheDrawing() {
5491 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5492 }
5493
5494 /**
5495 * Indicates whether this view reacts to click events or not.
5496 *
5497 * @return true if the view is clickable, false otherwise
5498 *
5499 * @see #setClickable(boolean)
5500 * @attr ref android.R.styleable#View_clickable
5501 */
5502 @ViewDebug.ExportedProperty
5503 public boolean isClickable() {
5504 return (mViewFlags & CLICKABLE) == CLICKABLE;
5505 }
5506
5507 /**
5508 * Enables or disables click events for this view. When a view
5509 * is clickable it will change its state to "pressed" on every click.
5510 * Subclasses should set the view clickable to visually react to
5511 * user's clicks.
5512 *
5513 * @param clickable true to make the view clickable, false otherwise
5514 *
5515 * @see #isClickable()
5516 * @attr ref android.R.styleable#View_clickable
5517 */
5518 public void setClickable(boolean clickable) {
5519 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5520 }
5521
5522 /**
5523 * Indicates whether this view reacts to long click events or not.
5524 *
5525 * @return true if the view is long clickable, false otherwise
5526 *
5527 * @see #setLongClickable(boolean)
5528 * @attr ref android.R.styleable#View_longClickable
5529 */
5530 public boolean isLongClickable() {
5531 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5532 }
5533
5534 /**
5535 * Enables or disables long click events for this view. When a view is long
5536 * clickable it reacts to the user holding down the button for a longer
5537 * duration than a tap. This event can either launch the listener or a
5538 * context menu.
5539 *
5540 * @param longClickable true to make the view long clickable, false otherwise
5541 * @see #isLongClickable()
5542 * @attr ref android.R.styleable#View_longClickable
5543 */
5544 public void setLongClickable(boolean longClickable) {
5545 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5546 }
5547
5548 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005549 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 *
5551 * @see #isClickable()
5552 * @see #setClickable(boolean)
5553 *
5554 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5555 * the View's internal state from a previously set "pressed" state.
5556 */
5557 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005558 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 if (pressed) {
5561 mPrivateFlags |= PRESSED;
5562 } else {
5563 mPrivateFlags &= ~PRESSED;
5564 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005565
5566 if (needsRefresh) {
5567 refreshDrawableState();
5568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 dispatchSetPressed(pressed);
5570 }
5571
5572 /**
5573 * Dispatch setPressed to all of this View's children.
5574 *
5575 * @see #setPressed(boolean)
5576 *
5577 * @param pressed The new pressed state
5578 */
5579 protected void dispatchSetPressed(boolean pressed) {
5580 }
5581
5582 /**
5583 * Indicates whether the view is currently in pressed state. Unless
5584 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5585 * the pressed state.
5586 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005587 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 * @see #isClickable()
5589 * @see #setClickable(boolean)
5590 *
5591 * @return true if the view is currently pressed, false otherwise
5592 */
5593 public boolean isPressed() {
5594 return (mPrivateFlags & PRESSED) == PRESSED;
5595 }
5596
5597 /**
5598 * Indicates whether this view will save its state (that is,
5599 * whether its {@link #onSaveInstanceState} method will be called).
5600 *
5601 * @return Returns true if the view state saving is enabled, else false.
5602 *
5603 * @see #setSaveEnabled(boolean)
5604 * @attr ref android.R.styleable#View_saveEnabled
5605 */
5606 public boolean isSaveEnabled() {
5607 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5608 }
5609
5610 /**
5611 * Controls whether the saving of this view's state is
5612 * enabled (that is, whether its {@link #onSaveInstanceState} method
5613 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005614 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 * for its state to be saved. This flag can only disable the
5616 * saving of this view; any child views may still have their state saved.
5617 *
5618 * @param enabled Set to false to <em>disable</em> state saving, or true
5619 * (the default) to allow it.
5620 *
5621 * @see #isSaveEnabled()
5622 * @see #setId(int)
5623 * @see #onSaveInstanceState()
5624 * @attr ref android.R.styleable#View_saveEnabled
5625 */
5626 public void setSaveEnabled(boolean enabled) {
5627 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5628 }
5629
Jeff Brown85a31762010-09-01 17:01:00 -07005630 /**
5631 * Gets whether the framework should discard touches when the view's
5632 * window is obscured by another visible window.
5633 * Refer to the {@link View} security documentation for more details.
5634 *
5635 * @return True if touch filtering is enabled.
5636 *
5637 * @see #setFilterTouchesWhenObscured(boolean)
5638 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5639 */
5640 @ViewDebug.ExportedProperty
5641 public boolean getFilterTouchesWhenObscured() {
5642 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5643 }
5644
5645 /**
5646 * Sets whether the framework should discard touches when the view's
5647 * window is obscured by another visible window.
5648 * Refer to the {@link View} security documentation for more details.
5649 *
5650 * @param enabled True if touch filtering should be enabled.
5651 *
5652 * @see #getFilterTouchesWhenObscured
5653 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5654 */
5655 public void setFilterTouchesWhenObscured(boolean enabled) {
5656 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5657 FILTER_TOUCHES_WHEN_OBSCURED);
5658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659
5660 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005661 * Indicates whether the entire hierarchy under this view will save its
5662 * state when a state saving traversal occurs from its parent. The default
5663 * is true; if false, these views will not be saved unless
5664 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5665 *
5666 * @return Returns true if the view state saving from parent is enabled, else false.
5667 *
5668 * @see #setSaveFromParentEnabled(boolean)
5669 */
5670 public boolean isSaveFromParentEnabled() {
5671 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5672 }
5673
5674 /**
5675 * Controls whether the entire hierarchy under this view will save its
5676 * state when a state saving traversal occurs from its parent. The default
5677 * is true; if false, these views will not be saved unless
5678 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5679 *
5680 * @param enabled Set to false to <em>disable</em> state saving, or true
5681 * (the default) to allow it.
5682 *
5683 * @see #isSaveFromParentEnabled()
5684 * @see #setId(int)
5685 * @see #onSaveInstanceState()
5686 */
5687 public void setSaveFromParentEnabled(boolean enabled) {
5688 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5689 }
5690
5691
5692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 * Returns whether this View is able to take focus.
5694 *
5695 * @return True if this view can take focus, or false otherwise.
5696 * @attr ref android.R.styleable#View_focusable
5697 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005698 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 public final boolean isFocusable() {
5700 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5701 }
5702
5703 /**
5704 * When a view is focusable, it may not want to take focus when in touch mode.
5705 * For example, a button would like focus when the user is navigating via a D-pad
5706 * so that the user can click on it, but once the user starts touching the screen,
5707 * the button shouldn't take focus
5708 * @return Whether the view is focusable in touch mode.
5709 * @attr ref android.R.styleable#View_focusableInTouchMode
5710 */
5711 @ViewDebug.ExportedProperty
5712 public final boolean isFocusableInTouchMode() {
5713 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5714 }
5715
5716 /**
5717 * Find the nearest view in the specified direction that can take focus.
5718 * This does not actually give focus to that view.
5719 *
5720 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5721 *
5722 * @return The nearest focusable in the specified direction, or null if none
5723 * can be found.
5724 */
5725 public View focusSearch(int direction) {
5726 if (mParent != null) {
5727 return mParent.focusSearch(this, direction);
5728 } else {
5729 return null;
5730 }
5731 }
5732
5733 /**
5734 * This method is the last chance for the focused view and its ancestors to
5735 * respond to an arrow key. This is called when the focused view did not
5736 * consume the key internally, nor could the view system find a new view in
5737 * the requested direction to give focus to.
5738 *
5739 * @param focused The currently focused view.
5740 * @param direction The direction focus wants to move. One of FOCUS_UP,
5741 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5742 * @return True if the this view consumed this unhandled move.
5743 */
5744 public boolean dispatchUnhandledMove(View focused, int direction) {
5745 return false;
5746 }
5747
5748 /**
5749 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005750 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005752 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5753 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 * @return The user specified next view, or null if there is none.
5755 */
5756 View findUserSetNextFocus(View root, int direction) {
5757 switch (direction) {
5758 case FOCUS_LEFT:
5759 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005760 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 case FOCUS_RIGHT:
5762 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005763 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 case FOCUS_UP:
5765 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005766 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 case FOCUS_DOWN:
5768 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005769 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005770 case FOCUS_FORWARD:
5771 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005772 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005773 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005774 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005775 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005776 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005777 @Override
5778 public boolean apply(View t) {
5779 return t.mNextFocusForwardId == id;
5780 }
5781 });
5782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 }
5784 return null;
5785 }
5786
Jeff Brown4dfbec22011-08-15 14:55:37 -07005787 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5788 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5789 @Override
5790 public boolean apply(View t) {
5791 return t.mID == childViewId;
5792 }
5793 });
5794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 if (result == null) {
5796 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5797 + "by user for id " + childViewId);
5798 }
5799 return result;
5800 }
5801
5802 /**
5803 * Find and return all focusable views that are descendants of this view,
5804 * possibly including this view if it is focusable itself.
5805 *
5806 * @param direction The direction of the focus
5807 * @return A list of focusable views
5808 */
5809 public ArrayList<View> getFocusables(int direction) {
5810 ArrayList<View> result = new ArrayList<View>(24);
5811 addFocusables(result, direction);
5812 return result;
5813 }
5814
5815 /**
5816 * Add any focusable views that are descendants of this view (possibly
5817 * including this view if it is focusable itself) to views. If we are in touch mode,
5818 * only add views that are also focusable in touch mode.
5819 *
5820 * @param views Focusable views found so far
5821 * @param direction The direction of the focus
5822 */
5823 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005824 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826
svetoslavganov75986cf2009-05-14 22:28:01 -07005827 /**
5828 * Adds any focusable views that are descendants of this view (possibly
5829 * including this view if it is focusable itself) to views. This method
5830 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005831 * only views focusable in touch mode if we are in touch mode or
5832 * only views that can take accessibility focus if accessibility is enabeld
5833 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005834 *
5835 * @param views Focusable views found so far or null if all we are interested is
5836 * the number of focusables.
5837 * @param direction The direction of the focus.
5838 * @param focusableMode The type of focusables to be added.
5839 *
5840 * @see #FOCUSABLES_ALL
5841 * @see #FOCUSABLES_TOUCH_MODE
5842 */
5843 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005844 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005845 return;
5846 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005847 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5848 if (AccessibilityManager.getInstance(mContext).isEnabled()
5849 && includeForAccessibility()) {
5850 views.add(this);
5851 return;
5852 }
5853 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005854 if (!isFocusable()) {
5855 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005856 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005857 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5858 && isInTouchMode() && !isFocusableInTouchMode()) {
5859 return;
5860 }
5861 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 }
5863
5864 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005865 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005866 * The search is performed by either the text that the View renders or the content
5867 * description that describes the view for accessibility purposes and the view does
5868 * not render or both. Clients can specify how the search is to be performed via
5869 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5870 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005871 *
5872 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005873 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005874 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005875 * @see #FIND_VIEWS_WITH_TEXT
5876 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5877 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005878 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005879 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005880 if (getAccessibilityNodeProvider() != null) {
5881 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5882 outViews.add(this);
5883 }
5884 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5885 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005886 String searchedLowerCase = searched.toString().toLowerCase();
5887 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5888 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5889 outViews.add(this);
5890 }
5891 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005892 }
5893
5894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 * Find and return all touchable views that are descendants of this view,
5896 * possibly including this view if it is touchable itself.
5897 *
5898 * @return A list of touchable views
5899 */
5900 public ArrayList<View> getTouchables() {
5901 ArrayList<View> result = new ArrayList<View>();
5902 addTouchables(result);
5903 return result;
5904 }
5905
5906 /**
5907 * Add any touchable views that are descendants of this view (possibly
5908 * including this view if it is touchable itself) to views.
5909 *
5910 * @param views Touchable views found so far
5911 */
5912 public void addTouchables(ArrayList<View> views) {
5913 final int viewFlags = mViewFlags;
5914
5915 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5916 && (viewFlags & ENABLED_MASK) == ENABLED) {
5917 views.add(this);
5918 }
5919 }
5920
5921 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005922 * Returns whether this View is accessibility focused.
5923 *
5924 * @return True if this View is accessibility focused.
5925 */
5926 boolean isAccessibilityFocused() {
5927 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5928 }
5929
5930 /**
5931 * Call this to try to give accessibility focus to this view.
5932 *
5933 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5934 * returns false or the view is no visible or the view already has accessibility
5935 * focus.
5936 *
5937 * See also {@link #focusSearch(int)}, which is what you call to say that you
5938 * have focus, and you want your parent to look for the next one.
5939 *
5940 * @return Whether this view actually took accessibility focus.
5941 *
5942 * @hide
5943 */
5944 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07005945 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
5946 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005947 return false;
5948 }
5949 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5950 return false;
5951 }
5952 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5953 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5954 ViewRootImpl viewRootImpl = getViewRootImpl();
5955 if (viewRootImpl != null) {
5956 viewRootImpl.setAccessibilityFocusedHost(this);
5957 }
5958 invalidate();
5959 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5960 notifyAccessibilityStateChanged();
5961 // Try to give input focus to this view - not a descendant.
5962 requestFocusNoSearch(View.FOCUS_DOWN, null);
5963 return true;
5964 }
5965 return false;
5966 }
5967
5968 /**
5969 * Call this to try to clear accessibility focus of this view.
5970 *
5971 * See also {@link #focusSearch(int)}, which is what you call to say that you
5972 * have focus, and you want your parent to look for the next one.
5973 *
5974 * @hide
5975 */
5976 public void clearAccessibilityFocus() {
5977 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5978 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5979 ViewRootImpl viewRootImpl = getViewRootImpl();
5980 if (viewRootImpl != null) {
5981 viewRootImpl.setAccessibilityFocusedHost(null);
5982 }
5983 invalidate();
5984 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5985 notifyAccessibilityStateChanged();
5986 // Try to move accessibility focus to the input focus.
5987 View rootView = getRootView();
5988 if (rootView != null) {
5989 View inputFocus = rootView.findFocus();
5990 if (inputFocus != null) {
5991 inputFocus.requestAccessibilityFocus();
5992 }
5993 }
5994 }
5995 }
5996
5997 /**
5998 * Find the best view to take accessibility focus from a hover.
5999 * This function finds the deepest actionable view and if that
6000 * fails ask the parent to take accessibility focus from hover.
6001 *
6002 * @param x The X hovered location in this view coorditantes.
6003 * @param y The Y hovered location in this view coorditantes.
6004 * @return Whether the request was handled.
6005 *
6006 * @hide
6007 */
6008 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6009 if (onRequestAccessibilityFocusFromHover(x, y)) {
6010 return true;
6011 }
6012 ViewParent parent = mParent;
6013 if (parent instanceof View) {
6014 View parentView = (View) parent;
6015
6016 float[] position = mAttachInfo.mTmpTransformLocation;
6017 position[0] = x;
6018 position[1] = y;
6019
6020 // Compensate for the transformation of the current matrix.
6021 if (!hasIdentityMatrix()) {
6022 getMatrix().mapPoints(position);
6023 }
6024
6025 // Compensate for the parent scroll and the offset
6026 // of this view stop from the parent top.
6027 position[0] += mLeft - parentView.mScrollX;
6028 position[1] += mTop - parentView.mScrollY;
6029
6030 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6031 }
6032 return false;
6033 }
6034
6035 /**
6036 * Requests to give this View focus from hover.
6037 *
6038 * @param x The X hovered location in this view coorditantes.
6039 * @param y The Y hovered location in this view coorditantes.
6040 * @return Whether the request was handled.
6041 *
6042 * @hide
6043 */
6044 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6045 if (includeForAccessibility()
6046 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6047 return requestAccessibilityFocus();
6048 }
6049 return false;
6050 }
6051
6052 /**
6053 * Clears accessibility focus without calling any callback methods
6054 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6055 * is used for clearing accessibility focus when giving this focus to
6056 * another view.
6057 */
6058 void clearAccessibilityFocusNoCallbacks() {
6059 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6060 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6061 invalidate();
6062 }
6063 }
6064
6065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 * Call this to try to give focus to a specific view or to one of its
6067 * descendants.
6068 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006069 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6070 * false), or if it is focusable and it is not focusable in touch mode
6071 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006073 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 * have focus, and you want your parent to look for the next one.
6075 *
6076 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6077 * {@link #FOCUS_DOWN} and <code>null</code>.
6078 *
6079 * @return Whether this view or one of its descendants actually took focus.
6080 */
6081 public final boolean requestFocus() {
6082 return requestFocus(View.FOCUS_DOWN);
6083 }
6084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 /**
6086 * Call this to try to give focus to a specific view or to one of its
6087 * descendants and give it a hint about what direction focus is heading.
6088 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006089 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6090 * false), or if it is focusable and it is not focusable in touch mode
6091 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006093 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 * have focus, and you want your parent to look for the next one.
6095 *
6096 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6097 * <code>null</code> set for the previously focused rectangle.
6098 *
6099 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6100 * @return Whether this view or one of its descendants actually took focus.
6101 */
6102 public final boolean requestFocus(int direction) {
6103 return requestFocus(direction, null);
6104 }
6105
6106 /**
6107 * Call this to try to give focus to a specific view or to one of its descendants
6108 * and give it hints about the direction and a specific rectangle that the focus
6109 * is coming from. The rectangle can help give larger views a finer grained hint
6110 * about where focus is coming from, and therefore, where to show selection, or
6111 * forward focus change internally.
6112 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006113 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6114 * false), or if it is focusable and it is not focusable in touch mode
6115 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 *
6117 * A View will not take focus if it is not visible.
6118 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006119 * A View will not take focus if one of its parents has
6120 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6121 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006123 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 * have focus, and you want your parent to look for the next one.
6125 *
6126 * You may wish to override this method if your custom {@link View} has an internal
6127 * {@link View} that it wishes to forward the request to.
6128 *
6129 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6130 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6131 * to give a finer grained hint about where focus is coming from. May be null
6132 * if there is no hint.
6133 * @return Whether this view or one of its descendants actually took focus.
6134 */
6135 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006136 return requestFocusNoSearch(direction, previouslyFocusedRect);
6137 }
6138
6139 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140 // need to be focusable
6141 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6142 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6143 return false;
6144 }
6145
6146 // need to be focusable in touch mode if in touch mode
6147 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006148 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6149 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 }
6151
6152 // need to not have any parents blocking us
6153 if (hasAncestorThatBlocksDescendantFocus()) {
6154 return false;
6155 }
6156
6157 handleFocusGainInternal(direction, previouslyFocusedRect);
6158 return true;
6159 }
6160
6161 /**
6162 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6163 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6164 * touch mode to request focus when they are touched.
6165 *
6166 * @return Whether this view or one of its descendants actually took focus.
6167 *
6168 * @see #isInTouchMode()
6169 *
6170 */
6171 public final boolean requestFocusFromTouch() {
6172 // Leave touch mode if we need to
6173 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006174 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006175 if (viewRoot != null) {
6176 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 }
6178 }
6179 return requestFocus(View.FOCUS_DOWN);
6180 }
6181
6182 /**
6183 * @return Whether any ancestor of this view blocks descendant focus.
6184 */
6185 private boolean hasAncestorThatBlocksDescendantFocus() {
6186 ViewParent ancestor = mParent;
6187 while (ancestor instanceof ViewGroup) {
6188 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6189 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6190 return true;
6191 } else {
6192 ancestor = vgAncestor.getParent();
6193 }
6194 }
6195 return false;
6196 }
6197
6198 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006199 * Gets the mode for determining whether this View is important for accessibility
6200 * which is if it fires accessibility events and if it is reported to
6201 * accessibility services that query the screen.
6202 *
6203 * @return The mode for determining whether a View is important for accessibility.
6204 *
6205 * @attr ref android.R.styleable#View_importantForAccessibility
6206 *
6207 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6208 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6209 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6210 */
6211 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6212 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6213 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6214 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6215 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6216 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6217 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6218 })
6219 public int getImportantForAccessibility() {
6220 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6221 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6222 }
6223
6224 /**
6225 * Sets how to determine whether this view is important for accessibility
6226 * which is if it fires accessibility events and if it is reported to
6227 * accessibility services that query the screen.
6228 *
6229 * @param mode How to determine whether this view is important for accessibility.
6230 *
6231 * @attr ref android.R.styleable#View_importantForAccessibility
6232 *
6233 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6234 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6235 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6236 */
6237 public void setImportantForAccessibility(int mode) {
6238 if (mode != getImportantForAccessibility()) {
6239 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6240 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6241 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6242 notifyAccessibilityStateChanged();
6243 }
6244 }
6245
6246 /**
6247 * Gets whether this view should be exposed for accessibility.
6248 *
6249 * @return Whether the view is exposed for accessibility.
6250 *
6251 * @hide
6252 */
6253 public boolean isImportantForAccessibility() {
6254 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6255 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6256 switch (mode) {
6257 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6258 return true;
6259 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6260 return false;
6261 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6262 return isActionableForAccessibility() || hasListenersForAccessibility();
6263 default:
6264 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6265 + mode);
6266 }
6267 }
6268
6269 /**
6270 * Gets the parent for accessibility purposes. Note that the parent for
6271 * accessibility is not necessary the immediate parent. It is the first
6272 * predecessor that is important for accessibility.
6273 *
6274 * @return The parent for accessibility purposes.
6275 */
6276 public ViewParent getParentForAccessibility() {
6277 if (mParent instanceof View) {
6278 View parentView = (View) mParent;
6279 if (parentView.includeForAccessibility()) {
6280 return mParent;
6281 } else {
6282 return mParent.getParentForAccessibility();
6283 }
6284 }
6285 return null;
6286 }
6287
6288 /**
6289 * Adds the children of a given View for accessibility. Since some Views are
6290 * not important for accessibility the children for accessibility are not
6291 * necessarily direct children of the riew, rather they are the first level of
6292 * descendants important for accessibility.
6293 *
6294 * @param children The list of children for accessibility.
6295 */
6296 public void addChildrenForAccessibility(ArrayList<View> children) {
6297 if (includeForAccessibility()) {
6298 children.add(this);
6299 }
6300 }
6301
6302 /**
6303 * Whether to regard this view for accessibility. A view is regarded for
6304 * accessibility if it is important for accessibility or the querying
6305 * accessibility service has explicitly requested that view not
6306 * important for accessibility are regarded.
6307 *
6308 * @return Whether to regard the view for accessibility.
6309 */
6310 boolean includeForAccessibility() {
6311 if (mAttachInfo != null) {
6312 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006313 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006314 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006315 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006316 }
6317 }
6318 return false;
6319 }
6320
6321 /**
6322 * Returns whether the View is considered actionable from
6323 * accessibility perspective. Such view are important for
6324 * accessiiblity.
6325 *
6326 * @return True if the view is actionable for accessibility.
6327 */
6328 private boolean isActionableForAccessibility() {
6329 return (isClickable() || isLongClickable() || isFocusable());
6330 }
6331
6332 /**
6333 * Returns whether the View has registered callbacks wich makes it
6334 * important for accessiiblity.
6335 *
6336 * @return True if the view is actionable for accessibility.
6337 */
6338 private boolean hasListenersForAccessibility() {
6339 ListenerInfo info = getListenerInfo();
6340 return mTouchDelegate != null || info.mOnKeyListener != null
6341 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6342 || info.mOnHoverListener != null || info.mOnDragListener != null;
6343 }
6344
6345 /**
6346 * Notifies accessibility services that some view's important for
6347 * accessibility state has changed. Note that such notifications
6348 * are made at most once every
6349 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6350 * to avoid unnecessary load to the system. Also once a view has
6351 * made a notifucation this method is a NOP until the notification has
6352 * been sent to clients.
6353 *
6354 * @hide
6355 *
6356 * TODO: Makse sure this method is called for any view state change
6357 * that is interesting for accessilility purposes.
6358 */
6359 public void notifyAccessibilityStateChanged() {
6360 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6361 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6362 if (mParent != null) {
6363 mParent.childAccessibilityStateChanged(this);
6364 }
6365 }
6366 }
6367
6368 /**
6369 * Reset the state indicating the this view has requested clients
6370 * interested in its accessiblity state to be notified.
6371 *
6372 * @hide
6373 */
6374 public void resetAccessibilityStateChanged() {
6375 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6376 }
6377
6378 /**
6379 * Performs the specified accessibility action on the view. For
6380 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6381 *
6382 * @param action The action to perform.
6383 * @return Whether the action was performed.
6384 */
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07006385 public boolean performAccessibilityAction(int action, Bundle args) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006386 switch (action) {
6387 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006388 if (isClickable()) {
6389 performClick();
6390 }
6391 } break;
6392 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6393 if (isLongClickable()) {
6394 performLongClick();
6395 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006396 } break;
6397 case AccessibilityNodeInfo.ACTION_FOCUS: {
6398 if (!hasFocus()) {
6399 // Get out of touch mode since accessibility
6400 // wants to move focus around.
6401 getViewRootImpl().ensureTouchMode(false);
6402 return requestFocus();
6403 }
6404 } break;
6405 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6406 if (hasFocus()) {
6407 clearFocus();
6408 return !isFocused();
6409 }
6410 } break;
6411 case AccessibilityNodeInfo.ACTION_SELECT: {
6412 if (!isSelected()) {
6413 setSelected(true);
6414 return isSelected();
6415 }
6416 } break;
6417 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6418 if (isSelected()) {
6419 setSelected(false);
6420 return !isSelected();
6421 }
6422 } break;
6423 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6424 if (!isAccessibilityFocused()) {
6425 return requestAccessibilityFocus();
6426 }
6427 } break;
6428 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6429 if (isAccessibilityFocused()) {
6430 clearAccessibilityFocus();
6431 return true;
6432 }
6433 } break;
6434 }
6435 return false;
6436 }
6437
6438 /**
Romain Guya440b002010-02-24 15:57:54 -08006439 * @hide
6440 */
6441 public void dispatchStartTemporaryDetach() {
6442 onStartTemporaryDetach();
6443 }
6444
6445 /**
6446 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6448 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006449 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450 */
6451 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006452 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006453 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006454 }
6455
6456 /**
6457 * @hide
6458 */
6459 public void dispatchFinishTemporaryDetach() {
6460 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 }
Romain Guy8506ab42009-06-11 17:35:47 -07006462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 /**
6464 * Called after {@link #onStartTemporaryDetach} when the container is done
6465 * changing the view.
6466 */
6467 public void onFinishTemporaryDetach() {
6468 }
Romain Guy8506ab42009-06-11 17:35:47 -07006469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006471 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6472 * for this view's window. Returns null if the view is not currently attached
6473 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006474 * just use the standard high-level event callbacks like
6475 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006476 */
6477 public KeyEvent.DispatcherState getKeyDispatcherState() {
6478 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6479 }
Joe Malin32736f02011-01-19 16:14:20 -08006480
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 * Dispatch a key event before it is processed by any input method
6483 * associated with the view hierarchy. This can be used to intercept
6484 * key events in special situations before the IME consumes them; a
6485 * typical example would be handling the BACK key to update the application's
6486 * UI instead of allowing the IME to see it and close itself.
6487 *
6488 * @param event The key event to be dispatched.
6489 * @return True if the event was handled, false otherwise.
6490 */
6491 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6492 return onKeyPreIme(event.getKeyCode(), event);
6493 }
6494
6495 /**
6496 * Dispatch a key event to the next view on the focus path. This path runs
6497 * from the top of the view tree down to the currently focused view. If this
6498 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6499 * the next node down the focus path. This method also fires any key
6500 * listeners.
6501 *
6502 * @param event The key event to be dispatched.
6503 * @return True if the event was handled, false otherwise.
6504 */
6505 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006506 if (mInputEventConsistencyVerifier != null) {
6507 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509
Jeff Brown21bc5c92011-02-28 18:27:14 -08006510 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006511 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006512 ListenerInfo li = mListenerInfo;
6513 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6514 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 return true;
6516 }
6517
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006518 if (event.dispatch(this, mAttachInfo != null
6519 ? mAttachInfo.mKeyDispatchState : null, this)) {
6520 return true;
6521 }
6522
6523 if (mInputEventConsistencyVerifier != null) {
6524 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6525 }
6526 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 }
6528
6529 /**
6530 * Dispatches a key shortcut event.
6531 *
6532 * @param event The key event to be dispatched.
6533 * @return True if the event was handled by the view, false otherwise.
6534 */
6535 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6536 return onKeyShortcut(event.getKeyCode(), event);
6537 }
6538
6539 /**
6540 * Pass the touch screen motion event down to the target view, or this
6541 * view if it is the target.
6542 *
6543 * @param event The motion event to be dispatched.
6544 * @return True if the event was handled by the view, false otherwise.
6545 */
6546 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006547 if (mInputEventConsistencyVerifier != null) {
6548 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6549 }
6550
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006551 if (onFilterTouchEventForSecurity(event)) {
6552 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006553 ListenerInfo li = mListenerInfo;
6554 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6555 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006556 return true;
6557 }
6558
6559 if (onTouchEvent(event)) {
6560 return true;
6561 }
Jeff Brown85a31762010-09-01 17:01:00 -07006562 }
6563
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006564 if (mInputEventConsistencyVerifier != null) {
6565 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006567 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006568 }
6569
6570 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006571 * Filter the touch event to apply security policies.
6572 *
6573 * @param event The motion event to be filtered.
6574 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006575 *
Jeff Brown85a31762010-09-01 17:01:00 -07006576 * @see #getFilterTouchesWhenObscured
6577 */
6578 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006579 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006580 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6581 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6582 // Window is obscured, drop this touch.
6583 return false;
6584 }
6585 return true;
6586 }
6587
6588 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 * Pass a trackball motion event down to the focused view.
6590 *
6591 * @param event The motion event to be dispatched.
6592 * @return True if the event was handled by the view, false otherwise.
6593 */
6594 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006595 if (mInputEventConsistencyVerifier != null) {
6596 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6597 }
6598
Romain Guy02ccac62011-06-24 13:20:23 -07006599 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006600 }
6601
6602 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006603 * Dispatch a generic motion event.
6604 * <p>
6605 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6606 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006607 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006608 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006609 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006610 *
6611 * @param event The motion event to be dispatched.
6612 * @return True if the event was handled by the view, false otherwise.
6613 */
6614 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006615 if (mInputEventConsistencyVerifier != null) {
6616 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6617 }
6618
Jeff Browna032cc02011-03-07 16:56:21 -08006619 final int source = event.getSource();
6620 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6621 final int action = event.getAction();
6622 if (action == MotionEvent.ACTION_HOVER_ENTER
6623 || action == MotionEvent.ACTION_HOVER_MOVE
6624 || action == MotionEvent.ACTION_HOVER_EXIT) {
6625 if (dispatchHoverEvent(event)) {
6626 return true;
6627 }
6628 } else if (dispatchGenericPointerEvent(event)) {
6629 return true;
6630 }
6631 } else if (dispatchGenericFocusedEvent(event)) {
6632 return true;
6633 }
6634
Jeff Brown10b62902011-06-20 16:40:37 -07006635 if (dispatchGenericMotionEventInternal(event)) {
6636 return true;
6637 }
6638
6639 if (mInputEventConsistencyVerifier != null) {
6640 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6641 }
6642 return false;
6643 }
6644
6645 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006646 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006647 ListenerInfo li = mListenerInfo;
6648 if (li != null && li.mOnGenericMotionListener != null
6649 && (mViewFlags & ENABLED_MASK) == ENABLED
6650 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006651 return true;
6652 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006653
6654 if (onGenericMotionEvent(event)) {
6655 return true;
6656 }
6657
6658 if (mInputEventConsistencyVerifier != null) {
6659 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6660 }
6661 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006662 }
6663
6664 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006665 * Dispatch a hover event.
6666 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006667 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006668 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006669 * </p>
6670 *
6671 * @param event The motion event to be dispatched.
6672 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006673 */
6674 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006675 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006676 ListenerInfo li = mListenerInfo;
6677 if (li != null && li.mOnHoverListener != null
6678 && (mViewFlags & ENABLED_MASK) == ENABLED
6679 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006680 return true;
6681 }
6682
Jeff Browna032cc02011-03-07 16:56:21 -08006683 return onHoverEvent(event);
6684 }
6685
6686 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006687 * Returns true if the view has a child to which it has recently sent
6688 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6689 * it does not have a hovered child, then it must be the innermost hovered view.
6690 * @hide
6691 */
6692 protected boolean hasHoveredChild() {
6693 return false;
6694 }
6695
6696 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006697 * Dispatch a generic motion event to the view under the first pointer.
6698 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006699 * Do not call this method directly.
6700 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006701 * </p>
6702 *
6703 * @param event The motion event to be dispatched.
6704 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006705 */
6706 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6707 return false;
6708 }
6709
6710 /**
6711 * Dispatch a generic motion event to the currently focused view.
6712 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006713 * Do not call this method directly.
6714 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006715 * </p>
6716 *
6717 * @param event The motion event to be dispatched.
6718 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006719 */
6720 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6721 return false;
6722 }
6723
6724 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006725 * Dispatch a pointer event.
6726 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006727 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6728 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6729 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006730 * and should not be expected to handle other pointing device features.
6731 * </p>
6732 *
6733 * @param event The motion event to be dispatched.
6734 * @return True if the event was handled by the view, false otherwise.
6735 * @hide
6736 */
6737 public final boolean dispatchPointerEvent(MotionEvent event) {
6738 if (event.isTouchEvent()) {
6739 return dispatchTouchEvent(event);
6740 } else {
6741 return dispatchGenericMotionEvent(event);
6742 }
6743 }
6744
6745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 * Called when the window containing this view gains or loses window focus.
6747 * ViewGroups should override to route to their children.
6748 *
6749 * @param hasFocus True if the window containing this view now has focus,
6750 * false otherwise.
6751 */
6752 public void dispatchWindowFocusChanged(boolean hasFocus) {
6753 onWindowFocusChanged(hasFocus);
6754 }
6755
6756 /**
6757 * Called when the window containing this view gains or loses focus. Note
6758 * that this is separate from view focus: to receive key events, both
6759 * your view and its window must have focus. If a window is displayed
6760 * on top of yours that takes input focus, then your own window will lose
6761 * focus but the view focus will remain unchanged.
6762 *
6763 * @param hasWindowFocus True if the window containing this view now has
6764 * focus, false otherwise.
6765 */
6766 public void onWindowFocusChanged(boolean hasWindowFocus) {
6767 InputMethodManager imm = InputMethodManager.peekInstance();
6768 if (!hasWindowFocus) {
6769 if (isPressed()) {
6770 setPressed(false);
6771 }
6772 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6773 imm.focusOut(this);
6774 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006775 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006776 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006777 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6779 imm.focusIn(this);
6780 }
6781 refreshDrawableState();
6782 }
6783
6784 /**
6785 * Returns true if this view is in a window that currently has window focus.
6786 * Note that this is not the same as the view itself having focus.
6787 *
6788 * @return True if this view is in a window that currently has window focus.
6789 */
6790 public boolean hasWindowFocus() {
6791 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6792 }
6793
6794 /**
Adam Powell326d8082009-12-09 15:10:07 -08006795 * Dispatch a view visibility change down the view hierarchy.
6796 * ViewGroups should override to route to their children.
6797 * @param changedView The view whose visibility changed. Could be 'this' or
6798 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006799 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6800 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006801 */
6802 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6803 onVisibilityChanged(changedView, visibility);
6804 }
6805
6806 /**
6807 * Called when the visibility of the view or an ancestor of the view is changed.
6808 * @param changedView The view whose visibility changed. Could be 'this' or
6809 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006810 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6811 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006812 */
6813 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006814 if (visibility == VISIBLE) {
6815 if (mAttachInfo != null) {
6816 initialAwakenScrollBars();
6817 } else {
6818 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6819 }
6820 }
Adam Powell326d8082009-12-09 15:10:07 -08006821 }
6822
6823 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006824 * Dispatch a hint about whether this view is displayed. For instance, when
6825 * a View moves out of the screen, it might receives a display hint indicating
6826 * the view is not displayed. Applications should not <em>rely</em> on this hint
6827 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006828 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006829 * @param hint A hint about whether or not this view is displayed:
6830 * {@link #VISIBLE} or {@link #INVISIBLE}.
6831 */
6832 public void dispatchDisplayHint(int hint) {
6833 onDisplayHint(hint);
6834 }
6835
6836 /**
6837 * Gives this view a hint about whether is displayed or not. For instance, when
6838 * a View moves out of the screen, it might receives a display hint indicating
6839 * the view is not displayed. Applications should not <em>rely</em> on this hint
6840 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006841 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006842 * @param hint A hint about whether or not this view is displayed:
6843 * {@link #VISIBLE} or {@link #INVISIBLE}.
6844 */
6845 protected void onDisplayHint(int hint) {
6846 }
6847
6848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 * Dispatch a window visibility change down the view hierarchy.
6850 * ViewGroups should override to route to their children.
6851 *
6852 * @param visibility The new visibility of the window.
6853 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006854 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 */
6856 public void dispatchWindowVisibilityChanged(int visibility) {
6857 onWindowVisibilityChanged(visibility);
6858 }
6859
6860 /**
6861 * Called when the window containing has change its visibility
6862 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6863 * that this tells you whether or not your window is being made visible
6864 * to the window manager; this does <em>not</em> tell you whether or not
6865 * your window is obscured by other windows on the screen, even if it
6866 * is itself visible.
6867 *
6868 * @param visibility The new visibility of the window.
6869 */
6870 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006871 if (visibility == VISIBLE) {
6872 initialAwakenScrollBars();
6873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 }
6875
6876 /**
6877 * Returns the current visibility of the window this view is attached to
6878 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6879 *
6880 * @return Returns the current visibility of the view's window.
6881 */
6882 public int getWindowVisibility() {
6883 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6884 }
6885
6886 /**
6887 * Retrieve the overall visible display size in which the window this view is
6888 * attached to has been positioned in. This takes into account screen
6889 * decorations above the window, for both cases where the window itself
6890 * is being position inside of them or the window is being placed under
6891 * then and covered insets are used for the window to position its content
6892 * inside. In effect, this tells you the available area where content can
6893 * be placed and remain visible to users.
6894 *
6895 * <p>This function requires an IPC back to the window manager to retrieve
6896 * the requested information, so should not be used in performance critical
6897 * code like drawing.
6898 *
6899 * @param outRect Filled in with the visible display frame. If the view
6900 * is not attached to a window, this is simply the raw display size.
6901 */
6902 public void getWindowVisibleDisplayFrame(Rect outRect) {
6903 if (mAttachInfo != null) {
6904 try {
6905 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6906 } catch (RemoteException e) {
6907 return;
6908 }
6909 // XXX This is really broken, and probably all needs to be done
6910 // in the window manager, and we need to know more about whether
6911 // we want the area behind or in front of the IME.
6912 final Rect insets = mAttachInfo.mVisibleInsets;
6913 outRect.left += insets.left;
6914 outRect.top += insets.top;
6915 outRect.right -= insets.right;
6916 outRect.bottom -= insets.bottom;
6917 return;
6918 }
6919 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006920 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 }
6922
6923 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006924 * Dispatch a notification about a resource configuration change down
6925 * the view hierarchy.
6926 * ViewGroups should override to route to their children.
6927 *
6928 * @param newConfig The new resource configuration.
6929 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006930 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006931 */
6932 public void dispatchConfigurationChanged(Configuration newConfig) {
6933 onConfigurationChanged(newConfig);
6934 }
6935
6936 /**
6937 * Called when the current configuration of the resources being used
6938 * by the application have changed. You can use this to decide when
6939 * to reload resources that can changed based on orientation and other
6940 * configuration characterstics. You only need to use this if you are
6941 * not relying on the normal {@link android.app.Activity} mechanism of
6942 * recreating the activity instance upon a configuration change.
6943 *
6944 * @param newConfig The new resource configuration.
6945 */
6946 protected void onConfigurationChanged(Configuration newConfig) {
6947 }
6948
6949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 * Private function to aggregate all per-view attributes in to the view
6951 * root.
6952 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006953 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6954 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 }
6956
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006957 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6958 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006959 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006960 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006961 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006962 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006963 ListenerInfo li = mListenerInfo;
6964 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006965 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 }
6968 }
6969
6970 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006971 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006973 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6974 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 ai.mRecomputeGlobalAttributes = true;
6976 }
6977 }
6978 }
6979
6980 /**
6981 * Returns whether the device is currently in touch mode. Touch mode is entered
6982 * once the user begins interacting with the device by touch, and affects various
6983 * things like whether focus is always visible to the user.
6984 *
6985 * @return Whether the device is in touch mode.
6986 */
6987 @ViewDebug.ExportedProperty
6988 public boolean isInTouchMode() {
6989 if (mAttachInfo != null) {
6990 return mAttachInfo.mInTouchMode;
6991 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006992 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 }
6994 }
6995
6996 /**
6997 * Returns the context the view is running in, through which it can
6998 * access the current theme, resources, etc.
6999 *
7000 * @return The view's Context.
7001 */
7002 @ViewDebug.CapturedViewProperty
7003 public final Context getContext() {
7004 return mContext;
7005 }
7006
7007 /**
7008 * Handle a key event before it is processed by any input method
7009 * associated with the view hierarchy. This can be used to intercept
7010 * key events in special situations before the IME consumes them; a
7011 * typical example would be handling the BACK key to update the application's
7012 * UI instead of allowing the IME to see it and close itself.
7013 *
7014 * @param keyCode The value in event.getKeyCode().
7015 * @param event Description of the key event.
7016 * @return If you handled the event, return true. If you want to allow the
7017 * event to be handled by the next receiver, return false.
7018 */
7019 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7020 return false;
7021 }
7022
7023 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007024 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7025 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7027 * is released, if the view is enabled and clickable.
7028 *
7029 * @param keyCode A key code that represents the button pressed, from
7030 * {@link android.view.KeyEvent}.
7031 * @param event The KeyEvent object that defines the button action.
7032 */
7033 public boolean onKeyDown(int keyCode, KeyEvent event) {
7034 boolean result = false;
7035
7036 switch (keyCode) {
7037 case KeyEvent.KEYCODE_DPAD_CENTER:
7038 case KeyEvent.KEYCODE_ENTER: {
7039 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7040 return true;
7041 }
7042 // Long clickable items don't necessarily have to be clickable
7043 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7044 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7045 (event.getRepeatCount() == 0)) {
7046 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007047 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007048 return true;
7049 }
7050 break;
7051 }
7052 }
7053 return result;
7054 }
7055
7056 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007057 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7058 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7059 * the event).
7060 */
7061 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7062 return false;
7063 }
7064
7065 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007066 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7067 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7069 * {@link KeyEvent#KEYCODE_ENTER} is released.
7070 *
7071 * @param keyCode A key code that represents the button pressed, from
7072 * {@link android.view.KeyEvent}.
7073 * @param event The KeyEvent object that defines the button action.
7074 */
7075 public boolean onKeyUp(int keyCode, KeyEvent event) {
7076 boolean result = false;
7077
7078 switch (keyCode) {
7079 case KeyEvent.KEYCODE_DPAD_CENTER:
7080 case KeyEvent.KEYCODE_ENTER: {
7081 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7082 return true;
7083 }
7084 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7085 setPressed(false);
7086
7087 if (!mHasPerformedLongPress) {
7088 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007089 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090
7091 result = performClick();
7092 }
7093 }
7094 break;
7095 }
7096 }
7097 return result;
7098 }
7099
7100 /**
7101 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7102 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7103 * the event).
7104 *
7105 * @param keyCode A key code that represents the button pressed, from
7106 * {@link android.view.KeyEvent}.
7107 * @param repeatCount The number of times the action was made.
7108 * @param event The KeyEvent object that defines the button action.
7109 */
7110 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7111 return false;
7112 }
7113
7114 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007115 * Called on the focused view when a key shortcut event is not handled.
7116 * Override this method to implement local key shortcuts for the View.
7117 * Key shortcuts can also be implemented by setting the
7118 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 *
7120 * @param keyCode The value in event.getKeyCode().
7121 * @param event Description of the key event.
7122 * @return If you handled the event, return true. If you want to allow the
7123 * event to be handled by the next receiver, return false.
7124 */
7125 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7126 return false;
7127 }
7128
7129 /**
7130 * Check whether the called view is a text editor, in which case it
7131 * would make sense to automatically display a soft input window for
7132 * it. Subclasses should override this if they implement
7133 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007134 * a call on that method would return a non-null InputConnection, and
7135 * they are really a first-class editor that the user would normally
7136 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007137 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007138 * <p>The default implementation always returns false. This does
7139 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7140 * will not be called or the user can not otherwise perform edits on your
7141 * view; it is just a hint to the system that this is not the primary
7142 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 * @return Returns true if this view is a text editor, else false.
7145 */
7146 public boolean onCheckIsTextEditor() {
7147 return false;
7148 }
Romain Guy8506ab42009-06-11 17:35:47 -07007149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 /**
7151 * Create a new InputConnection for an InputMethod to interact
7152 * with the view. The default implementation returns null, since it doesn't
7153 * support input methods. You can override this to implement such support.
7154 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 * <p>When implementing this, you probably also want to implement
7157 * {@link #onCheckIsTextEditor()} to indicate you will return a
7158 * non-null InputConnection.
7159 *
7160 * @param outAttrs Fill in with attribute information about the connection.
7161 */
7162 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7163 return null;
7164 }
7165
7166 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007167 * Called by the {@link android.view.inputmethod.InputMethodManager}
7168 * when a view who is not the current
7169 * input connection target is trying to make a call on the manager. The
7170 * default implementation returns false; you can override this to return
7171 * true for certain views if you are performing InputConnection proxying
7172 * to them.
7173 * @param view The View that is making the InputMethodManager call.
7174 * @return Return true to allow the call, false to reject.
7175 */
7176 public boolean checkInputConnectionProxy(View view) {
7177 return false;
7178 }
Romain Guy8506ab42009-06-11 17:35:47 -07007179
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007181 * Show the context menu for this view. It is not safe to hold on to the
7182 * menu after returning from this method.
7183 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007184 * You should normally not overload this method. Overload
7185 * {@link #onCreateContextMenu(ContextMenu)} or define an
7186 * {@link OnCreateContextMenuListener} to add items to the context menu.
7187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 * @param menu The context menu to populate
7189 */
7190 public void createContextMenu(ContextMenu menu) {
7191 ContextMenuInfo menuInfo = getContextMenuInfo();
7192
7193 // Sets the current menu info so all items added to menu will have
7194 // my extra info set.
7195 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7196
7197 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007198 ListenerInfo li = mListenerInfo;
7199 if (li != null && li.mOnCreateContextMenuListener != null) {
7200 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 }
7202
7203 // Clear the extra information so subsequent items that aren't mine don't
7204 // have my extra info.
7205 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7206
7207 if (mParent != null) {
7208 mParent.createContextMenu(menu);
7209 }
7210 }
7211
7212 /**
7213 * Views should implement this if they have extra information to associate
7214 * with the context menu. The return result is supplied as a parameter to
7215 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7216 * callback.
7217 *
7218 * @return Extra information about the item for which the context menu
7219 * should be shown. This information will vary across different
7220 * subclasses of View.
7221 */
7222 protected ContextMenuInfo getContextMenuInfo() {
7223 return null;
7224 }
7225
7226 /**
7227 * Views should implement this if the view itself is going to add items to
7228 * the context menu.
7229 *
7230 * @param menu the context menu to populate
7231 */
7232 protected void onCreateContextMenu(ContextMenu menu) {
7233 }
7234
7235 /**
7236 * Implement this method to handle trackball motion events. The
7237 * <em>relative</em> movement of the trackball since the last event
7238 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7239 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7240 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7241 * they will often be fractional values, representing the more fine-grained
7242 * movement information available from a trackball).
7243 *
7244 * @param event The motion event.
7245 * @return True if the event was handled, false otherwise.
7246 */
7247 public boolean onTrackballEvent(MotionEvent event) {
7248 return false;
7249 }
7250
7251 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007252 * Implement this method to handle generic motion events.
7253 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007254 * Generic motion events describe joystick movements, mouse hovers, track pad
7255 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007256 * {@link MotionEvent#getSource() source} of the motion event specifies
7257 * the class of input that was received. Implementations of this method
7258 * must examine the bits in the source before processing the event.
7259 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007260 * </p><p>
7261 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7262 * are delivered to the view under the pointer. All other generic motion events are
7263 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007264 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007265 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007266 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007267 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7268 * // process the joystick movement...
7269 * return true;
7270 * }
7271 * }
7272 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7273 * switch (event.getAction()) {
7274 * case MotionEvent.ACTION_HOVER_MOVE:
7275 * // process the mouse hover movement...
7276 * return true;
7277 * case MotionEvent.ACTION_SCROLL:
7278 * // process the scroll wheel movement...
7279 * return true;
7280 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007281 * }
7282 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007283 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007284 *
7285 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007286 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007287 */
7288 public boolean onGenericMotionEvent(MotionEvent event) {
7289 return false;
7290 }
7291
7292 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007293 * Implement this method to handle hover events.
7294 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007295 * This method is called whenever a pointer is hovering into, over, or out of the
7296 * bounds of a view and the view is not currently being touched.
7297 * Hover events are represented as pointer events with action
7298 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7299 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7300 * </p>
7301 * <ul>
7302 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7303 * when the pointer enters the bounds of the view.</li>
7304 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7305 * when the pointer has already entered the bounds of the view and has moved.</li>
7306 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7307 * when the pointer has exited the bounds of the view or when the pointer is
7308 * about to go down due to a button click, tap, or similar user action that
7309 * causes the view to be touched.</li>
7310 * </ul>
7311 * <p>
7312 * The view should implement this method to return true to indicate that it is
7313 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007314 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007315 * The default implementation calls {@link #setHovered} to update the hovered state
7316 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007317 * is enabled and is clickable. The default implementation also sends hover
7318 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007319 * </p>
7320 *
7321 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007322 * @return True if the view handled the hover event.
7323 *
7324 * @see #isHovered
7325 * @see #setHovered
7326 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007327 */
7328 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007329 // The root view may receive hover (or touch) events that are outside the bounds of
7330 // the window. This code ensures that we only send accessibility events for
7331 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007332 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007333 if (!mSendingHoverAccessibilityEvents) {
7334 if ((action == MotionEvent.ACTION_HOVER_ENTER
7335 || action == MotionEvent.ACTION_HOVER_MOVE)
7336 && !hasHoveredChild()
7337 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007338 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007339 mSendingHoverAccessibilityEvents = true;
7340 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007341 }
7342 } else {
7343 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007344 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007345 && !pointInView(event.getX(), event.getY()))) {
7346 mSendingHoverAccessibilityEvents = false;
7347 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007348 // If the window does not have input focus we take away accessibility
7349 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007350 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007351 getViewRootImpl().setAccessibilityFocusedHost(null);
7352 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007353 }
Jeff Browna1b24182011-07-28 13:38:24 -07007354 }
7355
Jeff Brown87b7f802011-06-21 18:35:45 -07007356 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007357 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007358 case MotionEvent.ACTION_HOVER_ENTER:
7359 setHovered(true);
7360 break;
7361 case MotionEvent.ACTION_HOVER_EXIT:
7362 setHovered(false);
7363 break;
7364 }
Jeff Browna1b24182011-07-28 13:38:24 -07007365
7366 // Dispatch the event to onGenericMotionEvent before returning true.
7367 // This is to provide compatibility with existing applications that
7368 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7369 // break because of the new default handling for hoverable views
7370 // in onHoverEvent.
7371 // Note that onGenericMotionEvent will be called by default when
7372 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7373 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007374 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007375 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007376
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007377 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007378 }
7379
7380 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007381 * Returns true if the view should handle {@link #onHoverEvent}
7382 * by calling {@link #setHovered} to change its hovered state.
7383 *
7384 * @return True if the view is hoverable.
7385 */
7386 private boolean isHoverable() {
7387 final int viewFlags = mViewFlags;
7388 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7389 return false;
7390 }
7391
7392 return (viewFlags & CLICKABLE) == CLICKABLE
7393 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7394 }
7395
7396 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007397 * Returns true if the view is currently hovered.
7398 *
7399 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007400 *
7401 * @see #setHovered
7402 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007403 */
Jeff Brown10b62902011-06-20 16:40:37 -07007404 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007405 public boolean isHovered() {
7406 return (mPrivateFlags & HOVERED) != 0;
7407 }
7408
7409 /**
7410 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007411 * <p>
7412 * Calling this method also changes the drawable state of the view. This
7413 * enables the view to react to hover by using different drawable resources
7414 * to change its appearance.
7415 * </p><p>
7416 * The {@link #onHoverChanged} method is called when the hovered state changes.
7417 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007418 *
7419 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007420 *
7421 * @see #isHovered
7422 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007423 */
7424 public void setHovered(boolean hovered) {
7425 if (hovered) {
7426 if ((mPrivateFlags & HOVERED) == 0) {
7427 mPrivateFlags |= HOVERED;
7428 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007429 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007430 }
7431 } else {
7432 if ((mPrivateFlags & HOVERED) != 0) {
7433 mPrivateFlags &= ~HOVERED;
7434 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007435 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007436 }
7437 }
7438 }
7439
7440 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007441 * Implement this method to handle hover state changes.
7442 * <p>
7443 * This method is called whenever the hover state changes as a result of a
7444 * call to {@link #setHovered}.
7445 * </p>
7446 *
7447 * @param hovered The current hover state, as returned by {@link #isHovered}.
7448 *
7449 * @see #isHovered
7450 * @see #setHovered
7451 */
7452 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007453 }
7454
7455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 * Implement this method to handle touch screen motion events.
7457 *
7458 * @param event The motion event.
7459 * @return True if the event was handled, false otherwise.
7460 */
7461 public boolean onTouchEvent(MotionEvent event) {
7462 final int viewFlags = mViewFlags;
7463
7464 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007465 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007466 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 // A disabled view that is clickable still consumes the touch
7469 // events, it just doesn't respond to them.
7470 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7471 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7472 }
7473
7474 if (mTouchDelegate != null) {
7475 if (mTouchDelegate.onTouchEvent(event)) {
7476 return true;
7477 }
7478 }
7479
7480 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7481 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7482 switch (event.getAction()) {
7483 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007484 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7485 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 // take focus if we don't have it already and we should in
7487 // touch mode.
7488 boolean focusTaken = false;
7489 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7490 focusTaken = requestFocus();
7491 }
7492
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007493 if (prepressed) {
7494 // The button is being released before we actually
7495 // showed it as pressed. Make it show the pressed
7496 // state now (before scheduling the click) to ensure
7497 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007498 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007499 }
Joe Malin32736f02011-01-19 16:14:20 -08007500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 if (!mHasPerformedLongPress) {
7502 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007503 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504
7505 // Only perform take click actions if we were in the pressed state
7506 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007507 // Use a Runnable and post this rather than calling
7508 // performClick directly. This lets other visual state
7509 // of the view update before click actions start.
7510 if (mPerformClick == null) {
7511 mPerformClick = new PerformClick();
7512 }
7513 if (!post(mPerformClick)) {
7514 performClick();
7515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 }
7517 }
7518
7519 if (mUnsetPressedState == null) {
7520 mUnsetPressedState = new UnsetPressedState();
7521 }
7522
Adam Powelle14579b2009-12-16 18:39:52 -08007523 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007524 postDelayed(mUnsetPressedState,
7525 ViewConfiguration.getPressedStateDuration());
7526 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 // If the post failed, unpress right now
7528 mUnsetPressedState.run();
7529 }
Adam Powelle14579b2009-12-16 18:39:52 -08007530 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 }
7532 break;
7533
7534 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007535 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007536
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007537 if (performButtonActionOnTouchDown(event)) {
7538 break;
7539 }
7540
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007541 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007542 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007543
7544 // For views inside a scrolling container, delay the pressed feedback for
7545 // a short period in case this is a scroll.
7546 if (isInScrollingContainer) {
7547 mPrivateFlags |= PREPRESSED;
7548 if (mPendingCheckForTap == null) {
7549 mPendingCheckForTap = new CheckForTap();
7550 }
7551 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7552 } else {
7553 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007554 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007555 checkForLongClick(0);
7556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007557 break;
7558
7559 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007560 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007561 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 break;
7563
7564 case MotionEvent.ACTION_MOVE:
7565 final int x = (int) event.getX();
7566 final int y = (int) event.getY();
7567
7568 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007569 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007571 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007573 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007574 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007575
Adam Powell4d6f0662012-02-21 15:11:11 -08007576 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 }
7579 break;
7580 }
7581 return true;
7582 }
7583
7584 return false;
7585 }
7586
7587 /**
Adam Powell10298662011-08-14 18:26:30 -07007588 * @hide
7589 */
7590 public boolean isInScrollingContainer() {
7591 ViewParent p = getParent();
7592 while (p != null && p instanceof ViewGroup) {
7593 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7594 return true;
7595 }
7596 p = p.getParent();
7597 }
7598 return false;
7599 }
7600
7601 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007602 * Remove the longpress detection timer.
7603 */
7604 private void removeLongPressCallback() {
7605 if (mPendingCheckForLongPress != null) {
7606 removeCallbacks(mPendingCheckForLongPress);
7607 }
7608 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007609
7610 /**
7611 * Remove the pending click action
7612 */
7613 private void removePerformClickCallback() {
7614 if (mPerformClick != null) {
7615 removeCallbacks(mPerformClick);
7616 }
7617 }
7618
Adam Powelle14579b2009-12-16 18:39:52 -08007619 /**
Romain Guya440b002010-02-24 15:57:54 -08007620 * Remove the prepress detection timer.
7621 */
7622 private void removeUnsetPressCallback() {
7623 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7624 setPressed(false);
7625 removeCallbacks(mUnsetPressedState);
7626 }
7627 }
7628
7629 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007630 * Remove the tap detection timer.
7631 */
7632 private void removeTapCallback() {
7633 if (mPendingCheckForTap != null) {
7634 mPrivateFlags &= ~PREPRESSED;
7635 removeCallbacks(mPendingCheckForTap);
7636 }
7637 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007638
7639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 * Cancels a pending long press. Your subclass can use this if you
7641 * want the context menu to come up if the user presses and holds
7642 * at the same place, but you don't want it to come up if they press
7643 * and then move around enough to cause scrolling.
7644 */
7645 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007646 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007647
7648 /*
7649 * The prepressed state handled by the tap callback is a display
7650 * construct, but the tap callback will post a long press callback
7651 * less its own timeout. Remove it here.
7652 */
7653 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 }
7655
7656 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007657 * Remove the pending callback for sending a
7658 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7659 */
7660 private void removeSendViewScrolledAccessibilityEventCallback() {
7661 if (mSendViewScrolledAccessibilityEvent != null) {
7662 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7663 }
7664 }
7665
7666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 * Sets the TouchDelegate for this View.
7668 */
7669 public void setTouchDelegate(TouchDelegate delegate) {
7670 mTouchDelegate = delegate;
7671 }
7672
7673 /**
7674 * Gets the TouchDelegate for this View.
7675 */
7676 public TouchDelegate getTouchDelegate() {
7677 return mTouchDelegate;
7678 }
7679
7680 /**
7681 * Set flags controlling behavior of this view.
7682 *
7683 * @param flags Constant indicating the value which should be set
7684 * @param mask Constant indicating the bit range that should be changed
7685 */
7686 void setFlags(int flags, int mask) {
7687 int old = mViewFlags;
7688 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7689
7690 int changed = mViewFlags ^ old;
7691 if (changed == 0) {
7692 return;
7693 }
7694 int privateFlags = mPrivateFlags;
7695
7696 /* Check if the FOCUSABLE bit has changed */
7697 if (((changed & FOCUSABLE_MASK) != 0) &&
7698 ((privateFlags & HAS_BOUNDS) !=0)) {
7699 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7700 && ((privateFlags & FOCUSED) != 0)) {
7701 /* Give up focus if we are no longer focusable */
7702 clearFocus();
7703 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7704 && ((privateFlags & FOCUSED) == 0)) {
7705 /*
7706 * Tell the view system that we are now available to take focus
7707 * if no one else already has it.
7708 */
7709 if (mParent != null) mParent.focusableViewAvailable(this);
7710 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007711 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7712 notifyAccessibilityStateChanged();
7713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007714 }
7715
7716 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7717 if ((changed & VISIBILITY_MASK) != 0) {
7718 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007719 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007720 * it was not visible. Marking it drawn ensures that the invalidation will
7721 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007723 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007724 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725
7726 needGlobalAttributesUpdate(true);
7727
7728 // a view becoming visible is worth notifying the parent
7729 // about in case nothing has focus. even if this specific view
7730 // isn't focusable, it may contain something that is, so let
7731 // the root view try to give this focus if nothing else does.
7732 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7733 mParent.focusableViewAvailable(this);
7734 }
7735 }
7736 }
7737
7738 /* Check if the GONE bit has changed */
7739 if ((changed & GONE) != 0) {
7740 needGlobalAttributesUpdate(false);
7741 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007742
Romain Guyecd80ee2009-12-03 17:13:02 -08007743 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7744 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007745 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007746 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007747 if (mParent instanceof View) {
7748 // GONE views noop invalidation, so invalidate the parent
7749 ((View) mParent).invalidate(true);
7750 }
7751 // Mark the view drawn to ensure that it gets invalidated properly the next
7752 // time it is visible and gets invalidated
7753 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 }
7755 if (mAttachInfo != null) {
7756 mAttachInfo.mViewVisibilityChanged = true;
7757 }
7758 }
7759
7760 /* Check if the VISIBLE bit has changed */
7761 if ((changed & INVISIBLE) != 0) {
7762 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007763 /*
7764 * If this view is becoming invisible, set the DRAWN flag so that
7765 * the next invalidate() will not be skipped.
7766 */
7767 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768
7769 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007770 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 if (getRootView() != this) {
7772 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007773 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 }
7775 }
7776 if (mAttachInfo != null) {
7777 mAttachInfo.mViewVisibilityChanged = true;
7778 }
7779 }
7780
Adam Powell326d8082009-12-09 15:10:07 -08007781 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007782 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007783 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7784 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007785 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007786 } else if (mParent != null) {
7787 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007788 }
Adam Powell326d8082009-12-09 15:10:07 -08007789 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7790 }
7791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7793 destroyDrawingCache();
7794 }
7795
7796 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7797 destroyDrawingCache();
7798 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007799 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 }
7801
7802 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7803 destroyDrawingCache();
7804 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7805 }
7806
7807 if ((changed & DRAW_MASK) != 0) {
7808 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007809 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 mPrivateFlags &= ~SKIP_DRAW;
7811 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7812 } else {
7813 mPrivateFlags |= SKIP_DRAW;
7814 }
7815 } else {
7816 mPrivateFlags &= ~SKIP_DRAW;
7817 }
7818 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007819 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 }
7821
7822 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007823 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 mParent.recomputeViewAttributes(this);
7825 }
7826 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007827
7828 if (AccessibilityManager.getInstance(mContext).isEnabled()
7829 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7830 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7831 notifyAccessibilityStateChanged();
7832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 }
7834
7835 /**
7836 * Change the view's z order in the tree, so it's on top of other sibling
7837 * views
7838 */
7839 public void bringToFront() {
7840 if (mParent != null) {
7841 mParent.bringChildToFront(this);
7842 }
7843 }
7844
7845 /**
7846 * This is called in response to an internal scroll in this view (i.e., the
7847 * view scrolled its own contents). This is typically as a result of
7848 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7849 * called.
7850 *
7851 * @param l Current horizontal scroll origin.
7852 * @param t Current vertical scroll origin.
7853 * @param oldl Previous horizontal scroll origin.
7854 * @param oldt Previous vertical scroll origin.
7855 */
7856 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007857 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7858 postSendViewScrolledAccessibilityEventCallback();
7859 }
7860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 mBackgroundSizeChanged = true;
7862
7863 final AttachInfo ai = mAttachInfo;
7864 if (ai != null) {
7865 ai.mViewScrollChanged = true;
7866 }
7867 }
7868
7869 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007870 * Interface definition for a callback to be invoked when the layout bounds of a view
7871 * changes due to layout processing.
7872 */
7873 public interface OnLayoutChangeListener {
7874 /**
7875 * Called when the focus state of a view has changed.
7876 *
7877 * @param v The view whose state has changed.
7878 * @param left The new value of the view's left property.
7879 * @param top The new value of the view's top property.
7880 * @param right The new value of the view's right property.
7881 * @param bottom The new value of the view's bottom property.
7882 * @param oldLeft The previous value of the view's left property.
7883 * @param oldTop The previous value of the view's top property.
7884 * @param oldRight The previous value of the view's right property.
7885 * @param oldBottom The previous value of the view's bottom property.
7886 */
7887 void onLayoutChange(View v, int left, int top, int right, int bottom,
7888 int oldLeft, int oldTop, int oldRight, int oldBottom);
7889 }
7890
7891 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 * This is called during layout when the size of this view has changed. If
7893 * you were just added to the view hierarchy, you're called with the old
7894 * values of 0.
7895 *
7896 * @param w Current width of this view.
7897 * @param h Current height of this view.
7898 * @param oldw Old width of this view.
7899 * @param oldh Old height of this view.
7900 */
7901 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7902 }
7903
7904 /**
7905 * Called by draw to draw the child views. This may be overridden
7906 * by derived classes to gain control just before its children are drawn
7907 * (but after its own view has been drawn).
7908 * @param canvas the canvas on which to draw the view
7909 */
7910 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 }
7913
7914 /**
7915 * Gets the parent of this view. Note that the parent is a
7916 * ViewParent and not necessarily a View.
7917 *
7918 * @return Parent of this view.
7919 */
7920 public final ViewParent getParent() {
7921 return mParent;
7922 }
7923
7924 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007925 * Set the horizontal scrolled position of your view. This will cause a call to
7926 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7927 * invalidated.
7928 * @param value the x position to scroll to
7929 */
7930 public void setScrollX(int value) {
7931 scrollTo(value, mScrollY);
7932 }
7933
7934 /**
7935 * Set the vertical scrolled position of your view. This will cause a call to
7936 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7937 * invalidated.
7938 * @param value the y position to scroll to
7939 */
7940 public void setScrollY(int value) {
7941 scrollTo(mScrollX, value);
7942 }
7943
7944 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007945 * Return the scrolled left position of this view. This is the left edge of
7946 * the displayed part of your view. You do not need to draw any pixels
7947 * farther left, since those are outside of the frame of your view on
7948 * screen.
7949 *
7950 * @return The left edge of the displayed part of your view, in pixels.
7951 */
7952 public final int getScrollX() {
7953 return mScrollX;
7954 }
7955
7956 /**
7957 * Return the scrolled top position of this view. This is the top edge of
7958 * the displayed part of your view. You do not need to draw any pixels above
7959 * it, since those are outside of the frame of your view on screen.
7960 *
7961 * @return The top edge of the displayed part of your view, in pixels.
7962 */
7963 public final int getScrollY() {
7964 return mScrollY;
7965 }
7966
7967 /**
7968 * Return the width of the your view.
7969 *
7970 * @return The width of your view, in pixels.
7971 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007972 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 public final int getWidth() {
7974 return mRight - mLeft;
7975 }
7976
7977 /**
7978 * Return the height of your view.
7979 *
7980 * @return The height of your view, in pixels.
7981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007982 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 public final int getHeight() {
7984 return mBottom - mTop;
7985 }
7986
7987 /**
7988 * Return the visible drawing bounds of your view. Fills in the output
7989 * rectangle with the values from getScrollX(), getScrollY(),
7990 * getWidth(), and getHeight().
7991 *
7992 * @param outRect The (scrolled) drawing bounds of the view.
7993 */
7994 public void getDrawingRect(Rect outRect) {
7995 outRect.left = mScrollX;
7996 outRect.top = mScrollY;
7997 outRect.right = mScrollX + (mRight - mLeft);
7998 outRect.bottom = mScrollY + (mBottom - mTop);
7999 }
8000
8001 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008002 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8003 * raw width component (that is the result is masked by
8004 * {@link #MEASURED_SIZE_MASK}).
8005 *
8006 * @return The raw measured width of this view.
8007 */
8008 public final int getMeasuredWidth() {
8009 return mMeasuredWidth & MEASURED_SIZE_MASK;
8010 }
8011
8012 /**
8013 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008014 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008015 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 * This should be used during measurement and layout calculations only. Use
8017 * {@link #getWidth()} to see how wide a view is after layout.
8018 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008019 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008021 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 return mMeasuredWidth;
8023 }
8024
8025 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008026 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8027 * raw width component (that is the result is masked by
8028 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008030 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 */
8032 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008033 return mMeasuredHeight & MEASURED_SIZE_MASK;
8034 }
8035
8036 /**
8037 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008038 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008039 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8040 * This should be used during measurement and layout calculations only. Use
8041 * {@link #getHeight()} to see how wide a view is after layout.
8042 *
8043 * @return The measured width of this view as a bit mask.
8044 */
8045 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 return mMeasuredHeight;
8047 }
8048
8049 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008050 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8051 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8052 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8053 * and the height component is at the shifted bits
8054 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8055 */
8056 public final int getMeasuredState() {
8057 return (mMeasuredWidth&MEASURED_STATE_MASK)
8058 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8059 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8060 }
8061
8062 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008063 * The transform matrix of this view, which is calculated based on the current
8064 * roation, scale, and pivot properties.
8065 *
8066 * @see #getRotation()
8067 * @see #getScaleX()
8068 * @see #getScaleY()
8069 * @see #getPivotX()
8070 * @see #getPivotY()
8071 * @return The current transform matrix for the view
8072 */
8073 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008074 if (mTransformationInfo != null) {
8075 updateMatrix();
8076 return mTransformationInfo.mMatrix;
8077 }
8078 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008079 }
8080
8081 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008082 * Utility function to determine if the value is far enough away from zero to be
8083 * considered non-zero.
8084 * @param value A floating point value to check for zero-ness
8085 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8086 */
8087 private static boolean nonzero(float value) {
8088 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8089 }
8090
8091 /**
Jeff Brown86671742010-09-30 20:00:15 -07008092 * Returns true if the transform matrix is the identity matrix.
8093 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008094 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008095 * @return True if the transform matrix is the identity matrix, false otherwise.
8096 */
Jeff Brown86671742010-09-30 20:00:15 -07008097 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008098 if (mTransformationInfo != null) {
8099 updateMatrix();
8100 return mTransformationInfo.mMatrixIsIdentity;
8101 }
8102 return true;
8103 }
8104
8105 void ensureTransformationInfo() {
8106 if (mTransformationInfo == null) {
8107 mTransformationInfo = new TransformationInfo();
8108 }
Jeff Brown86671742010-09-30 20:00:15 -07008109 }
8110
8111 /**
8112 * Recomputes the transform matrix if necessary.
8113 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008114 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008115 final TransformationInfo info = mTransformationInfo;
8116 if (info == null) {
8117 return;
8118 }
8119 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008120 // transform-related properties have changed since the last time someone
8121 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008122
8123 // Figure out if we need to update the pivot point
8124 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008125 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8126 info.mPrevWidth = mRight - mLeft;
8127 info.mPrevHeight = mBottom - mTop;
8128 info.mPivotX = info.mPrevWidth / 2f;
8129 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008130 }
8131 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008132 info.mMatrix.reset();
8133 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8134 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8135 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8136 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008137 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008138 if (info.mCamera == null) {
8139 info.mCamera = new Camera();
8140 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008141 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008142 info.mCamera.save();
8143 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8144 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8145 info.mCamera.getMatrix(info.matrix3D);
8146 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8147 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8148 info.mPivotY + info.mTranslationY);
8149 info.mMatrix.postConcat(info.matrix3D);
8150 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008151 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008152 info.mMatrixDirty = false;
8153 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8154 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008155 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008156 }
8157
8158 /**
8159 * Utility method to retrieve the inverse of the current mMatrix property.
8160 * We cache the matrix to avoid recalculating it when transform properties
8161 * have not changed.
8162 *
8163 * @return The inverse of the current matrix of this view.
8164 */
Jeff Brown86671742010-09-30 20:00:15 -07008165 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008166 final TransformationInfo info = mTransformationInfo;
8167 if (info != null) {
8168 updateMatrix();
8169 if (info.mInverseMatrixDirty) {
8170 if (info.mInverseMatrix == null) {
8171 info.mInverseMatrix = new Matrix();
8172 }
8173 info.mMatrix.invert(info.mInverseMatrix);
8174 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008175 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008176 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008177 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008178 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008179 }
8180
8181 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008182 * Gets the distance along the Z axis from the camera to this view.
8183 *
8184 * @see #setCameraDistance(float)
8185 *
8186 * @return The distance along the Z axis.
8187 */
8188 public float getCameraDistance() {
8189 ensureTransformationInfo();
8190 final float dpi = mResources.getDisplayMetrics().densityDpi;
8191 final TransformationInfo info = mTransformationInfo;
8192 if (info.mCamera == null) {
8193 info.mCamera = new Camera();
8194 info.matrix3D = new Matrix();
8195 }
8196 return -(info.mCamera.getLocationZ() * dpi);
8197 }
8198
8199 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008200 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8201 * views are drawn) from the camera to this view. The camera's distance
8202 * affects 3D transformations, for instance rotations around the X and Y
8203 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008204 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008205 * use a camera distance that's greater than the height (X axis rotation) or
8206 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008207 *
Romain Guya5364ee2011-02-24 14:46:04 -08008208 * <p>The distance of the camera from the view plane can have an affect on the
8209 * perspective distortion of the view when it is rotated around the x or y axis.
8210 * For example, a large distance will result in a large viewing angle, and there
8211 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008212 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008213 * also result in some drawing artifacts if the rotated view ends up partially
8214 * behind the camera (which is why the recommendation is to use a distance at
8215 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008216 *
Romain Guya5364ee2011-02-24 14:46:04 -08008217 * <p>The distance is expressed in "depth pixels." The default distance depends
8218 * on the screen density. For instance, on a medium density display, the
8219 * default distance is 1280. On a high density display, the default distance
8220 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008221 *
Romain Guya5364ee2011-02-24 14:46:04 -08008222 * <p>If you want to specify a distance that leads to visually consistent
8223 * results across various densities, use the following formula:</p>
8224 * <pre>
8225 * float scale = context.getResources().getDisplayMetrics().density;
8226 * view.setCameraDistance(distance * scale);
8227 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008228 *
Romain Guya5364ee2011-02-24 14:46:04 -08008229 * <p>The density scale factor of a high density display is 1.5,
8230 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008231 *
Romain Guya5364ee2011-02-24 14:46:04 -08008232 * @param distance The distance in "depth pixels", if negative the opposite
8233 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008234 *
8235 * @see #setRotationX(float)
8236 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008237 */
8238 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008239 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008240
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008241 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008242 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008243 final TransformationInfo info = mTransformationInfo;
8244 if (info.mCamera == null) {
8245 info.mCamera = new Camera();
8246 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008247 }
8248
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008249 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8250 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008251
Chet Haase9d1992d2012-03-13 11:03:25 -07008252 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008253 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008254 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008255 }
Romain Guya5364ee2011-02-24 14:46:04 -08008256 }
8257
8258 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008259 * The degrees that the view is rotated around the pivot point.
8260 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008261 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008262 * @see #getPivotX()
8263 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008264 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008265 * @return The degrees of rotation.
8266 */
Chet Haasea5531132012-02-02 13:41:44 -08008267 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008268 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008269 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008270 }
8271
8272 /**
Chet Haase897247b2010-09-09 14:54:47 -07008273 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8274 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008275 *
8276 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008277 *
8278 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008279 * @see #getPivotX()
8280 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008281 * @see #setRotationX(float)
8282 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008283 *
8284 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008285 */
8286 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008287 ensureTransformationInfo();
8288 final TransformationInfo info = mTransformationInfo;
8289 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008290 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008291 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008292 info.mRotation = rotation;
8293 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008294 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008295 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008296 mDisplayList.setRotation(rotation);
8297 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008298 }
8299 }
8300
8301 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008302 * The degrees that the view is rotated around the vertical axis through the pivot point.
8303 *
8304 * @see #getPivotX()
8305 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008306 * @see #setRotationY(float)
8307 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008308 * @return The degrees of Y rotation.
8309 */
Chet Haasea5531132012-02-02 13:41:44 -08008310 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008311 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008312 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008313 }
8314
8315 /**
Chet Haase897247b2010-09-09 14:54:47 -07008316 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8317 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8318 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008319 *
Romain Guya5364ee2011-02-24 14:46:04 -08008320 * When rotating large views, it is recommended to adjust the camera distance
8321 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008322 *
8323 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008324 *
8325 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008326 * @see #getPivotX()
8327 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008328 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008329 * @see #setRotationX(float)
8330 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008331 *
8332 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008333 */
8334 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008335 ensureTransformationInfo();
8336 final TransformationInfo info = mTransformationInfo;
8337 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008338 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008339 info.mRotationY = rotationY;
8340 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008341 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008342 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008343 mDisplayList.setRotationY(rotationY);
8344 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008345 }
8346 }
8347
8348 /**
8349 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8350 *
8351 * @see #getPivotX()
8352 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008353 * @see #setRotationX(float)
8354 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008355 * @return The degrees of X rotation.
8356 */
Chet Haasea5531132012-02-02 13:41:44 -08008357 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008358 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008359 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008360 }
8361
8362 /**
Chet Haase897247b2010-09-09 14:54:47 -07008363 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8364 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8365 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008366 *
Romain Guya5364ee2011-02-24 14:46:04 -08008367 * When rotating large views, it is recommended to adjust the camera distance
8368 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008369 *
8370 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008371 *
8372 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008373 * @see #getPivotX()
8374 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008375 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008376 * @see #setRotationY(float)
8377 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008378 *
8379 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008380 */
8381 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008382 ensureTransformationInfo();
8383 final TransformationInfo info = mTransformationInfo;
8384 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008385 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008386 info.mRotationX = rotationX;
8387 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008388 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008389 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008390 mDisplayList.setRotationX(rotationX);
8391 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008392 }
8393 }
8394
8395 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008396 * The amount that the view is scaled in x around the pivot point, as a proportion of
8397 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8398 *
Joe Onorato93162322010-09-16 15:42:01 -04008399 * <p>By default, this is 1.0f.
8400 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008401 * @see #getPivotX()
8402 * @see #getPivotY()
8403 * @return The scaling factor.
8404 */
Chet Haasea5531132012-02-02 13:41:44 -08008405 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008406 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008407 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008408 }
8409
8410 /**
8411 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8412 * the view's unscaled width. A value of 1 means that no scaling is applied.
8413 *
8414 * @param scaleX The scaling factor.
8415 * @see #getPivotX()
8416 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008417 *
8418 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008419 */
8420 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008421 ensureTransformationInfo();
8422 final TransformationInfo info = mTransformationInfo;
8423 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008424 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008425 info.mScaleX = scaleX;
8426 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008427 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008428 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008429 mDisplayList.setScaleX(scaleX);
8430 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008431 }
8432 }
8433
8434 /**
8435 * The amount that the view is scaled in y around the pivot point, as a proportion of
8436 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8437 *
Joe Onorato93162322010-09-16 15:42:01 -04008438 * <p>By default, this is 1.0f.
8439 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008440 * @see #getPivotX()
8441 * @see #getPivotY()
8442 * @return The scaling factor.
8443 */
Chet Haasea5531132012-02-02 13:41:44 -08008444 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008445 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008446 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008447 }
8448
8449 /**
8450 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8451 * the view's unscaled width. A value of 1 means that no scaling is applied.
8452 *
8453 * @param scaleY The scaling factor.
8454 * @see #getPivotX()
8455 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008456 *
8457 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008458 */
8459 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008460 ensureTransformationInfo();
8461 final TransformationInfo info = mTransformationInfo;
8462 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008463 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008464 info.mScaleY = scaleY;
8465 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008466 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008467 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008468 mDisplayList.setScaleY(scaleY);
8469 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008470 }
8471 }
8472
8473 /**
8474 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8475 * and {@link #setScaleX(float) scaled}.
8476 *
8477 * @see #getRotation()
8478 * @see #getScaleX()
8479 * @see #getScaleY()
8480 * @see #getPivotY()
8481 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008482 *
8483 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008484 */
Chet Haasea5531132012-02-02 13:41:44 -08008485 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008486 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008487 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008488 }
8489
8490 /**
8491 * Sets the x location of the point around which the view is
8492 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008493 * By default, the pivot point is centered on the object.
8494 * Setting this property disables this behavior and causes the view to use only the
8495 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008496 *
8497 * @param pivotX The x location of the pivot point.
8498 * @see #getRotation()
8499 * @see #getScaleX()
8500 * @see #getScaleY()
8501 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008502 *
8503 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008504 */
8505 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008506 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008507 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008508 final TransformationInfo info = mTransformationInfo;
8509 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008510 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008511 info.mPivotX = pivotX;
8512 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008513 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008514 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008515 mDisplayList.setPivotX(pivotX);
8516 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008517 }
8518 }
8519
8520 /**
8521 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8522 * and {@link #setScaleY(float) scaled}.
8523 *
8524 * @see #getRotation()
8525 * @see #getScaleX()
8526 * @see #getScaleY()
8527 * @see #getPivotY()
8528 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008529 *
8530 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008531 */
Chet Haasea5531132012-02-02 13:41:44 -08008532 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008533 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008534 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008535 }
8536
8537 /**
8538 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008539 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8540 * Setting this property disables this behavior and causes the view to use only the
8541 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008542 *
8543 * @param pivotY The y location of the pivot point.
8544 * @see #getRotation()
8545 * @see #getScaleX()
8546 * @see #getScaleY()
8547 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008548 *
8549 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008550 */
8551 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008552 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008553 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008554 final TransformationInfo info = mTransformationInfo;
8555 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008556 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008557 info.mPivotY = pivotY;
8558 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008559 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008560 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008561 mDisplayList.setPivotY(pivotY);
8562 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008563 }
8564 }
8565
8566 /**
8567 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8568 * completely transparent and 1 means the view is completely opaque.
8569 *
Joe Onorato93162322010-09-16 15:42:01 -04008570 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008571 * @return The opacity of the view.
8572 */
Chet Haasea5531132012-02-02 13:41:44 -08008573 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008574 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008575 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008576 }
8577
8578 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008579 * Returns whether this View has content which overlaps. This function, intended to be
8580 * overridden by specific View types, is an optimization when alpha is set on a view. If
8581 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8582 * and then composited it into place, which can be expensive. If the view has no overlapping
8583 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8584 * An example of overlapping rendering is a TextView with a background image, such as a
8585 * Button. An example of non-overlapping rendering is a TextView with no background, or
8586 * an ImageView with only the foreground image. The default implementation returns true;
8587 * subclasses should override if they have cases which can be optimized.
8588 *
8589 * @return true if the content in this view might overlap, false otherwise.
8590 */
8591 public boolean hasOverlappingRendering() {
8592 return true;
8593 }
8594
8595 /**
Romain Guy171c5922011-01-06 10:04:23 -08008596 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8597 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008598 *
Romain Guy171c5922011-01-06 10:04:23 -08008599 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8600 * responsible for applying the opacity itself. Otherwise, calling this method is
8601 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008602 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008603 *
Chet Haasea5531132012-02-02 13:41:44 -08008604 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8605 * performance implications. It is generally best to use the alpha property sparingly and
8606 * transiently, as in the case of fading animations.</p>
8607 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008608 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008609 *
Joe Malin32736f02011-01-19 16:14:20 -08008610 * @see #setLayerType(int, android.graphics.Paint)
8611 *
Chet Haase73066682010-11-29 15:55:32 -08008612 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008613 */
8614 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008615 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008616 if (mTransformationInfo.mAlpha != alpha) {
8617 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008618 if (onSetAlpha((int) (alpha * 255))) {
8619 mPrivateFlags |= ALPHA_SET;
8620 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008621 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008622 invalidate(true);
8623 } else {
8624 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008625 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008626 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008627 mDisplayList.setAlpha(alpha);
8628 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008629 }
Chet Haaseed032702010-10-01 14:05:54 -07008630 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 }
8632
8633 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008634 * Faster version of setAlpha() which performs the same steps except there are
8635 * no calls to invalidate(). The caller of this function should perform proper invalidation
8636 * on the parent and this object. The return value indicates whether the subclass handles
8637 * alpha (the return value for onSetAlpha()).
8638 *
8639 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008640 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8641 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008642 */
8643 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008644 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008645 if (mTransformationInfo.mAlpha != alpha) {
8646 mTransformationInfo.mAlpha = alpha;
8647 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8648 if (subclassHandlesAlpha) {
8649 mPrivateFlags |= ALPHA_SET;
8650 return true;
8651 } else {
8652 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008653 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008654 mDisplayList.setAlpha(alpha);
8655 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008656 }
Chet Haasea00f3862011-02-22 06:34:40 -08008657 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008658 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008659 }
8660
8661 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008662 * Top position of this view relative to its parent.
8663 *
8664 * @return The top of this view, in pixels.
8665 */
8666 @ViewDebug.CapturedViewProperty
8667 public final int getTop() {
8668 return mTop;
8669 }
8670
8671 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008672 * Sets the top position of this view relative to its parent. This method is meant to be called
8673 * by the layout system and should not generally be called otherwise, because the property
8674 * may be changed at any time by the layout.
8675 *
8676 * @param top The top of this view, in pixels.
8677 */
8678 public final void setTop(int top) {
8679 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008680 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008681 final boolean matrixIsIdentity = mTransformationInfo == null
8682 || mTransformationInfo.mMatrixIsIdentity;
8683 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008684 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008685 int minTop;
8686 int yLoc;
8687 if (top < mTop) {
8688 minTop = top;
8689 yLoc = top - mTop;
8690 } else {
8691 minTop = mTop;
8692 yLoc = 0;
8693 }
Chet Haasee9140a72011-02-16 16:23:29 -08008694 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008695 }
8696 } else {
8697 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008698 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008699 }
8700
Chet Haaseed032702010-10-01 14:05:54 -07008701 int width = mRight - mLeft;
8702 int oldHeight = mBottom - mTop;
8703
Chet Haase21cd1382010-09-01 17:42:29 -07008704 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07008705 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008706 mDisplayList.setTop(mTop);
8707 }
Chet Haase21cd1382010-09-01 17:42:29 -07008708
Chet Haaseed032702010-10-01 14:05:54 -07008709 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8710
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008711 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008712 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8713 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008714 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008715 }
Chet Haase21cd1382010-09-01 17:42:29 -07008716 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008717 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008718 }
Chet Haase55dbb652010-12-21 20:15:08 -08008719 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008720 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008721 }
8722 }
8723
8724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008725 * Bottom position of this view relative to its parent.
8726 *
8727 * @return The bottom of this view, in pixels.
8728 */
8729 @ViewDebug.CapturedViewProperty
8730 public final int getBottom() {
8731 return mBottom;
8732 }
8733
8734 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008735 * True if this view has changed since the last time being drawn.
8736 *
8737 * @return The dirty state of this view.
8738 */
8739 public boolean isDirty() {
8740 return (mPrivateFlags & DIRTY_MASK) != 0;
8741 }
8742
8743 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008744 * Sets the bottom position of this view relative to its parent. This method is meant to be
8745 * called by the layout system and should not generally be called otherwise, because the
8746 * property may be changed at any time by the layout.
8747 *
8748 * @param bottom The bottom of this view, in pixels.
8749 */
8750 public final void setBottom(int bottom) {
8751 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008752 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008753 final boolean matrixIsIdentity = mTransformationInfo == null
8754 || mTransformationInfo.mMatrixIsIdentity;
8755 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008756 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008757 int maxBottom;
8758 if (bottom < mBottom) {
8759 maxBottom = mBottom;
8760 } else {
8761 maxBottom = bottom;
8762 }
Chet Haasee9140a72011-02-16 16:23:29 -08008763 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008764 }
8765 } else {
8766 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008767 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008768 }
8769
Chet Haaseed032702010-10-01 14:05:54 -07008770 int width = mRight - mLeft;
8771 int oldHeight = mBottom - mTop;
8772
Chet Haase21cd1382010-09-01 17:42:29 -07008773 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07008774 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008775 mDisplayList.setBottom(mBottom);
8776 }
Chet Haase21cd1382010-09-01 17:42:29 -07008777
Chet Haaseed032702010-10-01 14:05:54 -07008778 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8779
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008780 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008781 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8782 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008783 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008784 }
Chet Haase21cd1382010-09-01 17:42:29 -07008785 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008786 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008787 }
Chet Haase55dbb652010-12-21 20:15:08 -08008788 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008789 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008790 }
8791 }
8792
8793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008794 * Left position of this view relative to its parent.
8795 *
8796 * @return The left edge of this view, in pixels.
8797 */
8798 @ViewDebug.CapturedViewProperty
8799 public final int getLeft() {
8800 return mLeft;
8801 }
8802
8803 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008804 * Sets the left position of this view relative to its parent. This method is meant to be called
8805 * by the layout system and should not generally be called otherwise, because the property
8806 * may be changed at any time by the layout.
8807 *
8808 * @param left The bottom of this view, in pixels.
8809 */
8810 public final void setLeft(int left) {
8811 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008812 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008813 final boolean matrixIsIdentity = mTransformationInfo == null
8814 || mTransformationInfo.mMatrixIsIdentity;
8815 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008816 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008817 int minLeft;
8818 int xLoc;
8819 if (left < mLeft) {
8820 minLeft = left;
8821 xLoc = left - mLeft;
8822 } else {
8823 minLeft = mLeft;
8824 xLoc = 0;
8825 }
Chet Haasee9140a72011-02-16 16:23:29 -08008826 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008827 }
8828 } else {
8829 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008830 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008831 }
8832
Chet Haaseed032702010-10-01 14:05:54 -07008833 int oldWidth = mRight - mLeft;
8834 int height = mBottom - mTop;
8835
Chet Haase21cd1382010-09-01 17:42:29 -07008836 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07008837 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008838 mDisplayList.setLeft(left);
8839 }
Chet Haase21cd1382010-09-01 17:42:29 -07008840
Chet Haaseed032702010-10-01 14:05:54 -07008841 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8842
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008843 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008844 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8845 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008846 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008847 }
Chet Haase21cd1382010-09-01 17:42:29 -07008848 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008849 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008850 }
Chet Haase55dbb652010-12-21 20:15:08 -08008851 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008852 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008853 }
8854 }
8855
8856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 * Right position of this view relative to its parent.
8858 *
8859 * @return The right edge of this view, in pixels.
8860 */
8861 @ViewDebug.CapturedViewProperty
8862 public final int getRight() {
8863 return mRight;
8864 }
8865
8866 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008867 * Sets the right position of this view relative to its parent. This method is meant to be called
8868 * by the layout system and should not generally be called otherwise, because the property
8869 * may be changed at any time by the layout.
8870 *
8871 * @param right The bottom of this view, in pixels.
8872 */
8873 public final void setRight(int right) {
8874 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008875 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008876 final boolean matrixIsIdentity = mTransformationInfo == null
8877 || mTransformationInfo.mMatrixIsIdentity;
8878 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008879 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008880 int maxRight;
8881 if (right < mRight) {
8882 maxRight = mRight;
8883 } else {
8884 maxRight = right;
8885 }
Chet Haasee9140a72011-02-16 16:23:29 -08008886 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008887 }
8888 } else {
8889 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008890 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008891 }
8892
Chet Haaseed032702010-10-01 14:05:54 -07008893 int oldWidth = mRight - mLeft;
8894 int height = mBottom - mTop;
8895
Chet Haase21cd1382010-09-01 17:42:29 -07008896 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07008897 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008898 mDisplayList.setRight(mRight);
8899 }
Chet Haase21cd1382010-09-01 17:42:29 -07008900
Chet Haaseed032702010-10-01 14:05:54 -07008901 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8902
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008903 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008904 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8905 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008906 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008907 }
Chet Haase21cd1382010-09-01 17:42:29 -07008908 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008909 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008910 }
Chet Haase55dbb652010-12-21 20:15:08 -08008911 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008912 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008913 }
8914 }
8915
8916 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008917 * The visual x position of this view, in pixels. This is equivalent to the
8918 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008919 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008920 *
Chet Haasedf030d22010-07-30 17:22:38 -07008921 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008922 */
Chet Haasea5531132012-02-02 13:41:44 -08008923 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008924 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008925 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008926 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008927
Chet Haasedf030d22010-07-30 17:22:38 -07008928 /**
8929 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8930 * {@link #setTranslationX(float) translationX} property to be the difference between
8931 * the x value passed in and the current {@link #getLeft() left} property.
8932 *
8933 * @param x The visual x position of this view, in pixels.
8934 */
8935 public void setX(float x) {
8936 setTranslationX(x - mLeft);
8937 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008938
Chet Haasedf030d22010-07-30 17:22:38 -07008939 /**
8940 * The visual y position of this view, in pixels. This is equivalent to the
8941 * {@link #setTranslationY(float) translationY} property plus the current
8942 * {@link #getTop() top} property.
8943 *
8944 * @return The visual y position of this view, in pixels.
8945 */
Chet Haasea5531132012-02-02 13:41:44 -08008946 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008947 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008948 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008949 }
8950
8951 /**
8952 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8953 * {@link #setTranslationY(float) translationY} property to be the difference between
8954 * the y value passed in and the current {@link #getTop() top} property.
8955 *
8956 * @param y The visual y position of this view, in pixels.
8957 */
8958 public void setY(float y) {
8959 setTranslationY(y - mTop);
8960 }
8961
8962
8963 /**
8964 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8965 * This position is post-layout, in addition to wherever the object's
8966 * layout placed it.
8967 *
8968 * @return The horizontal position of this view relative to its left position, in pixels.
8969 */
Chet Haasea5531132012-02-02 13:41:44 -08008970 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008971 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008972 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008973 }
8974
8975 /**
8976 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8977 * This effectively positions the object post-layout, in addition to wherever the object's
8978 * layout placed it.
8979 *
8980 * @param translationX The horizontal position of this view relative to its left position,
8981 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008982 *
8983 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008984 */
8985 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008986 ensureTransformationInfo();
8987 final TransformationInfo info = mTransformationInfo;
8988 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008989 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008990 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 info.mTranslationX = translationX;
8992 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008993 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008994 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008995 mDisplayList.setTranslationX(translationX);
8996 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008997 }
8998 }
8999
9000 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009001 * The horizontal location of this view relative to its {@link #getTop() top} position.
9002 * This position is post-layout, in addition to wherever the object's
9003 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009004 *
Chet Haasedf030d22010-07-30 17:22:38 -07009005 * @return The vertical position of this view relative to its top position,
9006 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009007 */
Chet Haasea5531132012-02-02 13:41:44 -08009008 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009009 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009010 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009011 }
9012
9013 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009014 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9015 * This effectively positions the object post-layout, in addition to wherever the object's
9016 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 *
Chet Haasedf030d22010-07-30 17:22:38 -07009018 * @param translationY The vertical position of this view relative to its top position,
9019 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009020 *
9021 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009022 */
Chet Haasedf030d22010-07-30 17:22:38 -07009023 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009024 ensureTransformationInfo();
9025 final TransformationInfo info = mTransformationInfo;
9026 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009027 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009028 info.mTranslationY = translationY;
9029 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009030 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009031 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009032 mDisplayList.setTranslationY(translationY);
9033 }
Chet Haasedf030d22010-07-30 17:22:38 -07009034 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009035 }
9036
9037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 * Hit rectangle in parent's coordinates
9039 *
9040 * @param outRect The hit rectangle of the view.
9041 */
9042 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009043 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009044 final TransformationInfo info = mTransformationInfo;
9045 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 outRect.set(mLeft, mTop, mRight, mBottom);
9047 } else {
9048 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009049 tmpRect.set(-info.mPivotX, -info.mPivotY,
9050 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9051 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009052 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9053 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009054 }
9055 }
9056
9057 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009058 * Determines whether the given point, in local coordinates is inside the view.
9059 */
9060 /*package*/ final boolean pointInView(float localX, float localY) {
9061 return localX >= 0 && localX < (mRight - mLeft)
9062 && localY >= 0 && localY < (mBottom - mTop);
9063 }
9064
9065 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009066 * Utility method to determine whether the given point, in local coordinates,
9067 * is inside the view, where the area of the view is expanded by the slop factor.
9068 * This method is called while processing touch-move events to determine if the event
9069 * is still within the view.
9070 */
9071 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009072 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009073 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009074 }
9075
9076 /**
9077 * When a view has focus and the user navigates away from it, the next view is searched for
9078 * starting from the rectangle filled in by this method.
9079 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009080 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9081 * of the view. However, if your view maintains some idea of internal selection,
9082 * such as a cursor, or a selected row or column, you should override this method and
9083 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 *
9085 * @param r The rectangle to fill in, in this view's coordinates.
9086 */
9087 public void getFocusedRect(Rect r) {
9088 getDrawingRect(r);
9089 }
9090
9091 /**
9092 * If some part of this view is not clipped by any of its parents, then
9093 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009094 * coordinates (without taking possible View rotations into account), offset
9095 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9096 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 *
9098 * @param r If true is returned, r holds the global coordinates of the
9099 * visible portion of this view.
9100 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9101 * between this view and its root. globalOffet may be null.
9102 * @return true if r is non-empty (i.e. part of the view is visible at the
9103 * root level.
9104 */
9105 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9106 int width = mRight - mLeft;
9107 int height = mBottom - mTop;
9108 if (width > 0 && height > 0) {
9109 r.set(0, 0, width, height);
9110 if (globalOffset != null) {
9111 globalOffset.set(-mScrollX, -mScrollY);
9112 }
9113 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9114 }
9115 return false;
9116 }
9117
9118 public final boolean getGlobalVisibleRect(Rect r) {
9119 return getGlobalVisibleRect(r, null);
9120 }
9121
9122 public final boolean getLocalVisibleRect(Rect r) {
9123 Point offset = new Point();
9124 if (getGlobalVisibleRect(r, offset)) {
9125 r.offset(-offset.x, -offset.y); // make r local
9126 return true;
9127 }
9128 return false;
9129 }
9130
9131 /**
9132 * Offset this view's vertical location by the specified number of pixels.
9133 *
9134 * @param offset the number of pixels to offset the view by
9135 */
9136 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009137 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009138 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009139 final boolean matrixIsIdentity = mTransformationInfo == null
9140 || mTransformationInfo.mMatrixIsIdentity;
9141 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009142 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009143 invalidateViewProperty(false, false);
9144 } else {
9145 final ViewParent p = mParent;
9146 if (p != null && mAttachInfo != null) {
9147 final Rect r = mAttachInfo.mTmpInvalRect;
9148 int minTop;
9149 int maxBottom;
9150 int yLoc;
9151 if (offset < 0) {
9152 minTop = mTop + offset;
9153 maxBottom = mBottom;
9154 yLoc = offset;
9155 } else {
9156 minTop = mTop;
9157 maxBottom = mBottom + offset;
9158 yLoc = 0;
9159 }
9160 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9161 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009162 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009163 }
9164 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009165 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009166 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009167
Chet Haasec3aa3612010-06-17 08:50:37 -07009168 mTop += offset;
9169 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009170 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009171 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009172 invalidateViewProperty(false, false);
9173 } else {
9174 if (!matrixIsIdentity) {
9175 invalidateViewProperty(false, true);
9176 }
9177 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009178 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 }
9181
9182 /**
9183 * Offset this view's horizontal location by the specified amount of pixels.
9184 *
9185 * @param offset the numer of pixels to offset the view by
9186 */
9187 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009188 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009189 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009190 final boolean matrixIsIdentity = mTransformationInfo == null
9191 || mTransformationInfo.mMatrixIsIdentity;
9192 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009193 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009194 invalidateViewProperty(false, false);
9195 } else {
9196 final ViewParent p = mParent;
9197 if (p != null && mAttachInfo != null) {
9198 final Rect r = mAttachInfo.mTmpInvalRect;
9199 int minLeft;
9200 int maxRight;
9201 if (offset < 0) {
9202 minLeft = mLeft + offset;
9203 maxRight = mRight;
9204 } else {
9205 minLeft = mLeft;
9206 maxRight = mRight + offset;
9207 }
9208 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9209 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009210 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009211 }
9212 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009213 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009214 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009215
Chet Haasec3aa3612010-06-17 08:50:37 -07009216 mLeft += offset;
9217 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009218 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009219 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009220 invalidateViewProperty(false, false);
9221 } else {
9222 if (!matrixIsIdentity) {
9223 invalidateViewProperty(false, true);
9224 }
9225 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009226 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 }
9229
9230 /**
9231 * Get the LayoutParams associated with this view. All views should have
9232 * layout parameters. These supply parameters to the <i>parent</i> of this
9233 * view specifying how it should be arranged. There are many subclasses of
9234 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9235 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009236 *
9237 * This method may return null if this View is not attached to a parent
9238 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9239 * was not invoked successfully. When a View is attached to a parent
9240 * ViewGroup, this method must not return null.
9241 *
9242 * @return The LayoutParams associated with this view, or null if no
9243 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009244 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009245 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009246 public ViewGroup.LayoutParams getLayoutParams() {
9247 return mLayoutParams;
9248 }
9249
9250 /**
9251 * Set the layout parameters associated with this view. These supply
9252 * parameters to the <i>parent</i> of this view specifying how it should be
9253 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9254 * correspond to the different subclasses of ViewGroup that are responsible
9255 * for arranging their children.
9256 *
Romain Guy01c174b2011-02-22 11:51:06 -08009257 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 */
9259 public void setLayoutParams(ViewGroup.LayoutParams params) {
9260 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009261 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 }
9263 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009264 if (mParent instanceof ViewGroup) {
9265 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009267 requestLayout();
9268 }
9269
9270 /**
9271 * Set the scrolled position of your view. This will cause a call to
9272 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9273 * invalidated.
9274 * @param x the x position to scroll to
9275 * @param y the y position to scroll to
9276 */
9277 public void scrollTo(int x, int y) {
9278 if (mScrollX != x || mScrollY != y) {
9279 int oldX = mScrollX;
9280 int oldY = mScrollY;
9281 mScrollX = x;
9282 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009283 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009284 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009285 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009286 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 }
9289 }
9290
9291 /**
9292 * Move the scrolled position of your view. This will cause a call to
9293 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9294 * invalidated.
9295 * @param x the amount of pixels to scroll by horizontally
9296 * @param y the amount of pixels to scroll by vertically
9297 */
9298 public void scrollBy(int x, int y) {
9299 scrollTo(mScrollX + x, mScrollY + y);
9300 }
9301
9302 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009303 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9304 * animation to fade the scrollbars out after a default delay. If a subclass
9305 * provides animated scrolling, the start delay should equal the duration
9306 * of the scrolling animation.</p>
9307 *
9308 * <p>The animation starts only if at least one of the scrollbars is
9309 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9310 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9311 * this method returns true, and false otherwise. If the animation is
9312 * started, this method calls {@link #invalidate()}; in that case the
9313 * caller should not call {@link #invalidate()}.</p>
9314 *
9315 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009316 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009317 *
9318 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9319 * and {@link #scrollTo(int, int)}.</p>
9320 *
9321 * @return true if the animation is played, false otherwise
9322 *
9323 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009324 * @see #scrollBy(int, int)
9325 * @see #scrollTo(int, int)
9326 * @see #isHorizontalScrollBarEnabled()
9327 * @see #isVerticalScrollBarEnabled()
9328 * @see #setHorizontalScrollBarEnabled(boolean)
9329 * @see #setVerticalScrollBarEnabled(boolean)
9330 */
9331 protected boolean awakenScrollBars() {
9332 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009333 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009334 }
9335
9336 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009337 * Trigger the scrollbars to draw.
9338 * This method differs from awakenScrollBars() only in its default duration.
9339 * initialAwakenScrollBars() will show the scroll bars for longer than
9340 * usual to give the user more of a chance to notice them.
9341 *
9342 * @return true if the animation is played, false otherwise.
9343 */
9344 private boolean initialAwakenScrollBars() {
9345 return mScrollCache != null &&
9346 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9347 }
9348
9349 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009350 * <p>
9351 * Trigger the scrollbars to draw. When invoked this method starts an
9352 * animation to fade the scrollbars out after a fixed delay. If a subclass
9353 * provides animated scrolling, the start delay should equal the duration of
9354 * the scrolling animation.
9355 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009356 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009357 * <p>
9358 * The animation starts only if at least one of the scrollbars is enabled,
9359 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9360 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9361 * this method returns true, and false otherwise. If the animation is
9362 * started, this method calls {@link #invalidate()}; in that case the caller
9363 * should not call {@link #invalidate()}.
9364 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009365 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009366 * <p>
9367 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009368 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009369 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009370 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009371 * @param startDelay the delay, in milliseconds, after which the animation
9372 * should start; when the delay is 0, the animation starts
9373 * immediately
9374 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009375 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009376 * @see #scrollBy(int, int)
9377 * @see #scrollTo(int, int)
9378 * @see #isHorizontalScrollBarEnabled()
9379 * @see #isVerticalScrollBarEnabled()
9380 * @see #setHorizontalScrollBarEnabled(boolean)
9381 * @see #setVerticalScrollBarEnabled(boolean)
9382 */
9383 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009384 return awakenScrollBars(startDelay, true);
9385 }
Joe Malin32736f02011-01-19 16:14:20 -08009386
Mike Cleron290947b2009-09-29 18:34:32 -07009387 /**
9388 * <p>
9389 * Trigger the scrollbars to draw. When invoked this method starts an
9390 * animation to fade the scrollbars out after a fixed delay. If a subclass
9391 * provides animated scrolling, the start delay should equal the duration of
9392 * the scrolling animation.
9393 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009394 *
Mike Cleron290947b2009-09-29 18:34:32 -07009395 * <p>
9396 * The animation starts only if at least one of the scrollbars is enabled,
9397 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9398 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9399 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009400 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009401 * is set to true; in that case the caller
9402 * should not call {@link #invalidate()}.
9403 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009404 *
Mike Cleron290947b2009-09-29 18:34:32 -07009405 * <p>
9406 * This method should be invoked everytime a subclass directly updates the
9407 * scroll parameters.
9408 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009409 *
Mike Cleron290947b2009-09-29 18:34:32 -07009410 * @param startDelay the delay, in milliseconds, after which the animation
9411 * should start; when the delay is 0, the animation starts
9412 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009413 *
Mike Cleron290947b2009-09-29 18:34:32 -07009414 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009415 *
Mike Cleron290947b2009-09-29 18:34:32 -07009416 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009417 *
Mike Cleron290947b2009-09-29 18:34:32 -07009418 * @see #scrollBy(int, int)
9419 * @see #scrollTo(int, int)
9420 * @see #isHorizontalScrollBarEnabled()
9421 * @see #isVerticalScrollBarEnabled()
9422 * @see #setHorizontalScrollBarEnabled(boolean)
9423 * @see #setVerticalScrollBarEnabled(boolean)
9424 */
9425 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009426 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009427
Mike Cleronf116bf82009-09-27 19:14:12 -07009428 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9429 return false;
9430 }
9431
9432 if (scrollCache.scrollBar == null) {
9433 scrollCache.scrollBar = new ScrollBarDrawable();
9434 }
9435
9436 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9437
Mike Cleron290947b2009-09-29 18:34:32 -07009438 if (invalidate) {
9439 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009440 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009441 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009442
9443 if (scrollCache.state == ScrollabilityCache.OFF) {
9444 // FIXME: this is copied from WindowManagerService.
9445 // We should get this value from the system when it
9446 // is possible to do so.
9447 final int KEY_REPEAT_FIRST_DELAY = 750;
9448 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9449 }
9450
9451 // Tell mScrollCache when we should start fading. This may
9452 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009453 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009454 scrollCache.fadeStartTime = fadeStartTime;
9455 scrollCache.state = ScrollabilityCache.ON;
9456
9457 // Schedule our fader to run, unscheduling any old ones first
9458 if (mAttachInfo != null) {
9459 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9460 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9461 }
9462
9463 return true;
9464 }
9465
9466 return false;
9467 }
9468
9469 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009470 * Do not invalidate views which are not visible and which are not running an animation. They
9471 * will not get drawn and they should not set dirty flags as if they will be drawn
9472 */
9473 private boolean skipInvalidate() {
9474 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9475 (!(mParent instanceof ViewGroup) ||
9476 !((ViewGroup) mParent).isViewTransitioning(this));
9477 }
9478 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009479 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009480 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9481 * in the future. This must be called from a UI thread. To call from a non-UI
9482 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 *
9484 * WARNING: This method is destructive to dirty.
9485 * @param dirty the rectangle representing the bounds of the dirty region
9486 */
9487 public void invalidate(Rect dirty) {
9488 if (ViewDebug.TRACE_HIERARCHY) {
9489 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9490 }
9491
Chet Haaseaceafe62011-08-26 15:44:33 -07009492 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009493 return;
9494 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009495 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009496 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9497 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009499 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009500 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 final ViewParent p = mParent;
9502 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009503 //noinspection PointlessBooleanExpression,ConstantConditions
9504 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9505 if (p != null && ai != null && ai.mHardwareAccelerated) {
9506 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009507 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009508 p.invalidateChild(this, null);
9509 return;
9510 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 if (p != null && ai != null) {
9513 final int scrollX = mScrollX;
9514 final int scrollY = mScrollY;
9515 final Rect r = ai.mTmpInvalRect;
9516 r.set(dirty.left - scrollX, dirty.top - scrollY,
9517 dirty.right - scrollX, dirty.bottom - scrollY);
9518 mParent.invalidateChild(this, r);
9519 }
9520 }
9521 }
9522
9523 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009524 * 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 -08009525 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009526 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9527 * will be called at some point in the future. This must be called from
9528 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 * @param l the left position of the dirty region
9530 * @param t the top position of the dirty region
9531 * @param r the right position of the dirty region
9532 * @param b the bottom position of the dirty region
9533 */
9534 public void invalidate(int l, int t, int r, int b) {
9535 if (ViewDebug.TRACE_HIERARCHY) {
9536 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9537 }
9538
Chet Haaseaceafe62011-08-26 15:44:33 -07009539 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009540 return;
9541 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009542 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009543 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9544 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009546 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009547 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009548 final ViewParent p = mParent;
9549 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009550 //noinspection PointlessBooleanExpression,ConstantConditions
9551 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9552 if (p != null && ai != null && ai.mHardwareAccelerated) {
9553 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009554 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009555 p.invalidateChild(this, null);
9556 return;
9557 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 if (p != null && ai != null && l < r && t < b) {
9560 final int scrollX = mScrollX;
9561 final int scrollY = mScrollY;
9562 final Rect tmpr = ai.mTmpInvalRect;
9563 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9564 p.invalidateChild(this, tmpr);
9565 }
9566 }
9567 }
9568
9569 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009570 * Invalidate the whole view. If the view is visible,
9571 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9572 * the future. This must be called from a UI thread. To call from a non-UI thread,
9573 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009574 */
9575 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009576 invalidate(true);
9577 }
Joe Malin32736f02011-01-19 16:14:20 -08009578
Chet Haaseed032702010-10-01 14:05:54 -07009579 /**
9580 * This is where the invalidate() work actually happens. A full invalidate()
9581 * causes the drawing cache to be invalidated, but this function can be called with
9582 * invalidateCache set to false to skip that invalidation step for cases that do not
9583 * need it (for example, a component that remains at the same dimensions with the same
9584 * content).
9585 *
9586 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9587 * well. This is usually true for a full invalidate, but may be set to false if the
9588 * View's contents or dimensions have not changed.
9589 */
Romain Guy849d0a32011-02-01 17:20:48 -08009590 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009591 if (ViewDebug.TRACE_HIERARCHY) {
9592 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9593 }
9594
Chet Haaseaceafe62011-08-26 15:44:33 -07009595 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009596 return;
9597 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009598 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009599 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009600 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9601 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009602 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009603 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009604 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009605 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009606 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009609 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009610 //noinspection PointlessBooleanExpression,ConstantConditions
9611 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9612 if (p != null && ai != null && ai.mHardwareAccelerated) {
9613 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009614 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009615 p.invalidateChild(this, null);
9616 return;
9617 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009618 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 if (p != null && ai != null) {
9621 final Rect r = ai.mTmpInvalRect;
9622 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9623 // Don't call invalidate -- we don't want to internally scroll
9624 // our own bounds
9625 p.invalidateChild(this, r);
9626 }
9627 }
9628 }
9629
9630 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009631 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9632 * set any flags or handle all of the cases handled by the default invalidation methods.
9633 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9634 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9635 * walk up the hierarchy, transforming the dirty rect as necessary.
9636 *
9637 * The method also handles normal invalidation logic if display list properties are not
9638 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9639 * backup approach, to handle these cases used in the various property-setting methods.
9640 *
9641 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9642 * are not being used in this view
9643 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9644 * list properties are not being used in this view
9645 */
9646 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009647 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009648 if (invalidateParent) {
9649 invalidateParentCaches();
9650 }
9651 if (forceRedraw) {
9652 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9653 }
9654 invalidate(false);
9655 } else {
9656 final AttachInfo ai = mAttachInfo;
9657 final ViewParent p = mParent;
9658 if (p != null && ai != null) {
9659 final Rect r = ai.mTmpInvalRect;
9660 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9661 if (mParent instanceof ViewGroup) {
9662 ((ViewGroup) mParent).invalidateChildFast(this, r);
9663 } else {
9664 mParent.invalidateChild(this, r);
9665 }
9666 }
9667 }
9668 }
9669
9670 /**
9671 * Utility method to transform a given Rect by the current matrix of this view.
9672 */
9673 void transformRect(final Rect rect) {
9674 if (!getMatrix().isIdentity()) {
9675 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9676 boundingRect.set(rect);
9677 getMatrix().mapRect(boundingRect);
9678 rect.set((int) (boundingRect.left - 0.5f),
9679 (int) (boundingRect.top - 0.5f),
9680 (int) (boundingRect.right + 0.5f),
9681 (int) (boundingRect.bottom + 0.5f));
9682 }
9683 }
9684
9685 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009686 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009687 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9688 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009689 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9690 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009691 *
9692 * @hide
9693 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009694 protected void invalidateParentCaches() {
9695 if (mParent instanceof View) {
9696 ((View) mParent).mPrivateFlags |= INVALIDATED;
9697 }
9698 }
Joe Malin32736f02011-01-19 16:14:20 -08009699
Romain Guy0fd89bf2011-01-26 15:41:30 -08009700 /**
9701 * Used to indicate that the parent of this view should be invalidated. This functionality
9702 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9703 * which is necessary when various parent-managed properties of the view change, such as
9704 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9705 * an invalidation event to the parent.
9706 *
9707 * @hide
9708 */
9709 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009710 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009711 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009712 }
9713 }
9714
9715 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009716 * Indicates whether this View is opaque. An opaque View guarantees that it will
9717 * draw all the pixels overlapping its bounds using a fully opaque color.
9718 *
9719 * Subclasses of View should override this method whenever possible to indicate
9720 * whether an instance is opaque. Opaque Views are treated in a special way by
9721 * the View hierarchy, possibly allowing it to perform optimizations during
9722 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009723 *
Romain Guy24443ea2009-05-11 11:56:30 -07009724 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009726 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009727 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009728 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009729 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9730 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009731 }
9732
Adam Powell20232d02010-12-08 21:08:53 -08009733 /**
9734 * @hide
9735 */
9736 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009737 // Opaque if:
9738 // - Has a background
9739 // - Background is opaque
9740 // - Doesn't have scrollbars or scrollbars are inside overlay
9741
Philip Milne6c8ea062012-04-03 17:38:43 -07009742 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009743 mPrivateFlags |= OPAQUE_BACKGROUND;
9744 } else {
9745 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9746 }
9747
9748 final int flags = mViewFlags;
9749 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9750 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9751 mPrivateFlags |= OPAQUE_SCROLLBARS;
9752 } else {
9753 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9754 }
9755 }
9756
9757 /**
9758 * @hide
9759 */
9760 protected boolean hasOpaqueScrollbars() {
9761 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009762 }
9763
9764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009765 * @return A handler associated with the thread running the View. This
9766 * handler can be used to pump events in the UI events queue.
9767 */
9768 public Handler getHandler() {
9769 if (mAttachInfo != null) {
9770 return mAttachInfo.mHandler;
9771 }
9772 return null;
9773 }
9774
9775 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009776 * Gets the view root associated with the View.
9777 * @return The view root, or null if none.
9778 * @hide
9779 */
9780 public ViewRootImpl getViewRootImpl() {
9781 if (mAttachInfo != null) {
9782 return mAttachInfo.mViewRootImpl;
9783 }
9784 return null;
9785 }
9786
9787 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009788 * <p>Causes the Runnable to be added to the message queue.
9789 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009790 *
Romain Guye63a4f32011-08-11 11:33:31 -07009791 * <p>This method can be invoked from outside of the UI thread
9792 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 *
9794 * @param action The Runnable that will be executed.
9795 *
9796 * @return Returns true if the Runnable was successfully placed in to the
9797 * message queue. Returns false on failure, usually because the
9798 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009799 *
9800 * @see #postDelayed
9801 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 */
9803 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009804 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009805 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009806 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009808 // Assume that post will succeed later
9809 ViewRootImpl.getRunQueue().post(action);
9810 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 }
9812
9813 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009814 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009816 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009817 *
Romain Guye63a4f32011-08-11 11:33:31 -07009818 * <p>This method can be invoked from outside of the UI thread
9819 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 *
9821 * @param action The Runnable that will be executed.
9822 * @param delayMillis The delay (in milliseconds) until the Runnable
9823 * will be executed.
9824 *
9825 * @return true if the Runnable was successfully placed in to the
9826 * message queue. Returns false on failure, usually because the
9827 * looper processing the message queue is exiting. Note that a
9828 * result of true does not mean the Runnable will be processed --
9829 * if the looper is quit before the delivery time of the message
9830 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009831 *
9832 * @see #post
9833 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 */
9835 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009836 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009837 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009838 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009840 // Assume that post will succeed later
9841 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9842 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009843 }
9844
9845 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009846 * <p>Causes the Runnable to execute on the next animation time step.
9847 * The runnable will be run on the user interface thread.</p>
9848 *
9849 * <p>This method can be invoked from outside of the UI thread
9850 * only when this View is attached to a window.</p>
9851 *
9852 * @param action The Runnable that will be executed.
9853 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009854 * @see #postOnAnimationDelayed
9855 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009856 */
9857 public void postOnAnimation(Runnable action) {
9858 final AttachInfo attachInfo = mAttachInfo;
9859 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009860 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9861 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009862 } else {
9863 // Assume that post will succeed later
9864 ViewRootImpl.getRunQueue().post(action);
9865 }
9866 }
9867
9868 /**
9869 * <p>Causes the Runnable to execute on the next animation time step,
9870 * after the specified amount of time elapses.
9871 * The runnable will be run on the user interface thread.</p>
9872 *
9873 * <p>This method can be invoked from outside of the UI thread
9874 * only when this View is attached to a window.</p>
9875 *
9876 * @param action The Runnable that will be executed.
9877 * @param delayMillis The delay (in milliseconds) until the Runnable
9878 * will be executed.
9879 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009880 * @see #postOnAnimation
9881 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009882 */
9883 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9884 final AttachInfo attachInfo = mAttachInfo;
9885 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009886 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9887 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009888 } else {
9889 // Assume that post will succeed later
9890 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9891 }
9892 }
9893
9894 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009895 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009896 *
Romain Guye63a4f32011-08-11 11:33:31 -07009897 * <p>This method can be invoked from outside of the UI thread
9898 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899 *
9900 * @param action The Runnable to remove from the message handling queue
9901 *
9902 * @return true if this view could ask the Handler to remove the Runnable,
9903 * false otherwise. When the returned value is true, the Runnable
9904 * may or may not have been actually removed from the message queue
9905 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009906 *
9907 * @see #post
9908 * @see #postDelayed
9909 * @see #postOnAnimation
9910 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 */
9912 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009913 if (action != null) {
9914 final AttachInfo attachInfo = mAttachInfo;
9915 if (attachInfo != null) {
9916 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009917 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9918 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009919 } else {
9920 // Assume that post will succeed later
9921 ViewRootImpl.getRunQueue().removeCallbacks(action);
9922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 return true;
9925 }
9926
9927 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009928 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9929 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 *
Romain Guye63a4f32011-08-11 11:33:31 -07009931 * <p>This method can be invoked from outside of the UI thread
9932 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009935 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 */
9937 public void postInvalidate() {
9938 postInvalidateDelayed(0);
9939 }
9940
9941 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009942 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9943 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009944 *
Romain Guye63a4f32011-08-11 11:33:31 -07009945 * <p>This method can be invoked from outside of the UI thread
9946 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 *
9948 * @param left The left coordinate of the rectangle to invalidate.
9949 * @param top The top coordinate of the rectangle to invalidate.
9950 * @param right The right coordinate of the rectangle to invalidate.
9951 * @param bottom The bottom coordinate of the rectangle to invalidate.
9952 *
9953 * @see #invalidate(int, int, int, int)
9954 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009955 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 */
9957 public void postInvalidate(int left, int top, int right, int bottom) {
9958 postInvalidateDelayed(0, left, top, right, bottom);
9959 }
9960
9961 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009962 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9963 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009964 *
Romain Guye63a4f32011-08-11 11:33:31 -07009965 * <p>This method can be invoked from outside of the UI thread
9966 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 *
9968 * @param delayMilliseconds the duration in milliseconds to delay the
9969 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009970 *
9971 * @see #invalidate()
9972 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009973 */
9974 public void postInvalidateDelayed(long delayMilliseconds) {
9975 // We try only with the AttachInfo because there's no point in invalidating
9976 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009977 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009978 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009979 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 }
9981 }
9982
9983 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009984 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9985 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009986 *
Romain Guye63a4f32011-08-11 11:33:31 -07009987 * <p>This method can be invoked from outside of the UI thread
9988 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 *
9990 * @param delayMilliseconds the duration in milliseconds to delay the
9991 * invalidation by
9992 * @param left The left coordinate of the rectangle to invalidate.
9993 * @param top The top coordinate of the rectangle to invalidate.
9994 * @param right The right coordinate of the rectangle to invalidate.
9995 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009996 *
9997 * @see #invalidate(int, int, int, int)
9998 * @see #invalidate(Rect)
9999 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 */
10001 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10002 int right, int bottom) {
10003
10004 // We try only with the AttachInfo because there's no point in invalidating
10005 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010006 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010007 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10009 info.target = this;
10010 info.left = left;
10011 info.top = top;
10012 info.right = right;
10013 info.bottom = bottom;
10014
Jeff Browna175a5b2012-02-15 19:18:31 -080010015 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 }
10017 }
10018
10019 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010020 * <p>Cause an invalidate to happen on the next animation time step, typically the
10021 * next display frame.</p>
10022 *
10023 * <p>This method can be invoked from outside of the UI thread
10024 * only when this View is attached to a window.</p>
10025 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010026 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010027 */
10028 public void postInvalidateOnAnimation() {
10029 // We try only with the AttachInfo because there's no point in invalidating
10030 // if we are not attached to our window
10031 final AttachInfo attachInfo = mAttachInfo;
10032 if (attachInfo != null) {
10033 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10034 }
10035 }
10036
10037 /**
10038 * <p>Cause an invalidate of the specified area to happen on the next animation
10039 * time step, typically the next display frame.</p>
10040 *
10041 * <p>This method can be invoked from outside of the UI thread
10042 * only when this View is attached to a window.</p>
10043 *
10044 * @param left The left coordinate of the rectangle to invalidate.
10045 * @param top The top coordinate of the rectangle to invalidate.
10046 * @param right The right coordinate of the rectangle to invalidate.
10047 * @param bottom The bottom coordinate of the rectangle to invalidate.
10048 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010049 * @see #invalidate(int, int, int, int)
10050 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010051 */
10052 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10053 // We try only with the AttachInfo because there's no point in invalidating
10054 // if we are not attached to our window
10055 final AttachInfo attachInfo = mAttachInfo;
10056 if (attachInfo != null) {
10057 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10058 info.target = this;
10059 info.left = left;
10060 info.top = top;
10061 info.right = right;
10062 info.bottom = bottom;
10063
10064 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10065 }
10066 }
10067
10068 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010069 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10070 * This event is sent at most once every
10071 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10072 */
10073 private void postSendViewScrolledAccessibilityEventCallback() {
10074 if (mSendViewScrolledAccessibilityEvent == null) {
10075 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10076 }
10077 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10078 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10079 postDelayed(mSendViewScrolledAccessibilityEvent,
10080 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10081 }
10082 }
10083
10084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 * Called by a parent to request that a child update its values for mScrollX
10086 * and mScrollY if necessary. This will typically be done if the child is
10087 * animating a scroll using a {@link android.widget.Scroller Scroller}
10088 * object.
10089 */
10090 public void computeScroll() {
10091 }
10092
10093 /**
10094 * <p>Indicate whether the horizontal edges are faded when the view is
10095 * scrolled horizontally.</p>
10096 *
10097 * @return true if the horizontal edges should are faded on scroll, false
10098 * otherwise
10099 *
10100 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010101 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010102 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 */
10104 public boolean isHorizontalFadingEdgeEnabled() {
10105 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10106 }
10107
10108 /**
10109 * <p>Define whether the horizontal edges should be faded when this view
10110 * is scrolled horizontally.</p>
10111 *
10112 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10113 * be faded when the view is scrolled
10114 * horizontally
10115 *
10116 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010117 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010118 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 */
10120 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10121 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10122 if (horizontalFadingEdgeEnabled) {
10123 initScrollCache();
10124 }
10125
10126 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10127 }
10128 }
10129
10130 /**
10131 * <p>Indicate whether the vertical edges are faded when the view is
10132 * scrolled horizontally.</p>
10133 *
10134 * @return true if the vertical edges should are faded on scroll, false
10135 * otherwise
10136 *
10137 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010138 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010139 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010140 */
10141 public boolean isVerticalFadingEdgeEnabled() {
10142 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10143 }
10144
10145 /**
10146 * <p>Define whether the vertical edges should be faded when this view
10147 * is scrolled vertically.</p>
10148 *
10149 * @param verticalFadingEdgeEnabled true if the vertical edges should
10150 * be faded when the view is scrolled
10151 * vertically
10152 *
10153 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010154 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010155 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 */
10157 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10158 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10159 if (verticalFadingEdgeEnabled) {
10160 initScrollCache();
10161 }
10162
10163 mViewFlags ^= FADING_EDGE_VERTICAL;
10164 }
10165 }
10166
10167 /**
10168 * Returns the strength, or intensity, of the top faded edge. The strength is
10169 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10170 * returns 0.0 or 1.0 but no value in between.
10171 *
10172 * Subclasses should override this method to provide a smoother fade transition
10173 * when scrolling occurs.
10174 *
10175 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10176 */
10177 protected float getTopFadingEdgeStrength() {
10178 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10179 }
10180
10181 /**
10182 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10183 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10184 * returns 0.0 or 1.0 but no value in between.
10185 *
10186 * Subclasses should override this method to provide a smoother fade transition
10187 * when scrolling occurs.
10188 *
10189 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10190 */
10191 protected float getBottomFadingEdgeStrength() {
10192 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10193 computeVerticalScrollRange() ? 1.0f : 0.0f;
10194 }
10195
10196 /**
10197 * Returns the strength, or intensity, of the left faded edge. The strength is
10198 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10199 * returns 0.0 or 1.0 but no value in between.
10200 *
10201 * Subclasses should override this method to provide a smoother fade transition
10202 * when scrolling occurs.
10203 *
10204 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10205 */
10206 protected float getLeftFadingEdgeStrength() {
10207 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10208 }
10209
10210 /**
10211 * Returns the strength, or intensity, of the right faded edge. The strength is
10212 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10213 * returns 0.0 or 1.0 but no value in between.
10214 *
10215 * Subclasses should override this method to provide a smoother fade transition
10216 * when scrolling occurs.
10217 *
10218 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10219 */
10220 protected float getRightFadingEdgeStrength() {
10221 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10222 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10223 }
10224
10225 /**
10226 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10227 * scrollbar is not drawn by default.</p>
10228 *
10229 * @return true if the horizontal scrollbar should be painted, false
10230 * otherwise
10231 *
10232 * @see #setHorizontalScrollBarEnabled(boolean)
10233 */
10234 public boolean isHorizontalScrollBarEnabled() {
10235 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10236 }
10237
10238 /**
10239 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10240 * scrollbar is not drawn by default.</p>
10241 *
10242 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10243 * be painted
10244 *
10245 * @see #isHorizontalScrollBarEnabled()
10246 */
10247 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10248 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10249 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010250 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010251 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010252 }
10253 }
10254
10255 /**
10256 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10257 * scrollbar is not drawn by default.</p>
10258 *
10259 * @return true if the vertical scrollbar should be painted, false
10260 * otherwise
10261 *
10262 * @see #setVerticalScrollBarEnabled(boolean)
10263 */
10264 public boolean isVerticalScrollBarEnabled() {
10265 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10266 }
10267
10268 /**
10269 * <p>Define whether the vertical scrollbar should be drawn or not. The
10270 * scrollbar is not drawn by default.</p>
10271 *
10272 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10273 * be painted
10274 *
10275 * @see #isVerticalScrollBarEnabled()
10276 */
10277 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10278 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10279 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010280 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010281 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 }
10283 }
10284
Adam Powell20232d02010-12-08 21:08:53 -080010285 /**
10286 * @hide
10287 */
10288 protected void recomputePadding() {
10289 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 }
Joe Malin32736f02011-01-19 16:14:20 -080010291
Mike Cleronfe81d382009-09-28 14:22:16 -070010292 /**
10293 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010294 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010295 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010296 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010297 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010298 */
10299 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10300 initScrollCache();
10301 final ScrollabilityCache scrollabilityCache = mScrollCache;
10302 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010303 if (fadeScrollbars) {
10304 scrollabilityCache.state = ScrollabilityCache.OFF;
10305 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010306 scrollabilityCache.state = ScrollabilityCache.ON;
10307 }
10308 }
Joe Malin32736f02011-01-19 16:14:20 -080010309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 /**
Joe Malin32736f02011-01-19 16:14:20 -080010311 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010312 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010313 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010314 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010315 *
10316 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010317 */
10318 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010319 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010320 }
Joe Malin32736f02011-01-19 16:14:20 -080010321
Mike Cleron52f0a642009-09-28 18:21:37 -070010322 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010323 *
10324 * Returns the delay before scrollbars fade.
10325 *
10326 * @return the delay before scrollbars fade
10327 *
10328 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10329 */
10330 public int getScrollBarDefaultDelayBeforeFade() {
10331 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10332 mScrollCache.scrollBarDefaultDelayBeforeFade;
10333 }
10334
10335 /**
10336 * Define the delay before scrollbars fade.
10337 *
10338 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10339 *
10340 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10341 */
10342 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10343 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10344 }
10345
10346 /**
10347 *
10348 * Returns the scrollbar fade duration.
10349 *
10350 * @return the scrollbar fade duration
10351 *
10352 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10353 */
10354 public int getScrollBarFadeDuration() {
10355 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10356 mScrollCache.scrollBarFadeDuration;
10357 }
10358
10359 /**
10360 * Define the scrollbar fade duration.
10361 *
10362 * @param scrollBarFadeDuration - the scrollbar fade duration
10363 *
10364 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10365 */
10366 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10367 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10368 }
10369
10370 /**
10371 *
10372 * Returns the scrollbar size.
10373 *
10374 * @return the scrollbar size
10375 *
10376 * @attr ref android.R.styleable#View_scrollbarSize
10377 */
10378 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010379 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010380 mScrollCache.scrollBarSize;
10381 }
10382
10383 /**
10384 * Define the scrollbar size.
10385 *
10386 * @param scrollBarSize - the scrollbar size
10387 *
10388 * @attr ref android.R.styleable#View_scrollbarSize
10389 */
10390 public void setScrollBarSize(int scrollBarSize) {
10391 getScrollCache().scrollBarSize = scrollBarSize;
10392 }
10393
10394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10396 * inset. When inset, they add to the padding of the view. And the scrollbars
10397 * can be drawn inside the padding area or on the edge of the view. For example,
10398 * if a view has a background drawable and you want to draw the scrollbars
10399 * inside the padding specified by the drawable, you can use
10400 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10401 * appear at the edge of the view, ignoring the padding, then you can use
10402 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10403 * @param style the style of the scrollbars. Should be one of
10404 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10405 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10406 * @see #SCROLLBARS_INSIDE_OVERLAY
10407 * @see #SCROLLBARS_INSIDE_INSET
10408 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10409 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010410 *
10411 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010412 */
10413 public void setScrollBarStyle(int style) {
10414 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10415 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010416 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010417 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418 }
10419 }
10420
10421 /**
10422 * <p>Returns the current scrollbar style.</p>
10423 * @return the current scrollbar style
10424 * @see #SCROLLBARS_INSIDE_OVERLAY
10425 * @see #SCROLLBARS_INSIDE_INSET
10426 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10427 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010428 *
10429 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010431 @ViewDebug.ExportedProperty(mapping = {
10432 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10433 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10434 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10435 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10436 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437 public int getScrollBarStyle() {
10438 return mViewFlags & SCROLLBARS_STYLE_MASK;
10439 }
10440
10441 /**
10442 * <p>Compute the horizontal range that the horizontal scrollbar
10443 * represents.</p>
10444 *
10445 * <p>The range is expressed in arbitrary units that must be the same as the
10446 * units used by {@link #computeHorizontalScrollExtent()} and
10447 * {@link #computeHorizontalScrollOffset()}.</p>
10448 *
10449 * <p>The default range is the drawing width of this view.</p>
10450 *
10451 * @return the total horizontal range represented by the horizontal
10452 * scrollbar
10453 *
10454 * @see #computeHorizontalScrollExtent()
10455 * @see #computeHorizontalScrollOffset()
10456 * @see android.widget.ScrollBarDrawable
10457 */
10458 protected int computeHorizontalScrollRange() {
10459 return getWidth();
10460 }
10461
10462 /**
10463 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10464 * within the horizontal range. This value is used to compute the position
10465 * of the thumb within the scrollbar's track.</p>
10466 *
10467 * <p>The range is expressed in arbitrary units that must be the same as the
10468 * units used by {@link #computeHorizontalScrollRange()} and
10469 * {@link #computeHorizontalScrollExtent()}.</p>
10470 *
10471 * <p>The default offset is the scroll offset of this view.</p>
10472 *
10473 * @return the horizontal offset of the scrollbar's thumb
10474 *
10475 * @see #computeHorizontalScrollRange()
10476 * @see #computeHorizontalScrollExtent()
10477 * @see android.widget.ScrollBarDrawable
10478 */
10479 protected int computeHorizontalScrollOffset() {
10480 return mScrollX;
10481 }
10482
10483 /**
10484 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10485 * within the horizontal range. This value is used to compute the length
10486 * of the thumb within the scrollbar's track.</p>
10487 *
10488 * <p>The range is expressed in arbitrary units that must be the same as the
10489 * units used by {@link #computeHorizontalScrollRange()} and
10490 * {@link #computeHorizontalScrollOffset()}.</p>
10491 *
10492 * <p>The default extent is the drawing width of this view.</p>
10493 *
10494 * @return the horizontal extent of the scrollbar's thumb
10495 *
10496 * @see #computeHorizontalScrollRange()
10497 * @see #computeHorizontalScrollOffset()
10498 * @see android.widget.ScrollBarDrawable
10499 */
10500 protected int computeHorizontalScrollExtent() {
10501 return getWidth();
10502 }
10503
10504 /**
10505 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10506 *
10507 * <p>The range is expressed in arbitrary units that must be the same as the
10508 * units used by {@link #computeVerticalScrollExtent()} and
10509 * {@link #computeVerticalScrollOffset()}.</p>
10510 *
10511 * @return the total vertical range represented by the vertical scrollbar
10512 *
10513 * <p>The default range is the drawing height of this view.</p>
10514 *
10515 * @see #computeVerticalScrollExtent()
10516 * @see #computeVerticalScrollOffset()
10517 * @see android.widget.ScrollBarDrawable
10518 */
10519 protected int computeVerticalScrollRange() {
10520 return getHeight();
10521 }
10522
10523 /**
10524 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10525 * within the horizontal range. This value is used to compute the position
10526 * of the thumb within the scrollbar's track.</p>
10527 *
10528 * <p>The range is expressed in arbitrary units that must be the same as the
10529 * units used by {@link #computeVerticalScrollRange()} and
10530 * {@link #computeVerticalScrollExtent()}.</p>
10531 *
10532 * <p>The default offset is the scroll offset of this view.</p>
10533 *
10534 * @return the vertical offset of the scrollbar's thumb
10535 *
10536 * @see #computeVerticalScrollRange()
10537 * @see #computeVerticalScrollExtent()
10538 * @see android.widget.ScrollBarDrawable
10539 */
10540 protected int computeVerticalScrollOffset() {
10541 return mScrollY;
10542 }
10543
10544 /**
10545 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10546 * within the vertical range. This value is used to compute the length
10547 * of the thumb within the scrollbar's track.</p>
10548 *
10549 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010550 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 * {@link #computeVerticalScrollOffset()}.</p>
10552 *
10553 * <p>The default extent is the drawing height of this view.</p>
10554 *
10555 * @return the vertical extent of the scrollbar's thumb
10556 *
10557 * @see #computeVerticalScrollRange()
10558 * @see #computeVerticalScrollOffset()
10559 * @see android.widget.ScrollBarDrawable
10560 */
10561 protected int computeVerticalScrollExtent() {
10562 return getHeight();
10563 }
10564
10565 /**
Adam Powell69159442011-06-13 17:53:06 -070010566 * Check if this view can be scrolled horizontally in a certain direction.
10567 *
10568 * @param direction Negative to check scrolling left, positive to check scrolling right.
10569 * @return true if this view can be scrolled in the specified direction, false otherwise.
10570 */
10571 public boolean canScrollHorizontally(int direction) {
10572 final int offset = computeHorizontalScrollOffset();
10573 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10574 if (range == 0) return false;
10575 if (direction < 0) {
10576 return offset > 0;
10577 } else {
10578 return offset < range - 1;
10579 }
10580 }
10581
10582 /**
10583 * Check if this view can be scrolled vertically in a certain direction.
10584 *
10585 * @param direction Negative to check scrolling up, positive to check scrolling down.
10586 * @return true if this view can be scrolled in the specified direction, false otherwise.
10587 */
10588 public boolean canScrollVertically(int direction) {
10589 final int offset = computeVerticalScrollOffset();
10590 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10591 if (range == 0) return false;
10592 if (direction < 0) {
10593 return offset > 0;
10594 } else {
10595 return offset < range - 1;
10596 }
10597 }
10598
10599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10601 * scrollbars are painted only if they have been awakened first.</p>
10602 *
10603 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010604 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010605 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010607 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 // scrollbars are drawn only when the animation is running
10609 final ScrollabilityCache cache = mScrollCache;
10610 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010611
Mike Cleronf116bf82009-09-27 19:14:12 -070010612 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010613
Mike Cleronf116bf82009-09-27 19:14:12 -070010614 if (state == ScrollabilityCache.OFF) {
10615 return;
10616 }
Joe Malin32736f02011-01-19 16:14:20 -080010617
Mike Cleronf116bf82009-09-27 19:14:12 -070010618 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010619
Mike Cleronf116bf82009-09-27 19:14:12 -070010620 if (state == ScrollabilityCache.FADING) {
10621 // We're fading -- get our fade interpolation
10622 if (cache.interpolatorValues == null) {
10623 cache.interpolatorValues = new float[1];
10624 }
Joe Malin32736f02011-01-19 16:14:20 -080010625
Mike Cleronf116bf82009-09-27 19:14:12 -070010626 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010627
Mike Cleronf116bf82009-09-27 19:14:12 -070010628 // Stops the animation if we're done
10629 if (cache.scrollBarInterpolator.timeToValues(values) ==
10630 Interpolator.Result.FREEZE_END) {
10631 cache.state = ScrollabilityCache.OFF;
10632 } else {
10633 cache.scrollBar.setAlpha(Math.round(values[0]));
10634 }
Joe Malin32736f02011-01-19 16:14:20 -080010635
10636 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010637 // drawing. We only want this when we're fading so that
10638 // we prevent excessive redraws
10639 invalidate = true;
10640 } else {
10641 // We're just on -- but we may have been fading before so
10642 // reset alpha
10643 cache.scrollBar.setAlpha(255);
10644 }
10645
Joe Malin32736f02011-01-19 16:14:20 -080010646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 final int viewFlags = mViewFlags;
10648
10649 final boolean drawHorizontalScrollBar =
10650 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10651 final boolean drawVerticalScrollBar =
10652 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10653 && !isVerticalScrollBarHidden();
10654
10655 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10656 final int width = mRight - mLeft;
10657 final int height = mBottom - mTop;
10658
10659 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660
Mike Reede8853fc2009-09-04 14:01:48 -040010661 final int scrollX = mScrollX;
10662 final int scrollY = mScrollY;
10663 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10664
Mike Cleronf116bf82009-09-27 19:14:12 -070010665 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010668 int size = scrollBar.getSize(false);
10669 if (size <= 0) {
10670 size = cache.scrollBarSize;
10671 }
10672
Mike Cleronf116bf82009-09-27 19:14:12 -070010673 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010674 computeHorizontalScrollOffset(),
10675 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010676 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010677 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010678 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010679 left = scrollX + (mPaddingLeft & inside);
10680 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10681 bottom = top + size;
10682 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10683 if (invalidate) {
10684 invalidate(left, top, right, bottom);
10685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 }
10687
10688 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010689 int size = scrollBar.getSize(true);
10690 if (size <= 0) {
10691 size = cache.scrollBarSize;
10692 }
10693
Mike Reede8853fc2009-09-04 14:01:48 -040010694 scrollBar.setParameters(computeVerticalScrollRange(),
10695 computeVerticalScrollOffset(),
10696 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010697 switch (mVerticalScrollbarPosition) {
10698 default:
10699 case SCROLLBAR_POSITION_DEFAULT:
10700 case SCROLLBAR_POSITION_RIGHT:
10701 left = scrollX + width - size - (mUserPaddingRight & inside);
10702 break;
10703 case SCROLLBAR_POSITION_LEFT:
10704 left = scrollX + (mUserPaddingLeft & inside);
10705 break;
10706 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010707 top = scrollY + (mPaddingTop & inside);
10708 right = left + size;
10709 bottom = scrollY + height - (mUserPaddingBottom & inside);
10710 onDrawVerticalScrollBar(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 }
10717 }
Romain Guy8506ab42009-06-11 17:35:47 -070010718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010720 * 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 -080010721 * FastScroller is visible.
10722 * @return whether to temporarily hide the vertical scrollbar
10723 * @hide
10724 */
10725 protected boolean isVerticalScrollBarHidden() {
10726 return false;
10727 }
10728
10729 /**
10730 * <p>Draw the horizontal scrollbar if
10731 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010733 * @param canvas the canvas on which to draw the scrollbar
10734 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 *
10736 * @see #isHorizontalScrollBarEnabled()
10737 * @see #computeHorizontalScrollRange()
10738 * @see #computeHorizontalScrollExtent()
10739 * @see #computeHorizontalScrollOffset()
10740 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010741 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010742 */
Romain Guy8fb95422010-08-17 18:38:51 -070010743 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10744 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010745 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010746 scrollBar.draw(canvas);
10747 }
Mike Reede8853fc2009-09-04 14:01:48 -040010748
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10751 * returns true.</p>
10752 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 * @param canvas the canvas on which to draw the scrollbar
10754 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755 *
10756 * @see #isVerticalScrollBarEnabled()
10757 * @see #computeVerticalScrollRange()
10758 * @see #computeVerticalScrollExtent()
10759 * @see #computeVerticalScrollOffset()
10760 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010761 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 */
Romain Guy8fb95422010-08-17 18:38:51 -070010763 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10764 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010765 scrollBar.setBounds(l, t, r, b);
10766 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010767 }
10768
10769 /**
10770 * Implement this to do your drawing.
10771 *
10772 * @param canvas the canvas on which the background will be drawn
10773 */
10774 protected void onDraw(Canvas canvas) {
10775 }
10776
10777 /*
10778 * Caller is responsible for calling requestLayout if necessary.
10779 * (This allows addViewInLayout to not request a new layout.)
10780 */
10781 void assignParent(ViewParent parent) {
10782 if (mParent == null) {
10783 mParent = parent;
10784 } else if (parent == null) {
10785 mParent = null;
10786 } else {
10787 throw new RuntimeException("view " + this + " being added, but"
10788 + " it already has a parent");
10789 }
10790 }
10791
10792 /**
10793 * This is called when the view is attached to a window. At this point it
10794 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010795 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10796 * however it may be called any time before the first onDraw -- including
10797 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 *
10799 * @see #onDetachedFromWindow()
10800 */
10801 protected void onAttachedToWindow() {
10802 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10803 mParent.requestTransparentRegion(this);
10804 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010805 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10806 initialAwakenScrollBars();
10807 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10808 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010809 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010810 // Order is important here: LayoutDirection MUST be resolved before Padding
10811 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010812 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010813 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010814 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010815 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010816 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010817 if (isFocused()) {
10818 InputMethodManager imm = InputMethodManager.peekInstance();
10819 imm.focusIn(this);
10820 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010821 }
Cibu Johny86666632010-02-22 13:01:02 -080010822
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010823 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010824 * @see #onScreenStateChanged(int)
10825 */
10826 void dispatchScreenStateChanged(int screenState) {
10827 onScreenStateChanged(screenState);
10828 }
10829
10830 /**
10831 * This method is called whenever the state of the screen this view is
10832 * attached to changes. A state change will usually occurs when the screen
10833 * turns on or off (whether it happens automatically or the user does it
10834 * manually.)
10835 *
10836 * @param screenState The new state of the screen. Can be either
10837 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10838 */
10839 public void onScreenStateChanged(int screenState) {
10840 }
10841
10842 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010843 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10844 */
10845 private boolean hasRtlSupport() {
10846 return mContext.getApplicationInfo().hasRtlSupport();
10847 }
10848
10849 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010850 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10851 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010852 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010853 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010854 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010855 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010856 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010857
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010858 if (hasRtlSupport()) {
10859 // Set resolved depending on layout direction
10860 switch (getLayoutDirection()) {
10861 case LAYOUT_DIRECTION_INHERIT:
10862 // If this is root view, no need to look at parent's layout dir.
10863 if (canResolveLayoutDirection()) {
10864 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010865
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010866 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10867 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10868 }
10869 } else {
10870 // Nothing to do, LTR by default
10871 }
10872 break;
10873 case LAYOUT_DIRECTION_RTL:
10874 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10875 break;
10876 case LAYOUT_DIRECTION_LOCALE:
10877 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010878 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10879 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010880 break;
10881 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010882 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010883 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010884 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010885
10886 // Set to resolved
10887 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010888 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010889 // Resolve padding
10890 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010891 }
10892
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010893 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010894 * Called when layout direction has been resolved.
10895 *
10896 * The default implementation does nothing.
10897 */
10898 public void onResolvedLayoutDirectionChanged() {
10899 }
10900
10901 /**
10902 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010903 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010904 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010905 // If the user specified the absolute padding (either with android:padding or
10906 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10907 // use the default padding or the padding from the background drawable
10908 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010909 int resolvedLayoutDirection = getResolvedLayoutDirection();
10910 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010911 case LAYOUT_DIRECTION_RTL:
10912 // Start user padding override Right user padding. Otherwise, if Right user
10913 // padding is not defined, use the default Right padding. If Right user padding
10914 // is defined, just use it.
10915 if (mUserPaddingStart >= 0) {
10916 mUserPaddingRight = mUserPaddingStart;
10917 } else if (mUserPaddingRight < 0) {
10918 mUserPaddingRight = mPaddingRight;
10919 }
10920 if (mUserPaddingEnd >= 0) {
10921 mUserPaddingLeft = mUserPaddingEnd;
10922 } else if (mUserPaddingLeft < 0) {
10923 mUserPaddingLeft = mPaddingLeft;
10924 }
10925 break;
10926 case LAYOUT_DIRECTION_LTR:
10927 default:
10928 // Start user padding override Left user padding. Otherwise, if Left user
10929 // padding is not defined, use the default left padding. If Left user padding
10930 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010931 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010932 mUserPaddingLeft = mUserPaddingStart;
10933 } else if (mUserPaddingLeft < 0) {
10934 mUserPaddingLeft = mPaddingLeft;
10935 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010936 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010937 mUserPaddingRight = mUserPaddingEnd;
10938 } else if (mUserPaddingRight < 0) {
10939 mUserPaddingRight = mPaddingRight;
10940 }
10941 }
10942
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010943 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10944
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010945 if(isPaddingRelative()) {
10946 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10947 } else {
10948 recomputePadding();
10949 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010950 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010951 }
10952
10953 /**
10954 * Resolve padding depending on the layout direction. Subclasses that care about
10955 * padding resolution should override this method. The default implementation does
10956 * nothing.
10957 *
10958 * @param layoutDirection the direction of the layout
10959 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010960 * @see {@link #LAYOUT_DIRECTION_LTR}
10961 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010962 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010963 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010964 }
10965
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010966 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010967 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010968 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010969 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010970 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010971 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010972 switch (getLayoutDirection()) {
10973 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010974 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010975 default:
10976 return true;
10977 }
10978 }
10979
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010980 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010981 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10982 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010983 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010984 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010985 // Reset the current resolved bits
10986 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010987 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010988 // Reset also the text direction
10989 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010990 }
10991
10992 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010993 * Called during reset of resolved layout direction.
10994 *
10995 * Subclasses need to override this method to clear cached information that depends on the
10996 * resolved layout direction, or to inform child views that inherit their layout direction.
10997 *
10998 * The default implementation does nothing.
10999 */
11000 public void onResolvedLayoutDirectionReset() {
11001 }
11002
11003 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011004 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011005 *
11006 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011007 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011008 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011009 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011010 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 }
11012
11013 /**
11014 * This is called when the view is detached from a window. At this point it
11015 * no longer has a surface for drawing.
11016 *
11017 * @see #onAttachedToWindow()
11018 */
11019 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011020 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011021
Romain Guya440b002010-02-24 15:57:54 -080011022 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011023 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011024 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011025 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011027 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011028
Romain Guya998dff2012-03-23 18:58:36 -070011029 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011030
11031 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011032 if (mDisplayList != null) {
11033 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11034 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011035 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011036 } else {
11037 if (mDisplayList != null) {
11038 // Should never happen
11039 mDisplayList.invalidate();
11040 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011041 }
11042
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011043 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011044
11045 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011046 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011047 resetAccessibilityStateChanged();
11048 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011049 }
11050
11051 /**
11052 * @return The number of times this view has been attached to a window
11053 */
11054 protected int getWindowAttachCount() {
11055 return mWindowAttachCount;
11056 }
11057
11058 /**
11059 * Retrieve a unique token identifying the window this view is attached to.
11060 * @return Return the window's token for use in
11061 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11062 */
11063 public IBinder getWindowToken() {
11064 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11065 }
11066
11067 /**
11068 * Retrieve a unique token identifying the top-level "real" window of
11069 * the window that this view is attached to. That is, this is like
11070 * {@link #getWindowToken}, except if the window this view in is a panel
11071 * window (attached to another containing window), then the token of
11072 * the containing window is returned instead.
11073 *
11074 * @return Returns the associated window token, either
11075 * {@link #getWindowToken()} or the containing window's token.
11076 */
11077 public IBinder getApplicationWindowToken() {
11078 AttachInfo ai = mAttachInfo;
11079 if (ai != null) {
11080 IBinder appWindowToken = ai.mPanelParentWindowToken;
11081 if (appWindowToken == null) {
11082 appWindowToken = ai.mWindowToken;
11083 }
11084 return appWindowToken;
11085 }
11086 return null;
11087 }
11088
11089 /**
11090 * Retrieve private session object this view hierarchy is using to
11091 * communicate with the window manager.
11092 * @return the session object to communicate with the window manager
11093 */
11094 /*package*/ IWindowSession getWindowSession() {
11095 return mAttachInfo != null ? mAttachInfo.mSession : null;
11096 }
11097
11098 /**
11099 * @param info the {@link android.view.View.AttachInfo} to associated with
11100 * this view
11101 */
11102 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11103 //System.out.println("Attached! " + this);
11104 mAttachInfo = info;
11105 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011106 // We will need to evaluate the drawable state at least once.
11107 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 if (mFloatingTreeObserver != null) {
11109 info.mTreeObserver.merge(mFloatingTreeObserver);
11110 mFloatingTreeObserver = null;
11111 }
11112 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11113 mAttachInfo.mScrollContainers.add(this);
11114 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11115 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011116 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011117 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011118
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011119 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011120 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011121 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011122 if (listeners != null && listeners.size() > 0) {
11123 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11124 // perform the dispatching. The iterator is a safe guard against listeners that
11125 // could mutate the list by calling the various add/remove methods. This prevents
11126 // the array from being modified while we iterate it.
11127 for (OnAttachStateChangeListener listener : listeners) {
11128 listener.onViewAttachedToWindow(this);
11129 }
11130 }
11131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 int vis = info.mWindowVisibility;
11133 if (vis != GONE) {
11134 onWindowVisibilityChanged(vis);
11135 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011136 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11137 // If nobody has evaluated the drawable state yet, then do it now.
11138 refreshDrawableState();
11139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 }
11141
11142 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 AttachInfo info = mAttachInfo;
11144 if (info != null) {
11145 int vis = info.mWindowVisibility;
11146 if (vis != GONE) {
11147 onWindowVisibilityChanged(GONE);
11148 }
11149 }
11150
11151 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011152
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011153 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011154 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011155 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011156 if (listeners != null && listeners.size() > 0) {
11157 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11158 // perform the dispatching. The iterator is a safe guard against listeners that
11159 // could mutate the list by calling the various add/remove methods. This prevents
11160 // the array from being modified while we iterate it.
11161 for (OnAttachStateChangeListener listener : listeners) {
11162 listener.onViewDetachedFromWindow(this);
11163 }
11164 }
11165
Romain Guy01d5edc2011-01-28 11:28:53 -080011166 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011167 mAttachInfo.mScrollContainers.remove(this);
11168 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11169 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011171 mAttachInfo = null;
11172 }
11173
11174 /**
11175 * Store this view hierarchy's frozen state into the given container.
11176 *
11177 * @param container The SparseArray in which to save the view's state.
11178 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011179 * @see #restoreHierarchyState(android.util.SparseArray)
11180 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11181 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011182 */
11183 public void saveHierarchyState(SparseArray<Parcelable> container) {
11184 dispatchSaveInstanceState(container);
11185 }
11186
11187 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011188 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11189 * this view and its children. May be overridden to modify how freezing happens to a
11190 * 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 -080011191 *
11192 * @param container The SparseArray in which to save the view's state.
11193 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011194 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11195 * @see #saveHierarchyState(android.util.SparseArray)
11196 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011197 */
11198 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11199 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11200 mPrivateFlags &= ~SAVE_STATE_CALLED;
11201 Parcelable state = onSaveInstanceState();
11202 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11203 throw new IllegalStateException(
11204 "Derived class did not call super.onSaveInstanceState()");
11205 }
11206 if (state != null) {
11207 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11208 // + ": " + state);
11209 container.put(mID, state);
11210 }
11211 }
11212 }
11213
11214 /**
11215 * Hook allowing a view to generate a representation of its internal state
11216 * that can later be used to create a new instance with that same state.
11217 * This state should only contain information that is not persistent or can
11218 * not be reconstructed later. For example, you will never store your
11219 * current position on screen because that will be computed again when a
11220 * new instance of the view is placed in its view hierarchy.
11221 * <p>
11222 * Some examples of things you may store here: the current cursor position
11223 * in a text view (but usually not the text itself since that is stored in a
11224 * content provider or other persistent storage), the currently selected
11225 * item in a list view.
11226 *
11227 * @return Returns a Parcelable object containing the view's current dynamic
11228 * state, or null if there is nothing interesting to save. The
11229 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011230 * @see #onRestoreInstanceState(android.os.Parcelable)
11231 * @see #saveHierarchyState(android.util.SparseArray)
11232 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 * @see #setSaveEnabled(boolean)
11234 */
11235 protected Parcelable onSaveInstanceState() {
11236 mPrivateFlags |= SAVE_STATE_CALLED;
11237 return BaseSavedState.EMPTY_STATE;
11238 }
11239
11240 /**
11241 * Restore this view hierarchy's frozen state from the given container.
11242 *
11243 * @param container The SparseArray which holds previously frozen states.
11244 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011245 * @see #saveHierarchyState(android.util.SparseArray)
11246 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11247 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248 */
11249 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11250 dispatchRestoreInstanceState(container);
11251 }
11252
11253 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011254 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11255 * state for this view and its children. May be overridden to modify how restoring
11256 * happens to a view's children; for example, some views may want to not store state
11257 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 *
11259 * @param container The SparseArray which holds previously saved state.
11260 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011261 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11262 * @see #restoreHierarchyState(android.util.SparseArray)
11263 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 */
11265 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11266 if (mID != NO_ID) {
11267 Parcelable state = container.get(mID);
11268 if (state != null) {
11269 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11270 // + ": " + state);
11271 mPrivateFlags &= ~SAVE_STATE_CALLED;
11272 onRestoreInstanceState(state);
11273 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11274 throw new IllegalStateException(
11275 "Derived class did not call super.onRestoreInstanceState()");
11276 }
11277 }
11278 }
11279 }
11280
11281 /**
11282 * Hook allowing a view to re-apply a representation of its internal state that had previously
11283 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11284 * null state.
11285 *
11286 * @param state The frozen state that had previously been returned by
11287 * {@link #onSaveInstanceState}.
11288 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011289 * @see #onSaveInstanceState()
11290 * @see #restoreHierarchyState(android.util.SparseArray)
11291 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 */
11293 protected void onRestoreInstanceState(Parcelable state) {
11294 mPrivateFlags |= SAVE_STATE_CALLED;
11295 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011296 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11297 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011298 + "when two views of different type have the same id in the same hierarchy. "
11299 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011300 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011301 }
11302 }
11303
11304 /**
11305 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11306 *
11307 * @return the drawing start time in milliseconds
11308 */
11309 public long getDrawingTime() {
11310 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11311 }
11312
11313 /**
11314 * <p>Enables or disables the duplication of the parent's state into this view. When
11315 * duplication is enabled, this view gets its drawable state from its parent rather
11316 * than from its own internal properties.</p>
11317 *
11318 * <p>Note: in the current implementation, setting this property to true after the
11319 * view was added to a ViewGroup might have no effect at all. This property should
11320 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11321 *
11322 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11323 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011324 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011325 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11326 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011327 *
11328 * @param enabled True to enable duplication of the parent's drawable state, false
11329 * to disable it.
11330 *
11331 * @see #getDrawableState()
11332 * @see #isDuplicateParentStateEnabled()
11333 */
11334 public void setDuplicateParentStateEnabled(boolean enabled) {
11335 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11336 }
11337
11338 /**
11339 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11340 *
11341 * @return True if this view's drawable state is duplicated from the parent,
11342 * false otherwise
11343 *
11344 * @see #getDrawableState()
11345 * @see #setDuplicateParentStateEnabled(boolean)
11346 */
11347 public boolean isDuplicateParentStateEnabled() {
11348 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11349 }
11350
11351 /**
Romain Guy171c5922011-01-06 10:04:23 -080011352 * <p>Specifies the type of layer backing this view. The layer can be
11353 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11354 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011355 *
Romain Guy171c5922011-01-06 10:04:23 -080011356 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11357 * instance that controls how the layer is composed on screen. The following
11358 * properties of the paint are taken into account when composing the layer:</p>
11359 * <ul>
11360 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11361 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11362 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11363 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011364 *
Romain Guy171c5922011-01-06 10:04:23 -080011365 * <p>If this view has an alpha value set to < 1.0 by calling
11366 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11367 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11368 * equivalent to setting a hardware layer on this view and providing a paint with
11369 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011370 *
Romain Guy171c5922011-01-06 10:04:23 -080011371 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11372 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11373 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011374 *
Romain Guy171c5922011-01-06 10:04:23 -080011375 * @param layerType The ype of layer to use with this view, must be one of
11376 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11377 * {@link #LAYER_TYPE_HARDWARE}
11378 * @param paint The paint used to compose the layer. This argument is optional
11379 * and can be null. It is ignored when the layer type is
11380 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011381 *
11382 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011383 * @see #LAYER_TYPE_NONE
11384 * @see #LAYER_TYPE_SOFTWARE
11385 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011386 * @see #setAlpha(float)
11387 *
Romain Guy171c5922011-01-06 10:04:23 -080011388 * @attr ref android.R.styleable#View_layerType
11389 */
11390 public void setLayerType(int layerType, Paint paint) {
11391 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011392 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011393 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11394 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011395
Romain Guyd6cd5722011-01-17 14:42:41 -080011396 if (layerType == mLayerType) {
11397 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11398 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011399 invalidateParentCaches();
11400 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011401 }
11402 return;
11403 }
Romain Guy171c5922011-01-06 10:04:23 -080011404
11405 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011406 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011407 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011408 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011409 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011410 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011411 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011412 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011413 default:
11414 break;
Romain Guy171c5922011-01-06 10:04:23 -080011415 }
11416
11417 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011418 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11419 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11420 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011421
Romain Guy0fd89bf2011-01-26 15:41:30 -080011422 invalidateParentCaches();
11423 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011424 }
11425
11426 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011427 * Indicates whether this view has a static layer. A view with layer type
11428 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11429 * dynamic.
11430 */
11431 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011432 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011433 }
11434
11435 /**
Romain Guy171c5922011-01-06 10:04:23 -080011436 * Indicates what type of layer is currently associated with this view. By default
11437 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11438 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11439 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011440 *
Romain Guy171c5922011-01-06 10:04:23 -080011441 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11442 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011443 *
11444 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011445 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011446 * @see #LAYER_TYPE_NONE
11447 * @see #LAYER_TYPE_SOFTWARE
11448 * @see #LAYER_TYPE_HARDWARE
11449 */
11450 public int getLayerType() {
11451 return mLayerType;
11452 }
Joe Malin32736f02011-01-19 16:14:20 -080011453
Romain Guy6c319ca2011-01-11 14:29:25 -080011454 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011455 * Forces this view's layer to be created and this view to be rendered
11456 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11457 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011458 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011459 * This method can for instance be used to render a view into its layer before
11460 * starting an animation. If this view is complex, rendering into the layer
11461 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011462 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011463 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011464 *
11465 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011466 */
11467 public void buildLayer() {
11468 if (mLayerType == LAYER_TYPE_NONE) return;
11469
11470 if (mAttachInfo == null) {
11471 throw new IllegalStateException("This view must be attached to a window first");
11472 }
11473
11474 switch (mLayerType) {
11475 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011476 if (mAttachInfo.mHardwareRenderer != null &&
11477 mAttachInfo.mHardwareRenderer.isEnabled() &&
11478 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011479 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011480 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011481 break;
11482 case LAYER_TYPE_SOFTWARE:
11483 buildDrawingCache(true);
11484 break;
11485 }
11486 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011487
Romain Guy9c4b79a2011-11-10 19:23:58 -080011488 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11489 void flushLayer() {
11490 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11491 mHardwareLayer.flush();
11492 }
11493 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011494
11495 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011496 * <p>Returns a hardware layer that can be used to draw this view again
11497 * without executing its draw method.</p>
11498 *
11499 * @return A HardwareLayer ready to render, or null if an error occurred.
11500 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011501 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011502 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11503 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011504 return null;
11505 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011506
Romain Guy9c4b79a2011-11-10 19:23:58 -080011507 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011508
11509 final int width = mRight - mLeft;
11510 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011511
Romain Guy6c319ca2011-01-11 14:29:25 -080011512 if (width == 0 || height == 0) {
11513 return null;
11514 }
11515
11516 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11517 if (mHardwareLayer == null) {
11518 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11519 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011520 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011521 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11522 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011523 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011524 }
11525
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011526 // The layer is not valid if the underlying GPU resources cannot be allocated
11527 if (!mHardwareLayer.isValid()) {
11528 return null;
11529 }
11530
Chet Haasea1cff502012-02-21 13:43:44 -080011531 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011532 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011533 }
11534
11535 return mHardwareLayer;
11536 }
Romain Guy171c5922011-01-06 10:04:23 -080011537
Romain Guy589b0bb2011-10-10 13:57:47 -070011538 /**
11539 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011540 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011541 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011542 *
11543 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011544 * @see #LAYER_TYPE_HARDWARE
11545 */
Romain Guya998dff2012-03-23 18:58:36 -070011546 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011547 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011548 AttachInfo info = mAttachInfo;
11549 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011550 info.mHardwareRenderer.isEnabled() &&
11551 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011552 mHardwareLayer.destroy();
11553 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011554
Romain Guy9c4b79a2011-11-10 19:23:58 -080011555 invalidate(true);
11556 invalidateParentCaches();
11557 }
Romain Guy65b345f2011-07-27 18:51:50 -070011558 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011559 }
Romain Guy65b345f2011-07-27 18:51:50 -070011560 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011561 }
11562
Romain Guy171c5922011-01-06 10:04:23 -080011563 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011564 * Destroys all hardware rendering resources. This method is invoked
11565 * when the system needs to reclaim resources. Upon execution of this
11566 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011567 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011568 * Note: you <strong>must</strong> call
11569 * <code>super.destroyHardwareResources()</code> when overriding
11570 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011571 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011572 * @hide
11573 */
11574 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011575 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011576 }
11577
11578 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011579 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11580 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11581 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11582 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11583 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11584 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011585 *
Romain Guy171c5922011-01-06 10:04:23 -080011586 * <p>Enabling the drawing cache is similar to
11587 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011588 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11589 * drawing cache has no effect on rendering because the system uses a different mechanism
11590 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11591 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11592 * for information on how to enable software and hardware layers.</p>
11593 *
11594 * <p>This API can be used to manually generate
11595 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11596 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011597 *
11598 * @param enabled true to enable the drawing cache, false otherwise
11599 *
11600 * @see #isDrawingCacheEnabled()
11601 * @see #getDrawingCache()
11602 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011603 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011604 */
11605 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011606 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011607 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11608 }
11609
11610 /**
11611 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11612 *
11613 * @return true if the drawing cache is enabled
11614 *
11615 * @see #setDrawingCacheEnabled(boolean)
11616 * @see #getDrawingCache()
11617 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011618 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011619 public boolean isDrawingCacheEnabled() {
11620 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11621 }
11622
11623 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011624 * Debugging utility which recursively outputs the dirty state of a view and its
11625 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011626 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011627 * @hide
11628 */
Romain Guy676b1732011-02-14 14:45:33 -080011629 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011630 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11631 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11632 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11633 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11634 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11635 if (clear) {
11636 mPrivateFlags &= clearMask;
11637 }
11638 if (this instanceof ViewGroup) {
11639 ViewGroup parent = (ViewGroup) this;
11640 final int count = parent.getChildCount();
11641 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011642 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011643 child.outputDirtyFlags(indent + " ", clear, clearMask);
11644 }
11645 }
11646 }
11647
11648 /**
11649 * This method is used by ViewGroup to cause its children to restore or recreate their
11650 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11651 * to recreate its own display list, which would happen if it went through the normal
11652 * draw/dispatchDraw mechanisms.
11653 *
11654 * @hide
11655 */
11656 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011657
11658 /**
11659 * A view that is not attached or hardware accelerated cannot create a display list.
11660 * This method checks these conditions and returns the appropriate result.
11661 *
11662 * @return true if view has the ability to create a display list, false otherwise.
11663 *
11664 * @hide
11665 */
11666 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011667 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011668 }
Joe Malin32736f02011-01-19 16:14:20 -080011669
Chet Haasedaf98e92011-01-10 14:10:36 -080011670 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011671 * @return The HardwareRenderer associated with that view or null if hardware rendering
11672 * is not supported or this this has not been attached to a window.
11673 *
11674 * @hide
11675 */
11676 public HardwareRenderer getHardwareRenderer() {
11677 if (mAttachInfo != null) {
11678 return mAttachInfo.mHardwareRenderer;
11679 }
11680 return null;
11681 }
11682
11683 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011684 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11685 * Otherwise, the same display list will be returned (after having been rendered into
11686 * along the way, depending on the invalidation state of the view).
11687 *
11688 * @param displayList The previous version of this displayList, could be null.
11689 * @param isLayer Whether the requester of the display list is a layer. If so,
11690 * the view will avoid creating a layer inside the resulting display list.
11691 * @return A new or reused DisplayList object.
11692 */
11693 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11694 if (!canHaveDisplayList()) {
11695 return null;
11696 }
11697
11698 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11699 displayList == null || !displayList.isValid() ||
11700 (!isLayer && mRecreateDisplayList))) {
11701 // Don't need to recreate the display list, just need to tell our
11702 // children to restore/recreate theirs
11703 if (displayList != null && displayList.isValid() &&
11704 !isLayer && !mRecreateDisplayList) {
11705 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11706 mPrivateFlags &= ~DIRTY_MASK;
11707 dispatchGetDisplayList();
11708
11709 return displayList;
11710 }
11711
11712 if (!isLayer) {
11713 // If we got here, we're recreating it. Mark it as such to ensure that
11714 // we copy in child display lists into ours in drawChild()
11715 mRecreateDisplayList = true;
11716 }
11717 if (displayList == null) {
11718 final String name = getClass().getSimpleName();
11719 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11720 // If we're creating a new display list, make sure our parent gets invalidated
11721 // since they will need to recreate their display list to account for this
11722 // new child display list.
11723 invalidateParentCaches();
11724 }
11725
11726 boolean caching = false;
11727 final HardwareCanvas canvas = displayList.start();
11728 int restoreCount = 0;
11729 int width = mRight - mLeft;
11730 int height = mBottom - mTop;
11731
11732 try {
11733 canvas.setViewport(width, height);
11734 // The dirty rect should always be null for a display list
11735 canvas.onPreDraw(null);
11736 int layerType = (
11737 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11738 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070011739 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070011740 if (layerType == LAYER_TYPE_HARDWARE) {
11741 final HardwareLayer layer = getHardwareLayer();
11742 if (layer != null && layer.isValid()) {
11743 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11744 } else {
11745 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11746 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11747 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11748 }
11749 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011750 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011751 buildDrawingCache(true);
11752 Bitmap cache = getDrawingCache(true);
11753 if (cache != null) {
11754 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11755 caching = true;
11756 }
Chet Haasea1cff502012-02-21 13:43:44 -080011757 }
Chet Haasea1cff502012-02-21 13:43:44 -080011758 } else {
11759
11760 computeScroll();
11761
Chet Haasea1cff502012-02-21 13:43:44 -080011762 canvas.translate(-mScrollX, -mScrollY);
11763 if (!isLayer) {
11764 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11765 mPrivateFlags &= ~DIRTY_MASK;
11766 }
11767
11768 // Fast path for layouts with no backgrounds
11769 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11770 dispatchDraw(canvas);
11771 } else {
11772 draw(canvas);
11773 }
11774 }
11775 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080011776 canvas.onPostDraw();
11777
11778 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070011779 displayList.setCaching(caching);
11780 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080011781 displayList.setLeftTopRightBottom(0, 0, width, height);
11782 } else {
11783 setDisplayListProperties(displayList);
11784 }
11785 }
11786 } else if (!isLayer) {
11787 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11788 mPrivateFlags &= ~DIRTY_MASK;
11789 }
11790
11791 return displayList;
11792 }
11793
11794 /**
11795 * Get the DisplayList for the HardwareLayer
11796 *
11797 * @param layer The HardwareLayer whose DisplayList we want
11798 * @return A DisplayList fopr the specified HardwareLayer
11799 */
11800 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11801 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11802 layer.setDisplayList(displayList);
11803 return displayList;
11804 }
11805
11806
11807 /**
Romain Guyb051e892010-09-28 19:09:36 -070011808 * <p>Returns a display list that can be used to draw this view again
11809 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011810 *
Romain Guyb051e892010-09-28 19:09:36 -070011811 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011812 *
11813 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011814 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011815 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011816 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011817 return mDisplayList;
11818 }
11819
11820 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011821 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011822 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011823 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011824 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011825 * @see #getDrawingCache(boolean)
11826 */
11827 public Bitmap getDrawingCache() {
11828 return getDrawingCache(false);
11829 }
11830
11831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011832 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11833 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11834 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11835 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11836 * request the drawing cache by calling this method and draw it on screen if the
11837 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011838 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011839 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11840 * this method will create a bitmap of the same size as this view. Because this bitmap
11841 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11842 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11843 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11844 * size than the view. This implies that your application must be able to handle this
11845 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011846 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011847 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11848 * the current density of the screen when the application is in compatibility
11849 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011851 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011852 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 * @see #setDrawingCacheEnabled(boolean)
11854 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011855 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 * @see #destroyDrawingCache()
11857 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011858 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11860 return null;
11861 }
11862 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011863 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011864 }
Romain Guy02890fd2010-08-06 17:58:44 -070011865 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866 }
11867
11868 /**
11869 * <p>Frees the resources used by the drawing cache. If you call
11870 * {@link #buildDrawingCache()} manually without calling
11871 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11872 * should cleanup the cache with this method afterwards.</p>
11873 *
11874 * @see #setDrawingCacheEnabled(boolean)
11875 * @see #buildDrawingCache()
11876 * @see #getDrawingCache()
11877 */
11878 public void destroyDrawingCache() {
11879 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011880 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011881 mDrawingCache = null;
11882 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011883 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011884 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011885 mUnscaledDrawingCache = null;
11886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 }
11888
11889 /**
11890 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011891 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 * view will always be drawn on top of a solid color.
11893 *
11894 * @param color The background color to use for the drawing cache's bitmap
11895 *
11896 * @see #setDrawingCacheEnabled(boolean)
11897 * @see #buildDrawingCache()
11898 * @see #getDrawingCache()
11899 */
11900 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011901 if (color != mDrawingCacheBackgroundColor) {
11902 mDrawingCacheBackgroundColor = color;
11903 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011905 }
11906
11907 /**
11908 * @see #setDrawingCacheBackgroundColor(int)
11909 *
11910 * @return The background color to used for the drawing cache's bitmap
11911 */
11912 public int getDrawingCacheBackgroundColor() {
11913 return mDrawingCacheBackgroundColor;
11914 }
11915
11916 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011917 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011918 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011919 * @see #buildDrawingCache(boolean)
11920 */
11921 public void buildDrawingCache() {
11922 buildDrawingCache(false);
11923 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011924
Romain Guyfbd8f692009-06-26 14:51:58 -070011925 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11927 *
11928 * <p>If you call {@link #buildDrawingCache()} manually without calling
11929 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11930 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011931 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011932 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11933 * this method will create a bitmap of the same size as this view. Because this bitmap
11934 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11935 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11936 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11937 * size than the view. This implies that your application must be able to handle this
11938 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011939 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011940 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11941 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011942 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011943 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 *
11945 * @see #getDrawingCache()
11946 * @see #destroyDrawingCache()
11947 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011948 public void buildDrawingCache(boolean autoScale) {
11949 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011950 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011951 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011952
11953 if (ViewDebug.TRACE_HIERARCHY) {
11954 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011956
Romain Guy8506ab42009-06-11 17:35:47 -070011957 int width = mRight - mLeft;
11958 int height = mBottom - mTop;
11959
11960 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011961 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011962
Romain Guye1123222009-06-29 14:24:56 -070011963 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011964 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11965 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011967
11968 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011969 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011970 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971
11972 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011973 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011974 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011975 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11976 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011977 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978 return;
11979 }
11980
11981 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011982 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983
11984 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011985 Bitmap.Config quality;
11986 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011987 // Never pick ARGB_4444 because it looks awful
11988 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11990 case DRAWING_CACHE_QUALITY_AUTO:
11991 quality = Bitmap.Config.ARGB_8888;
11992 break;
11993 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011994 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 break;
11996 case DRAWING_CACHE_QUALITY_HIGH:
11997 quality = Bitmap.Config.ARGB_8888;
11998 break;
11999 default:
12000 quality = Bitmap.Config.ARGB_8888;
12001 break;
12002 }
12003 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012004 // Optimization for translucent windows
12005 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012006 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012007 }
12008
12009 // Try to cleanup memory
12010 if (bitmap != null) bitmap.recycle();
12011
12012 try {
12013 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012014 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012015 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012016 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012017 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012018 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012019 }
Adam Powell26153a32010-11-08 15:22:27 -080012020 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012021 } catch (OutOfMemoryError e) {
12022 // If there is not enough memory to create the bitmap cache, just
12023 // ignore the issue as bitmap caches are not required to draw the
12024 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012025 if (autoScale) {
12026 mDrawingCache = null;
12027 } else {
12028 mUnscaledDrawingCache = null;
12029 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012030 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 return;
12032 }
12033
12034 clear = drawingCacheBackgroundColor != 0;
12035 }
12036
12037 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012038 if (attachInfo != null) {
12039 canvas = attachInfo.mCanvas;
12040 if (canvas == null) {
12041 canvas = new Canvas();
12042 }
12043 canvas.setBitmap(bitmap);
12044 // Temporarily clobber the cached Canvas in case one of our children
12045 // is also using a drawing cache. Without this, the children would
12046 // steal the canvas by attaching their own bitmap to it and bad, bad
12047 // thing would happen (invisible views, corrupted drawings, etc.)
12048 attachInfo.mCanvas = null;
12049 } else {
12050 // This case should hopefully never or seldom happen
12051 canvas = new Canvas(bitmap);
12052 }
12053
12054 if (clear) {
12055 bitmap.eraseColor(drawingCacheBackgroundColor);
12056 }
12057
12058 computeScroll();
12059 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012060
Romain Guye1123222009-06-29 14:24:56 -070012061 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012062 final float scale = attachInfo.mApplicationScale;
12063 canvas.scale(scale, scale);
12064 }
Joe Malin32736f02011-01-19 16:14:20 -080012065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 canvas.translate(-mScrollX, -mScrollY);
12067
Romain Guy5bcdff42009-05-14 21:27:18 -070012068 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012069 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12070 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012071 mPrivateFlags |= DRAWING_CACHE_VALID;
12072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012073
12074 // Fast path for layouts with no backgrounds
12075 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12076 if (ViewDebug.TRACE_HIERARCHY) {
12077 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12078 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012079 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 dispatchDraw(canvas);
12081 } else {
12082 draw(canvas);
12083 }
12084
12085 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012086 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087
12088 if (attachInfo != null) {
12089 // Restore the cached Canvas for our siblings
12090 attachInfo.mCanvas = canvas;
12091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012092 }
12093 }
12094
12095 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012096 * Create a snapshot of the view into a bitmap. We should probably make
12097 * some form of this public, but should think about the API.
12098 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012099 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012100 int width = mRight - mLeft;
12101 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012102
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012103 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012104 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012105 width = (int) ((width * scale) + 0.5f);
12106 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012107
Romain Guy8c11e312009-09-14 15:15:30 -070012108 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012109 if (bitmap == null) {
12110 throw new OutOfMemoryError();
12111 }
12112
Romain Guyc529d8d2011-09-06 15:01:39 -070012113 Resources resources = getResources();
12114 if (resources != null) {
12115 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12116 }
Joe Malin32736f02011-01-19 16:14:20 -080012117
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012118 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012119 if (attachInfo != null) {
12120 canvas = attachInfo.mCanvas;
12121 if (canvas == null) {
12122 canvas = new Canvas();
12123 }
12124 canvas.setBitmap(bitmap);
12125 // Temporarily clobber the cached Canvas in case one of our children
12126 // is also using a drawing cache. Without this, the children would
12127 // steal the canvas by attaching their own bitmap to it and bad, bad
12128 // things would happen (invisible views, corrupted drawings, etc.)
12129 attachInfo.mCanvas = null;
12130 } else {
12131 // This case should hopefully never or seldom happen
12132 canvas = new Canvas(bitmap);
12133 }
12134
Romain Guy5bcdff42009-05-14 21:27:18 -070012135 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012136 bitmap.eraseColor(backgroundColor);
12137 }
12138
12139 computeScroll();
12140 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012141 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012142 canvas.translate(-mScrollX, -mScrollY);
12143
Romain Guy5bcdff42009-05-14 21:27:18 -070012144 // Temporarily remove the dirty mask
12145 int flags = mPrivateFlags;
12146 mPrivateFlags &= ~DIRTY_MASK;
12147
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012148 // Fast path for layouts with no backgrounds
12149 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12150 dispatchDraw(canvas);
12151 } else {
12152 draw(canvas);
12153 }
12154
Romain Guy5bcdff42009-05-14 21:27:18 -070012155 mPrivateFlags = flags;
12156
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012157 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012158 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012159
12160 if (attachInfo != null) {
12161 // Restore the cached Canvas for our siblings
12162 attachInfo.mCanvas = canvas;
12163 }
Romain Guy8506ab42009-06-11 17:35:47 -070012164
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012165 return bitmap;
12166 }
12167
12168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012169 * Indicates whether this View is currently in edit mode. A View is usually
12170 * in edit mode when displayed within a developer tool. For instance, if
12171 * this View is being drawn by a visual user interface builder, this method
12172 * should return true.
12173 *
12174 * Subclasses should check the return value of this method to provide
12175 * different behaviors if their normal behavior might interfere with the
12176 * host environment. For instance: the class spawns a thread in its
12177 * constructor, the drawing code relies on device-specific features, etc.
12178 *
12179 * This method is usually checked in the drawing code of custom widgets.
12180 *
12181 * @return True if this View is in edit mode, false otherwise.
12182 */
12183 public boolean isInEditMode() {
12184 return false;
12185 }
12186
12187 /**
12188 * If the View draws content inside its padding and enables fading edges,
12189 * it needs to support padding offsets. Padding offsets are added to the
12190 * fading edges to extend the length of the fade so that it covers pixels
12191 * drawn inside the padding.
12192 *
12193 * Subclasses of this class should override this method if they need
12194 * to draw content inside the padding.
12195 *
12196 * @return True if padding offset must be applied, false otherwise.
12197 *
12198 * @see #getLeftPaddingOffset()
12199 * @see #getRightPaddingOffset()
12200 * @see #getTopPaddingOffset()
12201 * @see #getBottomPaddingOffset()
12202 *
12203 * @since CURRENT
12204 */
12205 protected boolean isPaddingOffsetRequired() {
12206 return false;
12207 }
12208
12209 /**
12210 * Amount by which to extend the left fading region. Called only when
12211 * {@link #isPaddingOffsetRequired()} returns true.
12212 *
12213 * @return The left padding offset in pixels.
12214 *
12215 * @see #isPaddingOffsetRequired()
12216 *
12217 * @since CURRENT
12218 */
12219 protected int getLeftPaddingOffset() {
12220 return 0;
12221 }
12222
12223 /**
12224 * Amount by which to extend the right fading region. Called only when
12225 * {@link #isPaddingOffsetRequired()} returns true.
12226 *
12227 * @return The right padding offset in pixels.
12228 *
12229 * @see #isPaddingOffsetRequired()
12230 *
12231 * @since CURRENT
12232 */
12233 protected int getRightPaddingOffset() {
12234 return 0;
12235 }
12236
12237 /**
12238 * Amount by which to extend the top fading region. Called only when
12239 * {@link #isPaddingOffsetRequired()} returns true.
12240 *
12241 * @return The top padding offset in pixels.
12242 *
12243 * @see #isPaddingOffsetRequired()
12244 *
12245 * @since CURRENT
12246 */
12247 protected int getTopPaddingOffset() {
12248 return 0;
12249 }
12250
12251 /**
12252 * Amount by which to extend the bottom fading region. Called only when
12253 * {@link #isPaddingOffsetRequired()} returns true.
12254 *
12255 * @return The bottom padding offset in pixels.
12256 *
12257 * @see #isPaddingOffsetRequired()
12258 *
12259 * @since CURRENT
12260 */
12261 protected int getBottomPaddingOffset() {
12262 return 0;
12263 }
12264
12265 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012266 * @hide
12267 * @param offsetRequired
12268 */
12269 protected int getFadeTop(boolean offsetRequired) {
12270 int top = mPaddingTop;
12271 if (offsetRequired) top += getTopPaddingOffset();
12272 return top;
12273 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012274
Romain Guyf2fc4602011-07-19 15:20:03 -070012275 /**
12276 * @hide
12277 * @param offsetRequired
12278 */
12279 protected int getFadeHeight(boolean offsetRequired) {
12280 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012281 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012282 return mBottom - mTop - mPaddingBottom - padding;
12283 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012284
Romain Guyf2fc4602011-07-19 15:20:03 -070012285 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012286 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012287 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012288 *
Romain Guy2bffd262010-09-12 17:40:02 -070012289 * <p>Even if this method returns true, it does not mean that every call
12290 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12291 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012292 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012293 * window is hardware accelerated,
12294 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12295 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012296 *
Romain Guy2bffd262010-09-12 17:40:02 -070012297 * @return True if the view is attached to a window and the window is
12298 * hardware accelerated; false in any other case.
12299 */
12300 public boolean isHardwareAccelerated() {
12301 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12302 }
Joe Malin32736f02011-01-19 16:14:20 -080012303
Romain Guy2bffd262010-09-12 17:40:02 -070012304 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012305 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12306 * case of an active Animation being run on the view.
12307 */
12308 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12309 Animation a, boolean scalingRequired) {
12310 Transformation invalidationTransform;
12311 final int flags = parent.mGroupFlags;
12312 final boolean initialized = a.isInitialized();
12313 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012314 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012315 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12316 onAnimationStart();
12317 }
12318
12319 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12320 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12321 if (parent.mInvalidationTransformation == null) {
12322 parent.mInvalidationTransformation = new Transformation();
12323 }
12324 invalidationTransform = parent.mInvalidationTransformation;
12325 a.getTransformation(drawingTime, invalidationTransform, 1f);
12326 } else {
12327 invalidationTransform = parent.mChildTransformation;
12328 }
12329 if (more) {
12330 if (!a.willChangeBounds()) {
12331 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12332 parent.FLAG_OPTIMIZE_INVALIDATE) {
12333 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12334 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12335 // The child need to draw an animation, potentially offscreen, so
12336 // make sure we do not cancel invalidate requests
12337 parent.mPrivateFlags |= DRAW_ANIMATION;
12338 parent.invalidate(mLeft, mTop, mRight, mBottom);
12339 }
12340 } else {
12341 if (parent.mInvalidateRegion == null) {
12342 parent.mInvalidateRegion = new RectF();
12343 }
12344 final RectF region = parent.mInvalidateRegion;
12345 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12346 invalidationTransform);
12347
12348 // The child need to draw an animation, potentially offscreen, so
12349 // make sure we do not cancel invalidate requests
12350 parent.mPrivateFlags |= DRAW_ANIMATION;
12351
12352 final int left = mLeft + (int) region.left;
12353 final int top = mTop + (int) region.top;
12354 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12355 top + (int) (region.height() + .5f));
12356 }
12357 }
12358 return more;
12359 }
12360
Chet Haasea1cff502012-02-21 13:43:44 -080012361 void setDisplayListProperties() {
12362 setDisplayListProperties(mDisplayList);
12363 }
12364
12365 /**
12366 * This method is called by getDisplayList() when a display list is created or re-rendered.
12367 * It sets or resets the current value of all properties on that display list (resetting is
12368 * necessary when a display list is being re-created, because we need to make sure that
12369 * previously-set transform values
12370 */
12371 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012372 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012373 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012374 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012375 if (mParent instanceof ViewGroup) {
12376 displayList.setClipChildren(
12377 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12378 }
Chet Haase9420abd2012-03-29 16:28:32 -070012379 float alpha = 1;
12380 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12381 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12382 ViewGroup parentVG = (ViewGroup) mParent;
12383 final boolean hasTransform =
12384 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12385 if (hasTransform) {
12386 Transformation transform = parentVG.mChildTransformation;
12387 final int transformType = parentVG.mChildTransformation.getTransformationType();
12388 if (transformType != Transformation.TYPE_IDENTITY) {
12389 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12390 alpha = transform.getAlpha();
12391 }
12392 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12393 displayList.setStaticMatrix(transform.getMatrix());
12394 }
12395 }
12396 }
Chet Haasea1cff502012-02-21 13:43:44 -080012397 }
12398 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012399 alpha *= mTransformationInfo.mAlpha;
12400 if (alpha < 1) {
12401 final int multipliedAlpha = (int) (255 * alpha);
12402 if (onSetAlpha(multipliedAlpha)) {
12403 alpha = 1;
12404 }
12405 }
12406 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012407 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12408 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12409 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12410 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012411 if (mTransformationInfo.mCamera == null) {
12412 mTransformationInfo.mCamera = new Camera();
12413 mTransformationInfo.matrix3D = new Matrix();
12414 }
12415 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012416 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12417 displayList.setPivotX(getPivotX());
12418 displayList.setPivotY(getPivotY());
12419 }
Chet Haase9420abd2012-03-29 16:28:32 -070012420 } else if (alpha < 1) {
12421 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012422 }
12423 }
12424 }
12425
Chet Haasebcca79a2012-02-14 08:45:14 -080012426 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012427 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12428 * This draw() method is an implementation detail and is not intended to be overridden or
12429 * to be called from anywhere else other than ViewGroup.drawChild().
12430 */
12431 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012432 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012433 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012434 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012435 final int flags = parent.mGroupFlags;
12436
Chet Haasea1cff502012-02-21 13:43:44 -080012437 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012438 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012439 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012440 }
12441
12442 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012443 boolean concatMatrix = false;
12444
12445 boolean scalingRequired = false;
12446 boolean caching;
12447 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12448
12449 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012450 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12451 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012452 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012453 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012454 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12455 } else {
12456 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12457 }
12458
Chet Haasebcca79a2012-02-14 08:45:14 -080012459 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012460 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012461 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012462 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012463 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012464 } else if (!useDisplayListProperties &&
12465 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012466 final boolean hasTransform =
12467 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012468 if (hasTransform) {
12469 final int transformType = parent.mChildTransformation.getTransformationType();
12470 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12471 parent.mChildTransformation : null;
12472 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12473 }
12474 }
12475
12476 concatMatrix |= !childHasIdentityMatrix;
12477
12478 // Sets the flag as early as possible to allow draw() implementations
12479 // to call invalidate() successfully when doing animations
12480 mPrivateFlags |= DRAWN;
12481
Chet Haasebcca79a2012-02-14 08:45:14 -080012482 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012483 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12484 return more;
12485 }
12486
12487 if (hardwareAccelerated) {
12488 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12489 // retain the flag's value temporarily in the mRecreateDisplayList flag
12490 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12491 mPrivateFlags &= ~INVALIDATED;
12492 }
12493
12494 computeScroll();
12495
12496 final int sx = mScrollX;
12497 final int sy = mScrollY;
12498
12499 DisplayList displayList = null;
12500 Bitmap cache = null;
12501 boolean hasDisplayList = false;
12502 if (caching) {
12503 if (!hardwareAccelerated) {
12504 if (layerType != LAYER_TYPE_NONE) {
12505 layerType = LAYER_TYPE_SOFTWARE;
12506 buildDrawingCache(true);
12507 }
12508 cache = getDrawingCache(true);
12509 } else {
12510 switch (layerType) {
12511 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012512 if (useDisplayListProperties) {
12513 hasDisplayList = canHaveDisplayList();
12514 } else {
12515 buildDrawingCache(true);
12516 cache = getDrawingCache(true);
12517 }
Chet Haase64a48c12012-02-13 16:33:29 -080012518 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012519 case LAYER_TYPE_HARDWARE:
12520 if (useDisplayListProperties) {
12521 hasDisplayList = canHaveDisplayList();
12522 }
12523 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012524 case LAYER_TYPE_NONE:
12525 // Delay getting the display list until animation-driven alpha values are
12526 // set up and possibly passed on to the view
12527 hasDisplayList = canHaveDisplayList();
12528 break;
12529 }
12530 }
12531 }
Chet Haasea1cff502012-02-21 13:43:44 -080012532 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012533 if (useDisplayListProperties) {
12534 displayList = getDisplayList();
12535 if (!displayList.isValid()) {
12536 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12537 // to getDisplayList(), the display list will be marked invalid and we should not
12538 // try to use it again.
12539 displayList = null;
12540 hasDisplayList = false;
12541 useDisplayListProperties = false;
12542 }
12543 }
Chet Haase64a48c12012-02-13 16:33:29 -080012544
12545 final boolean hasNoCache = cache == null || hasDisplayList;
12546 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12547 layerType != LAYER_TYPE_HARDWARE;
12548
Chet Haasea1cff502012-02-21 13:43:44 -080012549 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012550 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012551 restoreTo = canvas.save();
12552 }
Chet Haase64a48c12012-02-13 16:33:29 -080012553 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012554 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012555 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012556 if (!useDisplayListProperties) {
12557 canvas.translate(mLeft, mTop);
12558 }
Chet Haase64a48c12012-02-13 16:33:29 -080012559 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012560 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012561 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012562 restoreTo = canvas.save();
12563 }
Chet Haase64a48c12012-02-13 16:33:29 -080012564 // mAttachInfo cannot be null, otherwise scalingRequired == false
12565 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12566 canvas.scale(scale, scale);
12567 }
12568 }
12569
Chet Haasea1cff502012-02-21 13:43:44 -080012570 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012571 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012572 if (transformToApply != null || !childHasIdentityMatrix) {
12573 int transX = 0;
12574 int transY = 0;
12575
12576 if (offsetForScroll) {
12577 transX = -sx;
12578 transY = -sy;
12579 }
12580
12581 if (transformToApply != null) {
12582 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012583 if (useDisplayListProperties) {
12584 displayList.setAnimationMatrix(transformToApply.getMatrix());
12585 } else {
12586 // Undo the scroll translation, apply the transformation matrix,
12587 // then redo the scroll translate to get the correct result.
12588 canvas.translate(-transX, -transY);
12589 canvas.concat(transformToApply.getMatrix());
12590 canvas.translate(transX, transY);
12591 }
Chet Haasea1cff502012-02-21 13:43:44 -080012592 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012593 }
12594
12595 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012596 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012597 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012598 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012599 }
12600 }
12601
Chet Haasea1cff502012-02-21 13:43:44 -080012602 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012603 canvas.translate(-transX, -transY);
12604 canvas.concat(getMatrix());
12605 canvas.translate(transX, transY);
12606 }
12607 }
12608
Chet Haase9420abd2012-03-29 16:28:32 -070012609 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012610 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012611 if (hasNoCache) {
12612 final int multipliedAlpha = (int) (255 * alpha);
12613 if (!onSetAlpha(multipliedAlpha)) {
12614 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012615 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012616 layerType != LAYER_TYPE_NONE) {
12617 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12618 }
Chet Haase9420abd2012-03-29 16:28:32 -070012619 if (useDisplayListProperties) {
12620 displayList.setAlpha(alpha * getAlpha());
12621 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012622 final int scrollX = hasDisplayList ? 0 : sx;
12623 final int scrollY = hasDisplayList ? 0 : sy;
12624 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12625 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012626 }
12627 } else {
12628 // Alpha is handled by the child directly, clobber the layer's alpha
12629 mPrivateFlags |= ALPHA_SET;
12630 }
12631 }
12632 }
12633 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12634 onSetAlpha(255);
12635 mPrivateFlags &= ~ALPHA_SET;
12636 }
12637
Chet Haasea1cff502012-02-21 13:43:44 -080012638 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12639 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012640 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012641 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012642 } else {
12643 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012644 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012645 } else {
12646 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12647 }
12648 }
12649 }
12650
Chet Haase9420abd2012-03-29 16:28:32 -070012651 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012652 displayList = getDisplayList();
12653 if (!displayList.isValid()) {
12654 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12655 // to getDisplayList(), the display list will be marked invalid and we should not
12656 // try to use it again.
12657 displayList = null;
12658 hasDisplayList = false;
12659 }
12660 }
12661
12662 if (hasNoCache) {
12663 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012664 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012665 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012666 if (layer != null && layer.isValid()) {
12667 mLayerPaint.setAlpha((int) (alpha * 255));
12668 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12669 layerRendered = true;
12670 } else {
12671 final int scrollX = hasDisplayList ? 0 : sx;
12672 final int scrollY = hasDisplayList ? 0 : sy;
12673 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012674 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012675 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12676 }
12677 }
12678
12679 if (!layerRendered) {
12680 if (!hasDisplayList) {
12681 // Fast path for layouts with no backgrounds
12682 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12683 if (ViewDebug.TRACE_HIERARCHY) {
12684 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12685 }
12686 mPrivateFlags &= ~DIRTY_MASK;
12687 dispatchDraw(canvas);
12688 } else {
12689 draw(canvas);
12690 }
12691 } else {
12692 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070012693 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012694 }
12695 }
12696 } else if (cache != null) {
12697 mPrivateFlags &= ~DIRTY_MASK;
12698 Paint cachePaint;
12699
12700 if (layerType == LAYER_TYPE_NONE) {
12701 cachePaint = parent.mCachePaint;
12702 if (cachePaint == null) {
12703 cachePaint = new Paint();
12704 cachePaint.setDither(false);
12705 parent.mCachePaint = cachePaint;
12706 }
Chet Haase9420abd2012-03-29 16:28:32 -070012707 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012708 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012709 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12710 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012711 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012712 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012713 }
12714 } else {
12715 cachePaint = mLayerPaint;
12716 cachePaint.setAlpha((int) (alpha * 255));
12717 }
12718 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12719 }
12720
Chet Haasea1cff502012-02-21 13:43:44 -080012721 if (restoreTo >= 0) {
12722 canvas.restoreToCount(restoreTo);
12723 }
Chet Haase64a48c12012-02-13 16:33:29 -080012724
12725 if (a != null && !more) {
12726 if (!hardwareAccelerated && !a.getFillAfter()) {
12727 onSetAlpha(255);
12728 }
12729 parent.finishAnimatingView(this, a);
12730 }
12731
12732 if (more && hardwareAccelerated) {
12733 // invalidation is the trigger to recreate display lists, so if we're using
12734 // display lists to render, force an invalidate to allow the animation to
12735 // continue drawing another frame
12736 parent.invalidate(true);
12737 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12738 // alpha animations should cause the child to recreate its display list
12739 invalidate(true);
12740 }
12741 }
12742
12743 mRecreateDisplayList = false;
12744
12745 return more;
12746 }
12747
12748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012749 * Manually render this view (and all of its children) to the given Canvas.
12750 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012751 * called. When implementing a view, implement
12752 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12753 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012754 *
12755 * @param canvas The Canvas to which the View is rendered.
12756 */
12757 public void draw(Canvas canvas) {
12758 if (ViewDebug.TRACE_HIERARCHY) {
12759 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12760 }
12761
Romain Guy5bcdff42009-05-14 21:27:18 -070012762 final int privateFlags = mPrivateFlags;
12763 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12764 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12765 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012767 /*
12768 * Draw traversal performs several drawing steps which must be executed
12769 * in the appropriate order:
12770 *
12771 * 1. Draw the background
12772 * 2. If necessary, save the canvas' layers to prepare for fading
12773 * 3. Draw view's content
12774 * 4. Draw children
12775 * 5. If necessary, draw the fading edges and restore layers
12776 * 6. Draw decorations (scrollbars for instance)
12777 */
12778
12779 // Step 1, draw the background, if needed
12780 int saveCount;
12781
Romain Guy24443ea2009-05-11 11:56:30 -070012782 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012783 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012784 if (background != null) {
12785 final int scrollX = mScrollX;
12786 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012787
Romain Guy24443ea2009-05-11 11:56:30 -070012788 if (mBackgroundSizeChanged) {
12789 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12790 mBackgroundSizeChanged = false;
12791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012792
Romain Guy24443ea2009-05-11 11:56:30 -070012793 if ((scrollX | scrollY) == 0) {
12794 background.draw(canvas);
12795 } else {
12796 canvas.translate(scrollX, scrollY);
12797 background.draw(canvas);
12798 canvas.translate(-scrollX, -scrollY);
12799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012800 }
12801 }
12802
12803 // skip step 2 & 5 if possible (common case)
12804 final int viewFlags = mViewFlags;
12805 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12806 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12807 if (!verticalEdges && !horizontalEdges) {
12808 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012809 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810
12811 // Step 4, draw the children
12812 dispatchDraw(canvas);
12813
12814 // Step 6, draw decorations (scrollbars)
12815 onDrawScrollBars(canvas);
12816
12817 // we're done...
12818 return;
12819 }
12820
12821 /*
12822 * Here we do the full fledged routine...
12823 * (this is an uncommon case where speed matters less,
12824 * this is why we repeat some of the tests that have been
12825 * done above)
12826 */
12827
12828 boolean drawTop = false;
12829 boolean drawBottom = false;
12830 boolean drawLeft = false;
12831 boolean drawRight = false;
12832
12833 float topFadeStrength = 0.0f;
12834 float bottomFadeStrength = 0.0f;
12835 float leftFadeStrength = 0.0f;
12836 float rightFadeStrength = 0.0f;
12837
12838 // Step 2, save the canvas' layers
12839 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012840
12841 final boolean offsetRequired = isPaddingOffsetRequired();
12842 if (offsetRequired) {
12843 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012844 }
12845
12846 int left = mScrollX + paddingLeft;
12847 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012848 int top = mScrollY + getFadeTop(offsetRequired);
12849 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012850
12851 if (offsetRequired) {
12852 right += getRightPaddingOffset();
12853 bottom += getBottomPaddingOffset();
12854 }
12855
12856 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012857 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012858 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859
12860 // clip the fade length if top and bottom fades overlap
12861 // overlapping fades produce odd-looking artifacts
12862 if (verticalEdges && (top + length > bottom - length)) {
12863 length = (bottom - top) / 2;
12864 }
12865
12866 // also clip horizontal fades if necessary
12867 if (horizontalEdges && (left + length > right - length)) {
12868 length = (right - left) / 2;
12869 }
12870
12871 if (verticalEdges) {
12872 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012873 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012874 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012875 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012876 }
12877
12878 if (horizontalEdges) {
12879 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012880 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012882 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012883 }
12884
12885 saveCount = canvas.getSaveCount();
12886
12887 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012888 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012889 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12890
12891 if (drawTop) {
12892 canvas.saveLayer(left, top, right, top + length, null, flags);
12893 }
12894
12895 if (drawBottom) {
12896 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12897 }
12898
12899 if (drawLeft) {
12900 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12901 }
12902
12903 if (drawRight) {
12904 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12905 }
12906 } else {
12907 scrollabilityCache.setFadeColor(solidColor);
12908 }
12909
12910 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012911 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012912
12913 // Step 4, draw the children
12914 dispatchDraw(canvas);
12915
12916 // Step 5, draw the fade effect and restore layers
12917 final Paint p = scrollabilityCache.paint;
12918 final Matrix matrix = scrollabilityCache.matrix;
12919 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012920
12921 if (drawTop) {
12922 matrix.setScale(1, fadeHeight * topFadeStrength);
12923 matrix.postTranslate(left, top);
12924 fade.setLocalMatrix(matrix);
12925 canvas.drawRect(left, top, right, top + length, p);
12926 }
12927
12928 if (drawBottom) {
12929 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12930 matrix.postRotate(180);
12931 matrix.postTranslate(left, bottom);
12932 fade.setLocalMatrix(matrix);
12933 canvas.drawRect(left, bottom - length, right, bottom, p);
12934 }
12935
12936 if (drawLeft) {
12937 matrix.setScale(1, fadeHeight * leftFadeStrength);
12938 matrix.postRotate(-90);
12939 matrix.postTranslate(left, top);
12940 fade.setLocalMatrix(matrix);
12941 canvas.drawRect(left, top, left + length, bottom, p);
12942 }
12943
12944 if (drawRight) {
12945 matrix.setScale(1, fadeHeight * rightFadeStrength);
12946 matrix.postRotate(90);
12947 matrix.postTranslate(right, top);
12948 fade.setLocalMatrix(matrix);
12949 canvas.drawRect(right - length, top, right, bottom, p);
12950 }
12951
12952 canvas.restoreToCount(saveCount);
12953
12954 // Step 6, draw decorations (scrollbars)
12955 onDrawScrollBars(canvas);
12956 }
12957
12958 /**
12959 * Override this if your view is known to always be drawn on top of a solid color background,
12960 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12961 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12962 * should be set to 0xFF.
12963 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012964 * @see #setVerticalFadingEdgeEnabled(boolean)
12965 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012966 *
12967 * @return The known solid color background for this view, or 0 if the color may vary
12968 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012969 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012970 public int getSolidColor() {
12971 return 0;
12972 }
12973
12974 /**
12975 * Build a human readable string representation of the specified view flags.
12976 *
12977 * @param flags the view flags to convert to a string
12978 * @return a String representing the supplied flags
12979 */
12980 private static String printFlags(int flags) {
12981 String output = "";
12982 int numFlags = 0;
12983 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12984 output += "TAKES_FOCUS";
12985 numFlags++;
12986 }
12987
12988 switch (flags & VISIBILITY_MASK) {
12989 case INVISIBLE:
12990 if (numFlags > 0) {
12991 output += " ";
12992 }
12993 output += "INVISIBLE";
12994 // USELESS HERE numFlags++;
12995 break;
12996 case GONE:
12997 if (numFlags > 0) {
12998 output += " ";
12999 }
13000 output += "GONE";
13001 // USELESS HERE numFlags++;
13002 break;
13003 default:
13004 break;
13005 }
13006 return output;
13007 }
13008
13009 /**
13010 * Build a human readable string representation of the specified private
13011 * view flags.
13012 *
13013 * @param privateFlags the private view flags to convert to a string
13014 * @return a String representing the supplied flags
13015 */
13016 private static String printPrivateFlags(int privateFlags) {
13017 String output = "";
13018 int numFlags = 0;
13019
13020 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13021 output += "WANTS_FOCUS";
13022 numFlags++;
13023 }
13024
13025 if ((privateFlags & FOCUSED) == FOCUSED) {
13026 if (numFlags > 0) {
13027 output += " ";
13028 }
13029 output += "FOCUSED";
13030 numFlags++;
13031 }
13032
13033 if ((privateFlags & SELECTED) == SELECTED) {
13034 if (numFlags > 0) {
13035 output += " ";
13036 }
13037 output += "SELECTED";
13038 numFlags++;
13039 }
13040
13041 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13042 if (numFlags > 0) {
13043 output += " ";
13044 }
13045 output += "IS_ROOT_NAMESPACE";
13046 numFlags++;
13047 }
13048
13049 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13050 if (numFlags > 0) {
13051 output += " ";
13052 }
13053 output += "HAS_BOUNDS";
13054 numFlags++;
13055 }
13056
13057 if ((privateFlags & DRAWN) == DRAWN) {
13058 if (numFlags > 0) {
13059 output += " ";
13060 }
13061 output += "DRAWN";
13062 // USELESS HERE numFlags++;
13063 }
13064 return output;
13065 }
13066
13067 /**
13068 * <p>Indicates whether or not this view's layout will be requested during
13069 * the next hierarchy layout pass.</p>
13070 *
13071 * @return true if the layout will be forced during next layout pass
13072 */
13073 public boolean isLayoutRequested() {
13074 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13075 }
13076
13077 /**
13078 * Assign a size and position to a view and all of its
13079 * descendants
13080 *
13081 * <p>This is the second phase of the layout mechanism.
13082 * (The first is measuring). In this phase, each parent calls
13083 * layout on all of its children to position them.
13084 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013085 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013086 *
Chet Haase9c087442011-01-12 16:20:16 -080013087 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013088 * Derived classes with children should override
13089 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013090 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013091 *
13092 * @param l Left position, relative to parent
13093 * @param t Top position, relative to parent
13094 * @param r Right position, relative to parent
13095 * @param b Bottom position, relative to parent
13096 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013097 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013098 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013099 int oldL = mLeft;
13100 int oldT = mTop;
13101 int oldB = mBottom;
13102 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013103 boolean changed = setFrame(l, t, r, b);
13104 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13105 if (ViewDebug.TRACE_HIERARCHY) {
13106 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13107 }
13108
13109 onLayout(changed, l, t, r, b);
13110 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013111
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013112 ListenerInfo li = mListenerInfo;
13113 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013114 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013115 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013116 int numListeners = listenersCopy.size();
13117 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013118 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013119 }
13120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013121 }
13122 mPrivateFlags &= ~FORCE_LAYOUT;
13123 }
13124
13125 /**
13126 * Called from layout when this view should
13127 * assign a size and position to each of its children.
13128 *
13129 * Derived classes with children should override
13130 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013131 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013132 * @param changed This is a new size or position for this view
13133 * @param left Left position, relative to parent
13134 * @param top Top position, relative to parent
13135 * @param right Right position, relative to parent
13136 * @param bottom Bottom position, relative to parent
13137 */
13138 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13139 }
13140
13141 /**
13142 * Assign a size and position to this view.
13143 *
13144 * This is called from layout.
13145 *
13146 * @param left Left position, relative to parent
13147 * @param top Top position, relative to parent
13148 * @param right Right position, relative to parent
13149 * @param bottom Bottom position, relative to parent
13150 * @return true if the new size and position are different than the
13151 * previous ones
13152 * {@hide}
13153 */
13154 protected boolean setFrame(int left, int top, int right, int bottom) {
13155 boolean changed = false;
13156
13157 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013158 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013159 + right + "," + bottom + ")");
13160 }
13161
13162 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13163 changed = true;
13164
13165 // Remember our drawn bit
13166 int drawn = mPrivateFlags & DRAWN;
13167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013168 int oldWidth = mRight - mLeft;
13169 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013170 int newWidth = right - left;
13171 int newHeight = bottom - top;
13172 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13173
13174 // Invalidate our old position
13175 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013176
13177 mLeft = left;
13178 mTop = top;
13179 mRight = right;
13180 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013181 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013182 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013184
13185 mPrivateFlags |= HAS_BOUNDS;
13186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187
Chet Haase75755e22011-07-18 17:48:25 -070013188 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013189 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13190 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013191 if (mTransformationInfo != null) {
13192 mTransformationInfo.mMatrixDirty = true;
13193 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013195 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13196 }
13197
13198 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13199 // If we are visible, force the DRAWN bit to on so that
13200 // this invalidate will go through (at least to our parent).
13201 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013202 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013203 // the DRAWN bit.
13204 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013205 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013206 // parent display list may need to be recreated based on a change in the bounds
13207 // of any child
13208 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013209 }
13210
13211 // Reset drawn bit to original value (invalidate turns it off)
13212 mPrivateFlags |= drawn;
13213
13214 mBackgroundSizeChanged = true;
13215 }
13216 return changed;
13217 }
13218
13219 /**
13220 * Finalize inflating a view from XML. This is called as the last phase
13221 * of inflation, after all child views have been added.
13222 *
13223 * <p>Even if the subclass overrides onFinishInflate, they should always be
13224 * sure to call the super method, so that we get called.
13225 */
13226 protected void onFinishInflate() {
13227 }
13228
13229 /**
13230 * Returns the resources associated with this view.
13231 *
13232 * @return Resources object.
13233 */
13234 public Resources getResources() {
13235 return mResources;
13236 }
13237
13238 /**
13239 * Invalidates the specified Drawable.
13240 *
13241 * @param drawable the drawable to invalidate
13242 */
13243 public void invalidateDrawable(Drawable drawable) {
13244 if (verifyDrawable(drawable)) {
13245 final Rect dirty = drawable.getBounds();
13246 final int scrollX = mScrollX;
13247 final int scrollY = mScrollY;
13248
13249 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13250 dirty.right + scrollX, dirty.bottom + scrollY);
13251 }
13252 }
13253
13254 /**
13255 * Schedules an action on a drawable to occur at a specified time.
13256 *
13257 * @param who the recipient of the action
13258 * @param what the action to run on the drawable
13259 * @param when the time at which the action must occur. Uses the
13260 * {@link SystemClock#uptimeMillis} timebase.
13261 */
13262 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013263 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013264 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013265 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013266 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13267 Choreographer.CALLBACK_ANIMATION, what, who,
13268 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013269 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013270 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013272 }
13273 }
13274
13275 /**
13276 * Cancels a scheduled action on a drawable.
13277 *
13278 * @param who the recipient of the action
13279 * @param what the action to cancel
13280 */
13281 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013282 if (verifyDrawable(who) && what != null) {
13283 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013284 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13285 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013286 } else {
13287 ViewRootImpl.getRunQueue().removeCallbacks(what);
13288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013289 }
13290 }
13291
13292 /**
13293 * Unschedule any events associated with the given Drawable. This can be
13294 * used when selecting a new Drawable into a view, so that the previous
13295 * one is completely unscheduled.
13296 *
13297 * @param who The Drawable to unschedule.
13298 *
13299 * @see #drawableStateChanged
13300 */
13301 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013302 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013303 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13304 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013305 }
13306 }
13307
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013308 /**
13309 * Return the layout direction of a given Drawable.
13310 *
13311 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013312 */
13313 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013314 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013315 }
13316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013317 /**
13318 * If your view subclass is displaying its own Drawable objects, it should
13319 * override this function and return true for any Drawable it is
13320 * displaying. This allows animations for those drawables to be
13321 * scheduled.
13322 *
13323 * <p>Be sure to call through to the super class when overriding this
13324 * function.
13325 *
13326 * @param who The Drawable to verify. Return true if it is one you are
13327 * displaying, else return the result of calling through to the
13328 * super class.
13329 *
13330 * @return boolean If true than the Drawable is being displayed in the
13331 * view; else false and it is not allowed to animate.
13332 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013333 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13334 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013335 */
13336 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013337 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338 }
13339
13340 /**
13341 * This function is called whenever the state of the view changes in such
13342 * a way that it impacts the state of drawables being shown.
13343 *
13344 * <p>Be sure to call through to the superclass when overriding this
13345 * function.
13346 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013347 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013348 */
13349 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013350 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013351 if (d != null && d.isStateful()) {
13352 d.setState(getDrawableState());
13353 }
13354 }
13355
13356 /**
13357 * Call this to force a view to update its drawable state. This will cause
13358 * drawableStateChanged to be called on this view. Views that are interested
13359 * in the new state should call getDrawableState.
13360 *
13361 * @see #drawableStateChanged
13362 * @see #getDrawableState
13363 */
13364 public void refreshDrawableState() {
13365 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13366 drawableStateChanged();
13367
13368 ViewParent parent = mParent;
13369 if (parent != null) {
13370 parent.childDrawableStateChanged(this);
13371 }
13372 }
13373
13374 /**
13375 * Return an array of resource IDs of the drawable states representing the
13376 * current state of the view.
13377 *
13378 * @return The current drawable state
13379 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013380 * @see Drawable#setState(int[])
13381 * @see #drawableStateChanged()
13382 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013383 */
13384 public final int[] getDrawableState() {
13385 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13386 return mDrawableState;
13387 } else {
13388 mDrawableState = onCreateDrawableState(0);
13389 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13390 return mDrawableState;
13391 }
13392 }
13393
13394 /**
13395 * Generate the new {@link android.graphics.drawable.Drawable} state for
13396 * this view. This is called by the view
13397 * system when the cached Drawable state is determined to be invalid. To
13398 * retrieve the current state, you should use {@link #getDrawableState}.
13399 *
13400 * @param extraSpace if non-zero, this is the number of extra entries you
13401 * would like in the returned array in which you can place your own
13402 * states.
13403 *
13404 * @return Returns an array holding the current {@link Drawable} state of
13405 * the view.
13406 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013407 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408 */
13409 protected int[] onCreateDrawableState(int extraSpace) {
13410 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13411 mParent instanceof View) {
13412 return ((View) mParent).onCreateDrawableState(extraSpace);
13413 }
13414
13415 int[] drawableState;
13416
13417 int privateFlags = mPrivateFlags;
13418
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013419 int viewStateIndex = 0;
13420 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13421 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13422 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013423 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013424 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13425 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013426 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13427 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013428 // This is set if HW acceleration is requested, even if the current
13429 // process doesn't allow it. This is just to allow app preview
13430 // windows to better match their app.
13431 viewStateIndex |= VIEW_STATE_ACCELERATED;
13432 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013433 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013434
Christopher Tate3d4bf172011-03-28 16:16:46 -070013435 final int privateFlags2 = mPrivateFlags2;
13436 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13437 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013439 drawableState = VIEW_STATE_SETS[viewStateIndex];
13440
13441 //noinspection ConstantIfStatement
13442 if (false) {
13443 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13444 Log.i("View", toString()
13445 + " pressed=" + ((privateFlags & PRESSED) != 0)
13446 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13447 + " fo=" + hasFocus()
13448 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013449 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013450 + ": " + Arrays.toString(drawableState));
13451 }
13452
13453 if (extraSpace == 0) {
13454 return drawableState;
13455 }
13456
13457 final int[] fullState;
13458 if (drawableState != null) {
13459 fullState = new int[drawableState.length + extraSpace];
13460 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13461 } else {
13462 fullState = new int[extraSpace];
13463 }
13464
13465 return fullState;
13466 }
13467
13468 /**
13469 * Merge your own state values in <var>additionalState</var> into the base
13470 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013471 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013472 *
13473 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013474 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013475 * own additional state values.
13476 *
13477 * @param additionalState The additional state values you would like
13478 * added to <var>baseState</var>; this array is not modified.
13479 *
13480 * @return As a convenience, the <var>baseState</var> array you originally
13481 * passed into the function is returned.
13482 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013483 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013484 */
13485 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13486 final int N = baseState.length;
13487 int i = N - 1;
13488 while (i >= 0 && baseState[i] == 0) {
13489 i--;
13490 }
13491 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13492 return baseState;
13493 }
13494
13495 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013496 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13497 * on all Drawable objects associated with this view.
13498 */
13499 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013500 if (mBackground != null) {
13501 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013502 }
13503 }
13504
13505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 * Sets the background color for this view.
13507 * @param color the color of the background
13508 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013509 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013511 if (mBackground instanceof ColorDrawable) {
13512 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013513 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013514 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013516 }
13517
13518 /**
13519 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013520 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013521 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013523 * @attr ref android.R.styleable#View_background
13524 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013525 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013526 public void setBackgroundResource(int resid) {
13527 if (resid != 0 && resid == mBackgroundResource) {
13528 return;
13529 }
13530
13531 Drawable d= null;
13532 if (resid != 0) {
13533 d = mResources.getDrawable(resid);
13534 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013535 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013536
13537 mBackgroundResource = resid;
13538 }
13539
13540 /**
13541 * Set the background to a given Drawable, or remove the background. If the
13542 * background has padding, this View's padding is set to the background's
13543 * padding. However, when a background is removed, this View's padding isn't
13544 * touched. If setting the padding is desired, please use
13545 * {@link #setPadding(int, int, int, int)}.
13546 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013547 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013548 * background
13549 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013550 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013551 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013552 setBackgroundDrawable(background);
13553 }
13554
13555 /**
13556 * @deprecated use {@link #setBackground(Drawable)} instead
13557 */
13558 @Deprecated
13559 public void setBackgroundDrawable(Drawable background) {
13560 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013561 return;
13562 }
13563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013564 boolean requestLayout = false;
13565
13566 mBackgroundResource = 0;
13567
13568 /*
13569 * Regardless of whether we're setting a new background or not, we want
13570 * to clear the previous drawable.
13571 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013572 if (mBackground != null) {
13573 mBackground.setCallback(null);
13574 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013575 }
13576
Philip Milne6c8ea062012-04-03 17:38:43 -070013577 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013578 Rect padding = sThreadLocal.get();
13579 if (padding == null) {
13580 padding = new Rect();
13581 sThreadLocal.set(padding);
13582 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013583 if (background.getPadding(padding)) {
13584 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013585 case LAYOUT_DIRECTION_RTL:
13586 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13587 break;
13588 case LAYOUT_DIRECTION_LTR:
13589 default:
13590 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013592 }
13593
13594 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13595 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013596 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13597 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013598 requestLayout = true;
13599 }
13600
Philip Milne6c8ea062012-04-03 17:38:43 -070013601 background.setCallback(this);
13602 if (background.isStateful()) {
13603 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013604 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013605 background.setVisible(getVisibility() == VISIBLE, false);
13606 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013607
13608 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13609 mPrivateFlags &= ~SKIP_DRAW;
13610 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13611 requestLayout = true;
13612 }
13613 } else {
13614 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013615 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013616
13617 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13618 /*
13619 * This view ONLY drew the background before and we're removing
13620 * the background, so now it won't draw anything
13621 * (hence we SKIP_DRAW)
13622 */
13623 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13624 mPrivateFlags |= SKIP_DRAW;
13625 }
13626
13627 /*
13628 * When the background is set, we try to apply its padding to this
13629 * View. When the background is removed, we don't touch this View's
13630 * padding. This is noted in the Javadocs. Hence, we don't need to
13631 * requestLayout(), the invalidate() below is sufficient.
13632 */
13633
13634 // The old background's minimum size could have affected this
13635 // View's layout, so let's requestLayout
13636 requestLayout = true;
13637 }
13638
Romain Guy8f1344f52009-05-15 16:03:59 -070013639 computeOpaqueFlags();
13640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013641 if (requestLayout) {
13642 requestLayout();
13643 }
13644
13645 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013646 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013647 }
13648
13649 /**
13650 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013651 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013653 *
13654 * @see #setBackground(Drawable)
13655 *
13656 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013657 */
13658 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013659 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013660 }
13661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013662 /**
13663 * Sets the padding. The view may add on the space required to display
13664 * the scrollbars, depending on the style and visibility of the scrollbars.
13665 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13666 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13667 * from the values set in this call.
13668 *
13669 * @attr ref android.R.styleable#View_padding
13670 * @attr ref android.R.styleable#View_paddingBottom
13671 * @attr ref android.R.styleable#View_paddingLeft
13672 * @attr ref android.R.styleable#View_paddingRight
13673 * @attr ref android.R.styleable#View_paddingTop
13674 * @param left the left padding in pixels
13675 * @param top the top padding in pixels
13676 * @param right the right padding in pixels
13677 * @param bottom the bottom padding in pixels
13678 */
13679 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013680 mUserPaddingStart = -1;
13681 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013682 mUserPaddingRelative = false;
13683
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013684 internalSetPadding(left, top, right, bottom);
13685 }
13686
13687 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013688 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013689 mUserPaddingRight = right;
13690 mUserPaddingBottom = bottom;
13691
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013692 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013693 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013694
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013695 // Common case is there are no scroll bars.
13696 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013697 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013698 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013699 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013700 switch (mVerticalScrollbarPosition) {
13701 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013702 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13703 left += offset;
13704 } else {
13705 right += offset;
13706 }
13707 break;
Adam Powell20232d02010-12-08 21:08:53 -080013708 case SCROLLBAR_POSITION_RIGHT:
13709 right += offset;
13710 break;
13711 case SCROLLBAR_POSITION_LEFT:
13712 left += offset;
13713 break;
13714 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013715 }
Adam Powell20232d02010-12-08 21:08:53 -080013716 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013717 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13718 ? 0 : getHorizontalScrollbarHeight();
13719 }
13720 }
Romain Guy8506ab42009-06-11 17:35:47 -070013721
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013722 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013723 changed = true;
13724 mPaddingLeft = left;
13725 }
13726 if (mPaddingTop != top) {
13727 changed = true;
13728 mPaddingTop = top;
13729 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013730 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013732 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013733 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013734 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013736 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737 }
13738
13739 if (changed) {
13740 requestLayout();
13741 }
13742 }
13743
13744 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013745 * Sets the relative padding. The view may add on the space required to display
13746 * the scrollbars, depending on the style and visibility of the scrollbars.
13747 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13748 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13749 * from the values set in this call.
13750 *
13751 * @attr ref android.R.styleable#View_padding
13752 * @attr ref android.R.styleable#View_paddingBottom
13753 * @attr ref android.R.styleable#View_paddingStart
13754 * @attr ref android.R.styleable#View_paddingEnd
13755 * @attr ref android.R.styleable#View_paddingTop
13756 * @param start the start padding in pixels
13757 * @param top the top padding in pixels
13758 * @param end the end padding in pixels
13759 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013760 */
13761 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013762 mUserPaddingStart = start;
13763 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013764 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013765
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013766 switch(getResolvedLayoutDirection()) {
13767 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013768 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013769 break;
13770 case LAYOUT_DIRECTION_LTR:
13771 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013772 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013773 }
13774 }
13775
13776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013777 * Returns the top padding of this view.
13778 *
13779 * @return the top padding in pixels
13780 */
13781 public int getPaddingTop() {
13782 return mPaddingTop;
13783 }
13784
13785 /**
13786 * Returns the bottom padding of this view. If there are inset and enabled
13787 * scrollbars, this value may include the space required to display the
13788 * scrollbars as well.
13789 *
13790 * @return the bottom padding in pixels
13791 */
13792 public int getPaddingBottom() {
13793 return mPaddingBottom;
13794 }
13795
13796 /**
13797 * Returns the left padding of this view. If there are inset and enabled
13798 * scrollbars, this value may include the space required to display the
13799 * scrollbars as well.
13800 *
13801 * @return the left padding in pixels
13802 */
13803 public int getPaddingLeft() {
13804 return mPaddingLeft;
13805 }
13806
13807 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013808 * Returns the start padding of this view depending on its resolved layout direction.
13809 * If there are inset and enabled scrollbars, this value may include the space
13810 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013811 *
13812 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013813 */
13814 public int getPaddingStart() {
13815 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13816 mPaddingRight : mPaddingLeft;
13817 }
13818
13819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820 * Returns the right padding of this view. If there are inset and enabled
13821 * scrollbars, this value may include the space required to display the
13822 * scrollbars as well.
13823 *
13824 * @return the right padding in pixels
13825 */
13826 public int getPaddingRight() {
13827 return mPaddingRight;
13828 }
13829
13830 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013831 * Returns the end padding of this view depending on its resolved layout direction.
13832 * If there are inset and enabled scrollbars, this value may include the space
13833 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013834 *
13835 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013836 */
13837 public int getPaddingEnd() {
13838 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13839 mPaddingLeft : mPaddingRight;
13840 }
13841
13842 /**
13843 * Return if the padding as been set thru relative values
13844 * {@link #setPaddingRelative(int, int, int, int)} or thru
13845 * @attr ref android.R.styleable#View_paddingStart or
13846 * @attr ref android.R.styleable#View_paddingEnd
13847 *
13848 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013849 */
13850 public boolean isPaddingRelative() {
13851 return mUserPaddingRelative;
13852 }
13853
13854 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013855 * @hide
13856 */
Philip Milne7a23b492012-04-24 22:12:36 -070013857 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070013858 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013859 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013860 }
13861 return mLayoutInsets;
13862 }
13863
13864 /**
13865 * @hide
13866 */
13867 public void setLayoutInsets(Insets layoutInsets) {
13868 mLayoutInsets = layoutInsets;
13869 }
13870
13871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013872 * Changes the selection state of this view. A view can be selected or not.
13873 * Note that selection is not the same as focus. Views are typically
13874 * selected in the context of an AdapterView like ListView or GridView;
13875 * the selected view is the view that is highlighted.
13876 *
13877 * @param selected true if the view must be selected, false otherwise
13878 */
13879 public void setSelected(boolean selected) {
13880 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13881 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013882 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013883 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013884 refreshDrawableState();
13885 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013886 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13887 notifyAccessibilityStateChanged();
13888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013889 }
13890 }
13891
13892 /**
13893 * Dispatch setSelected to all of this View's children.
13894 *
13895 * @see #setSelected(boolean)
13896 *
13897 * @param selected The new selected state
13898 */
13899 protected void dispatchSetSelected(boolean selected) {
13900 }
13901
13902 /**
13903 * Indicates the selection state of this view.
13904 *
13905 * @return true if the view is selected, false otherwise
13906 */
13907 @ViewDebug.ExportedProperty
13908 public boolean isSelected() {
13909 return (mPrivateFlags & SELECTED) != 0;
13910 }
13911
13912 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013913 * Changes the activated state of this view. A view can be activated or not.
13914 * Note that activation is not the same as selection. Selection is
13915 * a transient property, representing the view (hierarchy) the user is
13916 * currently interacting with. Activation is a longer-term state that the
13917 * user can move views in and out of. For example, in a list view with
13918 * single or multiple selection enabled, the views in the current selection
13919 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13920 * here.) The activated state is propagated down to children of the view it
13921 * is set on.
13922 *
13923 * @param activated true if the view must be activated, false otherwise
13924 */
13925 public void setActivated(boolean activated) {
13926 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13927 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013928 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013929 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013930 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013931 }
13932 }
13933
13934 /**
13935 * Dispatch setActivated to all of this View's children.
13936 *
13937 * @see #setActivated(boolean)
13938 *
13939 * @param activated The new activated state
13940 */
13941 protected void dispatchSetActivated(boolean activated) {
13942 }
13943
13944 /**
13945 * Indicates the activation state of this view.
13946 *
13947 * @return true if the view is activated, false otherwise
13948 */
13949 @ViewDebug.ExportedProperty
13950 public boolean isActivated() {
13951 return (mPrivateFlags & ACTIVATED) != 0;
13952 }
13953
13954 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13956 * observer can be used to get notifications when global events, like
13957 * layout, happen.
13958 *
13959 * The returned ViewTreeObserver observer is not guaranteed to remain
13960 * valid for the lifetime of this View. If the caller of this method keeps
13961 * a long-lived reference to ViewTreeObserver, it should always check for
13962 * the return value of {@link ViewTreeObserver#isAlive()}.
13963 *
13964 * @return The ViewTreeObserver for this view's hierarchy.
13965 */
13966 public ViewTreeObserver getViewTreeObserver() {
13967 if (mAttachInfo != null) {
13968 return mAttachInfo.mTreeObserver;
13969 }
13970 if (mFloatingTreeObserver == null) {
13971 mFloatingTreeObserver = new ViewTreeObserver();
13972 }
13973 return mFloatingTreeObserver;
13974 }
13975
13976 /**
13977 * <p>Finds the topmost view in the current view hierarchy.</p>
13978 *
13979 * @return the topmost view containing this view
13980 */
13981 public View getRootView() {
13982 if (mAttachInfo != null) {
13983 final View v = mAttachInfo.mRootView;
13984 if (v != null) {
13985 return v;
13986 }
13987 }
Romain Guy8506ab42009-06-11 17:35:47 -070013988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 View parent = this;
13990
13991 while (parent.mParent != null && parent.mParent instanceof View) {
13992 parent = (View) parent.mParent;
13993 }
13994
13995 return parent;
13996 }
13997
13998 /**
13999 * <p>Computes the coordinates of this view on the screen. The argument
14000 * must be an array of two integers. After the method returns, the array
14001 * contains the x and y location in that order.</p>
14002 *
14003 * @param location an array of two integers in which to hold the coordinates
14004 */
14005 public void getLocationOnScreen(int[] location) {
14006 getLocationInWindow(location);
14007
14008 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014009 if (info != null) {
14010 location[0] += info.mWindowLeft;
14011 location[1] += info.mWindowTop;
14012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 }
14014
14015 /**
14016 * <p>Computes the coordinates of this view in its window. The argument
14017 * must be an array of two integers. After the method returns, the array
14018 * contains the x and y location in that order.</p>
14019 *
14020 * @param location an array of two integers in which to hold the coordinates
14021 */
14022 public void getLocationInWindow(int[] location) {
14023 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014024 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014025 }
14026
Gilles Debunne6583ce52011-12-06 18:09:02 -080014027 if (mAttachInfo == null) {
14028 // When the view is not attached to a window, this method does not make sense
14029 location[0] = location[1] = 0;
14030 return;
14031 }
14032
Gilles Debunnecea45132011-11-24 02:19:27 +010014033 float[] position = mAttachInfo.mTmpTransformLocation;
14034 position[0] = position[1] = 0.0f;
14035
14036 if (!hasIdentityMatrix()) {
14037 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039
Gilles Debunnecea45132011-11-24 02:19:27 +010014040 position[0] += mLeft;
14041 position[1] += mTop;
14042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014043 ViewParent viewParent = mParent;
14044 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014045 final View view = (View) viewParent;
14046
14047 position[0] -= view.mScrollX;
14048 position[1] -= view.mScrollY;
14049
14050 if (!view.hasIdentityMatrix()) {
14051 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014052 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014053
14054 position[0] += view.mLeft;
14055 position[1] += view.mTop;
14056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014057 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014058 }
Romain Guy8506ab42009-06-11 17:35:47 -070014059
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014060 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014062 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14063 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014064 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014065
14066 location[0] = (int) (position[0] + 0.5f);
14067 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014068 }
14069
14070 /**
14071 * {@hide}
14072 * @param id the id of the view to be found
14073 * @return the view of the specified id, null if cannot be found
14074 */
14075 protected View findViewTraversal(int id) {
14076 if (id == mID) {
14077 return this;
14078 }
14079 return null;
14080 }
14081
14082 /**
14083 * {@hide}
14084 * @param tag the tag of the view to be found
14085 * @return the view of specified tag, null if cannot be found
14086 */
14087 protected View findViewWithTagTraversal(Object tag) {
14088 if (tag != null && tag.equals(mTag)) {
14089 return this;
14090 }
14091 return null;
14092 }
14093
14094 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014095 * {@hide}
14096 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014097 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014098 * @return The first view that matches the predicate or null.
14099 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014100 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014101 if (predicate.apply(this)) {
14102 return this;
14103 }
14104 return null;
14105 }
14106
14107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108 * Look for a child view with the given id. If this view has the given
14109 * id, return this view.
14110 *
14111 * @param id The id to search for.
14112 * @return The view that has the given id in the hierarchy or null
14113 */
14114 public final View findViewById(int id) {
14115 if (id < 0) {
14116 return null;
14117 }
14118 return findViewTraversal(id);
14119 }
14120
14121 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014122 * Finds a view by its unuque and stable accessibility id.
14123 *
14124 * @param accessibilityId The searched accessibility id.
14125 * @return The found view.
14126 */
14127 final View findViewByAccessibilityId(int accessibilityId) {
14128 if (accessibilityId < 0) {
14129 return null;
14130 }
14131 return findViewByAccessibilityIdTraversal(accessibilityId);
14132 }
14133
14134 /**
14135 * Performs the traversal to find a view by its unuque and stable accessibility id.
14136 *
14137 * <strong>Note:</strong>This method does not stop at the root namespace
14138 * boundary since the user can touch the screen at an arbitrary location
14139 * potentially crossing the root namespace bounday which will send an
14140 * accessibility event to accessibility services and they should be able
14141 * to obtain the event source. Also accessibility ids are guaranteed to be
14142 * unique in the window.
14143 *
14144 * @param accessibilityId The accessibility id.
14145 * @return The found view.
14146 */
14147 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14148 if (getAccessibilityViewId() == accessibilityId) {
14149 return this;
14150 }
14151 return null;
14152 }
14153
14154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014155 * Look for a child view with the given tag. If this view has the given
14156 * tag, return this view.
14157 *
14158 * @param tag The tag to search for, using "tag.equals(getTag())".
14159 * @return The View that has the given tag in the hierarchy or null
14160 */
14161 public final View findViewWithTag(Object tag) {
14162 if (tag == null) {
14163 return null;
14164 }
14165 return findViewWithTagTraversal(tag);
14166 }
14167
14168 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014169 * {@hide}
14170 * Look for a child view that matches the specified predicate.
14171 * If this view matches the predicate, return this view.
14172 *
14173 * @param predicate The predicate to evaluate.
14174 * @return The first view that matches the predicate or null.
14175 */
14176 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014177 return findViewByPredicateTraversal(predicate, null);
14178 }
14179
14180 /**
14181 * {@hide}
14182 * Look for a child view that matches the specified predicate,
14183 * starting with the specified view and its descendents and then
14184 * recusively searching the ancestors and siblings of that view
14185 * until this view is reached.
14186 *
14187 * This method is useful in cases where the predicate does not match
14188 * a single unique view (perhaps multiple views use the same id)
14189 * and we are trying to find the view that is "closest" in scope to the
14190 * starting view.
14191 *
14192 * @param start The view to start from.
14193 * @param predicate The predicate to evaluate.
14194 * @return The first view that matches the predicate or null.
14195 */
14196 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14197 View childToSkip = null;
14198 for (;;) {
14199 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14200 if (view != null || start == this) {
14201 return view;
14202 }
14203
14204 ViewParent parent = start.getParent();
14205 if (parent == null || !(parent instanceof View)) {
14206 return null;
14207 }
14208
14209 childToSkip = start;
14210 start = (View) parent;
14211 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014212 }
14213
14214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215 * Sets the identifier for this view. The identifier does not have to be
14216 * unique in this view's hierarchy. The identifier should be a positive
14217 * number.
14218 *
14219 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014220 * @see #getId()
14221 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014222 *
14223 * @param id a number used to identify the view
14224 *
14225 * @attr ref android.R.styleable#View_id
14226 */
14227 public void setId(int id) {
14228 mID = id;
14229 }
14230
14231 /**
14232 * {@hide}
14233 *
14234 * @param isRoot true if the view belongs to the root namespace, false
14235 * otherwise
14236 */
14237 public void setIsRootNamespace(boolean isRoot) {
14238 if (isRoot) {
14239 mPrivateFlags |= IS_ROOT_NAMESPACE;
14240 } else {
14241 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14242 }
14243 }
14244
14245 /**
14246 * {@hide}
14247 *
14248 * @return true if the view belongs to the root namespace, false otherwise
14249 */
14250 public boolean isRootNamespace() {
14251 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14252 }
14253
14254 /**
14255 * Returns this view's identifier.
14256 *
14257 * @return a positive integer used to identify the view or {@link #NO_ID}
14258 * if the view has no ID
14259 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014260 * @see #setId(int)
14261 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 * @attr ref android.R.styleable#View_id
14263 */
14264 @ViewDebug.CapturedViewProperty
14265 public int getId() {
14266 return mID;
14267 }
14268
14269 /**
14270 * Returns this view's tag.
14271 *
14272 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014273 *
14274 * @see #setTag(Object)
14275 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014276 */
14277 @ViewDebug.ExportedProperty
14278 public Object getTag() {
14279 return mTag;
14280 }
14281
14282 /**
14283 * Sets the tag associated with this view. A tag can be used to mark
14284 * a view in its hierarchy and does not have to be unique within the
14285 * hierarchy. Tags can also be used to store data within a view without
14286 * resorting to another data structure.
14287 *
14288 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014289 *
14290 * @see #getTag()
14291 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014292 */
14293 public void setTag(final Object tag) {
14294 mTag = tag;
14295 }
14296
14297 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014298 * Returns the tag associated with this view and the specified key.
14299 *
14300 * @param key The key identifying the tag
14301 *
14302 * @return the Object stored in this view as a tag
14303 *
14304 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014305 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014306 */
14307 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014308 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014309 return null;
14310 }
14311
14312 /**
14313 * Sets a tag associated with this view and a key. A tag can be used
14314 * to mark a view in its hierarchy and does not have to be unique within
14315 * the hierarchy. Tags can also be used to store data within a view
14316 * without resorting to another data structure.
14317 *
14318 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014319 * application to ensure it is unique (see the <a
14320 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14321 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014322 * the Android framework or not associated with any package will cause
14323 * an {@link IllegalArgumentException} to be thrown.
14324 *
14325 * @param key The key identifying the tag
14326 * @param tag An Object to tag the view with
14327 *
14328 * @throws IllegalArgumentException If they specified key is not valid
14329 *
14330 * @see #setTag(Object)
14331 * @see #getTag(int)
14332 */
14333 public void setTag(int key, final Object tag) {
14334 // If the package id is 0x00 or 0x01, it's either an undefined package
14335 // or a framework id
14336 if ((key >>> 24) < 2) {
14337 throw new IllegalArgumentException("The key must be an application-specific "
14338 + "resource id.");
14339 }
14340
Adam Powell2b2f6d62011-09-23 11:15:39 -070014341 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014342 }
14343
14344 /**
14345 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14346 * framework id.
14347 *
14348 * @hide
14349 */
14350 public void setTagInternal(int key, Object tag) {
14351 if ((key >>> 24) != 0x1) {
14352 throw new IllegalArgumentException("The key must be a framework-specific "
14353 + "resource id.");
14354 }
14355
Adam Powell2b2f6d62011-09-23 11:15:39 -070014356 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014357 }
14358
Adam Powell2b2f6d62011-09-23 11:15:39 -070014359 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014360 if (mKeyedTags == null) {
14361 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014362 }
14363
Adam Powell7db82ac2011-09-22 19:44:04 -070014364 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014365 }
14366
14367 /**
Romain Guy13922e02009-05-12 17:56:14 -070014368 * @param consistency The type of consistency. See ViewDebug for more information.
14369 *
14370 * @hide
14371 */
14372 protected boolean dispatchConsistencyCheck(int consistency) {
14373 return onConsistencyCheck(consistency);
14374 }
14375
14376 /**
14377 * Method that subclasses should implement to check their consistency. The type of
14378 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014379 *
Romain Guy13922e02009-05-12 17:56:14 -070014380 * @param consistency The type of consistency. See ViewDebug for more information.
14381 *
14382 * @throws IllegalStateException if the view is in an inconsistent state.
14383 *
14384 * @hide
14385 */
14386 protected boolean onConsistencyCheck(int consistency) {
14387 boolean result = true;
14388
14389 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14390 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14391
14392 if (checkLayout) {
14393 if (getParent() == null) {
14394 result = false;
14395 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14396 "View " + this + " does not have a parent.");
14397 }
14398
14399 if (mAttachInfo == null) {
14400 result = false;
14401 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14402 "View " + this + " is not attached to a window.");
14403 }
14404 }
14405
14406 if (checkDrawing) {
14407 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14408 // from their draw() method
14409
14410 if ((mPrivateFlags & DRAWN) != DRAWN &&
14411 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14412 result = false;
14413 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14414 "View " + this + " was invalidated but its drawing cache is valid.");
14415 }
14416 }
14417
14418 return result;
14419 }
14420
14421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 * Prints information about this view in the log output, with the tag
14423 * {@link #VIEW_LOG_TAG}.
14424 *
14425 * @hide
14426 */
14427 public void debug() {
14428 debug(0);
14429 }
14430
14431 /**
14432 * Prints information about this view in the log output, with the tag
14433 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14434 * indentation defined by the <code>depth</code>.
14435 *
14436 * @param depth the indentation level
14437 *
14438 * @hide
14439 */
14440 protected void debug(int depth) {
14441 String output = debugIndent(depth - 1);
14442
14443 output += "+ " + this;
14444 int id = getId();
14445 if (id != -1) {
14446 output += " (id=" + id + ")";
14447 }
14448 Object tag = getTag();
14449 if (tag != null) {
14450 output += " (tag=" + tag + ")";
14451 }
14452 Log.d(VIEW_LOG_TAG, output);
14453
14454 if ((mPrivateFlags & FOCUSED) != 0) {
14455 output = debugIndent(depth) + " FOCUSED";
14456 Log.d(VIEW_LOG_TAG, output);
14457 }
14458
14459 output = debugIndent(depth);
14460 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14461 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14462 + "} ";
14463 Log.d(VIEW_LOG_TAG, output);
14464
14465 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14466 || mPaddingBottom != 0) {
14467 output = debugIndent(depth);
14468 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14469 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14470 Log.d(VIEW_LOG_TAG, output);
14471 }
14472
14473 output = debugIndent(depth);
14474 output += "mMeasureWidth=" + mMeasuredWidth +
14475 " mMeasureHeight=" + mMeasuredHeight;
14476 Log.d(VIEW_LOG_TAG, output);
14477
14478 output = debugIndent(depth);
14479 if (mLayoutParams == null) {
14480 output += "BAD! no layout params";
14481 } else {
14482 output = mLayoutParams.debug(output);
14483 }
14484 Log.d(VIEW_LOG_TAG, output);
14485
14486 output = debugIndent(depth);
14487 output += "flags={";
14488 output += View.printFlags(mViewFlags);
14489 output += "}";
14490 Log.d(VIEW_LOG_TAG, output);
14491
14492 output = debugIndent(depth);
14493 output += "privateFlags={";
14494 output += View.printPrivateFlags(mPrivateFlags);
14495 output += "}";
14496 Log.d(VIEW_LOG_TAG, output);
14497 }
14498
14499 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014500 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 *
14502 * @param depth the indentation level
14503 * @return a String containing (depth * 2 + 3) * 2 white spaces
14504 *
14505 * @hide
14506 */
14507 protected static String debugIndent(int depth) {
14508 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14509 for (int i = 0; i < (depth * 2) + 3; i++) {
14510 spaces.append(' ').append(' ');
14511 }
14512 return spaces.toString();
14513 }
14514
14515 /**
14516 * <p>Return the offset of the widget's text baseline from the widget's top
14517 * boundary. If this widget does not support baseline alignment, this
14518 * method returns -1. </p>
14519 *
14520 * @return the offset of the baseline within the widget's bounds or -1
14521 * if baseline alignment is not supported
14522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014523 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 public int getBaseline() {
14525 return -1;
14526 }
14527
14528 /**
14529 * Call this when something has changed which has invalidated the
14530 * layout of this view. This will schedule a layout pass of the view
14531 * tree.
14532 */
14533 public void requestLayout() {
14534 if (ViewDebug.TRACE_HIERARCHY) {
14535 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14536 }
14537
14538 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014539 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014540
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014541 if (mLayoutParams != null) {
14542 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14543 }
14544
14545 if (mParent != null && !mParent.isLayoutRequested()) {
14546 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014547 }
14548 }
14549
14550 /**
14551 * Forces this view to be laid out during the next layout pass.
14552 * This method does not call requestLayout() or forceLayout()
14553 * on the parent.
14554 */
14555 public void forceLayout() {
14556 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014557 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014558 }
14559
14560 /**
14561 * <p>
14562 * This is called to find out how big a view should be. The parent
14563 * supplies constraint information in the width and height parameters.
14564 * </p>
14565 *
14566 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014567 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014568 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014569 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014570 * </p>
14571 *
14572 *
14573 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14574 * parent
14575 * @param heightMeasureSpec Vertical space requirements as imposed by the
14576 * parent
14577 *
14578 * @see #onMeasure(int, int)
14579 */
14580 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14581 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14582 widthMeasureSpec != mOldWidthMeasureSpec ||
14583 heightMeasureSpec != mOldHeightMeasureSpec) {
14584
14585 // first clears the measured dimension flag
14586 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14587
14588 if (ViewDebug.TRACE_HIERARCHY) {
14589 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14590 }
14591
14592 // measure ourselves, this should set the measured dimension flag back
14593 onMeasure(widthMeasureSpec, heightMeasureSpec);
14594
14595 // flag not set, setMeasuredDimension() was not invoked, we raise
14596 // an exception to warn the developer
14597 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14598 throw new IllegalStateException("onMeasure() did not set the"
14599 + " measured dimension by calling"
14600 + " setMeasuredDimension()");
14601 }
14602
14603 mPrivateFlags |= LAYOUT_REQUIRED;
14604 }
14605
14606 mOldWidthMeasureSpec = widthMeasureSpec;
14607 mOldHeightMeasureSpec = heightMeasureSpec;
14608 }
14609
14610 /**
14611 * <p>
14612 * Measure the view and its content to determine the measured width and the
14613 * measured height. This method is invoked by {@link #measure(int, int)} and
14614 * should be overriden by subclasses to provide accurate and efficient
14615 * measurement of their contents.
14616 * </p>
14617 *
14618 * <p>
14619 * <strong>CONTRACT:</strong> When overriding this method, you
14620 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14621 * measured width and height of this view. Failure to do so will trigger an
14622 * <code>IllegalStateException</code>, thrown by
14623 * {@link #measure(int, int)}. Calling the superclass'
14624 * {@link #onMeasure(int, int)} is a valid use.
14625 * </p>
14626 *
14627 * <p>
14628 * The base class implementation of measure defaults to the background size,
14629 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14630 * override {@link #onMeasure(int, int)} to provide better measurements of
14631 * their content.
14632 * </p>
14633 *
14634 * <p>
14635 * If this method is overridden, it is the subclass's responsibility to make
14636 * sure the measured height and width are at least the view's minimum height
14637 * and width ({@link #getSuggestedMinimumHeight()} and
14638 * {@link #getSuggestedMinimumWidth()}).
14639 * </p>
14640 *
14641 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14642 * The requirements are encoded with
14643 * {@link android.view.View.MeasureSpec}.
14644 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14645 * The requirements are encoded with
14646 * {@link android.view.View.MeasureSpec}.
14647 *
14648 * @see #getMeasuredWidth()
14649 * @see #getMeasuredHeight()
14650 * @see #setMeasuredDimension(int, int)
14651 * @see #getSuggestedMinimumHeight()
14652 * @see #getSuggestedMinimumWidth()
14653 * @see android.view.View.MeasureSpec#getMode(int)
14654 * @see android.view.View.MeasureSpec#getSize(int)
14655 */
14656 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14657 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14658 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14659 }
14660
14661 /**
14662 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14663 * measured width and measured height. Failing to do so will trigger an
14664 * exception at measurement time.</p>
14665 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014666 * @param measuredWidth The measured width of this view. May be a complex
14667 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14668 * {@link #MEASURED_STATE_TOO_SMALL}.
14669 * @param measuredHeight The measured height of this view. May be a complex
14670 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14671 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014672 */
14673 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14674 mMeasuredWidth = measuredWidth;
14675 mMeasuredHeight = measuredHeight;
14676
14677 mPrivateFlags |= MEASURED_DIMENSION_SET;
14678 }
14679
14680 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014681 * Merge two states as returned by {@link #getMeasuredState()}.
14682 * @param curState The current state as returned from a view or the result
14683 * of combining multiple views.
14684 * @param newState The new view state to combine.
14685 * @return Returns a new integer reflecting the combination of the two
14686 * states.
14687 */
14688 public static int combineMeasuredStates(int curState, int newState) {
14689 return curState | newState;
14690 }
14691
14692 /**
14693 * Version of {@link #resolveSizeAndState(int, int, int)}
14694 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14695 */
14696 public static int resolveSize(int size, int measureSpec) {
14697 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14698 }
14699
14700 /**
14701 * Utility to reconcile a desired size and state, with constraints imposed
14702 * by a MeasureSpec. Will take the desired size, unless a different size
14703 * is imposed by the constraints. The returned value is a compound integer,
14704 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14705 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14706 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014707 *
14708 * @param size How big the view wants to be
14709 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014710 * @return Size information bit mask as defined by
14711 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014712 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014713 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014714 int result = size;
14715 int specMode = MeasureSpec.getMode(measureSpec);
14716 int specSize = MeasureSpec.getSize(measureSpec);
14717 switch (specMode) {
14718 case MeasureSpec.UNSPECIFIED:
14719 result = size;
14720 break;
14721 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014722 if (specSize < size) {
14723 result = specSize | MEASURED_STATE_TOO_SMALL;
14724 } else {
14725 result = size;
14726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014727 break;
14728 case MeasureSpec.EXACTLY:
14729 result = specSize;
14730 break;
14731 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014732 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014733 }
14734
14735 /**
14736 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014737 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 * by the MeasureSpec.
14739 *
14740 * @param size Default size for this view
14741 * @param measureSpec Constraints imposed by the parent
14742 * @return The size this view should be.
14743 */
14744 public static int getDefaultSize(int size, int measureSpec) {
14745 int result = size;
14746 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014747 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014748
14749 switch (specMode) {
14750 case MeasureSpec.UNSPECIFIED:
14751 result = size;
14752 break;
14753 case MeasureSpec.AT_MOST:
14754 case MeasureSpec.EXACTLY:
14755 result = specSize;
14756 break;
14757 }
14758 return result;
14759 }
14760
14761 /**
14762 * Returns the suggested minimum height that the view should use. This
14763 * returns the maximum of the view's minimum height
14764 * and the background's minimum height
14765 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14766 * <p>
14767 * When being used in {@link #onMeasure(int, int)}, the caller should still
14768 * ensure the returned height is within the requirements of the parent.
14769 *
14770 * @return The suggested minimum height of the view.
14771 */
14772 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014773 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014775 }
14776
14777 /**
14778 * Returns the suggested minimum width that the view should use. This
14779 * returns the maximum of the view's minimum width)
14780 * and the background's minimum width
14781 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14782 * <p>
14783 * When being used in {@link #onMeasure(int, int)}, the caller should still
14784 * ensure the returned width is within the requirements of the parent.
14785 *
14786 * @return The suggested minimum width of the view.
14787 */
14788 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014789 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014791
Philip Milne6c8ea062012-04-03 17:38:43 -070014792 /**
14793 * Returns the minimum height of the view.
14794 *
14795 * @return the minimum height the view will try to be.
14796 *
14797 * @see #setMinimumHeight(int)
14798 *
14799 * @attr ref android.R.styleable#View_minHeight
14800 */
14801 public int getMinimumHeight() {
14802 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014803 }
14804
14805 /**
14806 * Sets the minimum height of the view. It is not guaranteed the view will
14807 * be able to achieve this minimum height (for example, if its parent layout
14808 * constrains it with less available height).
14809 *
14810 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014811 *
14812 * @see #getMinimumHeight()
14813 *
14814 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014815 */
14816 public void setMinimumHeight(int minHeight) {
14817 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014818 requestLayout();
14819 }
14820
14821 /**
14822 * Returns the minimum width of the view.
14823 *
14824 * @return the minimum width the view will try to be.
14825 *
14826 * @see #setMinimumWidth(int)
14827 *
14828 * @attr ref android.R.styleable#View_minWidth
14829 */
14830 public int getMinimumWidth() {
14831 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014832 }
14833
14834 /**
14835 * Sets the minimum width of the view. It is not guaranteed the view will
14836 * be able to achieve this minimum width (for example, if its parent layout
14837 * constrains it with less available width).
14838 *
14839 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014840 *
14841 * @see #getMinimumWidth()
14842 *
14843 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014844 */
14845 public void setMinimumWidth(int minWidth) {
14846 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014847 requestLayout();
14848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014849 }
14850
14851 /**
14852 * Get the animation currently associated with this view.
14853 *
14854 * @return The animation that is currently playing or
14855 * scheduled to play for this view.
14856 */
14857 public Animation getAnimation() {
14858 return mCurrentAnimation;
14859 }
14860
14861 /**
14862 * Start the specified animation now.
14863 *
14864 * @param animation the animation to start now
14865 */
14866 public void startAnimation(Animation animation) {
14867 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14868 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014869 invalidateParentCaches();
14870 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014871 }
14872
14873 /**
14874 * Cancels any animations for this view.
14875 */
14876 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014877 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014878 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014881 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014882 }
14883
14884 /**
14885 * Sets the next animation to play for this view.
14886 * If you want the animation to play immediately, use
14887 * startAnimation. This method provides allows fine-grained
14888 * control over the start time and invalidation, but you
14889 * must make sure that 1) the animation has a start time set, and
14890 * 2) the view will be invalidated when the animation is supposed to
14891 * start.
14892 *
14893 * @param animation The next animation, or null.
14894 */
14895 public void setAnimation(Animation animation) {
14896 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014898 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014899 // If the screen is off assume the animation start time is now instead of
14900 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14901 // would cause the animation to start when the screen turns back on
14902 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14903 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14904 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014906 animation.reset();
14907 }
14908 }
14909
14910 /**
14911 * Invoked by a parent ViewGroup to notify the start of the animation
14912 * currently associated with this view. If you override this method,
14913 * always call super.onAnimationStart();
14914 *
14915 * @see #setAnimation(android.view.animation.Animation)
14916 * @see #getAnimation()
14917 */
14918 protected void onAnimationStart() {
14919 mPrivateFlags |= ANIMATION_STARTED;
14920 }
14921
14922 /**
14923 * Invoked by a parent ViewGroup to notify the end of the animation
14924 * currently associated with this view. If you override this method,
14925 * always call super.onAnimationEnd();
14926 *
14927 * @see #setAnimation(android.view.animation.Animation)
14928 * @see #getAnimation()
14929 */
14930 protected void onAnimationEnd() {
14931 mPrivateFlags &= ~ANIMATION_STARTED;
14932 }
14933
14934 /**
14935 * Invoked if there is a Transform that involves alpha. Subclass that can
14936 * draw themselves with the specified alpha should return true, and then
14937 * respect that alpha when their onDraw() is called. If this returns false
14938 * then the view may be redirected to draw into an offscreen buffer to
14939 * fulfill the request, which will look fine, but may be slower than if the
14940 * subclass handles it internally. The default implementation returns false.
14941 *
14942 * @param alpha The alpha (0..255) to apply to the view's drawing
14943 * @return true if the view can draw with the specified alpha.
14944 */
14945 protected boolean onSetAlpha(int alpha) {
14946 return false;
14947 }
14948
14949 /**
14950 * This is used by the RootView to perform an optimization when
14951 * the view hierarchy contains one or several SurfaceView.
14952 * SurfaceView is always considered transparent, but its children are not,
14953 * therefore all View objects remove themselves from the global transparent
14954 * region (passed as a parameter to this function).
14955 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014956 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014957 *
14958 * @return Returns true if the effective visibility of the view at this
14959 * point is opaque, regardless of the transparent region; returns false
14960 * if it is possible for underlying windows to be seen behind the view.
14961 *
14962 * {@hide}
14963 */
14964 public boolean gatherTransparentRegion(Region region) {
14965 final AttachInfo attachInfo = mAttachInfo;
14966 if (region != null && attachInfo != null) {
14967 final int pflags = mPrivateFlags;
14968 if ((pflags & SKIP_DRAW) == 0) {
14969 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14970 // remove it from the transparent region.
14971 final int[] location = attachInfo.mTransparentLocation;
14972 getLocationInWindow(location);
14973 region.op(location[0], location[1], location[0] + mRight - mLeft,
14974 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014975 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014976 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14977 // exists, so we remove the background drawable's non-transparent
14978 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014979 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014980 }
14981 }
14982 return true;
14983 }
14984
14985 /**
14986 * Play a sound effect for this view.
14987 *
14988 * <p>The framework will play sound effects for some built in actions, such as
14989 * clicking, but you may wish to play these effects in your widget,
14990 * for instance, for internal navigation.
14991 *
14992 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14993 * {@link #isSoundEffectsEnabled()} is true.
14994 *
14995 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14996 */
14997 public void playSoundEffect(int soundConstant) {
14998 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14999 return;
15000 }
15001 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15002 }
15003
15004 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015005 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015006 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015007 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015008 *
15009 * <p>The framework will provide haptic feedback for some built in actions,
15010 * such as long presses, but you may wish to provide feedback for your
15011 * own widget.
15012 *
15013 * <p>The feedback will only be performed if
15014 * {@link #isHapticFeedbackEnabled()} is true.
15015 *
15016 * @param feedbackConstant One of the constants defined in
15017 * {@link HapticFeedbackConstants}
15018 */
15019 public boolean performHapticFeedback(int feedbackConstant) {
15020 return performHapticFeedback(feedbackConstant, 0);
15021 }
15022
15023 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015024 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015025 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015026 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015027 *
15028 * @param feedbackConstant One of the constants defined in
15029 * {@link HapticFeedbackConstants}
15030 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15031 */
15032 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15033 if (mAttachInfo == null) {
15034 return false;
15035 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015036 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015037 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015038 && !isHapticFeedbackEnabled()) {
15039 return false;
15040 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015041 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15042 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015043 }
15044
15045 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015046 * Request that the visibility of the status bar or other screen/window
15047 * decorations be changed.
15048 *
15049 * <p>This method is used to put the over device UI into temporary modes
15050 * where the user's attention is focused more on the application content,
15051 * by dimming or hiding surrounding system affordances. This is typically
15052 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15053 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15054 * to be placed behind the action bar (and with these flags other system
15055 * affordances) so that smooth transitions between hiding and showing them
15056 * can be done.
15057 *
15058 * <p>Two representative examples of the use of system UI visibility is
15059 * implementing a content browsing application (like a magazine reader)
15060 * and a video playing application.
15061 *
15062 * <p>The first code shows a typical implementation of a View in a content
15063 * browsing application. In this implementation, the application goes
15064 * into a content-oriented mode by hiding the status bar and action bar,
15065 * and putting the navigation elements into lights out mode. The user can
15066 * then interact with content while in this mode. Such an application should
15067 * provide an easy way for the user to toggle out of the mode (such as to
15068 * check information in the status bar or access notifications). In the
15069 * implementation here, this is done simply by tapping on the content.
15070 *
15071 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15072 * content}
15073 *
15074 * <p>This second code sample shows a typical implementation of a View
15075 * in a video playing application. In this situation, while the video is
15076 * playing the application would like to go into a complete full-screen mode,
15077 * to use as much of the display as possible for the video. When in this state
15078 * the user can not interact with the application; the system intercepts
15079 * touching on the screen to pop the UI out of full screen mode.
15080 *
15081 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15082 * content}
15083 *
15084 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15085 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15086 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15087 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015088 */
15089 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015090 if (visibility != mSystemUiVisibility) {
15091 mSystemUiVisibility = visibility;
15092 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15093 mParent.recomputeViewAttributes(this);
15094 }
Joe Onorato664644d2011-01-23 17:53:23 -080015095 }
15096 }
15097
15098 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015099 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15100 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15101 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15102 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15103 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015104 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015105 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015106 return mSystemUiVisibility;
15107 }
15108
Scott Mainec6331b2011-05-24 16:55:56 -070015109 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015110 * Returns the current system UI visibility that is currently set for
15111 * the entire window. This is the combination of the
15112 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15113 * views in the window.
15114 */
15115 public int getWindowSystemUiVisibility() {
15116 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15117 }
15118
15119 /**
15120 * Override to find out when the window's requested system UI visibility
15121 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15122 * This is different from the callbacks recieved through
15123 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15124 * in that this is only telling you about the local request of the window,
15125 * not the actual values applied by the system.
15126 */
15127 public void onWindowSystemUiVisibilityChanged(int visible) {
15128 }
15129
15130 /**
15131 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15132 * the view hierarchy.
15133 */
15134 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15135 onWindowSystemUiVisibilityChanged(visible);
15136 }
15137
15138 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015139 * Set a listener to receive callbacks when the visibility of the system bar changes.
15140 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15141 */
Joe Onorato664644d2011-01-23 17:53:23 -080015142 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015143 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015144 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15145 mParent.recomputeViewAttributes(this);
15146 }
15147 }
15148
15149 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015150 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15151 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015152 */
15153 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015154 ListenerInfo li = mListenerInfo;
15155 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15156 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015157 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015158 }
15159 }
15160
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015161 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15162 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15163 if (val != mSystemUiVisibility) {
15164 setSystemUiVisibility(val);
15165 }
15166 }
15167
Joe Onorato664644d2011-01-23 17:53:23 -080015168 /**
Joe Malin32736f02011-01-19 16:14:20 -080015169 * Creates an image that the system displays during the drag and drop
15170 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15171 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15172 * appearance as the given View. The default also positions the center of the drag shadow
15173 * directly under the touch point. If no View is provided (the constructor with no parameters
15174 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15175 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15176 * default is an invisible drag shadow.
15177 * <p>
15178 * You are not required to use the View you provide to the constructor as the basis of the
15179 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15180 * anything you want as the drag shadow.
15181 * </p>
15182 * <p>
15183 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15184 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15185 * size and position of the drag shadow. It uses this data to construct a
15186 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15187 * so that your application can draw the shadow image in the Canvas.
15188 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015189 *
15190 * <div class="special reference">
15191 * <h3>Developer Guides</h3>
15192 * <p>For a guide to implementing drag and drop features, read the
15193 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15194 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015195 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015196 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015197 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015198
15199 /**
Joe Malin32736f02011-01-19 16:14:20 -080015200 * Constructs a shadow image builder based on a View. By default, the resulting drag
15201 * shadow will have the same appearance and dimensions as the View, with the touch point
15202 * over the center of the View.
15203 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015204 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015205 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015206 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015207 }
15208
Christopher Tate17ed60c2011-01-18 12:50:26 -080015209 /**
15210 * Construct a shadow builder object with no associated View. This
15211 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15212 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15213 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015214 * reference to any View object. If they are not overridden, then the result is an
15215 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015216 */
15217 public DragShadowBuilder() {
15218 mView = new WeakReference<View>(null);
15219 }
15220
15221 /**
15222 * Returns the View object that had been passed to the
15223 * {@link #View.DragShadowBuilder(View)}
15224 * constructor. If that View parameter was {@code null} or if the
15225 * {@link #View.DragShadowBuilder()}
15226 * constructor was used to instantiate the builder object, this method will return
15227 * null.
15228 *
15229 * @return The View object associate with this builder object.
15230 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015231 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015232 final public View getView() {
15233 return mView.get();
15234 }
15235
Christopher Tate2c095f32010-10-04 14:13:40 -070015236 /**
Joe Malin32736f02011-01-19 16:14:20 -080015237 * Provides the metrics for the shadow image. These include the dimensions of
15238 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015239 * be centered under the touch location while dragging.
15240 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015241 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015242 * same as the dimensions of the View itself and centers the shadow under
15243 * the touch point.
15244 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015245 *
Joe Malin32736f02011-01-19 16:14:20 -080015246 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15247 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15248 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15249 * image.
15250 *
15251 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15252 * shadow image that should be underneath the touch point during the drag and drop
15253 * operation. Your application must set {@link android.graphics.Point#x} to the
15254 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015255 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015256 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015257 final View view = mView.get();
15258 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015259 shadowSize.set(view.getWidth(), view.getHeight());
15260 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015261 } else {
15262 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15263 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015264 }
15265
15266 /**
Joe Malin32736f02011-01-19 16:14:20 -080015267 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15268 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015269 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015270 *
Joe Malin32736f02011-01-19 16:14:20 -080015271 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015272 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015273 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015274 final View view = mView.get();
15275 if (view != null) {
15276 view.draw(canvas);
15277 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015278 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015279 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015280 }
15281 }
15282
15283 /**
Joe Malin32736f02011-01-19 16:14:20 -080015284 * Starts a drag and drop operation. When your application calls this method, it passes a
15285 * {@link android.view.View.DragShadowBuilder} object to the system. The
15286 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15287 * to get metrics for the drag shadow, and then calls the object's
15288 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15289 * <p>
15290 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15291 * drag events to all the View objects in your application that are currently visible. It does
15292 * this either by calling the View object's drag listener (an implementation of
15293 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15294 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15295 * Both are passed a {@link android.view.DragEvent} object that has a
15296 * {@link android.view.DragEvent#getAction()} value of
15297 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15298 * </p>
15299 * <p>
15300 * Your application can invoke startDrag() on any attached View object. The View object does not
15301 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15302 * be related to the View the user selected for dragging.
15303 * </p>
15304 * @param data A {@link android.content.ClipData} object pointing to the data to be
15305 * transferred by the drag and drop operation.
15306 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15307 * drag shadow.
15308 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15309 * drop operation. This Object is put into every DragEvent object sent by the system during the
15310 * current drag.
15311 * <p>
15312 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15313 * to the target Views. For example, it can contain flags that differentiate between a
15314 * a copy operation and a move operation.
15315 * </p>
15316 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15317 * so the parameter should be set to 0.
15318 * @return {@code true} if the method completes successfully, or
15319 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15320 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015321 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015322 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015323 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015324 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015325 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015326 }
15327 boolean okay = false;
15328
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015329 Point shadowSize = new Point();
15330 Point shadowTouchPoint = new Point();
15331 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015332
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015333 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15334 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15335 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015336 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015337
Chris Tatea32dcf72010-10-14 12:13:50 -070015338 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015339 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15340 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015341 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015342 Surface surface = new Surface();
15343 try {
15344 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015345 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015346 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015347 + " surface=" + surface);
15348 if (token != null) {
15349 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015350 try {
Chris Tate6b391282010-10-14 15:48:59 -070015351 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015352 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015353 } finally {
15354 surface.unlockCanvasAndPost(canvas);
15355 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015356
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015357 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015358
15359 // Cache the local state object for delivery with DragEvents
15360 root.setLocalDragState(myLocalState);
15361
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015362 // repurpose 'shadowSize' for the last touch point
15363 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015364
Christopher Tatea53146c2010-09-07 11:57:52 -070015365 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015366 shadowSize.x, shadowSize.y,
15367 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015368 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015369
15370 // Off and running! Release our local surface instance; the drag
15371 // shadow surface is now managed by the system process.
15372 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015373 }
15374 } catch (Exception e) {
15375 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15376 surface.destroy();
15377 }
15378
15379 return okay;
15380 }
15381
Christopher Tatea53146c2010-09-07 11:57:52 -070015382 /**
Joe Malin32736f02011-01-19 16:14:20 -080015383 * Handles drag events sent by the system following a call to
15384 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15385 *<p>
15386 * When the system calls this method, it passes a
15387 * {@link android.view.DragEvent} object. A call to
15388 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15389 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15390 * operation.
15391 * @param event The {@link android.view.DragEvent} sent by the system.
15392 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15393 * in DragEvent, indicating the type of drag event represented by this object.
15394 * @return {@code true} if the method was successful, otherwise {@code false}.
15395 * <p>
15396 * The method should return {@code true} in response to an action type of
15397 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15398 * operation.
15399 * </p>
15400 * <p>
15401 * The method should also return {@code true} in response to an action type of
15402 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15403 * {@code false} if it didn't.
15404 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015405 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015406 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015407 return false;
15408 }
15409
15410 /**
Joe Malin32736f02011-01-19 16:14:20 -080015411 * Detects if this View is enabled and has a drag event listener.
15412 * If both are true, then it calls the drag event listener with the
15413 * {@link android.view.DragEvent} it received. If the drag event listener returns
15414 * {@code true}, then dispatchDragEvent() returns {@code true}.
15415 * <p>
15416 * For all other cases, the method calls the
15417 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15418 * method and returns its result.
15419 * </p>
15420 * <p>
15421 * This ensures that a drag event is always consumed, even if the View does not have a drag
15422 * event listener. However, if the View has a listener and the listener returns true, then
15423 * onDragEvent() is not called.
15424 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015425 */
15426 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015427 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015428 ListenerInfo li = mListenerInfo;
15429 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15430 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015431 return true;
15432 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015433 return onDragEvent(event);
15434 }
15435
Christopher Tate3d4bf172011-03-28 16:16:46 -070015436 boolean canAcceptDrag() {
15437 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15438 }
15439
Christopher Tatea53146c2010-09-07 11:57:52 -070015440 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015441 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15442 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015443 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015444 */
15445 public void onCloseSystemDialogs(String reason) {
15446 }
Joe Malin32736f02011-01-19 16:14:20 -080015447
Dianne Hackbornffa42482009-09-23 22:20:11 -070015448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015449 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015450 * update a Region being computed for
15451 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 * that any non-transparent parts of the Drawable are removed from the
15453 * given transparent region.
15454 *
15455 * @param dr The Drawable whose transparency is to be applied to the region.
15456 * @param region A Region holding the current transparency information,
15457 * where any parts of the region that are set are considered to be
15458 * transparent. On return, this region will be modified to have the
15459 * transparency information reduced by the corresponding parts of the
15460 * Drawable that are not transparent.
15461 * {@hide}
15462 */
15463 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15464 if (DBG) {
15465 Log.i("View", "Getting transparent region for: " + this);
15466 }
15467 final Region r = dr.getTransparentRegion();
15468 final Rect db = dr.getBounds();
15469 final AttachInfo attachInfo = mAttachInfo;
15470 if (r != null && attachInfo != null) {
15471 final int w = getRight()-getLeft();
15472 final int h = getBottom()-getTop();
15473 if (db.left > 0) {
15474 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15475 r.op(0, 0, db.left, h, Region.Op.UNION);
15476 }
15477 if (db.right < w) {
15478 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15479 r.op(db.right, 0, w, h, Region.Op.UNION);
15480 }
15481 if (db.top > 0) {
15482 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15483 r.op(0, 0, w, db.top, Region.Op.UNION);
15484 }
15485 if (db.bottom < h) {
15486 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15487 r.op(0, db.bottom, w, h, Region.Op.UNION);
15488 }
15489 final int[] location = attachInfo.mTransparentLocation;
15490 getLocationInWindow(location);
15491 r.translate(location[0], location[1]);
15492 region.op(r, Region.Op.INTERSECT);
15493 } else {
15494 region.op(db, Region.Op.DIFFERENCE);
15495 }
15496 }
15497
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015498 private void checkForLongClick(int delayOffset) {
15499 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15500 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015501
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015502 if (mPendingCheckForLongPress == null) {
15503 mPendingCheckForLongPress = new CheckForLongPress();
15504 }
15505 mPendingCheckForLongPress.rememberWindowAttachCount();
15506 postDelayed(mPendingCheckForLongPress,
15507 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015509 }
15510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015511 /**
15512 * Inflate a view from an XML resource. This convenience method wraps the {@link
15513 * LayoutInflater} class, which provides a full range of options for view inflation.
15514 *
15515 * @param context The Context object for your activity or application.
15516 * @param resource The resource ID to inflate
15517 * @param root A view group that will be the parent. Used to properly inflate the
15518 * layout_* parameters.
15519 * @see LayoutInflater
15520 */
15521 public static View inflate(Context context, int resource, ViewGroup root) {
15522 LayoutInflater factory = LayoutInflater.from(context);
15523 return factory.inflate(resource, root);
15524 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526 /**
Adam Powell637d3372010-08-25 14:37:03 -070015527 * Scroll the view with standard behavior for scrolling beyond the normal
15528 * content boundaries. Views that call this method should override
15529 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15530 * results of an over-scroll operation.
15531 *
15532 * Views can use this method to handle any touch or fling-based scrolling.
15533 *
15534 * @param deltaX Change in X in pixels
15535 * @param deltaY Change in Y in pixels
15536 * @param scrollX Current X scroll value in pixels before applying deltaX
15537 * @param scrollY Current Y scroll value in pixels before applying deltaY
15538 * @param scrollRangeX Maximum content scroll range along the X axis
15539 * @param scrollRangeY Maximum content scroll range along the Y axis
15540 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15541 * along the X axis.
15542 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15543 * along the Y axis.
15544 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15545 * @return true if scrolling was clamped to an over-scroll boundary along either
15546 * axis, false otherwise.
15547 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015548 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015549 protected boolean overScrollBy(int deltaX, int deltaY,
15550 int scrollX, int scrollY,
15551 int scrollRangeX, int scrollRangeY,
15552 int maxOverScrollX, int maxOverScrollY,
15553 boolean isTouchEvent) {
15554 final int overScrollMode = mOverScrollMode;
15555 final boolean canScrollHorizontal =
15556 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15557 final boolean canScrollVertical =
15558 computeVerticalScrollRange() > computeVerticalScrollExtent();
15559 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15560 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15561 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15562 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15563
15564 int newScrollX = scrollX + deltaX;
15565 if (!overScrollHorizontal) {
15566 maxOverScrollX = 0;
15567 }
15568
15569 int newScrollY = scrollY + deltaY;
15570 if (!overScrollVertical) {
15571 maxOverScrollY = 0;
15572 }
15573
15574 // Clamp values if at the limits and record
15575 final int left = -maxOverScrollX;
15576 final int right = maxOverScrollX + scrollRangeX;
15577 final int top = -maxOverScrollY;
15578 final int bottom = maxOverScrollY + scrollRangeY;
15579
15580 boolean clampedX = false;
15581 if (newScrollX > right) {
15582 newScrollX = right;
15583 clampedX = true;
15584 } else if (newScrollX < left) {
15585 newScrollX = left;
15586 clampedX = true;
15587 }
15588
15589 boolean clampedY = false;
15590 if (newScrollY > bottom) {
15591 newScrollY = bottom;
15592 clampedY = true;
15593 } else if (newScrollY < top) {
15594 newScrollY = top;
15595 clampedY = true;
15596 }
15597
15598 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15599
15600 return clampedX || clampedY;
15601 }
15602
15603 /**
15604 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15605 * respond to the results of an over-scroll operation.
15606 *
15607 * @param scrollX New X scroll value in pixels
15608 * @param scrollY New Y scroll value in pixels
15609 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15610 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15611 */
15612 protected void onOverScrolled(int scrollX, int scrollY,
15613 boolean clampedX, boolean clampedY) {
15614 // Intentionally empty.
15615 }
15616
15617 /**
15618 * Returns the over-scroll mode for this view. The result will be
15619 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15620 * (allow over-scrolling only if the view content is larger than the container),
15621 * or {@link #OVER_SCROLL_NEVER}.
15622 *
15623 * @return This view's over-scroll mode.
15624 */
15625 public int getOverScrollMode() {
15626 return mOverScrollMode;
15627 }
15628
15629 /**
15630 * Set the over-scroll mode for this view. Valid over-scroll modes are
15631 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15632 * (allow over-scrolling only if the view content is larger than the container),
15633 * or {@link #OVER_SCROLL_NEVER}.
15634 *
15635 * Setting the over-scroll mode of a view will have an effect only if the
15636 * view is capable of scrolling.
15637 *
15638 * @param overScrollMode The new over-scroll mode for this view.
15639 */
15640 public void setOverScrollMode(int overScrollMode) {
15641 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15642 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15643 overScrollMode != OVER_SCROLL_NEVER) {
15644 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15645 }
15646 mOverScrollMode = overScrollMode;
15647 }
15648
15649 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015650 * Gets a scale factor that determines the distance the view should scroll
15651 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15652 * @return The vertical scroll scale factor.
15653 * @hide
15654 */
15655 protected float getVerticalScrollFactor() {
15656 if (mVerticalScrollFactor == 0) {
15657 TypedValue outValue = new TypedValue();
15658 if (!mContext.getTheme().resolveAttribute(
15659 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15660 throw new IllegalStateException(
15661 "Expected theme to define listPreferredItemHeight.");
15662 }
15663 mVerticalScrollFactor = outValue.getDimension(
15664 mContext.getResources().getDisplayMetrics());
15665 }
15666 return mVerticalScrollFactor;
15667 }
15668
15669 /**
15670 * Gets a scale factor that determines the distance the view should scroll
15671 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15672 * @return The horizontal scroll scale factor.
15673 * @hide
15674 */
15675 protected float getHorizontalScrollFactor() {
15676 // TODO: Should use something else.
15677 return getVerticalScrollFactor();
15678 }
15679
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015680 /**
15681 * Return the value specifying the text direction or policy that was set with
15682 * {@link #setTextDirection(int)}.
15683 *
15684 * @return the defined text direction. It can be one of:
15685 *
15686 * {@link #TEXT_DIRECTION_INHERIT},
15687 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15688 * {@link #TEXT_DIRECTION_ANY_RTL},
15689 * {@link #TEXT_DIRECTION_LTR},
15690 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015691 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015692 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015693 @ViewDebug.ExportedProperty(category = "text", mapping = {
15694 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15695 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15696 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15697 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15698 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15699 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15700 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015701 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015702 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015703 }
15704
15705 /**
15706 * Set the text direction.
15707 *
15708 * @param textDirection the direction to set. Should be one of:
15709 *
15710 * {@link #TEXT_DIRECTION_INHERIT},
15711 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15712 * {@link #TEXT_DIRECTION_ANY_RTL},
15713 * {@link #TEXT_DIRECTION_LTR},
15714 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015715 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015716 */
15717 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015718 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015719 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015720 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015721 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015722 // Set the new text direction
15723 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015724 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015725 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015726 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015727 }
15728 }
15729
15730 /**
15731 * Return the resolved text direction.
15732 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015733 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15734 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15735 * up the parent chain of the view. if there is no parent, then it will return the default
15736 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15737 *
15738 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015739 *
Doug Feltcb3791202011-07-07 11:57:48 -070015740 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15741 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015742 * {@link #TEXT_DIRECTION_LTR},
15743 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015744 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015745 */
15746 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015747 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015748 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015749 resolveTextDirection();
15750 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015751 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015752 }
15753
15754 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015755 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15756 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015757 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015758 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015759 // Reset any previous text direction resolution
15760 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15761
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015762 if (hasRtlSupport()) {
15763 // Set resolved text direction flag depending on text direction flag
15764 final int textDirection = getTextDirection();
15765 switch(textDirection) {
15766 case TEXT_DIRECTION_INHERIT:
15767 if (canResolveTextDirection()) {
15768 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015769
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015770 // Set current resolved direction to the same value as the parent's one
15771 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15772 switch (parentResolvedDirection) {
15773 case TEXT_DIRECTION_FIRST_STRONG:
15774 case TEXT_DIRECTION_ANY_RTL:
15775 case TEXT_DIRECTION_LTR:
15776 case TEXT_DIRECTION_RTL:
15777 case TEXT_DIRECTION_LOCALE:
15778 mPrivateFlags2 |=
15779 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15780 break;
15781 default:
15782 // Default resolved direction is "first strong" heuristic
15783 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15784 }
15785 } else {
15786 // We cannot do the resolution if there is no parent, so use the default one
15787 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015788 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015789 break;
15790 case TEXT_DIRECTION_FIRST_STRONG:
15791 case TEXT_DIRECTION_ANY_RTL:
15792 case TEXT_DIRECTION_LTR:
15793 case TEXT_DIRECTION_RTL:
15794 case TEXT_DIRECTION_LOCALE:
15795 // Resolved direction is the same as text direction
15796 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15797 break;
15798 default:
15799 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015800 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015801 }
15802 } else {
15803 // Default resolved direction is "first strong" heuristic
15804 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015805 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015806
15807 // Set to resolved
15808 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015809 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015810 }
15811
15812 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015813 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015814 * resolution should override this method.
15815 *
15816 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015817 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015818 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015819 }
15820
15821 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015822 * Check if text direction resolution can be done.
15823 *
15824 * @return true if text direction resolution can be done otherwise return false.
15825 */
15826 public boolean canResolveTextDirection() {
15827 switch (getTextDirection()) {
15828 case TEXT_DIRECTION_INHERIT:
15829 return (mParent != null) && (mParent instanceof ViewGroup);
15830 default:
15831 return true;
15832 }
15833 }
15834
15835 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015836 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015837 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015838 * reset is done.
15839 */
15840 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015841 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015842 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015843 }
15844
15845 /**
15846 * Called when text direction is reset. Subclasses that care about text direction reset should
15847 * override this method and do a reset of the text direction of their children. The default
15848 * implementation does nothing.
15849 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015850 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015851 }
15852
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015853 /**
15854 * Return the value specifying the text alignment or policy that was set with
15855 * {@link #setTextAlignment(int)}.
15856 *
15857 * @return the defined text alignment. It can be one of:
15858 *
15859 * {@link #TEXT_ALIGNMENT_INHERIT},
15860 * {@link #TEXT_ALIGNMENT_GRAVITY},
15861 * {@link #TEXT_ALIGNMENT_CENTER},
15862 * {@link #TEXT_ALIGNMENT_TEXT_START},
15863 * {@link #TEXT_ALIGNMENT_TEXT_END},
15864 * {@link #TEXT_ALIGNMENT_VIEW_START},
15865 * {@link #TEXT_ALIGNMENT_VIEW_END}
15866 */
15867 @ViewDebug.ExportedProperty(category = "text", mapping = {
15868 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15869 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15870 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15871 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15872 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15873 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15874 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15875 })
15876 public int getTextAlignment() {
15877 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15878 }
15879
15880 /**
15881 * Set the text alignment.
15882 *
15883 * @param textAlignment The text alignment to set. Should be one of
15884 *
15885 * {@link #TEXT_ALIGNMENT_INHERIT},
15886 * {@link #TEXT_ALIGNMENT_GRAVITY},
15887 * {@link #TEXT_ALIGNMENT_CENTER},
15888 * {@link #TEXT_ALIGNMENT_TEXT_START},
15889 * {@link #TEXT_ALIGNMENT_TEXT_END},
15890 * {@link #TEXT_ALIGNMENT_VIEW_START},
15891 * {@link #TEXT_ALIGNMENT_VIEW_END}
15892 *
15893 * @attr ref android.R.styleable#View_textAlignment
15894 */
15895 public void setTextAlignment(int textAlignment) {
15896 if (textAlignment != getTextAlignment()) {
15897 // Reset the current and resolved text alignment
15898 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15899 resetResolvedTextAlignment();
15900 // Set the new text alignment
15901 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15902 // Refresh
15903 requestLayout();
15904 invalidate(true);
15905 }
15906 }
15907
15908 /**
15909 * Return the resolved text alignment.
15910 *
15911 * The resolved text alignment. This needs resolution if the value is
15912 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15913 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15914 *
15915 * @return the resolved text alignment. Returns one of:
15916 *
15917 * {@link #TEXT_ALIGNMENT_GRAVITY},
15918 * {@link #TEXT_ALIGNMENT_CENTER},
15919 * {@link #TEXT_ALIGNMENT_TEXT_START},
15920 * {@link #TEXT_ALIGNMENT_TEXT_END},
15921 * {@link #TEXT_ALIGNMENT_VIEW_START},
15922 * {@link #TEXT_ALIGNMENT_VIEW_END}
15923 */
15924 @ViewDebug.ExportedProperty(category = "text", mapping = {
15925 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15926 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15927 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15928 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15929 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15930 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15931 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15932 })
15933 public int getResolvedTextAlignment() {
15934 // If text alignment is not resolved, then resolve it
15935 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15936 resolveTextAlignment();
15937 }
15938 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15939 }
15940
15941 /**
15942 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15943 * resolution is done.
15944 */
15945 public void resolveTextAlignment() {
15946 // Reset any previous text alignment resolution
15947 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15948
15949 if (hasRtlSupport()) {
15950 // Set resolved text alignment flag depending on text alignment flag
15951 final int textAlignment = getTextAlignment();
15952 switch (textAlignment) {
15953 case TEXT_ALIGNMENT_INHERIT:
15954 // Check if we can resolve the text alignment
15955 if (canResolveLayoutDirection() && mParent instanceof View) {
15956 View view = (View) mParent;
15957
15958 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15959 switch (parentResolvedTextAlignment) {
15960 case TEXT_ALIGNMENT_GRAVITY:
15961 case TEXT_ALIGNMENT_TEXT_START:
15962 case TEXT_ALIGNMENT_TEXT_END:
15963 case TEXT_ALIGNMENT_CENTER:
15964 case TEXT_ALIGNMENT_VIEW_START:
15965 case TEXT_ALIGNMENT_VIEW_END:
15966 // Resolved text alignment is the same as the parent resolved
15967 // text alignment
15968 mPrivateFlags2 |=
15969 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15970 break;
15971 default:
15972 // Use default resolved text alignment
15973 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15974 }
15975 }
15976 else {
15977 // We cannot do the resolution if there is no parent so use the default
15978 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15979 }
15980 break;
15981 case TEXT_ALIGNMENT_GRAVITY:
15982 case TEXT_ALIGNMENT_TEXT_START:
15983 case TEXT_ALIGNMENT_TEXT_END:
15984 case TEXT_ALIGNMENT_CENTER:
15985 case TEXT_ALIGNMENT_VIEW_START:
15986 case TEXT_ALIGNMENT_VIEW_END:
15987 // Resolved text alignment is the same as text alignment
15988 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15989 break;
15990 default:
15991 // Use default resolved text alignment
15992 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15993 }
15994 } else {
15995 // Use default resolved text alignment
15996 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15997 }
15998
15999 // Set the resolved
16000 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16001 onResolvedTextAlignmentChanged();
16002 }
16003
16004 /**
16005 * Check if text alignment resolution can be done.
16006 *
16007 * @return true if text alignment resolution can be done otherwise return false.
16008 */
16009 public boolean canResolveTextAlignment() {
16010 switch (getTextAlignment()) {
16011 case TEXT_DIRECTION_INHERIT:
16012 return (mParent != null);
16013 default:
16014 return true;
16015 }
16016 }
16017
16018 /**
16019 * Called when text alignment has been resolved. Subclasses that care about text alignment
16020 * resolution should override this method.
16021 *
16022 * The default implementation does nothing.
16023 */
16024 public void onResolvedTextAlignmentChanged() {
16025 }
16026
16027 /**
16028 * Reset resolved text alignment. Text alignment can be resolved with a call to
16029 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16030 * reset is done.
16031 */
16032 public void resetResolvedTextAlignment() {
16033 // Reset any previous text alignment resolution
16034 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16035 onResolvedTextAlignmentReset();
16036 }
16037
16038 /**
16039 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16040 * override this method and do a reset of the text alignment of their children. The default
16041 * implementation does nothing.
16042 */
16043 public void onResolvedTextAlignmentReset() {
16044 }
16045
Chet Haaseb39f0512011-05-24 14:36:40 -070016046 //
16047 // Properties
16048 //
16049 /**
16050 * A Property wrapper around the <code>alpha</code> functionality handled by the
16051 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16052 */
Chet Haased47f1532011-12-16 11:18:52 -080016053 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016054 @Override
16055 public void setValue(View object, float value) {
16056 object.setAlpha(value);
16057 }
16058
16059 @Override
16060 public Float get(View object) {
16061 return object.getAlpha();
16062 }
16063 };
16064
16065 /**
16066 * A Property wrapper around the <code>translationX</code> functionality handled by the
16067 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16068 */
Chet Haased47f1532011-12-16 11:18:52 -080016069 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016070 @Override
16071 public void setValue(View object, float value) {
16072 object.setTranslationX(value);
16073 }
16074
16075 @Override
16076 public Float get(View object) {
16077 return object.getTranslationX();
16078 }
16079 };
16080
16081 /**
16082 * A Property wrapper around the <code>translationY</code> functionality handled by the
16083 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16084 */
Chet Haased47f1532011-12-16 11:18:52 -080016085 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016086 @Override
16087 public void setValue(View object, float value) {
16088 object.setTranslationY(value);
16089 }
16090
16091 @Override
16092 public Float get(View object) {
16093 return object.getTranslationY();
16094 }
16095 };
16096
16097 /**
16098 * A Property wrapper around the <code>x</code> functionality handled by the
16099 * {@link View#setX(float)} and {@link View#getX()} methods.
16100 */
Chet Haased47f1532011-12-16 11:18:52 -080016101 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016102 @Override
16103 public void setValue(View object, float value) {
16104 object.setX(value);
16105 }
16106
16107 @Override
16108 public Float get(View object) {
16109 return object.getX();
16110 }
16111 };
16112
16113 /**
16114 * A Property wrapper around the <code>y</code> functionality handled by the
16115 * {@link View#setY(float)} and {@link View#getY()} methods.
16116 */
Chet Haased47f1532011-12-16 11:18:52 -080016117 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016118 @Override
16119 public void setValue(View object, float value) {
16120 object.setY(value);
16121 }
16122
16123 @Override
16124 public Float get(View object) {
16125 return object.getY();
16126 }
16127 };
16128
16129 /**
16130 * A Property wrapper around the <code>rotation</code> functionality handled by the
16131 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16132 */
Chet Haased47f1532011-12-16 11:18:52 -080016133 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016134 @Override
16135 public void setValue(View object, float value) {
16136 object.setRotation(value);
16137 }
16138
16139 @Override
16140 public Float get(View object) {
16141 return object.getRotation();
16142 }
16143 };
16144
16145 /**
16146 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16147 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16148 */
Chet Haased47f1532011-12-16 11:18:52 -080016149 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016150 @Override
16151 public void setValue(View object, float value) {
16152 object.setRotationX(value);
16153 }
16154
16155 @Override
16156 public Float get(View object) {
16157 return object.getRotationX();
16158 }
16159 };
16160
16161 /**
16162 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16163 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16164 */
Chet Haased47f1532011-12-16 11:18:52 -080016165 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016166 @Override
16167 public void setValue(View object, float value) {
16168 object.setRotationY(value);
16169 }
16170
16171 @Override
16172 public Float get(View object) {
16173 return object.getRotationY();
16174 }
16175 };
16176
16177 /**
16178 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16179 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16180 */
Chet Haased47f1532011-12-16 11:18:52 -080016181 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016182 @Override
16183 public void setValue(View object, float value) {
16184 object.setScaleX(value);
16185 }
16186
16187 @Override
16188 public Float get(View object) {
16189 return object.getScaleX();
16190 }
16191 };
16192
16193 /**
16194 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16195 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16196 */
Chet Haased47f1532011-12-16 11:18:52 -080016197 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016198 @Override
16199 public void setValue(View object, float value) {
16200 object.setScaleY(value);
16201 }
16202
16203 @Override
16204 public Float get(View object) {
16205 return object.getScaleY();
16206 }
16207 };
16208
Jeff Brown33bbfd22011-02-24 20:55:35 -080016209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016210 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16211 * Each MeasureSpec represents a requirement for either the width or the height.
16212 * A MeasureSpec is comprised of a size and a mode. There are three possible
16213 * modes:
16214 * <dl>
16215 * <dt>UNSPECIFIED</dt>
16216 * <dd>
16217 * The parent has not imposed any constraint on the child. It can be whatever size
16218 * it wants.
16219 * </dd>
16220 *
16221 * <dt>EXACTLY</dt>
16222 * <dd>
16223 * The parent has determined an exact size for the child. The child is going to be
16224 * given those bounds regardless of how big it wants to be.
16225 * </dd>
16226 *
16227 * <dt>AT_MOST</dt>
16228 * <dd>
16229 * The child can be as large as it wants up to the specified size.
16230 * </dd>
16231 * </dl>
16232 *
16233 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16234 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16235 */
16236 public static class MeasureSpec {
16237 private static final int MODE_SHIFT = 30;
16238 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16239
16240 /**
16241 * Measure specification mode: The parent has not imposed any constraint
16242 * on the child. It can be whatever size it wants.
16243 */
16244 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16245
16246 /**
16247 * Measure specification mode: The parent has determined an exact size
16248 * for the child. The child is going to be given those bounds regardless
16249 * of how big it wants to be.
16250 */
16251 public static final int EXACTLY = 1 << MODE_SHIFT;
16252
16253 /**
16254 * Measure specification mode: The child can be as large as it wants up
16255 * to the specified size.
16256 */
16257 public static final int AT_MOST = 2 << MODE_SHIFT;
16258
16259 /**
16260 * Creates a measure specification based on the supplied size and mode.
16261 *
16262 * The mode must always be one of the following:
16263 * <ul>
16264 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16265 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16266 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16267 * </ul>
16268 *
16269 * @param size the size of the measure specification
16270 * @param mode the mode of the measure specification
16271 * @return the measure specification based on size and mode
16272 */
16273 public static int makeMeasureSpec(int size, int mode) {
16274 return size + mode;
16275 }
16276
16277 /**
16278 * Extracts the mode from the supplied measure specification.
16279 *
16280 * @param measureSpec the measure specification to extract the mode from
16281 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16282 * {@link android.view.View.MeasureSpec#AT_MOST} or
16283 * {@link android.view.View.MeasureSpec#EXACTLY}
16284 */
16285 public static int getMode(int measureSpec) {
16286 return (measureSpec & MODE_MASK);
16287 }
16288
16289 /**
16290 * Extracts the size from the supplied measure specification.
16291 *
16292 * @param measureSpec the measure specification to extract the size from
16293 * @return the size in pixels defined in the supplied measure specification
16294 */
16295 public static int getSize(int measureSpec) {
16296 return (measureSpec & ~MODE_MASK);
16297 }
16298
16299 /**
16300 * Returns a String representation of the specified measure
16301 * specification.
16302 *
16303 * @param measureSpec the measure specification to convert to a String
16304 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16305 */
16306 public static String toString(int measureSpec) {
16307 int mode = getMode(measureSpec);
16308 int size = getSize(measureSpec);
16309
16310 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16311
16312 if (mode == UNSPECIFIED)
16313 sb.append("UNSPECIFIED ");
16314 else if (mode == EXACTLY)
16315 sb.append("EXACTLY ");
16316 else if (mode == AT_MOST)
16317 sb.append("AT_MOST ");
16318 else
16319 sb.append(mode).append(" ");
16320
16321 sb.append(size);
16322 return sb.toString();
16323 }
16324 }
16325
16326 class CheckForLongPress implements Runnable {
16327
16328 private int mOriginalWindowAttachCount;
16329
16330 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016331 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016332 && mOriginalWindowAttachCount == mWindowAttachCount) {
16333 if (performLongClick()) {
16334 mHasPerformedLongPress = true;
16335 }
16336 }
16337 }
16338
16339 public void rememberWindowAttachCount() {
16340 mOriginalWindowAttachCount = mWindowAttachCount;
16341 }
16342 }
Joe Malin32736f02011-01-19 16:14:20 -080016343
Adam Powelle14579b2009-12-16 18:39:52 -080016344 private final class CheckForTap implements Runnable {
16345 public void run() {
16346 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016347 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016348 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016349 }
16350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016351
Adam Powella35d7682010-03-12 14:48:13 -080016352 private final class PerformClick implements Runnable {
16353 public void run() {
16354 performClick();
16355 }
16356 }
16357
Dianne Hackborn63042d62011-01-26 18:56:29 -080016358 /** @hide */
16359 public void hackTurnOffWindowResizeAnim(boolean off) {
16360 mAttachInfo.mTurnOffWindowResizeAnim = off;
16361 }
Joe Malin32736f02011-01-19 16:14:20 -080016362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016363 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016364 * This method returns a ViewPropertyAnimator object, which can be used to animate
16365 * specific properties on this View.
16366 *
16367 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16368 */
16369 public ViewPropertyAnimator animate() {
16370 if (mAnimator == null) {
16371 mAnimator = new ViewPropertyAnimator(this);
16372 }
16373 return mAnimator;
16374 }
16375
16376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016377 * Interface definition for a callback to be invoked when a key event is
16378 * dispatched to this view. The callback will be invoked before the key
16379 * event is given to the view.
16380 */
16381 public interface OnKeyListener {
16382 /**
16383 * Called when a key is dispatched to a view. This allows listeners to
16384 * get a chance to respond before the target view.
16385 *
16386 * @param v The view the key has been dispatched to.
16387 * @param keyCode The code for the physical key that was pressed
16388 * @param event The KeyEvent object containing full information about
16389 * the event.
16390 * @return True if the listener has consumed the event, false otherwise.
16391 */
16392 boolean onKey(View v, int keyCode, KeyEvent event);
16393 }
16394
16395 /**
16396 * Interface definition for a callback to be invoked when a touch event is
16397 * dispatched to this view. The callback will be invoked before the touch
16398 * event is given to the view.
16399 */
16400 public interface OnTouchListener {
16401 /**
16402 * Called when a touch event is dispatched to a view. This allows listeners to
16403 * get a chance to respond before the target view.
16404 *
16405 * @param v The view the touch event has been dispatched to.
16406 * @param event The MotionEvent object containing full information about
16407 * the event.
16408 * @return True if the listener has consumed the event, false otherwise.
16409 */
16410 boolean onTouch(View v, MotionEvent event);
16411 }
16412
16413 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016414 * Interface definition for a callback to be invoked when a hover event is
16415 * dispatched to this view. The callback will be invoked before the hover
16416 * event is given to the view.
16417 */
16418 public interface OnHoverListener {
16419 /**
16420 * Called when a hover event is dispatched to a view. This allows listeners to
16421 * get a chance to respond before the target view.
16422 *
16423 * @param v The view the hover event has been dispatched to.
16424 * @param event The MotionEvent object containing full information about
16425 * the event.
16426 * @return True if the listener has consumed the event, false otherwise.
16427 */
16428 boolean onHover(View v, MotionEvent event);
16429 }
16430
16431 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016432 * Interface definition for a callback to be invoked when a generic motion event is
16433 * dispatched to this view. The callback will be invoked before the generic motion
16434 * event is given to the view.
16435 */
16436 public interface OnGenericMotionListener {
16437 /**
16438 * Called when a generic motion event is dispatched to a view. This allows listeners to
16439 * get a chance to respond before the target view.
16440 *
16441 * @param v The view the generic motion event has been dispatched to.
16442 * @param event The MotionEvent object containing full information about
16443 * the event.
16444 * @return True if the listener has consumed the event, false otherwise.
16445 */
16446 boolean onGenericMotion(View v, MotionEvent event);
16447 }
16448
16449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016450 * Interface definition for a callback to be invoked when a view has been clicked and held.
16451 */
16452 public interface OnLongClickListener {
16453 /**
16454 * Called when a view has been clicked and held.
16455 *
16456 * @param v The view that was clicked and held.
16457 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016458 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016459 */
16460 boolean onLongClick(View v);
16461 }
16462
16463 /**
Chris Tate32affef2010-10-18 15:29:21 -070016464 * Interface definition for a callback to be invoked when a drag is being dispatched
16465 * to this view. The callback will be invoked before the hosting view's own
16466 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16467 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016468 *
16469 * <div class="special reference">
16470 * <h3>Developer Guides</h3>
16471 * <p>For a guide to implementing drag and drop features, read the
16472 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16473 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016474 */
16475 public interface OnDragListener {
16476 /**
16477 * Called when a drag event is dispatched to a view. This allows listeners
16478 * to get a chance to override base View behavior.
16479 *
Joe Malin32736f02011-01-19 16:14:20 -080016480 * @param v The View that received the drag event.
16481 * @param event The {@link android.view.DragEvent} object for the drag event.
16482 * @return {@code true} if the drag event was handled successfully, or {@code false}
16483 * if the drag event was not handled. Note that {@code false} will trigger the View
16484 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016485 */
16486 boolean onDrag(View v, DragEvent event);
16487 }
16488
16489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016490 * Interface definition for a callback to be invoked when the focus state of
16491 * a view changed.
16492 */
16493 public interface OnFocusChangeListener {
16494 /**
16495 * Called when the focus state of a view has changed.
16496 *
16497 * @param v The view whose state has changed.
16498 * @param hasFocus The new focus state of v.
16499 */
16500 void onFocusChange(View v, boolean hasFocus);
16501 }
16502
16503 /**
16504 * Interface definition for a callback to be invoked when a view is clicked.
16505 */
16506 public interface OnClickListener {
16507 /**
16508 * Called when a view has been clicked.
16509 *
16510 * @param v The view that was clicked.
16511 */
16512 void onClick(View v);
16513 }
16514
16515 /**
16516 * Interface definition for a callback to be invoked when the context menu
16517 * for this view is being built.
16518 */
16519 public interface OnCreateContextMenuListener {
16520 /**
16521 * Called when the context menu for this view is being built. It is not
16522 * safe to hold onto the menu after this method returns.
16523 *
16524 * @param menu The context menu that is being built
16525 * @param v The view for which the context menu is being built
16526 * @param menuInfo Extra information about the item for which the
16527 * context menu should be shown. This information will vary
16528 * depending on the class of v.
16529 */
16530 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16531 }
16532
Joe Onorato664644d2011-01-23 17:53:23 -080016533 /**
16534 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016535 * visibility. This reports <strong>global</strong> changes to the system UI
16536 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016537 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016538 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016539 */
16540 public interface OnSystemUiVisibilityChangeListener {
16541 /**
16542 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016543 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016544 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016545 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016546 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16547 * <strong>global</strong> state of the UI visibility flags, not what your
16548 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016549 */
16550 public void onSystemUiVisibilityChange(int visibility);
16551 }
16552
Adam Powell4afd62b2011-02-18 15:02:18 -080016553 /**
16554 * Interface definition for a callback to be invoked when this view is attached
16555 * or detached from its window.
16556 */
16557 public interface OnAttachStateChangeListener {
16558 /**
16559 * Called when the view is attached to a window.
16560 * @param v The view that was attached
16561 */
16562 public void onViewAttachedToWindow(View v);
16563 /**
16564 * Called when the view is detached from a window.
16565 * @param v The view that was detached
16566 */
16567 public void onViewDetachedFromWindow(View v);
16568 }
16569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016570 private final class UnsetPressedState implements Runnable {
16571 public void run() {
16572 setPressed(false);
16573 }
16574 }
16575
16576 /**
16577 * Base class for derived classes that want to save and restore their own
16578 * state in {@link android.view.View#onSaveInstanceState()}.
16579 */
16580 public static class BaseSavedState extends AbsSavedState {
16581 /**
16582 * Constructor used when reading from a parcel. Reads the state of the superclass.
16583 *
16584 * @param source
16585 */
16586 public BaseSavedState(Parcel source) {
16587 super(source);
16588 }
16589
16590 /**
16591 * Constructor called by derived classes when creating their SavedState objects
16592 *
16593 * @param superState The state of the superclass of this view
16594 */
16595 public BaseSavedState(Parcelable superState) {
16596 super(superState);
16597 }
16598
16599 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16600 new Parcelable.Creator<BaseSavedState>() {
16601 public BaseSavedState createFromParcel(Parcel in) {
16602 return new BaseSavedState(in);
16603 }
16604
16605 public BaseSavedState[] newArray(int size) {
16606 return new BaseSavedState[size];
16607 }
16608 };
16609 }
16610
16611 /**
16612 * A set of information given to a view when it is attached to its parent
16613 * window.
16614 */
16615 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016616 interface Callbacks {
16617 void playSoundEffect(int effectId);
16618 boolean performHapticFeedback(int effectId, boolean always);
16619 }
16620
16621 /**
16622 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16623 * to a Handler. This class contains the target (View) to invalidate and
16624 * the coordinates of the dirty rectangle.
16625 *
16626 * For performance purposes, this class also implements a pool of up to
16627 * POOL_LIMIT objects that get reused. This reduces memory allocations
16628 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016629 */
Romain Guyd928d682009-03-31 17:52:16 -070016630 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016631 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016632 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16633 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016634 public InvalidateInfo newInstance() {
16635 return new InvalidateInfo();
16636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016637
Romain Guyd928d682009-03-31 17:52:16 -070016638 public void onAcquired(InvalidateInfo element) {
16639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016640
Romain Guyd928d682009-03-31 17:52:16 -070016641 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016642 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016643 }
16644 }, POOL_LIMIT)
16645 );
16646
16647 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016648 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016649
16650 View target;
16651
16652 int left;
16653 int top;
16654 int right;
16655 int bottom;
16656
Romain Guyd928d682009-03-31 17:52:16 -070016657 public void setNextPoolable(InvalidateInfo element) {
16658 mNext = element;
16659 }
16660
16661 public InvalidateInfo getNextPoolable() {
16662 return mNext;
16663 }
16664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016665 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016666 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016667 }
16668
16669 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016670 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016671 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016672
16673 public boolean isPooled() {
16674 return mIsPooled;
16675 }
16676
16677 public void setPooled(boolean isPooled) {
16678 mIsPooled = isPooled;
16679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016680 }
16681
16682 final IWindowSession mSession;
16683
16684 final IWindow mWindow;
16685
16686 final IBinder mWindowToken;
16687
16688 final Callbacks mRootCallbacks;
16689
Romain Guy59a12ca2011-06-09 17:48:21 -070016690 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016692 /**
16693 * The top view of the hierarchy.
16694 */
16695 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016697 IBinder mPanelParentWindowToken;
16698 Surface mSurface;
16699
Romain Guyb051e892010-09-28 19:09:36 -070016700 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016701 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016702 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016703
Romain Guy7e4e5612012-03-05 14:37:29 -080016704 boolean mScreenOn;
16705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016706 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016707 * Scale factor used by the compatibility mode
16708 */
16709 float mApplicationScale;
16710
16711 /**
16712 * Indicates whether the application is in compatibility mode
16713 */
16714 boolean mScalingRequired;
16715
16716 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016717 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016718 */
16719 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016720
Dianne Hackborn63042d62011-01-26 18:56:29 -080016721 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016722 * Left position of this view's window
16723 */
16724 int mWindowLeft;
16725
16726 /**
16727 * Top position of this view's window
16728 */
16729 int mWindowTop;
16730
16731 /**
Adam Powell26153a32010-11-08 15:22:27 -080016732 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016733 */
Adam Powell26153a32010-11-08 15:22:27 -080016734 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016735
16736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016737 * For windows that are full-screen but using insets to layout inside
16738 * of the screen decorations, these are the current insets for the
16739 * content of the window.
16740 */
16741 final Rect mContentInsets = new Rect();
16742
16743 /**
16744 * For windows that are full-screen but using insets to layout inside
16745 * of the screen decorations, these are the current insets for the
16746 * actual visible parts of the window.
16747 */
16748 final Rect mVisibleInsets = new Rect();
16749
16750 /**
16751 * The internal insets given by this window. This value is
16752 * supplied by the client (through
16753 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16754 * be given to the window manager when changed to be used in laying
16755 * out windows behind it.
16756 */
16757 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16758 = new ViewTreeObserver.InternalInsetsInfo();
16759
16760 /**
16761 * All views in the window's hierarchy that serve as scroll containers,
16762 * used to determine if the window can be resized or must be panned
16763 * to adjust for a soft input area.
16764 */
16765 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16766
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016767 final KeyEvent.DispatcherState mKeyDispatchState
16768 = new KeyEvent.DispatcherState();
16769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016770 /**
16771 * Indicates whether the view's window currently has the focus.
16772 */
16773 boolean mHasWindowFocus;
16774
16775 /**
16776 * The current visibility of the window.
16777 */
16778 int mWindowVisibility;
16779
16780 /**
16781 * Indicates the time at which drawing started to occur.
16782 */
16783 long mDrawingTime;
16784
16785 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016786 * Indicates whether or not ignoring the DIRTY_MASK flags.
16787 */
16788 boolean mIgnoreDirtyState;
16789
16790 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016791 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16792 * to avoid clearing that flag prematurely.
16793 */
16794 boolean mSetIgnoreDirtyState = false;
16795
16796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016797 * Indicates whether the view's window is currently in touch mode.
16798 */
16799 boolean mInTouchMode;
16800
16801 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016802 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016803 * the next time it performs a traversal
16804 */
16805 boolean mRecomputeGlobalAttributes;
16806
16807 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016808 * Always report new attributes at next traversal.
16809 */
16810 boolean mForceReportNewAttributes;
16811
16812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016813 * Set during a traveral if any views want to keep the screen on.
16814 */
16815 boolean mKeepScreenOn;
16816
16817 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016818 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16819 */
16820 int mSystemUiVisibility;
16821
16822 /**
16823 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16824 * attached.
16825 */
16826 boolean mHasSystemUiListeners;
16827
16828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016829 * Set if the visibility of any views has changed.
16830 */
16831 boolean mViewVisibilityChanged;
16832
16833 /**
16834 * Set to true if a view has been scrolled.
16835 */
16836 boolean mViewScrollChanged;
16837
16838 /**
16839 * Global to the view hierarchy used as a temporary for dealing with
16840 * x/y points in the transparent region computations.
16841 */
16842 final int[] mTransparentLocation = new int[2];
16843
16844 /**
16845 * Global to the view hierarchy used as a temporary for dealing with
16846 * x/y points in the ViewGroup.invalidateChild implementation.
16847 */
16848 final int[] mInvalidateChildLocation = new int[2];
16849
Chet Haasec3aa3612010-06-17 08:50:37 -070016850
16851 /**
16852 * Global to the view hierarchy used as a temporary for dealing with
16853 * x/y location when view is transformed.
16854 */
16855 final float[] mTmpTransformLocation = new float[2];
16856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016857 /**
16858 * The view tree observer used to dispatch global events like
16859 * layout, pre-draw, touch mode change, etc.
16860 */
16861 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16862
16863 /**
16864 * A Canvas used by the view hierarchy to perform bitmap caching.
16865 */
16866 Canvas mCanvas;
16867
16868 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016869 * The view root impl.
16870 */
16871 final ViewRootImpl mViewRootImpl;
16872
16873 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016874 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016875 * handler can be used to pump events in the UI events queue.
16876 */
16877 final Handler mHandler;
16878
16879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016880 * Temporary for use in computing invalidate rectangles while
16881 * calling up the hierarchy.
16882 */
16883 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016884
16885 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016886 * Temporary for use in computing hit areas with transformed views
16887 */
16888 final RectF mTmpTransformRect = new RectF();
16889
16890 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016891 * Temporary list for use in collecting focusable descendents of a view.
16892 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016893 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016895 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016896 * The id of the window for accessibility purposes.
16897 */
16898 int mAccessibilityWindowId = View.NO_ID;
16899
16900 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016901 * Whether to ingore not exposed for accessibility Views when
16902 * reporting the view tree to accessibility services.
16903 */
16904 boolean mIncludeNotImportantViews;
16905
16906 /**
16907 * The drawable for highlighting accessibility focus.
16908 */
16909 Drawable mAccessibilityFocusDrawable;
16910
16911 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070016912 * Show where the margins, bounds and layout bounds are for each view.
16913 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070016914 final boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070016915
16916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016917 * Creates a new set of attachment information with the specified
16918 * events handler and thread.
16919 *
16920 * @param handler the events handler the view must use
16921 */
16922 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016923 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016924 mSession = session;
16925 mWindow = window;
16926 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016927 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016928 mHandler = handler;
16929 mRootCallbacks = effectPlayer;
16930 }
16931 }
16932
16933 /**
16934 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16935 * is supported. This avoids keeping too many unused fields in most
16936 * instances of View.</p>
16937 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016938 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016939
Mike Cleronf116bf82009-09-27 19:14:12 -070016940 /**
16941 * Scrollbars are not visible
16942 */
16943 public static final int OFF = 0;
16944
16945 /**
16946 * Scrollbars are visible
16947 */
16948 public static final int ON = 1;
16949
16950 /**
16951 * Scrollbars are fading away
16952 */
16953 public static final int FADING = 2;
16954
16955 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016957 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016958 public int scrollBarDefaultDelayBeforeFade;
16959 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016960
16961 public int scrollBarSize;
16962 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016963 public float[] interpolatorValues;
16964 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016965
16966 public final Paint paint;
16967 public final Matrix matrix;
16968 public Shader shader;
16969
Mike Cleronf116bf82009-09-27 19:14:12 -070016970 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16971
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016972 private static final float[] OPAQUE = { 255 };
16973 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016974
Mike Cleronf116bf82009-09-27 19:14:12 -070016975 /**
16976 * When fading should start. This time moves into the future every time
16977 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16978 */
16979 public long fadeStartTime;
16980
16981
16982 /**
16983 * The current state of the scrollbars: ON, OFF, or FADING
16984 */
16985 public int state = OFF;
16986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016987 private int mLastColor;
16988
Mike Cleronf116bf82009-09-27 19:14:12 -070016989 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016990 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16991 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016992 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16993 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016994
16995 paint = new Paint();
16996 matrix = new Matrix();
16997 // use use a height of 1, and then wack the matrix each time we
16998 // actually use it.
16999 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017001 paint.setShader(shader);
17002 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017003 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017004 }
Romain Guy8506ab42009-06-11 17:35:47 -070017005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017006 public void setFadeColor(int color) {
17007 if (color != 0 && color != mLastColor) {
17008 mLastColor = color;
17009 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017010
Romain Guye55e1a72009-08-27 10:42:26 -070017011 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17012 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017014 paint.setShader(shader);
17015 // Restore the default transfer mode (src_over)
17016 paint.setXfermode(null);
17017 }
17018 }
Joe Malin32736f02011-01-19 16:14:20 -080017019
Mike Cleronf116bf82009-09-27 19:14:12 -070017020 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017021 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017022 if (now >= fadeStartTime) {
17023
17024 // the animation fades the scrollbars out by changing
17025 // the opacity (alpha) from fully opaque to fully
17026 // transparent
17027 int nextFrame = (int) now;
17028 int framesCount = 0;
17029
17030 Interpolator interpolator = scrollBarInterpolator;
17031
17032 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017033 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017034
17035 // End transparent
17036 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017037 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017038
17039 state = FADING;
17040
17041 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017042 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017043 }
17044 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017045 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017046
Svetoslav Ganova0156172011-06-26 17:55:44 -070017047 /**
17048 * Resuable callback for sending
17049 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17050 */
17051 private class SendViewScrolledAccessibilityEvent implements Runnable {
17052 public volatile boolean mIsPending;
17053
17054 public void run() {
17055 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17056 mIsPending = false;
17057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017058 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017059
17060 /**
17061 * <p>
17062 * This class represents a delegate that can be registered in a {@link View}
17063 * to enhance accessibility support via composition rather via inheritance.
17064 * It is specifically targeted to widget developers that extend basic View
17065 * classes i.e. classes in package android.view, that would like their
17066 * applications to be backwards compatible.
17067 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017068 * <div class="special reference">
17069 * <h3>Developer Guides</h3>
17070 * <p>For more information about making applications accessible, read the
17071 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17072 * developer guide.</p>
17073 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017074 * <p>
17075 * A scenario in which a developer would like to use an accessibility delegate
17076 * is overriding a method introduced in a later API version then the minimal API
17077 * version supported by the application. For example, the method
17078 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17079 * in API version 4 when the accessibility APIs were first introduced. If a
17080 * developer would like his application to run on API version 4 devices (assuming
17081 * all other APIs used by the application are version 4 or lower) and take advantage
17082 * of this method, instead of overriding the method which would break the application's
17083 * backwards compatibility, he can override the corresponding method in this
17084 * delegate and register the delegate in the target View if the API version of
17085 * the system is high enough i.e. the API version is same or higher to the API
17086 * version that introduced
17087 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17088 * </p>
17089 * <p>
17090 * Here is an example implementation:
17091 * </p>
17092 * <code><pre><p>
17093 * if (Build.VERSION.SDK_INT >= 14) {
17094 * // If the API version is equal of higher than the version in
17095 * // which onInitializeAccessibilityNodeInfo was introduced we
17096 * // register a delegate with a customized implementation.
17097 * View view = findViewById(R.id.view_id);
17098 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17099 * public void onInitializeAccessibilityNodeInfo(View host,
17100 * AccessibilityNodeInfo info) {
17101 * // Let the default implementation populate the info.
17102 * super.onInitializeAccessibilityNodeInfo(host, info);
17103 * // Set some other information.
17104 * info.setEnabled(host.isEnabled());
17105 * }
17106 * });
17107 * }
17108 * </code></pre></p>
17109 * <p>
17110 * This delegate contains methods that correspond to the accessibility methods
17111 * in View. If a delegate has been specified the implementation in View hands
17112 * off handling to the corresponding method in this delegate. The default
17113 * implementation the delegate methods behaves exactly as the corresponding
17114 * method in View for the case of no accessibility delegate been set. Hence,
17115 * to customize the behavior of a View method, clients can override only the
17116 * corresponding delegate method without altering the behavior of the rest
17117 * accessibility related methods of the host view.
17118 * </p>
17119 */
17120 public static class AccessibilityDelegate {
17121
17122 /**
17123 * Sends an accessibility event of the given type. If accessibility is not
17124 * enabled this method has no effect.
17125 * <p>
17126 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17127 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17128 * been set.
17129 * </p>
17130 *
17131 * @param host The View hosting the delegate.
17132 * @param eventType The type of the event to send.
17133 *
17134 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17135 */
17136 public void sendAccessibilityEvent(View host, int eventType) {
17137 host.sendAccessibilityEventInternal(eventType);
17138 }
17139
17140 /**
17141 * Sends an accessibility event. This method behaves exactly as
17142 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17143 * empty {@link AccessibilityEvent} and does not perform a check whether
17144 * accessibility is enabled.
17145 * <p>
17146 * The default implementation behaves as
17147 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17148 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17149 * the case of no accessibility delegate been set.
17150 * </p>
17151 *
17152 * @param host The View hosting the delegate.
17153 * @param event The event to send.
17154 *
17155 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17156 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17157 */
17158 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17159 host.sendAccessibilityEventUncheckedInternal(event);
17160 }
17161
17162 /**
17163 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17164 * to its children for adding their text content to the event.
17165 * <p>
17166 * The default implementation behaves as
17167 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17168 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17169 * the case of no accessibility delegate been set.
17170 * </p>
17171 *
17172 * @param host The View hosting the delegate.
17173 * @param event The event.
17174 * @return True if the event population was completed.
17175 *
17176 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17177 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17178 */
17179 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17180 return host.dispatchPopulateAccessibilityEventInternal(event);
17181 }
17182
17183 /**
17184 * Gives a chance to the host View to populate the accessibility event with its
17185 * text content.
17186 * <p>
17187 * The default implementation behaves as
17188 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17189 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17190 * the case of no accessibility delegate been set.
17191 * </p>
17192 *
17193 * @param host The View hosting the delegate.
17194 * @param event The accessibility event which to populate.
17195 *
17196 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17197 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17198 */
17199 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17200 host.onPopulateAccessibilityEventInternal(event);
17201 }
17202
17203 /**
17204 * Initializes an {@link AccessibilityEvent} with information about the
17205 * the host View which is the event source.
17206 * <p>
17207 * The default implementation behaves as
17208 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17209 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17210 * the case of no accessibility delegate been set.
17211 * </p>
17212 *
17213 * @param host The View hosting the delegate.
17214 * @param event The event to initialize.
17215 *
17216 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17217 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17218 */
17219 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17220 host.onInitializeAccessibilityEventInternal(event);
17221 }
17222
17223 /**
17224 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17225 * <p>
17226 * The default implementation behaves as
17227 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17228 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17229 * the case of no accessibility delegate been set.
17230 * </p>
17231 *
17232 * @param host The View hosting the delegate.
17233 * @param info The instance to initialize.
17234 *
17235 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17236 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17237 */
17238 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17239 host.onInitializeAccessibilityNodeInfoInternal(info);
17240 }
17241
17242 /**
17243 * Called when a child of the host View has requested sending an
17244 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17245 * to augment the event.
17246 * <p>
17247 * The default implementation behaves as
17248 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17249 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17250 * the case of no accessibility delegate been set.
17251 * </p>
17252 *
17253 * @param host The View hosting the delegate.
17254 * @param child The child which requests sending the event.
17255 * @param event The event to be sent.
17256 * @return True if the event should be sent
17257 *
17258 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17259 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17260 */
17261 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17262 AccessibilityEvent event) {
17263 return host.onRequestSendAccessibilityEventInternal(child, event);
17264 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017265
17266 /**
17267 * Gets the provider for managing a virtual view hierarchy rooted at this View
17268 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17269 * that explore the window content.
17270 * <p>
17271 * The default implementation behaves as
17272 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17273 * the case of no accessibility delegate been set.
17274 * </p>
17275 *
17276 * @return The provider.
17277 *
17278 * @see AccessibilityNodeProvider
17279 */
17280 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17281 return null;
17282 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017284}